From patchwork Tue Dec 19 12:03:37 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Wilson X-Patchwork-Id: 10123131 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 6DDCC603B5 for ; Tue, 19 Dec 2017 12:03:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5725C29273 for ; Tue, 19 Dec 2017 12:03:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4BC602927D; Tue, 19 Dec 2017 12:03:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D7EE029273 for ; Tue, 19 Dec 2017 12:03:45 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C0FE489CB8; Tue, 19 Dec 2017 12:03:44 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from fireflyinternet.com (mail.fireflyinternet.com [109.228.58.192]) by gabe.freedesktop.org (Postfix) with ESMTPS id 203B189CB8 for ; Tue, 19 Dec 2017 12:03:42 +0000 (UTC) X-Default-Received-SPF: pass (skip=forwardok (res=PASS)) x-ip-name=78.156.65.138; Received: from localhost (unverified [78.156.65.138]) by fireflyinternet.com (Firefly Internet (M1)) with ESMTP (TLS) id 10052671-1500050 for multiple; Tue, 19 Dec 2017 12:03:38 +0000 MIME-Version: 1.0 From: Chris Wilson User-Agent: alot/0.3.6 To: Dave Airlie , dri-devel@lists.freedesktop.org References: <20171219113012.32391-1-airlied@gmail.com> In-Reply-To: <20171219113012.32391-1-airlied@gmail.com> Message-ID: <151368501731.3125.11907069207212078869@mail.alporthouse.com> Subject: Re: [PATCH] drm/syncobj: reset file ptr to NULL when released. Date: Tue, 19 Dec 2017 12:03:37 +0000 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP Quoting Dave Airlie (2017-12-19 11:30:12) > From: Dave Airlie > > The vk cts test: > dEQP-VK.api.external.semaphore.opaque_fd.export_multiple_times_temporary > > triggers a lot of > VFS: Close: file count is 0 > > This patch fixes it, but I'm guessing it's racy and someone will > smell rcu, but I just wanted to send out the proof of fixing it > so I remember. > > Signed-off-by: Dave Airlie > --- > drivers/gpu/drm/drm_syncobj.c | 1 + > 1 file changed, 1 insertion(+) > > diff --git a/drivers/gpu/drm/drm_syncobj.c b/drivers/gpu/drm/drm_syncobj.c > index f776fc1cc543..ffa5bbd75852 100644 > --- a/drivers/gpu/drm/drm_syncobj.c > +++ b/drivers/gpu/drm/drm_syncobj.c > @@ -361,6 +361,7 @@ static int drm_syncobj_file_release(struct inode *inode, struct file *file) > { > struct drm_syncobj *syncobj = file->private_data; > > + syncobj->file = NULL; Oh. That's scary as the opposite side assumes that the syncobj->file is assigned for the lifetime of the syncobj - once allocated is never unset. If we stop trying to reuse the struct file and just allocated one for each fd, the complications just vanish? diff --cc include/drm/drm_syncobj.h index 3980602472c0,ba54e0e58bbc..000000000000 --- a/include/drm/drm_syncobj.h +++ b/include/drm/drm_syncobj.h @@@ -56,10 -57,8 +56,6 @@@ struct drm_syncobj * @lock: Protects &cb_list and write-locks &fence. */ spinlock_t lock; - /** - * @file: A file backing for this syncobj. - */ - struct file *file; - - struct rcu_head rcu; }; typedef void (*drm_syncobj_func_t)(struct drm_syncobj *syncobj, diff --git a/drivers/gpu/drm/drm_syncobj.c b/drivers/gpu/drm/drm_syncobj.c index 131695915acd..0cca2e792719 100644 --- a/drivers/gpu/drm/drm_syncobj.c +++ b/drivers/gpu/drm/drm_syncobj.c @@ -399,23 +399,6 @@ static const struct file_operations drm_syncobj_file_fops = { .release = drm_syncobj_file_release, }; -static int drm_syncobj_alloc_file(struct drm_syncobj *syncobj) -{ - struct file *file = anon_inode_getfile("syncobj_file", - &drm_syncobj_file_fops, - syncobj, 0); - if (IS_ERR(file)) - return PTR_ERR(file); - - drm_syncobj_get(syncobj); - if (cmpxchg(&syncobj->file, NULL, file)) { - /* lost the race */ - fput(file); - } - - return 0; -} - /** * drm_syncobj_get_fd - get a file descriptor from a syncobj * @syncobj: Sync object to export @@ -427,21 +410,24 @@ static int drm_syncobj_alloc_file(struct drm_syncobj *syncobj) */ int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd) { - int ret; + struct file *file; int fd; fd = get_unused_fd_flags(O_CLOEXEC); if (fd < 0) return fd; - if (!syncobj->file) { - ret = drm_syncobj_alloc_file(syncobj); - if (ret) { - put_unused_fd(fd); - return ret; - } + file = anon_inode_getfile("syncobj_file", + &drm_syncobj_file_fops, + syncobj, 0); + if (IS_ERR(file)) { + put_unused_fd(fd); + return PTR_ERR(file); } - fd_install(fd, syncobj->file); + + drm_syncobj_get(syncobj); + fd_install(fd, file); + *p_fd = fd; return 0; } @@ -461,31 +447,24 @@ static int drm_syncobj_handle_to_fd(struct drm_file *file_private, return ret; } -static struct drm_syncobj *drm_syncobj_fdget(int fd) -{ - struct file *file = fget(fd); - - if (!file) - return NULL; - if (file->f_op != &drm_syncobj_file_fops) - goto err; - - return file->private_data; -err: - fput(file); - return NULL; -}; - static int drm_syncobj_fd_to_handle(struct drm_file *file_private, int fd, u32 *handle) { - struct drm_syncobj *syncobj = drm_syncobj_fdget(fd); + struct drm_syncobj *syncobj; + struct file *file; int ret; - if (!syncobj) + file = fget(fd); + if (!file) return -EINVAL; + if (file->f_op != &drm_syncobj_file_fops) { + fput(file); + return -EINVAL; + } + /* take a reference to put in the idr */ + syncobj = file->private_data; drm_syncobj_get(syncobj); idr_preload(GFP_KERNEL); @@ -494,12 +473,11 @@ static int drm_syncobj_fd_to_handle(struct drm_file *file_private, spin_unlock(&file_private->syncobj_table_lock); idr_preload_end(); - if (ret < 0) { - fput(syncobj->file); - return ret; - } - *handle = ret; - return 0; + if (ret > 0) + *handle = ret; + + fput(file); + return ret; } static int drm_syncobj_import_sync_file_fence(struct drm_file *file_private,