From patchwork Sat Feb 1 16:31:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mateusz Guzik X-Patchwork-Id: 13956257 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A7382C0218A for ; Sat, 1 Feb 2025 16:31:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 37DC9280004; Sat, 1 Feb 2025 11:31:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2DE78280001; Sat, 1 Feb 2025 11:31:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 131B8280004; Sat, 1 Feb 2025 11:31:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id E7746280001 for ; Sat, 1 Feb 2025 11:31:40 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id AF011B1CD5 for ; Sat, 1 Feb 2025 16:31:40 +0000 (UTC) X-FDA: 83071916760.30.339841B Received: from mail-ed1-f42.google.com (mail-ed1-f42.google.com [209.85.208.42]) by imf26.hostedemail.com (Postfix) with ESMTP id A73AD14000F for ; Sat, 1 Feb 2025 16:31:38 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ig7gwdCI; spf=pass (imf26.hostedemail.com: domain of mjguzik@gmail.com designates 209.85.208.42 as permitted sender) smtp.mailfrom=mjguzik@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738427498; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=9gAV0esEOMPqj1wMAKXBZQiiLf9TUFsrr9mzaYygi1E=; b=V4RuSjo6nbV309ZD4srTSU2qzLgkydf4lZHT+dQGivMCbNDUBHpbu9DGNpgKhVc8vn8l25 A6sO6EBM2zAf37Y5EN0+n7pwnQzzM8dFKbx5BD7AhnFxs3tTr8jKWKVoEF0xNY3KVHV9W6 evHg6EConY5t3O6MnpLzPzFZYCf9jT8= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ig7gwdCI; spf=pass (imf26.hostedemail.com: domain of mjguzik@gmail.com designates 209.85.208.42 as permitted sender) smtp.mailfrom=mjguzik@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738427498; a=rsa-sha256; cv=none; b=Sqq/foRsaf49lOQiEatMarNvGTNXDGFaL9EMeQ8mGMwtfc0KqNGnj2MQrApw4m1Za1BEWg XN5+LtLfKJTf1jn7dbjk5UC2ADO59p+Fr1FVoFuHwwPoMKdEiYoV6ONaipY2eT28HK1QIX yHiMvqkWx8n2wOOuJOmky+MdBJ6B7lY= Received: by mail-ed1-f42.google.com with SMTP id 4fb4d7f45d1cf-5d9837f201aso7574766a12.0 for ; Sat, 01 Feb 2025 08:31:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738427497; x=1739032297; darn=kvack.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=9gAV0esEOMPqj1wMAKXBZQiiLf9TUFsrr9mzaYygi1E=; b=ig7gwdCIEsXUs2CpkD+OX+uKmzVyBai+Kjo9yiMj1vlLkwvqNdGkvOYQ8yiqwF4BjH DzJymg8AZK/cQOiikENgcTGE5TUre74YaVJFdt8nShePTokIqoJ8X2D7jyJtiaii0oSO b4ttgfSU0CDLNHMa0Ik2q6t11c9h71aBkjBnhyf4jmnDeelSIGzbdLzv3tnQDxAOyPI5 m+Wb4AuBaIfiRiRTFsIuO9jjw9x0AsSJvnM9KkbC1qIObugKDbWRtJbke0gTiLJ2r/FA RJmk4O7tMun5X/tydj/uTxeE4w1iGEakaAHbpmD9UbTgHqV4m5TvCff7Fc+uzyfX3KYk bTcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738427497; x=1739032297; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9gAV0esEOMPqj1wMAKXBZQiiLf9TUFsrr9mzaYygi1E=; b=cXhZolEO2cfYanGJ5mabL6Z1AUDVxLIH81v5ySt2xpTv8HlPN15g06FjoruvV0g3wb u/5LbIhDID35zYiieffFiM106rUEbl7EmzCjy0e989oXDn7tE9OomBN29jTuCfbQM5No tTlYpIeL/veP/j8ukkOyEdYezAeP7MF+6Uc1/fcrTwkSziQKGdjZ+WjBgBo24eTIqr2k sX+HU9B/Ltm/8Iisw1+2624VZxhi6RAHa9qcDwpn66X9klhmmv+wG4FKm3eDHBDrQxOC uvIxzasbE2r61nvMzCSjtmVFyObx+CaOS5a8AipCqfzGGKyTdVe3lCrOAF5EKWsdc3CA KyTQ== X-Forwarded-Encrypted: i=1; AJvYcCXhIHA9DhqeFDkAL+8wR2JydMiqAnbVvlTRZ9ejN1wrLHSF0DGaOOfquGM3PCQ68jPFdd70dlhRJQ==@kvack.org X-Gm-Message-State: AOJu0YwfXupxkyJ47jPvED9wvosUjkoHBv0/fgo577yddHBxA+1nHIA3 kX+uRvGrVlu1ZNiPAJtLp5TP+fwRspNF+2yM/KuidZGMbARvKPTcBjwSuA== X-Gm-Gg: ASbGncv5/xQHaIXtktKaLACI1qt2qc8NwryzkaxVu1Nh4Ay5JJfllKRabA8PSSmTk7e KPsows2i97WfM5VH5pJ+ORQ5Qk3ouuSH9ckFn4jSmWr1f4LH91nfSXveubSCHM6BPGAZtm/d0Rs 0P5AYzef7IidCusqBExhORLPnR/A8yi2oB3jn7HtOQ4chR/x0zWHhDgAxEyKgLPmyzHfB7lNik3 s56xQ9+ntWZhOsykxQHbtbKkkbkcnRqVnAbiGyWoS9ICEJpwzsHVjkkASULJ1jX96ZLshXADQrO D0dJmJORjp57/qpQoCWB8KW1Jl14p/NyrQ== X-Google-Smtp-Source: AGHT+IF4wAmURNewahoQOVfYM6M5jXjyc6eKsbposvu/SCSkZtrKzenFnpL1jbA6kPR8ePzJ8PJdEw== X-Received: by 2002:a05:6402:847:b0:5dc:783e:3efe with SMTP id 4fb4d7f45d1cf-5dc7fbb58ebmr8188423a12.6.1738427496993; Sat, 01 Feb 2025 08:31:36 -0800 (PST) Received: from f.. (cst-prg-25-223.cust.vodafone.cz. [46.135.25.223]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5dc723cff72sm4636959a12.15.2025.02.01.08.31.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 01 Feb 2025 08:31:36 -0800 (PST) From: Mateusz Guzik To: ebiederm@xmission.com, oleg@redhat.com Cc: brauner@kernel.org, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Mateusz Guzik Subject: [PATCH v3 5/6] pid: perform free_pid() calls outside of tasklist_lock Date: Sat, 1 Feb 2025 17:31:05 +0100 Message-ID: <20250201163106.28912-6-mjguzik@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250201163106.28912-1-mjguzik@gmail.com> References: <20250201163106.28912-1-mjguzik@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: A73AD14000F X-Stat-Signature: r48gmd3eiph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YPAdpzrTab4i39Y0DGXKcgB8A8l7LDTU7d9FC1L5+4B3MpkQX0P3EvBDpcRvcFXNfmbEvKoZBsvz1B1u46M/ETB5O8sK427/QTL+kaChKMooIhSPhQXUREXQjY2NxmIFe0H+1gAdLcbWi+C/XKq0YbBvZgE31RM/sTObBt9UkY69XdCzDH5zlRpACEmOD72zB0MHlpqFsBeHaFeJdTFXs4NfAeRpxmQ80t1mVw5iYZHDAp1TsY7C+I5+bJ+tZlFijhEpT0O5JGG0LyhvaW7205zb6Q0q5AIgoqpk2yOsDXi124ggf1NZWhqIvDp0D0fMbsdq820Hlgv6XIuy4dzqN0AU5sZyTInLg4SQdz0gebv+6sV51fiZodR X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: As the clone side already executes pid allocation with only pidmap_lock held, issuing free_pid() while still holding tasklist_lock exacerbates total hold time of the latter. Signed-off-by: Mateusz Guzik --- include/linux/pid.h | 7 ++++--- kernel/exit.c | 15 +++++++++------ kernel/pid.c | 44 ++++++++++++++++++++++---------------------- kernel/sys.c | 14 +++++++++----- 4 files changed, 44 insertions(+), 36 deletions(-) diff --git a/include/linux/pid.h b/include/linux/pid.h index 98837a1ff0f3..311ecebd7d56 100644 --- a/include/linux/pid.h +++ b/include/linux/pid.h @@ -101,9 +101,9 @@ extern struct pid *get_task_pid(struct task_struct *task, enum pid_type type); * these helpers must be called with the tasklist_lock write-held. */ extern void attach_pid(struct task_struct *task, enum pid_type); -extern void detach_pid(struct task_struct *task, enum pid_type); -extern void change_pid(struct task_struct *task, enum pid_type, - struct pid *pid); +void detach_pid(struct pid **pids, struct task_struct *task, enum pid_type); +void change_pid(struct pid **pids, struct task_struct *task, enum pid_type, + struct pid *pid); extern void exchange_tids(struct task_struct *task, struct task_struct *old); extern void transfer_pid(struct task_struct *old, struct task_struct *new, enum pid_type); @@ -129,6 +129,7 @@ extern struct pid *find_ge_pid(int nr, struct pid_namespace *); extern struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid, size_t set_tid_size); extern void free_pid(struct pid *pid); +void free_pids(struct pid **pids); extern void disable_pid_allocation(struct pid_namespace *ns); /* diff --git a/kernel/exit.c b/kernel/exit.c index d2c74f93f7d2..a90a0d159570 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -127,16 +127,18 @@ late_initcall(kernel_exit_sysfs_init); */ struct release_task_post { struct tty_struct *tty; + struct pid *pids[PIDTYPE_MAX]; }; -static void __unhash_process(struct task_struct *p, bool group_dead) +static void __unhash_process(struct release_task_post *post, struct task_struct *p, + bool group_dead) { nr_threads--; - detach_pid(p, PIDTYPE_PID); + detach_pid(post->pids, p, PIDTYPE_PID); if (group_dead) { - detach_pid(p, PIDTYPE_TGID); - detach_pid(p, PIDTYPE_PGID); - detach_pid(p, PIDTYPE_SID); + detach_pid(post->pids, p, PIDTYPE_TGID); + detach_pid(post->pids, p, PIDTYPE_PGID); + detach_pid(post->pids, p, PIDTYPE_SID); list_del_rcu(&p->tasks); list_del_init(&p->sibling); @@ -200,7 +202,7 @@ static void __exit_signal(struct release_task_post *post, struct task_struct *ts task_io_accounting_add(&sig->ioac, &tsk->ioac); sig->sum_sched_runtime += tsk->se.sum_exec_runtime; sig->nr_threads--; - __unhash_process(tsk, group_dead); + __unhash_process(post, tsk, group_dead); write_sequnlock(&sig->stats_lock); /* @@ -285,6 +287,7 @@ void release_task(struct task_struct *p) put_pid(thread_pid); add_device_randomness((const void*) &p->se.sum_exec_runtime, sizeof(unsigned long long)); + free_pids(post.pids); release_thread(p); put_task_struct_rcu_user(p); tty_kref_put(post.tty); diff --git a/kernel/pid.c b/kernel/pid.c index 2ae872f689a7..73625f28c166 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -88,20 +88,6 @@ struct pid_namespace init_pid_ns = { }; EXPORT_SYMBOL_GPL(init_pid_ns); -/* - * Note: disable interrupts while the pidmap_lock is held as an - * interrupt might come in and do read_lock(&tasklist_lock). - * - * If we don't disable interrupts there is a nasty deadlock between - * detach_pid()->free_pid() and another cpu that does - * spin_lock(&pidmap_lock) followed by an interrupt routine that does - * read_lock(&tasklist_lock); - * - * After we clean up the tasklist_lock and know there are no - * irq handlers that take it we can leave the interrupts enabled. - * For now it is easier to be safe than to prove it can't happen. - */ - static __cacheline_aligned_in_smp DEFINE_SPINLOCK(pidmap_lock); seqcount_spinlock_t pidmap_lock_seq = SEQCNT_SPINLOCK_ZERO(pidmap_lock_seq, &pidmap_lock); @@ -128,10 +114,11 @@ static void delayed_put_pid(struct rcu_head *rhp) void free_pid(struct pid *pid) { - /* We can be called with write_lock_irq(&tasklist_lock) held */ int i; unsigned long flags; + lockdep_assert_not_held(&tasklist_lock); + spin_lock_irqsave(&pidmap_lock, flags); for (i = 0; i <= pid->level; i++) { struct upid *upid = pid->numbers + i; @@ -160,6 +147,18 @@ void free_pid(struct pid *pid) call_rcu(&pid->rcu, delayed_put_pid); } +void free_pids(struct pid **pids) +{ + int tmp; + + /* + * This can batch pidmap_lock. + */ + for (tmp = PIDTYPE_MAX; --tmp >= 0; ) + if (pids[tmp]) + free_pid(pids[tmp]); +} + struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid, size_t set_tid_size) { @@ -347,8 +346,8 @@ void attach_pid(struct task_struct *task, enum pid_type type) hlist_add_head_rcu(&task->pid_links[type], &pid->tasks[type]); } -static void __change_pid(struct task_struct *task, enum pid_type type, - struct pid *new) +static void __change_pid(struct pid **pids, struct task_struct *task, + enum pid_type type, struct pid *new) { struct pid **pid_ptr, *pid; int tmp; @@ -370,18 +369,19 @@ static void __change_pid(struct task_struct *task, enum pid_type type, if (pid_has_task(pid, tmp)) return; - free_pid(pid); + WARN_ON(pids[type]); + pids[type] = pid; } -void detach_pid(struct task_struct *task, enum pid_type type) +void detach_pid(struct pid **pids, struct task_struct *task, enum pid_type type) { - __change_pid(task, type, NULL); + __change_pid(pids, task, type, NULL); } -void change_pid(struct task_struct *task, enum pid_type type, +void change_pid(struct pid **pids, struct task_struct *task, enum pid_type type, struct pid *pid) { - __change_pid(task, type, pid); + __change_pid(pids, task, type, pid); attach_pid(task, type); } diff --git a/kernel/sys.c b/kernel/sys.c index cb366ff8703a..4efca8a97d62 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -1085,6 +1085,7 @@ SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid) { struct task_struct *p; struct task_struct *group_leader = current->group_leader; + struct pid *pids[PIDTYPE_MAX] = { 0 }; struct pid *pgrp; int err; @@ -1142,13 +1143,14 @@ SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid) goto out; if (task_pgrp(p) != pgrp) - change_pid(p, PIDTYPE_PGID, pgrp); + change_pid(pids, p, PIDTYPE_PGID, pgrp); err = 0; out: /* All paths lead to here, thus we are safe. -DaveM */ write_unlock_irq(&tasklist_lock); rcu_read_unlock(); + free_pids(pids); return err; } @@ -1222,21 +1224,22 @@ SYSCALL_DEFINE1(getsid, pid_t, pid) return retval; } -static void set_special_pids(struct pid *pid) +static void set_special_pids(struct pid **pids, struct pid *pid) { struct task_struct *curr = current->group_leader; if (task_session(curr) != pid) - change_pid(curr, PIDTYPE_SID, pid); + change_pid(pids, curr, PIDTYPE_SID, pid); if (task_pgrp(curr) != pid) - change_pid(curr, PIDTYPE_PGID, pid); + change_pid(pids, curr, PIDTYPE_PGID, pid); } int ksys_setsid(void) { struct task_struct *group_leader = current->group_leader; struct pid *sid = task_pid(group_leader); + struct pid *pids[PIDTYPE_MAX] = { 0 }; pid_t session = pid_vnr(sid); int err = -EPERM; @@ -1252,13 +1255,14 @@ int ksys_setsid(void) goto out; group_leader->signal->leader = 1; - set_special_pids(sid); + set_special_pids(pids, sid); proc_clear_tty(group_leader); err = session; out: write_unlock_irq(&tasklist_lock); + free_pids(pids); if (err > 0) { proc_sid_connector(group_leader); sched_autogroup_create_attach(group_leader);