]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
mm: refactor mm_access() to not return NULL
authorLorenzo Stoakes <lorenzo.stoakes@oracle.com>
Tue, 24 Sep 2024 20:10:23 +0000 (21:10 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Wed, 6 Nov 2024 00:56:23 +0000 (16:56 -0800)
mm_access() can return NULL if the mm is not found, but this is handled
the same as an error in all callers, with some translating this into an
-ESRCH error.

Only proc_mem_open() returns NULL if no mm is found, however in this case
it is clearer and makes more sense to explicitly handle the error.
Additionally we take the opportunity to refactor the function to eliminate
unnecessary nesting.

Simplify things by simply returning -ESRCH if no mm is found - this both
eliminates confusing use of the IS_ERR_OR_NULL() macro, and simplifies
callers which would return -ESRCH by returning this error directly.

[lorenzo.stoakes@oracle.com: prefer neater pointer error comparison]
Link: https://lkml.kernel.org/r/2fae1834-749a-45e1-8594-5e5979cf7103@lucifer.local
Link: https://lkml.kernel.org/r/20240924201023.193135-1-lorenzo.stoakes@oracle.com
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Suggested-by: Arnd Bergmann <arnd@arndb.de>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
fs/proc/base.c
kernel/fork.c
mm/madvise.c
mm/process_vm_access.c

index b31283d81c52ea2a984519dac166d9bcbb7c99a8..94112df5f2a24d0b0a3e848a26e1a954547c6f16 100644 (file)
@@ -832,19 +832,21 @@ static const struct file_operations proc_single_file_operations = {
 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
 {
        struct task_struct *task = get_proc_task(inode);
-       struct mm_struct *mm = ERR_PTR(-ESRCH);
+       struct mm_struct *mm;
 
-       if (task) {
-               mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
-               put_task_struct(task);
+       if (!task)
+               return ERR_PTR(-ESRCH);
 
-               if (!IS_ERR_OR_NULL(mm)) {
-                       /* ensure this mm_struct can't be freed */
-                       mmgrab(mm);
-                       /* but do not pin its memory */
-                       mmput(mm);
-               }
-       }
+       mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
+       put_task_struct(task);
+
+       if (IS_ERR(mm))
+               return mm == ERR_PTR(-ESRCH) ? NULL : mm;
+
+       /* ensure this mm_struct can't be freed */
+       mmgrab(mm);
+       /* but do not pin its memory */
+       mmput(mm);
 
        return mm;
 }
@@ -2208,7 +2210,7 @@ static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
                goto out_notask;
 
        mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
-       if (IS_ERR_OR_NULL(mm))
+       if (IS_ERR(mm))
                goto out;
 
        if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
index 22f43721d031d48fd5be2606e86642334be9735f..b2ab422f62309a477ab1128b032b61f7f8530202 100644 (file)
@@ -1546,8 +1546,9 @@ struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
                return ERR_PTR(err);
 
        mm = get_task_mm(task);
-       if (mm && mm != current->mm &&
-                       !ptrace_may_access(task, mode)) {
+       if (!mm) {
+               mm = ERR_PTR(-ESRCH);
+       } else if (mm != current->mm && !ptrace_may_access(task, mode)) {
                mmput(mm);
                mm = ERR_PTR(-EACCES);
        }
index ff139e57cca292edcae682b8afc3b7a8c43f1cab..50d223ab3894fe128c69b00b2bd607908b914dc6 100644 (file)
@@ -1511,8 +1511,8 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
 
        /* Require PTRACE_MODE_READ to avoid leaking ASLR metadata. */
        mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
-       if (IS_ERR_OR_NULL(mm)) {
-               ret = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
+       if (IS_ERR(mm)) {
+               ret = PTR_ERR(mm);
                goto release_task;
        }
 
index b308e96cd05a28f1bac259169fcf98a969294cf9..656d3e88755b6f3bc3157777844b306ae5fec7e5 100644 (file)
@@ -201,8 +201,8 @@ static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter,
        }
 
        mm = mm_access(task, PTRACE_MODE_ATTACH_REALCREDS);
-       if (!mm || IS_ERR(mm)) {
-               rc = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
+       if (IS_ERR(mm)) {
+               rc = PTR_ERR(mm);
                /*
                 * Explicitly map EACCES to EPERM as EPERM is a more
                 * appropriate error code for process_vw_readv/writev