From patchwork Tue Jul 31 16:20:21 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rob Clark X-Patchwork-Id: 10430 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id A25A424051 for ; Tue, 31 Jul 2012 16:20:40 +0000 (UTC) Received: from mail-gg0-f180.google.com (mail-gg0-f180.google.com [209.85.161.180]) by fiordland.canonical.com (Postfix) with ESMTP id 47C30A1812C for ; Tue, 31 Jul 2012 16:20:40 +0000 (UTC) Received: by ggnf1 with SMTP id f1so6128683ggn.11 for ; Tue, 31 Jul 2012 09:20:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-forwarded-to:x-forwarded-for:delivered-to:received-spf :dkim-signature:sender:from:to:cc:subject:date:message-id:x-mailer :x-gm-message-state; bh=oIGcqf+ZMKvyPR/JVLFmjqSS6K5jFfw35Z7LLTXTXd0=; b=KFavGlvUSjKy1wgP1Mo26Nkzkikt6v8tMH4jNcBYCBN6TWNARKvS/LgDb79IU67aiD zvtUCSwxQQ6nPZm57GdzhuTodIACd0BK5IF20t9o26QAOqYfZCg6/weHselB//Cy4uSL pxBDQRMLy7zOOAbhDOpHGn75sbKKOPel7qIeHUQIEYlhYZBNljExL4n+v4fPFWg6rBzh dGWXSHY2UwprqDUGbQ74+eTGsb5Xe11JZxjKp6ZSl+EV4YVxRqs97PYct5hlcWdZwB11 Dv7d+3l9It51JRKCgE7lh8EK3GJ7q8r5UvRGFVKJPid/QCyId0cdSTH5AS8ECpoT2hIr hWNA== Received: by 10.50.219.194 with SMTP id pq2mr1204650igc.25.1343751639310; Tue, 31 Jul 2012 09:20:39 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.50.87.40 with SMTP id u8csp153221igz; Tue, 31 Jul 2012 09:20:38 -0700 (PDT) Received: by 10.60.31.165 with SMTP id b5mr24061655oei.58.1343751637053; Tue, 31 Jul 2012 09:20:37 -0700 (PDT) Received: from mail-ob0-f178.google.com (mail-ob0-f178.google.com [209.85.214.178]) by mx.google.com with ESMTPS id e8si592290oee.15.2012.07.31.09.20.36 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 31 Jul 2012 09:20:37 -0700 (PDT) Received-SPF: pass (google.com: domain of robdclark@gmail.com designates 209.85.214.178 as permitted sender) client-ip=209.85.214.178; Authentication-Results: mx.google.com; spf=pass (google.com: domain of robdclark@gmail.com designates 209.85.214.178 as permitted sender) smtp.mail=robdclark@gmail.com; dkim=pass header.i=@gmail.com Received: by obbwd20 with SMTP id wd20so13991952obb.37 for ; Tue, 31 Jul 2012 09:20:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer; bh=oIGcqf+ZMKvyPR/JVLFmjqSS6K5jFfw35Z7LLTXTXd0=; b=z3372IuGG+cZ2FwKoYkUE8GRn003FOGaRTCP/dTw6N8POGWvba9f0UdWPGCash+kc/ +MI74NemC7odnsy2+YIo29UzwTwv5/x3FY5Br4skqLjLO8SobOd00AZ0mRCFsbO1H+nh 9CivBXKLsLl3aBSTQr6sU1yIQhJWKWw0bDs6rF3QjLTWul8Ak1GkXUe5Q0LBHVt2VvuZ 2/wOgtArC7iuP9AcEFEGiURm1wNOQMdXOGarfalT0ovjw81zUVNiUcW9SCa7Xn+mOkDP HI5t1bagIqw6r8vuyGSdgATAh8m57FudoxatgAHkvtrLHCb71CrDm/SOrWntoxxvrXn0 ck7A== Received: by 10.182.39.39 with SMTP id m7mr24432530obk.20.1343751636560; Tue, 31 Jul 2012 09:20:36 -0700 (PDT) Received: from localhost (ppp-70-129-143-140.dsl.rcsntx.swbell.net. [70.129.143.140]) by mx.google.com with ESMTPS id ql3sm380717obc.17.2012.07.31.09.20.35 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 31 Jul 2012 09:20:35 -0700 (PDT) Sender: Rob Clark From: Rob Clark To: dri-devel@lists.freedesktop.org, linux-omap@vger.kernel.org Cc: patches@linaro.org, Greg KH , Tomi Valkeinen , Andy Gross , Rob Clark Subject: [PATCH] drm: refcnt drm_framebuffer Date: Tue, 31 Jul 2012 11:20:21 -0500 Message-Id: <1343751621-1868-1-git-send-email-rob.clark@linaro.org> X-Mailer: git-send-email 1.7.9.5 X-Gm-Message-State: ALoCoQkz+Py6h3watifdrkqlCMyvZbd03SUYsT1GSQy3UZ1hxJcUqolsxVBNZI6jxYBJ1+xGDNhf From: Rob Clark This simplifies drm fb lifetime, and if the crtc/plane needs to hold a ref to the fb when disabling a pipe until the next vblank, this avoids the need to make disabling an overlay synchronous. This is a problem that shows up when userspace is using a drm plane to implement a hw cursor.. making overlay disable synchronous causes a performance problem when x11 is rapidly enabling/disabling the hw cursor. But not making it synchronous opens up a race condition for crashing if userspace turns around and immediately deletes the fb. Refcnt'ing the fb makes it possible to solve this problem. Signed-off-by: Rob Clark --- drivers/gpu/drm/drm_crtc.c | 38 ++++++++++++++++++++++++++--- drivers/gpu/drm/exynos/exynos_drm_fbdev.c | 4 +-- drivers/gpu/drm/i915/intel_display.c | 4 +-- include/drm/drm_crtc.h | 4 +++ 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/drm_crtc.c b/drivers/gpu/drm/drm_crtc.c index 08a7aa7..2f928a3 100644 --- a/drivers/gpu/drm/drm_crtc.c +++ b/drivers/gpu/drm/drm_crtc.c @@ -294,6 +294,8 @@ int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb, { int ret; + kref_init(&fb->refcount); + ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB); if (ret) return ret; @@ -307,6 +309,36 @@ int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb, } EXPORT_SYMBOL(drm_framebuffer_init); +static void drm_framebuffer_free(struct kref *kref) +{ + struct drm_framebuffer *fb = + container_of(kref, struct drm_framebuffer, refcount); + fb->funcs->destroy(fb); +} + +/** + * drm_framebuffer_unreference - unref a framebuffer + * + * LOCKING: + * Caller must hold mode config lock. + */ +void drm_framebuffer_unreference(struct drm_framebuffer *fb) +{ + struct drm_device *dev = fb->dev; + WARN_ON(!mutex_is_locked(&dev->mode_config.mutex)); + kref_put(&fb->refcount, drm_framebuffer_free); +} +EXPORT_SYMBOL(drm_framebuffer_unreference); + +/** + * drm_framebuffer_reference - incr the fb refcnt + */ +void drm_framebuffer_reference(struct drm_framebuffer *fb) +{ + kref_get(&fb->refcount); +} +EXPORT_SYMBOL(drm_framebuffer_reference); + /** * drm_framebuffer_cleanup - remove a framebuffer object * @fb: framebuffer to remove @@ -1031,7 +1063,7 @@ void drm_mode_config_cleanup(struct drm_device *dev) } list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) { - fb->funcs->destroy(fb); + drm_framebuffer_unreference(fb); } list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) { @@ -2339,7 +2371,7 @@ int drm_mode_rmfb(struct drm_device *dev, /* TODO unhock the destructor from the buffer object */ list_del(&fb->filp_head); - fb->funcs->destroy(fb); + drm_framebuffer_unreference(fb); out: mutex_unlock(&dev->mode_config.mutex); @@ -2490,7 +2522,7 @@ void drm_fb_release(struct drm_file *priv) mutex_lock(&dev->mode_config.mutex); list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) { list_del(&fb->filp_head); - fb->funcs->destroy(fb); + drm_framebuffer_unreference(fb); } mutex_unlock(&dev->mode_config.mutex); } diff --git a/drivers/gpu/drm/exynos/exynos_drm_fbdev.c b/drivers/gpu/drm/exynos/exynos_drm_fbdev.c index d5586cc..05695d6 100644 --- a/drivers/gpu/drm/exynos/exynos_drm_fbdev.c +++ b/drivers/gpu/drm/exynos/exynos_drm_fbdev.c @@ -266,8 +266,8 @@ static void exynos_drm_fbdev_destroy(struct drm_device *dev, /* release drm framebuffer and real buffer */ if (fb_helper->fb && fb_helper->fb->funcs) { fb = fb_helper->fb; - if (fb && fb->funcs->destroy) - fb->funcs->destroy(fb); + if (fb) + drm_framebuffer_unreference(fb); } /* release linux framebuffer */ diff --git a/drivers/gpu/drm/i915/intel_display.c b/drivers/gpu/drm/i915/intel_display.c index a8538ac..a9d2328 100644 --- a/drivers/gpu/drm/i915/intel_display.c +++ b/drivers/gpu/drm/i915/intel_display.c @@ -5467,7 +5467,7 @@ bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder, if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) { DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n"); if (old->release_fb) - old->release_fb->funcs->destroy(old->release_fb); + drm_framebuffer_unreference(old->release_fb); crtc->fb = old_fb; return false; } @@ -5497,7 +5497,7 @@ void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder, drm_helper_disable_unused_functions(dev); if (old->release_fb) - old->release_fb->funcs->destroy(old->release_fb); + drm_framebuffer_unreference(old->release_fb); return; } diff --git a/include/drm/drm_crtc.h b/include/drm/drm_crtc.h index bac55c2..8a5b16d 100644 --- a/include/drm/drm_crtc.h +++ b/include/drm/drm_crtc.h @@ -221,6 +221,7 @@ struct drm_display_info { }; struct drm_framebuffer_funcs { + /* note: use drm_framebuffer_unreference() */ void (*destroy)(struct drm_framebuffer *framebuffer); int (*create_handle)(struct drm_framebuffer *fb, struct drm_file *file_priv, @@ -245,6 +246,7 @@ struct drm_framebuffer_funcs { struct drm_framebuffer { struct drm_device *dev; + struct kref refcount; struct list_head head; struct drm_mode_object base; const struct drm_framebuffer_funcs *funcs; @@ -923,6 +925,8 @@ extern void drm_framebuffer_set_object(struct drm_device *dev, extern int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb, const struct drm_framebuffer_funcs *funcs); +void drm_framebuffer_unreference(struct drm_framebuffer *fb); +void drm_framebuffer_reference(struct drm_framebuffer *fb); extern void drm_framebuffer_cleanup(struct drm_framebuffer *fb); extern int drmfb_probe(struct drm_device *dev, struct drm_crtc *crtc); extern int drmfb_remove(struct drm_device *dev, struct drm_framebuffer *fb);