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.
Showing the changes to include/linux (2 of 4 files). Show all
Unified · Split
| @@ -13,6 +13,7 @@ · 12 unmodified lines | |||
| 13 | struct file; | 13 | struct file; |
| 14 | 14 | ||
| 15 | extern void fput(struct file *); | 15 | extern void fput(struct file *); |
| 16 | extern void fput_many(struct file *, unsigned int); | ||
| 16 | 17 | ||
| 17 | struct file_operations; | 18 | struct file_operations; |
| 18 | struct vfsmount; | 19 | struct vfsmount; |
| @@ -44,6 +45,7 @@ static inline void fdput(struct fd fd) · 25 unmodified lines | |||
| 44 | } | 45 | } |
| 45 | 46 | ||
| 46 | extern struct file *fget(unsigned int fd); | 47 | extern struct file *fget(unsigned int fd); |
| 48 | extern struct file *fget_many(unsigned int fd, unsigned int refs); | ||
| 47 | extern struct file *fget_raw(unsigned int fd); | 49 | extern struct file *fget_raw(unsigned int fd); |
| 48 | extern unsigned long __fdget(unsigned int fd); | 50 | extern unsigned long __fdget(unsigned int fd); |
| 49 | extern unsigned long __fdget_raw(unsigned int fd); | 51 | extern unsigned long __fdget_raw(unsigned int fd); |
| @@ -952,7 +952,9 @@ static inline struct file *get_file(struct file *f) · 951 unmodified lines | |||
| 952 | atomic_long_inc(&f->f_count); | 952 | atomic_long_inc(&f->f_count); |
| 953 | return f; | 953 | return f; |
| 954 | } | 954 | } |
| 955 | #define get_file_rcu(x) atomic_long_inc_not_zero(&(x)->f_count) | 955 | #define get_file_rcu_many(x, cnt) \ |
| 956 | atomic_long_add_unless(&(x)->f_count, (cnt), 0) | ||
| 957 | #define get_file_rcu(x) get_file_rcu_many((x), 1) | ||
| 956 | #define fput_atomic(x) atomic_long_add_unless(&(x)->f_count, -1, 1) | 958 | #define fput_atomic(x) atomic_long_add_unless(&(x)->f_count, -1, 1) |
| 957 | #define file_count(x) atomic_long_read(&(x)->f_count) | 959 | #define file_count(x) atomic_long_read(&(x)->f_count) |
| 958 | 960 | ||