Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1462212rwb; Thu, 8 Dec 2022 10:57:51 -0800 (PST) X-Google-Smtp-Source: AA0mqf7t11hgbIFx3KykEIJprbOdEF3GqQfewUCXR73cUvPAgEMY3twvc/2jkbHFEPEOuoQrnxYM X-Received: by 2002:aa7:820e:0:b0:573:ab15:1d6f with SMTP id k14-20020aa7820e000000b00573ab151d6fmr84062315pfi.9.1670525871493; Thu, 08 Dec 2022 10:57:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670525871; cv=none; d=google.com; s=arc-20160816; b=G9ezSxzqq6ZPEm+gSkjSn63Wkfm+p4u1MLhgHvEOilcQfSQKQ6mnFX6l4Eoy95OL6K Tea9IEq7m00XecQqldIXBIRnnb2R8ONNzhxlF6yYsb6jObzTdfImbwR0rXB/tbjJUUR2 8hodep/kO6wuarlfQIfz7c2MvnaCfVsJ7TAT6aDo3veLWZo/hzqQ6+mV/o6jAKsEjdS5 YWQ5r+AAUH6m1cV1ikeatEi5X8pMDKMtaJPBy4QwtpMKuPS5Ils3Sk3y9ptEQV2QOMVh Z3s68D1nBcrnHr1qb1CYtvWOrAUP6efZKmjWNh+1FbL0kPFrCJv6s15gxCbBqM40leQP q17Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version:reply-to :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=E96Wm/nE/BO1oqx7KrRbQksoP0SM8QfWpUYCtfF7ZOY=; b=h04tEoEPva9IKCWJCYEJpZiuh7L8fJoQ4NS8Z7QRxFa3290aEp/y0FEdSKy6xGvLEv ezQrCZ7yjR/Jnek3hejGp0bunorcTxYN55fckjdJoDsENvx/9Cp0FGl8qGFUofV0QXl+ Zt8sT0cBFnJbH95az+nyQ/MaB63BKD5wmMc1Z29Y47c3c/SA1JkxJexHzLlYBT8h6tjb ilykPPAW/aDWEbLc/XtV/u9G5RbmjJiTeqJIdKsO59AOvqsJRSfpyXbby14KJf0t7EIU fArck1AiG4sPdZbCcyvVQryUr9Ux2cHxt5BtvnsyW7Nq4AO/2v0/MYKudn87AGJYxCX0 AYGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=elKCQdyB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w6-20020a63f506000000b00478bf145f38si11943956pgh.41.2022.12.08.10.57.37; Thu, 08 Dec 2022 10:57:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=elKCQdyB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229573AbiLHSDZ (ORCPT + 72 others); Thu, 8 Dec 2022 13:03:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229894AbiLHSDT (ORCPT ); Thu, 8 Dec 2022 13:03:19 -0500 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57D0D5BD60 for ; Thu, 8 Dec 2022 10:03:16 -0800 (PST) Received: by mail-pj1-x102d.google.com with SMTP id o12so2306871pjo.4 for ; Thu, 08 Dec 2022 10:03:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=E96Wm/nE/BO1oqx7KrRbQksoP0SM8QfWpUYCtfF7ZOY=; b=elKCQdyBJ0zfJyNZL9MWCMZLH4HbJp0sc16DHkwY2E5BuxBXc6+kjPqmOH0MEON8c+ x1q8YZAV5N61RQT/m3OutBjmiEN7IieOIDAJHd/hq9flNutIgFBfrmAvkwCHiWZpU968 l3IuT9uaAPkuHG77zs7QLsSUC6ld+XhS8pJMfNyXq93eNDv1gMb8XaRWqLbPCQg4FLds Zw5LVsx1I3Bf87bQVz8e4Azxrug7PBuZkoHYH/SIAcF3bH6ttLQh+TrjPhbCygJXcrfP dhI88Wq2hZmQ5pkdV4izpmRYDsPTRa9HKcydpMuA1M6ve9u4TicYtF5kp0e90xqzNGBJ sKCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=E96Wm/nE/BO1oqx7KrRbQksoP0SM8QfWpUYCtfF7ZOY=; b=4w8ywLpr/2ueM0D5+aEjz0b4IF03Y2zZ3ofn/dDlUpb5x1JiUyQHliVmlynL7GRONq GFuRvYC2zlp+8IRnksjUOzgl0FhzSYNEW8jbQT/zfAc4GpNdLDocHXFLZYH2x2RQJ6gG YKe07sATnwSo2xLYug9D1QHtI8gPRf6V3pr7NCdrii3pd8dunhM3KTEwItVtobHWPwOj aNsTeEhkXxwXv7WV3arupWlm0bj+ir/pwDkfOlONDyjKk+sBiepw8lXv/6ZPvW/w8YNB fxvgsh7Ccwnv7RNalfDSWwMNu+XfFvGYPQQdlRpJ3E03BNqi7eLNs/+SkiOvrqLZ5keV izzw== X-Gm-Message-State: ANoB5pmOKyGH2sf0GlR8NjCjW3NuTYAe83iqkT5vPJV0Bt7qAjC+g7zn 0WSCQG8vxNSM0U/ZPyzR7lw= X-Received: by 2002:a17:902:d58a:b0:189:a11e:9995 with SMTP id k10-20020a170902d58a00b00189a11e9995mr3153672plh.13.1670522595747; Thu, 08 Dec 2022 10:03:15 -0800 (PST) Received: from localhost.localdomain ([198.13.51.166]) by smtp.gmail.com with ESMTPSA id x23-20020a63db57000000b004785e505bcdsm13377909pgi.51.2022.12.08.10.03.10 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 08 Dec 2022 10:03:15 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Miaohe Lin , David Hildenbrand , "Huang, Ying" , Hugh Dickins , Kairui Song Subject: [PATCH 1/5] swapfile: get rid of volatile and avoid redundant read Date: Fri, 9 Dec 2022 02:02:05 +0800 Message-Id: <20221208180209.50845-2-ryncsn@gmail.com> X-Mailer: git-send-email 2.35.2 In-Reply-To: <20221208180209.50845-1-ryncsn@gmail.com> References: <20221208180209.50845-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kairui Song Convert a volatile variable to more readable READ_ONCE. And this actually avoids the code from reading the variable twice redundantly when it races. Signed-off-by: Kairui Song --- mm/swapfile.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 72e481aacd5d..ff4f3cb85232 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1836,13 +1836,13 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, pte_t *pte; struct swap_info_struct *si; int ret = 0; - volatile unsigned char *swap_map; si = swap_info[type]; pte = pte_offset_map(pmd, addr); do { struct folio *folio; unsigned long offset; + unsigned char swp_count; if (!is_swap_pte(*pte)) continue; @@ -1853,7 +1853,6 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, offset = swp_offset(entry); pte_unmap(pte); - swap_map = &si->swap_map[offset]; folio = swap_cache_get_folio(entry, vma, addr); if (!folio) { struct page *page; @@ -1870,8 +1869,10 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, folio = page_folio(page); } if (!folio) { - if (*swap_map == 0 || *swap_map == SWAP_MAP_BAD) + swp_count = READ_ONCE(si->swap_map[offset]); + if (swp_count == 0 || swp_count == SWAP_MAP_BAD) goto try_next; + return -ENOMEM; } -- 2.35.2