diff mbox series

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

Message ID 20220123100837.GA1491@haolee.io (mailing list archive)
State New, archived
Headers show
Series proc: use kmalloc instead of __get_free_page() to alloc path buffer | expand

Commit Message

Hao Lee Jan. 23, 2022, 10:08 a.m. UTC
It's not a standard approach that use __get_free_page() to alloc path
buffer directly. We'd better use kmalloc and PATH_MAX.

Signed-off-by: Hao Lee <haolee.swjtu@gmail.com>
---
 fs/proc/base.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

Comments

Alexey Dobriyan Jan. 23, 2022, 1:55 p.m. UTC | #1
On Sun, Jan 23, 2022 at 10:08:37AM +0000, Hao Lee wrote:
> It's not a standard approach that use __get_free_page() to alloc path
> buffer directly. We'd better use kmalloc and PATH_MAX.

> --- a/fs/proc/base.c
> +++ b/fs/proc/base.c
> @@ -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.
Torin Carey Jan. 25, 2022, 2:03 p.m. UTC | #2
On Sun, Jan 23, 2022 at 10:08:37AM +0000, Hao Lee wrote:
> 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
diff mbox series

Patch

diff --git a/fs/proc/base.c b/fs/proc/base.c
index d654ce7150fd..74cfef87fe45 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -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;
 }