mm, thp: remove duplication and fix locking issues in swapin
Subsystems (2)
v2 cover mm, thp: remove duplication and fix locking issues in swapin
This patch series removes duplication of included header and fixes locking inconsistency in khugepaged swapin.
Ebru Akagunduz (3):
mm, thp: remove duplication of included header mm, thp: fix possible circular locking dependency caused by
sum_vm_event()
mm, thp: make swapin readahead under down_read of mmap_sem
mm/huge_memory.c | 101 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 68 insertions(+), 33 deletions(-)
Signature · 7 lines
1.9.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v2 1/3 mm, thp: remove duplication of included header
swapops.h included for a second time in the commit:
http://git.kernel.org/cgit/linux/kernel/git/next/linux-next.git/commit/?id=639040960a340f6f987065fc52e149f4ea25ce25
This patch removes the duplication.
---
Changes in v2:
- Nothing changed
mm/huge_memory.c | 1 - 1 file changed, 1 deletion(-)
mm/huge_memory.c +0 -1
@@ -30,7 +30,6 @@#include <linux/hashtable.h> #include <linux/userfaultfd_k.h> #include <linux/page_idle.h> -#include <linux/swapops.h> #include <asm/tlb.h> #include <asm/pgalloc.h>
Signature · 7 lines
1.9.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v2 2/3 mm, thp: fix possible circular locking dependency caused by sum_vm_event()
In reply to Ebru AkagunduzNested circular locking dependency detected by kernel robot (udevadm).
udevadm/221 is trying to acquire lock:
(&mm->mmap_sem){++++++}, at: [<ffffffff81262543>] __might_fault+0x83/0x150
but task is already holding lock:
(s_active#12){++++.+}, at: [<ffffffff813315ee>] kernfs_fop_write+0x8e/0x250
which lock already depends on the new lock.
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(s_active);
lock(cpu_hotplug.lock);
lock(s_active); lock(&mm->mmap_sem);
the existing dependency chain (in reverse order) is:
-> #2 (s_active#12){++++.+}:
[<ffffffff8117da2c>] lock_acquire+0xac/0x180
[<ffffffff8132f50a>] __kernfs_remove+0x2da/0x410
[<ffffffff81330630>] kernfs_remove_by_name_ns+0x40/0x90
[<ffffffff813339fb>] sysfs_remove_file_ns+0x2b/0x70
[<ffffffff81ba8a16>] device_del+0x166/0x320
[<ffffffff81ba943c>] device_destroy+0x3c/0x50
[<ffffffff8105aa61>] cpuid_class_cpu_callback+0x51/0x70
[<ffffffff81131ce9>] notifier_call_chain+0x59/0x190
[<ffffffff81132749>] __raw_notifier_call_chain+0x9/0x10
[<ffffffff810fe6b0>] __cpu_notify+0x40/0x90
[<ffffffff810fe890>] cpu_notify_nofail+0x10/0x30
[<ffffffff810fe8d7>] notify_dead+0x27/0x1e0
[<ffffffff810fe273>] cpuhp_down_callbacks+0x93/0x190
[<ffffffff82096062>] _cpu_down+0xc2/0x1e0
[<ffffffff810ff727>] do_cpu_down+0x37/0x50
[<ffffffff8110003b>] cpu_down+0xb/0x10
[<ffffffff81038e4d>] _debug_hotplug_cpu+0x7d/0xd0
[<ffffffff8435d6bb>] debug_hotplug_cpu+0xd/0x11
[<ffffffff84352426>] do_one_initcall+0x138/0x1cf
[<ffffffff8435270a>] kernel_init_freeable+0x24d/0x2de
[<ffffffff8209533a>] kernel_init+0xa/0x120
[<ffffffff820a7972>] ret_from_fork+0x22/0x50-> #1 (cpu_hotplug.lock#2){+.+.+.}:
[<ffffffff8117da2c>] lock_acquire+0xac/0x180
[<ffffffff820a20d1>] mutex_lock_nested+0x71/0x4c0
[<ffffffff810ff526>] get_online_cpus+0x66/0x80
[<ffffffff81246fb3>] sum_vm_event+0x23/0x1b0
[<ffffffff81293768>] collapse_huge_page+0x118/0x10b0
[<ffffffff81294c5d>] khugepaged+0x55d/0xe80
[<ffffffff81130304>] kthread+0x134/0x1a0
[<ffffffff820a7972>] ret_from_fork+0x22/0x50-> #0 (&mm->mmap_sem){++++++}:
[<ffffffff8117bf61>] __lock_acquire+0x2861/0x31f0
[<ffffffff8117da2c>] lock_acquire+0xac/0x180
[<ffffffff8126257e>] __might_fault+0xbe/0x150
[<ffffffff8133160f>] kernfs_fop_write+0xaf/0x250
[<ffffffff812a8933>] __vfs_write+0x43/0x1a0
[<ffffffff812a8d3a>] vfs_write+0xda/0x240
[<ffffffff812a8f84>] SyS_write+0x44/0xa0
[<ffffffff820a773c>] entry_SYSCALL_64_fastpath+0x1f/0xbdThis patch moves sum_vm_event() before taking down_write(&mm->mmap_sem) to solve dependency lock.
---
Changes in v2:
- Nothing changed
mm/huge_memory.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
mm/huge_memory.c +3 -2
@@ -2452,6 +2452,9 @@ static void collapse_huge_page(struct mm_struct *mm, goto out_nolock; } + swap =get_mm_counter (mm,MM_SWAPENTS ); + curr_allocstall =sum_vm_event (ALLOCSTALL ); +/* * Prevent all access to pagetables with the exception of * gup_fast later hanlded by the ptep_clear_flush and the VM
@@ -2484,8 +2487,6 @@ static void collapse_huge_page(struct mm_struct *mm, goto out; } - swap =get_mm_counter (mm,MM_SWAPENTS ); - curr_allocstall =sum_vm_event (ALLOCSTALL );/* * Don't perform swapin readahead when the system is under pressure, * to avoid unnecessary resource consumption.
Signature · 7 lines
1.9.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v2 2/3] mm, thp: fix possible circular locking dependency caused by sum_vm_event()
Hi Ebru,
Quoted 62 lines · Ebru Akagunduz
Nested circular locking dependency detected by kernel robot (udevadm).
udevadm/221 is trying to acquire lock:
(&mm->mmap_sem){++++++}, at: [<ffffffff81262543>] __might_fault+0x83/0x150 but task is already holding lock: (s_active#12){++++.+}, at: [<ffffffff813315ee>] kernfs_fop_write+0x8e/0x250which lock already depends on the new lock.
Possible unsafe locking scenario:CPU0 CPU1
---- ----lock(s_active); lock(cpu_hotplug.lock); lock(s_active);
lock(&mm->mmap_sem);the existing dependency chain (in reverse order) is:
-> #2 (s_active#12){++++.+}:[<ffffffff8117da2c>] lock_acquire+0xac/0x180 [<ffffffff8132f50a>] __kernfs_remove+0x2da/0x410 [<ffffffff81330630>] kernfs_remove_by_name_ns+0x40/0x90 [<ffffffff813339fb>] sysfs_remove_file_ns+0x2b/0x70 [<ffffffff81ba8a16>] device_del+0x166/0x320 [<ffffffff81ba943c>] device_destroy+0x3c/0x50 [<ffffffff8105aa61>] cpuid_class_cpu_callback+0x51/0x70 [<ffffffff81131ce9>] notifier_call_chain+0x59/0x190 [<ffffffff81132749>] __raw_notifier_call_chain+0x9/0x10 [<ffffffff810fe6b0>] __cpu_notify+0x40/0x90 [<ffffffff810fe890>] cpu_notify_nofail+0x10/0x30 [<ffffffff810fe8d7>] notify_dead+0x27/0x1e0 [<ffffffff810fe273>] cpuhp_down_callbacks+0x93/0x190 [<ffffffff82096062>] _cpu_down+0xc2/0x1e0 [<ffffffff810ff727>] do_cpu_down+0x37/0x50 [<ffffffff8110003b>] cpu_down+0xb/0x10 [<ffffffff81038e4d>] _debug_hotplug_cpu+0x7d/0xd0 [<ffffffff8435d6bb>] debug_hotplug_cpu+0xd/0x11 [<ffffffff84352426>] do_one_initcall+0x138/0x1cf [<ffffffff8435270a>] kernel_init_freeable+0x24d/0x2de [<ffffffff8209533a>] kernel_init+0xa/0x120 [<ffffffff820a7972>] ret_from_fork+0x22/0x50-> #1 (cpu_hotplug.lock#2){+.+.+.}:
[<ffffffff8117da2c>] lock_acquire+0xac/0x180 [<ffffffff820a20d1>] mutex_lock_nested+0x71/0x4c0 [<ffffffff810ff526>] get_online_cpus+0x66/0x80 [<ffffffff81246fb3>] sum_vm_event+0x23/0x1b0 [<ffffffff81293768>] collapse_huge_page+0x118/0x10b0 [<ffffffff81294c5d>] khugepaged+0x55d/0xe80 [<ffffffff81130304>] kthread+0x134/0x1a0 [<ffffffff820a7972>] ret_from_fork+0x22/0x50-> #0 (&mm->mmap_sem){++++++}:
[<ffffffff8117bf61>] __lock_acquire+0x2861/0x31f0 [<ffffffff8117da2c>] lock_acquire+0xac/0x180 [<ffffffff8126257e>] __might_fault+0xbe/0x150 [<ffffffff8133160f>] kernfs_fop_write+0xaf/0x250 [<ffffffff812a8933>] __vfs_write+0x43/0x1a0 [<ffffffff812a8d3a>] vfs_write+0xda/0x240 [<ffffffff812a8f84>] SyS_write+0x44/0xa0 [<ffffffff820a773c>] entry_SYSCALL_64_fastpath+0x1f/0xbd
Quoted 2 lines
This patch moves
sum_vm_event()before takingdown_write(&mm->mmap_sem)to solve dependency lock.
sum_vm_event works with CONFIG_VM_EVENT_COUNTERS so if a system doesn't enable the config, THP swapin throttling logic is void in that case. Right? If so, shouldn't we approach another way?
If it was already discussed, sorry for bothering you.
Quoted 40 lines
Signed-off-by Ebru Akagunduz <[email protected]>---
Changes in v2:
- Nothing changedmm/huge_memory.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)mm/huge_memory.c +3
-2@@ -2452,6 +2452,9 @@ static void collapse_huge_page(struct mm_struct *mm, goto out_nolock; } + swap =get_mm_counter (mm,MM_SWAPENTS ); + curr_allocstall =sum_vm_event (ALLOCSTALL ); +/* * Prevent all access to pagetables with the exception of * gup_fast later hanlded by the ptep_clear_flush and the VM @@ -2484,8 +2487,6 @@ static void collapse_huge_page(struct mm_struct *mm, goto out; } - swap =get_mm_counter (mm,MM_SWAPENTS ); - curr_allocstall =sum_vm_event (ALLOCSTALL );/* * Don't perform swapin readahead when the system is under pressure, * to avoid unnecessary resource consumption. Signature · 7 lines
1.9.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v2 3/3 mm, thp: make swapin readahead under down_read of mmap_sem
In reply to Ebru AkagunduzCurrently khugepaged makes swapin readahead under down_write. This patch supplies to make swapin readahead under down_read instead of down_write.
The patch was tested with a test program that allocates 800MB of memory, writes to it, and then sleeps. The system was forced to swap out all. Afterwards, the test program touches the area by writing, it skips a page in each 20 pages of the area.
---
Changes in v2:
- Keep the comment next to down_write (Andrea Arcangeli)
- To revalidate vma, use the same check methods which is placed in collapse_huge_page (Andrea Arcangeli)
- Collect the methods in a helper function and replace the content with function, if needed (Ebru Akagunduz)
mm/huge_memory.c | 97 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 66 insertions(+), 31 deletions(-)
mm/huge_memory.c +66 -31
@@ -2380,6 +2380,35 @@ static bool hugepage_vma_check(struct vm_area_struct *vma) }/* +* If mmap_sem temporarily dropped, revalidate vma +* before taking mmap_sem. +* Return 0 if succeeds, otherwise return none-zero +* value (scan code). +*/ + +static int hugepage_vma_revalidate (struct mm_struct *mm, +struct vm_area_struct *vma, +unsigned long address) +{ +unsigned long hstart, hend; + +if (unlikely (khugepaged_test_exit (mm))) +return SCAN_ANY_PROCESS ; + + vma =find_vma (mm, address); +if (!vma) +return SCAN_VMA_NULL ; + + hstart = (vma->vm_start + ~HPAGE_PMD_MASK ) &HPAGE_PMD_MASK ; + hend = vma->vm_end &HPAGE_PMD_MASK ; +if (address < hstart || address +HPAGE_PMD_SIZE > hend) +return SCAN_ADDRESS_RANGE ; +if (!hugepage_vma_check (vma)) +return SCAN_VMA_CHECK ; +return 0 ; +} + +/* * Bring missing pages in from swap, to complete THP collapse. * Only done if khugepaged_scan_pmd believes it is worthwhile. *
@@ -2387,7 +2416,7 @@ static bool hugepage_vma_check(struct vm_area_struct *vma)* but with mmap_sem held to protect against vma changes. */ -static void __collapse_huge_page_swapin (struct mm_struct *mm, +static bool __collapse_huge_page_swapin (struct mm_struct *mm,struct vm_area_struct *vma,unsigned long address,pmd_t *pmd) {
@@ -2403,11 +2432,18 @@ static void __collapse_huge_page_swapin(struct mm_struct *mm,continue ; swapped_in++; ret =do_swap_page (mm, vma, _address, pte, pmd, -FAULT_FLAG_ALLOW_RETRY |FAULT_FLAG_RETRY_NOWAIT , +FAULT_FLAG_ALLOW_RETRY , pteval); +/* do_swap_page returns VM_FAULT_RETRY with released mmap_sem */ +if (ret &VM_FAULT_RETRY ) { +down_read (&mm->mmap_sem); +/* vma is no longer available, don't continue to swapin */ +if (hugepage_vma_revalidate (mm, vma, address)) +return false; + }if (ret &VM_FAULT_ERROR ) {trace_mm_collapse_huge_page_swapin (mm, swapped_in,0 ); -return ; +return false; }/* pte is unmapped now, we need to map it */ pte =pte_offset_map (pmd, _address);
@@ -2415,6 +2451,7 @@ static void __collapse_huge_page_swapin(struct mm_struct *mm, pte--;pte_unmap (pte);trace_mm_collapse_huge_page_swapin (mm, swapped_in,1 ); +return true; } static void collapse_huge_page(struct mm_struct *mm,
@@ -2429,7 +2466,7 @@ static void collapse_huge_page(struct mm_struct *mm,struct page *new_page;spinlock_t *pmd_ptl, *pte_ptl;int isolated =0 , result =0 ; -unsigned long hstart, hend, swap, curr_allocstall; +unsigned long swap, curr_allocstall;struct mem_cgroup *memcg;unsigned long mmun_start;/* For mmu_notifiers */ unsigned long mmun_end;/* For mmu_notifiers */
@@ -2454,33 +2491,11 @@ static void collapse_huge_page(struct mm_struct *mm, swap =get_mm_counter (mm,MM_SWAPENTS ); curr_allocstall =sum_vm_event (ALLOCSTALL ); - -/* -* Prevent all access to pagetables with the exception of -* gup_fast later hanlded by the ptep_clear_flush and the VM -* handled by the anon_vma lock + PG_lock. -*/ -down_write (&mm->mmap_sem); -if (unlikely (khugepaged_test_exit (mm))) { - result =SCAN_ANY_PROCESS ; +down_read (&mm->mmap_sem); + result =hugepage_vma_revalidate (mm, vma, address); +if (result) goto out; - } - vma =find_vma (mm, address); -if (!vma) { - result =SCAN_VMA_NULL ; - goto out; - } - hstart = (vma->vm_start + ~HPAGE_PMD_MASK ) &HPAGE_PMD_MASK ; - hend = vma->vm_end &HPAGE_PMD_MASK ; -if (address < hstart || address +HPAGE_PMD_SIZE > hend) { - result =SCAN_ADDRESS_RANGE ; - goto out; - } -if (!hugepage_vma_check (vma)) { - result =SCAN_VMA_CHECK ; - goto out; - } pmd =mm_find_pmd (mm, address);if (!pmd) { result =SCAN_PMD_NULL ;
@@ -2491,8 +2506,28 @@ static void collapse_huge_page(struct mm_struct *mm,* Don't perform swapin readahead when the system is under pressure, * to avoid unnecessary resource consumption. */ -if (allocstall == curr_allocstall && swap !=0 ) -__collapse_huge_page_swapin (mm, vma, address, pmd); +if (allocstall == curr_allocstall && swap !=0 ) { +/* +* __collapse_huge_page_swapin always returns with mmap_sem +* locked. If it fails, release mmap_sem and jump directly +* label out. Continuing to collapse causes inconsistency. +*/ +if (!__collapse_huge_page_swapin (mm, vma, address, pmd)) { +up_read (&mm->mmap_sem); + goto out; + } + } + +up_read (&mm->mmap_sem); +/* +* Prevent all access to pagetables with the exception of +* gup_fast later hanlded by the ptep_clear_flush and the VM +* handled by the anon_vma lock + PG_lock. +*/ +down_write (&mm->mmap_sem); + result =hugepage_vma_revalidate (mm, vma, address); +if (result) + goto out;anon_vma_lock_write (vma->anon_vma);
Signature · 7 lines
1.9.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>