From patchwork Sun Jul 15 19:04:41 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Herrmann X-Patchwork-Id: 1199471 Return-Path: X-Original-To: patchwork-linux-fbdev@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id D3600E0004 for ; Sun, 15 Jul 2012 19:08:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752482Ab2GOTGV (ORCPT ); Sun, 15 Jul 2012 15:06:21 -0400 Received: from mail-we0-f174.google.com ([74.125.82.174]:50941 "EHLO mail-we0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751568Ab2GOTF3 (ORCPT ); Sun, 15 Jul 2012 15:05:29 -0400 Received: by weyx8 with SMTP id x8so3536669wey.19 for ; Sun, 15 Jul 2012 12:05:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=ohpS+t21pEjpe6M+q0ZMJ/1YoIlmDY9IN1x8XkHkjMo=; b=GpmwmcJaqbp0VcpA7orN8wBrlELFsCrgV6IJvoJAbtspiooUmnnj0ATGiedvfriXey I3p3PsHmyNQUosyIF81pAffJ7hh7LbgXq7JXIEClwlBNVk6nJLyc89uqHk4FMxnsEZhJ tW+OMqJRHGTIc7q6i8GhK5ZBL7WSPLDNhEFvA935pY76CMXY4KsY594W/euXy4pavdvp 3g9Hwm/1zLJmk/oc+8BKcAD54oQQqkguBV1C8TNn9CV6PM+QLjaMJqJ2Wz8f8Q+/UZ+f eUrNi2usIuMSEpTHCspXi5w8AnhHx1Ypwl06e94djLasAAPG5VGQt+fZSEFGwA7az1xZ 164w== Received: by 10.180.100.133 with SMTP id ey5mr12636677wib.4.1342379126966; Sun, 15 Jul 2012 12:05:26 -0700 (PDT) Received: from localhost.localdomain (stgt-5f71ba59.pool.mediaWays.net. [95.113.186.89]) by mx.google.com with ESMTPS id j6sm25479086wiy.4.2012.07.15.12.05.25 (version=TLSv1/SSLv3 cipher=OTHER); Sun, 15 Jul 2012 12:05:26 -0700 (PDT) From: David Herrmann To: linux-kernel@vger.kernel.org Cc: Florian Tobias Schandinat , Andrew Morton , Greg Kroah-Hartman , linux-fbdev@vger.kernel.org, linux-serial@vger.kernel.org, Alan Cox , David Herrmann Subject: [PATCH v3 06/11] fblog: open fb on registration Date: Sun, 15 Jul 2012 21:04:41 +0200 Message-Id: <1342379086-7583-7-git-send-email-dh.herrmann@googlemail.com> X-Mailer: git-send-email 1.7.11.2 In-Reply-To: <1342379086-7583-1-git-send-email-dh.herrmann@googlemail.com> References: <1342379086-7583-1-git-send-email-dh.herrmann@googlemail.com> Sender: linux-fbdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fbdev@vger.kernel.org This opens the framebuffer upon registration so we can use it for drawing-operations. On unregistration we close it again. While opening/closing or accessing the fb in any other way, we must hold the fb-mutex. However, since the notifiers are often called with the mutex already held, we cannot lock it _after_ taking the fblog_registration_lock. Therefore, we require the caller to make sure the fb-mutex is held. Signed-off-by: David Herrmann --- drivers/video/console/fblog.c | 94 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/drivers/video/console/fblog.c b/drivers/video/console/fblog.c index 279f4d8..1c526c5 100644 --- a/drivers/video/console/fblog.c +++ b/drivers/video/console/fblog.c @@ -32,12 +32,14 @@ enum fblog_flags { FBLOG_KILLED, + FBLOG_OPEN, }; struct fblog_fb { unsigned long flags; struct fb_info *info; struct device dev; + struct mutex lock; }; static DEFINE_MUTEX(fblog_registration_lock); @@ -46,6 +48,74 @@ static struct fblog_fb *fblog_fbs[FB_MAX]; #define to_fblog_dev(_d) container_of(_d, struct fblog_fb, dev) /* + * fblog_open/close() + * These functions manage access to the underlying framebuffer. While opened, we + * have a valid reference to the fb and can use it for drawing operations. When + * the fb is unregistered, we drop our reference and close the fb so it can get + * deleted properly. We also mark it as dead so no further fblog_open() call + * will succeed. + * Both functions must be called with the fb->info->lock mutex held! But make + * sure to lock it _before_ locking the fblog-registration-lock. Otherwise, we + * will dead-lock with fb-registration. + */ + +static int fblog_open(struct fblog_fb *fb) +{ + int ret; + + mutex_lock(&fb->lock); + + if (test_bit(FBLOG_KILLED, &fb->flags)) { + ret = -ENODEV; + goto unlock; + } + + if (test_bit(FBLOG_OPEN, &fb->flags)) { + ret = 0; + goto unlock; + } + + if (!try_module_get(fb->info->fbops->owner)) { + ret = -ENODEV; + goto out_killed; + } + + if (fb->info->fbops->fb_open && fb->info->fbops->fb_open(fb->info, 0)) { + ret = -EIO; + goto out_unref; + } + + set_bit(FBLOG_OPEN, &fb->flags); + mutex_unlock(&fb->lock); + return 0; + +out_unref: + module_put(fb->info->fbops->owner); +out_killed: + set_bit(FBLOG_KILLED, &fb->flags); +unlock: + mutex_unlock(&fb->lock); + return ret; +} + +static void fblog_close(struct fblog_fb *fb, bool kill_dev) +{ + mutex_lock(&fb->lock); + + if (test_bit(FBLOG_OPEN, &fb->flags)) { + if (fb->info->fbops->fb_release) + fb->info->fbops->fb_release(fb->info, 0); + module_put(fb->info->fbops->owner); + clear_bit(FBLOG_OPEN, &fb->flags); + } + + if (kill_dev) + set_bit(FBLOG_KILLED, &fb->flags); + + mutex_unlock(&fb->lock); +} + +/* * fblog framebuffer list * The fblog_fbs[] array contains all currently registered framebuffers. If a * framebuffer is in that list, we always must make sure that we own a reference @@ -77,6 +147,7 @@ static void fblog_do_unregister(struct fb_info *info) fblog_fbs[info->node] = NULL; + fblog_close(fb, true); device_del(&fb->dev); put_device(&fb->dev); } @@ -99,6 +170,7 @@ static void fblog_do_register(struct fb_info *info, bool force) return; fb->info = info; + mutex_init(&fb->lock); __module_get(THIS_MODULE); device_initialize(&fb->dev); fb->dev.class = fb_class; @@ -113,6 +185,8 @@ static void fblog_do_register(struct fb_info *info, bool force) put_device(&fb->dev); return; } + + fblog_open(fb); } static void fblog_register(struct fb_info *info, bool force) @@ -134,6 +208,7 @@ static int fblog_event(struct notifier_block *self, unsigned long action, { struct fb_event *event = data; struct fb_info *info = event->info; + struct fblog_fb *fb; switch(action) { case FB_EVENT_FB_REGISTERED: @@ -145,8 +220,21 @@ static int fblog_event(struct notifier_block *self, unsigned long action, case FB_EVENT_FB_UNREGISTERED: /* This is called when a low-level system driver unregisters a * framebuffer. The registration lock is held but the console - * lock might not be held. */ + * lock might not be held. The fb-lock is not held, either! */ + mutex_lock(&info->lock); fblog_unregister(info); + mutex_unlock(&info->lock); + break; + case FB_EVENT_FB_UNBIND: + /* Called directly before unregistering an FB. The FB is still + * valid here and the registration lock is held but the console + * lock might not be held (really?). */ + mutex_lock(&fblog_registration_lock); + fb = fblog_fbs[info->node]; + mutex_unlock(&fblog_registration_lock); + + if (fb) + fblog_close(fb, true); break; } @@ -163,7 +251,9 @@ static void fblog_scan(void) if (!info || IS_ERR(info)) continue; + mutex_lock(&info->lock); fblog_register(info, false); + mutex_unlock(&info->lock); /* There is a very subtle race-condition. Even though we might * own a reference to the fb, it may still get unregistered @@ -224,7 +314,9 @@ static void __exit fblog_exit(void) if (!info || IS_ERR(info)) continue; + mutex_lock(&info->lock); fblog_unregister(info); + mutex_unlock(&info->lock); put_fb_info(info); } }