mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] userns: skip id map lookup for init_user_ns
@ 2026-12-04  6:47 Li Qiang
  2026-12-04  7:43 ` Bradley Morgan
  2026-12-04  8:54 ` Alexander Mikhalitsyn
  0 siblings, 2 replies; 4+ messages in thread
From: Li Qiang @ 2026-12-04  6:47 UTC (permalink / raw)
  To: Christian Brauner
  Cc: Jan Kara, Seth Forshee, Alexander Viro, linux-fsdevel,
	containers, linux-kernel, Li Qiang

init_user_ns has an immutable [0, (u32)-2] identity map, so every u32 id
(including (u32)-1) maps to itself.  Return id directly when
ns == &init_user_ns, matching initial_idmapping(), to avoid smp_rmb()
and the map_id_down()/map_id_up() call.

Results on 256-CPU Hygon C86-4G (CONFIG_USER_NS=y,
CONFIG_MITIGATION_RETHUNK=y):

  getuid()                141.8 -> 128.5 ns    (-9.4%)
  getresuid()             366.1 -> 326.6 ns   (-10.8%)
  setfsuid(-1)            167.1 -> 141.6 ns   (-15.2%)
  fstat() (tmpfs)         437.4 -> 413.0 ns    (-5.6%)
  fstat() (256 threads)   447.3 -> 485.3 Mop/s (+8.5%)
  fstat() (CLONE_NEWUSER) 437.6 -> 437.6 ns    (-0.0%)

Signed-off-by: Li Qiang <liqiang01@kylinos.cn>
---
 kernel/user_namespace.c | 38 ++++++++++++++++++++++++++++++++------
 1 file changed, 32 insertions(+), 6 deletions(-)

diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
index cd748e1..dbdf827 100644
--- a/kernel/user_namespace.c
+++ b/kernel/user_namespace.c
@@ -407,6 +407,31 @@ u32 map_id_up(const struct uid_gid_map *map, u32 id)
 	return map_id_range_up(map, id, 1);
 }
 
+/*
+ * The uid, gid and projid maps of init_user_ns are statically initialized
+ * to a single identity extent covering [0, (u32)-2] and can never be
+ * written (proc_*_map_write() rejects namespaces without a parent).  A
+ * lookup in them therefore returns its input unchanged: every valid id maps
+ * to itself and (u32)-1, the only unmapped id, is returned as (u32)-1 which
+ * is exactly the lookup failure value.  As the maps never change, the
+ * smp_rmb() pairing with map_write() is not needed either.
+ */
+static inline u32 userns_map_id_down(const struct user_namespace *ns,
+				     const struct uid_gid_map *map, u32 id)
+{
+	if (ns == &init_user_ns)
+		return id;
+	return map_id_down(map, id);
+}
+
+static inline u32 userns_map_id_up(const struct user_namespace *ns,
+				   const struct uid_gid_map *map, u32 id)
+{
+	if (ns == &init_user_ns)
+		return id;
+	return map_id_up(map, id);
+}
+
 /**
  *	make_kuid - Map a user-namespace uid pair into a kuid.
  *	@ns:  User namespace that the uid is in
@@ -423,7 +448,7 @@ 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 */
-	return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
+	return KUIDT_INIT(userns_map_id_down(ns, &ns->uid_map, uid));
 }
 EXPORT_SYMBOL(make_kuid);
 
@@ -442,7 +467,7 @@ EXPORT_SYMBOL(make_kuid);
 uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
 {
 	/* Map the uid from a global kernel uid */
-	return map_id_up(&targ->uid_map, __kuid_val(kuid));
+	return userns_map_id_up(targ, &targ->uid_map, __kuid_val(kuid));
 }
 EXPORT_SYMBOL(from_kuid);
 
@@ -491,7 +516,7 @@ EXPORT_SYMBOL(from_kuid_munged);
 kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
 {
 	/* Map the gid to a global kernel gid */
-	return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
+	return KGIDT_INIT(userns_map_id_down(ns, &ns->gid_map, gid));
 }
 EXPORT_SYMBOL(make_kgid);
 
@@ -510,7 +535,7 @@ EXPORT_SYMBOL(make_kgid);
 gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
 {
 	/* Map the gid from a global kernel gid */
-	return map_id_up(&targ->gid_map, __kgid_val(kgid));
+	return userns_map_id_up(targ, &targ->gid_map, __kgid_val(kgid));
 }
 EXPORT_SYMBOL(from_kgid);
 
@@ -558,7 +583,7 @@ EXPORT_SYMBOL(from_kgid_munged);
 kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
 {
 	/* Map the uid to a global kernel uid */
-	return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
+	return KPROJIDT_INIT(userns_map_id_down(ns, &ns->projid_map, projid));
 }
 EXPORT_SYMBOL(make_kprojid);
 
@@ -577,7 +602,8 @@ EXPORT_SYMBOL(make_kprojid);
 projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
 {
 	/* Map the uid from a global kernel uid */
-	return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
+	return userns_map_id_up(targ, &targ->projid_map,
+				__kprojid_val(kprojid));
 }
 EXPORT_SYMBOL(from_kprojid);
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-05  9:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-04  6:47 [PATCH] userns: skip id map lookup for init_user_ns Li Qiang
2026-12-04  7:43 ` Bradley Morgan
2026-12-04  8:54 ` Alexander Mikhalitsyn
2026-12-05  9:51   ` Li Qiang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®