@@ -294,11 +294,24 @@ static void drm_mode_object_put(struct drm_device *dev,
294294 mutex_unlock (& dev -> mode_config .idr_mutex );
295295}
296296
297+ /**
298+ * drm_mode_object_find - look up a drm object with static lifetime
299+ * @dev: drm device
300+ * @id: id of the mode object
301+ * @type: type of the mode object
302+ *
303+ * Note that framebuffers cannot be looked up with this functions - since those
304+ * are reference counted, they need special treatment.
305+ */
297306struct drm_mode_object * drm_mode_object_find (struct drm_device * dev ,
298307 uint32_t id , uint32_t type )
299308{
300309 struct drm_mode_object * obj = NULL ;
301310
311+ /* Framebuffers are reference counted and need their own lookup
312+ * function.*/
313+ WARN_ON (type == DRM_MODE_OBJECT_FB );
314+
302315 mutex_lock (& dev -> mode_config .idr_mutex );
303316 obj = idr_find (& dev -> mode_config .crtc_idr , id );
304317 if (!obj || (obj -> type != type ) || (obj -> id != id ))
@@ -358,6 +371,40 @@ static void drm_framebuffer_free(struct kref *kref)
358371 fb -> funcs -> destroy (fb );
359372}
360373
374+ /**
375+ * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
376+ * @dev: drm device
377+ * @id: id of the fb object
378+ *
379+ * If successful, this grabs an additional reference to the framebuffer -
380+ * callers need to make sure to eventually unreference the returned framebuffer
381+ * again.
382+ */
383+ struct drm_framebuffer * drm_framebuffer_lookup (struct drm_device * dev ,
384+ uint32_t id )
385+ {
386+ struct drm_mode_object * obj = NULL ;
387+ struct drm_framebuffer * fb ;
388+
389+ mutex_lock (& dev -> mode_config .fb_lock );
390+
391+ mutex_lock (& dev -> mode_config .idr_mutex );
392+ obj = idr_find (& dev -> mode_config .crtc_idr , id );
393+ if (!obj || (obj -> type != DRM_MODE_OBJECT_FB ) || (obj -> id != id ))
394+ fb = NULL ;
395+ else
396+ fb = obj_to_fb (obj );
397+ mutex_unlock (& dev -> mode_config .idr_mutex );
398+
399+ if (fb )
400+ kref_get (& fb -> refcount );
401+
402+ mutex_unlock (& dev -> mode_config .fb_lock );
403+
404+ return fb ;
405+ }
406+ EXPORT_SYMBOL (drm_framebuffer_lookup );
407+
361408/**
362409 * drm_framebuffer_unreference - unref a framebuffer
363410 * @fb: framebuffer to unref
@@ -1788,17 +1835,15 @@ int drm_mode_setplane(struct drm_device *dev, void *data,
17881835 }
17891836 crtc = obj_to_crtc (obj );
17901837
1791- mutex_lock (& dev -> mode_config .fb_lock );
1792- obj = drm_mode_object_find (dev , plane_req -> fb_id ,
1793- DRM_MODE_OBJECT_FB );
1794- mutex_unlock (& dev -> mode_config .fb_lock );
1795- if (!obj ) {
1838+ fb = drm_framebuffer_lookup (dev , plane_req -> fb_id );
1839+ if (!fb ) {
17961840 DRM_DEBUG_KMS ("Unknown framebuffer ID %d\n" ,
17971841 plane_req -> fb_id );
17981842 ret = - ENOENT ;
17991843 goto out ;
18001844 }
1801- fb = obj_to_fb (obj );
1845+ /* fb is protect by the mode_config lock, so drop the ref immediately */
1846+ drm_framebuffer_unreference (fb );
18021847
18031848 /* Check whether this plane supports the fb pixel format. */
18041849 for (i = 0 ; i < plane -> format_count ; i ++ )
@@ -1933,17 +1978,16 @@ int drm_mode_setcrtc(struct drm_device *dev, void *data,
19331978 }
19341979 fb = crtc -> fb ;
19351980 } else {
1936- mutex_lock (& dev -> mode_config .fb_lock );
1937- obj = drm_mode_object_find (dev , crtc_req -> fb_id ,
1938- DRM_MODE_OBJECT_FB );
1939- mutex_unlock (& dev -> mode_config .fb_lock );
1940- if (!obj ) {
1981+ fb = drm_framebuffer_lookup (dev , crtc_req -> fb_id );
1982+ if (!fb ) {
19411983 DRM_DEBUG_KMS ("Unknown FB ID%d\n" ,
19421984 crtc_req -> fb_id );
19431985 ret = - EINVAL ;
19441986 goto out ;
19451987 }
1946- fb = obj_to_fb (obj );
1988+ /* fb is protect by the mode_config lock, so drop the
1989+ * ref immediately */
1990+ drm_framebuffer_unreference (fb );
19471991 }
19481992
19491993 mode = drm_mode_create (dev );
@@ -2392,7 +2436,6 @@ int drm_mode_addfb2(struct drm_device *dev,
23922436int drm_mode_rmfb (struct drm_device * dev ,
23932437 void * data , struct drm_file * file_priv )
23942438{
2395- struct drm_mode_object * obj ;
23962439 struct drm_framebuffer * fb = NULL ;
23972440 struct drm_framebuffer * fbl = NULL ;
23982441 uint32_t * id = data ;
@@ -2403,16 +2446,13 @@ int drm_mode_rmfb(struct drm_device *dev,
24032446 return - EINVAL ;
24042447
24052448 drm_modeset_lock_all (dev );
2406- mutex_lock (& dev -> mode_config .fb_lock );
2407- obj = drm_mode_object_find (dev , * id , DRM_MODE_OBJECT_FB );
2408- /* TODO check that we really get a framebuffer back. */
2409- if (!obj ) {
2410- mutex_unlock (& dev -> mode_config .fb_lock );
2449+ fb = drm_framebuffer_lookup (dev , * id );
2450+ if (!fb ) {
24112451 ret = - EINVAL ;
24122452 goto out ;
24132453 }
2414- fb = obj_to_fb ( obj );
2415- mutex_unlock ( & dev -> mode_config . fb_lock );
2454+ /* fb is protect by the mode_config lock, so drop the ref immediately */
2455+ drm_framebuffer_unreference ( fb );
24162456
24172457 mutex_lock (& file_priv -> fbs_lock );
24182458 list_for_each_entry (fbl , & file_priv -> fbs , filp_head )
@@ -2451,22 +2491,20 @@ int drm_mode_getfb(struct drm_device *dev,
24512491 void * data , struct drm_file * file_priv )
24522492{
24532493 struct drm_mode_fb_cmd * r = data ;
2454- struct drm_mode_object * obj ;
24552494 struct drm_framebuffer * fb ;
24562495 int ret = 0 ;
24572496
24582497 if (!drm_core_check_feature (dev , DRIVER_MODESET ))
24592498 return - EINVAL ;
24602499
24612500 drm_modeset_lock_all (dev );
2462- mutex_lock (& dev -> mode_config .fb_lock );
2463- obj = drm_mode_object_find (dev , r -> fb_id , DRM_MODE_OBJECT_FB );
2464- mutex_unlock (& dev -> mode_config .fb_lock );
2465- if (!obj ) {
2501+ fb = drm_framebuffer_lookup (dev , r -> fb_id );
2502+ if (!fb ) {
24662503 ret = - EINVAL ;
24672504 goto out ;
24682505 }
2469- fb = obj_to_fb (obj );
2506+ /* fb is protect by the mode_config lock, so drop the ref immediately */
2507+ drm_framebuffer_unreference (fb );
24702508
24712509 r -> height = fb -> height ;
24722510 r -> width = fb -> width ;
@@ -2489,7 +2527,6 @@ int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
24892527 struct drm_clip_rect __user * clips_ptr ;
24902528 struct drm_clip_rect * clips = NULL ;
24912529 struct drm_mode_fb_dirty_cmd * r = data ;
2492- struct drm_mode_object * obj ;
24932530 struct drm_framebuffer * fb ;
24942531 unsigned flags ;
24952532 int num_clips ;
@@ -2499,14 +2536,13 @@ int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
24992536 return - EINVAL ;
25002537
25012538 drm_modeset_lock_all (dev );
2502- mutex_lock (& dev -> mode_config .fb_lock );
2503- obj = drm_mode_object_find (dev , r -> fb_id , DRM_MODE_OBJECT_FB );
2504- mutex_unlock (& dev -> mode_config .fb_lock );
2505- if (!obj ) {
2539+ fb = drm_framebuffer_lookup (dev , r -> fb_id );
2540+ if (!fb ) {
25062541 ret = - EINVAL ;
25072542 goto out_err1 ;
25082543 }
2509- fb = obj_to_fb (obj );
2544+ /* fb is protect by the mode_config lock, so drop the ref immediately */
2545+ drm_framebuffer_unreference (fb );
25102546
25112547 num_clips = r -> num_clips ;
25122548 clips_ptr = (struct drm_clip_rect __user * )(unsigned long )r -> clips_ptr ;
@@ -3586,12 +3622,11 @@ int drm_mode_page_flip_ioctl(struct drm_device *dev,
35863622 if (crtc -> funcs -> page_flip == NULL )
35873623 goto out ;
35883624
3589- mutex_lock (& dev -> mode_config .fb_lock );
3590- obj = drm_mode_object_find (dev , page_flip -> fb_id , DRM_MODE_OBJECT_FB );
3591- mutex_unlock (& dev -> mode_config .fb_lock );
3592- if (!obj )
3625+ fb = drm_framebuffer_lookup (dev , page_flip -> fb_id );
3626+ if (!fb )
35933627 goto out ;
3594- fb = obj_to_fb (obj );
3628+ /* fb is protect by the mode_config lock, so drop the ref immediately */
3629+ drm_framebuffer_unreference (fb );
35953630
35963631 hdisplay = crtc -> mode .hdisplay ;
35973632 vdisplay = crtc -> mode .vdisplay ;
0 commit comments