From mboxrd@z Thu Jan 1 00:00:00 1970 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 9ABC6D42BBE for ; Tue, 12 Nov 2024 18:10:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3497A6B00F5; Tue, 12 Nov 2024 13:10:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2D9E76B00F6; Tue, 12 Nov 2024 13:10:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 14A206B00F7; Tue, 12 Nov 2024 13:10:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id E101B6B00F5 for ; Tue, 12 Nov 2024 13:10:14 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 9EDCB140596 for ; Tue, 12 Nov 2024 18:10:14 +0000 (UTC) X-FDA: 82778230752.25.0683D83 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) by imf28.hostedemail.com (Postfix) with ESMTP id D8722C0021 for ; Tue, 12 Nov 2024 18:09:29 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KFg5nuID; spf=pass (imf28.hostedemail.com: domain of andrii.nakryiko@gmail.com designates 209.85.215.179 as permitted sender) smtp.mailfrom=andrii.nakryiko@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731434870; 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=i6RgqeDH1wVQOoWDzTtohzRu8Xfe2E7upOTxZntw7p0=; b=WeZWHLwfdAOOCAljwTXTnVQ4MNe4qnclz1W9MT6tXDg4ycCuLvH1n4GwX18hC1s2Atz56V 0hrAZ3k7zFXal0+fm91knW1fChvYSwETC7I5f30LtNGtLUQ4zjlD68VblAvEtObo+csMlT 77mPYhlI6aRKEUobnvAxHeb36y+4UOA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731434870; a=rsa-sha256; cv=none; b=iqlNpbx8R6TgAKvyNEvvMNP1eYByHQMA/oiamdqGYFRBv4RJCvfuLbIPwsb56qDvSRD2V9 ny2C9nJnsHTM0hT+9rhp/l4nT2lfxTVc15e1YaBsJ6gDICD4E2fxHVqtvglUWQRRXW80z5 7jxfNgO8WHVP+pT633/P4KWiCGq6+RM= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KFg5nuID; spf=pass (imf28.hostedemail.com: domain of andrii.nakryiko@gmail.com designates 209.85.215.179 as permitted sender) smtp.mailfrom=andrii.nakryiko@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-7e6cbf6cd1dso3921355a12.3 for ; Tue, 12 Nov 2024 10:10:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731435011; x=1732039811; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=i6RgqeDH1wVQOoWDzTtohzRu8Xfe2E7upOTxZntw7p0=; b=KFg5nuIDZ5r8KHaLfV62/2cNo0/0Amvq/cFAnKeoS90SqCrq6ADZUt0+poiT+/FKbx 1UAFlbwboE0r4f95qiPVjoPcW42nnDeomPFYfwZd34PEKRPDywO68YfAmbEE9uEIBbke GFPzfBHo7l2bX+8rUgaV5j/6d3ZuRmObF4aXeqMthIxQKdzObeTBbLYJyUWgnCurcpAB 9O+zSWR3Lp/FSlK3eTt+MGnnFlJSpLBRNPyGC70aqmYNXtRLMsqrdzNCJbBF0jGNGtXB 3gwNOFFj0bQv6P2LGUIk8GoejUvs3pbLvUShM1Fj81LZs+UglQPNVqsg+IHsmQq5xWqL iTQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731435011; x=1732039811; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=i6RgqeDH1wVQOoWDzTtohzRu8Xfe2E7upOTxZntw7p0=; b=OYvOd75imR6RqB3pKGmlYP33TSHUeB25Q38aKOMzvSUr03YhcF7YroMxIbArnfT5t9 jpBz0TJXxQhyEewy8CF/7Ho4iS5jXMEvFXMsNsMjHopa0d73/XTtUdfisv/djncDmKVX n0P05ZrmQyaiaMZHRY9AzOELNpB3oXeEmsTKo4w+ftCbvJ22WSH7gwliUuqJ97O7PFr1 gRpbCjbMCCGsqtw7WQguCLxV06lFVpRIqsVP1ZQchvTKwtBm5lV0vfFHpOAzcyxSFkpK b89y/Y3NxMG6vUS7Fmu9fMCZJKadhG/9RCXv+FTSd+ThCu6UXy8BgY2kw8CApuFt263D nLUQ== X-Forwarded-Encrypted: i=1; AJvYcCVj92wa0h+nkgALEezE4x/OiILacQwNfD2RaANdY2VWLdJOrHaiKnECv8n7rfO9d5+5HtbcAr6nkA==@kvack.org X-Gm-Message-State: AOJu0YzWPrKu/95RFknl/DGQ6M///gKAn5UsVayibs3owFZdpouIWaEp 9+qhyp2wxV0CVg/vJssy+UOYnnOuE5/6pceNsyGc5ZbpeRkx6BwL2tWFaaCCUkElBZ3xJqEtzZ3 C8ZtvU0/a/F+95U/Dg3E2DpJt4Ko= X-Google-Smtp-Source: AGHT+IFl6MFFzHYffblN+GvtzC1b0S3nhKK4UXwxyrJe3p27dCMFRGYiwzk6JTdRx/iUsGAyUyP+x/dM/zCVqV+Dbtk= X-Received: by 2002:a17:90b:350b:b0:2e8:f58e:27bb with SMTP id 98e67ed59e1d1-2e9e4aa8cadmr4501330a91.8.1731435010936; Tue, 12 Nov 2024 10:10:10 -0800 (PST) MIME-Version: 1.0 References: <20241028010818.2487581-1-andrii@kernel.org> <20241028010818.2487581-5-andrii@kernel.org> <20241112092816.cf5b0aa1ef10f50ce872892f@kernel.org> In-Reply-To: From: Andrii Nakryiko Date: Tue, 12 Nov 2024 10:09:58 -0800 Message-ID: Subject: Re: [PATCH v4 tip/perf/core 4/4] uprobes: add speculative lockless VMA-to-inode-to-uprobe resolution To: Suren Baghdasaryan Cc: Masami Hiramatsu , Andrii Nakryiko , linux-trace-kernel@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, peterz@infradead.org, oleg@redhat.com, rostedt@goodmis.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, jolsa@kernel.org, paulmck@kernel.org, willy@infradead.org, mjguzik@gmail.com, brauner@kernel.org, jannh@google.com, mhocko@kernel.org, vbabka@suse.cz, shakeel.butt@linux.dev, hannes@cmpxchg.org, Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, david@redhat.com, arnd@arndb.de, richard.weiyang@gmail.com, zhangpeng.00@bytedance.com, linmiaohe@huawei.com, viro@zeniv.linux.org.uk, hca@linux.ibm.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: D8722C0021 X-Stat-Signature: tyki16exjecsfhizghud18ntm4oikmyo X-HE-Tag: 1731434969-693658 X-HE-Meta: U2FsdGVkX181NapHNxk0Xp3GQtuK2R9dgEJpqrQDvkaq7RBJg/Y1c7rfrYcbR/IwrHUSO4URhDorCTDbLHOSvlMLnuWryW9BVvQlOZDSwrDR/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CgmFNHevR7g4uF8foBP/judHNsbbRmTHt8rFpLX2OGcoJKPiXtZZqBxcytScM1SNtrtmrcGv3f9CBrCY2nw0w/FKY9QhwGwd4VzEVL5hRH8GHIK9wQFlV4rtSYbzp+83RiNXPGtz09bGYdOBj77rsMWeq+MlzYTcvDjy+RxUqNPY1YqCh4UOJ2IjfTUzqKZHChobn/DZqhUpLLBtcmniSBPcoZnNQ/kmk9clr9qPvAnsU7WhaTzSkWuVb+ERVdgGCejRxhSlGFMjYnn3MmTqo9ZEPFwDlj4QzxNXVF83hn79V8MUsc2G8IT4sY7H6jUZhG9rtZbQz6lJqElZettf0fpk3oKFusvwrNICMyF5kUPO4= 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: On Mon, Nov 11, 2024 at 5:05=E2=80=AFPM Suren Baghdasaryan wrote: > > On Mon, Nov 11, 2024 at 4:28=E2=80=AFPM Masami Hiramatsu wrote: > > > > On Sun, 27 Oct 2024 18:08:18 -0700 > > Andrii Nakryiko wrote: > > > > > Given filp_cachep is marked SLAB_TYPESAFE_BY_RCU (and FMODE_BACKING > > > files, a special case, now goes through RCU-delated freeing), 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, assuming that the= re > > > 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_{begin,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) > > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D > > > uprobe-nop ( 1 cpus): 3.384 =C2=B1 0.004M/s ( 3.384M/= s/cpu) > > > uprobe-nop ( 2 cpus): 5.456 =C2=B1 0.005M/s ( 2.728M/= s/cpu) > > > uprobe-nop ( 3 cpus): 7.863 =C2=B1 0.015M/s ( 2.621M/= s/cpu) > > > uprobe-nop ( 4 cpus): 9.442 =C2=B1 0.008M/s ( 2.360M/= s/cpu) > > > uprobe-nop ( 5 cpus): 11.036 =C2=B1 0.013M/s ( 2.207M/= s/cpu) > > > uprobe-nop ( 6 cpus): 10.884 =C2=B1 0.019M/s ( 1.814M/= s/cpu) > > > uprobe-nop ( 7 cpus): 7.897 =C2=B1 0.145M/s ( 1.128M/= s/cpu) > > > uprobe-nop ( 8 cpus): 10.021 =C2=B1 0.128M/s ( 1.253M/= s/cpu) > > > uprobe-nop (10 cpus): 9.932 =C2=B1 0.170M/s ( 0.993M/= s/cpu) > > > uprobe-nop (12 cpus): 8.369 =C2=B1 0.056M/s ( 0.697M/= s/cpu) > > > uprobe-nop (14 cpus): 8.678 =C2=B1 0.017M/s ( 0.620M/= s/cpu) > > > uprobe-nop (16 cpus): 7.392 =C2=B1 0.003M/s ( 0.462M/= s/cpu) > > > uprobe-nop (24 cpus): 5.326 =C2=B1 0.178M/s ( 0.222M/= s/cpu) > > > uprobe-nop (32 cpus): 5.426 =C2=B1 0.059M/s ( 0.170M/= s/cpu) > > > uprobe-nop (40 cpus): 5.262 =C2=B1 0.070M/s ( 0.132M/= s/cpu) > > > uprobe-nop (48 cpus): 6.121 =C2=B1 0.010M/s ( 0.128M/= s/cpu) > > > uprobe-nop (56 cpus): 6.252 =C2=B1 0.035M/s ( 0.112M/= s/cpu) > > > uprobe-nop (64 cpus): 7.644 =C2=B1 0.023M/s ( 0.119M/= s/cpu) > > > uprobe-nop (72 cpus): 7.781 =C2=B1 0.001M/s ( 0.108M/= s/cpu) > > > uprobe-nop (80 cpus): 8.992 =C2=B1 0.048M/s ( 0.112M/= s/cpu) > > > > > > AFTER > > > =3D=3D=3D=3D=3D > > > uprobe-nop ( 1 cpus): 3.534 =C2=B1 0.033M/s ( 3.534M/= s/cpu) > > > uprobe-nop ( 2 cpus): 6.701 =C2=B1 0.007M/s ( 3.351M/= s/cpu) > > > uprobe-nop ( 3 cpus): 10.031 =C2=B1 0.007M/s ( 3.344M/= s/cpu) > > > uprobe-nop ( 4 cpus): 13.003 =C2=B1 0.012M/s ( 3.251M/= s/cpu) > > > uprobe-nop ( 5 cpus): 16.274 =C2=B1 0.006M/s ( 3.255M/= s/cpu) > > > uprobe-nop ( 6 cpus): 19.563 =C2=B1 0.024M/s ( 3.261M/= s/cpu) > > > uprobe-nop ( 7 cpus): 22.696 =C2=B1 0.054M/s ( 3.242M/= s/cpu) > > > uprobe-nop ( 8 cpus): 24.534 =C2=B1 0.010M/s ( 3.067M/= s/cpu) > > > uprobe-nop (10 cpus): 30.475 =C2=B1 0.117M/s ( 3.047M/= s/cpu) > > > uprobe-nop (12 cpus): 33.371 =C2=B1 0.017M/s ( 2.781M/= s/cpu) > > > uprobe-nop (14 cpus): 38.864 =C2=B1 0.004M/s ( 2.776M/= s/cpu) > > > uprobe-nop (16 cpus): 41.476 =C2=B1 0.020M/s ( 2.592M/= s/cpu) > > > uprobe-nop (24 cpus): 64.696 =C2=B1 0.021M/s ( 2.696M/= s/cpu) > > > uprobe-nop (32 cpus): 85.054 =C2=B1 0.027M/s ( 2.658M/= s/cpu) > > > uprobe-nop (40 cpus): 101.979 =C2=B1 0.032M/s ( 2.549M/= s/cpu) > > > uprobe-nop (48 cpus): 110.518 =C2=B1 0.056M/s ( 2.302M/= s/cpu) > > > uprobe-nop (56 cpus): 117.737 =C2=B1 0.020M/s ( 2.102M/= s/cpu) > > > uprobe-nop (64 cpus): 124.613 =C2=B1 0.079M/s ( 1.947M/= s/cpu) > > > uprobe-nop (72 cpus): 133.239 =C2=B1 0.032M/s ( 1.851M/= s/cpu) > > > uprobe-nop (80 cpus): 142.037 =C2=B1 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 e= ach > > > added CPU, reaching 142mln/s at 80 CPUs (this was measured on a 80-co= re > > > Intel(R) Xeon(R) Gold 6138 CPU @ 2.00GHz). > > > > > > > Looks good to me, except one question below. > > > > > Reviewed-by: Oleg Nesterov > > > Suggested-by: Matthew Wilcox > > > Suggested-by: Peter Zijlstra > > > Signed-off-by: Andrii Nakryiko > > > --- > > > kernel/events/uprobes.c | 45 +++++++++++++++++++++++++++++++++++++++= ++ > > > 1 file changed, 45 insertions(+) > > > > > > diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c > > > index 290c445768fa..efcd62f7051d 100644 > > > --- a/kernel/events/uprobes.c > > > +++ b/kernel/events/uprobes.c > > > @@ -2074,6 +2074,47 @@ static int is_trap_at_addr(struct mm_struct *m= m, unsigned long vaddr) > > > return is_trap_insn(&opcode); > > > } > > > > > > +static struct uprobe *find_active_uprobe_speculative(unsigned long b= p_vaddr) > > > +{ > > > + struct mm_struct *mm =3D current->mm; > > > + struct uprobe *uprobe =3D NULL; > > > + struct vm_area_struct *vma; > > > + struct file *vm_file; > > > + loff_t offset; > > > + unsigned int seq; > > > + > > > + guard(rcu)(); > > > + > > > + if (!mmap_lock_speculation_begin(mm, &seq)) > > > + return NULL; > > > + > > > + vma =3D vma_lookup(mm, bp_vaddr); > > > + if (!vma) > > > + return NULL; > > > + > > > + /* > > > + * vm_file memory can be reused for another instance of struct = file, > > > + * but can't be freed from under us, so it's safe to read field= s from > > > + * it, even if the values are some garbage values; ultimately > > > + * find_uprobe_rcu() + mmap_lock_speculation_end() check will e= nsure > > > + * that whatever we speculatively found is correct > > > > If vm_file is a garbage value, may `vm_file->f_inode` access be dangero= us? > > > > > + */ > > > + vm_file =3D READ_ONCE(vma->vm_file); > > > + if (!vm_file) > > > + return NULL; > > > + > > > + offset =3D (loff_t)(vma->vm_pgoff << PAGE_SHIFT) + (bp_vaddr - = vma->vm_start); > > > + uprobe =3D find_uprobe_rcu(vm_file->f_inode, offset); > > ^^^^ Here > > > > if it only stores vm_file or NULL, there's no problem. > > IIRC correctly, vma->vm_file is RCU-safe and we are in the read RCU > section, so it should not contain a garbage value. Correct. vm_file itself can be either TYPESAFE_BY_RCU for normal files, or properly RCU protected for FMODE_BACKING ones. Either way, there is some correct struct file pointed to, and so all this is valid and won't dereference invalid memory. > > > > > Thank you, > > > > > + if (!uprobe) > > > + return NULL; > > > + > > > + /* now double check that nothing about MM changed */ > > > + if (!mmap_lock_speculation_end(mm, seq)) > > > + return NULL; > > > + > > > + return uprobe; > > > +} > > > + > > > /* assumes being inside RCU protected region */ > > > static struct uprobe *find_active_uprobe_rcu(unsigned long bp_vaddr,= int *is_swbp) > > > { > > > @@ -2081,6 +2122,10 @@ static struct uprobe *find_active_uprobe_rcu(u= nsigned long bp_vaddr, int *is_swb > > > struct uprobe *uprobe =3D NULL; > > > struct vm_area_struct *vma; > > > > > > + uprobe =3D find_active_uprobe_speculative(bp_vaddr); > > > + if (uprobe) > > > + return uprobe; > > > + > > > mmap_read_lock(mm); > > > vma =3D vma_lookup(mm, bp_vaddr); > > > if (vma) { > > > -- > > > 2.43.5 > > > > > > > > > -- > > Masami Hiramatsu (Google)