From: Bradley Morgan <bradley.morgan@linux.dev>
To: liqiang01@kylinos.cn
Cc: brauner@kernel.org, containers@lists.linux.dev, jack@suse.cz,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
sforshee@kernel.org, viro@zeniv.linux.org.uk
Subject: Re: [PATCH] userns: skip id map lookup for init_user_ns
Date: Fri, 04 Dec 2026 07:43:44 +0000 [thread overview]
Message-ID: <BE331E0A-4ED5-45C8-B71B-40359C95A22D@linux.dev> (raw)
In-Reply-To: <20261204064759.49001-1-liqiang01@kylinos.cn>
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!
next prev parent reply other threads:[~2026-12-04 7:43 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 6:47 [PATCH] userns: skip id map lookup for init_user_ns Li Qiang
2026-12-04 7:43 ` Bradley Morgan [this message]
2026-12-04 8:54 ` Alexander Mikhalitsyn
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=BE331E0A-4ED5-45C8-B71B-40359C95A22D@linux.dev \
--to=bradley.morgan@linux.dev \
--cc=brauner@kernel.org \
--cc=containers@lists.linux.dev \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=liqiang01@kylinos.cn \
--cc=sforshee@kernel.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox