linux-yocto/include/drm
Simona Vetter 2f4df5d07c drm/gem: Fix race in drm_gem_handle_create_tail()
commit bd46cece51 upstream.

Object creation is a careful dance where we must guarantee that the
object is fully constructed before it is visible to other threads, and
GEM buffer objects are no difference.

Final publishing happens by calling drm_gem_handle_create(). After
that the only allowed thing to do is call drm_gem_object_put() because
a concurrent call to the GEM_CLOSE ioctl with a correctly guessed id
(which is trivial since we have a linear allocator) can already tear
down the object again.

Luckily most drivers get this right, the very few exceptions I've
pinged the relevant maintainers for. Unfortunately we also need
drm_gem_handle_create() when creating additional handles for an
already existing object (e.g. GETFB ioctl or the various bo import
ioctl), and hence we cannot have a drm_gem_handle_create_and_put() as
the only exported function to stop these issues from happening.

Now unfortunately the implementation of drm_gem_handle_create() isn't
living up to standards: It does correctly finishe object
initialization at the global level, and hence is safe against a
concurrent tear down. But it also sets up the file-private aspects of
the handle, and that part goes wrong: We fully register the object in
the drm_file.object_idr before calling drm_vma_node_allow() or
obj->funcs->open, which opens up races against concurrent removal of
that handle in drm_gem_handle_delete().

Fix this with the usual two-stage approach of first reserving the
handle id, and then only registering the object after we've completed
the file-private setup.

Jacek reported this with a testcase of concurrently calling GEM_CLOSE
on a freshly-created object (which also destroys the object), but it
should be possible to hit this with just additional handles created
through import or GETFB without completed destroying the underlying
object with the concurrent GEM_CLOSE ioctl calls.

