Received: by 2002:ab2:f03:0:b0:1ef:ffd0:ce49 with SMTP id i3csp113945lqf; Tue, 26 Mar 2024 16:43:02 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUxMev38K2kMfA0akda1HsjNbL5/oAeHUk04RqEreWqluhGt2WsL/NCTzBaEFWpwGBF9qFs1KEzPjw5F07N3HIv57bSnU1SshkCbAENmg== X-Google-Smtp-Source: AGHT+IFLS1y3FgUzddX9QWmdlMhw9nilrDfmpQoVNKJOBcJ9zpB3V5Qhcwaa2cTruc2z3cVQ3cfa X-Received: by 2002:a05:620a:43a7:b0:788:1b28:b977 with SMTP id a39-20020a05620a43a700b007881b28b977mr1672936qkp.39.1711496581774; Tue, 26 Mar 2024 16:43:01 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711496581; cv=pass; d=google.com; s=arc-20160816; b=z0QQZ8+q+Yvs/yN8ZoeBtw27tlHck0M0RekEp9cq0WzK8dTz1IG25WdWiFAuK+84Ou Zrql/+W9tjUsN0MgGOcpk9M8Plq8qQJjzMz+riJQsiPxIR8ZEKo8DVFe73fBqWWsF5ZF hzbTe4UjcYvsItwqvU5goQdr8rpE8H6Lmeg4o8ARX/17utYO656IH7aMkapfGdeemstz L+3s90/OIIRRDV52CnRddLomSnRse/vioNdbTtB1nmLzh+4EClDkUdn5CCBLhJGNKgTU En7fdNAb7qhfV3KowGTi0980SXd1J6MxXex9vKBMHuKPNmnEVu76brVHJ2i6m6mcf5B9 tSOA== 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=lW4ilQAQTDKC0Ie+JvEvr8qCaoHCIdPxkGRtIpihcdQ=; fh=9z9gJx2JgNAgyg3MyTRbD4HR4Q0ZA06FBl0vzkTmQoY=; b=TdjKNWIqtCgyqEIsIfCUfAH/n+zraGT4YrlmKIZPOQY2exJnPb/voBNkKLVeWXsFHU /ME2IIcDohdNVCY7tmmQS2bJMPE7s1uAhrNeBAHVegbTft3heNPIbFwCwZZPpaQlX5oE LAZ8yBP2lcNzyGFFk0pMLw3mpMWdt3jLi6qba2RaLkeMQMQ+GcWpXul4+v7Akq71g3Qb 0IGdAHAwtlq1jwJirvKX51xn/paofRgKZ3za9VfN7LTWdqgKjOgoNhw4rjFCRy7jNE9R +FgnBLzOku0mqSX6KgCxQMnxA9AbC/J74LExset+N4o802rc0DMWxEa4bqFrTA/eSgej VajA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=IUTFzT5C; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-120144-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-120144-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 x24-20020a05620a099800b0078a36e7bd63si8527369qkx.491.2024.03.26.16.43.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 16:43:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-120144-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=IUTFzT5C; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-120144-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-120144-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 70A381C31AF1 for ; Tue, 26 Mar 2024 23:43:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7F2CE13E3E8; Tue, 26 Mar 2024 23:42:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="IUTFzT5C" 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 72C9E1E494; Tue, 26 Mar 2024 23:42:51 +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=1711496571; cv=none; b=QcgaoBpBLFIm4N+SRPUdXXuV12AP2TfrfFF4VKYC318a8Y2JiKIzMH0T9S6tem6nJy3jKLeiWcBYOO7inMKdiXnm8Jn/jaKMJ5+jA4NaoPV0wtuvcTHJndKcINhXFTpoLHnuZ+ftLjyMQg1xEU1RydqWvm5KPveTBTxq4FrTlU0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711496571; c=relaxed/simple; bh=FjP5n+j42VnJWYHH8MMl8FSdezlqHQIMCm7rYqBcEB8=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=Ajj8SaM0SLXkfmyHgof0cjtoYf7tj4S8V5qwrbUuWjigPSGHaU8GLoGMigGti0eKA50lFnmOzaoahcl6P5mOEJ6dacO1/a/A17UGVXtPR54UfMkQu1Xyj1/Ny9WwBj9W1zEf7eKCoOyQ51Q6EeTXM799+GvMFlZbQLjOeGC+NP8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=IUTFzT5C; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 357F1C433F1; Tue, 26 Mar 2024 23:42:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711496570; bh=FjP5n+j42VnJWYHH8MMl8FSdezlqHQIMCm7rYqBcEB8=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=IUTFzT5CTa+9vMCYpQcjKj+Vq5vfFQLP34fHE2oSRQJuHBcAzFVzA75LB8SpXTjaJ nYG7xl4pWrEfq1Z/3cWkeJUrWV+DdpBeA2dh4ZwJhbBJRBbciuzGTGMa+8NJSKNOG6 8DW91EEZ5W3NpIRlF8MJFrPhux06t+8xdHqJicF1suJ0NzVUfTnExWXWib9QXI30vw DebLXSyxPOXr62OSj2y7y8VGVaARnAdDRSUfbZPD0W0Co0fgBvZnzXtkvY6BqWwcs4 sABIAb7NsGstc2yp8Q25T9VJPQNZTgDUIkI8/9PMU7aVwyBrBZFPKA21nd9EEiNBAT xnItDEKrNg8cg== Date: Wed, 27 Mar 2024 08:42:45 +0900 From: Masami Hiramatsu (Google) To: Andrii Nakryiko Cc: Jonathan Haslam , 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] uprobes: reduce contention on uprobes_tree access Message-Id: <20240327084245.a890ae12e579f0be1902ae4a@kernel.org> In-Reply-To: References: <20240321145736.2373846-1-jonathan.haslam@gmail.com> <20240325120323.ec3248d330b2755e73a6571e@kernel.org> 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 Tue, 26 Mar 2024 09:01:47 -0700 Andrii Nakryiko wrote: > On Sun, Mar 24, 2024 at 8:03 PM Masami Hiramatsu wrote: > > > > On Thu, 21 Mar 2024 07:57:35 -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. > > > > > > This change has been tested against production workloads that exhibit > > > significant contention on the spinlock and an almost order of magnitude > > > reduction for mean uprobe execution time is observed (28 -> 3.5 microsecs). > > > > Looks good to me. > > > > Acked-by: Masami Hiramatsu (Google) > > Masami, > > Given the discussion around per-cpu rw semaphore and need for > (internal) batched attachment API for uprobes, do you think you can > apply this patch as is for now? We can then gain initial improvements > in scalability that are also easy to backport, and Jonathan will work > on a more complete solution based on per-cpu RW semaphore, as > suggested by Ingo. Yeah, it is interesting to use per-cpu rw semaphore on uprobe. I would like to wait for the next version. Thank you, > > > > > BTW, how did you measure the overhead? I think spinlock overhead > > will depend on how much lock contention happens. > > > > Thank you, > > > > > > > > [0] https://docs.kernel.org/locking/spinlocks.html > > > > > > 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 929e98c62965..42bf9b6e8bc0 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) -- Masami Hiramatsu (Google)