All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jens Axboe <axboe@kernel.dk>
To: linux-block@vger.kernel.org, linux-aio@kvack.org,
	linux-fsdevel@vger.kernel.org
Cc: Jens Axboe <axboe@kernel.dk>
Subject: [PATCH 19/20] fs: add fget_many() and fput_many()
Date: Mon, 26 Nov 2018 09:45:43 -0700	[thread overview]
Message-ID: <20181126164544.5699-20-axboe@kernel.dk> (raw)
In-Reply-To: <20181126164544.5699-1-axboe@kernel.dk>

Some uses cases repeatedly get and put references to the same file, but
the only exposed interface is doing these one at the time. As each of
these entail an atomic inc or dec on a shared structure, that cost can
add up.

Add fget_many(), which works just like fget(), except it takes an
argument for how many references to get on the file. Ditto fput_many(),
which can drop an arbitrary number of references to a file.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
---
 fs/file.c            | 15 ++++++++++-----
 fs/file_table.c      | 10 ++++++++--
 include/linux/file.h |  2 ++
 include/linux/fs.h   |  3 ++-
 4 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/fs/file.c b/fs/file.c
index 7ffd6e9d103d..ad9870edfd51 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -676,7 +676,7 @@ void do_close_on_exec(struct files_struct *files)
 	spin_unlock(&files->file_lock);
 }
 
-static struct file *__fget(unsigned int fd, fmode_t mask)
+static struct file *__fget(unsigned int fd, fmode_t mask, unsigned int refs)
 {
 	struct files_struct *files = current->files;
 	struct file *file;
@@ -691,7 +691,7 @@ static struct file *__fget(unsigned int fd, fmode_t mask)
 		 */
 		if (file->f_mode & mask)
 			file = NULL;
-		else if (!get_file_rcu(file))
+		else if (!get_file_rcu_many(file, refs))
 			goto loop;
 	}
 	rcu_read_unlock();
@@ -699,15 +699,20 @@ static struct file *__fget(unsigned int fd, fmode_t mask)
 	return file;
 }
 
+struct file *fget_many(unsigned int fd, unsigned int refs)
+{
+	return __fget(fd, FMODE_PATH, refs);
+}
+
 struct file *fget(unsigned int fd)
 {
-	return __fget(fd, FMODE_PATH);
+	return fget_many(fd, 1);
 }
 EXPORT_SYMBOL(fget);
 
 struct file *fget_raw(unsigned int fd)
 {
-	return __fget(fd, 0);
+	return __fget(fd, 0, 1);
 }
 EXPORT_SYMBOL(fget_raw);
 
@@ -738,7 +743,7 @@ static unsigned long __fget_light(unsigned int fd, fmode_t mask)
 			return 0;
 		return (unsigned long)file;
 	} else {
-		file = __fget(fd, mask);
+		file = __fget(fd, mask, 1);
 		if (!file)
 			return 0;
 		return FDPUT_FPUT | (unsigned long)file;
diff --git a/fs/file_table.c b/fs/file_table.c
index e49af4caf15d..6a3964df33e4 100644
--- a/fs/file_table.c
+++ b/fs/file_table.c
@@ -326,9 +326,9 @@ void flush_delayed_fput(void)
 
 static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
 
-void fput(struct file *file)
+void fput_many(struct file *file, unsigned int refs)
 {
-	if (atomic_long_dec_and_test(&file->f_count)) {
+	if (atomic_long_sub_and_test(refs, &file->f_count)) {
 		struct task_struct *task = current;
 
 		if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
@@ -347,6 +347,12 @@ void fput(struct file *file)
 	}
 }
 
+void fput(struct file *file)
+{
+	fput_many(file, 1);
+}
+
+
 /*
  * synchronous analog of fput(); for kernel threads that might be needed
  * in some umount() (and thus can't use flush_delayed_fput() without
diff --git a/include/linux/file.h b/include/linux/file.h
index 6b2fb032416c..3fcddff56bc4 100644
--- a/include/linux/file.h
+++ b/include/linux/file.h
@@ -13,6 +13,7 @@
 struct file;
 
 extern void fput(struct file *);
+extern void fput_many(struct file *, unsigned int);
 
 struct file_operations;
 struct vfsmount;
@@ -44,6 +45,7 @@ static inline void fdput(struct fd fd)
 }
 
 extern struct file *fget(unsigned int fd);
+extern struct file *fget_many(unsigned int fd, unsigned int refs);
 extern struct file *fget_raw(unsigned int fd);
 extern unsigned long __fdget(unsigned int fd);
 extern unsigned long __fdget_raw(unsigned int fd);
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 6a5f71f8ae06..dc54a65c401a 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -952,7 +952,8 @@ static inline struct file *get_file(struct file *f)
 	atomic_long_inc(&f->f_count);
 	return f;
 }
-#define get_file_rcu(x) atomic_long_inc_not_zero(&(x)->f_count)
+#define get_file_rcu_many(x, cnt) atomic_long_add_unless(&(x)->f_count, (cnt), 0)
+#define get_file_rcu(x) get_file_rcu_many((x), 1)
 #define fput_atomic(x)	atomic_long_add_unless(&(x)->f_count, -1, 1)
 #define file_count(x)	atomic_long_read(&(x)->f_count)
 
-- 
2.17.1


  parent reply	other threads:[~2018-11-26 16:46 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-26 16:45 [PATCHSET v3 0/18] Support for polled aio Jens Axboe
2018-11-26 16:45 ` [PATCH 01/20] aio: fix failure to put the file pointer Jens Axboe
2018-11-27  8:16   ` Johannes Thumshirn
2018-11-26 16:45 ` [PATCH 02/20] aio: clear IOCB_HIPRI Jens Axboe
2018-11-27  8:18   ` Johannes Thumshirn
2018-11-26 16:45 ` [PATCH 03/20] fs: add an iopoll method to struct file_operations Jens Axboe
2018-11-27  8:24   ` Johannes Thumshirn
2018-11-26 16:45 ` [PATCH 04/20] block: wire up block device iopoll method Jens Axboe
2018-11-27  8:29   ` Johannes Thumshirn
2018-11-26 16:45 ` [PATCH 05/20] block: ensure that async polled IO is marked REQ_NOWAIT Jens Axboe
2018-11-26 16:45 ` [PATCH 06/20] iomap: wire up the iopoll method Jens Axboe
2018-11-26 16:45 ` [PATCH 07/20] iomap: ensure that async polled IO is marked REQ_NOWAIT Jens Axboe
2018-11-26 16:45 ` [PATCH 08/20] aio: use assigned completion handler Jens Axboe
2018-11-26 16:45 ` [PATCH 09/20] aio: separate out ring reservation from req allocation Jens Axboe
2018-11-26 16:45 ` [PATCH 10/20] aio: don't zero entire aio_kiocb aio_get_req() Jens Axboe
2018-11-26 16:45 ` [PATCH 11/20] aio: only use blk plugs for > 2 depth submissions Jens Axboe
2018-11-26 16:45 ` [PATCH 12/20] aio: use iocb_put() instead of open coding it Jens Axboe
2018-11-26 16:45 ` [PATCH 13/20] aio: split out iocb copy from io_submit_one() Jens Axboe
2018-11-26 16:45 ` [PATCH 14/20] aio: abstract out io_event filler helper Jens Axboe
2018-11-26 16:45 ` [PATCH 15/20] aio: add io_setup2() system call Jens Axboe
2018-11-26 16:45 ` [PATCH 16/20] aio: add support for having user mapped iocbs Jens Axboe
2018-11-26 16:45 ` [PATCH 17/20] aio: support for IO polling Jens Axboe
2018-11-27  9:53   ` Benny Halevy
2018-11-27 15:24     ` Jens Axboe
2018-11-28  9:33       ` Benny Halevy
2018-11-28 18:50         ` Jens Axboe
2018-11-29 14:10           ` Benny Halevy
2018-11-26 16:45 ` [PATCH 18/20] aio: add submission side request cache Jens Axboe
2018-11-26 16:45 ` Jens Axboe [this message]
2018-11-26 16:45 ` [PATCH 20/20] aio: use fget/fput_many() for file references Jens Axboe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181126164544.5699-20-axboe@kernel.dk \
    --to=axboe@kernel.dk \
    --cc=linux-aio@kvack.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.