Linux filesystem development
 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; 3+ 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 related	[flat|nested] 3+ messages in thread

* Re: [PATCH] userns: skip id map lookup for init_user_ns
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Bradley Morgan @ 2026-12-04  7:43 UTC (permalink / raw)
  To: liqiang01
  Cc: brauner, containers, jack, linux-fsdevel, linux-kernel, sforshee,
	viro

On 4 December 2026 06:47:59 GMT, Li Qiang <liqiang01@kylinos.cn> wrote:
>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%)

That's not stupidly significant. Well to me it's not.

>
>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.
>+ */

Why this comment length? Claude verbosity.. (sigh)

>+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);
>+}
>+

Thumbs up for the code tho.

> /**
>  *	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);
> 
>

--- Thanks!

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

* Re: [PATCH] userns: skip id map lookup for init_user_ns
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Alexander Mikhalitsyn @ 2026-12-04  8:54 UTC (permalink / raw)
  To: Li Qiang, Christian Brauner
  Cc: Jan Kara, Seth Forshee, Alexander Viro, linux-fsdevel, containers,
	linux-kernel

On Fri, 2026-12-04 at 14:47 +0800, Li Qiang wrote:
> 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>
> ---

Hi Li,

>  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.
> + */

I don't think we really need this comment, for anybody touching this
code it should be obvious. We tend to have comments only in really
non-trivial or error-prone cases. IMHO.

> +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)

Why do we need this function to have 3 arguments, when it could have
only 2 - (const struct user_namespace *ns) and (u32 id).

Then you can just take &ns->uid_map and pass it down.

P.S. This change makes sense to me, we even have something similar
already for &nop_mnt_idmap - look in make_vfsuid() for example.

Kind regards,
Alex

> +{
> +	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);
>  

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

end of thread, other threads:[~2026-12-04  8:54 UTC | newest]

Thread overview: 3+ 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

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