Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp118503lqh; Wed, 27 Mar 2024 17:18:59 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUOiJ/hL4yZPGVJrMEfEbWeHwCDHAlEATV6hrI8T2D7ayjhnD/CrS7qtB49YClzvnSgog27btYPN6XZ4MFmsboIjf02HhxYhh0y5yDYow== X-Google-Smtp-Source: AGHT+IHWmQ7cUpll0tcsnWizOzvVOY9wCYvMs3oCW0PWg0grR5RKUM+LFdnaR6OrZU5Dk2c2o9G2 X-Received: by 2002:a17:902:f607:b0:1e2:367:9879 with SMTP id n7-20020a170902f60700b001e203679879mr1816867plg.45.1711585138703; Wed, 27 Mar 2024 17:18:58 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711585138; cv=pass; d=google.com; s=arc-20160816; b=YJrhD3obzFO/TrqqRppQH45jlNQJje1Fb7RMJqj1Bbxr7mFWL0J0npJtcf98iHyr5p u1qf7e+VwcM0LdjfO5i5iTJhfhexopNnUOX25WCFK1sXyJoqQYv7VwkyVSwiVCVuXC4p KUehHAGATI4Wbf6ezQRrRYWguA4zcpoBZtDwPiamnX7/y06gjimjx2H7Hqt6TOcgv+Rt 3eglQCkJVIrWNwEZmoHLihZM+I/aZrmQOJSAw33zpsny+pgKGsfi+fS/S6FpMpSOf279 yFGv2Obbpo7l9lO3foBny4Iq1PJgzO48vFpzC65dxh/pU/vwUxCSHeTvN4Bu7XEf4Uas EcTQ== 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=jORP8QQA/ompQP4/fhdor1A0XDwt7Osg7dwXyHGIgCQ=; fh=J8pOb9lFdqL2+9oBSvkV/lin9qrUma9xMWuJ8v9mO3o=; b=e7rugzQmyzPkoYxdGAdScFZwy+LkiWWsgYl8wVZk7eBFgPVzwRK18AQeV1ivR/3IMU QR9EQ7OBqD3wwwYo50DxpcJRgTp30ZnC+qREX7mlJZwZNEwa7u+MuNGHXAr9kSLqQLHu rlSGgyt6dNlP7mUt9A3YxtapLbsS+/jOCUUK7/NPenMfLwKi8qMVh2DXvSKG8nz4fenE VQikAkF4AAV9etSbfEGYJaBwNaXU4cHxFosrGD3SaHdC4hiYDP0Ysvx5dtxKQFL7iHql lgVUIjSvj6KKe0KXGnkGYkXmA43GN0+DPCAh6DA4x4GlimI2Gr5xLV5SH6I75wqsuOgM r35w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=q8E9L4JY; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-122208-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122208-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 z18-20020a170902ccd200b001dd779ddb5fsi219728ple.596.2024.03.27.17.18.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 17:18:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-122208-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=q8E9L4JY; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-122208-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122208-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 5DE44297783 for ; Thu, 28 Mar 2024 00:18:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 794E7BE6C; Thu, 28 Mar 2024 00:18:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="q8E9L4JY" 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 7E68A46BF; Thu, 28 Mar 2024 00:18:45 +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=1711585126; cv=none; b=e+Awu8mHtyFyWKju1cF7JIuJQJGPnvzY2YVuSpIXEH9mwdHOzljFyZfvCpxg5XjSZdKavjYxZWdqcf/BS8AONnGQF+AfT0ikQaxC8xfEvh9MpaTxZ+pPAR64KpNrSBBlU0sSg+w+0KI5lBdX9HAIsSS3e97Ky5mrQGf2WYI0uZc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711585126; c=relaxed/simple; bh=zsWvlwMlsTmNhgbPPby4pHnVE3v+PWwV4d8Iew4UmEY=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=U1MQXyyZenm37ZzQQ3MTsgV5GVqli7L/ZKUtmzOanL/3Tr4D3kPRSBswA/QaiT3gAiHuuFG40L58SYDOlMOrTcpaVwXf4KEEdkKHc9pxEvUIqDJQzPs27l/YdE2PAi6am2FfhMa8z2xl+0s85XLafxG+EbYXL5P5el3/wv6znk0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=q8E9L4JY; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id A6194C433F1; Thu, 28 Mar 2024 00:18:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711585125; bh=zsWvlwMlsTmNhgbPPby4pHnVE3v+PWwV4d8Iew4UmEY=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=q8E9L4JYgtRQazG941hY9iq9Ao7UCltmzgAzNf9H61D2wfNai/cDIjYj/WM2HJKGQ WuKQzpUCcM595zNc3cjmizTX1CRLwtWvTszj5hcxhNFOkBlDhBg/E0jLF5DkwWD+vX wtQbPoxnEFak7+9Vj9ne/6G2tYoY4dZoqLgU8/83pR9YU1XWU2e2o5uofQoIeXjLpu SWEVg6aux2xrzhEkHD7qX4QteLy7LZ6Lu3pTndHd7Z89TFnXh7eTxzW9nmA+0O5/lr CfGdwK/2Ennnq+xrmppDNd+vHwGtxBZwZ6hl3ssIM1kDlF/glf95DNV58ImhVIA0nh IHa2dmAlfCblg== Date: Thu, 28 Mar 2024 09:18:41 +0900 From: Masami Hiramatsu (Google) To: Jonthan Haslam Cc: Andrii Nakryiko , 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: <20240328091841.ce9cc613db375536de843cfb@kernel.org> In-Reply-To: <54jakntmdyedadce7yrf6kljcjapbwyoqqt26dnllrqvs3pg7x@itra4a2ikgqw> References: <20240321145736.2373846-1-jonathan.haslam@gmail.com> <20240325120323.ec3248d330b2755e73a6571e@kernel.org> <20240327084245.a890ae12e579f0be1902ae4a@kernel.org> <54jakntmdyedadce7yrf6kljcjapbwyoqqt26dnllrqvs3pg7x@itra4a2ikgqw> X-Mailer: Sylpheed 3.8.0beta1 (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=US-ASCII Content-Transfer-Encoding: 7bit On Wed, 27 Mar 2024 17:06:01 +0000 Jonthan Haslam wrote: > > > 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. > > My initial tests show a nice improvement on the over RW spinlocks but > significant regression in acquiring a write lock. I've got a few days > vacation over Easter but I'll aim to get some more formalised results out > to the thread toward the end of next week. As far as the write lock is only on the cold path, I think you can choose per-cpu RW semaphore. Since it does not do busy wait, the total system performance impact will be small. I look forward to your formalized results :) Thank you, > > Jon. > > > > > 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) -- Masami Hiramatsu (Google)