]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
fs/proc/task_mmu: move mmu notification mechanism inside mm lock
authorMuhammad Usama Anjum <usama.anjum@collabora.com>
Tue, 9 Jan 2024 11:24:42 +0000 (16:24 +0500)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 1 Feb 2024 00:21:02 +0000 (16:21 -0800)
commit 4cccb6221cae6d020270606b9e52b1678fc8b71a upstream.

Move mmu notification mechanism inside mm lock to prevent race condition
in other components which depend on it.  The notifier will invalidate
memory range.  Depending upon the number of iterations, different memory
ranges would be invalidated.

The following warning would be removed by this patch:
WARNING: CPU: 0 PID: 5067 at arch/x86/kvm/../../../virt/kvm/kvm_main.c:734 kvm_mmu_notifier_change_pte+0x860/0x960 arch/x86/kvm/../../../virt/kvm/kvm_main.c:734

There is no behavioural and performance change with this patch when
there is no component registered with the mmu notifier.

[akpm@linux-foundation.org: narrow the scope of `range', per Sean]
Link: https://lkml.kernel.org/r/20240109112445.590736-1-usama.anjum@collabora.com
Fixes: 52526ca7fdb9 ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs")
Signed-off-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
Reported-by: syzbot+81227d2bd69e9dedb802@syzkaller.appspotmail.com
Link: https://lore.kernel.org/all/000000000000f6d051060c6785bc@google.com/
Reviewed-by: Sean Christopherson <seanjc@google.com>
Cc: Andrei Vagin <avagin@google.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: David Hildenbrand <david@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
Cc: Liam R. Howlett <Liam.Howlett@oracle.com>
Cc: Michał Mirosław <mirq-linux@rere.qmqm.pl>
Cc: Peter Xu <peterx@redhat.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Stephen Rothwell <sfr@canb.auug.org.au>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/proc/task_mmu.c

index 435b61054b5b9e768ac34fafd4bf5a7a2c378f89..4905420d339f4d4e97f0c6a8ca2350f0b98cfe0c 100644 (file)
@@ -2415,7 +2415,6 @@ static long pagemap_scan_flush_buffer(struct pagemap_scan_private *p)
 
 static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
 {
-       struct mmu_notifier_range range;
        struct pagemap_scan_private p = {0};
        unsigned long walk_start;
        size_t n_ranges_out = 0;
@@ -2431,15 +2430,9 @@ static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
        if (ret)
                return ret;
 
-       /* Protection change for the range is going to happen. */
-       if (p.arg.flags & PM_SCAN_WP_MATCHING) {
-               mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA, 0,
-                                       mm, p.arg.start, p.arg.end);
-               mmu_notifier_invalidate_range_start(&range);
-       }
-
        for (walk_start = p.arg.start; walk_start < p.arg.end;
                        walk_start = p.arg.walk_end) {
+               struct mmu_notifier_range range;
                long n_out;
 
                if (fatal_signal_pending(current)) {
@@ -2450,8 +2443,20 @@ static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
                ret = mmap_read_lock_killable(mm);
                if (ret)
                        break;
+
+               /* Protection change for the range is going to happen. */
+               if (p.arg.flags & PM_SCAN_WP_MATCHING) {
+                       mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA, 0,
+                                               mm, walk_start, p.arg.end);
+                       mmu_notifier_invalidate_range_start(&range);
+               }
+
                ret = walk_page_range(mm, walk_start, p.arg.end,
                                      &pagemap_scan_ops, &p);
+
+               if (p.arg.flags & PM_SCAN_WP_MATCHING)
+                       mmu_notifier_invalidate_range_end(&range);
+
                mmap_read_unlock(mm);
 
                n_out = pagemap_scan_flush_buffer(&p);
@@ -2477,9 +2482,6 @@ static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
        if (pagemap_scan_writeback_args(&p.arg, uarg))
                ret = -EFAULT;
 
-       if (p.arg.flags & PM_SCAN_WP_MATCHING)
-               mmu_notifier_invalidate_range_end(&range);
-
        kfree(p.vec_buf);
        return ret;
 }