From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (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 26DD442A144 for ; Thu, 16 Jul 2026 14:11:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784211103; cv=none; b=WUG/1MJgGyMFS1OdyY04tUWMU/YiLVer04a6q7HkWsACTEODD2wKDwHGGojFyYyte3rzivrafQuInhq9XcxR+pcibexTgQqdc5YhzrZsaz09k3keCDr8FaR8fp1r2NVdZRBpiTq8qIEwjNXJynk4Ra3cE97gSgxysNoisKegTDw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784211103; c=relaxed/simple; bh=aeHVVy59tCknMURSUrWPoveUqkqys6elaWG4xPHuwcE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=rhqjboVTAfWAjVMRDs3SIVVaghmtpKJ84nYYN4QUOcNL+zvdSUKU+10UQhBIT6B21nlH5/bolyB1uZc1Zo/wIYKBrLc3jiC/JhL38dKy6LKj5FKDg8DcRO3Wa044Qnz4E+Nyg0u5Abq9JgaQLSVaCIE+rrbsV4rt4U16MDFizQc= 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=eyK1W/8k; arc=none smtp.client-ip=209.85.221.50 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="eyK1W/8k" Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-47dec32798aso6852949f8f.1 for ; Thu, 16 Jul 2026 07:11:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784211100; x=1784815900; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=ND1Mlfnj//sueUdP3Rtl6TIXHHpQPflpRLRpL4+r6dw=; b=eyK1W/8kF2T2BAeCKnWo96tbFh5CNJ4rGgD5cW6P2OZyIpdC0RAFVUPxsvE30VPv0M 8ztaI3llOjn+LCL13u2NA+V0lpe/7ttGWeBizk/TCIdVvRd3cYNRj9doQ1KWQDa6p8jk ld7fYTl2l5pzfGpJDyf5RxzM8VPa1Z+Qdzreof4THCnFU73wdSxNcN++Tv6sp+qF2LYe fPGYsd5VAW1eWVvwByzW1rDv2tLafIRFIxpIvEvvnJ3HEuoSjigdkxsqgRdj9KGKm/Q+ uC9ymlV8RUOM+cpT90V6QZnWJZ5xko8Z703taLHHAbcSDFvxQsmUi/yzcNV28vE04Rg0 BLFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784211100; x=1784815900; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=ND1Mlfnj//sueUdP3Rtl6TIXHHpQPflpRLRpL4+r6dw=; b=BaJvHa1z0ZTk6rxTDDbUlcBcePis2jxtcQdu/a16WVboEogVfY++HDXQCIEV6tMjqr 4iRCGPqDtH6LnXNy6cNkWGUGSszXI5DWVv/m0tXaCMuyx8w3tXTXMIlCU4gDnP7l0RRp JmEHeMAuafIUUBk3tbeUbHWfao/058jR48neRSLNEsZn09n9IpkDJU6SycoWyNEfcskK BfVooXv5xaJDQwOjrHso7J3VtpYu9hJgYtdEJyXyyKsURkt4sSpfPe5sFYnnbL3YLQcq D0KfGjA5TxADReCWvvbgIAY2tg89K40kn7mIu33uTECoxYhOKPkaNuVTMQ0wcXYHqZ/7 6qiQ== X-Gm-Message-State: AOJu0YyM1ZlENPw5lIPByc1itAktOmHkYIhak9zQvdihj6KAS469W5Zo CNeNeRFjZJywjXwGIrPWl4R9I/H0NhFnt7jEEuUQDYx2wwb0pkTOp1/JqB3OWQ== X-Gm-Gg: AfdE7clER6cL5/JHxKCpPrOZ6DivfHvR7VY1NZfP1r0Gs1bqAqT06qoFovmukVS/qcg dQjXCUOLFv9X1efbCJ+wD53MXPHU14zukI8D5c2n285k/xDZ88s5docruOez8Sc/PwXpZGQpbp2 SUfbWNvHngsZNO7sb+AqLcNkXt97HPq7qVBQe058CgUHZFDKH2J2iS8AbfFFfGsgTjUm9QSBCbo JCdxChIaWdokvzX3AseeIqF9hXHaM+zzxlZBlztwyPyfHWe1UR3DHsJwizrY/18jJDqRo1zMmE/ 2LjOI5nYHxiYWu4qvnl/MqH/3AwD+SBkHpXzmXGE/bOR8fgBah1TsQv7P/83iE98uFEZp8FTJEw 99h3t9yXkS7jacxxGSjWoO2KP1zSbR/RXui6K03pU3bijA2PChcMes+Er9t2Lq4nk7Qazwm/jou 6VjdTYyKKmDUlH0ByU73O1NAFUQxEIwkZj54txojSVlwZUfpfjFJGa X-Received: by 2002:a05:6000:310b:b0:46f:5d62:d914 with SMTP id ffacd0b85a97d-47f488488b5mr15090426f8f.12.1784211099956; Thu, 16 Jul 2026 07:11:39 -0700 (PDT) Received: from f.. (cst-prg-85-255.cust.vodafone.cz. [46.135.85.255]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47f464a973fsm26631643f8f.17.2026.07.16.07.11.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Jul 2026 07:11:39 -0700 (PDT) From: Mateusz Guzik To: akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Mateusz Guzik Subject: [RESEND PATCH] pid: cosmetic changes to alloc_pid() Date: Thu, 16 Jul 2026 16:11:30 +0200 Message-ID: <20260716141130.2573188-1-mjguzik@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Commit 6d864a1b182532e7 ("pid: only take pidmap_lock once on alloc") landed v2 of the patch instead of v3. This patch remedies the problem. No functional changes. Signed-off-by: Mateusz Guzik --- Hi Andrew, this is an old & uncontroversial patch which fell through the cracks. Would you mind picking it up? kernel/pid.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/kernel/pid.c b/kernel/pid.c index f55189a3d07d..3f58635a9b08 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -177,7 +177,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, * for a process in all nested PID namespaces but arg_set_tid_size must * never be greater than the current ns->level + 1. */ - if (arg_set_tid_size > ns->level + 1) + if (unlikely(arg_set_tid_size > ns->level + 1)) return ERR_PTR(-EINVAL); /* @@ -186,7 +186,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, * 1. allocate and fill in pid struct */ pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL); - if (!pid) + if (unlikely(!pid)) return ERR_PTR(retval); get_pid_ns(ns); @@ -205,7 +205,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, * This stores found pid_max to make sure the used value is the same should * later code need it. */ - for (tmp = ns, i = ns->level; i >= 0; i--) { + for (tmp = ns, i = ns->level; i >= 0;) { pid_max[ns->level - i] = READ_ONCE(tmp->pid_max); if (arg_set_tid_size) { @@ -221,6 +221,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, } tmp = tmp->parent; + i--; } /* @@ -241,10 +242,9 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, tid + 1, GFP_ATOMIC); /* * If ENOSPC is returned it means that the PID is - * alreay in use. Return EEXIST in that case. + * already in use. Return EEXIST in that case. */ if (nr == -ENOSPC) - nr = -EEXIST; } else { int pid_min = 1; @@ -270,12 +270,11 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, * Preload more memory if idr_alloc{,cyclic} failed with -ENOMEM. * * The IDR API only allows us to preload memory for one call, while we may end - * up doing several under pidmap_lock with GFP_ATOMIC. The situation may be - * salvageable with GFP_KERNEL. But make sure to not loop indefinitely if preload - * did not help (the routine unfortunately returns void, so we have no idea - * if it got anywhere). + * up doing several with GFP_ATOMIC. It may be the situation is salvageable with + * GFP_KERNEL. But make sure to not loop indefinitely if preload did not help + * (the routine unfortunately returns void, so we have no idea if it got anywhere). * - * The lock can be safely dropped and picked up as historically pid allocation + * The pidmap lock can be safely dropped and picked up as historically pid allocation * for different namespaces was *not* atomic -- we try to hold on to it the * entire time only for performance reasons. */ @@ -299,7 +298,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, /* * PID 1 (init) must be created first. */ - if (!READ_ONCE(tmp->child_reaper) && nr != 1) { + if (unlikely(!READ_ONCE(tmp->child_reaper) && nr != 1)) { retval = -EINVAL; goto out_free; } -- 2.48.1