Linux filesystem development
 help / color / mirror / Atom feed
From: Tao Cui <cui.tao@linux.dev>
To: Li Qiang <liqiang01@kylinos.cn>,
	Christian Brauner <brauner@kernel.org>,
	Alexander Mikhalitsyn <alex@amutable.com>
Cc: cui.tao@linux.dev, Jan Kara <jack@suse.cz>,
	Seth Forshee <sforshee@kernel.org>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Bradley Morgan <bradley.morgan@linux.dev>,
	linux-fsdevel@vger.kernel.org, containers@lists.linux.dev,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] userns: skip id map lookup for init_user_ns
Date: Sun, 6 Dec 2026 13:54:24 +0800	[thread overview]
Message-ID: <f8c00b8a-0bb5-4e36-8288-33c8015523b2@linux.dev> (raw)
In-Reply-To: <20261205095025.185272-1-liqiang01@kylinos.cn>

Hello, Qiang,

在 2026/12/5 17:49, Li Qiang 写道:
> 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%
> 
I tested v2 on top of your base 007722b8211f (7.4.0-rc5), KVM guest
on a Hygon host with rethunk active, gcc 12.3 build. Medians over
3 boots x 3 rounds:

  getuid()              100.2 -> 95.9 ns    (-4.3%)
  getresuid()           265.5 -> 251.1 ns   (-5.4%)
  setfsuid(-1)          108.2 -> 100.9 ns   (-6.8%)
  fstat() on tmpfs      326.1 -> 319.2 ns   (-2.1%)
  fstat(), 16 threads   +13.9%
  fstat(), child userns unchanged

A getpid() control run in between stayed flat.

FWIW these numbers are with gcc, which keeps the extent lookup
out-of-line. clang inlines it into make_kuid()/from_kuid(), so
there v2 only drops the single-extent loop (~1 ns per call,
measured from a module) and the syscall numbers are within noise.
Maybe mention the compiler in the commit message?

Otherwise looks correct to me. (u32)-1 falls outside the identity
extent so the early return matches the lookup failure value, and
from_kuid_munged()/kuid_has_mapping() go through from_kuid()/
make_kuid() anyway.

Tested-by: Tao Cui <cuitao@kylinos.cn>
Reviewed-by: Tao Cui <cuitao@kylinos.cn>

Thanks,
Tao
> Signed-off-by: Li Qiang <liqiang01@kylinos.cn>
> ---
> 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@kylinos.cn
> 
>  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


      parent reply	other threads:[~2026-12-06  5:54 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  9:49 [PATCH v2] userns: skip id map lookup for init_user_ns Li Qiang
2026-12-05 17:52 ` Bradley Morgan
2026-12-06  5:54 ` Tao Cui [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=f8c00b8a-0bb5-4e36-8288-33c8015523b2@linux.dev \
    --to=cui.tao@linux.dev \
    --cc=alex@amutable.com \
    --cc=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