Received: by 2002:a05:7412:ba23:b0:fa:4c10:6cad with SMTP id jp35csp1043034rdb; Fri, 19 Jan 2024 06:38:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IEVtsFcVMMU9EGjnVYF1uvGTWBpSEbx1D7OLZxNtNjOvc4tAamnmxuzDIaGWKVGfF3Lc9vZ X-Received: by 2002:a92:c6d2:0:b0:361:9667:1a5f with SMTP id v18-20020a92c6d2000000b0036196671a5fmr2368898ilm.124.1705675084636; Fri, 19 Jan 2024 06:38:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705675084; cv=pass; d=google.com; s=arc-20160816; b=xCLxTFYwLj4bX041qGnpAYLbms8GPuCl5DjpCdcRhyWUcNP1Pkay02bQG/8lnqzmeZ 1PRd9Pqb3zjVxxAUmIIUveV01oOKHhA8i9YvrV2JhkfPyid7yCTBCg70VaWBE9kTiZHQ fp8ebF0omYJf+551y+jbcWIZ/JYsCmeFjlW4v7kJQe8ANJeNmAzadEMoWqFgaKx4do4p rATUAKmlusdfVJlB1rHg9m4+stgrB4LjV56tSri/Extl2gLsMmsNaZhAftsTxpvVfGpD 7hrZHHFMZrw9C722u4WdfDNck6pfbIaW9o+HtwHXkqayMQ1iGIKGqfAikmtzdu9TJ3C7 08Rw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=7YFFwcg/CPkBkjIKKEkE3loFN/H6LCe9ccQqBi45MX4=; fh=KxsgfbqBAouwXGSyu/AaAda2jVuGMYqSU7/YAswB3bg=; b=KwyPnG6goyZlrrZzEfGm7J9YoTaC4o+xZp+1xf2y3Wk++UuSJB7LBJsdM9XZTnh1Lv +my6HA61V6yOZNVzspIvp2kXN4mrFp0UmnibejKDibOEFmpUG1Q2Oa1areV021aRDxEW JiRENPiZvvC2l1zlBOfrwqB7qx5YR0kjp3m1TYSzhHMeCErvKQwDaHVn9LpSQ47GvfDV mURdnw7AD/Khn8yYak7KnNNy149vy2sl5YG8i0b/y7bvjjL/Nwk9d9NK7lxwN1B3xg1G xj8z7v2NFZWlC66BPE0+0Whs7AU2eLjJOQ4slCKmLTsbEIPVIJWZhnPT9onWbjVnTZWE PkSw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=naNkM+aY; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-31265-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-31265-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id s20-20020a639254000000b005b915369815si3343667pgn.155.2024.01.19.06.38.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 06:38:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-31265-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=naNkM+aY; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-31265-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-31265-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 23561288ABA for ; Fri, 19 Jan 2024 14:37:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A0C0753E00; Fri, 19 Jan 2024 14:37:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="naNkM+aY" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C76D0524D4; Fri, 19 Jan 2024 14:37:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705675046; cv=none; b=egQZjpuvYsFisz4knJoZ8h0a+QqFsR9jZeveuM6l4cRD5EFJmA5u45AKrSHunust1gK/2dlPf68pKVAm3EN9yGXU9LIsj0Fg7QC1X+VXlZS/Pn+pv0A6D6gasc7qxOZrT0qqg48qtZshIpvZDZoiPbgKh+tD1MN+msVpR00KhfM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705675046; c=relaxed/simple; bh=T2yJaX7UwbgDvliRWFpAINaJXHgC+01Fx7/x+PKmJMA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=UFjBM1OoT9sgLNyJDyUiXgcM9/KBYzsqmeKdvItgrakMiCQAKQmz6PuoN4vGis+AMB3O7x7U09zva+Sh9pdrIhmoPPw0FGmP+jVuCzy/Y8i5pj3tBJXglGPFJgEjVQUCA/mNaUx74eNRefJxzgCwWE9D9Ph0xx4ncoeIiSSl3j0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=naNkM+aY; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8C942C433C7; Fri, 19 Jan 2024 14:37:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1705675046; bh=T2yJaX7UwbgDvliRWFpAINaJXHgC+01Fx7/x+PKmJMA=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=naNkM+aYQI79/kTXGLkKhTgHzwe822Bnvpmyv2ZQ3BrQz17lQhrdoJOOathBQbPGX yckHj1rsdksXD8wRDkgvLRfS9DTs5DwbQ+g/0wgkGh0Uf5+on1P6Xzjj06m81apWuy eJgn1WjcEDqyWlR1obLE/zq/NHbBoNQ1tEs5LrxXnUDlBfXPCOewWjINgPOL0X6xXk n4vWjiJ38qrSkqbbFodeS5h5Bsu76ffQIOHOxrBDwnl9ZR+1LO6KsFV0+dlaaBj9eW tUO+3n3VUvewfjYW0gHbN1+GizeB1VTPMHO8OmBijxChuO471pmCI/56KYlJkzL2d+ Laqoe7MVKySpA== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 18B48CE04A0; Fri, 19 Jan 2024 06:37:26 -0800 (PST) Date: Fri, 19 Jan 2024 06:37:26 -0800 From: "Paul E. McKenney" To: Chen Zhongjin Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, yangjihong1@huawei.com, naveen.n.rao@linux.ibm.com, anil.s.keshavamurthy@intel.com, davem@davemloft.net, mhiramat@kernel.org, akpm@linux-foundation.org, tglx@linutronix.de, peterz@infradead.org, pmladek@suse.com, dianders@chromium.org, npiggin@gmail.com, mpe@ellerman.id.au, jkl820.git@gmail.com, juerg.haefliger@canonical.com, rick.p.edgecombe@intel.com, eric.devolder@oracle.com, mic@digikod.net Subject: Re: [PATCH v2] kprobes: Use synchronize_rcu_tasks_rude in kprobe_optimizer Message-ID: Reply-To: paulmck@kernel.org References: <20240118021842.290665-1-chenzhongjin@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240118021842.290665-1-chenzhongjin@huawei.com> On Thu, Jan 18, 2024 at 02:18:42AM +0000, Chen Zhongjin wrote: > There is a deadlock scenario in kprobe_optimizer(): > > pid A pid B pid C > kprobe_optimizer() do_exit() perf_kprobe_init() > mutex_lock(&kprobe_mutex) exit_tasks_rcu_start() mutex_lock(&kprobe_mutex) > synchronize_rcu_tasks() zap_pid_ns_processes() // waiting kprobe_mutex > // waiting tasks_rcu_exit_srcu kernel_wait4() > // waiting pid C exit > > To avoid this deadlock loop, use synchronize_rcu_tasks_rude() in kprobe_optimizer() > rather than synchronize_rcu_tasks(). synchronize_rcu_tasks_rude() can also promise > that all preempted tasks have scheduled, but it will not wait tasks_rcu_exit_srcu. > > Fixes: a30b85df7d59 ("kprobes: Use synchronize_rcu_tasks() for optprobe with CONFIG_PREEMPT=y") > Signed-off-by: Chen Zhongjin Just so you know, your email ends up in gmail's spam folder. :-/ > --- > v1 -> v2: Add Fixes tag > --- > arch/Kconfig | 2 +- > kernel/kprobes.c | 2 +- > 2 files changed, 2 insertions(+), 2 deletions(-) > > diff --git a/arch/Kconfig b/arch/Kconfig > index f4b210ab0612..dc6a18854017 100644 > --- a/arch/Kconfig > +++ b/arch/Kconfig > @@ -104,7 +104,7 @@ config STATIC_CALL_SELFTEST > config OPTPROBES > def_bool y > depends on KPROBES && HAVE_OPTPROBES > - select TASKS_RCU if PREEMPTION > + select TASKS_RUDE_RCU > > config KPROBES_ON_FTRACE > def_bool y > diff --git a/kernel/kprobes.c b/kernel/kprobes.c > index d5a0ee40bf66..09056ae50c58 100644 > --- a/kernel/kprobes.c > +++ b/kernel/kprobes.c > @@ -623,7 +623,7 @@ static void kprobe_optimizer(struct work_struct *work) > * Note that on non-preemptive kernel, this is transparently converted > * to synchronoze_sched() to wait for all interrupts to have completed. > */ > - synchronize_rcu_tasks(); > + synchronize_rcu_tasks_rude(); Again, that comment reads in full as follows: /* * Step 2: Wait for quiesence period to ensure all potentially * preempted tasks to have normally scheduled. Because optprobe * may modify multiple instructions, there is a chance that Nth * instruction is preempted. In that case, such tasks can return * to 2nd-Nth byte of jump instruction. This wait is for avoiding it. * Note that on non-preemptive kernel, this is transparently converted * to synchronoze_sched() to wait for all interrupts to have completed. */ Please note well that first sentence. Unless that first sentence no longer holds, this patch cannot work because synchronize_rcu_tasks_rude() will not (repeat, NOT) wait for preempted tasks. So how to safely break this deadlock? Reproducing Chen Zhongjin's diagram: pid A pid B pid C kprobe_optimizer() do_exit() perf_kprobe_init() mutex_lock(&kprobe_mutex) exit_tasks_rcu_start() mutex_lock(&kprobe_mutex) synchronize_rcu_tasks() zap_pid_ns_processes() // waiting kprobe_mutex // waiting tasks_rcu_exit_srcu kernel_wait4() // waiting pid C exit We need to stop synchronize_rcu_tasks() from waiting on tasks like pid B that are voluntarily blocked. One way to do that is to replace SRCU with a set of per-CPU lists. Then exit_tasks_rcu_start() adds the current task to this list and does ... OK, this is getting a bit involved. If you would like to follow along, please feel free to look here: https://docs.google.com/document/d/1MEHHs5qbbZBzhN8dGP17pt-d87WptFJ2ZQcqS221d9I/edit?usp=sharing Thanx, Paul > /* Step 3: Optimize kprobes after quiesence period */ > do_optimize_kprobes(); > -- > 2.25.1 >