From nobody Wed Nov 27 08:37:08 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 239BC1CF5C5; Thu, 10 Oct 2024 20:56:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728593811; cv=none; b=O6viZ3zeckc3bzb+dL0EPzsjg8ux/wIMc0SilcWHpQCD3wNDqg/7lEwcSD7vnzdmKFzdbGGAFs+6Z2Ub60d4ua+HF45CEYEueR516y0iXCR4x707ZFW52eweGyHsMTSvn47mgFeGycX4u6G2KZe3+BFKh9zCHLLwubQmxMHjM4Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728593811; c=relaxed/simple; bh=wgt1aG7beo2Dv30obPLfAmhHU07l2a/OUlPnAMEzc64=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=guA6JHlRJ+SWD7cWJAHStbymfMLunwdRoKKntP5hLFZruFil5AjBrX9A3EsAG7tfEw0NoRhEPkmW+FopssyhatAYW5Gc/ta0tKcc+m4X7QyxZPcCRg9xhUZIUZ1qUFl5UyE9w1PlH6f9yjQ6OAzwT3B7nc1zL1EJdQCW0CUs7A4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=T/xpkIms; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="T/xpkIms" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8D916C4CECD; Thu, 10 Oct 2024 20:56:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728593810; bh=wgt1aG7beo2Dv30obPLfAmhHU07l2a/OUlPnAMEzc64=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T/xpkIms4/XteSjXOaghmqaQVxUthT47UHNM+oFCsckR7E3/ssFpC2mX6GbNYFNua c7GYJ/tay3Y31AM2Jrz+78QGi+BakcO2VBszuXhoE4TXE59jNBFRf5HbCii/XHY6Ju ZBJ7hzr4rp3ZbFkl0pAnhZKpDLNb5QPBiJXoCdgnt25FPcKDuiv9Fhn0on1J0/mcKc 8q6+B1AGFdV5jmva2MTrzj+PC0PcCptrwoFcIbfCWr43dCbkFE6F2qiAV3X8kN6SiB ht+oDb8FkkNv9pFXMfH8vUswVFgouEPlDaH6zjpszRYATw0AjV+4axBpdKG7KNpCoY Z16vjigxFwOfg== From: Andrii Nakryiko To: linux-trace-kernel@vger.kernel.org, linux-mm@kvack.org, peterz@infradead.org Cc: oleg@redhat.com, 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, 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, Andrii Nakryiko Subject: [PATCH v3 tip/perf/core 1/4] mm: introduce mmap_lock_speculation_{start|end} Date: Thu, 10 Oct 2024 13:56:41 -0700 Message-ID: <20241010205644.3831427-2-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241010205644.3831427-1-andrii@kernel.org> References: <20241010205644.3831427-1-andrii@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Suggested-by: Peter Zijlstra Signed-off-by: Suren Baghdasaryan Signed-off-by: Andrii Nakryiko Link: https://lore.kernel.org/bpf/20240912210222.186542-1-surenb@google.com Reviewed-by: Shakeel Butt --- include/linux/mm_types.h | 3 ++ include/linux/mmap_lock.h | 72 ++++++++++++++++++++++++++++++++------- kernel/fork.c | 3 -- 3 files changed, 63 insertions(+), 15 deletions(-) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 6e3bdf8e38bc..5d8cdebd42bc 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -887,6 +887,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..9d23635bc701 100644 --- a/include/linux/mmap_lock.h +++ b/include/linux/mmap_lock.h @@ -71,39 +71,84 @@ static inline void mmap_assert_write_locked(const struc= t mm_struct *mm) } =20 #ifdef CONFIG_PER_VMA_LOCK +static inline void init_mm_lock_seq(struct mm_struct *mm) +{ + mm->mm_lock_seq =3D 0; +} + /* - * Drop all currently-held per-VMA locks. - * This is called from the mmap_lock implementation directly before releas= ing - * 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 r= elease - * *all* VMA write locks, including ones from further up the stack. + * Increment mm->mm_lock_seq when mmap_lock is write-locked (ACQUIRE seman= tics) + * or write-unlocked (RELEASE semantics). */ -static inline void vma_end_write_all(struct mm_struct *mm) +static inline void inc_mm_lock_seq(struct mm_struct *mm, bool acquire) { mmap_assert_write_locked(mm); /* * Nobody can concurrently modify mm->mm_lock_seq due to exclusive * mmap_lock being held. - * We need RELEASE semantics here to ensure that preceding stores into - * the VMA take effect before we unlock it with this store. - * Pairs with ACQUIRE semantics in vma_start_read(). */ - smp_store_release(&mm->mm_lock_seq, mm->mm_lock_seq + 1); + + if (acquire) { + WRITE_ONCE(mm->mm_lock_seq, mm->mm_lock_seq + 1); + /* + * For ACQUIRE semantics we should ensure no following stores are + * reordered to appear before the mm->mm_lock_seq modification. + */ + smp_wmb(); + } else { + /* + * We need RELEASE semantics here to ensure that preceding stores + * into the VMA take effect before we unlock it with this store. + * Pairs with ACQUIRE semantics in vma_start_read(). + */ + 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 =3D smp_load_acquire(&mm->mm_lock_seq); + /* Allow speculation if mmap_lock is not write-locked */ + return (*seq & 1) =3D=3D 0; +} + +static inline bool mmap_lock_speculation_end(struct mm_struct *mm, int seq) +{ + /* Pairs with ACQUIRE semantics in inc_mm_lock_seq(). */ + smp_rmb(); + return seq =3D=3D READ_ONCE(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, bool acquire) {} +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 =20 +/* + * Drop all currently-held per-VMA locks. + * This is called from the mmap_lock implementation directly before releas= ing + * a write-locked mmap_lock (or downgrading it to read-locked). + * This should NOT be called manually from other places. + */ +static inline void vma_end_write_all(struct mm_struct *mm) +{ + inc_mm_lock_seq(mm, false); +} + static inline void mmap_init_lock(struct mm_struct *mm) { init_rwsem(&mm->mmap_lock); + init_mm_lock_seq(mm); } =20 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, true); __mmap_lock_trace_acquire_returned(mm, true, true); } =20 @@ -111,6 +156,7 @@ static inline void mmap_write_lock_nested(struct mm_str= uct *mm, int subclass) { __mmap_lock_trace_start_locking(mm, true); down_write_nested(&mm->mmap_lock, subclass); + inc_mm_lock_seq(mm, true); __mmap_lock_trace_acquire_returned(mm, true, true); } =20 @@ -120,6 +166,8 @@ static inline int mmap_write_lock_killable(struct mm_st= ruct *mm) =20 __mmap_lock_trace_start_locking(mm, true); ret =3D down_write_killable(&mm->mmap_lock); + if (!ret) + inc_mm_lock_seq(mm, true); __mmap_lock_trace_acquire_returned(mm, true, ret =3D=3D 0); return ret; } diff --git a/kernel/fork.c b/kernel/fork.c index 89ceb4a68af2..dd1bded0294d 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1261,9 +1261,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 =3D 0; -#endif mm_pgtables_bytes_init(mm); mm->map_count =3D 0; mm->locked_vm =3D 0; --=20 2.43.5 From nobody Wed Nov 27 08:37:08 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F6561E2029; Thu, 10 Oct 2024 20:56:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728593814; cv=none; b=Dmd11ZMwl9Ei1ghJeKs1I/LiO04z1dPaVvtca2RveJFdjfRTONfut3Bl7TA6sEgXFIk9C2YZv4OLCQ3Ta9lTSjedXXb6kgQBfoz9cEfUy4szXjHh1C+KhtdQHkH+uXej/bznpVsa3Y4X2BxkfBsUSqQgfLBeJ8NmJ9pXcXqu1Qw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728593814; c=relaxed/simple; bh=Fclxig+1EAtAWEBIe+FzYZRXWMXhfE7OvVxsNaE1DGc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=L4a1w6q+OddA/uUe5cgCGrt7iap6c6gioGGHT4YJG03C3o2+lQ2iouP7fBLMP5tlMlYrjoYHWq0AoucReRq0bkn6MsD6ti5EidEvShzP7l4zFD0AXskZf3n15vN9N2JG+3csiHFj1iK0Qlx+VZKHf0rHhSd6o6nwGYiqkjqBb0A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rIJtnOw3; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rIJtnOw3" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CF2A2C4CECD; Thu, 10 Oct 2024 20:56:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728593814; bh=Fclxig+1EAtAWEBIe+FzYZRXWMXhfE7OvVxsNaE1DGc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rIJtnOw3slAN3+Rawp25Ab7zwSbPGytnf251F+P9dJp6mu+wi/4bvnkUAD61lBZ+/ WiAu/Sv6kxIzrTRke67spN6D/KVkdsbex8Ahhuy9n+F4/ehSWhLtH+BytOQANs7lrb aONXzNWSCTZnJqFgqCP2i/b98GyBTchxH27Zw2pzlsoAgiRRdUty410qoKQcz+Xgn0 hR6S9fDgI4MghcYQ6mWM2w1TocGDnVZEg+S0ok1Wz7LhFGV3nzu3/SGSj6eoncF57G n9m1cte7bZ3wjUSFSd8j6GY78XZnXoaezm3DCmAjB6uJkKA7R6z0X3iGMac6OnqpgQ fl7T/DexrVneg== From: Andrii Nakryiko To: linux-trace-kernel@vger.kernel.org, linux-mm@kvack.org, peterz@infradead.org Cc: oleg@redhat.com, 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, 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, Andrii Nakryiko Subject: [PATCH v3 tip/perf/core 2/4] mm: switch to 64-bit mm_lock_seq/vm_lock_seq on 64-bit architectures Date: Thu, 10 Oct 2024 13:56:42 -0700 Message-ID: <20241010205644.3831427-3-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241010205644.3831427-1-andrii@kernel.org> References: <20241010205644.3831427-1-andrii@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To increase mm->mm_lock_seq robustness, switch it from int to long, so that it's a 64-bit counter on 64-bit systems and we can stop worrying about it wrapping around in just ~4 billion iterations. Same goes for VMA's matching vm_lock_seq, which is derived from mm_lock_seq. I didn't use __u64 outright to keep 32-bit architectures unaffected, but if it seems important enough, I have nothing against using __u64. Suggested-by: Jann Horn Signed-off-by: Andrii Nakryiko Reviewed-by: Shakeel Butt Reviewed-by: Suren Baghdasaryan --- include/linux/mm.h | 6 +++--- include/linux/mm_types.h | 4 ++-- include/linux/mmap_lock.h | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index ecf63d2b0582..97819437832e 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -730,7 +730,7 @@ static inline void vma_end_read(struct vm_area_struct *= vma) } =20 /* WARNING! Can only be used if mmap_lock is expected to be write-locked */ -static bool __is_vma_write_locked(struct vm_area_struct *vma, int *mm_lock= _seq) +static bool __is_vma_write_locked(struct vm_area_struct *vma, long *mm_loc= k_seq) { mmap_assert_write_locked(vma->vm_mm); =20 @@ -749,7 +749,7 @@ static bool __is_vma_write_locked(struct vm_area_struct= *vma, int *mm_lock_seq) */ static inline void vma_start_write(struct vm_area_struct *vma) { - int mm_lock_seq; + long mm_lock_seq; =20 if (__is_vma_write_locked(vma, &mm_lock_seq)) return; @@ -767,7 +767,7 @@ static inline void vma_start_write(struct vm_area_struc= t *vma) =20 static inline void vma_assert_write_locked(struct vm_area_struct *vma) { - int mm_lock_seq; + long mm_lock_seq; =20 VM_BUG_ON_VMA(!__is_vma_write_locked(vma, &mm_lock_seq), vma); } diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 5d8cdebd42bc..0dc57d6cfe38 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -715,7 +715,7 @@ struct vm_area_struct { * counter reuse can only lead to occasional unnecessary use of the * slowpath. */ - int vm_lock_seq; + long vm_lock_seq; /* Unstable RCU readers are allowed to read this. */ struct vma_lock *vm_lock; #endif @@ -898,7 +898,7 @@ struct mm_struct { * Can be read with ACQUIRE semantics if not holding write * mmap_lock. */ - int mm_lock_seq; + long mm_lock_seq; #endif =20 =20 diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h index 9d23635bc701..f8fd6d879aa9 100644 --- a/include/linux/mmap_lock.h +++ b/include/linux/mmap_lock.h @@ -105,7 +105,7 @@ static inline void inc_mm_lock_seq(struct mm_struct *mm= , bool acquire) } } =20 -static inline bool mmap_lock_speculation_start(struct mm_struct *mm, int *= seq) +static inline bool mmap_lock_speculation_start(struct mm_struct *mm, long = *seq) { /* Pairs with RELEASE semantics in inc_mm_lock_seq(). */ *seq =3D smp_load_acquire(&mm->mm_lock_seq); @@ -113,7 +113,7 @@ static inline bool mmap_lock_speculation_start(struct m= m_struct *mm, int *seq) return (*seq & 1) =3D=3D 0; } =20 -static inline bool mmap_lock_speculation_end(struct mm_struct *mm, int seq) +static inline bool mmap_lock_speculation_end(struct mm_struct *mm, long se= q) { /* Pairs with ACQUIRE semantics in inc_mm_lock_seq(). */ smp_rmb(); @@ -123,8 +123,8 @@ static inline bool mmap_lock_speculation_end(struct mm_= struct *mm, int seq) #else static inline void init_mm_lock_seq(struct mm_struct *mm) {} static inline void inc_mm_lock_seq(struct mm_struct *mm, bool acquire) {} -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; } +static inline bool mmap_lock_speculation_start(struct mm_struct *mm, long = *seq) { return false; } +static inline bool mmap_lock_speculation_end(struct mm_struct *mm, long se= q) { return false; } #endif =20 /* --=20 2.43.5 From nobody Wed Nov 27 08:37:08 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B49741E260D; Thu, 10 Oct 2024 20:56:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728593817; cv=none; b=AnGj4l/G0B1288kZArbWXlL1gk1IMfchLeTg9c0cMQ7khX48pIxm6R6kAa56D7s52Uteo/30u3ViMkS9dnST9Qrj5GntMNos1IwRVMn7gIKgGvsjobTQCCJG9xf9tvo4ck/0YeVCRUlwncNaeRSRHLGj7UP6VvLsC9z5eJxFr8o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728593817; c=relaxed/simple; bh=R2MF70Qe5RyiJ7bapva+CdTRJ3ORlm3QRnJgpBNcwdg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LvL7njcgnEMrU9Os9ym4gJJV/NkJ3ZDuvKY9qIpmT4BV6TQe9umjhPzoEQR+yXRvkpBjKkZFHQxKbfPNQ2n5GeyuV22oHcAg1IVK1FO9oNEgV3NvLReM6RYNHaQ56g8gCrsNwuIVqHiCBi9GQuOarZreslaK2gqpXJkqYPjHp/I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OdE/XF1b; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="OdE/XF1b" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 22F7FC4CEC5; Thu, 10 Oct 2024 20:56:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728593817; bh=R2MF70Qe5RyiJ7bapva+CdTRJ3ORlm3QRnJgpBNcwdg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OdE/XF1bptJss+RjCSwJXgar0qqj6hZ1g1s6lERBGX2VMmD6i1AXUd2ErsiARXGNW YRD21MpcXIx2VN8huDFgvMAkI2ywlHm1TLyrzAqmANnFxbUj513wQorX4kzCCbnNRF GA4pK3Mwa3/NB+GEQOQe8KCIJK85CcSZMGYtSaHpdZWuYJ7DbHT9VKA9XLmfNXtAXz BM2DzYEHyr6b1/yP6r6vqwCwOgdbFLq3QZ+VYIPzk4x+4WVdjutVExOVZR/DO9yjpN lyRpste/timi9kH9OupllEhR2R79MWCKf6hny5yBMJ2rsl4aUU6KtsLlmgq+io3mO2 gOytO7LwqRWfA== From: Andrii Nakryiko To: linux-trace-kernel@vger.kernel.org, linux-mm@kvack.org, peterz@infradead.org Cc: oleg@redhat.com, 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, 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, Andrii Nakryiko Subject: [PATCH v3 tip/perf/core 3/4] uprobes: simplify find_active_uprobe_rcu() VMA checks Date: Thu, 10 Oct 2024 13:56:43 -0700 Message-ID: <20241010205644.3831427-4-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241010205644.3831427-1-andrii@kernel.org> References: <20241010205644.3831427-1-andrii@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" At the point where find_active_uprobe_rcu() is used we know that VMA in question has triggered software breakpoint, so we don't need to validate vma->vm_flags. Keep only vma->vm_file NULL check. Acked-by: Oleg Nesterov Suggested-by: Oleg Nesterov Signed-off-by: Andrii Nakryiko --- kernel/events/uprobes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index 2a0059464383..fa1024aad6c4 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -2057,7 +2057,7 @@ static struct uprobe *find_active_uprobe_rcu(unsigned= long bp_vaddr, int *is_swb mmap_read_lock(mm); vma =3D vma_lookup(mm, bp_vaddr); if (vma) { - if (valid_vma(vma, false)) { + if (vma->vm_file) { struct inode *inode =3D file_inode(vma->vm_file); loff_t offset =3D vaddr_to_offset(vma, bp_vaddr); =20 --=20 2.43.5 From nobody Wed Nov 27 08:37:08 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AE7BA1E7C0D; Thu, 10 Oct 2024 20:57:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728593820; cv=none; b=e6NvkEgXsY67T1mc9cM9h1Df+umjz1hdtggjl4PnQ70vTZPQuci0CP89sRetJxghzjNiHAosAoo9tJMEsm0PQ7FdcaiAXlHfwIHnpDB8PteM/npkUy1/tKOuvpXaA8lebsDhWzfkMG6JfqYia0wVTDjWdC4JWe6RydCwxQb4pLs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728593820; c=relaxed/simple; bh=HJW3q0theOLZJq7cr7mtCh0i3gb8IviMxWPJ1EUCK8Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=guNOyMcTrzcmdUStIPtmSuMZVU2hL0FCqOyMoIi78N4IQ9Ly80hw/O+W9Rn+HFzqVEP8G/Sp6ho4oVnnJuilvFTa3jqWr3vXzrghcMrrMgpU6yMPfpB8tVYIrletc2Aq/6VN9HTkZFFZTpJsS/e7xhnd/hVJ1hZIQHTDUbQl25s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=M9r9qtsY; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="M9r9qtsY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5A9CDC4CEC5; Thu, 10 Oct 2024 20:57:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728593820; bh=HJW3q0theOLZJq7cr7mtCh0i3gb8IviMxWPJ1EUCK8Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=M9r9qtsYZGd4X6+5xoplvdRL+ZFYzemjolVpIJwvK0szlb1y4gDtezvN8agSMJscW l4n9x4peZxFi5llEIv76G3TpR+pfmzHPzF2NSoaw2YIhGup95xfoeAm4oRZCF7ayw5 bbGSHUhxT8S9BdKGdGHM4xEO+L/XOQx4hrEnt4f1tip4yJVtIWocycxX5JlRZlQtY/ Jq4d4xgAK681mlSZf1Ska7NNLb7oxoBj4XXqlR1yGxHQs2lnLVsIAqcg9xwLWO8jBt Ii17mcAKVvPskH8m79ilR+9AuuycUxN0LXsNDSe98+NQxOZIBxERYE+M6SLXHueeUD P+CEOcJbWed1g== From: Andrii Nakryiko To: linux-trace-kernel@vger.kernel.org, linux-mm@kvack.org, peterz@infradead.org Cc: oleg@redhat.com, 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, 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, Andrii Nakryiko Subject: [PATCH v3 tip/perf/core 4/4] uprobes: add speculative lockless VMA-to-inode-to-uprobe resolution Date: Thu, 10 Oct 2024 13:56:44 -0700 Message-ID: <20241010205644.3831427-5-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241010205644.3831427-1-andrii@kernel.org> References: <20241010205644.3831427-1-andrii@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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 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) =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 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 Reviewed-by: Oleg Nesterov --- kernel/events/uprobes.c | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index fa1024aad6c4..9dc6e78975c9 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -2047,6 +2047,52 @@ static int is_trap_at_addr(struct mm_struct *mm, uns= igned long vaddr) return is_trap_insn(&opcode); } =20 +static struct uprobe *find_active_uprobe_speculative(unsigned long bp_vadd= r) +{ + struct mm_struct *mm =3D current->mm; + struct uprobe *uprobe =3D NULL; + struct vm_area_struct *vma; + struct file *vm_file; + struct inode *vm_inode; + unsigned long vm_pgoff, vm_start; + loff_t offset; + long seq; + + guard(rcu)(); + + if (!mmap_lock_speculation_start(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 fields from + * it, even if the values are some garbage values; ultimately + * find_uprobe_rcu() + mmap_lock_speculation_end() check will ensure + * that whatever we speculatively found is correct + */ + vm_file =3D READ_ONCE(vma->vm_file); + if (!vm_file) + return NULL; + + vm_pgoff =3D data_race(vma->vm_pgoff); + vm_start =3D data_race(vma->vm_start); + vm_inode =3D data_race(vm_file->f_inode); + + offset =3D (loff_t)(vm_pgoff << PAGE_SHIFT) + (bp_vaddr - vm_start); + uprobe =3D find_uprobe_rcu(vm_inode, offset); + 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) { @@ -2054,6 +2100,10 @@ static struct uprobe *find_active_uprobe_rcu(unsigne= d long bp_vaddr, int *is_swb struct uprobe *uprobe =3D NULL; struct vm_area_struct *vma; =20 + 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) { --=20 2.43.5