From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 823CFC001DE for ; Fri, 18 Aug 2023 09:01:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8DAE6940057; Fri, 18 Aug 2023 05:01:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8887A940053; Fri, 18 Aug 2023 05:01:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 751DC940057; Fri, 18 Aug 2023 05:01:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 64696940053 for ; Fri, 18 Aug 2023 05:01:40 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2D3B3C03B3 for ; Fri, 18 Aug 2023 09:01:40 +0000 (UTC) X-FDA: 81136632360.02.D279104 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf17.hostedemail.com (Postfix) with ESMTP id D4F084001B for ; Fri, 18 Aug 2023 09:01:35 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=none; spf=pass (imf17.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1692349297; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Z1danj/DfjNbp1wb6hszm5FyVo6zfGHNfo8brbGar3M=; b=fwFAP+jMb22Kky4I6ttPjCrTvLQzdZgLVkQ7XgnZgnofMplNAY2EQx/h/KhjckctJoZ+Pl VeS5v4zt/hre+NW1fN4Kgm6uwUT7rVpIjKvl3rJuSGehFfcLr/NRSKjed0UBiq/OZ2c4lH W3sc5B22pXYSmvMLYmjcdoTtDIPCGjY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692349297; a=rsa-sha256; cv=none; b=UWw6aFjojWtUnPYYHc+ynMtLP6RfDHST1Q8UlIAJEA5Gub6OcAkcClP3m4tQOVviagUIYZ MPzYUwgBGu+kxyStawEAXPTSeTxFtWHhG8PDYGPKiWytfy5cqPRbrT4/vd+oagy/WOQwOC FInO7NJvYd5bkBBYf8qH2xnSyqwQHLI= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=none; spf=pass (imf17.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from dggpemm100001.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4RRwkb6B7FzNmkj; Fri, 18 Aug 2023 16:57:59 +0800 (CST) Received: from [10.174.177.243] (10.174.177.243) by dggpemm100001.china.huawei.com (7.185.36.93) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.31; Fri, 18 Aug 2023 17:01:31 +0800 Message-ID: <3e7b0fa9-ec9d-4f5f-bb1e-e8cf79058e1a@huawei.com> Date: Fri, 18 Aug 2023 17:01:31 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH v2-next] mm: memory-failure: use rcu lock instead of tasklist_lock when collect_procs() Content-Language: en-US To: Tong Tiangen , Andrew Morton , Naoya Horiguchi , Miaohe Lin CC: , , Guohanjun References: <20230818081727.4181963-1-tongtiangen@huawei.com> From: Kefeng Wang In-Reply-To: <20230818081727.4181963-1-tongtiangen@huawei.com> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.177.243] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm100001.china.huawei.com (7.185.36.93) X-CFilter-Loop: Reflected X-Rspamd-Queue-Id: D4F084001B X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: rt1g3u4j4cm3iohtzxqzq5kkubz36i68 X-HE-Tag: 1692349295-185439 X-HE-Meta: U2FsdGVkX1/wQGXtuN21wBvCeMH3gyBnUBodKKQBPdr6IkNG4MvdXQhTl3YZH261TFkzvQPwXBdt25nBuvDSZ+W+pNy7EoL1noXg5/tHDpjiPbnLHLYG46dFkGuk1L4f8X76iAptIqDv87+uhT7CSwwRgFgvQA216uDfzQofYhhxiqX5Wk1C2ZIyhpnd2Dnv8AGwWzIMCFEHN8q07A4+aXW+fEIvLk0Q6W14A/jrx+tCAR/E4K4eY54J1a9TIbPyMqgDEAvdq1SYdfkMfKB34Wjvg+/CTBN9+le+qfw2k3v8AG+rluNh8b/ZgMDoHufbwt0FDRA9bUhBFSZnXeW/mV9pE3ZLuU+rP9Sz9aKYDu+tII3uxhoNKnAbiwU0beJHqBLbvdfvIlqLaXlvMqK6DdBV7447AcyQT8IaKo4U7al+wx6QVSqwrDJGxQJbBK1AB4QwRwd/B3yhOGtqZSe4SC2NiP5vdAwQSdBUJWdJN79+nLOJGrZXf+888fPm5mO4H5uixtio/dMeFQvfDG0mUDTp84xZLER9TTqkgKro/z3M4dtBQ1ri7Nau4xwiYqRdFUSLfvBYaHY1jPMibwwLeZAE05QcGNJmr/YEBi2E/NvgrYzNEpGj5KtvxSBEabTcRHx5aYClTs0d+b5I02zycwC7IaOwTsNU1fnPpm80qD1Jx/z9FzJofrFsk3akIBueFMkI75jac1u6tOc8NSj5825t6hol0BxDwM8iaepQWnjGdwwNcLBb/1YrjDlf3sTWmDFYr2+k/5xK6YPcmnhb+V5WivH+0t5dXsTl9cEUFtD+vK0DpDxhSy8r6pkdIYTj5L2vuFMoDG24EFJSIBwcx+EtrITrfZZjmDguuBeSKdTIz1xKf8uIibYKVMfPTsualqopcKwuzi2ZqRU6kfapFe5SgJ6IVRse58enzTyGMsF+pbQ2W/e0JVNpKtAxSij9kjWqO1jNQyrbNdRr96y HoOqQTHP npcy8Ay+aJVcCXPLMUz99I4gZJ3bI1hqh6rO4odTzxo5vkhiDO+vzlxky6+M4H3jl+DX2nQ4e70qCCo+TnGcrDFAgp3ruZrL9jeizO+D2/Sc1LqZPh9DT8LDCx07ZG7AcETaSEXsWGz4A+BSdN2cS5z2s6XfGiobg9KBSKVrvUElXhHI3XtKSgEEMf/2kbjOokLAqkxZbnSWJZq9sNy4l9mEIdl8yS1vzdaKIUOtUcXjH8MmMXyJ5S3zGQQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 2023/8/18 16:17, Tong Tiangen wrote: > 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(), we will not modify the tasklist, but only perform > read traversal. Therefore, we can use rcu lock instead of spin lock > tasklist_lock, from this, we can break the softlock chain above. > > The same logic can also be applied to: > - collect_procs_file() > - collect_procs_fsdax() > - collect_procs_ksm() > - find_early_kill_thread() > > Signed-off-by: Tong Tiangen > --- > v2: > - 1. Modify the title description. > - 2. Optimize the implementation of find_early_kill_thread() without > functional changes. > --- Those changes are fine to me, please fix the comment mentioned by Noaoya. > mm/ksm.c | 4 ++-- > mm/memory-failure.c | 33 +++++++++++++++++++-------------- > 2 files changed, 21 insertions(+), 16 deletions(-) > > diff --git a/mm/ksm.c b/mm/ksm.c > index 6b7b8928fb96..dcbc0c7f68e7 100644 > --- a/mm/ksm.c > +++ b/mm/ksm.c > @@ -2919,7 +2919,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; > @@ -2938,7 +2938,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..4f3081f47798 100644 > --- a/mm/memory-failure.c > +++ b/mm/memory-failure.c > @@ -546,24 +546,29 @@ static void kill_procs(struct list_head *to_kill, int forcekill, bool fail, > * Find a dedicated thread which is supposed to handle SIGBUS(BUS_MCEERR_AO) > * 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. > */ > static struct task_struct *find_early_kill_thread(struct task_struct *tsk) > { > struct task_struct *t; > + bool found = false; > > + rcu_read_lock(); > for_each_thread(tsk, t) { > if (t->flags & PF_MCE_PROCESS) { > - if (t->flags & PF_MCE_EARLY) > - return t; > + if (t->flags & PF_MCE_EARLY) { > + found = true; > + break; > + } > } else { > - if (sysctl_memory_failure_early_kill) > - return t; > + if (sysctl_memory_failure_early_kill) { > + found = true; > + break; > + } > } > } > - return NULL; > + rcu_read_unlock(); > + > + return found ? t : NULL; > } > > /* > @@ -609,7 +614,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 +631,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 +647,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 +667,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 +690,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 +701,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 */