From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ed1-f52.google.com (mail-ed1-f52.google.com [209.85.208.52]) (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 515962DE70A for ; Wed, 3 Dec 2025 09:29:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764754150; cv=none; b=LoaOXHij+JMRJf+chSPAXF56ajgW6zYqgvBSINJClT32l/+NcO0TRXRdmK5ekDoAAYUfBXwVbBfl/9PhQi23nR5D5Kezd/HWq0OfclC1/jg08HhOhaa42ac0aYgN+PVvnxM75U0gALWVmYMzjyYv+VrFotDKpijx7aJmxyTAhpU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764754150; c=relaxed/simple; bh=KpXKLWSKUlXNXXl9ZKVbTZMEsjVR7loFj6kuc9KjEQk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mku4JNWp97xQLjhjwddZUCDT431cw++Rmm/1S18yJ9zsa13cJJvF6x1c7dz+XiMRHBPNG8Fmj5hBk/v6BA0p4fZ9gCzwfu+IVBO7Wrp2pGAPI3dmT7Svb0YyWuQ/Rfo1NSgHmVnm76rwDT1ykn5/1D6HGsC6dt8dI04Jv7YN7zw= 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=F19x8Q5Y; arc=none smtp.client-ip=209.85.208.52 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="F19x8Q5Y" Received: by mail-ed1-f52.google.com with SMTP id 4fb4d7f45d1cf-64080ccf749so9544012a12.2 for ; Wed, 03 Dec 2025 01:29:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764754145; x=1765358945; 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=QdTv6wkSHwAMSOll3YwLh5RVP5GPGXPQVcJa4xCpXQ8=; b=F19x8Q5YILXrf5Vo2IAltMv0iLZmWHdTkY+Fufx9DKNCRw7pNSAoALyypGKUoBg+NZ e8KR2GdvXotThh1d+DryURyY0+0X7Cc3+cs2WmhD0oiv2YL6xRFBZtb0yU6nWjI6pQdR U0oKkQgNL9q1Lt83ocKmd2F0vMyQvWCcY8eZ1pzKQSqZDIajMTblP9J1JUvdwkXcNqkx t8ifv+FvACHCVr3qHpDd8WwFbJ/5oBR+ua5Bbj3FD2n4G1jrVfTIBCpSXvA3G363nAZM rdmAL3aWLeTSsfOdUxI73N0/3DTObVzcDQbuCLFjZE/71b0Upk3+bfmcFAJAOKhoqS1L nBBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764754145; x=1765358945; 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=QdTv6wkSHwAMSOll3YwLh5RVP5GPGXPQVcJa4xCpXQ8=; b=cC2+wQ5tkzOwzP1Y7vE/oUs96zqHr7qMN1v2bM/yL8iJOQtivnbeU02yx4iXEvkVM5 W8uXvpxW3fhO6tnGIK5XodgrG6oIC+Qvz7BE3O91OBR6fStYkLi50mS2lNhiAQD44Dcz zf3ZVuCAPrB9S5tihK1Frt8CG0we9AWTiEiZan6e8vnr81c7LyOTgTlQwNLuOTABhI/4 MojZ1UsYpJ0oay+28b6N3ELdlJ4Q8745Knm5OEX1uK80VjDX1YO85NkaHtr+1Su3OdL4 KRYbefPOZHJyB+8bt6Hc0ID5qf3E0ZbRkXqeSbwCWlv1UM6AKw31D+VN+Ar9Pf4ZwAtX uiOQ== X-Forwarded-Encrypted: i=1; AJvYcCV7VwvfUHhzvVtqZQP0ZVyo9T1F49x8ZL76V7XomFOhKA4+LswPr+/HYNNyqRNnRjvJ3WxXzCtY0018VRw=@vger.kernel.org X-Gm-Message-State: AOJu0YztrO1kN793VZEytdLn0oGiNWI6iGaSqqlXwWUM1Zz2/npTfGJp S6vKNO5DWLN6J0DAe62fJPoC6uwVnjeX40flfOw3Hs7qZMRwGSqo3O3J X-Gm-Gg: ASbGncv0JGJPkjhyWWirOFjE0lMjoDcKr1myJYw5jM+jV//a+iFfGDYB5T77OMMApkh edEv9Q3z/Z+7a21JkoCdxFy0mnxW3A0qnYSn/Enyl+15e4OrJImhfNjYPlictRtqTLTBJS/7FLf sdOpYXExoCcbF7oS0swF6qeSPk02qSnshxElxaATljzsY7F/Swro7+wkst8L7oCkhFf00wMy6rc dNhgZnGryvdfk3K/nKM8gkjjLAY7EfMp9a+5+2s0Jtk40VjnI5ZQ6APFzoEnPn/ZdrMPdqNJM3j J1pyCz4WhqUD5Huxck8dAjrClVOZlscpkQqYf3ycnnNohacluNU4p0zAG5BDEnYi/BTeJZXvoJD b9l9TA9Vcg1WO7cgGjZeS9KuCCc4E/0nQg9jI2k0OPeOFUcshbPC2t4b1PHGsjzfrOifTzMJe3D xhSUT4OVTPM5YHyEKO6BLhp+nsK5IQ/4R/D/5Dskq1IjiELTv8imy13yeh62Q= X-Google-Smtp-Source: AGHT+IGafodK6wrQHJ3egSmEyL5fp27hY/MK2pNcmMmGZDlnu9hdJtMQDjyaHTeG4cmC0C2KG74s0Q== X-Received: by 2002:a05:6402:1468:b0:647:7b37:2bc5 with SMTP id 4fb4d7f45d1cf-6479c4b2a3bmr1126107a12.31.1764754145323; Wed, 03 Dec 2025 01:29:05 -0800 (PST) Received: from f.. (cst-prg-14-82.cust.vodafone.cz. [46.135.14.82]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-64750a90d14sm17859981a12.10.2025.12.03.01.29.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Dec 2025 01:29:04 -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, willy@infradead.org, Mateusz Guzik Subject: [PATCH v2 2/2] pid: only take pidmap_lock once on alloc Date: Wed, 3 Dec 2025 10:28:51 +0100 Message-ID: <20251203092851.287617-3-mjguzik@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251203092851.287617-1-mjguzik@gmail.com> References: <20251203092851.287617-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: 8bit When spawning and killing threads in separate processes in parallel the primary bottleneck on the stock kernel is pidmap_lock, largely because of a back-to-back acquire in the common case. This aspect is fixed with the patch. Performance improvement varies between reboots. When benchmarking with 20 processes creating and killing threads in a loop, the unpatched baseline hovers around 465k ops/s, while patched is anything between ~510k ops/s and ~560k depending on false-sharing (which I only minimally sanitized). So this is at least 10% if you are unlucky. The change also facilitated some cosmetic fixes. It has an unintentional side effect of no longer issuing spurious idr_preload() around idr_replace(). Signed-off-by: Mateusz Guzik --- kernel/pid.c | 131 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 85 insertions(+), 46 deletions(-) diff --git a/kernel/pid.c b/kernel/pid.c index a31771bc89c1..f45ae56db7da 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -159,58 +159,86 @@ void free_pids(struct pid **pids) free_pid(pids[tmp]); } -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] = {}; + int pid_max[MAX_PID_NS_LEVEL + 1] = {}; struct pid *pid; enum pid_type type; int i, nr; struct pid_namespace *tmp; struct upid *upid; int retval = -ENOMEM; + bool retried_preload; /* - * 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); + /* + * Prep before we take locks: + * + * 1. allocate and fill in pid struct + */ pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL); if (!pid) return ERR_PTR(retval); - tmp = ns; + get_pid_ns(ns); pid->level = ns->level; + refcount_set(&pid->count, 1); + spin_lock_init(&pid->lock); + for (type = 0; type < PIDTYPE_MAX; ++type) + INIT_HLIST_HEAD(&pid->tasks[type]); + init_waitqueue_head(&pid->wait_pidfd); + INIT_HLIST_HEAD(&pid->inodes); - for (i = ns->level; i >= 0; i--) { - int tid = 0; - int pid_max = 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 should + * later code need it. + */ + for (tmp = ns, i = ns->level; i >= 0; i--) { + pid_max[ns->level - i] = READ_ONCE(tmp->pid_max); - if (set_tid_size) { - tid = set_tid[ns->level - i]; + if (arg_set_tid_size) { + int tid = set_tid[ns->level - i] = arg_set_tid[ns->level - i]; retval = -EINVAL; - if (tid < 1 || tid >= pid_max) - goto out_free; + if (tid < 1 || tid >= pid_max[ns->level - i]) + goto out_abort; /* * Also fail if a PID != 1 is requested and * no PID 1 exists. */ if (tid != 1 && !tmp->child_reaper) - goto out_free; + goto out_abort; retval = -EPERM; if (!checkpoint_restore_ns_capable(tmp->user_ns)) - goto out_free; - set_tid_size--; + goto out_abort; + arg_set_tid_size--; } - idr_preload(GFP_KERNEL); - spin_lock(&pidmap_lock); + tmp = tmp->parent; + } + + /* + * Prep is done, id allocation goes here: + */ + retried_preload = false; + idr_preload(GFP_KERNEL); + spin_lock(&pidmap_lock); + for (tmp = ns, i = ns->level; i >= 0;) { + int tid = set_tid[ns->level - i]; if (tid) { nr = idr_alloc(&tmp->idr, NULL, tid, @@ -220,6 +248,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid, * alreay in use. Return EEXIST in that case. */ if (nr == -ENOSPC) + nr = -EEXIST; } else { int pid_min = 1; @@ -235,19 +264,42 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid, * a partially initialized PID (see below). */ nr = idr_alloc_cyclic(&tmp->idr, NULL, pid_min, - pid_max, GFP_ATOMIC); + pid_max[ns->level - i], GFP_ATOMIC); + if (nr == -ENOSPC) + nr = -EAGAIN; } - spin_unlock(&pidmap_lock); - idr_preload_end(); - if (nr < 0) { - retval = (nr == -ENOSPC) ? -EAGAIN : nr; + if (unlikely(nr < 0)) { + /* + * 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). + * + * The 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. + */ + if (nr == -ENOMEM && !retried_preload) { + spin_unlock(&pidmap_lock); + idr_preload_end(); + retried_preload = true; + idr_preload(GFP_KERNEL); + spin_lock(&pidmap_lock); + continue; + } + retval = nr; goto out_free; } pid->numbers[i].nr = nr; pid->numbers[i].ns = tmp; tmp = tmp->parent; + i--; + retried_preload = false; } /* @@ -257,25 +309,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 = -ENOMEM; - - get_pid_ns(ns); - refcount_set(&pid->count, 1); - spin_lock_init(&pid->lock); - for (type = 0; type < PIDTYPE_MAX; ++type) - INIT_HLIST_HEAD(&pid->tasks[type]); - - init_waitqueue_head(&pid->wait_pidfd); - INIT_HLIST_HEAD(&pid->inodes); - - upid = 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 >= pid->numbers; --upid) { + for (upid = pid->numbers + ns->level; upid >= 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 +328,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid, return pid; -out_unlock: - spin_unlock(&pidmap_lock); - idr_preload_end(); - put_pid_ns(ns); - out_free: - spin_lock(&pidmap_lock); while (++i <= ns->level) { upid = pid->numbers + i; idr_remove(&upid->ns->idr, upid->nr); @@ -303,7 +339,10 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid, idr_set_cursor(&ns->idr, 0); spin_unlock(&pidmap_lock); + idr_preload_end(); +out_abort: + put_pid_ns(ns); kmem_cache_free(ns->pid_cachep, pid); return ERR_PTR(retval); } -- 2.48.1