* [PATCH] mm/gup: add NULL check for unlocked parameter in fixup_user_fault()
@ 2026-08-26 6:31 Liu Dalin
2026-08-26 7:49 ` David Hildenbrand (Arm)
0 siblings, 1 reply; 4+ messages in thread
From: Liu Dalin @ 2026-08-26 6:31 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, Jason Gunthorpe
Cc: John Hubbard, Peter Xu, linux-mm, linux-kernel, Liu Dalin,
Deng Yingchao, Qin Yungao, Luo Qiu
While the current callers either pass a valid pointer or explicitly
pass NULL (indicating they don't need the unlock notification), it is
safer to add a defensive NULL check before dereferencing. This prevents
a kernel crash if any caller passes NULL and handle_mm_fault() returns
VM_FAULT_COMPLETED or VM_FAULT_RETRY.
Fixes smatch warnings:
- mm/gup.c:1597 fixup_user_fault() error: we previously assumed 'unlocked' could be null (see line 1573)
Fixes: 4bbd4c776a63 ("mm: move get_user_pages()-related code to separate file")
Assisted-by: smatch:2.0 [static analysis]
Signed-off-by: Liu Dalin <liudalin@kylinsec.com.cn>
---
mm/gup.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/mm/gup.c b/mm/gup.c
index 0692119b7904..d12475a7f7cc 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -1594,7 +1594,8 @@ int fixup_user_fault(struct mm_struct *mm,
* could tell the callers so they do not need to unlock.
*/
mmap_read_lock(mm);
- *unlocked = true;
+ if (unlocked)
+ *unlocked = true;
return 0;
}
@@ -1608,7 +1609,8 @@ int fixup_user_fault(struct mm_struct *mm,
if (ret & VM_FAULT_RETRY) {
mmap_read_lock(mm);
- *unlocked = true;
+ if (unlocked)
+ *unlocked = true;
fault_flags |= FAULT_FLAG_TRIED;
goto retry;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] mm/gup: add NULL check for unlocked parameter in fixup_user_fault()
2026-08-26 6:31 [PATCH] mm/gup: add NULL check for unlocked parameter in fixup_user_fault() Liu Dalin
@ 2026-08-26 7:49 ` David Hildenbrand (Arm)
0 siblings, 0 replies; 4+ messages in thread
From: David Hildenbrand (Arm) @ 2026-08-26 7:49 UTC (permalink / raw)
To: Liu Dalin, Andrew Morton, Jason Gunthorpe
Cc: John Hubbard, Peter Xu, linux-mm, linux-kernel, Deng Yingchao,
Qin Yungao, Luo Qiu
On 8/26/26 08:31, Liu Dalin wrote:
> While the current callers either pass a valid pointer or explicitly
> pass NULL (indicating they don't need the unlock notification), it is
> safer to add a defensive NULL check before dereferencing. This prevents
> a kernel crash if any caller passes NULL and handle_mm_fault() returns
> VM_FAULT_COMPLETED or VM_FAULT_RETRY.
>
> Fixes smatch warnings:
> - mm/gup.c:1597 fixup_user_fault() error: we previously assumed 'unlocked' could be null (see line 1573)
>
> Fixes: 4bbd4c776a63 ("mm: move get_user_pages()-related code to separate file")
If this is not an actual bugfix, we should specify Fixes:.
> Assisted-by: smatch:2.0 [static analysis]
> Signed-off-by: Liu Dalin <liudalin@kylinsec.com.cn>
You seem to have sent this patch twice by accident. :)
> ---
> mm/gup.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/mm/gup.c b/mm/gup.c
> index 0692119b7904..d12475a7f7cc 100644
> --- a/mm/gup.c
> +++ b/mm/gup.c
> @@ -1594,7 +1594,8 @@ int fixup_user_fault(struct mm_struct *mm,
> * could tell the callers so they do not need to unlock.
> */
> mmap_read_lock(mm);
> - *unlocked = true;
> + if (unlocked)
> + *unlocked = true;
> return 0;
> }
>
> @@ -1608,7 +1609,8 @@ int fixup_user_fault(struct mm_struct *mm,
>
> if (ret & VM_FAULT_RETRY) {
> mmap_read_lock(mm);
> - *unlocked = true;
> + if (unlocked)
> + *unlocked = true;
> fault_flags |= FAULT_FLAG_TRIED;
> goto retry;
> }
smatch doesn't understand that VM_FAULT_RETRY|VM_FAULT_COMPLETED implies that
FAULT_FLAG_ALLOW_RETRY must be set, which implies that the lock may be dropped,
which implies that unlocked is mandatory to be specified by the caller.
Checking for "unlocked" when we just did a mmap_read_lock() does not make any
sense, sorry.
Is there a way to teach smatch about this differently?
VM_WARN_ON_ONCE(unlocked);
could be used, but I am also not quite happy about that.
--
Cheers,
David
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH] mm/gup: add NULL check for unlocked parameter in fixup_user_fault()
@ 2026-08-26 6:29 Liu Dalin
2026-08-26 14:10 ` Jason Gunthorpe
0 siblings, 1 reply; 4+ messages in thread
From: Liu Dalin @ 2026-08-26 6:29 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, Jason Gunthorpe
Cc: John Hubbard, Peter Xu, linux-mm, linux-kernel, Liu Dalin,
Deng yingchao, Qin yungao, Luo Qiu
While the current callers either pass a valid pointer or explicitly
pass NULL (indicating they don't need the unlock notification), it is
safer to add a defensive NULL check before dereferencing. This prevents
a kernel crash if any caller passes NULL and handle_mm_fault() returns
VM_FAULT_COMPLETED or VM_FAULT_RETRY.
Fixes smatch warnings:
- mm/gup.c:1597 fixup_user_fault() error: we previously assumed 'unlocked' could be null (see line 1573)
Fixes: 4bbd4c776a63 ("mm: move get_user_pages()-related code to separate file")
Assisted-by: smatch:2.0 [static analysis]
Signed-off-by: Liu Dalin <liudalin@kylinsec.com.cn>
---
mm/gup.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/mm/gup.c b/mm/gup.c
index 0692119b7904..d12475a7f7cc 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -1594,7 +1594,8 @@ int fixup_user_fault(struct mm_struct *mm,
* could tell the callers so they do not need to unlock.
*/
mmap_read_lock(mm);
- *unlocked = true;
+ if (unlocked)
+ *unlocked = true;
return 0;
}
@@ -1608,7 +1609,8 @@ int fixup_user_fault(struct mm_struct *mm,
if (ret & VM_FAULT_RETRY) {
mmap_read_lock(mm);
- *unlocked = true;
+ if (unlocked)
+ *unlocked = true;
fault_flags |= FAULT_FLAG_TRIED;
goto retry;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] mm/gup: add NULL check for unlocked parameter in fixup_user_fault()
2026-08-26 6:29 Liu Dalin
@ 2026-08-26 14:10 ` Jason Gunthorpe
0 siblings, 0 replies; 4+ messages in thread
From: Jason Gunthorpe @ 2026-08-26 14:10 UTC (permalink / raw)
To: Liu Dalin
Cc: Andrew Morton, David Hildenbrand, John Hubbard, Peter Xu,
linux-mm, linux-kernel, Liu Dalin, Deng yingchao, Qin yungao,
Luo Qiu
On Wed, Aug 26, 2026 at 02:29:43PM +0800, Liu Dalin wrote:
> While the current callers either pass a valid pointer or explicitly
> pass NULL (indicating they don't need the unlock notification), it is
> safer to add a defensive NULL check before dereferencing. This prevents
> a kernel crash if any caller passes NULL and handle_mm_fault() returns
> VM_FAULT_COMPLETED or VM_FAULT_RETRY.
>
> Fixes smatch warnings:
> - mm/gup.c:1597 fixup_user_fault() error: we previously assumed 'unlocked' could be null (see line 1573)
It would be better to remove the previous check:
if (unlocked)
fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
And update the kdoc if it is now true that the unlocked parameter
cannot be null
Jason
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-08-26 14:10 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-26 6:31 [PATCH] mm/gup: add NULL check for unlocked parameter in fixup_user_fault() Liu Dalin
2026-08-26 7:49 ` David Hildenbrand (Arm)
-- strict thread matches above, loose matches on Subject: below --
2026-08-26 6:29 Liu Dalin
2026-08-26 14:10 ` Jason Gunthorpe
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox