Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp2970823rwj; Mon, 19 Dec 2022 11:06:43 -0800 (PST) X-Google-Smtp-Source: AA0mqf6X0pC+vuXKwUnPrhqN5Sg8LJWgRATaWTEWN9NUQrPwM/PoZGIEuCGfnIJe1ZUe2+K19nAi X-Received: by 2002:aa7:cf0c:0:b0:470:410a:8ea7 with SMTP id a12-20020aa7cf0c000000b00470410a8ea7mr38338921edy.18.1671476802884; Mon, 19 Dec 2022 11:06:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671476802; cv=none; d=google.com; s=arc-20160816; b=nkhLKV8YtyxW2laKiqEz8kayiHKNGx+9lFepVDbNDsT3EO+EtFZGhyQylKysDkNzcn Sm0FoXGrfuCUrI6D5iVrs1KFSsxBSokfvuX+j5iW3CehCedUooALXYZjnk24USBHwUDg Fiu8gi8iAZ2DE12bsxa5mlhaTNidARyOAnUe6UMdurGT0KCxWyBgLAMvLBayhK7zw3tT Dj6sWYLpbUTgw6v1HMIkW82c80gSaZ1ZZyr87nlJf9gjeAgUB75MfFZGQ2w31BOcLQW4 8hGTvQFCf8RipclrqR04M69JdvAFJDsis5ZrO7l4NxiMP67kD3M1DhrSgCaHo+0Zbhx4 qOWA== 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=XocXQJ8/KIfX9/nTT1Ghmvl4XAcC2jwtLySqVPgnO78=; b=bUailLbhg6MnIMOQoFdE008I24LeKF81g3snaj2DlIbibPkLohkAXSrkGjiGMNQjRh wrcSQWTqIytvneTzF1sxmzZmEKztv0PwFrCptkj3rrW5XMYvSnU5MuTMTQXF6fdiJD8U Xh6E84AIYpEYNosTgVVzzsHcMUqzGvzeOYb69ViOcZrhJt/89NnHACcD1iochF38tC4o nsBF/gsSb33YFtrECF8hQKGScgu7DyvlkmqPnobPaVwecM2LShgqgyPTRf1XA19P8aFq RiLnpWiqjJf4Dnzb8ubcAb3Ql729BGkN0KWcMSRfy+AY01MsevC8zN8QinqnauI20fvJ lo9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=gTdt6BQe; 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 y25-20020a50eb99000000b0046fa5a78da1si8868488edr.355.2022.12.19.11.06.26; Mon, 19 Dec 2022 11:06:42 -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=gTdt6BQe; 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 S232171AbiLSTA2 (ORCPT + 71 others); Mon, 19 Dec 2022 14:00:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232488AbiLSTAB (ORCPT ); Mon, 19 Dec 2022 14:00:01 -0500 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB1A76585 for ; Mon, 19 Dec 2022 10:59:16 -0800 (PST) Received: by mail-pj1-x102b.google.com with SMTP id u5so10022956pjy.5 for ; Mon, 19 Dec 2022 10:59: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=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=D3GxN6IM1VWdCuz4lGvkOldxbz8aa8quQfkJQzlS3wuTfIRPn5WxPm/4abSSBXDTDy n1MG6HfTy2hn99bXzPxR7q2gsInjtgW/A2aPqZZ+aoOUL0BhV8/8NKNwcFlQ4SvNzOgv 7Rz5DrDdQ667mMhLzb5U1gw6fVwL/r8naRKMlYi+Ari2CJsoBh09yZF2smcZT4nsflYX gyVEiylEk4/SlvLuC1sqL8zj+l2GA9STl75WwUB9zLEjqiHx/0m8yjvjGk1YlDg1Ir4J fHpCQIyb2E8MDsQb4Fj0tyY+Gesnr8lVmsHLsXi1oaUBq0On1p4ZJn1+ntVU060w+pFd 9WPQ== X-Gm-Message-State: ANoB5pmGsecc5B9HMYLbQp/rzY5pbzSxl+CjQCPyV8DAA4b+HNTkQB2G QgVejTy+Z3q/DPygWpo2hVI= 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 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 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; } -- 2.35.2