[PATCH v2] userns: skip id map lookup for init_user_ns

From: Li Qiang

Date: Sat Dec 05 2026 - 04:51:17 EST


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%

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
--
2.43.0