From patchwork Tue Sep 3 17:45:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13789153 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 1B2FFCD37AB for ; Tue, 3 Sep 2024 17:46:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A09B08D01B8; Tue, 3 Sep 2024 13:46:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BA138D018A; Tue, 3 Sep 2024 13:46:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8344D8D01B8; Tue, 3 Sep 2024 13:46:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 640748D018A for ; Tue, 3 Sep 2024 13:46:14 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 08D3A1607D2 for ; Tue, 3 Sep 2024 17:46:14 +0000 (UTC) X-FDA: 82524155868.07.6ED3D3A Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf22.hostedemail.com (Postfix) with ESMTP id 6714BC0012 for ; Tue, 3 Sep 2024 17:46:12 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=LDavbUqo; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of andrii@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=andrii@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725385466; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=m4BH+SwmaFOBSjPhmTCV44mznN5J0Dtj/Obb7FS9b9Q=; b=yLt3AQXRhefYLn26cRjNhECbCFmbz3ol2ami4ykG58Jyy61OMJl4ur2woYMZ32zpG3JziC 60K+MjfMQNxlPbfCWIgpUI1BnX1X1owT+vi8luYfn4dELNLcbEmoqt/njfBqNWZRYIhTuB X8NeHapdmGbbUJb0gyVJIqJk34ZOcgU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725385466; a=rsa-sha256; cv=none; b=qX91y3NHJI7jAfv4ZA9UDHSRlkkGYjuOxQFeRHar3UqPifRkm03VVv85AejUoyIE9rwJMY D6gQyZTZUhcnOJMqbdn/XHLCh2wMtSv9eXHhcoDMskmmx4QIfggYxI9WkVxOSNq4VP5FMQ o85/1Hb1IIR/ixlFzfTY4E9ts6C55UI= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=LDavbUqo; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of andrii@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=andrii@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 56A3EA42704; Tue, 3 Sep 2024 17:46:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1FDB3C4CEC6; Tue, 3 Sep 2024 17:46:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725385571; bh=Rxg7Tu+G/2GmZjbMNIm7JPVsxbOaRPHJxJ4sKFPmwPg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LDavbUqo2ULvupumGo7jK7HVlePICRjWhr3thztapEL7jWeMQr8jOjE8MsQELcsrd TurIIRYALlNU5tjt+RyHklryWjSQqn4jom7vAB5atB8Sv8SSsjLPIEL7oLPx7xyjZp 78f5+AItuXdlbwD8dsPIMo+YbRj9yxR1F8BzLtJzzrNiUnJJ2WINRf1CRi95e9nSOw I49TxFFhXtcQxSfzXzNhpiPbQKirUDkxLX4a+4Oxr2I4ra1GQy1eXRYW5dhHLUTh8n +EN6LAQqu1QfVXcxGeF0ByoS4iQLBIJ/rJrbC20vqv9xUJpS8So+BCAZWmkWG7Aypa Hls70L6bBAuKg== From: Andrii Nakryiko To: linux-trace-kernel@vger.kernel.org, peterz@infradead.org, oleg@redhat.com Cc: rostedt@goodmis.org, mhiramat@kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, jolsa@kernel.org, paulmck@kernel.org, willy@infradead.org, surenb@google.com, akpm@linux-foundation.org, linux-mm@kvack.org, Andrii Nakryiko Subject: [PATCH v5 1/8] uprobes: revamp uprobe refcounting and lifetime management Date: Tue, 3 Sep 2024 10:45:56 -0700 Message-ID: <20240903174603.3554182-2-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240903174603.3554182-1-andrii@kernel.org> References: <20240903174603.3554182-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 6714BC0012 X-Stat-Signature: 7endm7tf585hhqdf867rkmyc637tnadc X-Rspam-User: X-HE-Tag: 1725385572-84602 X-HE-Meta: U2FsdGVkX18y07BUIeDar//G2d/aVOjwFnS12ZdUx2ymncirmVh59U/I7mPbNlnEYNWpzYrH9fTL3Lxqji0An1nFls/F9XzHI69tlRyMC4Op2GOX+OULc0OPmB6XQbAQBTVU/zTw/TRvgkzb4USpsc/YQaN3xUAENpHJlQKbpvLkJfuchLeiqCIXD4Pxe3vf09YZu3axGLohZeJS+NC4EGQVVSc5MdUfPlF0pHVRgzwidsLFGCzHmjTlQu+G3GRdVW8ljD68HtPuxGGb7fkZXOuNRtbiHjPr98ZrZQTCCCbkbsGnj2tyLRM+6BgBhqv8oBRzoDT1Z6I86eP05eW9sWV/v9RPceTdfSl5Ukds+asO3iZdkyiJhi+MIU/W8X8Xv6ir7ub3wWvAHHJIZThf+ymjkHt26HudxxnKW7JDvIzDO44kaR2Or7KfNe9Zb1RgwIBujfXLD0TRoKcdk7G4b+a3eHsCqtYO6cJvtUiAxpW6NoAyhyDSI6XAbg0gg+FPsaIfFbwi7czr13GiFFkj22ffBkCuRmex7TCBRsd0j/ooS0TS7Lg1RFsxc2uOJBKRRlO1nOZFIOLh0J8+CobdkiEoZAMjPAg1ND54miF/oF3ECl/JsdwDO5U5QdkZ3Z7FK6W5bmGWtYY68IL3hI1OTUuYn8QmmtbFa+8J1w4EjEN1Mn3idurgoOET+JDwS0+4/uYEfBaMshouT3NNv1bipIDrNU0zSjpTqT6w39tDLF0ixTdASERBv2vOIUNFC+aYWJEiCMVZGfW6ikR3pVn2ghMZFA4JfRQzqZuZlxfjNrv8y9F38kym8ONqKoR2Ne8co3mOoTzO1Z9RuB1/COo65+v2RZMd9HQx/WN9wwM+ZsP7GbHxO2NiObsztn/gS+dCDa3OCuNIf081eFgzaJcHnqGTMc0rn4bo+5rh3zNiRATJ+LDTc1qzoTyjca2aS2vEbYVHWj30jfQkntVBosZ RR1ZRaH3 pxV8KhGbsxyNN9sLYxkyTgFXWQvAKVWK+IHR3ODm0Sg5jz/ISiUmQ0h8z8CIlGTBgeSSTqr+DfwoInf3s/GnNfg0CscZF1OV/5+GFgMpfqUlfB5aG1sVTw1BSeISMEPekbIJ1IIxwNMnRJxF5NGjR6C35Eo48Tu2E6qqqlwHUnibBA/gfD7EpM19jRWpkjsaGMAAs8NM0zYHU15KzMt1FgU5ylpwkDtqLbP34nnPKN5Ytf/psq9q6gg5xD45xy1DEjU3dp8z6YYFDWNU= 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: Revamp how struct uprobe is refcounted, and thus how its lifetime is managed. Right now, there are a few possible "owners" of uprobe refcount: - uprobes_tree RB tree assumes one refcount when uprobe is registered and added to the lookup tree; - while uprobe is triggered and kernel is handling it in the breakpoint handler code, temporary refcount bump is done to keep uprobe from being freed; - if we have uretprobe requested on a given struct uprobe instance, we take another refcount to keep uprobe alive until user space code returns from the function and triggers return handler. The uprobe_tree's extra refcount of 1 is confusing and problematic. No matter how many actual consumers are attached, they all share the same refcount, and we have an extra logic to drop the "last" (which might not really be last) refcount once uprobe's consumer list becomes empty. This is unconventional and has to be kept in mind as a special case all the time. Further, because of this design we have the situations where find_uprobe() will find uprobe, bump refcount, return it to the caller, but that uprobe will still need uprobe_is_active() check, after which the caller is required to drop refcount and try again. This is just too many details leaking to the higher level logic. This patch changes refcounting scheme in such a way as to not have uprobes_tree keeping extra refcount for struct uprobe. Instead, each uprobe_consumer is assuming its own refcount, which will be dropped when consumer is unregistered. Other than that, all the active users of uprobe (entry and return uprobe handling code) keeps exactly the same refcounting approach. With the above setup, once uprobe's refcount drops to zero, we need to make sure that uprobe's "destructor" removes uprobe from uprobes_tree, of course. This, though, races with uprobe entry handling code in handle_swbp(), which, through find_active_uprobe()->find_uprobe() lookup, can race with uprobe being destroyed after refcount drops to zero (e.g., due to uprobe_consumer unregistering). So we add try_get_uprobe(), which will attempt to bump refcount, unless it already is zero. Caller needs to guarantee that uprobe instance won't be freed in parallel, which is the case while we keep uprobes_treelock (for read or write, doesn't matter). Note also, we now don't leak the race between registration and unregistration, so we remove the retry logic completely. If find_uprobe() returns valid uprobe, it's guaranteed to remain in uprobes_tree with properly incremented refcount. The race is handled inside __insert_uprobe() and put_uprobe() working together: __insert_uprobe() will remove uprobe from RB-tree, if it can't bump refcount and will retry to insert the new uprobe instance. put_uprobe() won't attempt to remove uprobe from RB-tree, if it's already not there. All that is protected by uprobes_treelock, which keeps things simple. Reviewed-by: Oleg Nesterov Signed-off-by: Andrii Nakryiko --- kernel/events/uprobes.c | 179 +++++++++++++++++++++++----------------- 1 file changed, 101 insertions(+), 78 deletions(-) diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index 33349cc8de0c..147561c19d57 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -109,6 +109,11 @@ struct xol_area { unsigned long vaddr; /* Page(s) of instruction slots */ }; +static void uprobe_warn(struct task_struct *t, const char *msg) +{ + pr_warn("uprobe: %s:%d failed to %s\n", current->comm, current->pid, msg); +} + /* * valid_vma: Verify if the specified vma is an executable vma * Relax restrictions while unregistering: vm_flags might have @@ -587,25 +592,53 @@ set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long v *(uprobe_opcode_t *)&auprobe->insn); } +/* uprobe should have guaranteed positive refcount */ static struct uprobe *get_uprobe(struct uprobe *uprobe) { refcount_inc(&uprobe->ref); return uprobe; } +/* + * uprobe should have guaranteed lifetime, which can be either of: + * - caller already has refcount taken (and wants an extra one); + * - uprobe is RCU protected and won't be freed until after grace period; + * - we are holding uprobes_treelock (for read or write, doesn't matter). + */ +static struct uprobe *try_get_uprobe(struct uprobe *uprobe) +{ + if (refcount_inc_not_zero(&uprobe->ref)) + return uprobe; + return NULL; +} + +static inline bool uprobe_is_active(struct uprobe *uprobe) +{ + return !RB_EMPTY_NODE(&uprobe->rb_node); +} + static void put_uprobe(struct uprobe *uprobe) { - if (refcount_dec_and_test(&uprobe->ref)) { - /* - * If application munmap(exec_vma) before uprobe_unregister() - * gets called, we don't get a chance to remove uprobe from - * delayed_uprobe_list from remove_breakpoint(). Do it here. - */ - mutex_lock(&delayed_uprobe_lock); - delayed_uprobe_remove(uprobe, NULL); - mutex_unlock(&delayed_uprobe_lock); - kfree(uprobe); - } + if (!refcount_dec_and_test(&uprobe->ref)) + return; + + write_lock(&uprobes_treelock); + + if (uprobe_is_active(uprobe)) + rb_erase(&uprobe->rb_node, &uprobes_tree); + + write_unlock(&uprobes_treelock); + + /* + * If application munmap(exec_vma) before uprobe_unregister() + * gets called, we don't get a chance to remove uprobe from + * delayed_uprobe_list from remove_breakpoint(). Do it here. + */ + mutex_lock(&delayed_uprobe_lock); + delayed_uprobe_remove(uprobe, NULL); + mutex_unlock(&delayed_uprobe_lock); + + kfree(uprobe); } static __always_inline @@ -656,7 +689,7 @@ static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset) struct rb_node *node = rb_find(&key, &uprobes_tree, __uprobe_cmp_key); if (node) - return get_uprobe(__node_2_uprobe(node)); + return try_get_uprobe(__node_2_uprobe(node)); return NULL; } @@ -676,26 +709,44 @@ static struct uprobe *find_uprobe(struct inode *inode, loff_t offset) return uprobe; } +/* + * Attempt to insert a new uprobe into uprobes_tree. + * + * If uprobe already exists (for given inode+offset), we just increment + * refcount of previously existing uprobe. + * + * If not, a provided new instance of uprobe is inserted into the tree (with + * assumed initial refcount == 1). + * + * In any case, we return a uprobe instance that ends up being in uprobes_tree. + * Caller has to clean up new uprobe instance, if it ended up not being + * inserted into the tree. + * + * We assume that uprobes_treelock is held for writing. + */ static struct uprobe *__insert_uprobe(struct uprobe *uprobe) { struct rb_node *node; - +again: node = rb_find_add(&uprobe->rb_node, &uprobes_tree, __uprobe_cmp); - if (node) - return get_uprobe(__node_2_uprobe(node)); + if (node) { + struct uprobe *u = __node_2_uprobe(node); - /* get access + creation ref */ - refcount_set(&uprobe->ref, 2); - return NULL; + if (!try_get_uprobe(u)) { + rb_erase(node, &uprobes_tree); + RB_CLEAR_NODE(&u->rb_node); + goto again; + } + + return u; + } + + return uprobe; } /* - * Acquire uprobes_treelock. - * Matching uprobe already exists in rbtree; - * increment (access refcount) and return the matching uprobe. - * - * No matching uprobe; insert the uprobe in rb_tree; - * get a double refcount (access + creation) and return NULL. + * Acquire uprobes_treelock and insert uprobe into uprobes_tree + * (or reuse existing one, see __insert_uprobe() comments above). */ static struct uprobe *insert_uprobe(struct uprobe *uprobe) { @@ -732,11 +783,13 @@ static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset, uprobe->ref_ctr_offset = ref_ctr_offset; init_rwsem(&uprobe->register_rwsem); init_rwsem(&uprobe->consumer_rwsem); + RB_CLEAR_NODE(&uprobe->rb_node); + refcount_set(&uprobe->ref, 1); /* add to uprobes_tree, sorted on inode:offset */ cur_uprobe = insert_uprobe(uprobe); /* a uprobe exists for this inode:offset combination */ - if (cur_uprobe) { + if (cur_uprobe != uprobe) { if (cur_uprobe->ref_ctr_offset != uprobe->ref_ctr_offset) { ref_ctr_mismatch_warn(cur_uprobe, uprobe); put_uprobe(cur_uprobe); @@ -921,26 +974,6 @@ remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vad return set_orig_insn(&uprobe->arch, mm, vaddr); } -static inline bool uprobe_is_active(struct uprobe *uprobe) -{ - return !RB_EMPTY_NODE(&uprobe->rb_node); -} -/* - * There could be threads that have already hit the breakpoint. They - * will recheck the current insn and restart if find_uprobe() fails. - * See find_active_uprobe(). - */ -static void delete_uprobe(struct uprobe *uprobe) -{ - if (WARN_ON(!uprobe_is_active(uprobe))) - return; - - write_lock(&uprobes_treelock); - rb_erase(&uprobe->rb_node, &uprobes_tree); - write_unlock(&uprobes_treelock); - RB_CLEAR_NODE(&uprobe->rb_node); /* for uprobe_is_active() */ -} - struct map_info { struct map_info *next; struct mm_struct *mm; @@ -1094,17 +1127,13 @@ void uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc) int err; down_write(&uprobe->register_rwsem); - if (WARN_ON(!consumer_del(uprobe, uc))) + if (WARN_ON(!consumer_del(uprobe, uc))) { err = -ENOENT; - else + } else { err = register_for_each_vma(uprobe, NULL); - - /* TODO : cant unregister? schedule a worker thread */ - if (!err) { - if (!uprobe->consumers) - delete_uprobe(uprobe); - else - err = -EBUSY; + /* TODO : cant unregister? schedule a worker thread */ + if (unlikely(err)) + uprobe_warn(current, "unregister, leaking uprobe"); } up_write(&uprobe->register_rwsem); @@ -1159,27 +1188,16 @@ struct uprobe *uprobe_register(struct inode *inode, if (!IS_ALIGNED(ref_ctr_offset, sizeof(short))) return ERR_PTR(-EINVAL); - retry: uprobe = alloc_uprobe(inode, offset, ref_ctr_offset); if (IS_ERR(uprobe)) return uprobe; - /* - * We can race with uprobe_unregister()->delete_uprobe(). - * Check uprobe_is_active() and retry if it is false. - */ down_write(&uprobe->register_rwsem); - ret = -EAGAIN; - if (likely(uprobe_is_active(uprobe))) { - consumer_add(uprobe, uc); - ret = register_for_each_vma(uprobe, uc); - } + consumer_add(uprobe, uc); + ret = register_for_each_vma(uprobe, uc); up_write(&uprobe->register_rwsem); - put_uprobe(uprobe); if (ret) { - if (unlikely(ret == -EAGAIN)) - goto retry; uprobe_unregister(uprobe, uc); return ERR_PTR(ret); } @@ -1286,15 +1304,17 @@ static void build_probe_list(struct inode *inode, u = rb_entry(t, struct uprobe, rb_node); if (u->inode != inode || u->offset < min) break; - list_add(&u->pending_list, head); - get_uprobe(u); + /* if uprobe went away, it's safe to ignore it */ + if (try_get_uprobe(u)) + list_add(&u->pending_list, head); } for (t = n; (t = rb_next(t)); ) { u = rb_entry(t, struct uprobe, rb_node); if (u->inode != inode || u->offset > max) break; - list_add(&u->pending_list, head); - get_uprobe(u); + /* if uprobe went away, it's safe to ignore it */ + if (try_get_uprobe(u)) + list_add(&u->pending_list, head); } } read_unlock(&uprobes_treelock); @@ -1752,6 +1772,12 @@ static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask) return -ENOMEM; *n = *o; + /* + * uprobe's refcnt has to be positive at this point, kept by + * utask->return_instances items; return_instances can't be + * removed right now, as task is blocked due to duping; so + * get_uprobe() is safe to use here. + */ get_uprobe(n->uprobe); n->next = NULL; @@ -1763,12 +1789,6 @@ static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask) return 0; } -static void uprobe_warn(struct task_struct *t, const char *msg) -{ - pr_warn("uprobe: %s:%d failed to %s\n", - current->comm, current->pid, msg); -} - static void dup_xol_work(struct callback_head *work) { if (current->flags & PF_EXITING) @@ -1894,7 +1914,10 @@ static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs) } orig_ret_vaddr = utask->return_instances->orig_ret_vaddr; } - + /* + * uprobe's refcnt is positive, held by caller, so it's safe to + * unconditionally bump it one more time here + */ ri->uprobe = get_uprobe(uprobe); ri->func = instruction_pointer(regs); ri->stack = user_stack_pointer(regs); From patchwork Tue Sep 3 17:45:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13789154 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 837E2CD37AB for ; Tue, 3 Sep 2024 17:46:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0BC2F8D01B9; Tue, 3 Sep 2024 13:46:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 06C2E8D018A; Tue, 3 Sep 2024 13:46:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E29288D01B9; Tue, 3 Sep 2024 13:46:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id C296B8D018A for ; Tue, 3 Sep 2024 13:46:17 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 696D21408B4 for ; Tue, 3 Sep 2024 17:46:17 +0000 (UTC) X-FDA: 82524155994.04.AA5A5DC Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf26.hostedemail.com (Postfix) with ESMTP id C743314001B for ; Tue, 3 Sep 2024 17:46:15 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="X/XdVGmB"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of andrii@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=andrii@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725385470; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=6NBPB9PniRGCNQ6nesA6SFCH5a0WeOF0+31oZSlk7M8=; b=FmOZ+22V8HFYra6N9uPo8O7aAsqPXSJbRiTgXvaEN1zI8ssh60ft13OTt2NBMXEOKqWeFE nBEsIucffQJTOudldavZkvBeYvLKQpeZfNx/v8+MEkPzIf/YOBvtaa3w6UJ6GcE3c35pj2 wVtMvL58PO5KqIoKqbO0Fgz90YgVUDo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725385470; a=rsa-sha256; cv=none; b=KnRdEnXLtj6/aGRmtjxFXVnwNc2BR6UiXUwqfV4VzNhfzNbG6SnToAZrC3k9MZ48UHt3NM a91BE2+j/x7XyMUIJSc6Tld3jMwuhoSgZN55cqFKJcaaLYxDeu9N/xAXy+aOzjolXalZa+ l1fBu/PI3dqZlNBVky9/XBHbEuAPeGc= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="X/XdVGmB"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of andrii@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=andrii@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id BF669A43D30; Tue, 3 Sep 2024 17:46:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 68B77C4CEC4; Tue, 3 Sep 2024 17:46:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725385574; bh=LuCsrswkk1SUCus28K1voqG+eAVATzH+TzjbVd85GFQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=X/XdVGmButYhQJdzbx5ponCjmRt/zNDCzmG2Il6HZ2ax0xqGfIBVfHlUccBC5Bkeu dFGbWMZzhbFdfALDvspw8D/QZymNrNH1uCSlW5NmfGeY/IGSIkWMrqvI1EiJzBxQ/5 E7Tk4ApmII2xjbYccuPoQgfClLBBtCoJ+LLpW7csprjeGCHL3WNi61MZWfbtHvpxeR o7Lmg5p2vlzl9HlcgXjfSSdu9aCOkPadrqCUqdOuA0XK51+eWY/sIyeGj/nLJgF/jB gQy2J/2Qp6k7mLDaWqS8JU/gYNwEO0BdMJM77Vjp8aQar9/ZtRtE5r0wTxBylj/O76 dDuU85NfnNJNA== From: Andrii Nakryiko To: linux-trace-kernel@vger.kernel.org, peterz@infradead.org, oleg@redhat.com Cc: rostedt@goodmis.org, mhiramat@kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, jolsa@kernel.org, paulmck@kernel.org, willy@infradead.org, surenb@google.com, akpm@linux-foundation.org, linux-mm@kvack.org, Andrii Nakryiko Subject: [PATCH v5 2/8] uprobes: protected uprobe lifetime with SRCU Date: Tue, 3 Sep 2024 10:45:57 -0700 Message-ID: <20240903174603.3554182-3-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240903174603.3554182-1-andrii@kernel.org> References: <20240903174603.3554182-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: C743314001B X-Stat-Signature: ei9x4a47bunsywuwpzgs3j7diejqohcf X-Rspam-User: X-HE-Tag: 1725385575-704792 X-HE-Meta: U2FsdGVkX1/39QI28Jw/Vi4I/PFdhC5/XOHAyIucouUUZTS5h9wTzYCkeYCtH53hNE3mAE4EKssYL1Y3v4NfiPLM7KvLQ3fdF1T6ctvB1EFWCc5Tea/08BEWeDTloAHdqurV/YJK4NcAOb1EDxCPUWdAvUq4UR0O4pwsUJsrJAhZc7GcWRb+icjWJc5KJGNJ49QHBeokszjGiEJZmxsSMTCF23I0ZDGhyjJZcaySTEjg58JqFbWPOcrlk/dbN73RbxVckcD2kLbT4AJ/7Oi8GoNoTeq+ZIww165zcneCjNFIWpLoh/59tGPn8LyPkPmshDbZdBOaZNkWBhVY1T5jMTt1Tzp2STjfPCCgWCYNwNv/PjSHOlvIAuW/7QdSU3xufPH+a79YLhPnt9l1Kdig5Qzi7pydiV6tycmq0DjGC2nKFNhNlLYW7RwtBDBS8bFNpcGJ0YfhYSeUzq7h0gdKNs5zcbxrWxxKMRApy3jveWXZPJm9iyj0yyJobIxZeJ+kDkYjRUc7gDo5Qgh3bJSKwZHOm9bx8F+WdkjV05LOyFtJXoBvDDPnucNnxC8x8oEAZhu8HrHlThW5oJkMU9dGgCG79nGh44/Jd+We0HzGd9ch27t33srHL35+M1HeiO5c3EKXG+kJGS8IssstSQPuiaupnyBuC6g3//YVSpDfFu0g4rRAMgiVlS6xbA4ft8FjztClSwRfIyYllcy+emh/dIMtle4X7ScVhMe+t1aDERa73HxofDuFkuJMdPj3wmS8Y+LRIMtHuHiMUBGXtnnR0yAwR8d44vkUPRkONoVBJjpl6Q5OcV3CCp7a7iomoNrXH5NU8uK3srD4TXaUcdC3qfZ9eF18K0Huc0fc14owphNWo39J/iC1xyz5nQ/ZRYHPRWteMGc1HgF97eqy7dY7Y2wWiOeJCHWZi5c+1YgG66H5xPiFbmF9N6sTIP6eJHPKXx5F/WVzi9IlRVPG+VN jNl2kOvb kpRw2RTgj0D66PYvOAnY9JRtDnf/KMuJGgVMOtW+pywYNIPyVEadEsPKCCy5KRo8KQ81gpJbGev1LTLaGv5cqOqVdawVpM7Z7SAMJkALKjWK8cUR55mDvGbbBIPmo6tEdLZLcCFTgK88sRfacoE3h0BVMYfzHVpif7OrtM/z3eov817g+BCaKpMWVSTZbpJlKGtcu4OyyHSI5aGAgcxeC1YCW4UWT3jtUKJUBk3xLLkZQaj0fLA1qLMRsGulMhrc/u+xy 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: To avoid unnecessarily taking a (brief) refcount on uprobe during breakpoint handling in handle_swbp for entry uprobes, make find_uprobe() not take refcount, but protect the lifetime of a uprobe instance with RCU. This improves scalability, as refcount gets quite expensive due to cache line bouncing between multiple CPUs. Specifically, we utilize our own uprobe-specific SRCU instance for this RCU protection. put_uprobe() will delay actual kfree() using call_srcu(). For now, uretprobe and single-stepping handling will still acquire refcount as necessary. We'll address these issues in follow up patches by making them use SRCU with timeout. Reviewed-by: Oleg Nesterov Signed-off-by: Andrii Nakryiko --- kernel/events/uprobes.c | 94 +++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 40 deletions(-) diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index 147561c19d57..3e3595753e2c 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -41,6 +41,8 @@ static struct rb_root uprobes_tree = RB_ROOT; static DEFINE_RWLOCK(uprobes_treelock); /* serialize rbtree access */ +DEFINE_STATIC_SRCU(uprobes_srcu); + #define UPROBES_HASH_SZ 13 /* serialize uprobe->pending_list */ static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ]; @@ -59,6 +61,7 @@ struct uprobe { struct list_head pending_list; struct uprobe_consumer *consumers; struct inode *inode; /* Also hold a ref to inode */ + struct rcu_head rcu; loff_t offset; loff_t ref_ctr_offset; unsigned long flags; @@ -617,6 +620,13 @@ static inline bool uprobe_is_active(struct uprobe *uprobe) return !RB_EMPTY_NODE(&uprobe->rb_node); } +static void uprobe_free_rcu(struct rcu_head *rcu) +{ + struct uprobe *uprobe = container_of(rcu, struct uprobe, rcu); + + kfree(uprobe); +} + static void put_uprobe(struct uprobe *uprobe) { if (!refcount_dec_and_test(&uprobe->ref)) @@ -638,7 +648,7 @@ static void put_uprobe(struct uprobe *uprobe) delayed_uprobe_remove(uprobe, NULL); mutex_unlock(&delayed_uprobe_lock); - kfree(uprobe); + call_srcu(&uprobes_srcu, &uprobe->rcu, uprobe_free_rcu); } static __always_inline @@ -680,33 +690,25 @@ static inline int __uprobe_cmp(struct rb_node *a, const struct rb_node *b) return uprobe_cmp(u->inode, u->offset, __node_2_uprobe(b)); } -static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset) +/* + * Assumes being inside RCU protected region. + * No refcount is taken on returned uprobe. + */ +static struct uprobe *find_uprobe_rcu(struct inode *inode, loff_t offset) { struct __uprobe_key key = { .inode = inode, .offset = offset, }; - struct rb_node *node = rb_find(&key, &uprobes_tree, __uprobe_cmp_key); - - if (node) - return try_get_uprobe(__node_2_uprobe(node)); - - return NULL; -} + struct rb_node *node; -/* - * Find a uprobe corresponding to a given inode:offset - * Acquires uprobes_treelock - */ -static struct uprobe *find_uprobe(struct inode *inode, loff_t offset) -{ - struct uprobe *uprobe; + lockdep_assert(srcu_read_lock_held(&uprobes_srcu)); read_lock(&uprobes_treelock); - uprobe = __find_uprobe(inode, offset); + node = rb_find(&key, &uprobes_tree, __uprobe_cmp_key); read_unlock(&uprobes_treelock); - return uprobe; + return node ? __node_2_uprobe(node) : NULL; } /* @@ -1080,10 +1082,10 @@ register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new) goto free; /* * We take mmap_lock for writing to avoid the race with - * find_active_uprobe() which takes mmap_lock for reading. + * find_active_uprobe_rcu() which takes mmap_lock for reading. * Thus this install_breakpoint() can not make - * is_trap_at_addr() true right after find_uprobe() - * returns NULL in find_active_uprobe(). + * is_trap_at_addr() true right after find_uprobe_rcu() + * returns NULL in find_active_uprobe_rcu(). */ mmap_write_lock(mm); vma = find_vma(mm, info->vaddr); @@ -1885,9 +1887,13 @@ static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs) return; } + /* we need to bump refcount to store uprobe in utask */ + if (!try_get_uprobe(uprobe)) + return; + ri = kmalloc(sizeof(struct return_instance), GFP_KERNEL); if (!ri) - return; + goto fail; trampoline_vaddr = uprobe_get_trampoline_vaddr(); orig_ret_vaddr = arch_uretprobe_hijack_return_addr(trampoline_vaddr, regs); @@ -1914,11 +1920,7 @@ static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs) } orig_ret_vaddr = utask->return_instances->orig_ret_vaddr; } - /* - * uprobe's refcnt is positive, held by caller, so it's safe to - * unconditionally bump it one more time here - */ - ri->uprobe = get_uprobe(uprobe); + ri->uprobe = uprobe; ri->func = instruction_pointer(regs); ri->stack = user_stack_pointer(regs); ri->orig_ret_vaddr = orig_ret_vaddr; @@ -1929,8 +1931,9 @@ static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs) utask->return_instances = ri; return; - fail: +fail: kfree(ri); + put_uprobe(uprobe); } /* Prepare to single-step probed instruction out of line. */ @@ -1945,9 +1948,14 @@ pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long bp_vaddr) if (!utask) return -ENOMEM; + if (!try_get_uprobe(uprobe)) + return -EINVAL; + xol_vaddr = xol_get_insn_slot(uprobe); - if (!xol_vaddr) - return -ENOMEM; + if (!xol_vaddr) { + err = -ENOMEM; + goto err_out; + } utask->xol_vaddr = xol_vaddr; utask->vaddr = bp_vaddr; @@ -1955,12 +1963,15 @@ pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long bp_vaddr) err = arch_uprobe_pre_xol(&uprobe->arch, regs); if (unlikely(err)) { xol_free_insn_slot(current); - return err; + goto err_out; } utask->active_uprobe = uprobe; utask->state = UTASK_SSTEP; return 0; +err_out: + put_uprobe(uprobe); + return err; } /* @@ -2043,7 +2054,8 @@ static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr) return is_trap_insn(&opcode); } -static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp) +/* assumes being inside RCU protected region */ +static struct uprobe *find_active_uprobe_rcu(unsigned long bp_vaddr, int *is_swbp) { struct mm_struct *mm = current->mm; struct uprobe *uprobe = NULL; @@ -2056,7 +2068,7 @@ static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp) struct inode *inode = file_inode(vma->vm_file); loff_t offset = vaddr_to_offset(vma, bp_vaddr); - uprobe = find_uprobe(inode, offset); + uprobe = find_uprobe_rcu(inode, offset); } if (!uprobe) @@ -2202,13 +2214,15 @@ static void handle_swbp(struct pt_regs *regs) { struct uprobe *uprobe; unsigned long bp_vaddr; - int is_swbp; + int is_swbp, srcu_idx; bp_vaddr = uprobe_get_swbp_addr(regs); if (bp_vaddr == uprobe_get_trampoline_vaddr()) return uprobe_handle_trampoline(regs); - uprobe = find_active_uprobe(bp_vaddr, &is_swbp); + srcu_idx = srcu_read_lock(&uprobes_srcu); + + uprobe = find_active_uprobe_rcu(bp_vaddr, &is_swbp); if (!uprobe) { if (is_swbp > 0) { /* No matching uprobe; signal SIGTRAP. */ @@ -2224,7 +2238,7 @@ static void handle_swbp(struct pt_regs *regs) */ instruction_pointer_set(regs, bp_vaddr); } - return; + goto out; } /* change it in advance for ->handler() and restart */ @@ -2259,12 +2273,12 @@ static void handle_swbp(struct pt_regs *regs) if (arch_uprobe_skip_sstep(&uprobe->arch, regs)) goto out; - if (!pre_ssout(uprobe, regs, bp_vaddr)) - return; + if (pre_ssout(uprobe, regs, bp_vaddr)) + goto out; - /* arch_uprobe_skip_sstep() succeeded, or restart if can't singlestep */ out: - put_uprobe(uprobe); + /* arch_uprobe_skip_sstep() succeeded, or restart if can't singlestep */ + srcu_read_unlock(&uprobes_srcu, srcu_idx); } /* From patchwork Tue Sep 3 17:45:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13789155 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 20827CD37AB for ; Tue, 3 Sep 2024 17:46:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A5B7E8D01BA; Tue, 3 Sep 2024 13:46:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A08588D018A; Tue, 3 Sep 2024 13:46:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8A9A78D01BA; Tue, 3 Sep 2024 13:46:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 6AC758D018A for ; Tue, 3 Sep 2024 13:46:21 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 25A2A406FC for ; Tue, 3 Sep 2024 17:46:21 +0000 (UTC) X-FDA: 82524156162.26.F7F70EB Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf10.hostedemail.com (Postfix) with ESMTP id 8E76FC0003 for ; Tue, 3 Sep 2024 17:46:19 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Q+gUUnua; spf=pass (imf10.hostedemail.com: domain of andrii@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725385485; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=rXPUR5SjxBRO5VypakjGvUPsO0VzGBE4bMsaAuiYApk=; b=5l/PjHbUt/Pg5xhVPRKeUhx4M4E2a4tXSNid7aVuwEn5GTifjIMkhcB3OBfUxX2gjLj81y ij5OarxbLTCVIuxoosLthWVtC+g8h1PPF/3tZN1nJ7VbQQzpnwfFh8GHqQS1wQpuhtgrko T4sHoQk9RD2RO5t25AYxj/m+VLNAmuE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725385485; a=rsa-sha256; cv=none; b=MBb0tz+KpyAtFBKSnzZqbun23slRaMqA+jXjPFMiaEEVl9fOfA7N8c28g775M9NOccwDca 6bPHa76eUM5McS6F7WXKn37t0sYXZUkPVQFDiHCTTsB9RPQUueQDzfROtiwq8h8hdF2cIW sjGe4ElJBIFwa+x0OejJ1zEzGx4Swa4= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Q+gUUnua; spf=pass (imf10.hostedemail.com: domain of andrii@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 60695A43D32; Tue, 3 Sep 2024 17:46:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B5688C4CEC9; Tue, 3 Sep 2024 17:46:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725385577; bh=N5+oyD36t1Y3uV0QDaXdkNDQIadUdSARy7H0PZ8GrZ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Q+gUUnuasOBgH12BDlSefX51CiByLaX7P6euE1PZPq1aZGeB6vcaM3sc9UbSboX9x oP48UCrq4njBF7u0GklrP3Q7+F+wnuYG1WEd2Q00a+j31EbwdMfMFRaNRbZBVn14a/ c3E/xaBKcD3WgV5kuNEfr2lJYi3ziXSHCtYK10D3RZHPRCtAZWJFH0W/KShzQKEvv9 HDVmAxHhMAc4z1UKeFeKgc0fh+l5LSVEEW3xksY4HWJsePbRLTO1QFls/psiMDsdlO KWKgudcy3RhL3APUtIJ/WnLHjT60YzazSUo9/mOSoJgdzdKNLx7kJDSSWpWxfgVdoO McX/FLF4C/q+Q== From: Andrii Nakryiko To: linux-trace-kernel@vger.kernel.org, peterz@infradead.org, oleg@redhat.com Cc: rostedt@goodmis.org, mhiramat@kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, jolsa@kernel.org, paulmck@kernel.org, willy@infradead.org, surenb@google.com, akpm@linux-foundation.org, linux-mm@kvack.org, Andrii Nakryiko Subject: [PATCH v5 3/8] uprobes: get rid of enum uprobe_filter_ctx in uprobe filter callbacks Date: Tue, 3 Sep 2024 10:45:58 -0700 Message-ID: <20240903174603.3554182-4-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240903174603.3554182-1-andrii@kernel.org> References: <20240903174603.3554182-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 8E76FC0003 X-Stat-Signature: s8icdbpfoi13bqkeyahs5tefd8rb4ywt X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1725385579-304794 X-HE-Meta: U2FsdGVkX19q/tHH2XparoZgUwSDcqL/2t/KEL4Zq7+Ze+mDTUleZMCLjgA6/wMtJUCKgFabMUSGnVZalWksUxoVGbK3zUzH9bK835MQezWQxPf3f2C64j5Khr6ccPZj+eyxMsArYX4bdCFBmZxQ7YFgF8RTNLFI0yxX5gyKxOg1rfo5EVm7Q/Ag3C05LHu8X7tWAGlm9PlXqa8eYceaEM/kpaYJ0anKgDgiMDV1KD0rB4jBe2zNZP4aU0vPZrbyMdKGIJwQv53c0/BIkahpcx1rwFZZp7nQybNUvnOqLO0DxYAT1sBopRonLsoHaqUZW7p0VzqUXKMnG7iEY9zCLEugKx6GD9eYTptqRqDRam0Yvkr3rYzRAMXOidRFcVUT6FxcSftiGzCqhqAGIHCSN1nlrbYq+SCaTsgVaz9jgexDFrm0HcdmmWB7oF3+MYpjYsMwqphidKMCOypFYzdr6bHp9wmk8UqW2nvGh1VtiU4E9lDgxVFT9p8P8Qst8FNlur662ge6ZNNdh5970WiyseKsXSmcWtKWeS/oi0dX+FCTwn/x8mUKUsD5J6WnLywFsHeZKcsnEIvKaXDPmgm0gipu1dx6iTky+l2erSpESlFa1jcg4Bpa0q3B8PCmT+2PM+Yupn2zsV1b4UoysJ6Q8ROgHPUckB3frzurENsThK3USpXSAzEwy1N2xOTGiri2ADzzC+Xkxk45R+oKySrxIBaKsPE9cUpA9sQrfFkWPuzao7b8t3rU2IWGmuwHCfxhtDjrAyGiaEvJhw7jQZXdKB7hnoI93Ta6+Q/hxmWOrLy0YzuKaBEkoZ9QEc7G8WAahG1pZWJnrWQz/YsEIHGEIZ2xVRgdyVWWK8UhhOJ6JdrIpX8JUAfAOiFmz5s9UCn1K7dWcSBJJEa6BpwHtIcOYPpO2kBXeIrAGtEX8stH6RNtEsONIzFRkIwTEtjD5S0dMrYO4xf0Zb8lPIY3Crk uskd77QL j2L83LZDE0NZusych/jyjapjeeVaudnH5Rjzn05PAP3RpbUbYpT1iZNQmgw9EbEHrD0QSx2B76rmVhh1KRW5rFI2Xr3VnVV3oRyfo+IV7nrDArNn/g0yEFaVHqSXBceA0PYFboQaZONFudeqknfdqGST2VwAPZbTAt/LmHSbm0b06W4egpBD8KSY7jDEPOIfAknfAobDPTNj8AICxaqIrhQ9/T6FwjiKhIcXCNObs7ny9Q0D8Wvnx6bwDANmm7/HwB1VWW/NZxwQoF6ZVcs7ivCMenpdNWE9yC9LUDJuRVBCulubnCfXlWZ45Lw== 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: It serves no purpose beyond adding unnecessray argument passed to the filter callback. Just get rid of it, no one is actually using it. Reviewed-by: Oleg Nesterov Signed-off-by: Andrii Nakryiko --- include/linux/uprobes.h | 10 +--------- kernel/events/uprobes.c | 18 +++++++----------- kernel/trace/bpf_trace.c | 3 +-- kernel/trace/trace_uprobe.c | 9 +++------ 4 files changed, 12 insertions(+), 28 deletions(-) diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h index 6332c111036e..9cf0dce62e4c 100644 --- a/include/linux/uprobes.h +++ b/include/linux/uprobes.h @@ -28,20 +28,12 @@ struct page; #define MAX_URETPROBE_DEPTH 64 -enum uprobe_filter_ctx { - UPROBE_FILTER_REGISTER, - UPROBE_FILTER_UNREGISTER, - UPROBE_FILTER_MMAP, -}; - struct uprobe_consumer { int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs); int (*ret_handler)(struct uprobe_consumer *self, unsigned long func, struct pt_regs *regs); - bool (*filter)(struct uprobe_consumer *self, - enum uprobe_filter_ctx ctx, - struct mm_struct *mm); + bool (*filter)(struct uprobe_consumer *self, struct mm_struct *mm); struct uprobe_consumer *next; }; diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index 3e3595753e2c..8bdcdc6901b2 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -918,21 +918,19 @@ static int prepare_uprobe(struct uprobe *uprobe, struct file *file, return ret; } -static inline bool consumer_filter(struct uprobe_consumer *uc, - enum uprobe_filter_ctx ctx, struct mm_struct *mm) +static inline bool consumer_filter(struct uprobe_consumer *uc, struct mm_struct *mm) { - return !uc->filter || uc->filter(uc, ctx, mm); + return !uc->filter || uc->filter(uc, mm); } -static bool filter_chain(struct uprobe *uprobe, - enum uprobe_filter_ctx ctx, struct mm_struct *mm) +static bool filter_chain(struct uprobe *uprobe, struct mm_struct *mm) { struct uprobe_consumer *uc; bool ret = false; down_read(&uprobe->consumer_rwsem); for (uc = uprobe->consumers; uc; uc = uc->next) { - ret = consumer_filter(uc, ctx, mm); + ret = consumer_filter(uc, mm); if (ret) break; } @@ -1099,12 +1097,10 @@ register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new) if (is_register) { /* consult only the "caller", new consumer. */ - if (consumer_filter(new, - UPROBE_FILTER_REGISTER, mm)) + if (consumer_filter(new, mm)) err = install_breakpoint(uprobe, mm, vma, info->vaddr); } else if (test_bit(MMF_HAS_UPROBES, &mm->flags)) { - if (!filter_chain(uprobe, - UPROBE_FILTER_UNREGISTER, mm)) + if (!filter_chain(uprobe, mm)) err |= remove_breakpoint(uprobe, mm, info->vaddr); } @@ -1387,7 +1383,7 @@ int uprobe_mmap(struct vm_area_struct *vma) */ list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) { if (!fatal_signal_pending(current) && - filter_chain(uprobe, UPROBE_FILTER_MMAP, vma->vm_mm)) { + filter_chain(uprobe, vma->vm_mm)) { unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset); install_breakpoint(uprobe, vma->vm_mm, vma, vaddr); } diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 4e391daafa64..73c570b5988b 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -3320,8 +3320,7 @@ static int uprobe_prog_run(struct bpf_uprobe *uprobe, } static bool -uprobe_multi_link_filter(struct uprobe_consumer *con, enum uprobe_filter_ctx ctx, - struct mm_struct *mm) +uprobe_multi_link_filter(struct uprobe_consumer *con, struct mm_struct *mm) { struct bpf_uprobe *uprobe; diff --git a/kernel/trace/trace_uprobe.c b/kernel/trace/trace_uprobe.c index 52e76a73fa7c..7eb79e0a5352 100644 --- a/kernel/trace/trace_uprobe.c +++ b/kernel/trace/trace_uprobe.c @@ -1078,9 +1078,7 @@ print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *e return trace_handle_return(s); } -typedef bool (*filter_func_t)(struct uprobe_consumer *self, - enum uprobe_filter_ctx ctx, - struct mm_struct *mm); +typedef bool (*filter_func_t)(struct uprobe_consumer *self, struct mm_struct *mm); static int trace_uprobe_enable(struct trace_uprobe *tu, filter_func_t filter) { @@ -1339,8 +1337,7 @@ static int uprobe_perf_open(struct trace_event_call *call, return err; } -static bool uprobe_perf_filter(struct uprobe_consumer *uc, - enum uprobe_filter_ctx ctx, struct mm_struct *mm) +static bool uprobe_perf_filter(struct uprobe_consumer *uc, struct mm_struct *mm) { struct trace_uprobe_filter *filter; struct trace_uprobe *tu; @@ -1426,7 +1423,7 @@ static void __uprobe_perf_func(struct trace_uprobe *tu, static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs, struct uprobe_cpu_buffer **ucbp) { - if (!uprobe_perf_filter(&tu->consumer, 0, current->mm)) + if (!uprobe_perf_filter(&tu->consumer, current->mm)) return UPROBE_HANDLER_REMOVE; if (!is_ret_probe(tu)) From patchwork Tue Sep 3 17:45:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13789156 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 606CACD37AB for ; Tue, 3 Sep 2024 17:46:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E2E018D01BB; Tue, 3 Sep 2024 13:46:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DDA238D018A; Tue, 3 Sep 2024 13:46:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C7B468D01BB; Tue, 3 Sep 2024 13:46:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A5FFE8D018A for ; Tue, 3 Sep 2024 13:46:24 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 6B0F4807B2 for ; Tue, 3 Sep 2024 17:46:24 +0000 (UTC) X-FDA: 82524156288.17.CB1F2CA Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf18.hostedemail.com (Postfix) with ESMTP id C3C871C001D for ; Tue, 3 Sep 2024 17:46:22 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=dyMZEzMe; spf=pass (imf18.hostedemail.com: domain of andrii@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725385488; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=BhLJvULcEkpbElvX9A6UlZ/PB9VxlnTjBvkf6vUWgkY=; b=KgOwUoe9mKpz5MjRw7H6gZJJebJlErMuRJNDnWOKTMj0yaSs4MbL6MP7tkw5MyVmKqgjOB kI3XJ1JeryHkapgLGFG7Qxi9WMV/a/MBRp3BszFEpPZYO4D9K3XS1XN4PItVKovSs/LoHG Q6d3j9Z+QKQwyAYhjj/X1zF/ZhE9xxQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725385488; a=rsa-sha256; cv=none; b=7iKV65MlOcs2OnWMDFPigT3ikykEKsA4SQ1W9FAuM4/MXN03SjkzrlK2E33BuhrfJPTsvC t4I07+xB9DkgBvsjssS0UghfXuYkFHpVeoP4SrXLToUlk6nWqkyTYKXaOt0slvkgF1xrHe MSQEFreQkn4YNv91Tk081P25j7qSRu4= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=dyMZEzMe; spf=pass (imf18.hostedemail.com: domain of andrii@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 4C061A43D33; Tue, 3 Sep 2024 17:46:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E96DCC4CEC6; Tue, 3 Sep 2024 17:46:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725385581; bh=Od2hK7f+p9OrwoA3Etz1Ku5j7OT1LnL7qqTaMaUPv3M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dyMZEzMek0Ld2KoDhCgr9p7DhtmlN1ri51XVG/IozrLDGoLIQYhnEjcivG9aWHQF6 NN0wRc1HbxeLkoueLBgPSZYFhgiz5Z9bV6GN3vodOwHjAhh6Lpcv8mSxkoLv66ywYg YfYle/qNJBPZ/wiABePhe8WWeXsZGMdX9szPW1u6oZw06MFkf4bZEb/580LlmtZURp fkUdYci5Bvf+2jnoR2FbpL8KtMSYjWM5MagMwYc0ZrJg5dMYY9ZcMU0z91nNc6kjjw ZP4LAKWEBiAa2B5bbfQd8hLZNt2losVQ/ySslU/QmGtKC1S2MIuHVksmmViBRynSLt w/7/QfGnSlh6g== From: Andrii Nakryiko To: linux-trace-kernel@vger.kernel.org, peterz@infradead.org, oleg@redhat.com Cc: rostedt@goodmis.org, mhiramat@kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, jolsa@kernel.org, paulmck@kernel.org, willy@infradead.org, surenb@google.com, akpm@linux-foundation.org, linux-mm@kvack.org, Andrii Nakryiko Subject: [PATCH v5 4/8] uprobes: travers uprobe's consumer list locklessly under SRCU protection Date: Tue, 3 Sep 2024 10:45:59 -0700 Message-ID: <20240903174603.3554182-5-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240903174603.3554182-1-andrii@kernel.org> References: <20240903174603.3554182-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: C3C871C001D X-Stat-Signature: hu44yxgwe9wgy9sq1p3c6w8u81jrrrcs X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1725385582-18634 X-HE-Meta: U2FsdGVkX1/nVqtqbjNsLML6dWRrdExUJDpXsyhsEMbrf3ACio2Tg3xj1fcQzxp/o0qyVUaSZnyTVeOrALncPt2K3qha/mQ8qrTsIfz0Lc6TwYLMAxUVMYacZ79+WEUMcl2Iml1citL3+HYld1ouWkBmVAl6FFftPbNdryvYK1n+wB7iprWMd8fXofZKr4G/yBROLoy1rfnTbLOoq+c3HklZTx6W0ZSMSGwHBVF9SPc0mhd38GhfYXvFeK1IAiegbwfdZ+SS6z5HVfW7ANc8xhS0VS8YWW/VpzH7P8oDp42Qy8zv6gkJJmAL+0sFdqvux/u7vF1oEdqeS4m+QbJSKLKwn9NhSooSf1ebNZD58RVXOVYMYyhTdIKKXrV14ntw+10gCCd6LtiGH1lXpH+dqLj34bY/T2EXDxS0Pl+BHnUZsmIUpRV7jHmGyJohjyf9/YUcjLckvT/b7BRqG+irWtonI+s4/T8dSuoxQCghdKKNYVzzDPTAUCoUIRPM3CoFzr9pIn9dJGR8C3CshJ1UuEcUC05Svf+UUSeaAPcPU0WWXOLjqZtztlo7PB4W/d5o2NJYh6cJjOfwsDYp8QepXqaysw/ywOukY9rEti3t5D5lNAWF38FJop6ZJbJc/mZGOHn4soxtfwCgkEZwPKg8PhFeCGi3etGPZiElIzsFXoo/w1VqxCazPDS+V0s47oVYySvM2lrpxTIYiHGDDD623B3cojc7AgjUuW9szMGG0KepZfL/WvRC9CkysXrrVIQdtZWWugW+A+VM5FuV44cFpohdZbIx0QHZG1Z76VOczBetlkF2s49wr3p4SdsrZHjCHvkcOjyMjMXy/19KmSAuqfV0kkpIrVwFR11KREpZJP8hooCZOoXxOVbS3uc83GTlYODihHL2YyhPGrLKU3gSsvBAIvTS5Tweydajdy2uvuuComA/+91/k5QY5CEtm7Ybr4HPCAjVTvJ+m7FZvRF Q1RsKHjA XU9b3PtbYVZQDpKMeRKCaz8X4WSr2JC6kt1C2oh6gFWV3MVoN7JREjc95y4hsU/ZAPBgjQltaIij5n/Wq0SWiSncM4ha4NokXaRUnfGa5KF4Ks7RHFC0yl8ymyfSUb/7sjkI5uhphMXKpXxwQsFBph4nVCSWwvb6/13KQW8ecH3rxzsawdbBdLWsppEGpENrdhN9NOczbmOUP85N7FsEhspPeimbV62qAWpiSILtNYq7Wm1nlUS8akz/NKqs7GJlKTl/O 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: uprobe->register_rwsem is one of a few big bottlenecks to scalability of uprobes, so we need to get rid of it to improve uprobe performance and multi-CPU scalability. First, we turn uprobe's consumer list to a typical doubly-linked list and utilize existing RCU-aware helpers for traversing such lists, as well as adding and removing elements from it. For entry uprobes we already have SRCU protection active since before uprobe lookup. For uretprobe we keep refcount, guaranteeing that uprobe won't go away from under us, but we add SRCU protection around consumer list traversal. Lastly, to keep handler_chain()'s UPROBE_HANDLER_REMOVE handling simple, we remember whether any removal was requested during handler calls, but then we double-check the decision under a proper register_rwsem using consumers' filter callbacks. Handler removal is very rare, so this extra lock won't hurt performance, overall, but we also avoid the need for any extra protection (e.g., seqcount locks). Reviewed-by: Oleg Nesterov Signed-off-by: Andrii Nakryiko --- include/linux/uprobes.h | 10 +++- kernel/events/uprobes.c | 104 +++++++++++++++++++++++----------------- 2 files changed, 70 insertions(+), 44 deletions(-) diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h index 9cf0dce62e4c..2785d1bedb74 100644 --- a/include/linux/uprobes.h +++ b/include/linux/uprobes.h @@ -29,13 +29,21 @@ struct page; #define MAX_URETPROBE_DEPTH 64 struct uprobe_consumer { + /* + * handler() can return UPROBE_HANDLER_REMOVE to signal the need to + * unregister uprobe for current process. If UPROBE_HANDLER_REMOVE is + * returned, filter() callback has to be implemented as well and it + * should return false to "confirm" the decision to uninstall uprobe + * for the current process. If filter() is omitted or returns true, + * UPROBE_HANDLER_REMOVE is effectively ignored. + */ int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs); int (*ret_handler)(struct uprobe_consumer *self, unsigned long func, struct pt_regs *regs); bool (*filter)(struct uprobe_consumer *self, struct mm_struct *mm); - struct uprobe_consumer *next; + struct list_head cons_node; }; #ifdef CONFIG_UPROBES diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index 8bdcdc6901b2..97e58d160647 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -59,7 +59,7 @@ struct uprobe { struct rw_semaphore register_rwsem; struct rw_semaphore consumer_rwsem; struct list_head pending_list; - struct uprobe_consumer *consumers; + struct list_head consumers; struct inode *inode; /* Also hold a ref to inode */ struct rcu_head rcu; loff_t offset; @@ -783,6 +783,7 @@ static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset, uprobe->inode = inode; uprobe->offset = offset; uprobe->ref_ctr_offset = ref_ctr_offset; + INIT_LIST_HEAD(&uprobe->consumers); init_rwsem(&uprobe->register_rwsem); init_rwsem(&uprobe->consumer_rwsem); RB_CLEAR_NODE(&uprobe->rb_node); @@ -808,32 +809,19 @@ static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset, static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc) { down_write(&uprobe->consumer_rwsem); - uc->next = uprobe->consumers; - uprobe->consumers = uc; + list_add_rcu(&uc->cons_node, &uprobe->consumers); up_write(&uprobe->consumer_rwsem); } /* * For uprobe @uprobe, delete the consumer @uc. - * Return true if the @uc is deleted successfully - * or return false. + * Should never be called with consumer that's not part of @uprobe->consumers. */ -static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc) +static void consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc) { - struct uprobe_consumer **con; - bool ret = false; - down_write(&uprobe->consumer_rwsem); - for (con = &uprobe->consumers; *con; con = &(*con)->next) { - if (*con == uc) { - *con = uc->next; - ret = true; - break; - } - } + list_del_rcu(&uc->cons_node); up_write(&uprobe->consumer_rwsem); - - return ret; } static int __copy_insn(struct address_space *mapping, struct file *filp, @@ -929,7 +917,8 @@ static bool filter_chain(struct uprobe *uprobe, struct mm_struct *mm) bool ret = false; down_read(&uprobe->consumer_rwsem); - for (uc = uprobe->consumers; uc; uc = uc->next) { + list_for_each_entry_srcu(uc, &uprobe->consumers, cons_node, + srcu_read_lock_held(&uprobes_srcu)) { ret = consumer_filter(uc, mm); if (ret) break; @@ -1125,18 +1114,29 @@ void uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc) int err; down_write(&uprobe->register_rwsem); - if (WARN_ON(!consumer_del(uprobe, uc))) { - err = -ENOENT; - } else { - err = register_for_each_vma(uprobe, NULL); - /* TODO : cant unregister? schedule a worker thread */ - if (unlikely(err)) - uprobe_warn(current, "unregister, leaking uprobe"); - } + consumer_del(uprobe, uc); + err = register_for_each_vma(uprobe, NULL); up_write(&uprobe->register_rwsem); - if (!err) - put_uprobe(uprobe); + /* TODO : cant unregister? schedule a worker thread */ + if (unlikely(err)) { + uprobe_warn(current, "unregister, leaking uprobe"); + goto out_sync; + } + + put_uprobe(uprobe); + +out_sync: + /* + * Now that handler_chain() and handle_uretprobe_chain() iterate over + * uprobe->consumers list under RCU protection without holding + * uprobe->register_rwsem, we need to wait for RCU grace period to + * make sure that we can't call into just unregistered + * uprobe_consumer's callbacks anymore. If we don't do that, fast and + * unlucky enough caller can free consumer's memory and cause + * handler_chain() or handle_uretprobe_chain() to do an use-after-free. + */ + synchronize_srcu(&uprobes_srcu); } EXPORT_SYMBOL_GPL(uprobe_unregister); @@ -1214,13 +1214,20 @@ EXPORT_SYMBOL_GPL(uprobe_register); int uprobe_apply(struct uprobe *uprobe, struct uprobe_consumer *uc, bool add) { struct uprobe_consumer *con; - int ret = -ENOENT; + int ret = -ENOENT, srcu_idx; down_write(&uprobe->register_rwsem); - for (con = uprobe->consumers; con && con != uc ; con = con->next) - ; - if (con) - ret = register_for_each_vma(uprobe, add ? uc : NULL); + + srcu_idx = srcu_read_lock(&uprobes_srcu); + list_for_each_entry_srcu(con, &uprobe->consumers, cons_node, + srcu_read_lock_held(&uprobes_srcu)) { + if (con == uc) { + ret = register_for_each_vma(uprobe, add ? uc : NULL); + break; + } + } + srcu_read_unlock(&uprobes_srcu, srcu_idx); + up_write(&uprobe->register_rwsem); return ret; @@ -2085,10 +2092,12 @@ static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs) struct uprobe_consumer *uc; int remove = UPROBE_HANDLER_REMOVE; bool need_prep = false; /* prepare return uprobe, when needed */ + bool has_consumers = false; - down_read(&uprobe->register_rwsem); current->utask->auprobe = &uprobe->arch; - for (uc = uprobe->consumers; uc; uc = uc->next) { + + list_for_each_entry_srcu(uc, &uprobe->consumers, cons_node, + srcu_read_lock_held(&uprobes_srcu)) { int rc = 0; if (uc->handler) { @@ -2101,17 +2110,24 @@ static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs) need_prep = true; remove &= rc; + has_consumers = true; } current->utask->auprobe = NULL; if (need_prep && !remove) prepare_uretprobe(uprobe, regs); /* put bp at return */ - if (remove && uprobe->consumers) { - WARN_ON(!uprobe_is_active(uprobe)); - unapply_uprobe(uprobe, current->mm); + if (remove && has_consumers) { + down_read(&uprobe->register_rwsem); + + /* re-check that removal is still required, this time under lock */ + if (!filter_chain(uprobe, current->mm)) { + WARN_ON(!uprobe_is_active(uprobe)); + unapply_uprobe(uprobe, current->mm); + } + + up_read(&uprobe->register_rwsem); } - up_read(&uprobe->register_rwsem); } static void @@ -2119,13 +2135,15 @@ handle_uretprobe_chain(struct return_instance *ri, struct pt_regs *regs) { struct uprobe *uprobe = ri->uprobe; struct uprobe_consumer *uc; + int srcu_idx; - down_read(&uprobe->register_rwsem); - for (uc = uprobe->consumers; uc; uc = uc->next) { + srcu_idx = srcu_read_lock(&uprobes_srcu); + list_for_each_entry_srcu(uc, &uprobe->consumers, cons_node, + srcu_read_lock_held(&uprobes_srcu)) { if (uc->ret_handler) uc->ret_handler(uc, ri->func, regs); } - up_read(&uprobe->register_rwsem); + srcu_read_unlock(&uprobes_srcu, srcu_idx); } static struct return_instance *find_next_ret_chain(struct return_instance *ri) From patchwork Tue Sep 3 17:46:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13789157 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 12580CD37AA for ; Tue, 3 Sep 2024 17:46:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9E30D8D01BC; Tue, 3 Sep 2024 13:46:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9459F8D018A; Tue, 3 Sep 2024 13:46:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 749F28D01BC; Tue, 3 Sep 2024 13:46:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 565A28D018A for ; Tue, 3 Sep 2024 13:46:27 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 0D14DC05B7 for ; Tue, 3 Sep 2024 17:46:27 +0000 (UTC) X-FDA: 82524156414.21.D710F44 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf14.hostedemail.com (Postfix) with ESMTP id 6FC26100004 for ; Tue, 3 Sep 2024 17:46:25 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Qhx6yfQQ; spf=pass (imf14.hostedemail.com: domain of andrii@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725385537; a=rsa-sha256; cv=none; b=IW6I1WD4bZIqV6ddOCeEEvGoenXgyBwn/80uAy//etdmaHhSgCIQI2/2j6gbOioUNP7stV sOL/oQAnXOx11dF1qPcoDDKJ3JJUHtG+h+ZIXx/pn1OgAkmJUg8HPAYl6tqaDGBw/1MDQO 7gTpMMRLwVYCl1LQ9Va6BVjPm8ZVjjs= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Qhx6yfQQ; spf=pass (imf14.hostedemail.com: domain of andrii@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725385537; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=VlhBoxOuJqa5LFjF8se4V8UVnSnNObXK47ZlFyLkVWE=; b=fTDZZjoJMLRYwhFH+NZKd2wf82vcXnDOmIvFfTkNnCHw0VvIVELWPrOovS6tUFSwwdleqt Z88pLQ2sBLsYaKJJExFuJCntF/H5Y1tqm16dQinMo45TA2WH8uBy4agHQ9Q/SFRQibsolp FU+ZI5SnkT65bQx/h/lFJffDa0wnv7o= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 800F2A43D32; Tue, 3 Sep 2024 17:46:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3520AC4CEC4; Tue, 3 Sep 2024 17:46:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725385584; bh=+g5blMcMvR/SzeXxNsO+2FR1B0Xb/4hXSLWFzum/rNc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Qhx6yfQQFzXxpHP3LO+Fd1jKiUa4JhPuiOsfHedhqXJjYdmYOMAFnnX0y0a3/BRq/ zau5ZeAS43Xbm3KxfmouEia1A2VI7ZAacfnXnGy2G/qSuGT3ISARwoujzhXSfze6iN Ob9hpzh5HONT6NCQ3pjUef3Y8joaErhmOh4R3/xZleSPb44UYga9B6QwejU9EmNibZ whpmKRFcgTiO5x5NK49n22lMdZOz/WzO+14nZyR5/uWmmvH4GI2kLb9mfR2Dcxa8/a ePAX0kEJML3yYA+dDB8kYj65pehwax5J3lyQXqe7o0/VZOTFnL1D3bm0T6IB+AEd9g h3SuCX8DXCvvA== From: Andrii Nakryiko To: linux-trace-kernel@vger.kernel.org, peterz@infradead.org, oleg@redhat.com Cc: rostedt@goodmis.org, mhiramat@kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, jolsa@kernel.org, paulmck@kernel.org, willy@infradead.org, surenb@google.com, akpm@linux-foundation.org, linux-mm@kvack.org, Andrii Nakryiko Subject: [PATCH v5 5/8] perf/uprobe: split uprobe_unregister() Date: Tue, 3 Sep 2024 10:46:00 -0700 Message-ID: <20240903174603.3554182-6-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240903174603.3554182-1-andrii@kernel.org> References: <20240903174603.3554182-1-andrii@kernel.org> MIME-Version: 1.0 X-Stat-Signature: 18ogt5bskmgdmc6ummkpn45b68547edg X-Rspamd-Queue-Id: 6FC26100004 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1725385585-635439 X-HE-Meta: U2FsdGVkX1/clDtljA+tpeAEi4SAC0C93TlxYPkdVg9RqRom3UkVJ/4E1JrmMWHjTyeBa9dKnlWJLD+d3nQuW+5MrBl9TPQMJDgWeBRn9qswpqVTy1JWVVsgHlqVqlEq0zWx2SMN1KiOkLAdR88Xng11Ah04T5cVLVU7N43zv9vT4BAwHF1X7cgJwL+BG1RSHsCRAZFcwPAGcZwxWTt7ODz0izepKuccAJAe45Yc+xwVTDG22Rlq25sdhPqWDIQEWzbY4LsjlMeEtaRHju3o5C+Cx7MtKmqacc8OLVCFObQLKVgu59VNcl3SzDRxAaIEksEKalEFUt+gc9wBKpDW3Gy8aUqrWuFBH+KG6hC34kzC+kX7IF+kDSUMqWJYppud0MOHy4w2zwU4ac/FGUsZsu3C7oJPQPRAdmpQWDaC+KzmVen7rTk65ESPJuiCkqn3ZhtG+hAdlcB0pQWVlx6sJvLZNMC+gaTRCeFUcfzmlmh1K55PvthpadiwIm0SVBB+h6IDf+uTMM/SH2MzWnT9pL/QIlA0kx3e62tRpPuPrBuQ7MA5WLVDT69nsqNI79ryIHqzX0Dc1D4//xStm+YPMChB81TlVU0lUxPGXslIBCHLICoR/XmLn2au7yhDIUnbOeNVMNkKwtiF/U8guWgSHseRYzyz2PKNJWhQKCfdO5BQRoL3slKrlApH2V9PhQ2cl/4dr66ibnqepSIh36HCzoJELPbJsdZOTomSm6GOE26bPdJqYGtAuIvUa8hNMGwoznQd+P/dwSAK7j907+YE8xPtTl4S7kBVCDHbDMoSj7aoafXFPLMsR839GmHIs6M9ZCez2NQCDr8vLo+RrKe+Dyjx8fpc4i0isu7f1TpCcH2lESUG6PBltfIblw2Se19ZZOfaKuDFx+5OYDy79mrvflxdist+lQtxx3Rw2k2vEswbbk89C2jcYhq4CkmlFGzgpd+dQn97lgSvKHUPTAW aSE8cOir Dhf+S06YcLtYeUsyRgZ5yTn61x5M1U0gTsUGmBgDiARKPMqvb1ySCBKtgZ0GK3QySRUsbbDcjPsPtcTFMMfnqK9uyXMk5oxmxoab+RrSwCH+7myO7CNxgF8dTArSTu3EYax7AcVZPwb+nNHAtTzqHArLZJxUc9vD2Zb84Evb5EbNJwwgKOrY27bd7gvPlNbp+pnRwbzYT4HuHHJpyaDq1RZzPGdWH1lS5L4zXLj7LKvOtNfAXZZuNhP/p55RjDCrIPc3bQVwaCo6QXddxSCh8Cqh5KX3k7OF3CYD/viuyExgPD+KNO2PA6AUzUS+2/ggeabCtIz2+d1r0WFbeJ7hmSXjFTlTrfKwrNx1QFVacK9hm7DM= 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: From: Peter Zijlstra With uprobe_unregister() having grown a synchronize_srcu(), it becomes fairly slow to call. Esp. since both users of this API call it in a loop. Peel off the sync_srcu() and do it once, after the loop. We also need to add uprobe_unregister_sync() into uprobe_register()'s error handling path, as we need to be careful about returning to the caller before we have a guarantee that partially attached consumer won't be called anymore. This is an unlikely slow path and this should be totally fine to be slow in the case of a failed attach. Reviewed-by: Oleg Nesterov Signed-off-by: Peter Zijlstra (Intel) Co-developed-by: Andrii Nakryiko Signed-off-by: Andrii Nakryiko --- include/linux/uprobes.h | 8 +++++-- kernel/events/uprobes.c | 21 +++++++++++++------ kernel/trace/bpf_trace.c | 5 ++++- kernel/trace/trace_uprobe.c | 6 +++++- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 3 ++- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h index 2785d1bedb74..968230d81798 100644 --- a/include/linux/uprobes.h +++ b/include/linux/uprobes.h @@ -116,7 +116,8 @@ extern unsigned long uprobe_get_trap_addr(struct pt_regs *regs); extern int uprobe_write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr, uprobe_opcode_t); extern struct uprobe *uprobe_register(struct inode *inode, loff_t offset, loff_t ref_ctr_offset, struct uprobe_consumer *uc); extern int uprobe_apply(struct uprobe *uprobe, struct uprobe_consumer *uc, bool); -extern void uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc); +extern void uprobe_unregister_nosync(struct uprobe *uprobe, struct uprobe_consumer *uc); +extern void uprobe_unregister_sync(void); extern int uprobe_mmap(struct vm_area_struct *vma); extern void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end); extern void uprobe_start_dup_mmap(void); @@ -165,7 +166,10 @@ uprobe_apply(struct uprobe* uprobe, struct uprobe_consumer *uc, bool add) return -ENOSYS; } static inline void -uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc) +uprobe_unregister_nosync(struct uprobe *uprobe, struct uprobe_consumer *uc) +{ +} +static inline void uprobe_unregister_sync(void) { } static inline int uprobe_mmap(struct vm_area_struct *vma) diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index 97e58d160647..e9b755ddf960 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -1105,11 +1105,11 @@ register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new) } /** - * uprobe_unregister - unregister an already registered probe. + * uprobe_unregister_nosync - unregister an already registered probe. * @uprobe: uprobe to remove * @uc: identify which probe if multiple probes are colocated. */ -void uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc) +void uprobe_unregister_nosync(struct uprobe *uprobe, struct uprobe_consumer *uc) { int err; @@ -1121,12 +1121,15 @@ void uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc) /* TODO : cant unregister? schedule a worker thread */ if (unlikely(err)) { uprobe_warn(current, "unregister, leaking uprobe"); - goto out_sync; + return; } put_uprobe(uprobe); +} +EXPORT_SYMBOL_GPL(uprobe_unregister_nosync); -out_sync: +void uprobe_unregister_sync(void) +{ /* * Now that handler_chain() and handle_uretprobe_chain() iterate over * uprobe->consumers list under RCU protection without holding @@ -1138,7 +1141,7 @@ void uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc) */ synchronize_srcu(&uprobes_srcu); } -EXPORT_SYMBOL_GPL(uprobe_unregister); +EXPORT_SYMBOL_GPL(uprobe_unregister_sync); /** * uprobe_register - register a probe @@ -1196,7 +1199,13 @@ struct uprobe *uprobe_register(struct inode *inode, up_write(&uprobe->register_rwsem); if (ret) { - uprobe_unregister(uprobe, uc); + uprobe_unregister_nosync(uprobe, uc); + /* + * Registration might have partially succeeded, so we can have + * this consumer being called right at this time. We need to + * sync here. It's ok, it's unlikely slow path. + */ + uprobe_unregister_sync(); return ERR_PTR(ret); } diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 73c570b5988b..6b632710c98e 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -3184,7 +3184,10 @@ static void bpf_uprobe_unregister(struct bpf_uprobe *uprobes, u32 cnt) u32 i; for (i = 0; i < cnt; i++) - uprobe_unregister(uprobes[i].uprobe, &uprobes[i].consumer); + uprobe_unregister_nosync(uprobes[i].uprobe, &uprobes[i].consumer); + + if (cnt) + uprobe_unregister_sync(); } static void bpf_uprobe_multi_link_release(struct bpf_link *link) diff --git a/kernel/trace/trace_uprobe.c b/kernel/trace/trace_uprobe.c index 7eb79e0a5352..f7443e996b1b 100644 --- a/kernel/trace/trace_uprobe.c +++ b/kernel/trace/trace_uprobe.c @@ -1097,6 +1097,7 @@ static int trace_uprobe_enable(struct trace_uprobe *tu, filter_func_t filter) static void __probe_event_disable(struct trace_probe *tp) { struct trace_uprobe *tu; + bool sync = false; tu = container_of(tp, struct trace_uprobe, tp); WARN_ON(!uprobe_filter_is_empty(tu->tp.event->filter)); @@ -1105,9 +1106,12 @@ static void __probe_event_disable(struct trace_probe *tp) if (!tu->uprobe) continue; - uprobe_unregister(tu->uprobe, &tu->consumer); + uprobe_unregister_nosync(tu->uprobe, &tu->consumer); + sync = true; tu->uprobe = NULL; } + if (sync) + uprobe_unregister_sync(); } static int probe_event_enable(struct trace_event_call *call, diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c index 3c0515a27842..1fc16657cf42 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -475,7 +475,8 @@ static void testmod_unregister_uprobe(void) mutex_lock(&testmod_uprobe_mutex); if (uprobe.uprobe) { - uprobe_unregister(uprobe.uprobe, &uprobe.consumer); + uprobe_unregister_nosync(uprobe.uprobe, &uprobe.consumer); + uprobe_unregister_sync(); path_put(&uprobe.path); uprobe.uprobe = NULL; } From patchwork Tue Sep 3 17:46:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13789158 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 6F043CD37AA for ; Tue, 3 Sep 2024 17:46:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 047D88D01BD; Tue, 3 Sep 2024 13:46:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F3A9F8D018A; Tue, 3 Sep 2024 13:46:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E01B68D01BD; Tue, 3 Sep 2024 13:46:31 -0400 (EDT) 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 C08928D018A for ; Tue, 3 Sep 2024 13:46:31 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 057251207D2 for ; Tue, 3 Sep 2024 17:46:31 +0000 (UTC) X-FDA: 82524156582.08.60C7306 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf21.hostedemail.com (Postfix) with ESMTP id 4BBD91C0004 for ; Tue, 3 Sep 2024 17:46:29 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ePac60MH; spf=pass (imf21.hostedemail.com: domain of andrii@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725385494; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Uk348Kjht7XGQIw8421veAwyJRnWtKPa6jJWqAdo+Tc=; b=oI4O9WG1czioI9J64fnvPd4hoTyccbHb9aJ6ObZT2gG4b/j14gx33bLTvlicRhtGEUbY9s 1XR2mqjLps//kletBWr439T4NxYokNPzCwW/nCunQGdqiXDPyzrow3w+m8/gvW4VAXTe6Q SzRCnVxQTMVq7UaovQnmTosYWhtftWE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725385494; a=rsa-sha256; cv=none; b=LDxo4RIzUtmqqq/ql6B3kkoiujhvbnnm35i2pQibyvaOcMt9RbQdaKZ9+9/Jr8Tx7zsEn7 sbkU6GQqrVJdKDdS5fsc0vcaBzT6fUTQQ6D/YrF4SQgP9Z9nooxJRclGfC4Z4Q4tidrAHf sNO692vitiPp6GkOvGKCwlvgi1FEKjY= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ePac60MH; spf=pass (imf21.hostedemail.com: domain of andrii@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id A1FC35C5A44; Tue, 3 Sep 2024 17:46:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6E8DAC4CEC6; Tue, 3 Sep 2024 17:46:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725385587; bh=fbkVIaD1WSV5k2O1l+qbLGEuln2w8HvLuX0+cvs5jtI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ePac60MHZQZol0UGw03YynNTwklAOaXlOvgQPfdFV66MYzxkSzn+Zmk4d1UTS198j zU8y2voSoi8FHy8gTAz9OTJ/tflW4leFmuRPC95QNYsdovmI8L/kSz/oHSbwosTt4l 90yxAjvX0bAlHHDCz/NCCWumyGr9+oyKrPSrqZY5wXnN3jRekx9+V7cdxUl5sXMLkN qChy2ahmkKbxyNBiybflTFGhG9xQqB2nQW0rsB/wTcKY0458tc/hGBHh9VnRe/+5Oq UuYFJ19TN2rpx1y9fim9ueGgCZ24e5P73q8VPvfJvBuUviAuEuoLvMxFS4Z8cCJcb7 /vMnWH9OEYinw== From: Andrii Nakryiko To: linux-trace-kernel@vger.kernel.org, peterz@infradead.org, oleg@redhat.com Cc: rostedt@goodmis.org, mhiramat@kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, jolsa@kernel.org, paulmck@kernel.org, willy@infradead.org, surenb@google.com, akpm@linux-foundation.org, linux-mm@kvack.org, Andrii Nakryiko Subject: [PATCH v5 6/8] rbtree: provide rb_find_rcu() / rb_find_add_rcu() Date: Tue, 3 Sep 2024 10:46:01 -0700 Message-ID: <20240903174603.3554182-7-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240903174603.3554182-1-andrii@kernel.org> References: <20240903174603.3554182-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 4BBD91C0004 X-Stat-Signature: qkbaaxgb8zb8ykx7zr95u951xxido4u5 X-HE-Tag: 1725385589-543928 X-HE-Meta: U2FsdGVkX18dlPe+12eXZ2v/EtPuQmfC+H+F5E3BoMPrIgjuPzeyJd1LvF0GQNQ2xibgmrpghpbdsRFkLINakQ7HNg8s5stWu86Fzs1tXDbAgt+py/4pyZlDSPCSb2vRpPJTKzRCC76ubF8e3QL0va2CYhp2ylt1xkhLSpNZpGvH8c9ShALslGYysIi6fR/J72xnx4ZpI6tyhKzXwhosVccMXUTqkOc2lh84f2aNVxdNKZur4yCOZR2Mvsh09gCOEdhOqs+9eWWtssjtssV3m5fAO8OCwjcn28OqWlcSE6u57KrRw6Rlf+7i+o9daMC9K8xopbDfPOUeEGaxcC8ST3aY3mPQ200RglKATUvE3gLs6FSR9KaL1t1pzG502mXY2TCGXqQ7gsLksoig4pdnb4a2Fdcsm2FWtT28fsBxVysOB8vEHFzPf4369QBo56zjZOn8xNBFskTPp/43EEoEN21d+7bY3Uqh5iqbSxxoYJOLNrX0DOi8wvsfOB8ooRLSZcO24/dSIyYPZLrQDnldTJd66zJ4o9I9QHd5rzuYXDBe/+3c1K+OiKc+lFCuNhfO5+4ztSg3F0BuPCda6j0UzY8sfevnL0NaqPIbFbBFbd0HxwCfytGPTGyU+s+2N4Cn8kJE4W2+rvnWYmXfFA2ynTAOCpgzPQGhs6Ii6cFXm8QtvB8Lqos+W8tWAW0bVkShabpZippjG+fuB12RsIAF1HQocAasS7OEDHgM64U+RRmrG1unwg+0aFaPhB/AU059yJFHwXkhZTn4Me+YeKgWXQYI0mWUkffibupq5AE5bO0bm6YwtKypes7YiN9dvoMOOb5AuTo/zG1ZR8wXzC2ZV71qaBMbclnJYy3Jb4dBLry7/gbsfG3uhnGMqlxObCHUHcRQwvcL/rnpXMCOGjcckiWG8YDEuaXibgjvBzUYOFmWTbtYa1GTEpovMT2CIwUOQitGTv18ET/squCaMER ZlQee7zv 1I6Npp39offY8MJGlDLiA+LkIFuhrIVz0w6qqYQ6xGI40PM/rjvMG1pjSLH261XfHLcGGY/8ODMbnDzHYvKgiFau0fwzA3iYGTLRrISCRS9LZUH+QsfME0y61bxb8x59OOwC6gvXyH7A4IrYYpRR/C4XBeeWtLZwzS6tOCJyQNCKIeL1Vw8N4QEunrW92ClNqqMgeJcOz5fJ4TKys4l2i3/L3T5rv+rKhCp+Z5oPqWgm5BHHoJkLtK3sfKlghxcWEiF4ALBa/8PmVOGehKc2dwN4gwVZQA0Df3w4VwHo374YuT7ev4jwIX87ZjIUF765qnO7cKhhua6V8b7CQft8SKtzhDUb5GTzGYfdME1sF1BGQtBY= 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: From: Peter Zijlstra Much like latch_tree, add two RCU methods for the regular RB-tree, which can be used in conjunction with a seqcount to provide lockless lookups. Reviewed-by: Oleg Nesterov Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Masami Hiramatsu (Google) --- include/linux/rbtree.h | 67 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h index f7edca369eda..7c173aa64e1e 100644 --- a/include/linux/rbtree.h +++ b/include/linux/rbtree.h @@ -244,6 +244,42 @@ rb_find_add(struct rb_node *node, struct rb_root *tree, return NULL; } +/** + * rb_find_add_rcu() - find equivalent @node in @tree, or add @node + * @node: node to look-for / insert + * @tree: tree to search / modify + * @cmp: operator defining the node order + * + * Adds a Store-Release for link_node. + * + * Returns the rb_node matching @node, or NULL when no match is found and @node + * is inserted. + */ +static __always_inline struct rb_node * +rb_find_add_rcu(struct rb_node *node, struct rb_root *tree, + int (*cmp)(struct rb_node *, const struct rb_node *)) +{ + struct rb_node **link = &tree->rb_node; + struct rb_node *parent = NULL; + int c; + + while (*link) { + parent = *link; + c = cmp(node, parent); + + if (c < 0) + link = &parent->rb_left; + else if (c > 0) + link = &parent->rb_right; + else + return parent; + } + + rb_link_node_rcu(node, parent, link); + rb_insert_color(node, tree); + return NULL; +} + /** * rb_find() - find @key in tree @tree * @key: key to match @@ -272,6 +308,37 @@ rb_find(const void *key, const struct rb_root *tree, return NULL; } +/** + * rb_find_rcu() - find @key in tree @tree + * @key: key to match + * @tree: tree to search + * @cmp: operator defining the node order + * + * Notably, tree descent vs concurrent tree rotations is unsound and can result + * in false-negatives. + * + * Returns the rb_node matching @key or NULL. + */ +static __always_inline struct rb_node * +rb_find_rcu(const void *key, const struct rb_root *tree, + int (*cmp)(const void *key, const struct rb_node *)) +{ + struct rb_node *node = tree->rb_node; + + while (node) { + int c = cmp(key, node); + + if (c < 0) + node = rcu_dereference_raw(node->rb_left); + else if (c > 0) + node = rcu_dereference_raw(node->rb_right); + else + return node; + } + + return NULL; +} + /** * rb_find_first() - find the first @key in @tree * @key: key to match From patchwork Tue Sep 3 17:46:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13789159 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 D407CCD37AB for ; Tue, 3 Sep 2024 17:46:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 43E398D01BE; Tue, 3 Sep 2024 13:46:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3EE8B8D018A; Tue, 3 Sep 2024 13:46:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 267598D01BE; Tue, 3 Sep 2024 13:46:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 07A5C8D018A for ; Tue, 3 Sep 2024 13:46:34 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C2ECA1207D2 for ; Tue, 3 Sep 2024 17:46:33 +0000 (UTC) X-FDA: 82524156666.01.75A2F5B Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf20.hostedemail.com (Postfix) with ESMTP id 1B2611C0020 for ; Tue, 3 Sep 2024 17:46:31 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=SKhNGs1D; spf=pass (imf20.hostedemail.com: domain of andrii@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725385568; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bV4F9AJb16UUAfSetX/O8CLbZuY8uFXGxZE32BLGhr0=; b=5qFXyI5XTdzikX5sE9lRoYIxlmrhxrQfAuVVvN/YtRSZwNHvuTqZRIXEDiGQFmHaMWS/8L EtznOR4GBNPDWiB+DrWo/aavLF/fVtw8gSkXbMkw7L1vCdkIJP2oH8L6nLX8RMFP5ICTgp 31a8ej7fEuzOUNwXCfUfZvQTbFRJZbg= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=SKhNGs1D; spf=pass (imf20.hostedemail.com: domain of andrii@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725385568; a=rsa-sha256; cv=none; b=W37Ll5OtjEjfjuFzsmmMbOhW0FCH1VikAjC224eLez8tary25GkzuyIIWFFcnOu0Hsa5F2 d3xJA/q/FdUQq8UaeRePimlct3YjJypKrzecf1Bxb+4pwHc1mgorC5Z6NY6pST/mrE9hG/ bpEriNIjhRzoWyBZ8WfmdmGUao9NEMU= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 51986A43401; Tue, 3 Sep 2024 17:46:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A2803C4CEC4; Tue, 3 Sep 2024 17:46:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725385590; bh=RvQt+mn0Ib4bzcl2DbcJugmCiAEXvSCyxNzylfy/bEU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SKhNGs1DObzAy/uvZHlQgdMaLMKlO7U+tcdlSlZceAism21OBtxrovQA64NPpwVLO GcglGJsXb4iyd6KJDYJQDsda4kLcWkbICqjQlBGVEp0MWD8pt8XgkTRwbN4+H3gIDL GH9jODOs1OjLMtyK1qjE9D05z9d+2yAcnuVxw2JOkYdGy4T+qT54gF82tCTMXFukXj o8ygjUWUahKuKL8HgGYGYZhnF0SAnnsQjxYSmaUk0FvGjj7Q1paJIsaPFxifWgY8b+ 2xYntsad8LbEoczx4NMfU85OsVERPsaeOHQ2uDunAqe7yCZsnpzdpzm/IsvyDvKvbI fJOLjGoveDpCg== From: Andrii Nakryiko To: linux-trace-kernel@vger.kernel.org, peterz@infradead.org, oleg@redhat.com Cc: rostedt@goodmis.org, mhiramat@kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, jolsa@kernel.org, paulmck@kernel.org, willy@infradead.org, surenb@google.com, akpm@linux-foundation.org, linux-mm@kvack.org, Andrii Nakryiko Subject: [PATCH v5 7/8] uprobes: perform lockless SRCU-protected uprobes_tree lookup Date: Tue, 3 Sep 2024 10:46:02 -0700 Message-ID: <20240903174603.3554182-8-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240903174603.3554182-1-andrii@kernel.org> References: <20240903174603.3554182-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: uq4d71nbbdchkrw8g7yxo4bujoq8mmoz X-Rspamd-Queue-Id: 1B2611C0020 X-Rspamd-Server: rspam11 X-HE-Tag: 1725385591-121481 X-HE-Meta: U2FsdGVkX18zBt2HJDDJY4DsKzsSETMwkOWRoOEla1G2J+HgI2vM5QoVERG4/gfDLPsRGF/271ODcgmCbQUDrFzLPrSWo3rvVdljfjvVBk7uZN7fAp3wUrdLOio25r8GT980BMPi3z24bwlaoW1vzLgDx6NLqTR0GrVCQZlyxdaxx+mdEumD69Cwvt3NPO4U/+ULDN3mUkVRpFFNhyIHhq30sehL1xicP4KBKQ5Z18lOos+gUYQ5Y8+viW+PeJHUUbUL4y4yVdZ/TF1prgLMcXCEeI1cKziPL5BoLTBGxTfpfTl70NjezSb1wjdRz+oNVGxzRoXRf9/fP+1NIstX4z8xbbAp5fJKdE8t1hskW1FGF1UQpk6DsF1ESyptbdTlACn7QjgM74vFCe92LGVkucq8pEZJXZPTwVzN0hv3Y1QOWAv5K/pkur0b3gtQPuXaNbip+NxopSPcOUE6lvZfXjVKS5E/H/VvFx7CHVKrnY1qdT69YFniyyGHGFSuppNBB+e5eWXw9wZAa0m7UqbO71JgTaedkBmDqkCSkHgnrbdHh9qHkeVbnZbb9VxV+GJZ6doU5BDtjuj7uPMTLp6DjdgyvHoMyNpy9dHaMflHl7vWG7ZPBj/Oq031euD1IwyPJCdaOfHEvFp0pT6A1bkMfBH/v+w0bNC8jFzE1tRRCxKC5SOsbiPCC8Y9Ufp9l+lymhquONhyNBeQUfHot6tbD1QFFokNr1yNsK+Ho+NFLaYhm31iIFvGmnD1b4F7dnwoS6GPpBe9pbI46AzX7kDaIput9fA+IH7X+eVnM41PmF2gMltWCOPEKtVRtwqrs+qQiCNaEzwW3FpKq7HkXlDe/5S1nImRztWavqjeDSEQ5Gu4oJs5gLmDRvhai/rRn4UqjlgDUElGmdbJK4cnUg6+SAyhGaVfdRPj7BnjOvUKAdUCtUcHyXb65paMTKXe93ThxYh3tCP7NyPOQ4ZA0I8 /8uiFYyT kl/MiUFZVfGb9G1AnLGaPdd48u9p90ou+d2Yqtbc1uoFJ0nbXkevPV3dZdbxbBI2akuV0YT1plV9xpBJwuWQvIJOAoFqgSa4+XQbLFV1de0JvX/HlymnSmaj7e0uhfCJtyvc2cBydEHeP/S+cq4yit5qk/P1EXiJm4A9d7WumMeTo+R5zXq5kBwM05avv5k0QfTsb5CiHqeXsT3xc4hvfBz3qcbhff/5tilDezDGleYGLEmTOigiLPMzAO1vnKFOpmIg+GTBT+suU63AMb2NOpm6MDrNkoxyTNXMd1VY6q6cPQQSFWds1KLaGv1lSKgraTAyj79zr/PsubkoT0vZKQJ74v1RQVUby4GYw7lYesQ7YTcb3HbdNaxxkQPUxVolQuokylcX75hoY96o= 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: Another big bottleneck to scalablity is uprobe_treelock that's taken in a very hot path in handle_swbp(). Now that uprobes are SRCU-protected, take advantage of that and make uprobes_tree RB-tree look up lockless. To make RB-tree RCU-protected lockless lookup correct, we need to take into account that such RB-tree lookup can return false negatives if there are parallel RB-tree modifications (rotations) going on. We use seqcount lock to detect whether RB-tree changed, and if we find nothing while RB-tree got modified inbetween, we just retry. If uprobe was found, then it's guaranteed to be a correct lookup. With all the lock-avoiding changes done, we get a pretty decent improvement in performance and scalability of uprobes with number of CPUs, even though we are still nowhere near linear scalability. This is due to SRCU not really scaling very well with number of CPUs on a particular hardware that was used for testing (80-core Intel Xeon Gold 6138 CPU @ 2.00GHz), but also due to the remaning mmap_lock, which is currently taken to resolve interrupt address to inode+offset and then uprobe instance. And, of course, uretprobes still need similar RCU to avoid refcount in the hot path, which will be addressed in the follow up patches. Nevertheless, the improvement is good. We used BPF selftest-based uprobe-nop and uretprobe-nop benchmarks to get the below numbers, varying number of CPUs on which uprobes and uretprobes are triggered. BASELINE ======== uprobe-nop ( 1 cpus): 3.032 ± 0.023M/s ( 3.032M/s/cpu) uprobe-nop ( 2 cpus): 3.452 ± 0.005M/s ( 1.726M/s/cpu) uprobe-nop ( 4 cpus): 3.663 ± 0.005M/s ( 0.916M/s/cpu) uprobe-nop ( 8 cpus): 3.718 ± 0.038M/s ( 0.465M/s/cpu) uprobe-nop (16 cpus): 3.344 ± 0.008M/s ( 0.209M/s/cpu) uprobe-nop (32 cpus): 2.288 ± 0.021M/s ( 0.071M/s/cpu) uprobe-nop (64 cpus): 3.205 ± 0.004M/s ( 0.050M/s/cpu) uretprobe-nop ( 1 cpus): 1.979 ± 0.005M/s ( 1.979M/s/cpu) uretprobe-nop ( 2 cpus): 2.361 ± 0.005M/s ( 1.180M/s/cpu) uretprobe-nop ( 4 cpus): 2.309 ± 0.002M/s ( 0.577M/s/cpu) uretprobe-nop ( 8 cpus): 2.253 ± 0.001M/s ( 0.282M/s/cpu) uretprobe-nop (16 cpus): 2.007 ± 0.000M/s ( 0.125M/s/cpu) uretprobe-nop (32 cpus): 1.624 ± 0.003M/s ( 0.051M/s/cpu) uretprobe-nop (64 cpus): 2.149 ± 0.001M/s ( 0.034M/s/cpu) SRCU CHANGES ============ uprobe-nop ( 1 cpus): 3.276 ± 0.005M/s ( 3.276M/s/cpu) uprobe-nop ( 2 cpus): 4.125 ± 0.002M/s ( 2.063M/s/cpu) uprobe-nop ( 4 cpus): 7.713 ± 0.002M/s ( 1.928M/s/cpu) uprobe-nop ( 8 cpus): 8.097 ± 0.006M/s ( 1.012M/s/cpu) uprobe-nop (16 cpus): 6.501 ± 0.056M/s ( 0.406M/s/cpu) uprobe-nop (32 cpus): 4.398 ± 0.084M/s ( 0.137M/s/cpu) uprobe-nop (64 cpus): 6.452 ± 0.000M/s ( 0.101M/s/cpu) uretprobe-nop ( 1 cpus): 2.055 ± 0.001M/s ( 2.055M/s/cpu) uretprobe-nop ( 2 cpus): 2.677 ± 0.000M/s ( 1.339M/s/cpu) uretprobe-nop ( 4 cpus): 4.561 ± 0.003M/s ( 1.140M/s/cpu) uretprobe-nop ( 8 cpus): 5.291 ± 0.002M/s ( 0.661M/s/cpu) uretprobe-nop (16 cpus): 5.065 ± 0.019M/s ( 0.317M/s/cpu) uretprobe-nop (32 cpus): 3.622 ± 0.003M/s ( 0.113M/s/cpu) uretprobe-nop (64 cpus): 3.723 ± 0.002M/s ( 0.058M/s/cpu) Peak througput increased from 3.7 mln/s (uprobe triggerings) up to about 8 mln/s. For uretprobes it's a bit more modest with bump from 2.4 mln/s to 5mln/s. Reviewed-by: Oleg Nesterov Suggested-by: Peter Zijlstra (Intel) Signed-off-by: Andrii Nakryiko --- kernel/events/uprobes.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index e9b755ddf960..8a464cf38127 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -40,6 +40,7 @@ static struct rb_root uprobes_tree = RB_ROOT; #define no_uprobe_events() RB_EMPTY_ROOT(&uprobes_tree) static DEFINE_RWLOCK(uprobes_treelock); /* serialize rbtree access */ +static seqcount_rwlock_t uprobes_seqcount = SEQCNT_RWLOCK_ZERO(uprobes_seqcount, &uprobes_treelock); DEFINE_STATIC_SRCU(uprobes_srcu); @@ -634,8 +635,11 @@ static void put_uprobe(struct uprobe *uprobe) write_lock(&uprobes_treelock); - if (uprobe_is_active(uprobe)) + if (uprobe_is_active(uprobe)) { + write_seqcount_begin(&uprobes_seqcount); rb_erase(&uprobe->rb_node, &uprobes_tree); + write_seqcount_end(&uprobes_seqcount); + } write_unlock(&uprobes_treelock); @@ -701,14 +705,26 @@ static struct uprobe *find_uprobe_rcu(struct inode *inode, loff_t offset) .offset = offset, }; struct rb_node *node; + unsigned int seq; lockdep_assert(srcu_read_lock_held(&uprobes_srcu)); - read_lock(&uprobes_treelock); - node = rb_find(&key, &uprobes_tree, __uprobe_cmp_key); - read_unlock(&uprobes_treelock); + do { + seq = read_seqcount_begin(&uprobes_seqcount); + node = rb_find_rcu(&key, &uprobes_tree, __uprobe_cmp_key); + /* + * Lockless RB-tree lookups can result only in false negatives. + * If the element is found, it is correct and can be returned + * under RCU protection. If we find nothing, we need to + * validate that seqcount didn't change. If it did, we have to + * try again as we might have missed the element (false + * negative). If seqcount is unchanged, search truly failed. + */ + if (node) + return __node_2_uprobe(node); + } while (read_seqcount_retry(&uprobes_seqcount, seq)); - return node ? __node_2_uprobe(node) : NULL; + return NULL; } /* @@ -730,7 +746,7 @@ static struct uprobe *__insert_uprobe(struct uprobe *uprobe) { struct rb_node *node; again: - node = rb_find_add(&uprobe->rb_node, &uprobes_tree, __uprobe_cmp); + node = rb_find_add_rcu(&uprobe->rb_node, &uprobes_tree, __uprobe_cmp); if (node) { struct uprobe *u = __node_2_uprobe(node); @@ -755,7 +771,9 @@ static struct uprobe *insert_uprobe(struct uprobe *uprobe) struct uprobe *u; write_lock(&uprobes_treelock); + write_seqcount_begin(&uprobes_seqcount); u = __insert_uprobe(uprobe); + write_seqcount_end(&uprobes_seqcount); write_unlock(&uprobes_treelock); return u; From patchwork Tue Sep 3 17:46:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13789160 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 BBB52CD37AA for ; Tue, 3 Sep 2024 17:46:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4DD748D01BF; Tue, 3 Sep 2024 13:46:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 43C7E8D018A; Tue, 3 Sep 2024 13:46:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 26B5E8D01BF; Tue, 3 Sep 2024 13:46:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id F372B8D018A for ; Tue, 3 Sep 2024 13:46:37 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 45B451A084B for ; Tue, 3 Sep 2024 17:46:37 +0000 (UTC) X-FDA: 82524156834.20.D81D0F2 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf24.hostedemail.com (Postfix) with ESMTP id 614B4180033 for ; Tue, 3 Sep 2024 17:46:35 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=HI8wfMpG; spf=pass (imf24.hostedemail.com: domain of andrii@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725385469; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2cPfkNPUgpWNCJ64tOTVJ9JwI+/m8MnmG4i1rhglujk=; b=ycrCu5H1jND4Lh4am8WlDlsyBPr/Ufow0EtxSmP4Y/BcDUTfVPS1kkpdKTJyc1kPfneELo 8tXBTc42QsqYx3ZFr5EZmBtfxMxyLu643eOfu3AblGU7vhlmNYsYhN5IoE/H6uiiJlHBjV l0PlqauUreRPgqk/6qi3ZSofhh3Wz5k= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=HI8wfMpG; spf=pass (imf24.hostedemail.com: domain of andrii@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725385469; a=rsa-sha256; cv=none; b=VipVDXm929L8MBhGoOcBK90Wx8Hysu9gN4ak8aLXMUXunNFqBfcQyI9OVViPP4RYBf3CtD /1ZUV1P971EH7y98v5rmvt+6GgSlKW59hvmPYO2xNCVx/Qf0r1IVzqNYCOXLIE+oXGlt7J sSbbqAvyTCMMabFtZuOElWlqkmjOk9A= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 239E65C5A41; Tue, 3 Sep 2024 17:46:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E481FC4CEC6; Tue, 3 Sep 2024 17:46:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725385594; bh=R43LEMwmNgnFNuxwf3tZak7H2OM5C5eCgaYGHiHVLKQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HI8wfMpGnWd5IDk1+SWR2si4h7TTVdySI6NjRRNceULM9jxc1mTi8TSxrcv7qt2/H K2J8ytHFtwPEPduB0Thbxm+VtWE/ANW5HFXHbn8mAUuq0N08HRQGso0NR63YVe2Rtf wMytWtQfWut96YvdyPDW0Lknyiw6MN+OGUacfX3o4ZAc9fXxk+ttv+UQGl8MLnfmGf 1vi5w2koWFj9R87QOtca1gfaUU90bmfRwwTs8Kg/0szmEUvkSEB2PDMvjnkgDrd0hu a+xADk99DZUWTxFD4lQbcBxqcVRD7TQ3qYrqNDFxAhkvQsYUwBlVVsC9qvymVEfd8a RIcY0M0oKgmCg== From: Andrii Nakryiko To: linux-trace-kernel@vger.kernel.org, peterz@infradead.org, oleg@redhat.com Cc: rostedt@goodmis.org, mhiramat@kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, jolsa@kernel.org, paulmck@kernel.org, willy@infradead.org, surenb@google.com, akpm@linux-foundation.org, linux-mm@kvack.org, Andrii Nakryiko Subject: [PATCH v5 8/8] uprobes: switch to RCU Tasks Trace flavor for better performance Date: Tue, 3 Sep 2024 10:46:03 -0700 Message-ID: <20240903174603.3554182-9-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240903174603.3554182-1-andrii@kernel.org> References: <20240903174603.3554182-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 614B4180033 X-Stat-Signature: 64s1cqir8ncfqyuwqri1b6od5dc1df9m X-Rspam-User: X-HE-Tag: 1725385595-655170 X-HE-Meta: U2FsdGVkX194aCuI7V8Mc1jHB8zkWGca6VKxxglLzZgX5Lefy5r2G5HBBp1/kZPO6f/fQWHqcoze+aE1cZcuukmax6B72H3NxboHSE5mPTb2vZ7f875Pp9mwOCdyu2UmprWKfZS7nNOswbuS5xgfB4hFCPgmP6tvaDK+KHqmWxwosFo669DfdXH9AiTzM9wkRnKApp1yqqCcq49f+Gkz22u9WR/xTBKKlEiyOu113NOGXXgG2+jQQueR1RejfxKVut5LLtBR99xYUqrNUF8c12ryq3kxJJFLjSdDi/XeC9X4M9giZA6bD4u3ygZk+0aeNfCiGcz18pwDuOYVY9haNirCpWtOG1mAVxlFZS+tDxDP3Ncqfr6f0EYpVrbFlYjeC9cxZJzyLZ2NUlL2u3QgxMNBVfHdBGfK8h9RoYvpNcQOHkE+DY3dN/rnjPo0cMhTZT7jEdyRRF5warxZMZ3LZwwr0XkLcZz2iURvwKXBmzUKPH+G1CbQ7jL1zNQHTKbnGDX52PLWIl/HQyHo+4WaD0u7loe/bthxqHeAYz0mvAegFR686pdtoQeO0F0XXUtBpjGztoi922Ud/Wk/FhZ4hT6pRc/C85knuEhZlrh+tSA7BGmVwNVmYxiOSSOA4QFHQZRYHvI8NCYDwTu9q0r9O9umH+Bq/rqDv1ua9qqR4oWKuXN/PTW0bJU2MeHcQjGzuqlGe3bGpvP6RAYIchknlRQaXhagYY2ZZ5hZ1ITt4XcG4WCyYLxXu0qR0rGBa6+cMuWeK0T4nzsEYlm4Q/UxY8f7AjKnkJF/tXux+4Kem+POfn5Yervtf3ddVJvKUdRdcvvBUZPJcmMzOKLO/DqB0FBvZyaPMtfSLMohdAXZgZYCVrrGh/aRQYdS4IrO9MujjHTLRU/+wAsLGkOVPGEZ7SLeQJ1uhK01T4gdFBMURN8I+KklJnhkOQSkuUmGrE5LVSFYBC25d5impP/CmMG 3mOlcdS1 UAjnS5VsQzf4BcnfMmb6kg+Dw8Vv/aRnENr5e0YZZrBZfjKNpdi1t9pVUjJcoTJKXU25jUvGlWXCNtPlO0SpePwQgfPOHNCd5wpJePVtLDwxv4sJdgmOhZjqjsUuiBJa79AZTwjVcCL1Bev4WI7uLmPJei1EvbXQXUwJysfoARAVDsaRpsHfiTMGsmS+J2oPIjnFiIjhtGh+SuzJxGpPa5/PfMOQ7SdE4CfxSY0NWbHVQdDR7UnbDRvofU0UkDQ8B/5oRnDDn3jxkfw5HD5B+rdPBgUT5DtjMK48aZcWJ1qNQEZI= 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: This patch switches uprobes SRCU usage to RCU Tasks Trace flavor, which is optimized for more lightweight and quick readers (at the expense of slower writers, which for uprobes is a fine tradeof) and has better performance and scalability with number of CPUs. Similarly to baseline vs SRCU, we've benchmarked SRCU-based implementation vs RCU Tasks Trace implementation. SRCU ==== uprobe-nop ( 1 cpus): 3.276 ± 0.005M/s ( 3.276M/s/cpu) uprobe-nop ( 2 cpus): 4.125 ± 0.002M/s ( 2.063M/s/cpu) uprobe-nop ( 4 cpus): 7.713 ± 0.002M/s ( 1.928M/s/cpu) uprobe-nop ( 8 cpus): 8.097 ± 0.006M/s ( 1.012M/s/cpu) uprobe-nop (16 cpus): 6.501 ± 0.056M/s ( 0.406M/s/cpu) uprobe-nop (32 cpus): 4.398 ± 0.084M/s ( 0.137M/s/cpu) uprobe-nop (64 cpus): 6.452 ± 0.000M/s ( 0.101M/s/cpu) uretprobe-nop ( 1 cpus): 2.055 ± 0.001M/s ( 2.055M/s/cpu) uretprobe-nop ( 2 cpus): 2.677 ± 0.000M/s ( 1.339M/s/cpu) uretprobe-nop ( 4 cpus): 4.561 ± 0.003M/s ( 1.140M/s/cpu) uretprobe-nop ( 8 cpus): 5.291 ± 0.002M/s ( 0.661M/s/cpu) uretprobe-nop (16 cpus): 5.065 ± 0.019M/s ( 0.317M/s/cpu) uretprobe-nop (32 cpus): 3.622 ± 0.003M/s ( 0.113M/s/cpu) uretprobe-nop (64 cpus): 3.723 ± 0.002M/s ( 0.058M/s/cpu) RCU Tasks Trace =============== uprobe-nop ( 1 cpus): 3.396 ± 0.002M/s ( 3.396M/s/cpu) uprobe-nop ( 2 cpus): 4.271 ± 0.006M/s ( 2.135M/s/cpu) uprobe-nop ( 4 cpus): 8.499 ± 0.015M/s ( 2.125M/s/cpu) uprobe-nop ( 8 cpus): 10.355 ± 0.028M/s ( 1.294M/s/cpu) uprobe-nop (16 cpus): 7.615 ± 0.099M/s ( 0.476M/s/cpu) uprobe-nop (32 cpus): 4.430 ± 0.007M/s ( 0.138M/s/cpu) uprobe-nop (64 cpus): 6.887 ± 0.020M/s ( 0.108M/s/cpu) uretprobe-nop ( 1 cpus): 2.174 ± 0.001M/s ( 2.174M/s/cpu) uretprobe-nop ( 2 cpus): 2.853 ± 0.001M/s ( 1.426M/s/cpu) uretprobe-nop ( 4 cpus): 4.913 ± 0.002M/s ( 1.228M/s/cpu) uretprobe-nop ( 8 cpus): 5.883 ± 0.002M/s ( 0.735M/s/cpu) uretprobe-nop (16 cpus): 5.147 ± 0.001M/s ( 0.322M/s/cpu) uretprobe-nop (32 cpus): 3.738 ± 0.008M/s ( 0.117M/s/cpu) uretprobe-nop (64 cpus): 4.397 ± 0.002M/s ( 0.069M/s/cpu) Peak throughput for uprobes increases from 8 mln/s to 10.3 mln/s (+28%!), and for uretprobes from 5.3 mln/s to 5.8 mln/s (+11%), as we have more work to do on uretprobes side. Even single-thread (no contention) performance is slightly better: 3.276 mln/s to 3.396 mln/s (+3.5%) for uprobes, and 2.055 mln/s to 2.174 mln/s (+5.8%) for uretprobes. Reviewed-by: Oleg Nesterov Signed-off-by: Andrii Nakryiko --- kernel/events/uprobes.c | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index 8a464cf38127..1b3990dd9c93 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -26,6 +26,7 @@ #include #include #include +#include #include @@ -42,8 +43,6 @@ static struct rb_root uprobes_tree = RB_ROOT; static DEFINE_RWLOCK(uprobes_treelock); /* serialize rbtree access */ static seqcount_rwlock_t uprobes_seqcount = SEQCNT_RWLOCK_ZERO(uprobes_seqcount, &uprobes_treelock); -DEFINE_STATIC_SRCU(uprobes_srcu); - #define UPROBES_HASH_SZ 13 /* serialize uprobe->pending_list */ static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ]; @@ -652,7 +651,7 @@ static void put_uprobe(struct uprobe *uprobe) delayed_uprobe_remove(uprobe, NULL); mutex_unlock(&delayed_uprobe_lock); - call_srcu(&uprobes_srcu, &uprobe->rcu, uprobe_free_rcu); + call_rcu_tasks_trace(&uprobe->rcu, uprobe_free_rcu); } static __always_inline @@ -707,7 +706,7 @@ static struct uprobe *find_uprobe_rcu(struct inode *inode, loff_t offset) struct rb_node *node; unsigned int seq; - lockdep_assert(srcu_read_lock_held(&uprobes_srcu)); + lockdep_assert(rcu_read_lock_trace_held()); do { seq = read_seqcount_begin(&uprobes_seqcount); @@ -935,8 +934,7 @@ static bool filter_chain(struct uprobe *uprobe, struct mm_struct *mm) bool ret = false; down_read(&uprobe->consumer_rwsem); - list_for_each_entry_srcu(uc, &uprobe->consumers, cons_node, - srcu_read_lock_held(&uprobes_srcu)) { + list_for_each_entry_rcu(uc, &uprobe->consumers, cons_node, rcu_read_lock_trace_held()) { ret = consumer_filter(uc, mm); if (ret) break; @@ -1157,7 +1155,7 @@ void uprobe_unregister_sync(void) * unlucky enough caller can free consumer's memory and cause * handler_chain() or handle_uretprobe_chain() to do an use-after-free. */ - synchronize_srcu(&uprobes_srcu); + synchronize_rcu_tasks_trace(); } EXPORT_SYMBOL_GPL(uprobe_unregister_sync); @@ -1241,19 +1239,18 @@ EXPORT_SYMBOL_GPL(uprobe_register); int uprobe_apply(struct uprobe *uprobe, struct uprobe_consumer *uc, bool add) { struct uprobe_consumer *con; - int ret = -ENOENT, srcu_idx; + int ret = -ENOENT; down_write(&uprobe->register_rwsem); - srcu_idx = srcu_read_lock(&uprobes_srcu); - list_for_each_entry_srcu(con, &uprobe->consumers, cons_node, - srcu_read_lock_held(&uprobes_srcu)) { + rcu_read_lock_trace(); + list_for_each_entry_rcu(con, &uprobe->consumers, cons_node, rcu_read_lock_trace_held()) { if (con == uc) { ret = register_for_each_vma(uprobe, add ? uc : NULL); break; } } - srcu_read_unlock(&uprobes_srcu, srcu_idx); + rcu_read_unlock_trace(); up_write(&uprobe->register_rwsem); @@ -2123,8 +2120,7 @@ static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs) current->utask->auprobe = &uprobe->arch; - list_for_each_entry_srcu(uc, &uprobe->consumers, cons_node, - srcu_read_lock_held(&uprobes_srcu)) { + list_for_each_entry_rcu(uc, &uprobe->consumers, cons_node, rcu_read_lock_trace_held()) { int rc = 0; if (uc->handler) { @@ -2162,15 +2158,13 @@ handle_uretprobe_chain(struct return_instance *ri, struct pt_regs *regs) { struct uprobe *uprobe = ri->uprobe; struct uprobe_consumer *uc; - int srcu_idx; - srcu_idx = srcu_read_lock(&uprobes_srcu); - list_for_each_entry_srcu(uc, &uprobe->consumers, cons_node, - srcu_read_lock_held(&uprobes_srcu)) { + rcu_read_lock_trace(); + list_for_each_entry_rcu(uc, &uprobe->consumers, cons_node, rcu_read_lock_trace_held()) { if (uc->ret_handler) uc->ret_handler(uc, ri->func, regs); } - srcu_read_unlock(&uprobes_srcu, srcu_idx); + rcu_read_unlock_trace(); } static struct return_instance *find_next_ret_chain(struct return_instance *ri) @@ -2255,13 +2249,13 @@ static void handle_swbp(struct pt_regs *regs) { struct uprobe *uprobe; unsigned long bp_vaddr; - int is_swbp, srcu_idx; + int is_swbp; bp_vaddr = uprobe_get_swbp_addr(regs); if (bp_vaddr == uprobe_get_trampoline_vaddr()) return uprobe_handle_trampoline(regs); - srcu_idx = srcu_read_lock(&uprobes_srcu); + rcu_read_lock_trace(); uprobe = find_active_uprobe_rcu(bp_vaddr, &is_swbp); if (!uprobe) { @@ -2319,7 +2313,7 @@ static void handle_swbp(struct pt_regs *regs) out: /* arch_uprobe_skip_sstep() succeeded, or restart if can't singlestep */ - srcu_read_unlock(&uprobes_srcu, srcu_idx); + rcu_read_unlock_trace(); } /*