From patchwork Tue Oct 11 23:50:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ruchi Kandoi X-Patchwork-Id: 9371879 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 92DD36048F for ; Tue, 11 Oct 2016 23:51:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 82F6228643 for ; Tue, 11 Oct 2016 23:51:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7764C29120; Tue, 11 Oct 2016 23:51:33 +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=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E038E28643 for ; Tue, 11 Oct 2016 23:51:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753248AbcJKXus (ORCPT ); Tue, 11 Oct 2016 19:50:48 -0400 Received: from mail-pf0-f179.google.com ([209.85.192.179]:34223 "EHLO mail-pf0-f179.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752778AbcJKXup (ORCPT ); Tue, 11 Oct 2016 19:50:45 -0400 Received: by mail-pf0-f179.google.com with SMTP id 190so9743933pfv.1 for ; Tue, 11 Oct 2016 16:50:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:subject:date:message-id:in-reply-to:references; bh=UPMhElkuzK7UhKMWIYA9pq4SRuL4wasW6AXGtilW/uE=; b=jVpleVZxonvZ7k8Sm7Y14gSMNdeFyMSFKphfFgB0AssisAWZE3a65DxtiFIBl8oXv3 93FUaGi/48yxxB6SfaSlc3+tRksfcrAPEczqnVBIUZzZriTx7hnXt1RPKfz6RjA8Z69o RshrCm9qNHn3T3FOpI0ti23cv2cUbXo7f3JT+4UOKccLadoHioX882MqfR918wfZ6bQV 7GK2dMbKPzRlCoZxZs4QVDTYBkWeMC7zcGzZ6kyQaaPrPJk0/tDicjCHCkqNhp4K12Ud lx9EXJRRMoXX8TnXpb5syhVgUsDktSq4nSxnsiBl4WwqLxH+NZH6CnGvhFtMrx5EL4eB XYBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=UPMhElkuzK7UhKMWIYA9pq4SRuL4wasW6AXGtilW/uE=; b=jOVHGUXQL0ko6TqSUmFQGrJhLCU1pksOrSsIGfnrixJeGENJXrnMTPmpwu3L5MsGFd nSR1pKcdWOLhJygDV4IPfBz/0gMxA7WDFYqKL/PVAgwmmV+yyuoEwXQSp7j5WjsJW3IN 9ITr+A3JEaQs8d0KUaTbPWoXzzN2guZPdtKXt23uherlhzJ/DRR2bZHk+WI0/FsyvePb dn8uvQkicRhnGsQPOgxxuqy+4twp/S53yqYESrCIU+gjvT5T+U7p2aDeAhapXgJw6rtu rasfr3qTUzEoisCryW+e2QWZli9m0NbYd/pgQqAdMDq2R6e5h7SC/sQznrGAJghxRi9U YmJA== X-Gm-Message-State: AA6/9Rm6BOYeue0EB1wsqINtwSGbF3bPdjdY6SC+2AxISOu0r20n+yuASUnPM7b4h2SSVxWB X-Received: by 10.98.62.92 with SMTP id l89mr812845pfa.92.1476229836650; Tue, 11 Oct 2016 16:50:36 -0700 (PDT) Received: from kandoiruchi.mtv.corp.google.com ([100.98.120.59]) by smtp.gmail.com with ESMTPSA id i8sm6922381paw.25.2016.10.11.16.50.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 11 Oct 2016 16:50:36 -0700 (PDT) From: Ruchi Kandoi To: kandoiruchi@google.com, gregkh@linuxfoundation.org, arve@android.com, riandrews@android.com, sumit.semwal@linaro.org, arnd@arndb.de, labbott@redhat.com, viro@zeniv.linux.org.uk, jlayton@poochiereds.net, bfields@fieldses.org, mingo@redhat.com, peterz@infradead.org, akpm@linux-foundation.org, keescook@chromium.org, mhocko@suse.com, oleg@redhat.com, john.stultz@linaro.org, mguzik@redhat.com, jdanis@google.com, adobriyan@gmail.com, ghackmann@google.com, kirill.shutemov@linux.intel.com, vbabka@suse.cz, dave.hansen@linux.intel.com, dan.j.williams@intel.com, hannes@cmpxchg.org, iamjoonsoo.kim@lge.com, luto@kernel.org, tj@kernel.org, vdavydov.dev@gmail.com, ebiederm@xmission.com, linux-kernel@vger.kernel.org, devel@driverdev.osuosl.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: [RFC 1/6] fs: add installed and uninstalled file_operations Date: Tue, 11 Oct 2016 16:50:05 -0700 Message-Id: <1476229810-26570-2-git-send-email-kandoiruchi@google.com> X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 In-Reply-To: <1476229810-26570-1-git-send-email-kandoiruchi@google.com> References: <1476229810-26570-1-git-send-email-kandoiruchi@google.com> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP These optional file_operations notify a file implementation when it is installed or uninstalled from a task's fd table. This can be used for accounting of file-backed shared resources like dma-buf. This involves some changes to the __fd_install() and __close_fd() APIs to actually pass along the responsible task_struct. These are low-level APIs with only two in-tree callers, both adjusted in this patch. Signed-off-by: Greg Hackmann Signed-off-by: Ruchi Kandoi --- drivers/android/binder.c | 4 ++-- fs/file.c | 38 +++++++++++++++++++++++++++++--------- fs/open.c | 2 +- include/linux/fdtable.h | 4 ++-- include/linux/fs.h | 2 ++ 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/drivers/android/binder.c b/drivers/android/binder.c index 562af94..0bb174e 100644 --- a/drivers/android/binder.c +++ b/drivers/android/binder.c @@ -398,7 +398,7 @@ static void task_fd_install( struct binder_proc *proc, unsigned int fd, struct file *file) { if (proc->files) - __fd_install(proc->files, fd, file); + __fd_install(proc->tsk, fd, file); } /* @@ -411,7 +411,7 @@ static long task_close_fd(struct binder_proc *proc, unsigned int fd) if (proc->files == NULL) return -ESRCH; - retval = __close_fd(proc->files, fd); + retval = __close_fd(proc->tsk, fd); /* can't restart close syscall because file table entry was cleared */ if (unlikely(retval == -ERESTARTSYS || retval == -ERESTARTNOINTR || diff --git a/fs/file.c b/fs/file.c index 69d6990..19c5fad 100644 --- a/fs/file.c +++ b/fs/file.c @@ -282,6 +282,24 @@ static unsigned int count_open_files(struct fdtable *fdt) return i; } +static inline void fdt_install(struct fdtable *fdt, int fd, struct file *file, + struct task_struct *task) +{ + if (file->f_op->installed) + file->f_op->installed(file, task); + rcu_assign_pointer(fdt->fd[fd], file); +} + +static inline void fdt_uninstall(struct fdtable *fdt, int fd, + struct task_struct *task) +{ + struct file *old_file = fdt->fd[fd]; + + if (old_file->f_op->uninstalled) + old_file->f_op->uninstalled(old_file, task); + rcu_assign_pointer(fdt->fd[fd], NULL); +} + /* * Allocate a new files structure and copy contents from the * passed in files structure. @@ -543,7 +561,7 @@ int __alloc_fd(struct files_struct *files, /* Sanity check */ if (rcu_access_pointer(fdt->fd[fd]) != NULL) { printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd); - rcu_assign_pointer(fdt->fd[fd], NULL); + fdt_uninstall(fdt, fd, current); } #endif @@ -601,10 +619,11 @@ EXPORT_SYMBOL(put_unused_fd); * fd_install() instead. */ -void __fd_install(struct files_struct *files, unsigned int fd, +void __fd_install(struct task_struct *task, unsigned int fd, struct file *file) { struct fdtable *fdt; + struct files_struct *files = task->files; might_sleep(); rcu_read_lock_sched(); @@ -618,13 +637,13 @@ void __fd_install(struct files_struct *files, unsigned int fd, smp_rmb(); fdt = rcu_dereference_sched(files->fdt); BUG_ON(fdt->fd[fd] != NULL); - rcu_assign_pointer(fdt->fd[fd], file); + fdt_install(fdt, fd, file, task); rcu_read_unlock_sched(); } void fd_install(unsigned int fd, struct file *file) { - __fd_install(current->files, fd, file); + __fd_install(current, fd, file); } EXPORT_SYMBOL(fd_install); @@ -632,10 +651,11 @@ EXPORT_SYMBOL(fd_install); /* * The same warnings as for __alloc_fd()/__fd_install() apply here... */ -int __close_fd(struct files_struct *files, unsigned fd) +int __close_fd(struct task_struct *task, unsigned fd) { struct file *file; struct fdtable *fdt; + struct files_struct *files = task->files; spin_lock(&files->file_lock); fdt = files_fdtable(files); @@ -644,7 +664,7 @@ int __close_fd(struct files_struct *files, unsigned fd) file = fdt->fd[fd]; if (!file) goto out_unlock; - rcu_assign_pointer(fdt->fd[fd], NULL); + fdt_uninstall(fdt, fd, task); __clear_close_on_exec(fd, fdt); __put_unused_fd(files, fd); spin_unlock(&files->file_lock); @@ -679,7 +699,7 @@ void do_close_on_exec(struct files_struct *files) file = fdt->fd[fd]; if (!file) continue; - rcu_assign_pointer(fdt->fd[fd], NULL); + fdt_uninstall(fdt, fd, current); __put_unused_fd(files, fd); spin_unlock(&files->file_lock); filp_close(file, files); @@ -846,7 +866,7 @@ __releases(&files->file_lock) if (!tofree && fd_is_open(fd, fdt)) goto Ebusy; get_file(file); - rcu_assign_pointer(fdt->fd[fd], file); + fdt_install(fdt, fd, file, current); __set_open_fd(fd, fdt); if (flags & O_CLOEXEC) __set_close_on_exec(fd, fdt); @@ -870,7 +890,7 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags) struct files_struct *files = current->files; if (!file) - return __close_fd(files, fd); + return __close_fd(current, fd); if (fd >= rlimit(RLIMIT_NOFILE)) return -EBADF; diff --git a/fs/open.c b/fs/open.c index 8aeb08b..0f1db76 100644 --- a/fs/open.c +++ b/fs/open.c @@ -1120,7 +1120,7 @@ EXPORT_SYMBOL(filp_close); */ SYSCALL_DEFINE1(close, unsigned int, fd) { - int retval = __close_fd(current->files, fd); + int retval = __close_fd(current, fd); /* can't restart close syscall because file table entry was cleared */ if (unlikely(retval == -ERESTARTSYS || diff --git a/include/linux/fdtable.h b/include/linux/fdtable.h index aca2a6a..a45fce3 100644 --- a/include/linux/fdtable.h +++ b/include/linux/fdtable.h @@ -113,9 +113,9 @@ int iterate_fd(struct files_struct *, unsigned, extern int __alloc_fd(struct files_struct *files, unsigned start, unsigned end, unsigned flags); -extern void __fd_install(struct files_struct *files, +extern void __fd_install(struct task_struct *task, unsigned int fd, struct file *file); -extern int __close_fd(struct files_struct *files, +extern int __close_fd(struct task_struct *task, unsigned int fd); extern struct kmem_cache *files_cachep; diff --git a/include/linux/fs.h b/include/linux/fs.h index c145219..d62bce8 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1730,6 +1730,8 @@ struct file_operations { u64); ssize_t (*dedupe_file_range)(struct file *, u64, u64, struct file *, u64); + void (*installed)(struct file *file, struct task_struct *task); + void (*uninstalled)(struct file *file, struct task_struct *task); }; struct inode_operations {