From patchwork Fri Sep 6 05:12:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13793243 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 E6A35C54FC6 for ; Fri, 6 Sep 2024 05:12:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 330686B0085; Fri, 6 Sep 2024 01:12:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2E0DF6B0088; Fri, 6 Sep 2024 01:12:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 15A106B0089; Fri, 6 Sep 2024 01:12:22 -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 EA7F46B0085 for ; Fri, 6 Sep 2024 01:12:21 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 9B0BA140E9F for ; Fri, 6 Sep 2024 05:12:21 +0000 (UTC) X-FDA: 82533142482.07.CD847A4 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf21.hostedemail.com (Postfix) with ESMTP id 0AED11C0011 for ; Fri, 6 Sep 2024 05:12:19 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=rm0jdEna; spf=pass (imf21.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=1725599491; 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=bP6R0DkS+tsnHTedFJqfnXzJpWg2HbEuKwvHd0ydJ5A=; b=K/oTEu3TePOJQt3vYHQ5te5p2rPhkWWE0GqAb9Ts1jTfcorPDIF3quhs2GLjGF5ng9AT0o 2jeRfuOWyFxwTT4rXQmvqqrUngsc+ZsWjSRc8EH3/bOslnIb3wZqx+5QNxxTJy85uaoNi9 5gJFLgdaYxu9tO+6LUanpAe3fImyKmc= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=rm0jdEna; spf=pass (imf21.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=1725599491; a=rsa-sha256; cv=none; b=uYdlh4eG1WYGCjSf8guM0Xp8PWsFUzZLLPX8DciJiBq4MbHPHXghh6mIXl77DT6ZKraxwL dco7wVetJCtcfbbvrMXQebZ0cnzcCzOMMhv3jlD6jIXZ4lb6Fq2rSvx4shsCdjK4ajx3gF d7+aNn7gEZwoy400TQS3j9dEAgHTtNM= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 04133A4401B; Fri, 6 Sep 2024 05:12:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B0516C4CEC8; Fri, 6 Sep 2024 05:12:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725599538; bh=QeJaV7NxUMr6y7gsUb56bD7q6rDPJGAUdKl0q2s8gUQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rm0jdEnaH3gzrt/ZrzjYJggbJMRhkZ7pVttHT3Y+qQm8rP4KhY2QvSm9MAX8Eu2SE YsrL50oF9CAIz7Br9LxTh4dZT2uhC5roEACDiBZCrIoYMIk+yu7hwLc7AMIH6tFJar GRZ5BwFmqxuwpF6m6qRASY8wfKRHZWJ34QrPHK9cXo3OaHBwJ0urPXPHV4kJACdIBk Nz4szuc7WQA06iXoQBWbbcDWFJm9KnewfJ9cZ1gjWlI0NI+Pif3ioOEsAYa6s1uUy8 nUY5EWljCzZOS9t4CHvTPUDuvvpwtC30A8kEFcc5R3xvBR5SQ3jEUbBfkJYGlwt+MN fwoGEE4m1GKIQ== 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, mjguzik@gmail.com, brauner@kernel.org, jannh@google.com, Andrii Nakryiko Subject: [PATCH 1/2] mm: introduce mmap_lock_speculation_{start|end} Date: Thu, 5 Sep 2024 22:12:04 -0700 Message-ID: <20240906051205.530219-2-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240906051205.530219-1-andrii@kernel.org> References: <20240906051205.530219-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 0AED11C0011 X-Stat-Signature: 4rtufw39ayg8iksb7h5153c9tnrib5ac X-HE-Tag: 1725599539-855173 X-HE-Meta: U2FsdGVkX18/FO+oW91FsabjGB5HMOSmA6SnYkpXxZJIKEo/f75a6bUgfjFH7UqziD3oGay4nWnr0hCfTvgpGVx+700Gl5esXirKDxz//SefXfIqczt4oaJQFOX+43rxLANaDZNgfv4Y7YmmE6yeD/Na+Z0exBkjO6Rqf5PJhIPS5yjJxtBSeV9WNKuB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RQW0AIB+o+G8enwwFRQ/IAd75z8MmMnU9cdsCYLuJ4uv3/kBCLYKkcZzxm2606spk5PRWqflbGRm8xKAR4V7SvdDRKdmPjBoN6h9VKRIEEV5xyxw0OtaNY+z6JPW8E= 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: Suren Baghdasaryan Add helper functions to speculatively perform operations without read-locking mmap_lock, expecting that mmap_lock will not be write-locked and mm is not modified from under us. A few will-it-scale ([0]) bechmarks were run with and without the changes in this patch on a beefy server. The test script below was used. Most of background activity on the server was stopped, but there could still be some sporadic sources of noise. And frankly, will-it-scale benchmarks themselves aren't 100% noise-free and do fluctuate somewhat. Also malloc1 benchmark was getting stuck for some reason, so it was skipped from benchmarks. But I think it was still useful as a bit of sanity check, but take all of them with a grain of salt. Benchmark script: # cat will_it_scale.sh #!/bin/bash set -eufo pipefail for b in page_fault1 page_fault2 page_fault3 malloc2; do for p in 40; do echo BENCH $b CPU$p $(will-it-scale/${b}_threads -m -t$p -s60 | grep average) done; done; Before (w/o this patch) ======================= BENCH page_fault1 CPU40 average:5403940 BENCH page_fault2 CPU40 average:5019159 BENCH page_fault3 CPU40 average:971057 BENCH malloc2 CPU40 average:1364730680 After (w/ this patch) ===================== BENCH page_fault1 CPU40 average:5485892 BENCH page_fault2 CPU40 average:5047923 BENCH page_fault3 CPU40 average:982953 BENCH malloc2 CPU40 average:1361339890 Results seem to be within the noise of measurements, but perhaps mm folks might have better benchmarks to try. [0] https://github.com/antonblanchard/will-it-scale Suggested-by: Peter Zijlstra Signed-off-by: Suren Baghdasaryan Signed-off-by: Andrii Nakryiko --- include/linux/mm_types.h | 3 +++ include/linux/mmap_lock.h | 53 +++++++++++++++++++++++++++++++-------- kernel/fork.c | 3 --- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 485424979254..d5e3f907eea4 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -876,6 +876,9 @@ struct mm_struct { * Roughly speaking, incrementing the sequence number is * equivalent to releasing locks on VMAs; reading the sequence * number can be part of taking a read lock on a VMA. + * Incremented every time mmap_lock is write-locked/unlocked. + * Initialized to 0, therefore odd values indicate mmap_lock + * is write-locked and even values that it's released. * * Can be modified under write mmap_lock using RELEASE * semantics. diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h index de9dc20b01ba..5410ce741d75 100644 --- a/include/linux/mmap_lock.h +++ b/include/linux/mmap_lock.h @@ -71,15 +71,12 @@ static inline void mmap_assert_write_locked(const struct mm_struct *mm) } #ifdef CONFIG_PER_VMA_LOCK -/* - * Drop all currently-held per-VMA locks. - * This is called from the mmap_lock implementation directly before releasing - * a write-locked mmap_lock (or downgrading it to read-locked). - * This should normally NOT be called manually from other places. - * If you want to call this manually anyway, keep in mind that this will release - * *all* VMA write locks, including ones from further up the stack. - */ -static inline void vma_end_write_all(struct mm_struct *mm) +static inline void init_mm_lock_seq(struct mm_struct *mm) +{ + mm->mm_lock_seq = 0; +} + +static inline void inc_mm_lock_seq(struct mm_struct *mm) { mmap_assert_write_locked(mm); /* @@ -91,19 +88,52 @@ static inline void vma_end_write_all(struct mm_struct *mm) */ smp_store_release(&mm->mm_lock_seq, mm->mm_lock_seq + 1); } + +static inline bool mmap_lock_speculation_start(struct mm_struct *mm, int *seq) +{ + /* Pairs with RELEASE semantics in inc_mm_lock_seq(). */ + *seq = smp_load_acquire(&mm->mm_lock_seq); + /* Allow speculation if mmap_lock is not write-locked */ + return (*seq & 1) == 0; +} + +static inline bool mmap_lock_speculation_end(struct mm_struct *mm, int seq) +{ + /* Pairs with RELEASE semantics in inc_mm_lock_seq(). */ + return seq == smp_load_acquire(&mm->mm_lock_seq); +} + #else -static inline void vma_end_write_all(struct mm_struct *mm) {} +static inline void init_mm_lock_seq(struct mm_struct *mm) {} +static inline void inc_mm_lock_seq(struct mm_struct *mm) {} +static inline bool mmap_lock_speculation_start(struct mm_struct *mm, int *seq) { return false; } +static inline bool mmap_lock_speculation_end(struct mm_struct *mm, int seq) { return false; } #endif +/* + * Drop all currently-held per-VMA locks. + * This is called from the mmap_lock implementation directly before releasing + * a write-locked mmap_lock (or downgrading it to read-locked). + * This should normally NOT be called manually from other places. + * If you want to call this manually anyway, keep in mind that this will release + * *all* VMA write locks, including ones from further up the stack. + */ +static inline void vma_end_write_all(struct mm_struct *mm) +{ + inc_mm_lock_seq(mm); +} + static inline void mmap_init_lock(struct mm_struct *mm) { init_rwsem(&mm->mmap_lock); + init_mm_lock_seq(mm); } static inline void mmap_write_lock(struct mm_struct *mm) { __mmap_lock_trace_start_locking(mm, true); down_write(&mm->mmap_lock); + inc_mm_lock_seq(mm); __mmap_lock_trace_acquire_returned(mm, true, true); } @@ -111,6 +141,7 @@ static inline void mmap_write_lock_nested(struct mm_struct *mm, int subclass) { __mmap_lock_trace_start_locking(mm, true); down_write_nested(&mm->mmap_lock, subclass); + inc_mm_lock_seq(mm); __mmap_lock_trace_acquire_returned(mm, true, true); } @@ -120,6 +151,8 @@ static inline int mmap_write_lock_killable(struct mm_struct *mm) __mmap_lock_trace_start_locking(mm, true); ret = down_write_killable(&mm->mmap_lock); + if (!ret) + inc_mm_lock_seq(mm); __mmap_lock_trace_acquire_returned(mm, true, ret == 0); return ret; } diff --git a/kernel/fork.c b/kernel/fork.c index 18bdc87209d0..c44b71d354ee 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1259,9 +1259,6 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, seqcount_init(&mm->write_protect_seq); mmap_init_lock(mm); INIT_LIST_HEAD(&mm->mmlist); -#ifdef CONFIG_PER_VMA_LOCK - mm->mm_lock_seq = 0; -#endif mm_pgtables_bytes_init(mm); mm->map_count = 0; mm->locked_vm = 0; From patchwork Fri Sep 6 05:12:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13793244 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 81D11CD5BDD for ; Fri, 6 Sep 2024 05:12:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 024946B0089; Fri, 6 Sep 2024 01:12:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EEE5D6B008A; Fri, 6 Sep 2024 01:12:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D8F106B008C; Fri, 6 Sep 2024 01:12:25 -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 BB6D76B0089 for ; Fri, 6 Sep 2024 01:12:25 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 83FD940ED8 for ; Fri, 6 Sep 2024 05:12:25 +0000 (UTC) X-FDA: 82533142650.27.90C9806 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf16.hostedemail.com (Postfix) with ESMTP id CDC9A180007 for ; Fri, 6 Sep 2024 05:12:23 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QV5vPYv9; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf16.hostedemail.com: domain of andrii@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=andrii@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725599519; a=rsa-sha256; cv=none; b=o3nwZcrWwSZsteqVRu0y2hXEWH7RzpCfagi6976A0x5wX9xW3Jbo7iAJL1DMqTOl12l5QI kIPKycG3ulwIg5dgRECHgcVVOyVeBAAAdwVIXhqQgw2kjBDCb5x+ulQIQ9rVamCZpDHZhI S2ccKS2pcp16Ox3Y9jXBQNdf7jRCoy8= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QV5vPYv9; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf16.hostedemail.com: domain of andrii@kernel.org designates 139.178.84.217 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=1725599519; 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=YvMcVkuuDnnyRTEqCYf+7sjk1WXKLsWfuefXUId0sb8=; b=SB7LyDpuSUDYxINRtkGRP83Gx9KsjEUVMn2IOaU2KFA/nruqPCmQDM8QwBeH6zT0NGITxm B6UycL7FuNgj/gljMK1qmdw5Q7z9P4XG+KqRDWoGN/Y9KDFn5foD4u8VhYKh/9CyugjX0k SSiEKaHb+IOVT0x450kuwwAKaGNaFf8= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 10E765C5B00; Fri, 6 Sep 2024 05:12:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 073E6C4CEC6; Fri, 6 Sep 2024 05:12:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725599542; bh=a4NkBvZonTqJ8yoj/hSxb9ICiGMU6QXUrxyQkC7Vl7w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QV5vPYv97/m9EPJuKGcj8XYI2CIWFnMfV9qyOikRm2O2gONtbgbefgY1QLUToUfoI JGeqHH2XYcPRFqsIWepREFHoU4xeN9TEd0AkFH+iIYtYK1Rg0Ex98g80m2ssNbkxqg yrm3OuQYfId7lYZ8+GRSXf/mkEbATfZTqBc6UjtN9oUPEhiAo9QJhq78lDyUL16DWA PToWOgAQgGVUwkfytLNTxnmkD/wfUD9grIspqyWdMdftMmXOy4ls4XEwfG3PV0BbYj /olCX9wwPbpeFpFV7bS/whMyssZ5/N/5sxen+31HVo7vSb9CS2IqwIUYWhIQ9Wjf1H 4DQab422ljrsg== 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, mjguzik@gmail.com, brauner@kernel.org, jannh@google.com, Andrii Nakryiko Subject: [PATCH 2/2] uprobes: add speculative lockless VMA-to-inode-to-uprobe resolution Date: Thu, 5 Sep 2024 22:12:05 -0700 Message-ID: <20240906051205.530219-3-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240906051205.530219-1-andrii@kernel.org> References: <20240906051205.530219-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: CDC9A180007 X-Rspamd-Server: rspam01 X-Stat-Signature: hh3fmcjibw4k9f9u845ntxwu69rbxjr3 X-HE-Tag: 1725599543-105591 X-HE-Meta: U2FsdGVkX18gJuiFlfksWFoOtbFB/MaSz+DuCqNfqOXu5xGwoaOAYuzMhK1ROWBHvoiYrsDoXMmtMZdJgG2Ptd+nclN+n96BtBgG4DjePdckqM+yhNMupM6KEkYB6e3Ync9DIW/+rybCFG9zlJRKutZK2tw/Jox58/irLCvKnfpU99EdYd2odfqpcvvekxGNHF1bu42WGsSXfrzxYBMwsf2Mne8Jo3UwC59NZfLriYLaXZ5V9BH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-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: Given filp_cachep is already marked SLAB_TYPESAFE_BY_RCU, we can safely access vma->vm_file->f_inode field locklessly under just rcu_read_lock() protection, which enables looking up uprobe from uprobes_tree completely locklessly and speculatively without the need to acquire mmap_lock for reads. In most cases, anyway, under the assumption that there are no parallel mm and/or VMA modifications. The underlying struct file's memory won't go away from under us (even if struct file can be reused in the meantime). We rely on newly added mmap_lock_speculation_{start,end}() helpers to validate that mm_struct stays intact for entire duration of this speculation. If not, we fall back to mmap_lock-protected lookup. The speculative logic is written in such a way that it will safely handle any garbage values that might be read from vma or file structs. Benchmarking results speak for themselves. BEFORE (latest tip/perf/core) ============================= uprobe-nop ( 1 cpus): 3.384 ± 0.004M/s ( 3.384M/s/cpu) uprobe-nop ( 2 cpus): 5.456 ± 0.005M/s ( 2.728M/s/cpu) uprobe-nop ( 3 cpus): 7.863 ± 0.015M/s ( 2.621M/s/cpu) uprobe-nop ( 4 cpus): 9.442 ± 0.008M/s ( 2.360M/s/cpu) uprobe-nop ( 5 cpus): 11.036 ± 0.013M/s ( 2.207M/s/cpu) uprobe-nop ( 6 cpus): 10.884 ± 0.019M/s ( 1.814M/s/cpu) uprobe-nop ( 7 cpus): 7.897 ± 0.145M/s ( 1.128M/s/cpu) uprobe-nop ( 8 cpus): 10.021 ± 0.128M/s ( 1.253M/s/cpu) uprobe-nop (10 cpus): 9.932 ± 0.170M/s ( 0.993M/s/cpu) uprobe-nop (12 cpus): 8.369 ± 0.056M/s ( 0.697M/s/cpu) uprobe-nop (14 cpus): 8.678 ± 0.017M/s ( 0.620M/s/cpu) uprobe-nop (16 cpus): 7.392 ± 0.003M/s ( 0.462M/s/cpu) uprobe-nop (24 cpus): 5.326 ± 0.178M/s ( 0.222M/s/cpu) uprobe-nop (32 cpus): 5.426 ± 0.059M/s ( 0.170M/s/cpu) uprobe-nop (40 cpus): 5.262 ± 0.070M/s ( 0.132M/s/cpu) uprobe-nop (48 cpus): 6.121 ± 0.010M/s ( 0.128M/s/cpu) uprobe-nop (56 cpus): 6.252 ± 0.035M/s ( 0.112M/s/cpu) uprobe-nop (64 cpus): 7.644 ± 0.023M/s ( 0.119M/s/cpu) uprobe-nop (72 cpus): 7.781 ± 0.001M/s ( 0.108M/s/cpu) uprobe-nop (80 cpus): 8.992 ± 0.048M/s ( 0.112M/s/cpu) AFTER ===== uprobe-nop ( 1 cpus): 3.534 ± 0.033M/s ( 3.534M/s/cpu) uprobe-nop ( 2 cpus): 6.701 ± 0.007M/s ( 3.351M/s/cpu) uprobe-nop ( 3 cpus): 10.031 ± 0.007M/s ( 3.344M/s/cpu) uprobe-nop ( 4 cpus): 13.003 ± 0.012M/s ( 3.251M/s/cpu) uprobe-nop ( 5 cpus): 16.274 ± 0.006M/s ( 3.255M/s/cpu) uprobe-nop ( 6 cpus): 19.563 ± 0.024M/s ( 3.261M/s/cpu) uprobe-nop ( 7 cpus): 22.696 ± 0.054M/s ( 3.242M/s/cpu) uprobe-nop ( 8 cpus): 24.534 ± 0.010M/s ( 3.067M/s/cpu) uprobe-nop (10 cpus): 30.475 ± 0.117M/s ( 3.047M/s/cpu) uprobe-nop (12 cpus): 33.371 ± 0.017M/s ( 2.781M/s/cpu) uprobe-nop (14 cpus): 38.864 ± 0.004M/s ( 2.776M/s/cpu) uprobe-nop (16 cpus): 41.476 ± 0.020M/s ( 2.592M/s/cpu) uprobe-nop (24 cpus): 64.696 ± 0.021M/s ( 2.696M/s/cpu) uprobe-nop (32 cpus): 85.054 ± 0.027M/s ( 2.658M/s/cpu) uprobe-nop (40 cpus): 101.979 ± 0.032M/s ( 2.549M/s/cpu) uprobe-nop (48 cpus): 110.518 ± 0.056M/s ( 2.302M/s/cpu) uprobe-nop (56 cpus): 117.737 ± 0.020M/s ( 2.102M/s/cpu) uprobe-nop (64 cpus): 124.613 ± 0.079M/s ( 1.947M/s/cpu) uprobe-nop (72 cpus): 133.239 ± 0.032M/s ( 1.851M/s/cpu) uprobe-nop (80 cpus): 142.037 ± 0.138M/s ( 1.775M/s/cpu) Previously total throughput was maxing out at 11mln/s, and gradually declining past 8 cores. With this change, it now keeps growing with each added CPU, reaching 142mln/s at 80 CPUs (this was measured on a 80-core Intel(R) Xeon(R) Gold 6138 CPU @ 2.00GHz). Suggested-by: Matthew Wilcox Signed-off-by: Andrii Nakryiko --- kernel/events/uprobes.c | 51 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index a2e6a57f79f2..b7e0baa83de1 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -2081,6 +2081,53 @@ static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr) return is_trap_insn(&opcode); } +static struct uprobe *find_active_uprobe_speculative(unsigned long bp_vaddr) +{ + const vm_flags_t flags = VM_HUGETLB | VM_MAYEXEC | VM_MAYSHARE; + struct mm_struct *mm = current->mm; + struct uprobe *uprobe; + struct vm_area_struct *vma; + struct file *vm_file; + struct inode *vm_inode; + unsigned long vm_pgoff, vm_start; + int seq; + loff_t offset; + + if (!mmap_lock_speculation_start(mm, &seq)) + return NULL; + + rcu_read_lock(); + + vma = vma_lookup(mm, bp_vaddr); + if (!vma) + goto bail; + + vm_file = data_race(vma->vm_file); + if (!vm_file || (vma->vm_flags & flags) != VM_MAYEXEC) + goto bail; + + vm_inode = data_race(vm_file->f_inode); + vm_pgoff = data_race(vma->vm_pgoff); + vm_start = data_race(vma->vm_start); + + offset = (loff_t)(vm_pgoff << PAGE_SHIFT) + (bp_vaddr - vm_start); + uprobe = find_uprobe_rcu(vm_inode, offset); + if (!uprobe) + goto bail; + + /* now double check that nothing about MM changed */ + if (!mmap_lock_speculation_end(mm, seq)) + goto bail; + + rcu_read_unlock(); + + /* happy case, we speculated successfully */ + return uprobe; +bail: + rcu_read_unlock(); + return NULL; +} + /* assumes being inside RCU protected region */ static struct uprobe *find_active_uprobe_rcu(unsigned long bp_vaddr, int *is_swbp) { @@ -2088,6 +2135,10 @@ static struct uprobe *find_active_uprobe_rcu(unsigned long bp_vaddr, int *is_swb struct uprobe *uprobe = NULL; struct vm_area_struct *vma; + uprobe = find_active_uprobe_speculative(bp_vaddr); + if (uprobe) + return uprobe; + mmap_read_lock(mm); vma = vma_lookup(mm, bp_vaddr); if (vma) {