Skip to contentSkip to conversation

proc: use kmalloc instead of __get_free_page() to alloc path buffer

PATCH In review Hao Lee 3 participants 5 messages Jan 23, 2022 – Jan 25, 2022

Subsystems (2)
lore
Share & tools
lore

Hao Lee

author
#0 lore raw

PATCH 1/1 proc: use kmalloc instead of __get_free_page() to alloc path buffer

It's not a standard approach that use __get_free_page() to alloc path buffer directly. We'd better use kmalloc and PATH_MAX.

---
 fs/proc/base.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
fs/proc/base.c +6 -5
index d654ce7150fd..74cfef87fe45 100644
@@ -1764,25 +1764,26 @@ static const char *proc_pid_get_link(struct dentry *dentry,
 
 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
 {
-	char *tmp = (char *)__get_free_page(GFP_KERNEL);
+	char *buf = NULL;
 	char *pathname;
 	int len;
 
-	if (!tmp)
+	buf = kmalloc(PATH_MAX, GFP_KERNEL);
+	if (!buf)
 		return -ENOMEM;
 
-	pathname = d_path(path, tmp, PAGE_SIZE);
+	pathname = d_path(path, buf, PATH_MAX);
 	len = PTR_ERR(pathname);
 	if (IS_ERR(pathname))
 		goto out;
-	len = tmp + PAGE_SIZE - 1 - pathname;
+	len = buf + PATH_MAX - 1 - pathname;
 
 	if (len > buflen)
 		len = buflen;
 	if (copy_to_user(buffer, pathname, len))
 		len = -EFAULT;
  out:
-	free_page((unsigned long)tmp);
+	kfree(buf);
 	return len;
 }
 
Signature · 1 line
2.34.1
#1 lore raw

fs/proc/base.c static const char *proc_pid_get_link(struct dentry *dentry, · line 1767

Quoted 11 lines · Hao Lee

It's not a standard approach that use __get_free_page() to alloc path buffer directly. We'd better use kmalloc and PATH_MAX.

fs/proc/base.c +1 -1
@@ -1764,25 +1764,26 @@ static const char *proc_pid_get_link(struct dentry *dentry,
 
 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
 {
-	char *tmp = (char *)__get_free_page(GFP_KERNEL);
+	char *buf = NULL;

I'd rather not rename anything but keep it minimal.

#2 lore raw

PATCH v2 1/1 proc: alloc PATH_MAX bytes for /proc/${pid}/fd/ symlinks

From: Hao Lee <[email protected]>

It's not a standard approach that use __get_free_page() to alloc path buffer directly. We'd better use kmalloc and PATH_MAX.

PAGE_SIZE is different on different archs. An unlinked file with very long canonical pathname will readlink differently
because "(deleted)" eats into a buffer. --adobriyan

---

 fs/proc/base.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/proc/base.c +4 -4
@@ -1764,25 +1764,25 @@ static const char *proc_pid_get_link(struct dentry *dentry,
 
 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
 {
-	char *tmp = (char *)__get_free_page(GFP_KERNEL);
+	char *tmp = (char *)kmalloc(PATH_MAX, GFP_KERNEL);
 	char *pathname;
 	int len;
 
 	if (!tmp)
 		return -ENOMEM;
 
-	pathname = d_path(path, tmp, PAGE_SIZE);
+	pathname = d_path(path, tmp, PATH_MAX);
 	len = PTR_ERR(pathname);
 	if (IS_ERR(pathname))
 		goto out;
-	len = tmp + PAGE_SIZE - 1 - pathname;
+	len = tmp + PATH_MAX - 1 - pathname;
 
 	if (len > buflen)
 		len = buflen;
 	if (copy_to_user(buffer, pathname, len))
 		len = -EFAULT;
  out:
-	free_page((unsigned long)tmp);
+	kfree(tmp);
 	return len;
 }
 
author
#3 lore raw

Re: [PATCH v2] proc: alloc PATH_MAX bytes for /proc/${pid}/fd/ symlinks

Quoted 7 lines · Alexey Dobriyan

From: Hao Lee <[email protected]>

It's not a standard approach that use __get_free_page() to alloc path buffer directly. We'd better use kmalloc and PATH_MAX.

PAGE_SIZE is different on different archs.

This is indeed a worthy addition. Thanks.

Regards,
Hao Lee

Quoted 43 lines

An unlinked file
with very long canonical pathname will readlink differently because "(deleted)" eats into a buffer. --adobriyan

---

 fs/proc/base.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/proc/base.c +4 -4
@@ -1764,25 +1764,25 @@ static const char *proc_pid_get_link(struct dentry *dentry,
 
 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
 {
-       char *tmp = (char *)__get_free_page(GFP_KERNEL);
+       char *tmp = (char *)kmalloc(PATH_MAX, GFP_KERNEL);
        char *pathname;
        int len;
 
        if (!tmp)
                return -ENOMEM;
 
-       pathname = d_path(path, tmp, PAGE_SIZE);
+       pathname = d_path(path, tmp, PATH_MAX);
        len = PTR_ERR(pathname);
        if (IS_ERR(pathname))
                goto out;
-       len = tmp + PAGE_SIZE - 1 - pathname;
+       len = tmp + PATH_MAX - 1 - pathname;
 
        if (len > buflen)
                len = buflen;
        if (copy_to_user(buffer, pathname, len))
                len = -EFAULT;
  out:
-       free_page((unsigned long)tmp);
+       kfree(tmp);
        return len;
 }
 
#4 lore raw
In reply to Hao Lee
Quoted 2 lines · Hao Lee

It's not a standard approach that use __get_free_page() to alloc path buffer directly. We'd better use kmalloc and PATH_MAX.

If we're allocating PATH_MAX sized buffers for names, wouldn't it be suitable to use the name slab names_cachep declared in include/linux/fs.h using __getname() and __putname()?

Best,
Torin