proc,fcntl: introduce F_SET_DESCRIPTION
Subsystems (6)
PATCH 1/1 proc,fcntl: introduce F_SET_DESCRIPTION
This command attaches a description to a file descriptor for troubleshooting purposes. The free string is displayed in the process fdinfo file for that fd /proc/pid/fdinfo/fd.
One intended usage is to allow processes to self-document sockets for netstat and friends to report
---
More context sent earlier this week: https://lore.kernel.org/linux-api/CAGbU3_nVvuzMn2wo4_ZKufWcGfmGsopVujzTWw-Bbeky=xS+GA@mail.gmail.com/T/#u
Documentation/filesystems/proc.rst | 3 +++ fs/fcntl.c | 19 +++++++++++++++++++ fs/proc/fd.c | 5 +++++ include/linux/fs.h | 3 +++ include/uapi/linux/fcntl.h | 5 +++++ 5 files changed, 35 insertions(+)
Documentation/filesystems/proc.rst +3 -0
@@ -1918,6 +1918,9 @@ A typical output is:: flags: 0100002 mnt_id: 19 +An optional 'desc' is set if the process documented its usage of +the file via the fcntl command F_SET_DESCRIPTION. + All locks associated with a file descriptor are shown in its fdinfo too:: lock: 1: FLOCK ADVISORY WRITE 359 00:13:11691 0 EOF
fs/fcntl.c +19 -0
@@ -319,6 +319,22 @@ static long fcntl_rw_hint(struct file *file, unsigned int cmd, } } +static long fcntl_set_description (struct file *file,char __user *desc) +{ +char *d; + + d =strndup_user (desc,MAX_FILE_DESC_SIZE ); +if (IS_ERR (d)) +return PTR_ERR (d); + +spin_lock (&file->f_lock); +kfree (file->f_description); + file->f_description = d; +spin_unlock (&file->f_lock); + +return 0 ; +} +static long do_fcntl (int fd,unsigned int cmd,unsigned long arg,struct file *filp) {
@@ -426,6 +442,9 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,case F_SET_FILE_RW_HINT : err =fcntl_rw_hint (filp, cmd, arg);break ; +case F_SET_DESCRIPTION : + err =fcntl_set_description (filp, argp); +break ;default :break ; }
fs/proc/fd.c +5 -0
@@ -57,6 +57,11 @@ static int seq_show(struct seq_file *m, void *v) (long long )file->f_pos, f_flags, real_mount(file->f_path.mnt)->mnt_id); +spin_lock (&file->f_lock); +if (file->f_description) +seq_printf (m,"desc:\t%s\n" , file->f_description); +spin_unlock (&file->f_lock); +show_fd_locks (m, file, files);if (seq_has_overflowed (m)) goto out;
include/linux/fs.h +3 -0
@@ -980,6 +980,9 @@ struct file {struct address_space *f_mapping;errseq_t f_wb_err;errseq_t f_sb_err;/* for syncfs */ + +#define MAX_FILE_DESC_SIZE 256 +char *f_description; }__randomize_layout __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */
include/uapi/linux/fcntl.h +5 -0
@@ -55,6 +55,11 @@#define F_GET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 13)#define F_SET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 14) +/* +* Set file description +*/ +#define F_SET_DESCRIPTION (F_LINUX_SPECIFIC_BASE + 15) +/* * Valid hint values for F_{GET,SET}_RW_HINT. 0 is "not set", or can be * used to clear any hints previously set.
Signature · 1 line
2.25.1
Al Viro
maintainerQuoted 6 lines · Pascal Bouchareine
This command attaches a description to a file descriptor for troubleshooting purposes. The free string is displayed in the process fdinfo file for that fd
/proc/pid/fdinfo/fd.One intended usage is to allow processes to self-document sockets for netstat and friends to report
Have you even tried to test it? When will it ever free those things?
Quoted 1 line · Al Viro
Have you even tried to test it? When will it ever free those things?
Thanks for pointing that out, I'll try to address that in the thread
I did basic tests against 5.4: set a description, concurrently set it from multiple child processes, read, demo with ss/netstat.
However I rebased against master and have not tested the build after rebase, is that broken?
Should I use a different target for tests?
Thanks for your help
PATCH v2 1/1 proc,fcntl: introduce F_SET_DESCRIPTION
In reply to Pascal BouchareineThis command attaches a description to a file descriptor for troubleshooting purposes. The free string is displayed in the process fdinfo file for that fd /proc/pid/fdinfo/fd.
One intended usage is to allow processes to self-document sockets for netstat and friends to report
--- Documentation/filesystems/proc.rst | 3 +++ fs/fcntl.c | 19 +++++++++++++++++++ fs/file_table.c | 3 +++ fs/proc/fd.c | 5 +++++ include/linux/fs.h | 3 +++ include/uapi/linux/fcntl.h | 5 +++++ scripts/get_maintainer.pl | 2 ++ 7 files changed, 40 insertions(+)
Documentation/filesystems/proc.rst +3 -0
@@ -1918,6 +1918,9 @@ A typical output is:: flags: 0100002 mnt_id: 19 +An optional 'desc' is set if the process documented its usage of +the file via the fcntl command F_SET_DESCRIPTION. + All locks associated with a file descriptor are shown in its fdinfo too:: lock: 1: FLOCK ADVISORY WRITE 359 00:13:11691 0 EOF
fs/fcntl.c +19 -0
@@ -319,6 +319,22 @@ static long fcntl_rw_hint(struct file *file, unsigned int cmd, } } +static long fcntl_set_description (struct file *file,char __user *desc) +{ +char *d; + + d =strndup_user (desc,MAX_FILE_DESC_SIZE ); +if (IS_ERR (d)) +return PTR_ERR (d); + +spin_lock (&file->f_lock); +kfree (file->f_description); + file->f_description = d; +spin_unlock (&file->f_lock); + +return 0 ; +} +static long do_fcntl (int fd,unsigned int cmd,unsigned long arg,struct file *filp) {
@@ -426,6 +442,9 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,case F_SET_FILE_RW_HINT : err =fcntl_rw_hint (filp, cmd, arg);break ; +case F_SET_DESCRIPTION : + err =fcntl_set_description (filp, argp); +break ;default :break ; }
fs/file_table.c +3 -0
@@ -272,6 +272,9 @@ static void __fput(struct file *file)eventpoll_release (file);locks_remove_file (file); +if (file->f_description) +kfree (file->f_description); +ima_file_free (file);if (unlikely (file->f_flags &FASYNC )) {if (file->f_op->fasync)
fs/proc/fd.c +5 -0
@@ -57,6 +57,11 @@ static int seq_show(struct seq_file *m, void *v) (long long )file->f_pos, f_flags, real_mount(file->f_path.mnt)->mnt_id); +spin_lock (&file->f_lock); +if (file->f_description) +seq_printf (m,"desc:\t%s\n" , file->f_description); +spin_unlock (&file->f_lock); +show_fd_locks (m, file, files);if (seq_has_overflowed (m)) goto out;
include/linux/fs.h +3 -0
@@ -980,6 +980,9 @@ struct file {struct address_space *f_mapping;errseq_t f_wb_err;errseq_t f_sb_err;/* for syncfs */ + +#define MAX_FILE_DESC_SIZE 256 +char *f_description; }__randomize_layout __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */
include/uapi/linux/fcntl.h +5 -0
@@ -55,6 +55,11 @@#define F_GET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 13)#define F_SET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 14) +/* +* Set file description +*/ +#define F_SET_DESCRIPTION (F_LINUX_SPECIFIC_BASE + 15) +/* * Valid hint values for F_{GET,SET}_RW_HINT. 0 is "not set", or can be * used to clear any hints previously set.
scripts/get_maintainer.pl +2 -0
@@ -2309,6 +2309,8 @@ sub vcs_file_blame { my @commit_signers = (); my $cmd; + print STDERR "checking commit $commit\n"; + $cmd = $VCS_cmds{"find_commit_signers_cmd"}; $cmd =~ s/(\$\w+)/$1/eeg; #substitute variables in $cmd
Signature · 1 line
2.25.1
PATCH v3 1/1 This command attaches a description to a file descriptor for troubleshooting purposes. The free string is displayed in the process fdinfo file for that fd /proc/pid/fdinfo/fd.
One intended usage is to allow processes to self-document sockets for netstat and friends to report
--- Documentation/filesystems/proc.rst | 3 +++ fs/fcntl.c | 19 +++++++++++++++++++ fs/file_table.c | 2 ++ fs/proc/fd.c | 5 +++++ include/linux/fs.h | 3 +++ include/uapi/linux/fcntl.h | 5 +++++ 6 files changed, 37 insertions(+)
Documentation/filesystems/proc.rst +3 -0
@@ -1918,6 +1918,9 @@ A typical output is:: flags: 0100002 mnt_id: 19 +An optional 'desc' is set if the process documented its usage of +the file via the fcntl command F_SET_DESCRIPTION. + All locks associated with a file descriptor are shown in its fdinfo too:: lock: 1: FLOCK ADVISORY WRITE 359 00:13:11691 0 EOF
fs/fcntl.c +19 -0
@@ -319,6 +319,22 @@ static long fcntl_rw_hint(struct file *file, unsigned int cmd, } } +static long fcntl_set_description (struct file *file,char __user *desc) +{ +char *d; + + d =strndup_user (desc,MAX_FILE_DESC_SIZE ); +if (IS_ERR (d)) +return PTR_ERR (d); + +spin_lock (&file->f_lock); +kfree (file->f_description); + file->f_description = d; +spin_unlock (&file->f_lock); + +return 0 ; +} +static long do_fcntl (int fd,unsigned int cmd,unsigned long arg,struct file *filp) {
@@ -426,6 +442,9 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,case F_SET_FILE_RW_HINT : err =fcntl_rw_hint (filp, cmd, arg);break ; +case F_SET_DESCRIPTION : + err =fcntl_set_description (filp, argp); +break ;default :break ; }
fs/file_table.c +2 -0
@@ -272,6 +272,8 @@ static void __fput(struct file *file)eventpoll_release (file);locks_remove_file (file); +kfree (file->f_description); +ima_file_free (file);if (unlikely (file->f_flags &FASYNC )) {if (file->f_op->fasync)
fs/proc/fd.c +5 -0
@@ -57,6 +57,11 @@ static int seq_show(struct seq_file *m, void *v) (long long )file->f_pos, f_flags, real_mount(file->f_path.mnt)->mnt_id); +spin_lock (&file->f_lock); +if (file->f_description) +seq_printf (m,"desc:\t%s\n" , file->f_description); +spin_unlock (&file->f_lock); +show_fd_locks (m, file, files);if (seq_has_overflowed (m)) goto out;
include/linux/fs.h +3 -0
@@ -980,6 +980,9 @@ struct file {struct address_space *f_mapping;errseq_t f_wb_err;errseq_t f_sb_err;/* for syncfs */ + +#define MAX_FILE_DESC_SIZE 256 +char *f_description; }__randomize_layout __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */
include/uapi/linux/fcntl.h +5 -0
@@ -55,6 +55,11 @@#define F_GET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 13)#define F_SET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 14) +/* +* Set file description +*/ +#define F_SET_DESCRIPTION (F_LINUX_SPECIFIC_BASE + 15) +/* * Valid hint values for F_{GET,SET}_RW_HINT. 0 is "not set", or can be * used to clear any hints previously set.
Signature · 1 line
2.25.1
Re: [PATCH v3] This command attaches a description to a file descriptor for troubleshooting purposes. The free string is displayed in the process fdinfo file for that fd /proc/pid/fdinfo/fd.
Sorry about all the noise, please disregard this one.
Quoted 127 lines · Pascal Bouchareine
One intended usage is to allow processes to self-document sockets for netstat and friends to report
Signed-off-by Pascal Bouchareine <[email protected]>--- Documentation/filesystems/proc.rst | 3 +++ fs/fcntl.c | 19 +++++++++++++++++++ fs/file_table.c | 2 ++ fs/proc/fd.c | 5 +++++ include/linux/fs.h | 3 +++ include/uapi/linux/fcntl.h | 5 +++++ 6 files changed, 37 insertions(+)Documentation/filesystems/proc.rst +3
-0@@ -1918,6 +1918,9 @@ A typical output is:: flags: 0100002 mnt_id: 19 +An optional 'desc' is set if the process documented its usage of +the file via the fcntl command F_SET_DESCRIPTION. + All locks associated with a file descriptor are shown in its fdinfo too:: lock: 1: FLOCK ADVISORY WRITE 359 00:13:11691 0 EOFfs/fcntl.c +19
-0@@ -319,6 +319,22 @@ static long fcntl_rw_hint(struct file *file, unsigned int cmd, } } +static long fcntl_set_description (struct file *file,char __user *desc) +{ +char *d; + + d =strndup_user (desc,MAX_FILE_DESC_SIZE ); +if (IS_ERR (d)) +return PTR_ERR (d); + +spin_lock (&file->f_lock); +kfree (file->f_description); + file->f_description = d; +spin_unlock (&file->f_lock); + +return 0 ; +} +static long do_fcntl (int fd,unsigned int cmd,unsigned long arg,struct file *filp) {@@ -426,6 +442,9 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,case F_SET_FILE_RW_HINT : err =fcntl_rw_hint (filp, cmd, arg);break ; +case F_SET_DESCRIPTION : + err =fcntl_set_description (filp, argp); +break ;default :break ; }fs/file_table.c +2
-0@@ -272,6 +272,8 @@ static void __fput(struct file *file)eventpoll_release (file);locks_remove_file (file); +kfree (file->f_description); +ima_file_free (file);if (unlikely (file->f_flags &FASYNC )) {if (file->f_op->fasync)fs/proc/fd.c +5
-0@@ -57,6 +57,11 @@ static int seq_show(struct seq_file *m, void *v) (long long )file->f_pos, f_flags, real_mount(file->f_path.mnt)->mnt_id); +spin_lock (&file->f_lock); +if (file->f_description) +seq_printf (m,"desc:\t%s\n" , file->f_description); +spin_unlock (&file->f_lock); +show_fd_locks (m, file, files);if (seq_has_overflowed (m)) goto out;include/linux/fs.h +3
-0@@ -980,6 +980,9 @@ struct file {struct address_space *f_mapping;errseq_t f_wb_err;errseq_t f_sb_err;/* for syncfs */ + +#define MAX_FILE_DESC_SIZE 256 +char *f_description; }__randomize_layout __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */include/uapi/linux/fcntl.h +5
-1@@ -55,6 +55,11 @@#define F_GET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 13)#define F_SET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 14) +/* +* Set file description +*/ +#define F_SET_DESCRIPTION (F_LINUX_SPECIFIC_BASE + 15) +/* * Valid hint values for F_{GET,SET}_RW_HINT. 0 is "not set", or can be * used to clear any hints previously set. -- 2.25.1
PATCH v3 1/1 proc,fcntl: introduce F_SET_DESCRIPTION
In reply to Pascal BouchareineThis command attaches a description to a file descriptor for troubleshooting purposes. The free string is displayed in the process fdinfo file for that fd /proc/pid/fdinfo/fd.
One intended usage is to allow processes to self-document sockets for netstat and friends to report
--- Documentation/filesystems/proc.rst | 3 +++ fs/fcntl.c | 19 +++++++++++++++++++ fs/file_table.c | 2 ++ fs/proc/fd.c | 5 +++++ include/linux/fs.h | 3 +++ include/uapi/linux/fcntl.h | 5 +++++ 6 files changed, 37 insertions(+)
Documentation/filesystems/proc.rst +3 -0
@@ -1918,6 +1918,9 @@ A typical output is:: flags: 0100002 mnt_id: 19 +An optional 'desc' is set if the process documented its usage of +the file via the fcntl command F_SET_DESCRIPTION. + All locks associated with a file descriptor are shown in its fdinfo too:: lock: 1: FLOCK ADVISORY WRITE 359 00:13:11691 0 EOF
fs/fcntl.c +19 -0
@@ -319,6 +319,22 @@ static long fcntl_rw_hint(struct file *file, unsigned int cmd, } } +static long fcntl_set_description (struct file *file,char __user *desc) +{ +char *d; + + d =strndup_user (desc,MAX_FILE_DESC_SIZE ); +if (IS_ERR (d)) +return PTR_ERR (d); + +spin_lock (&file->f_lock); +kfree (file->f_description); + file->f_description = d; +spin_unlock (&file->f_lock); + +return 0 ; +} +static long do_fcntl (int fd,unsigned int cmd,unsigned long arg,struct file *filp) {
@@ -426,6 +442,9 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,case F_SET_FILE_RW_HINT : err =fcntl_rw_hint (filp, cmd, arg);break ; +case F_SET_DESCRIPTION : + err =fcntl_set_description (filp, argp); +break ;default :break ; }
fs/file_table.c +2 -0
@@ -272,6 +272,8 @@ static void __fput(struct file *file)eventpoll_release (file);locks_remove_file (file); +kfree (file->f_description); +ima_file_free (file);if (unlikely (file->f_flags &FASYNC )) {if (file->f_op->fasync)
fs/proc/fd.c +5 -0
@@ -57,6 +57,11 @@ static int seq_show(struct seq_file *m, void *v) (long long )file->f_pos, f_flags, real_mount(file->f_path.mnt)->mnt_id); +spin_lock (&file->f_lock); +if (file->f_description) +seq_printf (m,"desc:\t%s\n" , file->f_description); +spin_unlock (&file->f_lock); +show_fd_locks (m, file, files);if (seq_has_overflowed (m)) goto out;
include/linux/fs.h +3 -0
@@ -980,6 +980,9 @@ struct file {struct address_space *f_mapping;errseq_t f_wb_err;errseq_t f_sb_err;/* for syncfs */ + +#define MAX_FILE_DESC_SIZE 256 +char *f_description; }__randomize_layout __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */
include/uapi/linux/fcntl.h +5 -0
@@ -55,6 +55,11 @@#define F_GET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 13)#define F_SET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 14) +/* +* Set file description +*/ +#define F_SET_DESCRIPTION (F_LINUX_SPECIFIC_BASE + 15) +/* * Valid hint values for F_{GET,SET}_RW_HINT. 0 is "not set", or can be * used to clear any hints previously set.
Signature · 1 line
2.25.1
fs/fcntl.c static long fcntl_rw_hint(struct file *file, unsigned int cmd, · line 326
Quoted 12 lines · Pascal Bouchareine
This command attaches a description to a file descriptor for troubleshooting purposes. The free string is displayed in the process fdinfo file for that fd
/proc/pid/fdinfo/fd.One intended usage is to allow processes to self-document sockets for netstat and friends to report
fs/fcntl.c +5
-0+static long fcntl_set_description (struct file *file,char __user *desc) +{ +char *d; + + d =strndup_user (desc,MAX_FILE_DESC_SIZE );
This should be kmem accounted because allocation is persistent. To make things more entertaining, strndup_user() doesn't have gfp_t argument.
fs/fcntl.c static long fcntl_rw_hint(struct file *file, unsigned int cmd, · line 333
Quoted 7 lines
fs/fcntl.c +7
-0+if (IS_ERR (d)) +return PTR_ERR (d); + +spin_lock (&file->f_lock); +kfree (file->f_description); + file->f_description = d; +spin_unlock (&file->f_lock);
Generally kfree under spinlock is not good idea. You can replace the pointer and free without spinlock.
include/linux/fs.h struct file { · line 985
Quoted 9 lines
include/linux/fs.h +3
-0@@ -980,6 +980,9 @@ struct file {struct address_space *f_mapping;errseq_t f_wb_err;errseq_t f_sb_err;/* for syncfs */ + +#define MAX_FILE_DESC_SIZE 256 +char *f_description;
struct file is nicely aligned to 256 bytes on distro configs. Will this break everything?
$ cat /sys/kernel/slab/filp/object_size
Thanks for reviewing, I added some questions inline below
Quoted 4 lines · Alexey Dobriyan
Quoted 1 line
+ d =
strndup_user(desc, MAX_FILE_DESC_SIZE);This should be kmem accounted because allocation is persistent. To make things more entertaining,
strndup_user()doesn't havegfp_targument.
I had to look it up so I might be very far from it, but is that __GFP_ACCOUNT and would it be correct to assume memdup_user() should use it unconditionally?
Otherwise my simple option would be to implement the logic in the set_description, but the benefit would be very local.
Please let me know what you think is best, happy to read more doc if there's a more productive way to address that
Quoted 11 lines
fs/fcntl.c static long fcntl_rw_hint(struct file *file, unsigned int cmd, · line 333
Quoted 8 lines
fs/fcntl.c +7
-0+if (IS_ERR (d)) +return PTR_ERR (d); + +spin_lock (&file->f_lock); +kfree (file->f_description); + file->f_description = d; +spin_unlock (&file->f_lock);Generally kfree under spinlock is not good idea. You can replace the pointer and free without spinlock.
Sorry I also need some pointers here - do you suggest I move the kfree out of the locked section or that there is a safe way other than spinlock?
Quoted 4 lines
struct file is nicely aligned to 256 bytes on distro configs. Will this break everything?
$ cat /sys/kernel/slab/filp/object_size
Indeed on the config I'm using here this jumped to 264 bytes
Would it be better to move this to the inode struct? I don't know the implications of this - any other option?
Thanks!
Quoted 9 lines · Pascal Bouchareine
Quoted 4 lines
struct file is nicely aligned to 256 bytes on distro configs. Will this break everything?
$ cat /sys/kernel/slab/filp/object_sizeIndeed on the config I'm using here this jumped to 264 bytes
Would it be better to move this to the inode struct? I don't know the implications of this - any other option?
Well it doesn't actually make much sense to share that at that level
However the above 264 is building from 5.4 without f_sb_err, so I think master already passed the 256 bytes too ?