* [PATCH v3] mm: memory-failure: use rcu lock instead of tasklist_lock when collect_procs()
@ 2023-08-28 2:25 Tong Tiangen
2023-09-03 23:41 ` Andrew Morton
0 siblings, 1 reply; 3+ messages in thread
From: Tong Tiangen @ 2023-08-28 2:25 UTC (permalink / raw)
To: Andrew Morton, Matthew Wilcox, Naoya Horiguchi, wangkefeng.wang,
Paul E . McKenney, Miaohe Lin
Cc: linux-mm, linux-kernel, Tong Tiangen
We found a softlock issue in our test, analyzed the logs, and found that
the relevant CPU call trace as follows:
CPU0:
_do_fork
-> copy_process()
-> write_lock_irq(&tasklist_lock) //Disable irq,waiting for
//tasklist_lock
CPU1:
wp_page_copy()
->pte_offset_map_lock()
-> spin_lock(&page->ptl); //Hold page->ptl
-> ptep_clear_flush()
-> flush_tlb_others() ...
-> smp_call_function_many()
-> arch_send_call_function_ipi_mask()
-> csd_lock_wait() //Waiting for other CPUs respond
//IPI
CPU2:
collect_procs_anon()
-> read_lock(&tasklist_lock) //Hold tasklist_lock
->for_each_process(tsk)
-> page_mapped_in_vma()
-> page_vma_mapped_walk()
-> map_pte()
->spin_lock(&page->ptl) //Waiting for page->ptl
We can see that CPU1 waiting for CPU0 respond IPI,CPU0 waiting for CPU2
unlock tasklist_lock, CPU2 waiting for CPU1 unlock page->ptl. As a result,
softlockup is triggered.
For collect_procs_anon(), what we're doing is task list iteration, during
the iteration, with the help of call_rcu(), the task_struct object is freed
only after one or more grace periods elapse. the logic as follows:
release_task()
-> __exit_signal()
-> __unhash_process()
-> list_del_rcu()
-> put_task_struct_rcu_user()
-> call_rcu(&task->rcu, delayed_put_task_struct)
delayed_put_task_struct()
-> put_task_struct()
-> if (refcount_sub_and_test())
__put_task_struct()
-> free_task()
Therefore, under the protection of the rcu lock, we can safely use
get_task_struct() to ensure a safe reference to task_struct during the
iteration.
By removing the use of tasklist_lock in task list iteration, we can break
the softlock chain above.
The same logic can also be applied to:
- collect_procs_file()
- collect_procs_fsdax()
- collect_procs_ksm()
Signed-off-by: Tong Tiangen <tongtiangen@huawei.com>
Acked-by: Naoya Horiguchi <naoya.horiguchi@nec.com>
---
Since v2:
- 1. According to the analysis of Naoya,Matthew and Kefeng,update
the commit message.
Since v1:
- 1. According to Matthew's suggestion, only the comments of
find_early_kill_thread() are modified, no need to hold the rcu lock.
Changes since RFC[1]:
- 1. According to Naoya's suggestion, modify the tasklist_lock in the
comment about locking order in mm/filemap.c.
- 2. According to Kefeng's suggestion, optimize the implementation of
find_early_kill_thread() without functional changes.
- 3. Modify the title description.
[1] https://lore.kernel.org/lkml/20230815130154.1100779-1-tongtiangen@huawei.com/
---
mm/filemap.c | 3 ---
mm/ksm.c | 4 ++--
mm/memory-failure.c | 16 ++++++++--------
3 files changed, 10 insertions(+), 13 deletions(-)
diff --git a/mm/filemap.c b/mm/filemap.c
index 014b73eb96a1..dfade1ef1765 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -121,9 +121,6 @@
* bdi.wb->list_lock (zap_pte_range->set_page_dirty)
* ->inode->i_lock (zap_pte_range->set_page_dirty)
* ->private_lock (zap_pte_range->block_dirty_folio)
- *
- * ->i_mmap_rwsem
- * ->tasklist_lock (memory_failure, collect_procs_ao)
*/
static void page_cache_delete(struct address_space *mapping,
diff --git a/mm/ksm.c b/mm/ksm.c
index 8d6aee05421d..981af9c72e7a 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -2925,7 +2925,7 @@ void collect_procs_ksm(struct page *page, struct list_head *to_kill,
struct anon_vma *av = rmap_item->anon_vma;
anon_vma_lock_read(av);
- read_lock(&tasklist_lock);
+ rcu_read_lock();
for_each_process(tsk) {
struct anon_vma_chain *vmac;
unsigned long addr;
@@ -2944,7 +2944,7 @@ void collect_procs_ksm(struct page *page, struct list_head *to_kill,
}
}
}
- read_unlock(&tasklist_lock);
+ rcu_read_unlock();
anon_vma_unlock_read(av);
}
}
diff --git a/mm/memory-failure.c b/mm/memory-failure.c
index 7b01fffe7a79..4d6e43c88489 100644
--- a/mm/memory-failure.c
+++ b/mm/memory-failure.c
@@ -547,8 +547,8 @@ static void kill_procs(struct list_head *to_kill, int forcekill, bool fail,
* on behalf of the thread group. Return task_struct of the (first found)
* dedicated thread if found, and return NULL otherwise.
*
- * We already hold read_lock(&tasklist_lock) in the caller, so we don't
- * have to call rcu_read_lock/unlock() in this function.
+ * We already hold rcu lock in the caller, so we don't have to call
+ * rcu_read_lock/unlock() in this function.
*/
static struct task_struct *find_early_kill_thread(struct task_struct *tsk)
{
@@ -609,7 +609,7 @@ static void collect_procs_anon(struct page *page, struct list_head *to_kill,
return;
pgoff = page_to_pgoff(page);
- read_lock(&tasklist_lock);
+ rcu_read_lock();
for_each_process(tsk) {
struct anon_vma_chain *vmac;
struct task_struct *t = task_early_kill(tsk, force_early);
@@ -626,7 +626,7 @@ static void collect_procs_anon(struct page *page, struct list_head *to_kill,
add_to_kill_anon_file(t, page, vma, to_kill);
}
}
- read_unlock(&tasklist_lock);
+ rcu_read_unlock();
anon_vma_unlock_read(av);
}
@@ -642,7 +642,7 @@ static void collect_procs_file(struct page *page, struct list_head *to_kill,
pgoff_t pgoff;
i_mmap_lock_read(mapping);
- read_lock(&tasklist_lock);
+ rcu_read_lock();
pgoff = page_to_pgoff(page);
for_each_process(tsk) {
struct task_struct *t = task_early_kill(tsk, force_early);
@@ -662,7 +662,7 @@ static void collect_procs_file(struct page *page, struct list_head *to_kill,
add_to_kill_anon_file(t, page, vma, to_kill);
}
}
- read_unlock(&tasklist_lock);
+ rcu_read_unlock();
i_mmap_unlock_read(mapping);
}
@@ -685,7 +685,7 @@ static void collect_procs_fsdax(struct page *page,
struct task_struct *tsk;
i_mmap_lock_read(mapping);
- read_lock(&tasklist_lock);
+ rcu_read_lock();
for_each_process(tsk) {
struct task_struct *t = task_early_kill(tsk, true);
@@ -696,7 +696,7 @@ static void collect_procs_fsdax(struct page *page,
add_to_kill_fsdax(t, page, vma, to_kill, pgoff);
}
}
- read_unlock(&tasklist_lock);
+ rcu_read_unlock();
i_mmap_unlock_read(mapping);
}
#endif /* CONFIG_FS_DAX */
--
2.25.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v3] mm: memory-failure: use rcu lock instead of tasklist_lock when collect_procs()
2023-08-28 2:25 [PATCH v3] mm: memory-failure: use rcu lock instead of tasklist_lock when collect_procs() Tong Tiangen
@ 2023-09-03 23:41 ` Andrew Morton
2023-09-05 6:57 ` Tong Tiangen
0 siblings, 1 reply; 3+ messages in thread
From: Andrew Morton @ 2023-09-03 23:41 UTC (permalink / raw)
To: Tong Tiangen
Cc: Matthew Wilcox, Naoya Horiguchi, wangkefeng.wang,
Paul E . McKenney, Miaohe Lin, linux-mm, linux-kernel
On Mon, 28 Aug 2023 10:25:27 +0800 Tong Tiangen <tongtiangen@huawei.com> wrote:
> We found a softlock issue in our test, analyzed the logs, and found that
> the relevant CPU call trace as follows:
>
> ...
>
> By removing the use of tasklist_lock in task list iteration, we can break
> the softlock chain above.
>
So I assume we'd like to backport this fix into -stable kernels?
If so, are we able to identify a suitble Fixes: target?
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v3] mm: memory-failure: use rcu lock instead of tasklist_lock when collect_procs()
2023-09-03 23:41 ` Andrew Morton
@ 2023-09-05 6:57 ` Tong Tiangen
0 siblings, 0 replies; 3+ messages in thread
From: Tong Tiangen @ 2023-09-05 6:57 UTC (permalink / raw)
To: Andrew Morton
Cc: Matthew Wilcox, Naoya Horiguchi, Wangkefeng (OS Kernel Lab),
Paul E . McKenney, linmiaohe, linux-mm, linux-kernel
在 2023/9/4 7:41, Andrew Morton 写道:
> On Mon, 28 Aug 2023 10:25:27 +0800 Tong Tiangen <tongtiangen@huawei.com> wrote:
>
>> We found a softlock issue in our test, analyzed the logs, and found that
>> the relevant CPU call trace as follows:
>>
>> ...
>>
>> By removing the use of tasklist_lock in task list iteration, we can break
>> the softlock chain above.
>>
>
> So I assume we'd like to backport this fix into -stable kernels?
>
> If so, are we able to identify a suitble Fixes: target?
> .
After checking the git logs of these functions, the lock-related code
line is available when the functions are first introduced, as follows:
copy_process(): kernel/fork.c
commit 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (tag: v2.6.12-rc2)
Author: Linus Torvalds <torvalds@ppc970.osdl.org>
Date: Sat Apr 16 15:20:36 2005 -0700
Linux-2.6.12-rc2
wp_page_copy(): mm/memory.c
commit 2f38ab2c3c7fef04dca0313fd89d91f142ca9281
Author: Shachar Raindel <raindel@mellanox.com>
Date: Tue Apr 14 15:46:32 2015 -0700
mm: refactor do_wp_page, extract the page copy flow
pte_offset_map_lock: include/linux/mm.hq
commit c74df32c724a1652ad8399b4891bb02c9d43743a
Author: Hugh Dickins <hugh@veritas.com>
Date: Sat Oct 29 18:16:23 2005 -0700
[PATCH] mm: ptd_alloc take ptlock
collect_procs_anon(): mm/memory-failure.c
commit 6a46079cf57a7f7758e8b926980a4f852f89b34d
Author: Andi Kleen <andi@firstfloor.org>
Date: Wed Sep 16 11:50:15 2009 +0200
HWPOISON: The high level memory error handler in the VM v7
Is it appropriate to use the commit of the last introduction
function(wp_page_copy())?
Fixes: 2f38ab2c3c7f ("refactor do_wp_page, extract the page copy flow")
Or use the commit introduced by the error reporting
function(collect_procs_anon())?
Fixes: 6a46079cf57a ("HWPOISON: The high level memory error handler in
the VM v7")
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-09-05 6:57 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-28 2:25 [PATCH v3] mm: memory-failure: use rcu lock instead of tasklist_lock when collect_procs() Tong Tiangen
2023-09-03 23:41 ` Andrew Morton
2023-09-05 6:57 ` Tong Tiangen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox