From mboxrd@z Thu Jan 1 00:00:00 1970 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 43B7BC0218A for ; Tue, 28 Jan 2025 18:30:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8E36C280222; Tue, 28 Jan 2025 13:30:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 893602801A6; Tue, 28 Jan 2025 13:30:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7351C280222; Tue, 28 Jan 2025 13:30:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 69C2B2801A6 for ; Tue, 28 Jan 2025 13:30:14 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C17B71C6BA6 for ; Tue, 28 Jan 2025 18:30:13 +0000 (UTC) X-FDA: 83057700306.10.4198261 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf27.hostedemail.com (Postfix) with ESMTP id B2ADA40019 for ; Tue, 28 Jan 2025 18:30:11 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=fcBtmEbm; spf=pass (imf27.hostedemail.com: domain of oleg@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=oleg@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738089011; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5lJCewjvQpX5HZ5kYY65cGpBlC2Zbx9RhBUOy2QEULQ=; b=GQdCFCeZCqt/SfmCDVAIwDfuAipgGbXHQ9vdUKPJL1KMUoIIjTrYlT1N3ZE3tGXBsxyVP7 AvSC1hXeUCOMsaARG02CHCP3JzkVjSMhWYJxeBEMHpLg9ORvCoWo5Dac64VEdUsVovlxKw RD180RIlHh8KPBF5KCZml6TWsj7xIgw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738089011; a=rsa-sha256; cv=none; b=0eFdT7bwaMkgTU0HPW6fWpf5Sc5zaEzVrRFeci1JGWacX4AfjG1goO66DkT6U9f0MIkkxG 8ZwlvhBlUHlDhUb9mUh/a/XcTN4e1hd9qdKDcSyC4tHRoO9zH0KAJHGdm2MHIaSmg3nJrw 6MpuscrOHdUdK62QYGLIcmtCzOU66Gw= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=fcBtmEbm; spf=pass (imf27.hostedemail.com: domain of oleg@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=oleg@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738089011; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=5lJCewjvQpX5HZ5kYY65cGpBlC2Zbx9RhBUOy2QEULQ=; b=fcBtmEbmALmaksiePuXIVtO6GTeb6TKgfVXyr4Ff5L/OfhJg/Oe828r+lha/mTEFFrgmav nRgRRicyJE+X4Qfk9NpRoM5wHLxS5Lj5SmnmeaXKDuqlv/5xCHcVqmjDhLdSeWUBl6faTP mgfn4CPUGLC4P3bu5XuyIb9vTB6Df5g= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-629-LUMrfqt6PsKpz6xlV95JBg-1; Tue, 28 Jan 2025 13:30:06 -0500 X-MC-Unique: LUMrfqt6PsKpz6xlV95JBg-1 X-Mimecast-MFC-AGG-ID: LUMrfqt6PsKpz6xlV95JBg Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1D21118009C5; Tue, 28 Jan 2025 18:30:04 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.226.70]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id E04FD195608E; Tue, 28 Jan 2025 18:30:00 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Tue, 28 Jan 2025 19:29:37 +0100 (CET) Date: Tue, 28 Jan 2025 19:29:33 +0100 From: Oleg Nesterov To: Mateusz Guzik Cc: brauner@kernel.org, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, "Eric W. Biederman" Subject: Re: [PATCH v2] exit: perform randomness and pid work without tasklist_lock Message-ID: <20250128182932.GC24845@redhat.com> References: <20250128160743.3142544-1-mjguzik@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250128160743.3142544-1-mjguzik@gmail.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Rspamd-Queue-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TZ2ag0 GVpaUnsW nTVUxPvJacGuAefnEVEn5Sq+jOL4K0LO+N42OvXrwZLXjpfbMbSEMNHajAsQQJbEzljsBMhuA7+IH9lq9iTFm9f26C5BWjx0+c9mjOETpHdHqJjELSvSRldKyaeV28OBrn2VARVhW0mPPkf407eH/83O2fwMte8ofUIhp4qk87YvOL3KjLPysFwktEpQgSzbCQcn/0x/TzQdmoWbBrTBi32b9pO+YwUIrpcMAbhYTXOIAL/C2WAPiJ2xc2EIjsnaC/4Fg1iXNwM6rLSWV6Lljhjb62PDBQtrZ1MdxSkioAqDWPRibByhYuqFW7jO85nGgCuYoHSuaDYTJAXK2MTIvBxUhz1mBK7V0flQWyKBRyzgYOf01bydfwH0GFhIJHk8zjAK5 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: (Add Eric). On 01/28, Mateusz Guzik wrote: > > Both add_device_randomness() and attach_pid()/detach_pid() So afaics this patch does 2 different things, and I do think this needs 2 separate patches. Can you split this change please? As for add_device_randomness(). I must have missed something, but I still can't understand why we can't simply shift add_device_randomness(p->sum_exec_runtime) to release_release_task() and avoid release_task_post->randomness. You said: I wanted to keep the load where it was but why??? Again, I must have missed something, but to me this simply adds the unnecessary complications. Either way, ->sum_exec_runtime is not stable even if task-to-release != current, so what is the point? Oleg. > have their > own synchronisation mechanisms. > > The clone side calls them *without* the tasklist_lock held, meaning > parallel calls can contend on their locks. > > The exit side calls them *with* the tasklist_lock lock, which means the > hold time is avoidably extended by waiting on either of the 2 locks, in > turn exacerbating contention on tasklist_lock itself. > > Postponing the work until after the lock is dropped bumps thread > creation/destruction rate by 15% on a 24 core vm. > > Bench (plop into will-it-scale): > $ cat tests/threadspawn1.c > > char *testcase_description = "Thread creation and teardown"; > > static void *worker(void *arg) > { > return (NULL); > } > > void testcase(unsigned long long *iterations, unsigned long nr) > { > pthread_t thread; > int error; > > while (1) { > error = pthread_create(&thread, NULL, worker, NULL); > assert(error == 0); > error = pthread_join(thread, NULL); > assert(error == 0); > (*iterations)++; > } > } > > Run: > $ ./threadspawn1_processes -t 24 > > Signed-off-by: Mateusz Guzik > --- > > v2: > - introduce a struct to collect work > - move out pids as well > > there is more which can be pulled out > > this may look suspicious: > + proc_flush_pid(p->thread_pid); > > AFAICS this is constant for the duration of the lifetime, so i don't > there is a problem > > include/linux/pid.h | 1 + > kernel/exit.c | 53 +++++++++++++++++++++++++++++++++------------ > kernel/pid.c | 23 +++++++++++++++----- > 3 files changed, 58 insertions(+), 19 deletions(-) > > diff --git a/include/linux/pid.h b/include/linux/pid.h > index 98837a1ff0f3..6e9fcacd02cd 100644 > --- a/include/linux/pid.h > +++ b/include/linux/pid.h > @@ -101,6 +101,7 @@ 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 struct pid *detach_pid_return(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); > diff --git a/kernel/exit.c b/kernel/exit.c > index 1dcddfe537ee..4e452d3e3a89 100644 > --- a/kernel/exit.c > +++ b/kernel/exit.c > @@ -122,14 +122,23 @@ static __init int kernel_exit_sysfs_init(void) > late_initcall(kernel_exit_sysfs_init); > #endif > > -static void __unhash_process(struct task_struct *p, bool group_dead) > +/* > + * For things release_task would like to do *after* tasklist_lock is released. > + */ > +struct release_task_post { > + unsigned long long randomness; > + struct pid *pids[PIDTYPE_MAX]; > +}; > + > +static void __unhash_process(struct release_task_post *post, struct task_struct *p, > + bool group_dead) > { > nr_threads--; > - detach_pid(p, PIDTYPE_PID); > + post->pids[PIDTYPE_PID] = detach_pid_return(p, PIDTYPE_PID); > if (group_dead) { > - detach_pid(p, PIDTYPE_TGID); > - detach_pid(p, PIDTYPE_PGID); > - detach_pid(p, PIDTYPE_SID); > + post->pids[PIDTYPE_TGID] = detach_pid_return(p, PIDTYPE_TGID); > + post->pids[PIDTYPE_PGID] = detach_pid_return(p, PIDTYPE_PGID); > + post->pids[PIDTYPE_SID] = detach_pid_return(p, PIDTYPE_SID); > > list_del_rcu(&p->tasks); > list_del_init(&p->sibling); > @@ -141,7 +150,8 @@ static void __unhash_process(struct task_struct *p, bool group_dead) > /* > * This function expects the tasklist_lock write-locked. > */ > -static void __exit_signal(struct task_struct *tsk) > +static void __exit_signal(struct release_task_post *post, > + struct task_struct *tsk) > { > struct signal_struct *sig = tsk->signal; > bool group_dead = thread_group_leader(tsk); > @@ -174,8 +184,7 @@ static void __exit_signal(struct task_struct *tsk) > sig->curr_target = next_thread(tsk); > } > > - add_device_randomness((const void*) &tsk->se.sum_exec_runtime, > - sizeof(unsigned long long)); > + post->randomness = tsk->se.sum_exec_runtime; > > /* > * Accumulate here the counters for all threads as they die. We could > @@ -197,7 +206,7 @@ static void __exit_signal(struct task_struct *tsk) > 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); > > /* > @@ -240,9 +249,13 @@ void __weak release_thread(struct task_struct *dead_task) > void release_task(struct task_struct *p) > { > struct task_struct *leader; > - struct pid *thread_pid; > int zap_leader; > + struct release_task_post post; > repeat: > + memset(&post, 0, sizeof(post)); > + > + proc_flush_pid(p->thread_pid); > + > /* don't need to get the RCU readlock here - the process is dead and > * can't be modifying its own credentials. But shut RCU-lockdep up */ > rcu_read_lock(); > @@ -253,8 +266,7 @@ void release_task(struct task_struct *p) > > write_lock_irq(&tasklist_lock); > ptrace_release_task(p); > - thread_pid = get_pid(p->thread_pid); > - __exit_signal(p); > + __exit_signal(&post, p); > > /* > * If we are the last non-leader member of the thread > @@ -276,8 +288,21 @@ void release_task(struct task_struct *p) > } > > write_unlock_irq(&tasklist_lock); > - proc_flush_pid(thread_pid); > - put_pid(thread_pid); > + > + /* > + * Process clean up deferred to after we drop the tasklist lock. > + */ > + add_device_randomness((const void*) &post.randomness, > + sizeof(unsigned long long)); > + if (post.pids[PIDTYPE_PID]) > + free_pid(post.pids[PIDTYPE_PID]); > + if (post.pids[PIDTYPE_TGID]) > + free_pid(post.pids[PIDTYPE_TGID]); > + if (post.pids[PIDTYPE_PGID]) > + free_pid(post.pids[PIDTYPE_PGID]); > + if (post.pids[PIDTYPE_SID]) > + free_pid(post.pids[PIDTYPE_SID]); > + > release_thread(p); > put_task_struct_rcu_user(p); > > diff --git a/kernel/pid.c b/kernel/pid.c > index 3a10a7b6fcf8..047cdbcef5cf 100644 > --- a/kernel/pid.c > +++ b/kernel/pid.c > @@ -343,7 +343,7 @@ 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, > +static struct pid *__change_pid(struct task_struct *task, enum pid_type type, > struct pid *new) > { > struct pid **pid_ptr = task_pid_ptr(task, type); > @@ -362,20 +362,33 @@ static void __change_pid(struct task_struct *task, enum pid_type type, > > for (tmp = PIDTYPE_MAX; --tmp >= 0; ) > if (pid_has_task(pid, tmp)) > - return; > + return NULL; > > - free_pid(pid); > + return pid; > +} > + > +struct pid *detach_pid_return(struct task_struct *task, enum pid_type type) > +{ > + return __change_pid(task, type, NULL); > } > > void detach_pid(struct task_struct *task, enum pid_type type) > { > - __change_pid(task, type, NULL); > + struct pid *pid; > + > + pid = detach_pid_return(task, type); > + if (pid) > + free_pid(pid); > } > > void change_pid(struct task_struct *task, enum pid_type type, > struct pid *pid) > { > - __change_pid(task, type, pid); > + struct pid *opid; > + > + opid = __change_pid(task, type, pid); > + if (opid) > + free_pid(opid); > attach_pid(task, type); > } > > -- > 2.43.0 >