Re: [PATCH v3] proc,fcntl: introduce F_SET_DESCRIPTION

From
Alexey Dobriyan <hidden>
Date
2020-07-27 14:30:38
Also in
linux-fsdevel, lkml
Reply to
Pascal Bouchareine, 2020-07-26
On Fri, Jul 24, 2020 at 10:22:36PM -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
quoted hunk ↗ jump to hunk
+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.
quoted hunk ↗ jump to hunk
+	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.
quoted hunk ↗ jump to hunk
--- 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;
struct file is nicely aligned to 256 bytes on distro configs.
Will this break everything?

	$ cat /sys/kernel/slab/filp/object_size
Thread (10 messages) 10 messages, 3 authors, last reply 2020-07-28
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help