Re: [PATCH v2] userns: skip id map lookup for init_user_ns
From: Tao Cui
Date: Sun Dec 06 2026 - 00:54:41 EST
Hello, Qiang,
在 2026/12/5 17:49, Li Qiang 写道:
> Converting a UID, GID or project ID in init_user_ns calls
> map_id_down()/map_id_up(), executes smp_rmb(), and walks a single
> immutable identity extent. Return the input directly in the six
> make_k*() and from_k*() helpers when the namespace is init_user_ns,
> avoiding the call overhead and barrier. For (u32)-1, this returns the
> same value as a failed lookup.
>
> An earlier equivalent version measured on a 256-CPU Hygon C86-4G
> running a 6.6 kernel with user namespaces and return thunks enabled
> gave:
>
> Workload Baseline Patched Delta
> getuid() 141.8 ns 128.5 ns -9.4%
> getresuid() 366.1 ns 326.6 ns -10.8%
> setfsuid(-1) 167.1 ns 141.6 ns -15.2%
> fstat() on tmpfs 437.4 ns 413.0 ns -5.6%
> fstat(), 256 threads 447.3 Mop/s 485.3 Mop/s +8.5%
> fstat() in a child userns 437.6 ns 437.6 ns 0.0%
>
I tested v2 on top of your base 007722b8211f (7.4.0-rc5), KVM guest
on a Hygon host with rethunk active, gcc 12.3 build. Medians over
3 boots x 3 rounds:
getuid() 100.2 -> 95.9 ns (-4.3%)
getresuid() 265.5 -> 251.1 ns (-5.4%)
setfsuid(-1) 108.2 -> 100.9 ns (-6.8%)
fstat() on tmpfs 326.1 -> 319.2 ns (-2.1%)
fstat(), 16 threads +13.9%
fstat(), child userns unchanged
A getpid() control run in between stayed flat.
FWIW these numbers are with gcc, which keeps the extent lookup
out-of-line. clang inlines it into make_kuid()/from_kuid(), so
there v2 only drops the single-extent loop (~1 ns per call,
measured from a module) and the syscall numbers are within noise.
Maybe mention the compiler in the commit message?
Otherwise looks correct to me. (u32)-1 falls outside the identity
extent so the early return matches the lookup failure value, and
from_kuid_munged()/kuid_has_mapping() go through from_kuid()/
make_kuid() anyway.
Tested-by: Tao Cui <cuitao@xxxxxxxxxx>
Reviewed-by: Tao Cui <cuitao@xxxxxxxxxx>
Thanks,
Tao
> Signed-off-by: Li Qiang <liqiang01@xxxxxxxxxx>
> ---
> Changes in v2:
> - Drop the two three-argument wrappers. Check for init_user_ns in each
> conversion function so the fallback uses that ID type's map (Alex).
> - Drop the long comment (Alex, Bradley) and clarify the (u32)-1 case
> in the commit message.
> - Identify the 6.6 kernel used for the v1 measurements.
>
> v1: https://lore.kernel.org/r/20261204064759.49001-1-liqiang01@xxxxxxxxxx
>
> kernel/user_namespace.c | 24 ++++++++++++++++++------
> 1 file changed, 18 insertions(+), 6 deletions(-)
>
> diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
> index cd748e1cbace..6bfd7a0d1ad2 100644
> --- a/kernel/user_namespace.c
> +++ b/kernel/user_namespace.c
> @@ -422,7 +422,9 @@ u32 map_id_up(const struct uid_gid_map *map, u32 id)
> */
> kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
> {
> - /* Map the uid to a global kernel uid */
> + if (ns == &init_user_ns)
> + return KUIDT_INIT(uid);
> +
> return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
> }
> EXPORT_SYMBOL(make_kuid);
> @@ -441,7 +443,9 @@ EXPORT_SYMBOL(make_kuid);
> */
> uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
> {
> - /* Map the uid from a global kernel uid */
> + if (targ == &init_user_ns)
> + return __kuid_val(kuid);
> +
> return map_id_up(&targ->uid_map, __kuid_val(kuid));
> }
> EXPORT_SYMBOL(from_kuid);
> @@ -490,7 +494,9 @@ EXPORT_SYMBOL(from_kuid_munged);
> */
> kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
> {
> - /* Map the gid to a global kernel gid */
> + if (ns == &init_user_ns)
> + return KGIDT_INIT(gid);
> +
> return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
> }
> EXPORT_SYMBOL(make_kgid);
> @@ -509,7 +515,9 @@ EXPORT_SYMBOL(make_kgid);
> */
> gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
> {
> - /* Map the gid from a global kernel gid */
> + if (targ == &init_user_ns)
> + return __kgid_val(kgid);
> +
> return map_id_up(&targ->gid_map, __kgid_val(kgid));
> }
> EXPORT_SYMBOL(from_kgid);
> @@ -557,7 +565,9 @@ EXPORT_SYMBOL(from_kgid_munged);
> */
> kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
> {
> - /* Map the uid to a global kernel uid */
> + if (ns == &init_user_ns)
> + return KPROJIDT_INIT(projid);
> +
> return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
> }
> EXPORT_SYMBOL(make_kprojid);
> @@ -576,7 +586,9 @@ EXPORT_SYMBOL(make_kprojid);
> */
> projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
> {
> - /* Map the uid from a global kernel uid */
> + if (targ == &init_user_ns)
> + return __kprojid_val(kprojid);
> +
> return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
> }
> EXPORT_SYMBOL(from_kprojid);
>
> base-commit: 007722b8211f7b34fdf21b1e6a9c32497633ea29