On Fri, Apr 15, 2016 at 03:10:36PM +1000, Dave Airlie wrote:
> From: Dave Airlie
>
> This is the initial code to add references to some mode objects.
> In the future we need to start reference counting connectors so
> firstly I want to reorganise the code so the framebuffer ref counting
> uses the same paths.
>
> This patch shouldn't change any functionality, just moves the kref.
>
> Signed-off-by: Dave Airlie
> ---
> drivers/gpu/drm/drm_crtc.c | 72
> --
> include/drm/drm_crtc.h | 20 ++---
> 2 files changed, 54 insertions(+), 38 deletions(-)
>
> diff --git a/drivers/gpu/drm/drm_crtc.c b/drivers/gpu/drm/drm_crtc.c
> index 8616737..75a45e9 100644
> --- a/drivers/gpu/drm/drm_crtc.c
> +++ b/drivers/gpu/drm/drm_crtc.c
> @@ -275,7 +275,8 @@ EXPORT_SYMBOL(drm_get_format_name);
> static int drm_mode_object_get_reg(struct drm_device *dev,
> struct drm_mode_object *obj,
> uint32_t obj_type,
> -bool register_obj)
> +bool register_obj,
> +void (*obj_free_cb)(struct kref *kref))
> {
> int ret;
>
> @@ -288,6 +289,10 @@ static int drm_mode_object_get_reg(struct drm_device
> *dev,
>*/
> obj->id = ret;
> obj->type = obj_type;
> + if (obj_free_cb) {
> + obj->free_cb = obj_free_cb;
> + kref_init(>refcount);
> + }
> }
> mutex_unlock(>mode_config.idr_mutex);
>
> @@ -311,7 +316,7 @@ static int drm_mode_object_get_reg(struct drm_device *dev,
> int drm_mode_object_get(struct drm_device *dev,
> struct drm_mode_object *obj, uint32_t obj_type)
> {
> - return drm_mode_object_get_reg(dev, obj, obj_type, true);
> + return drm_mode_object_get_reg(dev, obj, obj_type, true, NULL);
> }
>
> static void drm_mode_object_register(struct drm_device *dev,
> @@ -389,10 +394,35 @@ struct drm_mode_object *drm_mode_object_find(struct
> drm_device *dev,
> }
> EXPORT_SYMBOL(drm_mode_object_find);
>
Kerneldoc for this one would be nice.
> +void drm_mode_object_unreference(struct drm_mode_object *obj)
> +{
> + if (obj->free_cb) {
> + DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id,
> atomic_read(>refcount.refcount));
> + kref_put(>refcount, obj->free_cb);
> + }
> +}
> +EXPORT_SYMBOL(drm_mode_object_unreference);
> +
> +/**
> + * drm_mode_object_reference - incr the fb refcnt
> + * @obj: mode_object
> + *
> + * This function operates only on refcounted objects.
> + * This functions increments the object's refcount.
> + */
> +void drm_mode_object_reference(struct drm_mode_object *obj)
> +{
> + if (obj->free_cb) {
> + DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id,
> atomic_read(>refcount.refcount));
> + kref_get(>refcount);
> + }
> +}
> +EXPORT_SYMBOL(drm_mode_object_reference);
> +
> static void drm_framebuffer_free(struct kref *kref)
> {
> struct drm_framebuffer *fb =
> - container_of(kref, struct drm_framebuffer, refcount);
> + container_of(kref, struct drm_framebuffer,
> base.refcount);
> struct drm_device *dev = fb->dev;
>
> /*
> @@ -430,12 +460,12 @@ int drm_framebuffer_init(struct drm_device *dev, struct
> drm_framebuffer *fb,
> int ret;
>
> mutex_lock(>mode_config.fb_lock);
> - kref_init(>refcount);
> INIT_LIST_HEAD(>filp_head);
> fb->dev = dev;
> fb->funcs = funcs;
>
> - ret = drm_mode_object_get(dev, >base, DRM_MODE_OBJECT_FB);
> + ret = drm_mode_object_get_reg(dev, >base, DRM_MODE_OBJECT_FB,
> + true, drm_framebuffer_free);
> if (ret)
> goto out;
>
> @@ -482,7 +512,7 @@ struct drm_framebuffer *drm_framebuffer_lookup(struct
> drm_device *dev,
> mutex_lock(>mode_config.fb_lock);
> fb = __drm_framebuffer_lookup(dev, id);
> if (fb) {
> - if (!kref_get_unless_zero(>refcount))
> + if (!kref_get_unless_zero(>base.refcount))
> fb = NULL;
> }
> mutex_unlock(>mode_config.fb_lock);
> @@ -492,32 +522,6 @@ struct drm_framebuffer *drm_framebuffer_lookup(struct
> drm_device *dev,
> EXPORT_SYMBOL(drm_framebuffer_lookup);
>
> /**
> - * drm_framebuffer_unreference - unref a framebuffer
> - * @fb: framebuffer to unref
> - *
> - * This functions decrements the fb's refcount and frees it if it drops to
> zero.
> - */
> -void drm_framebuffer_unreference(struct drm_framebuffer *fb)
> -{
> - DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id,
> atomic_read(>refcount.refcount));
> - kref_put(>refcount, drm_framebuffer_free);
> -}
> -EXPORT_SYMBOL(drm_framebuffer_unreference);
> -
> -/**
> - * drm_framebuffer_reference - incr the fb refcnt
> - * @fb: framebuffer
> - *
> -