From: Alexander Mikhalitsyn <alex@amutable.com>
To: Li Qiang <liqiang01@kylinos.cn>, Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>, Seth Forshee <sforshee@kernel.org>,
Alexander Viro <viro@zeniv.linux.org.uk>,
linux-fsdevel@vger.kernel.org, containers@lists.linux.dev,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] userns: skip id map lookup for init_user_ns
Date: Fri, 04 Dec 2026 09:54:03 +0100 [thread overview]
Message-ID: <197b33ecbf0b1fb9fb941f38dfb26a068cbc4d82.camel@amutable.com> (raw)
In-Reply-To: <20261204064759.49001-1-liqiang01@kylinos.cn>
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);
>
prev parent reply other threads:[~2026-12-04 8:54 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
2026-12-04 8:54 ` Alexander Mikhalitsyn [this message]
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=197b33ecbf0b1fb9fb941f38dfb26a068cbc4d82.camel@amutable.com \
--to=alex@amutable.com \
--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