2020-08-14 04:00:20

by Pascal Bouchareine

[permalink] [raw]
Subject: [PATCH v4 0/2] proc,fcntl: introduce F_SET_DESCRIPTION

This is a first attempt at taking Alexey's comments into account

This goes against v5.8

tl;dr in commit 2/2 but motivation is also described a bit in
https://lore.kernel.org/linux-api/CAGbU3_nVvuzMn2wo4_ZKufWcGfmGsopVujzTWw-Bbeky=xS+GA@mail.gmail.com/



2020-08-14 04:00:28

by Pascal Bouchareine

[permalink] [raw]
Subject: [PATCH v4 2/2] 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

Signed-off-by: Pascal Bouchareine <[email protected]>
---
fs/fcntl.c | 21 +++++++++++++++++++++
fs/file_table.c | 2 ++
fs/proc/fd.c | 5 +++++
include/linux/fs.h | 3 +++
include/uapi/linux/fcntl.h | 5 +++++
5 files changed, 36 insertions(+)

diff --git a/fs/fcntl.c b/fs/fcntl.c
index 2e4c0fa2074b..9fbeaaf02802 100644
--- a/fs/fcntl.c
+++ b/fs/fcntl.c
@@ -319,6 +319,24 @@ static long fcntl_rw_hint(struct file *file, unsigned int cmd,
}
}

+static long fcntl_set_description(struct file *file, char __user *desc)
+{
+ char *d, *old;
+
+ d = strndup_user(desc, MAX_FILE_DESC_SIZE, GFP_KERNEL_ACCOUNT);
+ if (IS_ERR(d))
+ return PTR_ERR(d);
+
+ spin_lock(&file->f_lock);
+ old = file->f_description;
+ file->f_description = d;
+ spin_unlock(&file->f_lock);
+
+ kfree(old);
+
+ return 0;
+}
+
static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
struct file *filp)
{
@@ -426,6 +444,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;
}
diff --git a/fs/file_table.c b/fs/file_table.c
index 656647f9575a..6673a48d2ea1 100644
--- a/fs/file_table.c
+++ b/fs/file_table.c
@@ -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)
diff --git a/fs/proc/fd.c b/fs/proc/fd.c
index 81882a13212d..60b3ff971b2b 100644
--- a/fs/proc/fd.c
+++ b/fs/proc/fd.c
@@ -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;
diff --git a/include/linux/fs.h b/include/linux/fs.h
index f5abba86107d..a2a683d603b6 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -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 */

diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h
index 2f86b2ad6d7e..465385e52f49 100644
--- a/include/uapi/linux/fcntl.h
+++ b/include/uapi/linux/fcntl.h
@@ -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

2020-08-14 05:29:20

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH v4 2/2] fcntl: introduce F_SET_DESCRIPTION

On Thu, Aug 13, 2020 at 08:54:53PM -0700, Pascal Bouchareine wrote:
> 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

NAK. There is no way we're going to bloat a criticial structure like
struct file for some vanity information like this.

2020-08-14 05:50:06

by Pascal Bouchareine

[permalink] [raw]
Subject: Re: [PATCH v4 2/2] fcntl: introduce F_SET_DESCRIPTION

On Thu, Aug 13, 2020 at 10:28 PM Christoph Hellwig <[email protected]> wrote:
> > One intended usage is to allow processes to self-document sockets
> > for netstat and friends to report
>
> NAK. There is no way we're going to bloat a criticial structure like
> struct file for some vanity information like this.

The useful case is for sockets - Is there a more suited place to do that?
Do you think adding a setsockopt and sk_description to struct sock
would work, or would be considered the same?