From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.176 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796370225; b=o5j+2SLRoiRzkLdHon+pUswj1GXwCe0UYoqgvBAbLDkU6jUddacS3r36MJ/eepWXWIqr IDvQ8/s3l2I1amaZb9MgYa98cGjVnNjGb2bGzHLej79LV8d1mLA6hpsSIFGl60+RfAAvd oa5SirJeu78juv4JV+64DB9nKvGtMRZeKVWUix6XaTzxQCyNSir02joJnsX7jpGt362TZ 1N7j0lOgolcEZ1I1sa3U2mdGb4EOEyVJ25sm5Ts2pNA4pCLy76DuZO355AzaukfOAGrRC mH1spY+Xy/swk1mILOg6KEkU+Py9oVCWeLf3g5JioGJ/Tzskv2lkhYS1gE10BxwrhuQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796370225; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Date:From:To:CC:Subject:In-Reply-To: Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding; bh=Cco+cz85dd512EqkxgwaPcMEgMfGLF6Zs3AZa7/jips=; b=ZCWgBeN/PXoNj+AxMU7W8Ooi99UNgtlk0S6hy1bfYj2xX/HOsG1argqUXoXFcyzrVVgs BvRIfBXbBbXfh+NE7JlJ/f68XtiMw0xU8i4gl2JV7FHKiQExAdAOv+Cs97OBXGgx5k094 qWoKnAGuXFCu4l53Kt5vsYGryJLG/XA5PzAqszE93TI8ac0GJG64nDC+e1gyT35mtl0ZH 4goUscA1yU99QBGvcKsd/NjWw36A80f+cT98kpBrjMTbzI++7LXNVB+mKdzvRrr+XZUCe j30VVCywAgM1JUhGbzkyi9P4gfR6BuGq0e1q9Ise73QQ20LIx27vfr8EwoxdDN2N3GA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=K9D5srCr; arc=none smtp.remote-ip=95.215.58.176 Received: from mta1.migadu.com (out-176.mta1.migadu.com [95.215.58.176]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C80E7606A337 for ; Fri, 4 Dec 2026 07:43:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C80E7606A337 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=K9D5srCr X-Envelope-To: containers@lists.linux.dev DKIM-Signature: a=rsa-sha256; bh=pKxGMsvTDClVdnrm7euEC0cM7ixtszzlQBHHLjDswbE=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796370222; v=1; x=1796975022; b=K9D5srCrVpkEQoTMqRnhzjmfEXzEdOKBOzTl/COZZ1GHXLXNVwA+9y2Uy5k0p1GGk2F55XyO DLeuTEJ1TWg7G5VTZQidx/tJwlxmbulcFkoJzQVl8l0B9XsEEw0asKPTnu0bx0mCBIGm5zfTXQL E9giw4IxsBlGtxjGafkwhz6k= X-Envelope-To: containers@lists.linux.dev Received: by smtp.migadu.com with ESMTPS id 8a829f05a30027b2; Fri, 04 Dec 2026 07:43:42 +0000 X-Mizu-Trace-ID: 8a829f05a30027b2 X-Migadu-Flow: FLOW_OUT Date: Fri, 04 Dec 2026 07:43:44 +0000 From: Bradley Morgan 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 In-Reply-To: <20261204064759.49001-1-liqiang01@kylinos.cn> Message-ID: X-Mailing-List: containers@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit On 4 December 2026 06:47:59 GMT, 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%) That's not stupidly significant. Well to me it's not. > >Signed-off-by: Li Qiang >--- > 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!