Skip to content

Commit ef69422

Browse files
aet00torvalds
authored andcommitted
ksm: cleanup: introduce find_mergeable_vma()
There are multiple places which perform the same check. Add a new find_mergeable_vma() to handle this. Signed-off-by: Bob Liu <lliubbo@gmail.com> Acked-by: Hugh Dickins <hughd@google.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent cc9a6c8 commit ef69422

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

mm/ksm.c

+19-15
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,20 @@ static int break_ksm(struct vm_area_struct *vma, unsigned long addr)
374374
return (ret & VM_FAULT_OOM) ? -ENOMEM : 0;
375375
}
376376

377+
static struct vm_area_struct *find_mergeable_vma(struct mm_struct *mm,
378+
unsigned long addr)
379+
{
380+
struct vm_area_struct *vma;
381+
if (ksm_test_exit(mm))
382+
return NULL;
383+
vma = find_vma(mm, addr);
384+
if (!vma || vma->vm_start > addr)
385+
return NULL;
386+
if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
387+
return NULL;
388+
return vma;
389+
}
390+
377391
static void break_cow(struct rmap_item *rmap_item)
378392
{
379393
struct mm_struct *mm = rmap_item->mm;
@@ -387,15 +401,9 @@ static void break_cow(struct rmap_item *rmap_item)
387401
put_anon_vma(rmap_item->anon_vma);
388402

389403
down_read(&mm->mmap_sem);
390-
if (ksm_test_exit(mm))
391-
goto out;
392-
vma = find_vma(mm, addr);
393-
if (!vma || vma->vm_start > addr)
394-
goto out;
395-
if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
396-
goto out;
397-
break_ksm(vma, addr);
398-
out:
404+
vma = find_mergeable_vma(mm, addr);
405+
if (vma)
406+
break_ksm(vma, addr);
399407
up_read(&mm->mmap_sem);
400408
}
401409

@@ -421,12 +429,8 @@ static struct page *get_mergeable_page(struct rmap_item *rmap_item)
421429
struct page *page;
422430

423431
down_read(&mm->mmap_sem);
424-
if (ksm_test_exit(mm))
425-
goto out;
426-
vma = find_vma(mm, addr);
427-
if (!vma || vma->vm_start > addr)
428-
goto out;
429-
if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
432+
vma = find_mergeable_vma(mm, addr);
433+
if (!vma)
430434
goto out;
431435

432436
page = follow_page(vma, addr, FOLL_GET);

0 commit comments

Comments
 (0)