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 EC50EC433EF for ; Mon, 20 Jun 2022 12:32:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 843D56B0071; Mon, 20 Jun 2022 08:32:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7F3146B0073; Mon, 20 Jun 2022 08:32:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6E1A68E0001; Mon, 20 Jun 2022 08:32:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 60B346B0071 for ; Mon, 20 Jun 2022 08:32:32 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 2D830608F5 for ; Mon, 20 Jun 2022 12:32:32 +0000 (UTC) X-FDA: 79598552544.21.63F78F6 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf27.hostedemail.com (Postfix) with ESMTP id 1797040020 for ; Mon, 20 Jun 2022 12:32:30 +0000 (UTC) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.53]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4LRTWM3Tfyz1KC5x; Mon, 20 Jun 2022 20:30:23 +0800 (CST) Received: from [10.174.177.76] (10.174.177.76) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 20 Jun 2022 20:32:27 +0800 Subject: Re: [PATCH v2 2/3] mm/swapfile: fix possible data races of inuse_pages From: Miaohe Lin To: Muchun Song , "Huang, Ying" , Qian Cai CC: , , , References: <20220608144031.829-1-linmiaohe@huawei.com> <20220608144031.829-3-linmiaohe@huawei.com> <87edzjrcq8.fsf@yhuang6-desk2.ccr.corp.intel.com> <13414d6a-9e72-fb6c-f0a8-8b83ba0455de@huawei.com> <09ffac27-7fe9-0977-cb33-30433e78e662@huawei.com> Message-ID: Date: Mon, 20 Jun 2022 20:32:27 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: <09ffac27-7fe9-0977-cb33-30433e78e662@huawei.com> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.177.76] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf27.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655728351; 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=2Mq9QjcCtgsLneZyJm2murdvIrVtxyS7QO58RUPsKSc=; b=5eOSoNZjptyKgNtV1+3kEf2bZUuV0ygNE8vJ1wrS55L6aTzBxCDF6IZ6rWrxfKP0JEJVfS YfmqKXM47nOo80uuM3crKWgMDh5sTbuRxLT0Ka6PIG464pXH4Y5FfdmaPk2gptWyJGy4uF 3mFAc+SjnzD9L25L00UKybRU9ZBJKJA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655728351; a=rsa-sha256; cv=none; b=sr+wCfKu7xh06+OURN6RPWhi1vkusRnoulFyp3+nM/mTtiAF6Ph4ayon8YkiFNu/jFcHaG N1Q/P0beE+H0TIgVydMOyPYuNTtvGuSR+9GqnuGl9tCCMSaU7p2dvkLwz7dJh404CKvHeZ hg8A9RL3LAsjMv5rBOuEgWktcEniSW0= X-Stat-Signature: 4ifxjmwodnymn6y417brw691fogog47h X-Rspamd-Queue-Id: 1797040020 X-Rspam-User: Authentication-Results: imf27.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf27.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com X-Rspamd-Server: rspam10 X-HE-Tag: 1655728350-939649 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 2022/6/20 20:23, Miaohe Lin wrote: > On 2022/6/20 17:23, Muchun Song wrote: >> On Mon, Jun 20, 2022 at 05:04:50PM +0800, Miaohe Lin wrote: >>> On 2022/6/20 15:54, Huang, Ying wrote: >>>> Miaohe Lin writes: >>>> >>>>> si->inuse_pages could still be accessed concurrently now. The plain reads >>>>> outside si->lock critical section, i.e. swap_show and si_swapinfo, which >>>>> results in data races. But these should be ok because they're just used >>>>> for showing swap info. >>>>> >>>>> Signed-off-by: Miaohe Lin >>>>> Reviewed-by: David Hildenbrand >>>>> --- >>>>> mm/swapfile.c | 4 ++-- >>>>> 1 file changed, 2 insertions(+), 2 deletions(-) >>>>> >>>>> diff --git a/mm/swapfile.c b/mm/swapfile.c >>>>> index d2bead7b8b70..3fa26f6971e9 100644 >>>>> --- a/mm/swapfile.c >>>>> +++ b/mm/swapfile.c >>>>> @@ -2646,7 +2646,7 @@ static int swap_show(struct seq_file *swap, void *v) >>>>> } >>>>> >>>>> bytes = si->pages << (PAGE_SHIFT - 10); >>>>> - inuse = si->inuse_pages << (PAGE_SHIFT - 10); >>>>> + inuse = READ_ONCE(si->inuse_pages) << (PAGE_SHIFT - 10); >>>>> >>>>> file = si->swap_file; >>>>> len = seq_file_path(swap, file, " \t\n\\"); >>>>> @@ -3265,7 +3265,7 @@ void si_swapinfo(struct sysinfo *val) >>>>> struct swap_info_struct *si = swap_info[type]; >>>>> >>>>> if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK)) >>>>> - nr_to_be_unused += si->inuse_pages; >>>>> + nr_to_be_unused += READ_ONCE(si->inuse_pages); >>>>> } >>>>> val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused; >>>>> val->totalswap = total_swap_pages + nr_to_be_unused; >>>> >>>> READ_ONCE() should be paired with WRITE_ONCE(). So, change the writer >>>> side too? >>> >>> READ_ONCE() is used to fix the complaint of concurrent accessing to si->inuse_pages from KCSAN here. >>> The similar commit is 218209487c3d ("mm/swapfile: fix data races in try_to_unuse()"). IMHO, it's fine >> >> I think the fix 218209487c3d is incomplete. The write side in swap_range_free() should >> also be fixed. Otherwise, IIUC, it cannot stop KCSAN complaining. > > I tend to agree with you. READ_ONCE() should be paired with WRITE_ONCE() theoretically. But WRITTE_ONCE() > is ignored while the commit is introduced. Add Qian Cai for helping verify it. It's very kind of @Qian Cai > if he could tell us whether WRITTE_ONCE() is ignored deliberately. Update the email address of Qian Cai. > > Thanks all of you. :) > >> >>> to see a not-uptodate value of si->inuse_pages because it's just used for showing swap info. So >>> WRITE_ONCE() is not obligatory. Or am I miss something? >>> >>>> >>>> Best Regards, >>>> Huang, Ying >>> >>> Thanks! >>> >>>> . >>>> >>> >>> >> . >> >