Note that the close-side of this race was fixed in f6cd7daecf ("drm:
Release driver references to handle before making it available
again"), which means a cool 9 years have passed until someone noticed
that we need to make this symmetry or there's still gaps left :-/
Without the 2-stage close approach we'd still have a race, therefore
that's an integral part of this bugfix.

More importantly, this means we can have NULL pointers behind
allocated id in our drm_file.object_idr. We need to check for that
now:

- drm_gem_handle_delete() checks for ERR_OR_NULL already

- drm_gem.c:object_lookup() also chekcs for NULL

- drm_gem_release() should never be called if there's another thread
  still existing that could call into an IOCTL that creates a new
  handle, so cannot race. For paranoia I added a NULL check to
  drm_gem_object_release_handle() though.

- most drivers (etnaviv, i915, msm) are find because they use
  idr_find(), which maps both ENOENT and NULL to NULL.

- drivers using idr_for_each_entry() should also be fine, because
  idr_get_next does filter out NULL entries and continues the
  iteration.

- The same holds for drm_show_memory_stats().

v2: Use drm_WARN_ON (Thomas)

Reported-by: Jacek Lawrynowicz <jacek.lawrynowicz@linux.intel.com>
Tested-by: Jacek Lawrynowicz <jacek.lawrynowicz@linux.intel.com>
Reviewed-by: Thomas Zimmermann <tzimmermann@suse.de>
Cc: stable@vger.kernel.org
Cc: Jacek Lawrynowicz <jacek.lawrynowicz@linux.intel.com>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Maxime Ripard <mripard@kernel.org>
Cc: Thomas Zimmermann <tzimmermann@suse.de>
Cc: David Airlie <airlied@gmail.com>
Cc: Simona Vetter <simona@ffwll.ch>
Signed-off-by: Simona Vetter <simona.vetter@intel.com>
Signed-off-by: Simona Vetter <simona.vetter@ffwll.ch>
Link: https://patchwork.freedesktop.org/patch/msgid/20250707151814.603897-1-simona.vetter@ffwll.ch
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2025-07-17 18:30:49 +02:00
..
bridge drm: bridge: dw-hdmi: Attach to next bridge if available 2021-07-28 16:33:13 +03:00
i2c
ttm drm/ttm: add ttm_resource_fini v2 2024-03-26 18:21:25 -04:00
amd_asic_type.h
drm_aperture.h
drm_atomic_helper.h
drm_atomic_state_helper.h
drm_atomic_uapi.h
drm_atomic.h drm/atomic: clarify the rules around drm_atomic_state->allow_modeset 2025-06-04 14:38:04 +02:00
drm_audio_component.h
drm_auth.h drm: clarify usage of drm leases 2021-07-29 09:12:01 +02:00
drm_blend.h
drm_bridge_connector.h
drm_bridge.h drm/bridge: Fix typo in post_disable() description 2024-01-25 14:52:41 -08:00
drm_cache.h
drm_client.h
drm_color_mgmt.h drm: using mul_u32_u32() requires linux/math64.h 2024-02-23 08:54:52 +01:00
drm_connector.h drm/connector: Fix typo in documentation 2022-04-08 14:24:12 +02:00
drm_crtc_helper.h
drm_crtc.h
drm_damage_helper.h drm/plane: Move drm_plane_enable_fb_damage_clips into core 2021-07-27 12:21:22 +02:00
drm_debugfs_crc.h
drm_debugfs.h
drm_device.h drm: IRQ midlayer is now legacy 2021-08-10 20:14:01 +02:00
drm_displayid.h
drm_dp_aux_bus.h
drm_dp_dual_mode_helper.h
drm_dp_helper.h drm/display/dp: Fix the DP DSC Receiver cap size 2023-08-30 16:18:19 +02:00
drm_dp_mst_helper.h drm/dp_mst: Fix drm RAD print 2025-04-10 14:31:58 +02:00
drm_drv.h drm: IRQ midlayer is now legacy 2021-08-10 20:14:01 +02:00
drm_dsc.h
drm_edid.h drm: fix EDID struct for old ARM OABI format 2022-06-09 10:22:44 +02:00
drm_encoder_slave.h
drm_encoder.h
drm_fb_cma_helper.h
drm_fb_helper.h fbdev: Rename pagelist to pagereflist for deferred I/O 2024-02-23 08:55:13 +01:00
drm_file.h drm/gem: Fix race in drm_gem_handle_create_tail() 2025-07-17 18:30:49 +02:00
drm_fixed.h drm: Don't treat 0 as -1 in drm_fixp2int_ceil 2024-03-26 18:21:25 -04:00
drm_flip_work.h
drm_format_helper.h
drm_fourcc.h drm: Define DRM_FORMAT_MAX_PLANES 2021-08-02 16:41:20 +02:00
drm_framebuffer.h drm: Define DRM_FORMAT_MAX_PLANES 2021-08-02 16:41:20 +02:00
drm_gem_atomic_helper.h drm/gem: Provide offset-adjusted framebuffer BO mappings 2021-08-08 20:26:16 +02:00
drm_gem_cma_helper.h
drm_gem_framebuffer_helper.h drm/gem: Provide offset-adjusted framebuffer BO mappings 2021-08-08 20:26:16 +02:00
drm_gem_shmem_helper.h drm/shmem-helper: Pass GEM shmem object in public interfaces 2022-08-17 14:23:19 +02:00
drm_gem_ttm_helper.h
drm_gem_vram_helper.h
drm_gem.h drm: Drop drm_gem_object_put_locked() 2021-07-27 18:09:18 -07:00
drm_hashtab.h
drm_hdcp.h
drm_ioctl.h
drm_lease.h
drm_legacy.h drm: IRQ midlayer is now legacy 2021-08-10 20:14:01 +02:00
drm_managed.h
drm_mipi_dbi.h
drm_mipi_dsi.h drm/mipi-dsi: use correct return type for the DSC functions 2024-06-16 13:39:30 +02:00
drm_mm.h
drm_mode_config.h
drm_mode_object.h
drm_modes.h
drm_modeset_helper_vtables.h
drm_modeset_helper.h
drm_modeset_lock.h
drm_of.h
drm_panel.h
drm_pciids.h
drm_plane_helper.h
drm_plane.h drm/plane: Move drm_plane_enable_fb_damage_clips into core 2021-07-27 12:21:22 +02:00
drm_prime.h
drm_print.h drm/printer: Allow NULL data in devcoredump printer 2024-10-17 15:11:32 +02:00
drm_probe_helper.h drm/probe-helper: Create a HPD IRQ event helper for a single connector 2025-03-13 12:50:48 +01:00
drm_property.h drm: document drm_property_enum.value for bitfields 2021-07-26 10:08:22 +02:00
drm_rect.h drm/plane: remove drm_helper_get_plane_damage_clips 2021-07-27 12:21:22 +02:00
drm_scdc_helper.h
drm_self_refresh_helper.h
drm_simple_kms_helper.h drm/simple-kms: Support custom CRTC state 2021-08-08 20:14:08 +02:00
drm_syncobj.h
drm_sysfs.h
drm_util.h
drm_utils.h
drm_vblank_work.h
drm_vblank.h
drm_vma_manager.h
drm_writeback.h
gma_drm.h
gpu_scheduler.h
gud.h
i915_component.h
i915_drm.h
i915_mei_hdcp_interface.h
i915_pciids.h
intel_lpe_audio.h
intel-gtt.h
spsc_queue.h drm/sched: Increment job count before swapping tail spsc queue 2025-07-17 18:30:49 +02:00
task_barrier.h