Skip to content

Commit 786b99e

Browse files
committed
drm: create drm_framebuffer_lookup
And replace all fb lookups with it. Also add a WARN to drm_mode_object_find since that is now no longer the blessed interface to look up an fb. And add kerneldoc to both functions. This only updates all callsites, but immediately drops the acquired refence again. Hence all callers still rely on the fact that a mode fb can't disappear while they're holding the struct mutex. Subsequent patches will instate proper use of refcounts, and then rework the rmfb and unref code to no longer serialize fb destruction with the mode_config lock. We don't want that since otherwise a compositor might end up stalling for a few frames in rmfb. v2: Don't use kref_get_unless_zero - Greg KH doesn't like that kind of interface. Reviewed-by: Rob Clark <rob@ti.com> Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
1 parent 4b096ac commit 786b99e

3 files changed

Lines changed: 86 additions & 49 deletions

File tree

‎drivers/gpu/drm/drm_crtc.c‎

Lines changed: 72 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
*/
297306
struct 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,
23922436
int 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;

‎drivers/gpu/drm/vmwgfx/vmwgfx_ioctl.c‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ int vmw_present_ioctl(struct drm_device *dev, void *data,
131131
struct vmw_master *vmaster = vmw_master(file_priv->master);
132132
struct drm_vmw_rect __user *clips_ptr;
133133
struct drm_vmw_rect *clips = NULL;
134-
struct drm_mode_object *obj;
134+
struct drm_framebuffer *fb;
135135
struct vmw_framebuffer *vfb;
136136
struct vmw_resource *res;
137137
uint32_t num_clips;
@@ -165,15 +165,15 @@ int vmw_present_ioctl(struct drm_device *dev, void *data,
165165

166166
drm_modeset_lock_all(dev);
167167

168-
mutex_lock(&dev->mode_config.fb_lock);
169-
obj = drm_mode_object_find(dev, arg->fb_id, DRM_MODE_OBJECT_FB);
170-
mutex_unlock(&dev->mode_config.fb_lock);
171-
if (!obj) {
168+
fb = drm_framebuffer_lookup(dev, arg->fb_id);
169+
if (!fb) {
172170
DRM_ERROR("Invalid framebuffer id.\n");
173171
ret = -EINVAL;
174172
goto out_no_fb;
175173
}
176-
vfb = vmw_framebuffer_to_vfb(obj_to_fb(obj));
174+
/* fb is protect by the mode_config lock, so drop the ref immediately */
175+
drm_framebuffer_unreference(fb);
176+
vfb = vmw_framebuffer_to_vfb(fb);
177177

178178
ret = ttm_read_lock(&vmaster->lock, true);
179179
if (unlikely(ret != 0))
@@ -217,7 +217,7 @@ int vmw_present_readback_ioctl(struct drm_device *dev, void *data,
217217
struct vmw_master *vmaster = vmw_master(file_priv->master);
218218
struct drm_vmw_rect __user *clips_ptr;
219219
struct drm_vmw_rect *clips = NULL;
220-
struct drm_mode_object *obj;
220+
struct drm_framebuffer *fb;
221221
struct vmw_framebuffer *vfb;
222222
uint32_t num_clips;
223223
int ret;
@@ -250,16 +250,16 @@ int vmw_present_readback_ioctl(struct drm_device *dev, void *data,
250250

251251
drm_modeset_lock_all(dev);
252252

253-
mutex_lock(&dev->mode_config.fb_lock);
254-
obj = drm_mode_object_find(dev, arg->fb_id, DRM_MODE_OBJECT_FB);
255-
mutex_unlock(&dev->mode_config.fb_lock);
256-
if (!obj) {
253+
fb = drm_framebuffer_lookup(dev, arg->fb_id);
254+
if (!fb) {
257255
DRM_ERROR("Invalid framebuffer id.\n");
258256
ret = -EINVAL;
259257
goto out_no_fb;
260258
}
259+
/* fb is protect by the mode_config lock, so drop the ref immediately */
260+
drm_framebuffer_unreference(fb);
261261

262-
vfb = vmw_framebuffer_to_vfb(obj_to_fb(obj));
262+
vfb = vmw_framebuffer_to_vfb(fb);
263263
if (!vfb->dmabuf) {
264264
DRM_ERROR("Framebuffer not dmabuf backed.\n");
265265
ret = -EINVAL;

‎include/drm/drm_crtc.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,8 @@ extern void drm_framebuffer_set_object(struct drm_device *dev,
958958
extern int drm_framebuffer_init(struct drm_device *dev,
959959
struct drm_framebuffer *fb,
960960
const struct drm_framebuffer_funcs *funcs);
961+
extern struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
962+
uint32_t id);
961963
extern void drm_framebuffer_unreference(struct drm_framebuffer *fb);
962964
extern void drm_framebuffer_reference(struct drm_framebuffer *fb);
963965
extern void drm_framebuffer_remove(struct drm_framebuffer *fb);

0 commit comments

Comments
 (0)