2023-12-07 09:49:24

by Alexander Gordeev

[permalink] [raw]
Subject: [PATCH] mm: do not protect VMA lock object in vma_end_read()

In early discussion to the implementation of vma_end_read()
Jann Horn pointed out that up_read() could access the VMA
lock object after it has already been acquired by someone
else. As result, up_read() is protected with RCU read lock:

rcu_read_lock(); /* keeps vma alive */
up_read(&vma->lock);
rcu_read_unlock();

Since commit 3f5245538a19 ("locking/rwsem: Disable preemption
in all down_read*() and up_read() code paths") __up_read()
disables preemption internally and thus the need to protect
the VMA lock object does not exist anymore.

Link: https://lore.kernel.org/all/CAG48ez3sCwasFzKD5CsqMFA2W57-2fazd75g7r0NaA_BVNTLow@mail.gmail.com/
Cc: Jann Horn <[email protected]>
Cc: Suren Baghdasaryan <[email protected]>
Signed-off-by: Alexander Gordeev <[email protected]>
---
include/linux/mm.h | 2 --
1 file changed, 2 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 418d26608ece..7b32bc75a4ab 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -683,9 +683,7 @@ static inline bool vma_start_read(struct vm_area_struct *vma)

static inline void vma_end_read(struct vm_area_struct *vma)
{
- rcu_read_lock(); /* keeps vma alive till the end of up_read */
up_read(&vma->vm_lock->lock);
- rcu_read_unlock();
}

/* WARNING! Can only be used if mmap_lock is expected to be write-locked */
--
2.40.1


2023-12-07 14:01:07

by Jann Horn

[permalink] [raw]
Subject: Re: [PATCH] mm: do not protect VMA lock object in vma_end_read()

On Thu, Dec 7, 2023 at 10:49 AM Alexander Gordeev
<[email protected]> wrote:
> In early discussion to the implementation of vma_end_read()
> Jann Horn pointed out that up_read() could access the VMA
> lock object after it has already been acquired by someone
> else. As result, up_read() is protected with RCU read lock:
>
> rcu_read_lock(); /* keeps vma alive */
> up_read(&vma->lock);
> rcu_read_unlock();
>
> Since commit 3f5245538a19 ("locking/rwsem: Disable preemption
> in all down_read*() and up_read() code paths") __up_read()
> disables preemption internally and thus the need to protect
> the VMA lock object does not exist anymore.

I think this is a bad idea. Please don't.

Yes, it looks like the (non-RT) implementation of __up_read
*currently* disables preemption. But that's an implementation detail,
not a documented API contract of up_read(), so there would be nothing
stopping someone from reimplementing __up_read() in the future such
that the preemption stuff disappears again.

And from what I can tell from a quick look, the RT implementation of
__up_read() does not currently give you this kind of guarantee.

In my opinion, if you want to make this change, then as a prerequisite
you have to get buy-in from the locking maintainers.


> Link: https://lore.kernel.org/all/CAG48ez3sCwasFzKD5CsqMFA2W57-2fazd75g7r0NaA_BVNTLow@mail.gmail.com/
> Cc: Jann Horn <[email protected]>
> Cc: Suren Baghdasaryan <[email protected]>
> Signed-off-by: Alexander Gordeev <[email protected]>
> ---
> include/linux/mm.h | 2 --
> 1 file changed, 2 deletions(-)
>
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 418d26608ece..7b32bc75a4ab 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -683,9 +683,7 @@ static inline bool vma_start_read(struct vm_area_struct *vma)
>
> static inline void vma_end_read(struct vm_area_struct *vma)
> {
> - rcu_read_lock(); /* keeps vma alive till the end of up_read */
> up_read(&vma->vm_lock->lock);
> - rcu_read_unlock();
> }
>
> /* WARNING! Can only be used if mmap_lock is expected to be write-locked */
> --
> 2.40.1
>