From nobody Tue Dec 2 00:45:58 2025 Received: from mail-ed1-f54.google.com (mail-ed1-f54.google.com [209.85.208.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 67F3B274659 for ; Sun, 23 Nov 2025 06:31:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763879474; cv=none; b=mInYYI74V/hQ2ZwWGURnByzrgSgvuona31NkI4a6itsI2/0aY63oXggZQaBl9mro0PDDSXp0AbbCtXnPHZ/naUeVfPFSkM5seavz9MiKl0qnx4cUyboQiL88+LIZ/88ozsdoe5KWpDjsKVV86VM5qObSljvHQu5YdDuU0dOzeJY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763879474; c=relaxed/simple; bh=6rRt9gp4BCv6QzkYYBFmv0E649ITm/lmKmfvlOY7ZYA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rCyDhOrSa5zH5czy3uMlr+XSRsbtBtGYydMH6SHJlGLpF0euqVGtBvBh0wa+LPzDqOpV4m897LrAftNMGriA+EH+34M2qQtQRCw/B6GFjC2AVnwQGi6HwYB8BIDXQ8axBu/3Q/Dr4FXkDWH0nuBdst2XEzPaiCqllVhsSTkPAcs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=HAURN2D4; arc=none smtp.client-ip=209.85.208.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HAURN2D4" Received: by mail-ed1-f54.google.com with SMTP id 4fb4d7f45d1cf-640bd9039fbso5473560a12.2 for ; Sat, 22 Nov 2025 22:31:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763879469; x=1764484269; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xOD2aQKV3axDn07/bVdfc1LsSZxl6BJ8A2aT5B+TqvY=; b=HAURN2D4HiYOaU5WpJRTSy3OkpG8wuWfaXvwg8u8XYPEhUXzLK+WxbKfztG3xURn/4 pScRTiHDx0TyXYPW8fRgCSB12lQdmV52n9gnNAQh6BvKyzsDrDa7qXLRiOno2mPPRBP+ VfCX4Z9zOAPL4rDIKTFJ/XPL1VBxPK8CJWukgg/U+AtGcmhYNkatyS5oxHiAFIxmJWQ4 eNUSzoCTmfjGu0WGLypwrcnikq/KnkTXfgiT2OtjtU+h48+yRTndVoEGnZLH9jpTOFw8 gkQ+h7NXvPwircMPzjIcIWXuaPl7laY4oAN6OMg6RkWZ7IgTZDNhJ7Qx1XFB0NtHUZtn qvhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763879469; x=1764484269; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=xOD2aQKV3axDn07/bVdfc1LsSZxl6BJ8A2aT5B+TqvY=; b=rjFZ+DC0bePmky/1i30J2Atmj+nQnvfdMZ3cEbWTkqqQ6Er/AI4oeBUp3YCqgl+TNC RusvkjG/sWHMdqDke41I97x8x0/IxqlBQ2rbVHwXlYFNIZxpyZa8DxQLonsutlFydOCY 4KECka7RpTmLnXUC9Qndg9w6ZDnka7ATh9LRY0kCJc4h+6XAUbNnc533Ku2BltUcqf8W Zmr1Ahk+cYBSw5MbsEX3pHlo0qg5MB87wWx5VzHKeGGTEkqV5E1bdZ+K05iNntZ0E1CP JdRzKO8HtFsGriRqvdNZ77CxVhOC1gMEQZf1Mlkr6Iofmnq11f/m/L/GhyGFA+8zGD2S OQgA== X-Forwarded-Encrypted: i=1; AJvYcCUbN+kzhggWPQawMXK9DRNjeos5YXSdSHV7oHFKI3x5ORk4gcCdxDZpMisdZMZxAVLwjktf5wpWgZ7wGAk=@vger.kernel.org X-Gm-Message-State: AOJu0YzedO1oU8bwCgWUlJvNy6JygJKVf/X9TES1ZAljkkYo8n1706jP lq7GMz3qCddQkCcUcrbTS52G4L5wYN59GfZms7R64kujSvyzzssAulI3J7a4uA== X-Gm-Gg: ASbGncvKo+D5XEaV57k2XTADijy/cYubaID/w+GOWRjUKu3nFA+Ap5WnmXJv8Z3eHcA +Y00sLZ7f5hEo+e9cwyJPJ7877x43bbdZNWJk09ssawoyotXaOtaa8J3OSzjxRGFMH27wRJaq6j 1q8vedC3M7AQBhEp9xUejuw+RKjqsEOQFDYskfMJwZlsBce7n036Y8Ff/NYxMmzfTCfF42SPIt1 GeBfMSvezg6rDRW58CrpGnZHgB408getS6F89pezLx0saFv5lxf944i1Xvinr4sgU4zWrA3wx9m mpWCKj/QSgl45A9RqBeWiDz3fZL7Ep5pZ+9uo5/8wxImMFEGpRp6+24dJQrzmVNxH863/Z/s8hj AEfkR1+djxcj65vCK2qs0FitNg0OvdvOdXEhElZhsOsrJNQ+i6NNqf9I5P5YgBTvc6Fp0Sx0dCO VXS6cOdUxemq0BtmzifTRUK9HNaA1IpoSAN0nHjPjnoaxiAhEJZPzqLwzNuuo= X-Google-Smtp-Source: AGHT+IE187VL3hUExsJspXYDW4wFYmYGutUdJGgD2n+2o5I+ckya4M5kz9tDUBfZuQeW/8hgUO5zZg== X-Received: by 2002:a17:906:181b:b0:b76:7fe7:ff37 with SMTP id a640c23a62f3a-b767fe8084emr434076066b.18.1763879468635; Sat, 22 Nov 2025 22:31:08 -0800 (PST) Received: from f.. (cst-prg-14-82.cust.vodafone.cz. [46.135.14.82]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b7654cf0435sm929432866b.4.2025.11.22.22.31.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 22:31:08 -0800 (PST) From: Mateusz Guzik To: oleg@redhat.com Cc: brauner@kernel.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, linux-mm@kvack.org, Mateusz Guzik Subject: [PATCH 1/3] idr: add idr_prealloc_many Date: Sun, 23 Nov 2025 07:30:52 +0100 Message-ID: <20251123063054.3502938-2-mjguzik@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251123063054.3502938-1-mjguzik@gmail.com> References: <20251123063054.3502938-1-mjguzik@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Signed-off-by: Mateusz Guzik --- include/linux/idr.h | 1 + lib/radix-tree.c | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/include/linux/idr.h b/include/linux/idr.h index 789e23e67444..c9aeae695442 100644 --- a/include/linux/idr.h +++ b/include/linux/idr.h @@ -110,6 +110,7 @@ static inline void idr_set_cursor(struct idr *idr, unsi= gned int val) #define idr_unlock_irqrestore(idr, flags) \ xa_unlock_irqrestore(&(idr)->idr_rt, flags) =20 +void idr_preload_many(int nr, gfp_t gfp_mask); void idr_preload(gfp_t gfp_mask); =20 int idr_alloc(struct idr *, void *ptr, int start, int end, gfp_t); diff --git a/lib/radix-tree.c b/lib/radix-tree.c index 976b9bd02a1b..2e71024e5323 100644 --- a/lib/radix-tree.c +++ b/lib/radix-tree.c @@ -1459,6 +1459,22 @@ int radix_tree_tagged(const struct radix_tree_root *= root, unsigned int tag) } EXPORT_SYMBOL(radix_tree_tagged); =20 +/** + * idr_preload_many - preload for idr_alloc() + * @gfp_mask: allocation mask to use for preloading + * @nr: how many calls to preload for + * + * Preallocate memory to use for n calls to idr_alloc(). This function + * returns with preemption disabled. It will be enabled by idr_preload_en= d(). + */ +void idr_preload_many(int nr, gfp_t gfp_mask) +{ + WARN_ON_ONCE(!nr); + if (__radix_tree_preload(gfp_mask, nr * IDR_PRELOAD_SIZE)) + local_lock(&radix_tree_preloads.lock); +} +EXPORT_SYMBOL(idr_preload_many); + /** * idr_preload - preload for idr_alloc() * @gfp_mask: allocation mask to use for preloading @@ -1468,8 +1484,7 @@ EXPORT_SYMBOL(radix_tree_tagged); */ void idr_preload(gfp_t gfp_mask) { - if (__radix_tree_preload(gfp_mask, IDR_PRELOAD_SIZE)) - local_lock(&radix_tree_preloads.lock); + idr_preload_many(1, gfp_mask); } EXPORT_SYMBOL(idr_preload); =20 --=20 2.48.1 From nobody Tue Dec 2 00:45:58 2025 Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0D74E273D66 for ; Sun, 23 Nov 2025 06:31:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763879473; cv=none; b=PACiFXScEKmEJdSIhNkn+VpW8anIBFFBDl4lKqUj2G2rKpwgUcC5I9i5zOxlPUe4gqlEiEhl8I8tpNJUu1TaWT91BBN6AQxooWv6IUGCTSYMMwh5Asmgo8DxaYXM4TTHIH81Snt37Pgdbghzk886UjOTa7kTQLxSC4EZJghjPYk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763879473; c=relaxed/simple; bh=NCDZRGh4CNEjF8wzrz0/qmUmjmi/vLJ1iChHLMKHngw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FfsasQByIwTSo/Xs9bfH4GS0sg71Y73yUKljZsPf3DZIPHXcAK7D9IAsIWVnzm8PTktc2OIWgdTfrTpI5BFK5+AAca0OHEO17zH3i4Vjp7Iz1gZsWvncAitAc6a/7VAFOMViaEtiGYI5LPMa6dGfHyK7ZYL7qn/E3yfoKjtLTZ4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=FOG4+ulQ; arc=none smtp.client-ip=209.85.218.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FOG4+ulQ" Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-b728a43e410so608543366b.1 for ; Sat, 22 Nov 2025 22:31:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763879470; x=1764484270; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ighOjyvrf0/Zx4X8xYQ5PxpLMHcY6lBYspK5VjpofZs=; b=FOG4+ulQIZ1YlDqfLOFF6h6S6RmkGo6FZbUZqHwVPSPZHYrrTUjDTpv3l1ysXtFlcP 56r7gDlGG5DKyk2+qLHtdABFysRsBOdMdnBQNMbG7o3FCWI9QvPLIaiv2CNQh/fCBM7y GADohncOKITeaRatgj4HYXizSetyAkTYsLfm3tLfncnQXyX2ixloLaMnJuYQiQrR2/Mc 0JgQ830RB6PoPZ/jiWIlwbXIIbV53Jd9/nk5kPuvNWXwiU8IyTEn3iB42/OoLgbKEaED U1IknJ5ncc73bI0za41ja15GKOQgDtaQrGUhr24abL06dwV5vyKdEPC0Emsc2fcYVHm/ Ysrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763879470; x=1764484270; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=ighOjyvrf0/Zx4X8xYQ5PxpLMHcY6lBYspK5VjpofZs=; b=Uzap7Piq+n1QKN2bZoOEVVxQt/98jBJ3jM5+KoaOYyOf29loUG41Awy14zHR0Pvosf DuJfshV2zHLxgtWmJFj1/ugendqjs5dtJaJrNRsWrU4I88Tv2yaRvp7+fcSnnHynjImv yVnnXmYkyOJfMTYu7DP6lg2FpwtYSEf5AKjTU9NH9Sd7N9+Xwf3MDWzG/Rd63gCQ68Ba RzmmFUL04MrFyzjjvCcH4uhpMQbxcBDIgC2wEaCQCzgJDP1rvwzlDWEH8LZDeSpEUxlZ U6GHbRsgWtPJTro18RlBnjwm7VKHsupGMGpZgz2S9vEh0g0qUxOLh2m0AV15jGSxifQK bN2w== X-Forwarded-Encrypted: i=1; AJvYcCUgvtVaNyriUu2iQqL+RXP12Ha/+CVWqIRan5/+lW27ctH/Y5AIErRDI1llNrZxepAys6MQTbyAUQFbdcs=@vger.kernel.org X-Gm-Message-State: AOJu0YzYdE3MNp5w7DbjYSchDCEmUB/wCWTSUzbmSSLx3OTrw/nVBAiK UD6DS9vvG5Su3vKUPs6adEszdEnCZYLbjl4fkYEBy0OFJR0vKKGovYpo X-Gm-Gg: ASbGncuRsekOYLA8IC9DGHcvSzDirLvl3srbPoIGg3wzhiQq0AMqr/08zmM/t2fqM/u AsinvWU+0r7LSMRZT//qeodpYsoBm4HpllbP70FlFonOQYDyaoV9wyIJQo1zb0Oi9ETt2cRaHIO peHXyfRt69VMePptTfZmF5fIN9cXGzG2JGvDYTbdPCNQ3fvGNo27SFJrqdbQn/LVJtuRk+Nc6ds 01icxosLnKHsd+OHX3R/byz5zQhTEsOcQQtSUMgQoDc/MFIpeEhEN/w9qcDupb3Y2elNJNNc+Qm rxBh5QwcQ1jJ6lEffYqqRI40RynJBYpGmZ2JolkMJr1ztnfaTrqyi1e8XmbUxEB4MR7XCOikTIM KTWiHtfI6vKMBd1Odaam+/SVX9cJIqFfgTu4re7yGVyyy98yi3HhffDANiE+IxOL0hpaTvreZhJ xONuhtqU0hI762J4smGNtm7KjhVWglYc3wv4ezoPm763OlIrKwKZibmW3Oek0xqkoupo1xww== X-Google-Smtp-Source: AGHT+IG+zh4ATHjbiK5vyKe+SzwhMWh4xwNYt1jBoUD/n+vAKGF6wzNqxClWwQoSqksxMaxVIvFoJA== X-Received: by 2002:a17:907:7286:b0:b3d:73e1:d809 with SMTP id a640c23a62f3a-b767173270emr1002823866b.48.1763879470281; Sat, 22 Nov 2025 22:31:10 -0800 (PST) Received: from f.. (cst-prg-14-82.cust.vodafone.cz. [46.135.14.82]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b7654cf0435sm929432866b.4.2025.11.22.22.31.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 22:31:09 -0800 (PST) From: Mateusz Guzik To: oleg@redhat.com Cc: brauner@kernel.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, linux-mm@kvack.org, Mateusz Guzik Subject: [PATCH 2/3] ns: pad refcount Date: Sun, 23 Nov 2025 07:30:53 +0100 Message-ID: <20251123063054.3502938-3-mjguzik@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251123063054.3502938-1-mjguzik@gmail.com> References: <20251123063054.3502938-1-mjguzik@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Note no effort is made to make sure structs embedding the namespace are themselves aligned, so this is not guaranteed to eliminate cacheline bouncing due to refcount management. Signed-off-by: Mateusz Guzik --- include/linux/ns/ns_common_types.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/linux/ns/ns_common_types.h b/include/linux/ns/ns_commo= n_types.h index b332b019b29c..0014fbc1c626 100644 --- a/include/linux/ns/ns_common_types.h +++ b/include/linux/ns/ns_common_types.h @@ -108,11 +108,13 @@ extern const struct proc_ns_operations utsns_operatio= ns; * @ns_tree: namespace tree nodes and active reference count */ struct ns_common { + struct { + refcount_t __ns_ref; /* do not use directly */ + } ____cacheline_aligned_in_smp; u32 ns_type; struct dentry *stashed; const struct proc_ns_operations *ops; unsigned int inum; - refcount_t __ns_ref; /* do not use directly */ union { struct ns_tree; struct rcu_head ns_rcu; --=20 2.48.1 From nobody Tue Dec 2 00:45:58 2025 Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D11AE2749DC for ; Sun, 23 Nov 2025 06:31:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763879475; cv=none; b=QceDJql/d+hAPT7zwEj3upNkk+e4faah1AoVG/nSJS+Z5sTOfWT4ifz69yH3/tVrXot56WglK9iqaJXA3jPld7ZJN29DOAYhPawAzx/hFHC/PdNfRdK7CSc0FOswK30aY+9mJT3riiyQjplnSLP+FFq7RS5P1Onu/QXHUMzRl2Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763879475; c=relaxed/simple; bh=D0Aild/73jmKg7ZMSpkPXvJNgfSF2aMMJG78JV64k5U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=U0g0j/vxoT7AUBPrM38U4fgant+Ab+yDh9M7FmL4TU3/dNDhWXbZpe0AhNp3A9ZpLDFLe1YvA/rqdvluCVWLy7WBhhgAVU+upQGN1jD9fcOyxkJGpUJ/5XXBbp8eDFu43hNGI6KiWRHHbdrIU2auiCII3OtuKfwz4FETCRBkPdA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=WNDjJZQR; arc=none smtp.client-ip=209.85.218.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="WNDjJZQR" Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-b762de65c07so41324766b.2 for ; Sat, 22 Nov 2025 22:31:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763879472; x=1764484272; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fzeVFSUzIu035ZBi7H1TZb6fE+64VF6oJ8fhy8LFjfw=; b=WNDjJZQRmXNcd9TlV5CCSx9VH5uxzU4ux+SxdYrFb9vzE8XdwnIGgYmz5gfExerRBE rYTj6JJ/l4YymlKjIVBJguPokgOK3U+dzmOjObtgR/4Q+xRrHH9pR+4j0374o7bN/zAU TPswPySL2zP5g4XQiVkP9jO04llWnd35UV7H02+Sadt+7qs+a2//QXkRB9lgP0HlcLwh FolX2ahMWpGGGtPG/YDc05EJoZO5dJS4sEViA4A3/klnI+Z53/kDwYoAuxXTOmtW8k3+ HU2H5V1u1PvF1tIZD3s1W/qR3UKptHoetLV5QQgn0cZRQh0P+MeVqgqbY05y34tBdoZU PyUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763879472; x=1764484272; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=fzeVFSUzIu035ZBi7H1TZb6fE+64VF6oJ8fhy8LFjfw=; b=UWMECkMjCVJCJPpIXhXiPbafkzqSrGcWUyIRgz59bQ0g6mTYgnYXE+wEKg82iD+nLg Nandg9aphpFjMcZI8ozk71fXck2gYePM8PFAAVQe4uIsNd93FuSlqhuKxtSx8SaF+v+0 BbsABQjS4Xm5pzRHpuEDmAtK2LNvZQ6AS3RGvDFfPEhTs+bxhHN2uVTN5ytlphA8e7jK Roai+uwdkeHjalpGxnoUkoqYT0eDkl0N/AFpqv/ZNsb4z7V2epU2RgscyBJk/TzdihWt 5X0skYKGH8pvAMGTt3rSRyfXBI6cab8MCvY75MGoa7HLQcFz5vhTpE1i4dmGzCYG/Otx tOww== X-Forwarded-Encrypted: i=1; AJvYcCVTsCrAh+dM6cizjhuHuArKyPopZ+84RlnCswixfiNVf4aUY0FXrKmps5vl69x8wPuJzjIgT7Rwz+D0LbQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxnOlodkNMDrU/J3rgPxxNr9tVaTfBS6xcke1MMGsSUcO8bCmCh VHDx8BXOGC1OuPRGF1nO2FpzCeO+/lSL+1oKVBnXxhFvpgafmdtpJHp4 X-Gm-Gg: ASbGnctAU5FNZ6Fg06DLadMjrIkXiEANuzLovm4nWqZP2grkq1xTP1npJkJKpA0dsUX TZk9gYBY97UX3hKn3l+DT+XNniszkj7A4ST/M7M8oCDH19bkzMtZGVo6JhePabqfMhIHixwpxi9 1uWYN1MxI5/fMhNgxgFiGaYfituShfOgZ3TNHBQzJqkRiWoZOi8sr0n1idNeL+FKijPWI34ca8n jpjSP3Fout9XgTaMXFrlQbHOUgpK6ghjGdV0TLrdUilNBSdIe46neyHOfYAiPp+L6dGwQ64KfZh jHiGxo4zhFzvy7oweLpuRK9FnkrgbESVznY+gttHlcPwjf3/y+XCOcx2eT1Z6fdqud5vUQJliJe yuvLDEGERdi1iYhbKdkHS3jfkIlQax72JDWODybWyxHcCdNhFKf1nM3G9qRcz9JcY+U7IXdTXgM EuyzGbhKhOOKy5MKrQuu6zbECJGm2WiOFo/qx1bTv7/hUA2MfmqeNSQ7flj5c= X-Google-Smtp-Source: AGHT+IG3sZ0KYmhLtFy9FEoE/ix2RTytRA6o14m8ny/qtVDn43naSDP8Y8a8Os47AAL1eliyOZzjSw== X-Received: by 2002:a17:907:940b:b0:b73:806c:bab7 with SMTP id a640c23a62f3a-b76715723ffmr788343166b.19.1763879472026; Sat, 22 Nov 2025 22:31:12 -0800 (PST) Received: from f.. (cst-prg-14-82.cust.vodafone.cz. [46.135.14.82]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b7654cf0435sm929432866b.4.2025.11.22.22.31.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 22:31:11 -0800 (PST) From: Mateusz Guzik To: oleg@redhat.com Cc: brauner@kernel.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, linux-mm@kvack.org, Mateusz Guzik Subject: [PATCH 3/3] pid: only take pidmap_lock once on alloc Date: Sun, 23 Nov 2025 07:30:54 +0100 Message-ID: <20251123063054.3502938-4-mjguzik@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251123063054.3502938-1-mjguzik@gmail.com> References: <20251123063054.3502938-1-mjguzik@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This reduces contention on the lock during parallel clone/exit. It remains the primary bottleneck in such a case. While here tidy up the code. Signed-off-by: Mateusz Guzik --- kernel/pid.c | 99 +++++++++++++++++++++++++++++----------------------- 1 file changed, 55 insertions(+), 44 deletions(-) diff --git a/kernel/pid.c b/kernel/pid.c index a31771bc89c1..6a87ce5a6040 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -159,9 +159,11 @@ void free_pids(struct pid **pids) free_pid(pids[tmp]); } =20 -struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid, - size_t set_tid_size) +struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, + size_t arg_set_tid_size) { + int set_tid[MAX_PID_NS_LEVEL + 1] =3D {}; + int pid_max[MAX_PID_NS_LEVEL + 1] =3D {}; struct pid *pid; enum pid_type type; int i, nr; @@ -170,47 +172,71 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t= *set_tid, int retval =3D -ENOMEM; =20 /* - * set_tid_size contains the size of the set_tid array. Starting at + * arg_set_tid_size contains the size of the arg_set_tid array. Starting = at * the most nested currently active PID namespace it tells alloc_pid() * which PID to set for a process in that most nested PID namespace - * up to set_tid_size PID namespaces. It does not have to set the PID - * for a process in all nested PID namespaces but set_tid_size must + * up to arg_set_tid_size PID namespaces. It does not have to set the PID + * for a process in all nested PID namespaces but arg_set_tid_size must * never be greater than the current ns->level + 1. */ - if (set_tid_size > ns->level + 1) + if (arg_set_tid_size > ns->level + 1) return ERR_PTR(-EINVAL); =20 + /* + * Prep before we take locks: + * + * 1. allocate and fill in pid struct + */ pid =3D kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL); if (!pid) return ERR_PTR(retval); =20 - tmp =3D ns; + get_pid_ns(ns); pid->level =3D ns->level; + refcount_set(&pid->count, 1); + spin_lock_init(&pid->lock); + for (type =3D 0; type < PIDTYPE_MAX; ++type) + INIT_HLIST_HEAD(&pid->tasks[type]); + init_waitqueue_head(&pid->wait_pidfd); + INIT_HLIST_HEAD(&pid->inodes); =20 - for (i =3D ns->level; i >=3D 0; i--) { - int tid =3D 0; - int pid_max =3D READ_ONCE(tmp->pid_max); + /* + * 2. perm check checkpoint_restore_ns_capable() + * + * This stores found pid_max to make sure the used value is the same shou= ld + * later code need it. + */ + for (tmp =3D ns, i =3D ns->level; i >=3D 0; i--) { + pid_max[ns->level - i] =3D READ_ONCE(tmp->pid_max); =20 - if (set_tid_size) { - tid =3D set_tid[ns->level - i]; + if (arg_set_tid_size) { + int tid =3D set_tid[ns->level - i] =3D arg_set_tid[ns->level - i]; =20 retval =3D -EINVAL; - if (tid < 1 || tid >=3D pid_max) - goto out_free; + if (tid < 1 || tid >=3D pid_max[ns->level - i]) + goto out_abort; /* * Also fail if a PID !=3D 1 is requested and * no PID 1 exists. */ if (tid !=3D 1 && !tmp->child_reaper) - goto out_free; + goto out_abort; retval =3D -EPERM; if (!checkpoint_restore_ns_capable(tmp->user_ns)) - goto out_free; - set_tid_size--; + goto out_abort; + arg_set_tid_size--; } =20 - idr_preload(GFP_KERNEL); - spin_lock(&pidmap_lock); + tmp =3D tmp->parent; + } + + /* + * Prep is done, id allocation goes here: + */ + idr_preload_many(ns->level + 1, GFP_KERNEL); + spin_lock(&pidmap_lock); + for (tmp =3D ns, i =3D ns->level; i >=3D 0; i--) { + int tid =3D set_tid[ns->level - i]; =20 if (tid) { nr =3D idr_alloc(&tmp->idr, NULL, tid, @@ -235,10 +261,8 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t = *set_tid, * a partially initialized PID (see below). */ nr =3D idr_alloc_cyclic(&tmp->idr, NULL, pid_min, - pid_max, GFP_ATOMIC); + pid_max[ns->level - i], GFP_ATOMIC); } - spin_unlock(&pidmap_lock); - idr_preload_end(); =20 if (nr < 0) { retval =3D (nr =3D=3D -ENOSPC) ? -EAGAIN : nr; @@ -257,25 +281,15 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t= *set_tid, * is what we have exposed to userspace for a long time and it is * documented behavior for pid namespaces. So we can't easily * change it even if there were an error code better suited. + * + * This can't be done earlier because we need to preserve other + * error conditions. */ retval =3D -ENOMEM; - - get_pid_ns(ns); - refcount_set(&pid->count, 1); - spin_lock_init(&pid->lock); - for (type =3D 0; type < PIDTYPE_MAX; ++type) - INIT_HLIST_HEAD(&pid->tasks[type]); - - init_waitqueue_head(&pid->wait_pidfd); - INIT_HLIST_HEAD(&pid->inodes); - - upid =3D pid->numbers + ns->level; - idr_preload(GFP_KERNEL); - spin_lock(&pidmap_lock); - if (!(ns->pid_allocated & PIDNS_ADDING)) - goto out_unlock; + if (unlikely(!(ns->pid_allocated & PIDNS_ADDING))) + goto out_free; pidfs_add_pid(pid); - for ( ; upid >=3D pid->numbers; --upid) { + for (upid =3D pid->numbers + ns->level; upid >=3D pid->numbers; --upid) { /* Make the PID visible to find_pid_ns. */ idr_replace(&upid->ns->idr, pid, upid->nr); upid->ns->pid_allocated++; @@ -286,13 +300,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t = *set_tid, =20 return pid; =20 -out_unlock: - spin_unlock(&pidmap_lock); - idr_preload_end(); - put_pid_ns(ns); - out_free: - spin_lock(&pidmap_lock); while (++i <=3D ns->level) { upid =3D pid->numbers + i; idr_remove(&upid->ns->idr, upid->nr); @@ -303,7 +311,10 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t = *set_tid, idr_set_cursor(&ns->idr, 0); =20 spin_unlock(&pidmap_lock); + idr_preload_end(); =20 +out_abort: + put_pid_ns(ns); kmem_cache_free(ns->pid_cachep, pid); return ERR_PTR(retval); } --=20 2.48.1