From patchwork Mon Dec 19 18:58:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13077054 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 A25C0C4332F for ; Mon, 19 Dec 2022 18:59:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 434898E0003; Mon, 19 Dec 2022 13:59:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3E4A88E0001; Mon, 19 Dec 2022 13:59:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2AC6F8E0003; Mon, 19 Dec 2022 13:59:20 -0500 (EST) 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 1C1348E0001 for ; Mon, 19 Dec 2022 13:59:20 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id E7EB4AB0EF for ; Mon, 19 Dec 2022 18:59:19 +0000 (UTC) X-FDA: 80259968838.29.E1466A3 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf20.hostedemail.com (Postfix) with ESMTP id 3685E1C0010 for ; Mon, 19 Dec 2022 18:59:17 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=gTdt6BQe; spf=pass (imf20.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671476358; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XocXQJ8/KIfX9/nTT1Ghmvl4XAcC2jwtLySqVPgnO78=; b=fg08vxl8XdlPA7SENrHVptBHlbBz4VFGK/JbvdWuu68hHgPhcAY/mO0HNli0PCXIDtSGkN yvv7zMBBXZFND0PlcoJjeo9anyziZLPbqu8V5nQHgFnbhkd41H+PLH+Je+dOfQrj/7tzWb /EwuTcKNd7HAGz5z1lZa8+VtqgZ1GZ4= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=gTdt6BQe; spf=pass (imf20.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671476358; a=rsa-sha256; cv=none; b=1F5r/GT6gpjkZ30jOAmh0wnT0K9mfZLBGuzJcwrXLPZDWkgFx5KnBP2KHNwL4VjCGFoDGe yBDQNk9PGNq4ICYLk+/dxywndM/++8niI9bTaK4/+kdJDtO3ZQip7NLSb3+yRpxQEA7L+n Udfa67tDZ4XmxxJrxAEje0lhoSC3OMg= Received: by mail-pl1-f172.google.com with SMTP id d15so9939677pls.6 for ; Mon, 19 Dec 2022 10:59:17 -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=XocXQJ8/KIfX9/nTT1Ghmvl4XAcC2jwtLySqVPgnO78=; b=gTdt6BQeQx60iQ2xEpVkG9N2cVTn02TqELcNPrH2DPQHOHfHgBH7nbVVm4lJ0If6bi u8KJrL4+epaHOp5Ca5289ihVcOtRR8nvfWgIYq4R4IUNs45HAdcXI2X0EmzFMGuLVQRm rYddyOZNUm6HMseaqPywQxKdvhG2PrAOir8HWF8OlQ3muwxqLRO1dpkL6Eof7Lc5DHsW nExx/9uTYg/ylbOa4u5j4XAy35yUDRjgsu1L3drtgnxRxc5xbLN9wgJjbTR5fPIq5k7h kjMeYKpySfSdX4ZrmK7ECOULbiV6BH1SAgg3unw12ilu4j/ouOJRVoSSO7S4krtdRzZV sWaQ== 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=XocXQJ8/KIfX9/nTT1Ghmvl4XAcC2jwtLySqVPgnO78=; b=LZohOwvqLpzrYmvLacKKKd4vT48xUE6nbQylbg/lKijK1mrhOV41/sdV4445polOEu C1l83EU5dcz3KIWHDxY0tfo7iOLKOLlVNWNqaRjdAAFeAslBjR41c3rp04uMli+Ti0cV s4QDoX6EPCM7Nq8oYydf36t8T+d6IzGQ2EbBGIIFGL0rIR7COg2jjJVIPWSzpv5J6GLe QI96mUOShL8GJ4+mWUWxdPzdKWYkvPju7gtmF3uTq8MRQezwhHpIW49sI4r0CwB63piM PWFcTxW73MqpXJiaXnBYmKjAyNSO+7XW/g3oSF1GhETtJmXQJCkS1I+W4xv18M8hEqzA LO6Q== X-Gm-Message-State: ANoB5pnSy6v5rFQK7LTNGgNRj9lZ35duo5OpRfQQ8MLV80BVR2N//+O+ tsRBPaSE/cGxHb84DAykd0YoIJMlPocSRu+f X-Google-Smtp-Source: AA0mqf5+mjYb7s7cWrqX58p+GXu392SobpICcwlH+tQQtjEkCkRadzwoFuctBrNg/jxllqNTLEPk4Q== X-Received: by 2002:a17:902:8209:b0:189:851d:644c with SMTP id x9-20020a170902820900b00189851d644cmr40913337pln.40.1671476356452; Mon, 19 Dec 2022 10:59:16 -0800 (PST) Received: from localhost.localdomain ([198.13.51.166]) by smtp.gmail.com with ESMTPSA id i6-20020a170902c94600b00189847cd4acsm7480790pla.237.2022.12.19.10.59.10 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 19 Dec 2022 10:59: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 , Matthew Wilcox , Kairui Song Subject: [PATCH v2 1/4] swapfile: get rid of volatile and avoid redundant read Date: Tue, 20 Dec 2022 02:58:37 +0800 Message-Id: <20221219185840.25441-2-ryncsn@gmail.com> X-Mailer: git-send-email 2.35.2 In-Reply-To: <20221219185840.25441-1-ryncsn@gmail.com> References: <20221219185840.25441-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 3685E1C0010 X-Stat-Signature: shwiyixss6f4mf3zco1b147jx93mcquw X-HE-Tag: 1671476357-272018 X-HE-Meta: U2FsdGVkX19OJeigg7zYXf9P2LKZLAZI8XcbVBfh1z+ocXIijFJmTAmS8Saynx2cB74VUWQLJ+6X8Vn+K/q9GPTumq0SXmTTaonKudWcA+eRyFzpnibekzedczleWk5eVBGRmEjEfNE2hV/kSOJgSBWWzXl96GdDe1fd0icMZ8F3TgS09ZMYXETzuOGWGuWMgkyWDByAs+fWRx8NG4UkqIbvqtRPwinw0KPaYTN4L7VS/IjSed/OnfsVIsKDIm4BjoHfzpTXcfor7+7MSNIM9DnEDirK7yqneScEd6/j+Dy2wQGfUA/yXYENVA1XT5feGTRI3uyIGgTU2OPpQUdfpA9LRD420KupARUV5iV1aBrGeMxPABIZhL55wbrq/sMDWkVyGSM92Wtt7IY1RlJXgk+P1jxWd8KPY4s3BgpMEatPuc0YUuvNqwSaZJfhhsDu+9RvPJ03HohJd54aXYjkPZnO7z7e9tl0lTqqZm7EjVOw/XJsQnRJFYPI/t6VIEYYrbKbjGmbbhCFax6ckZSCqCir55SVCuySWrbic1kqCpuXTV1432rbou7NFebBeIGOFno1JqQrjy0X0QtRY/kaikWdJlQ529kzDI365ZjQ+65PUCweuvKE52mwC0brB/0liBRRPtVG1YDZbAInxJ5dFfahERogoJtvWhHEvuQe6mcEglSSvrXe05Hm7HaSHTIMeflianrl/PGEJEPUurFqn43AVKM40txu/bqV+Qrm4oKqFRkPYyaxK/cjKYturIoiN8iIHJUxMOUVt5Db9L3rJugTE0DmSrKw1PgeHRIemavOcV9jqKJywKPaN3kyyMwacBqEe1WD1Egfy6DDpwg7IV+M43hKYW53kcdls0qSmv3jJaR3eFTn+jgWnaCWla5nvbi+9VeOBH76RaWjz56jm7OJ5c1NR6DPwG2EtqJX1q3bO+rpIMpjtRmXBAzd9zpd1pgVL3eQY3uj/JvzzYO Ux+DurOe ORsyIoS9iYFGIDrHPD4Mj6xOCtP5FMCNZ+TpT9semKPn6Nslzzk/aHs55+CN6ma/ulX/FrziYdGmIj8qYl/EbX6zaQRL37c5bADGLJFKpF1Y2lrUNvPit0eknZJg6/l9VL2N+X1sSCbn43invGz2tWF0GbHEFF7wGHuJzvW344DCJjkufpabp6XvV6N4JgzNYDGxjd7CK1LHBnm3fkgCQCZTguR54K1nDvhLxoh+uvfUO7FVfL+EdqIa0cPzaS/0zeIWRRXNaIwGivMDSkYbYHeSvyA== 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: 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 Reviewed-by: "Huang, Ying" --- mm/swapfile.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 908a529bca12..6d3f60bd383b 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1835,13 +1835,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; @@ -1852,7 +1852,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; @@ -1869,8 +1868,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; }