Skip to content

Commit 4cccb62

Browse files
Muhammad Usama Anjumakpm00
authored andcommitted
fs/proc/task_mmu: move mmu notification mechanism inside mm lock
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: 52526ca ("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>
1 parent ea52f71 commit 4cccb62

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

fs/proc/task_mmu.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,7 +2432,6 @@ static long pagemap_scan_flush_buffer(struct pagemap_scan_private *p)
24322432

24332433
static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
24342434
{
2435-
struct mmu_notifier_range range;
24362435
struct pagemap_scan_private p = {0};
24372436
unsigned long walk_start;
24382437
size_t n_ranges_out = 0;
@@ -2448,15 +2447,9 @@ static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
24482447
if (ret)
24492448
return ret;
24502449

2451-
/* Protection change for the range is going to happen. */
2452-
if (p.arg.flags & PM_SCAN_WP_MATCHING) {
2453-
mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA, 0,
2454-
mm, p.arg.start, p.arg.end);
2455-
mmu_notifier_invalidate_range_start(&range);
2456-
}
2457-
24582450
for (walk_start = p.arg.start; walk_start < p.arg.end;
24592451
walk_start = p.arg.walk_end) {
2452+
struct mmu_notifier_range range;
24602453
long n_out;
24612454

24622455
if (fatal_signal_pending(current)) {
@@ -2467,8 +2460,20 @@ static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
24672460
ret = mmap_read_lock_killable(mm);
24682461
if (ret)
24692462
break;
2463+
2464+
/* Protection change for the range is going to happen. */
2465+
if (p.arg.flags & PM_SCAN_WP_MATCHING) {
2466+
mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA, 0,
2467+
mm, walk_start, p.arg.end);
2468+
mmu_notifier_invalidate_range_start(&range);
2469+
}
2470+
24702471
ret = walk_page_range(mm, walk_start, p.arg.end,
24712472
&pagemap_scan_ops, &p);
2473+
2474+
if (p.arg.flags & PM_SCAN_WP_MATCHING)
2475+
mmu_notifier_invalidate_range_end(&range);
2476+
24722477
mmap_read_unlock(mm);
24732478

24742479
n_out = pagemap_scan_flush_buffer(&p);
@@ -2494,9 +2499,6 @@ static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
24942499
if (pagemap_scan_writeback_args(&p.arg, uarg))
24952500
ret = -EFAULT;
24962501

2497-
if (p.arg.flags & PM_SCAN_WP_MATCHING)
2498-
mmu_notifier_invalidate_range_end(&range);
2499-
25002502
kfree(p.vec_buf);
25012503
return ret;
25022504
}

0 commit comments

Comments
 (0)