Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp2350238lqt; Mon, 22 Apr 2024 08:23:26 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVWCi15EOAv4cc3qTTdssLbEr+uQKn3A952sVvuukizBREbQMIIoSZGiuLDttVH18rFyJIG7w5+ZaO3+aUxfoKhAsqB7pZwiHXh7zdGug== X-Google-Smtp-Source: AGHT+IF+jGpD9hlqUbHIA2KZH/1hkwqetTYZUaTc0meGCcUy8fnHFhahmJnUhqVbcLJuIBuDxUS8 X-Received: by 2002:a05:6359:4181:b0:186:11ac:36ff with SMTP id ki1-20020a056359418100b0018611ac36ffmr10634405rwc.9.1713799406134; Mon, 22 Apr 2024 08:23:26 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713799406; cv=pass; d=google.com; s=arc-20160816; b=d8GP2n4e3wFiw46JOd3kQept1ifpXfjVuaUkc9OUzZYuzuQzFGV2GdnDJlV7LHhAd+ pzdXnMJ1pKmK2fkBT79gNiswdlGEzkBAd2U9yU850bjkzzZrjadPphBGZ6Gl+v4ydZWG 9VStpWjrAzkrkC5fqa+XB/7gv8jTHiRbWvp7L8AzzuPz1sCMuowTaba254gP/vSWZOI4 7ZDDX8ak2JyvNF2ZqbxZZepBdPFQv2mCT0NkVIMd6t3iMbrwYyd0puJIuUwRluIHs1ny K5UXIPmhMPsCIUWR2UfYMLPj6Uv2XjbUxi+VsMYmsjKgN9TGlapsPk9IzGCFsdo3Gc2b 0pLg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=AcPXrLLIIF4N6GfTd86JIIR1EXlS23NU4a6xjGYi2f8=; fh=s25kNjeCVivgErIvYPqTjGEMlBYn38pjdUD9JyYDbzA=; b=PKlJfW7JNqJpYzsyMsRsq5963dDyBcA8Doo/456fG9uetG4sVcErYf/OJQ2Mihv2nq kOmnc2SbGuv+9Yyefse/DKCku7pGVAK0qsoXcP70P7EAyV2nepBV9Z7jQyES219n4VTj zX86dToPPVnG5nDimDSlMfbq/2l1yTpL9zKC3CF+cZe2tWDBjvtHKMAdS2QjPtYfJivA B97CJPK4lkOEPhXkNIASH6fHTUhgkBsZ8tnZoK49CTOrPdAP4e1pbwJvDIvQxVojKjx/ EuVyax29jtJ0daZRWwe6iGcCZmrMSvRHfnnqc5WpUqb64DVdAj70KmqolyzZh1FrDaG8 Egmg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=t4Y9gaGb; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-153623-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-153623-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id g8-20020a0561020cc800b0047a3cccbbe2si1716408vst.271.2024.04.22.08.23.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 08:23:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-153623-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=t4Y9gaGb; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-153623-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-153623-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 69A731C2160A for ; Mon, 22 Apr 2024 15:22:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5520A1534F1; Mon, 22 Apr 2024 15:22:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="t4Y9gaGb" 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 3AFBE152180; Mon, 22 Apr 2024 15:22:40 +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=1713799361; cv=none; b=BVuc+cTNms2lOpHRe1TjvFAzobvCXMtvCANZMQ99pMuUR8/hoyGGFVqOLmRh2252kGJ0ZCJIONIfgIQBIJk90NExcQlLyqOjQcuQDi0JBGTDXoj994eWCwrAqopi0ZLubaSNpZWgSnVllruqcwfW8Kb6Ca/p4YplDTahyO2o1cU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713799361; c=relaxed/simple; bh=//8KcpjpbvYDgi/6QOo9g1ClZ/5OAjmHm3CIAJSIR9w=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=Jv1HrTsSu3nCVhbKnfkoMG0tYiPWOv+S+Ih19fQlyoo/Xt8as2ZB9jvqRWOqGvOPIC2Q7QmLVrT/foytkrk1b8j1rluK9sRseKxPIxN5AsgEAYih7nctWiRmDUMnQWv99ysXQ6kXjTvgi9/42D5oQn2KdCgzauFcLGz9jeiYgz8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=t4Y9gaGb; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id EE60CC113CC; Mon, 22 Apr 2024 15:22:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713799360; bh=//8KcpjpbvYDgi/6QOo9g1ClZ/5OAjmHm3CIAJSIR9w=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=t4Y9gaGbcPig7Mdz+LEqNoPlC4cnjArOJwRu5PaTXNJM863eIgFwMZcNkZH2wD+xf kANHiNvsMwpyRee3B8kJFco0aLqPEj9cnwQ7bvQnl+Onc9cuuTAlN0EBqoYqG4GD1K Uq06fxqySyGqrw4bTCBBz3lxoCwSwvQ+w/Moka8jk6OisDm8TCmV1HFjPVLnqP3mM2 m4DDvLwuDzyNTM3J4scNNiWJPH+u8HOSTYu7nSEa5MMiqTWBXnRO0Jt6kvr5vllvh8 IiXT6yl9ZpmaE5gviSwAwoRJg9AYQFB9plw4gCzSY3pG/zlcO4UnIqAWMR7XdTop3i 7X6EKYTKe0ZfQ== Date: Tue, 23 Apr 2024 00:22:34 +0900 From: Masami Hiramatsu (Google) To: Jonathan Haslam Cc: linux-trace-kernel@vger.kernel.org, andrii@kernel.org, bpf@vger.kernel.org, rostedt@goodmis.org, Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] uprobes: reduce contention on uprobes_tree access Message-Id: <20240423002234.6104df82cb3f96c9d485d5b1@kernel.org> In-Reply-To: <20240422102306.6026-1-jonathan.haslam@gmail.com> References: <20240422102306.6026-1-jonathan.haslam@gmail.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) 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=UTF-8 Content-Transfer-Encoding: 8bit On Mon, 22 Apr 2024 03:23:05 -0700 Jonathan Haslam wrote: > Active uprobes are stored in an RB tree and accesses to this tree are > dominated by read operations. Currently these accesses are serialized by > a spinlock but this leads to enormous contention when large numbers of > threads are executing active probes. > > This patch converts the spinlock used to serialize access to the > uprobes_tree RB tree into a reader-writer spinlock. This lock type > aligns naturally with the overwhelmingly read-only nature of the tree > usage here. Although the addition of reader-writer spinlocks are > discouraged [0], this fix is proposed as an interim solution while an > RCU based approach is implemented (that work is in a nascent form). This > fix also has the benefit of being trivial, self contained and therefore > simple to backport. > > We have used a uprobe benchmark from the BPF selftests [1] to estimate > the improvements. Each block of results below show 1 line per execution > of the benchmark ("the "Summary" line) and each line is a run with one > more thread added - a thread is a "producer". The lines are edited to > remove extraneous output. > > The tests were executed with this driver script: > > for num_threads in {1..20} > do > sudo ./bench -a -p $num_threads trig-uprobe-nop | grep Summary > done > > SPINLOCK (BEFORE) > ================== > Summary: hits 1.396 ± 0.007M/s ( 1.396M/prod) > Summary: hits 1.656 ± 0.016M/s ( 0.828M/prod) > Summary: hits 2.246 ± 0.008M/s ( 0.749M/prod) > Summary: hits 2.114 ± 0.010M/s ( 0.529M/prod) > Summary: hits 2.013 ± 0.009M/s ( 0.403M/prod) > Summary: hits 1.753 ± 0.008M/s ( 0.292M/prod) > Summary: hits 1.847 ± 0.001M/s ( 0.264M/prod) > Summary: hits 1.889 ± 0.001M/s ( 0.236M/prod) > Summary: hits 1.833 ± 0.006M/s ( 0.204M/prod) > Summary: hits 1.900 ± 0.003M/s ( 0.190M/prod) > Summary: hits 1.918 ± 0.006M/s ( 0.174M/prod) > Summary: hits 1.925 ± 0.002M/s ( 0.160M/prod) > Summary: hits 1.837 ± 0.001M/s ( 0.141M/prod) > Summary: hits 1.898 ± 0.001M/s ( 0.136M/prod) > Summary: hits 1.799 ± 0.016M/s ( 0.120M/prod) > Summary: hits 1.850 ± 0.005M/s ( 0.109M/prod) > Summary: hits 1.816 ± 0.002M/s ( 0.101M/prod) > Summary: hits 1.787 ± 0.001M/s ( 0.094M/prod) > Summary: hits 1.764 ± 0.002M/s ( 0.088M/prod) > > RW SPINLOCK (AFTER) > =================== > Summary: hits 1.444 ± 0.020M/s ( 1.444M/prod) > Summary: hits 2.279 ± 0.011M/s ( 1.139M/prod) > Summary: hits 3.422 ± 0.014M/s ( 1.141M/prod) > Summary: hits 3.565 ± 0.017M/s ( 0.891M/prod) > Summary: hits 2.671 ± 0.013M/s ( 0.534M/prod) > Summary: hits 2.409 ± 0.005M/s ( 0.401M/prod) > Summary: hits 2.485 ± 0.008M/s ( 0.355M/prod) > Summary: hits 2.496 ± 0.003M/s ( 0.312M/prod) > Summary: hits 2.585 ± 0.002M/s ( 0.287M/prod) > Summary: hits 2.908 ± 0.011M/s ( 0.291M/prod) > Summary: hits 2.346 ± 0.016M/s ( 0.213M/prod) > Summary: hits 2.804 ± 0.004M/s ( 0.234M/prod) > Summary: hits 2.556 ± 0.001M/s ( 0.197M/prod) > Summary: hits 2.754 ± 0.004M/s ( 0.197M/prod) > Summary: hits 2.482 ± 0.002M/s ( 0.165M/prod) > Summary: hits 2.412 ± 0.005M/s ( 0.151M/prod) > Summary: hits 2.710 ± 0.003M/s ( 0.159M/prod) > Summary: hits 2.826 ± 0.005M/s ( 0.157M/prod) > Summary: hits 2.718 ± 0.001M/s ( 0.143M/prod) > Summary: hits 2.844 ± 0.006M/s ( 0.142M/prod) > > The numbers in parenthesis give averaged throughput per thread which is > of greatest interest here as a measure of scalability. Improvements are > in the order of 22 - 68% with this particular benchmark (mean = 43%). > > V2: > - Updated commit message to include benchmark results. > > [0] https://docs.kernel.org/locking/spinlocks.html > [1] https://github.com/torvalds/linux/blob/master/tools/testing/selftests/bpf/benchs/bench_trigger.c Thanks for update! This looks good to me. Let me pick this for probes/for-next. Thank you, > > Signed-off-by: Jonathan Haslam > --- > kernel/events/uprobes.c | 22 +++++++++++----------- > 1 file changed, 11 insertions(+), 11 deletions(-) > > diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c > index e4834d23e1d1..8ae0eefc3a34 100644 > --- a/kernel/events/uprobes.c > +++ b/kernel/events/uprobes.c > @@ -39,7 +39,7 @@ static struct rb_root uprobes_tree = RB_ROOT; > */ > #define no_uprobe_events() RB_EMPTY_ROOT(&uprobes_tree) > > -static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */ > +static DEFINE_RWLOCK(uprobes_treelock); /* serialize rbtree access */ > > #define UPROBES_HASH_SZ 13 > /* serialize uprobe->pending_list */ > @@ -669,9 +669,9 @@ static struct uprobe *find_uprobe(struct inode *inode, loff_t offset) > { > struct uprobe *uprobe; > > - spin_lock(&uprobes_treelock); > + read_lock(&uprobes_treelock); > uprobe = __find_uprobe(inode, offset); > - spin_unlock(&uprobes_treelock); > + read_unlock(&uprobes_treelock); > > return uprobe; > } > @@ -701,9 +701,9 @@ static struct uprobe *insert_uprobe(struct uprobe *uprobe) > { > struct uprobe *u; > > - spin_lock(&uprobes_treelock); > + write_lock(&uprobes_treelock); > u = __insert_uprobe(uprobe); > - spin_unlock(&uprobes_treelock); > + write_unlock(&uprobes_treelock); > > return u; > } > @@ -935,9 +935,9 @@ static void delete_uprobe(struct uprobe *uprobe) > if (WARN_ON(!uprobe_is_active(uprobe))) > return; > > - spin_lock(&uprobes_treelock); > + write_lock(&uprobes_treelock); > rb_erase(&uprobe->rb_node, &uprobes_tree); > - spin_unlock(&uprobes_treelock); > + write_unlock(&uprobes_treelock); > RB_CLEAR_NODE(&uprobe->rb_node); /* for uprobe_is_active() */ > put_uprobe(uprobe); > } > @@ -1298,7 +1298,7 @@ static void build_probe_list(struct inode *inode, > min = vaddr_to_offset(vma, start); > max = min + (end - start) - 1; > > - spin_lock(&uprobes_treelock); > + read_lock(&uprobes_treelock); > n = find_node_in_range(inode, min, max); > if (n) { > for (t = n; t; t = rb_prev(t)) { > @@ -1316,7 +1316,7 @@ static void build_probe_list(struct inode *inode, > get_uprobe(u); > } > } > - spin_unlock(&uprobes_treelock); > + read_unlock(&uprobes_treelock); > } > > /* @vma contains reference counter, not the probed instruction. */ > @@ -1407,9 +1407,9 @@ vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long e > min = vaddr_to_offset(vma, start); > max = min + (end - start) - 1; > > - spin_lock(&uprobes_treelock); > + read_lock(&uprobes_treelock); > n = find_node_in_range(inode, min, max); > - spin_unlock(&uprobes_treelock); > + read_unlock(&uprobes_treelock); > > return !!n; > } > -- > 2.43.0 > -- Masami Hiramatsu (Google)