Skip to content

Commit d256d1c

Browse files
Tong Tiangenakpm00
authored andcommitted
mm: memory-failure: use rcu lock instead of tasklist_lock when collect_procs()
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() Link: https://lkml.kernel.org/r/20230828022527.241693-1-tongtiangen@huawei.com Signed-off-by: Tong Tiangen <tongtiangen@huawei.com> Acked-by: Naoya Horiguchi <naoya.horiguchi@nec.com> Cc: Kefeng Wang <wangkefeng.wang@huawei.com> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Miaohe Lin <linmiaohe@huawei.com> Cc: Paul E. McKenney <paulmck@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 2562d67 commit d256d1c

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

mm/filemap.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,6 @@
121121
* bdi.wb->list_lock (zap_pte_range->set_page_dirty)
122122
* ->inode->i_lock (zap_pte_range->set_page_dirty)
123123
* ->private_lock (zap_pte_range->block_dirty_folio)
124-
*
125-
* ->i_mmap_rwsem
126-
* ->tasklist_lock (memory_failure, collect_procs_ao)
127124
*/
128125

129126
static void page_cache_delete(struct address_space *mapping,

mm/ksm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,7 +2925,7 @@ void collect_procs_ksm(struct page *page, struct list_head *to_kill,
29252925
struct anon_vma *av = rmap_item->anon_vma;
29262926

29272927
anon_vma_lock_read(av);
2928-
read_lock(&tasklist_lock);
2928+
rcu_read_lock();
29292929
for_each_process(tsk) {
29302930
struct anon_vma_chain *vmac;
29312931
unsigned long addr;
@@ -2944,7 +2944,7 @@ void collect_procs_ksm(struct page *page, struct list_head *to_kill,
29442944
}
29452945
}
29462946
}
2947-
read_unlock(&tasklist_lock);
2947+
rcu_read_unlock();
29482948
anon_vma_unlock_read(av);
29492949
}
29502950
}

mm/memory-failure.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,8 @@ static void kill_procs(struct list_head *to_kill, int forcekill, bool fail,
547547
* on behalf of the thread group. Return task_struct of the (first found)
548548
* dedicated thread if found, and return NULL otherwise.
549549
*
550-
* We already hold read_lock(&tasklist_lock) in the caller, so we don't
551-
* have to call rcu_read_lock/unlock() in this function.
550+
* We already hold rcu lock in the caller, so we don't have to call
551+
* rcu_read_lock/unlock() in this function.
552552
*/
553553
static struct task_struct *find_early_kill_thread(struct task_struct *tsk)
554554
{
@@ -609,7 +609,7 @@ static void collect_procs_anon(struct page *page, struct list_head *to_kill,
609609
return;
610610

611611
pgoff = page_to_pgoff(page);
612-
read_lock(&tasklist_lock);
612+
rcu_read_lock();
613613
for_each_process(tsk) {
614614
struct anon_vma_chain *vmac;
615615
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,
626626
add_to_kill_anon_file(t, page, vma, to_kill);
627627
}
628628
}
629-
read_unlock(&tasklist_lock);
629+
rcu_read_unlock();
630630
anon_vma_unlock_read(av);
631631
}
632632

@@ -642,7 +642,7 @@ static void collect_procs_file(struct page *page, struct list_head *to_kill,
642642
pgoff_t pgoff;
643643

644644
i_mmap_lock_read(mapping);
645-
read_lock(&tasklist_lock);
645+
rcu_read_lock();
646646
pgoff = page_to_pgoff(page);
647647
for_each_process(tsk) {
648648
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,
662662
add_to_kill_anon_file(t, page, vma, to_kill);
663663
}
664664
}
665-
read_unlock(&tasklist_lock);
665+
rcu_read_unlock();
666666
i_mmap_unlock_read(mapping);
667667
}
668668

@@ -685,7 +685,7 @@ static void collect_procs_fsdax(struct page *page,
685685
struct task_struct *tsk;
686686

687687
i_mmap_lock_read(mapping);
688-
read_lock(&tasklist_lock);
688+
rcu_read_lock();
689689
for_each_process(tsk) {
690690
struct task_struct *t = task_early_kill(tsk, true);
691691

@@ -696,7 +696,7 @@ static void collect_procs_fsdax(struct page *page,
696696
add_to_kill_fsdax(t, page, vma, to_kill, pgoff);
697697
}
698698
}
699-
read_unlock(&tasklist_lock);
699+
rcu_read_unlock();
700700
i_mmap_unlock_read(mapping);
701701
}
702702
#endif /* CONFIG_FS_DAX */

0 commit comments

Comments
 (0)