fs: add fget_many() and fput_many()
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. Reviewed-by: Hannes Reinecke <hare@suse.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Jens Axboe <axboe@kernel.dk>
This commit is contained in:
parent
def596e955
commit
091141a42e
15
fs/file.c
15
fs/file.c
|
@ -705,7 +705,7 @@ void do_close_on_exec(struct files_struct *files)
|
||||||
spin_unlock(&files->file_lock);
|
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 files_struct *files = current->files;
|
||||||
struct file *file;
|
struct file *file;
|
||||||
|
@ -720,7 +720,7 @@ loop:
|
||||||
*/
|
*/
|
||||||
if (file->f_mode & mask)
|
if (file->f_mode & mask)
|
||||||
file = NULL;
|
file = NULL;
|
||||||
else if (!get_file_rcu(file))
|
else if (!get_file_rcu_many(file, refs))
|
||||||
goto loop;
|
goto loop;
|
||||||
}
|
}
|
||||||
rcu_read_unlock();
|
rcu_read_unlock();
|
||||||
|
@ -728,15 +728,20 @@ loop:
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct file *fget_many(unsigned int fd, unsigned int refs)
|
||||||
|
{
|
||||||
|
return __fget(fd, FMODE_PATH, refs);
|
||||||
|
}
|
||||||
|
|
||||||
struct file *fget(unsigned int fd)
|
struct file *fget(unsigned int fd)
|
||||||
{
|
{
|
||||||
return __fget(fd, FMODE_PATH);
|
return __fget(fd, FMODE_PATH, 1);
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL(fget);
|
EXPORT_SYMBOL(fget);
|
||||||
|
|
||||||
struct file *fget_raw(unsigned int fd)
|
struct file *fget_raw(unsigned int fd)
|
||||||
{
|
{
|
||||||
return __fget(fd, 0);
|
return __fget(fd, 0, 1);
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL(fget_raw);
|
EXPORT_SYMBOL(fget_raw);
|
||||||
|
|
||||||
|
@ -767,7 +772,7 @@ static unsigned long __fget_light(unsigned int fd, fmode_t mask)
|
||||||
return 0;
|
return 0;
|
||||||
return (unsigned long)file;
|
return (unsigned long)file;
|
||||||
} else {
|
} else {
|
||||||
file = __fget(fd, mask);
|
file = __fget(fd, mask, 1);
|
||||||
if (!file)
|
if (!file)
|
||||||
return 0;
|
return 0;
|
||||||
return FDPUT_FPUT | (unsigned long)file;
|
return FDPUT_FPUT | (unsigned long)file;
|
||||||
|
|
|
@ -326,9 +326,9 @@ void flush_delayed_fput(void)
|
||||||
|
|
||||||
static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
|
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;
|
struct task_struct *task = current;
|
||||||
|
|
||||||
if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
|
if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
|
||||||
|
@ -347,6 +347,11 @@ 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
|
* synchronous analog of fput(); for kernel threads that might be needed
|
||||||
* in some umount() (and thus can't use flush_delayed_fput() without
|
* in some umount() (and thus can't use flush_delayed_fput() without
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
struct file;
|
struct file;
|
||||||
|
|
||||||
extern void fput(struct file *);
|
extern void fput(struct file *);
|
||||||
|
extern void fput_many(struct file *, unsigned int);
|
||||||
|
|
||||||
struct file_operations;
|
struct file_operations;
|
||||||
struct vfsmount;
|
struct vfsmount;
|
||||||
|
@ -44,6 +45,7 @@ static inline void fdput(struct fd fd)
|
||||||
}
|
}
|
||||||
|
|
||||||
extern struct file *fget(unsigned int 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 struct file *fget_raw(unsigned int fd);
|
||||||
extern unsigned long __fdget(unsigned int fd);
|
extern unsigned long __fdget(unsigned int fd);
|
||||||
extern unsigned long __fdget_raw(unsigned int fd);
|
extern unsigned long __fdget_raw(unsigned int fd);
|
||||||
|
|
|
@ -952,7 +952,9 @@ static inline struct file *get_file(struct file *f)
|
||||||
atomic_long_inc(&f->f_count);
|
atomic_long_inc(&f->f_count);
|
||||||
return f;
|
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 fput_atomic(x) atomic_long_add_unless(&(x)->f_count, -1, 1)
|
||||||
#define file_count(x) atomic_long_read(&(x)->f_count)
|
#define file_count(x) atomic_long_read(&(x)->f_count)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue