* [PATCH] selftests/proc: skip fd-003-kthread if run as non-root
@ 2026-11-20 6:29 Qingshuang Fu
2026-11-25 6:33 ` Andrew Morton
2026-11-25 6:34 ` + selftests-proc-skip-fd-003-kthread-if-run-as-non-root.patch added to mm-nonmm-unstable branch Andrew Morton
0 siblings, 2 replies; 4+ messages in thread
From: Qingshuang Fu @ 2026-11-20 6:29 UTC (permalink / raw)
To: Andy Whitcroft, Joe Perches, Shuah Khan, Alexey Dobriyan,
Andrew Morton
Cc: linux-fsdevel, linux-kselftest, linux-kernel, Qingshuang Fu
/proc/$KERNEL_THREAD/fd can only be opened with ptrace access, so
openat() on the "fd" directory of a kernel thread returns EACCES
when the test is run as non-root. kernel_thread_fd() conflates this
with "process is userspace process", the scan over /proc runs to
completion without finding a usable descriptor and the test exits
with 1 (KSFT_FAIL) instead of 4 (KSFT_SKIP):
$ ./fd-003-kthread; echo $?
1
Distinguish the two cases and skip the test like the rest of
selftests/proc does when privileges are missing, so that unprivileged
runs report "skip" instead of a false failure.
Fixes: b2f5de0334f0 ("tools/testing/selftests/proc: test /proc/*/fd a bit (+ PF_KTHREAD is ABI!)")
Signed-off-by: Qingshuang Fu <fuqingshuang@kylinos.cn>
---
tools/testing/selftests/proc/fd-003-kthread.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/tools/testing/selftests/proc/fd-003-kthread.c b/tools/testing/selftests/proc/fd-003-kthread.c
index dc591f97b63d..e939a7c5f546 100644
--- a/tools/testing/selftests/proc/fd-003-kthread.c
+++ b/tools/testing/selftests/proc/fd-003-kthread.c
@@ -36,6 +36,7 @@
*
* Return /proc/$PID/fd descriptor if process is kernel thread.
* Return -1 if a process is userspace process.
+ * Return -EACCES if run as non-root.
*/
static int kernel_thread_fd(unsigned int pid)
{
@@ -88,6 +89,8 @@ static int kernel_thread_fd(unsigned int pid)
fd = -1;
if (flags & PF_KHTREAD) {
fd = openat(dir_fd, "fd", O_RDONLY|O_DIRECTORY);
+ if (fd == -1 && errno == EACCES)
+ fd = -EACCES;
}
close(dir_fd);
return fd;
@@ -168,6 +171,8 @@ int main(void)
pid++;
}
/* EACCES if run as non-root. */
+ if (fd == -EACCES)
+ return 4;
if (pid >= 1024)
return 1;
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] selftests/proc: skip fd-003-kthread if run as non-root
2026-11-20 6:29 [PATCH] selftests/proc: skip fd-003-kthread if run as non-root Qingshuang Fu
@ 2026-11-25 6:33 ` Andrew Morton
2026-11-25 7:48 ` Qingshuang Fu
2026-11-25 6:34 ` + selftests-proc-skip-fd-003-kthread-if-run-as-non-root.patch added to mm-nonmm-unstable branch Andrew Morton
1 sibling, 1 reply; 4+ messages in thread
From: Andrew Morton @ 2026-11-25 6:33 UTC (permalink / raw)
To: Qingshuang Fu
Cc: Andy Whitcroft, Joe Perches, Shuah Khan, Alexey Dobriyan,
linux-fsdevel, linux-kselftest, linux-kernel
On Fri, 20 Nov 2026 14:29:33 +0800 Qingshuang Fu <fuqingshuang@kylinos.cn> wrote:
> /proc/$KERNEL_THREAD/fd can only be opened with ptrace access, so
> openat() on the "fd" directory of a kernel thread returns EACCES
> when the test is run as non-root. kernel_thread_fd() conflates this
> with "process is userspace process", the scan over /proc runs to
> completion without finding a usable descriptor and the test exits
> with 1 (KSFT_FAIL) instead of 4 (KSFT_SKIP):
>
> $ ./fd-003-kthread; echo $?
> 1
>
> Distinguish the two cases and skip the test like the rest of
> selftests/proc does when privileges are missing, so that unprivileged
> runs report "skip" instead of a false failure.
>
Fair enough.
> --- a/tools/testing/selftests/proc/fd-003-kthread.c
> +++ b/tools/testing/selftests/proc/fd-003-kthread.c
> @@ -36,6 +36,7 @@
> *
> * Return /proc/$PID/fd descriptor if process is kernel thread.
> * Return -1 if a process is userspace process.
> + * Return -EACCES if run as non-root.
> */
> static int kernel_thread_fd(unsigned int pid)
> {
> @@ -88,6 +89,8 @@ static int kernel_thread_fd(unsigned int pid)
> fd = -1;
> if (flags & PF_KHTREAD) {
> fd = openat(dir_fd, "fd", O_RDONLY|O_DIRECTORY);
> + if (fd == -1 && errno == EACCES)
> + fd = -EACCES;
> }
> close(dir_fd);
> return fd;
> @@ -168,6 +171,8 @@ int main(void)
> pid++;
> }
> /* EACCES if run as non-root. */
> + if (fd == -EACCES)
> + return 4;
> if (pid >= 1024)
> return 1;
Why "4"?
(and why didn't they call it EACCESS. Also s/creat/create/)
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] selftests/proc: skip fd-003-kthread if run as non-root
2026-11-25 6:33 ` Andrew Morton
@ 2026-11-25 7:48 ` Qingshuang Fu
0 siblings, 0 replies; 4+ messages in thread
From: Qingshuang Fu @ 2026-11-25 7:48 UTC (permalink / raw)
To: Andrew Morton
Cc: Andy Whitcroft, Joe Perches, Shuah Khan, Alexey Dobriyan,
linux-fsdevel, linux-kselftest, linux-kernel
在 2026/11/25 14:33, Andrew Morton 写道:
> Fair enough.
>
>> --- a/tools/testing/selftests/proc/fd-003-kthread.c
>> +++ b/tools/testing/selftests/proc/fd-003-kthread.c
>> @@ -36,6 +36,7 @@
>> *
>> * Return /proc/$PID/fd descriptor if process is kernel thread.
>> * Return -1 if a process is userspace process.
>> + * Return -EACCES if run as non-root.
>> */
>> static int kernel_thread_fd(unsigned int pid)
>> {
>> @@ -88,6 +89,8 @@ static int kernel_thread_fd(unsigned int pid)
>> fd = -1;
>> if (flags & PF_KHTREAD) {
>> fd = openat(dir_fd, "fd", O_RDONLY|O_DIRECTORY);
>> + if (fd == -1 && errno == EACCES)
>> + fd = -EACCES;
>> }
>> close(dir_fd);
>> return fd;
>> @@ -168,6 +171,8 @@ int main(void)
>> pid++;
>> }
>> /* EACCES if run as non-root. */
>> + if (fd == -EACCES)
>> + return 4;
>> if (pid >= 1024)
>> return 1;
>
> Why "4"?
4 is KSFT_SKIP. The convention in selftests/proc is a bare "return 4"
on skip paths -- 9 tests do it, e.g.:
proc-loadavg-001.c:32
proc-net-dev-lseek.c:35
proc-self-syscall.c:40
proc-self-wchan.c:30
setns-dcache.c:56
setns-sysvipc.c:55
Notably proc-pid-vm.c includes kselftest.h and still returns a bare
4 at the end of main(). So the bare 4 just follows the directory's
existing style.
>
> (and why didn't they call it EACCESS. Also s/creat/create/)
Ha. Renaming it now would break 50 years of userspace, but I agree
the historical spelling is unfortunate.
--
Qingshuang Fu
^ permalink raw reply [flat|nested] 4+ messages in thread
* + selftests-proc-skip-fd-003-kthread-if-run-as-non-root.patch added to mm-nonmm-unstable branch
2026-11-20 6:29 [PATCH] selftests/proc: skip fd-003-kthread if run as non-root Qingshuang Fu
2026-11-25 6:33 ` Andrew Morton
@ 2026-11-25 6:34 ` Andrew Morton
1 sibling, 0 replies; 4+ messages in thread
From: Andrew Morton @ 2026-11-25 6:34 UTC (permalink / raw)
To: mm-commits, shuah, joe, apw, akpm, adobriyan, fuqingshuang
The patch titled
Subject: selftests/proc: skip fd-003-kthread if run as non-root
has been added to the -mm mm-nonmm-unstable branch. Its filename is
selftests-proc-skip-fd-003-kthread-if-run-as-non-root.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/selftests-proc-skip-fd-003-kthread-if-run-as-non-root.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a
reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Qingshuang Fu <fuqingshuang@kylinos.cn>
Subject: selftests/proc: skip fd-003-kthread if run as non-root
Date: Fri, 20 Nov 2026 14:29:33 +0800
/proc/$KERNEL_THREAD/fd can only be opened with ptrace access, so
openat() on the "fd" directory of a kernel thread returns EACCES
when the test is run as non-root. kernel_thread_fd() conflates this
with "process is userspace process", the scan over /proc runs to
completion without finding a usable descriptor and the test exits
with 1 (KSFT_FAIL) instead of 4 (KSFT_SKIP):
$ ./fd-003-kthread; echo $?
1
Distinguish the two cases and skip the test like the rest of
selftests/proc does when privileges are missing, so that unprivileged
runs report "skip" instead of a false failure.
Link: https://patch.msgid.link/20261120062933.3169112-1-fuqingshuang@kylinos.cn
Fixes: b2f5de0334f0 ("tools/testing/selftests/proc: test /proc/*/fd a bit (+ PF_KTHREAD is ABI!)")
Signed-off-by: Qingshuang Fu <fuqingshuang@kylinos.cn>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Whitcroft <apw@canonical.com>
Cc: Joe Perches <joe@perches.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
tools/testing/selftests/proc/fd-003-kthread.c | 5 +++++
1 file changed, 5 insertions(+)
--- a/tools/testing/selftests/proc/fd-003-kthread.c~selftests-proc-skip-fd-003-kthread-if-run-as-non-root
+++ a/tools/testing/selftests/proc/fd-003-kthread.c
@@ -36,6 +36,7 @@
*
* Return /proc/$PID/fd descriptor if process is kernel thread.
* Return -1 if a process is userspace process.
+ * Return -EACCES if run as non-root.
*/
static int kernel_thread_fd(unsigned int pid)
{
@@ -88,6 +89,8 @@ static int kernel_thread_fd(unsigned int
fd = -1;
if (flags & PF_KHTREAD) {
fd = openat(dir_fd, "fd", O_RDONLY|O_DIRECTORY);
+ if (fd == -1 && errno == EACCES)
+ fd = -EACCES;
}
close(dir_fd);
return fd;
@@ -168,6 +171,8 @@ int main(void)
pid++;
}
/* EACCES if run as non-root. */
+ if (fd == -EACCES)
+ return 4;
if (pid >= 1024)
return 1;
_
Patches currently in -mm which might be from fuqingshuang@kylinos.cn are
selftests-proc-include-linux-mmanh-for-mremap_dontunmap.patch
selftests-proc-skip-fd-003-kthread-if-run-as-non-root.patch
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-25 7:49 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 6:29 [PATCH] selftests/proc: skip fd-003-kthread if run as non-root Qingshuang Fu
2026-11-25 6:33 ` Andrew Morton
2026-11-25 7:48 ` Qingshuang Fu
2026-11-25 6:34 ` + selftests-proc-skip-fd-003-kthread-if-run-as-non-root.patch added to mm-nonmm-unstable branch Andrew Morton
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.