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 X-Spam-Level: X-Spam-Status: No, score=-8.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 73E31C433B4 for ; Sun, 25 Apr 2021 04:20:25 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E53CF61404 for ; Sun, 25 Apr 2021 04:20:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E53CF61404 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=intel.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 05AC86B0036; Sun, 25 Apr 2021 00:20:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 00AE06B006C; Sun, 25 Apr 2021 00:20:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DC72B6B006E; Sun, 25 Apr 2021 00:20:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0160.hostedemail.com [216.40.44.160]) by kanga.kvack.org (Postfix) with ESMTP id BD7656B0036 for ; Sun, 25 Apr 2021 00:20:23 -0400 (EDT) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 6F39A52B3 for ; Sun, 25 Apr 2021 04:20:23 +0000 (UTC) X-FDA: 78069587526.10.CA8CFC6 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by imf01.hostedemail.com (Postfix) with ESMTP id B96A95001530 for ; Sun, 25 Apr 2021 04:20:17 +0000 (UTC) IronPort-SDR: Ssi1/yIRNAlABnNBZTijZdEmr2mF7CC+koCkUaCdMGp9zwL7jgN31FTpinsmMnQlumA+PsS950 QMp51YFv+icQ== X-IronPort-AV: E=McAfee;i="6200,9189,9964"; a="216903211" X-IronPort-AV: E=Sophos;i="5.82,249,1613462400"; d="scan'208";a="216903211" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Apr 2021 21:20:20 -0700 IronPort-SDR: ym8i6ju2tJwF/ZR3B2VcI2uSfKn88r2osxdbyTNHkYF7bbyB0Tssbnf2Mfov/F4fTM5Pywopsp 2mkKVVhlvOaw== X-IronPort-AV: E=Sophos;i="5.82,249,1613462400"; d="scan'208";a="428932092" Received: from yhuang6-desk1.sh.intel.com (HELO yhuang6-desk1.ccr.corp.intel.com) ([10.239.13.1]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Apr 2021 21:20:15 -0700 From: "Huang, Ying" To: Miaohe Lin Cc: , , , , , , , , , , , , , , Subject: Re: [PATCH v4 4/4] mm/shmem: fix shmem_swapin() race with swapoff References: <20210425023806.3537283-1-linmiaohe@huawei.com> <20210425023806.3537283-5-linmiaohe@huawei.com> <87bla3xdt0.fsf@yhuang6-desk1.ccr.corp.intel.com> <0213893e-2b05-8d2e-9a79-e8a71db23644@huawei.com> Date: Sun, 25 Apr 2021 12:20:13 +0800 In-Reply-To: <0213893e-2b05-8d2e-9a79-e8a71db23644@huawei.com> (Miaohe Lin's message of "Sun, 25 Apr 2021 11:33:42 +0800") Message-ID: <87y2d7vvuq.fsf@yhuang6-desk1.ccr.corp.intel.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=ascii X-Stat-Signature: 8ybyggb5up8j56tu1meyf3zhyschfcn4 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: B96A95001530 Received-SPF: none (intel.com>: No applicable sender policy available) receiver=imf01; identity=mailfrom; envelope-from=""; helo=mga01.intel.com; client-ip=192.55.52.88 X-HE-DKIM-Result: none/none X-HE-Tag: 1619324417-179214 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: Miaohe Lin writes: > On 2021/4/25 11:07, Huang, Ying wrote: >> I think it's better to put_swap_device() just before returning from the >> function. It's not a big issue to slow down swapoff() a little. And >> this will make the logic easier to be understood. >> > > shmem_swapin_page() already has a methed, i.e. locked page, to prevent races. I was intended > to not mix with that. But your suggestion is good as this will make the logic easier to be > understood. > > Just to make sure, is this what you mean? Many thanks! Yes. Just a minor comment. > > diff --git a/mm/shmem.c b/mm/shmem.c > index 26c76b13ad23..737e5b3200c3 100644 > --- a/mm/shmem.c > +++ b/mm/shmem.c > @@ -1696,6 +1696,7 @@ static int shmem_swapin_page(struct inode *inode, pgoff_t index, > struct address_space *mapping = inode->i_mapping; > struct shmem_inode_info *info = SHMEM_I(inode); > struct mm_struct *charge_mm = vma ? vma->vm_mm : current->mm; > + struct swap_info_struct *si; > struct page *page; > swp_entry_t swap; > int error; > @@ -1704,6 +1705,12 @@ static int shmem_swapin_page(struct inode *inode, pgoff_t index, > swap = radix_to_swp_entry(*pagep); > *pagep = NULL; > > + /* Prevent swapoff from happening to us. */ > + si = get_swap_device(swap); > + if (unlikely(!si)) { I don't think it's necessary to use unlikely() here. Best Regards, Huang, Ying > + error = EINVAL; > + goto failed; > + } > /* Look it up and read it in.. */ > page = lookup_swap_cache(swap, NULL, 0); > if (!page) { > @@ -1765,6 +1772,8 @@ static int shmem_swapin_page(struct inode *inode, pgoff_t index, > swap_free(swap); > > *pagep = page; > + if (si) > + put_swap_device(si); > return 0; > failed: > if (!shmem_confirm_swap(mapping, index, swap)) > @@ -1775,6 +1784,9 @@ static int shmem_swapin_page(struct inode *inode, pgoff_t index, > put_page(page); > } > > + if (si) > + put_swap_device(si); > + > return error; > } > >> Best Regards, >> Huang, Ying >>