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 55702C43334 for ; Mon, 20 Jun 2022 14:20:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EF76F6B0071; Mon, 20 Jun 2022 10:20:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E7FCF8E0001; Mon, 20 Jun 2022 10:20:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D21536B0074; Mon, 20 Jun 2022 10:20:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id BB4E06B0071 for ; Mon, 20 Jun 2022 10:20:13 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 79A0434C6A for ; Mon, 20 Jun 2022 14:20:13 +0000 (UTC) X-FDA: 79598823906.21.9522F75 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by imf05.hostedemail.com (Postfix) with ESMTP id EBAAA10009C for ; Mon, 20 Jun 2022 14:20:12 +0000 (UTC) Received: by mail-pf1-f177.google.com with SMTP id a15so3162686pfv.13 for ; Mon, 20 Jun 2022 07:20:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=J0C2HHsbOQSXfYAryP7A5YBKSGtDJBQq/a2915IoEXg=; b=tEPR2uts4ypvF3V0L7HGkn58uuvfZFTYbnas3kFCboi3LOGlOxKaa5q7HLVYiRzQq8 GxzbxWLit6Xdj5G4CUrPpzeCgbRbCnSfEcd+7k6tl/iUuDOSO7q83552eZ5U43f6sCOc 13l5POTPPKlGt8sRtRF47+dEnPX2bqRTYpgypMtEBTFYRNV697u0w467EKldQ7wrQXnO 1+pKc0zw2KSf7wh9r8A2ASxyQ5N/sZj3qY1f8LwWTxCLxNeWadmLolUWqlT3CX7gQ3My MuodctXcQSEuD6+Bc7PDXie0G/JQX9MQx0X8NmazazywSzi6wO5IyYZnAoUaxXlwUsGM jaIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=J0C2HHsbOQSXfYAryP7A5YBKSGtDJBQq/a2915IoEXg=; b=GW3FaO1hyh9FGnU6KJaWknbI1u6LaL0u+tH/GU6dqk4tHrc3nyxEqvvyRD7toknygW AfShW9RmfeD65Zo9XPa+IPsYJp1lA6hw3lKtLDHAmuuMeT35+6EX5rm6Mo4QJR4DmHg3 eV6dEC0j1rCJUQHV0DtlrszRKUMtH/TMPNzjBYnXfOAwvuWCuxg47pDMI3+JkqRMoY2T OTww4s7GkNqhIa4Y8SnpOGejoCNgfVwY//mHEcG07QRzF6IGFHw6Vh6dYxh84W54FGSu T7aDk8LpQJxaVbaDSc4SVoU6mdqKBcDedOs9n9oRUGEIwncNcvByaf9r6wqriwPrrauP OKEQ== X-Gm-Message-State: AJIora9TELPJK+dbUMSUFv+SfAUAa2+40wFkel82NwcZ79wHlCGk/3pz WzsOasiigUR765+sdEwem8hGOA== X-Google-Smtp-Source: AGRyM1toe+QYeMNfOIaXcAkjR7ZUQ3qcCY/dhFUqc4mi1yEH+v5CjC8N0Qn8/mR7HEDUAUsNWS6kzA== X-Received: by 2002:a62:3145:0:b0:525:1309:36f8 with SMTP id x66-20020a623145000000b00525130936f8mr11479093pfx.74.1655734811846; Mon, 20 Jun 2022 07:20:11 -0700 (PDT) Received: from localhost ([139.177.225.255]) by smtp.gmail.com with ESMTPSA id c3-20020a17090a8d0300b001ec84049064sm4896427pjo.41.2022.06.20.07.20.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jun 2022 07:20:11 -0700 (PDT) Date: Mon, 20 Jun 2022 22:20:07 +0800 From: Muchun Song To: Qian Cai Cc: Miaohe Lin , "Huang, Ying" , akpm@linux-foundation.org, david@redhat.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 2/3] mm/swapfile: fix possible data races of inuse_pages Message-ID: 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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655734813; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=J0C2HHsbOQSXfYAryP7A5YBKSGtDJBQq/a2915IoEXg=; b=seuuCMT5/0CRCYdYDm96ibClXMWhAqNCjXJZBWTJx5JxGjN64fOUyWKjE8sdyvBiimT/si ZvMbhHZB3nntdJCqE+iHzlZ/YpQjsBCCeJvPmw7aS4EVIYmhnsPbk6Imve6aEUFqOkFvZH EBgbiJDvatzAeitGuN+SrtyxkdZGxy4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655734813; a=rsa-sha256; cv=none; b=xDsVoIjQVYH8XUcNwp+mtwTvJ2GMAQB5fhtHgLihluzpSYI7MGkU2YvjsIlaKCEWDFjXmm LZM4DAKHIIaH/pVtP0oUzpRV9r4qxa8mw7YJ2Bj8y99QG+j5I/tZrWJ5K3m/2G8mDyT/IP rpbxUK0wtQv1iz5x5NaKbpNITdhxees= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=tEPR2uts; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf05.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=tEPR2uts; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf05.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com X-Rspamd-Server: rspam12 X-Rspam-User: X-Stat-Signature: n8en37az9ftzi1xor6e94yent6fz3px5 X-Rspamd-Queue-Id: EBAAA10009C X-HE-Tag: 1655734812-706024 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 Mon, Jun 20, 2022 at 09:46:47AM -0400, Qian Cai wrote: > On Mon, Jun 20, 2022 at 08:32:27PM +0800, Miaohe Lin wrote: > > >>>>> --- 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. > > The write side should be protected by the lock swap_info_struct::lock. Is > that not the case here? > The lock does not protect the read sides. So the write side should be fixed by WRITTE_ONCE(). Thanks.