From nobody Thu Sep 18 06:56:00 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8CE8CC4167B for ; Thu, 8 Dec 2022 18:03:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229650AbiLHSDZ (ORCPT ); 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-Google-Smtp-Source: AA0mqf4pM+kGOAZ0I0/E4epPe+dakTrALDjT5h+cTK/rmfAO6gWi5Nw7yw9MBJ7OdK2iYJAqFCaERA== 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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 72e481aacd5d..ff4f3cb85232 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1836,13 +1836,13 @@ static int unuse_pte_range(struct vm_area_struct *v= ma, pmd_t *pmd, pte_t *pte; struct swap_info_struct *si; int ret =3D 0; - volatile unsigned char *swap_map; =20 si =3D swap_info[type]; pte =3D pte_offset_map(pmd, addr); do { struct folio *folio; unsigned long offset; + unsigned char swp_count; =20 if (!is_swap_pte(*pte)) continue; @@ -1853,7 +1853,6 @@ static int unuse_pte_range(struct vm_area_struct *vma= , pmd_t *pmd, =20 offset =3D swp_offset(entry); pte_unmap(pte); - swap_map =3D &si->swap_map[offset]; folio =3D 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 *vm= a, pmd_t *pmd, folio =3D page_folio(page); } if (!folio) { - if (*swap_map =3D=3D 0 || *swap_map =3D=3D SWAP_MAP_BAD) + swp_count =3D READ_ONCE(si->swap_map[offset]); + if (swp_count =3D=3D 0 || swp_count =3D=3D SWAP_MAP_BAD) goto try_next; + return -ENOMEM; } =20 --=20 2.35.2 From nobody Thu Sep 18 06:56:00 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A54C9C4332F for ; Thu, 8 Dec 2022 18:03:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229950AbiLHSD3 (ORCPT ); Thu, 8 Dec 2022 13:03:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229918AbiLHSDX (ORCPT ); Thu, 8 Dec 2022 13:03:23 -0500 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04EDB5BD60 for ; Thu, 8 Dec 2022 10:03:21 -0800 (PST) Received: by mail-pj1-x1033.google.com with SMTP id t11-20020a17090a024b00b0021932afece4so5459620pje.5 for ; Thu, 08 Dec 2022 10:03:21 -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=riLhoP57eVPqwj4BrvS8Vazxssa2eImOkYsWKI1T0t8=; b=iViaYugsyY36Co7pKi2yW3+3twUOGGTtNqIFgF0+7MBH5JAuAvr7KaDfROp3l9GO1t Srqh6rR1dXZMZbpBUXgZqXAo4mtoHxV4pakZa2i/mghgx0d8YgDWCqTQH6v3TePUuhHI yX3yaekIseUtoERJq9NEzxrgtTJrfI8d5Cp1R6AWvyBnz6vV1oUAR2djd1j/hMSt2k+Z 4+LL5CGtnmGv/5ZLisC4P7VAVLqe/cweXpWm3xgkYbR/b+jTCHWNVTBP/iMbpt1jht3L panlWfFI7BbV1LS54KzTKj/isn6g3pxCZzMObb0FaAMwDqDUsIUTZlII4ARmHeQIrm9x GrGw== 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=riLhoP57eVPqwj4BrvS8Vazxssa2eImOkYsWKI1T0t8=; b=pZ5OcCK2yfOwutj/74jiOeQGySPltPyOqYae79+vY8acZPkJckWCwOy0Rk59bTPWTj TkHQTtZsShnQNRk+DBMJJGg1DMvf+EByNGveXo/RbAW6uCkJaotmWQUIQqba5OiTjGlG dhGciw9QFdJI14tkoRNdmROiealM2Jg4818niQXVUtdxKieoVnQP+eXGIyuVAnH5Byl+ S/DZoIiarcUBSeqr8UnojihQcz3p8ilZr01Uw/NalWowd2mXb7a7ZBAapDeCTbksGFQo JjSeLy5NKzkzs/fwrg00AvM4ZF1ycuZRwzFxglvuX6H+T9QtwALeytvGHDcFODRysYtA fqsg== X-Gm-Message-State: ANoB5pk8VJG+qq9uyx/y2e1rq/AGGLiO7HVaJbW3P1KOfAscVK5UWQUv 602ll6c1fdu1viR9492O4iU= X-Google-Smtp-Source: AA0mqf6+g/Oq8lkThW8oF445SVxUDWkraBM2XVzF2FkZ356SyOkPQrUSg1X2tifk5oVpQEdOJ+/wig== X-Received: by 2002:a17:90a:8c82:b0:213:1e29:c8dd with SMTP id b2-20020a17090a8c8200b002131e29c8ddmr2896254pjo.7.1670522601220; Thu, 08 Dec 2022 10:03:21 -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.16 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 08 Dec 2022 10:03:20 -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 2/5] swap: avoid a redundant pte map if ra window is 1 Date: Fri, 9 Dec 2022 02:02:06 +0800 Message-Id: <20221208180209.50845-3-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Kairui Song Avoid a redundant pte map/unmap when swap readahead window is 1. Signed-off-by: Kairui Song Reviewed-by: "Huang, Ying" --- mm/swap_state.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index 438d0676c5be..60136bda78e3 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -730,8 +730,6 @@ static void swap_ra_info(struct vm_fault *vmf, } =20 faddr =3D vmf->address; - orig_pte =3D pte =3D pte_offset_map(vmf->pmd, faddr); - fpfn =3D PFN_DOWN(faddr); ra_val =3D GET_SWAP_RA_VAL(vma); pfn =3D PFN_DOWN(SWAP_RA_ADDR(ra_val)); @@ -742,12 +740,11 @@ static void swap_ra_info(struct vm_fault *vmf, atomic_long_set(&vma->swap_readahead_info, SWAP_RA_VAL(faddr, win, 0)); =20 - if (win =3D=3D 1) { - pte_unmap(orig_pte); + if (win =3D=3D 1) return; - } =20 /* Copy the PTEs because the page table may be unmapped */ + orig_pte =3D pte =3D pte_offset_map(vmf->pmd, faddr); if (fpfn =3D=3D pfn + 1) swap_ra_clamp_pfn(vma, faddr, fpfn, fpfn + win, &start, &end); else if (pfn =3D=3D fpfn + 1) --=20 2.35.2 From nobody Thu Sep 18 06:56:00 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E2D2CC4332F for ; Thu, 8 Dec 2022 18:03:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229785AbiLHSDe (ORCPT ); Thu, 8 Dec 2022 13:03:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229894AbiLHSD1 (ORCPT ); Thu, 8 Dec 2022 13:03:27 -0500 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2CD845BD60 for ; Thu, 8 Dec 2022 10:03:27 -0800 (PST) Received: by mail-pj1-x102a.google.com with SMTP id v13-20020a17090a6b0d00b00219c3be9830so2337108pjj.4 for ; Thu, 08 Dec 2022 10:03:27 -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=sO5CoFHWHd3hJkT66P6565Tno5APV7gOVtSSvr1Kmak=; b=OpJs82IF483zMRR10B9ZK8uXKr26ww50hoZUuCpAvcj54ywnyFbiRuv6Hmy84v5RFq LvLarleVe0COhyAsoDAHvPMzpgopqUQQKO2ZbnnACbczT6d8YP0h0Wp/uJBcnmLwKfYn RnVLhdnDZPZIKpJVOKRkH69LIOnMGPC2BGyImnquIEmGGpbuHsOgGyJsgy2MSMcHx1rf Vq87c4y6s5XMnWQnnDuT5+NYeWEnBW/e5m5cbZwB04aLDpJV98fF+pn7N5Sdj3x+dsLn XMzHsVtrUQslsJQolDztnRuuDqwC7u5gtWQVj7HhF/cd/6Lh2plHxGOrsnFmAuNWKFGZ +yfg== 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=sO5CoFHWHd3hJkT66P6565Tno5APV7gOVtSSvr1Kmak=; b=NmKAOksxwG4aXpwsTjVn0V9cZpCfQnIguaXLA+Fc+ASg+dpkg0CeRnswy+86RYds2G 1zoLvhmm3cZpEDPehFemvSe5o6x7+6LamVrSjqiOdG03cmFjc5bW0tLbULnJb/6CXZLb EwIbgPjjejac9vs3XIFm/nPXUx4McgdcHrKzLZNiYoJjgUSvvpq3MK0QAVn3IbYBzRdl +LzsWtRLRhUE4IPnkH4qIP7ZVz1Cszd4zUiHZ8P92jkLVc29JS0qJRaLOd3zXfia7bh0 EQm1MnyDakHuhn5MeXSHzTO31+QQOGr2KSB+6sbWT8p8ysFzf/f9fhH+PyMpcHGLh0lH 59Yw== X-Gm-Message-State: ANoB5pkYNSOHhnSnlik8xV+RpD+mczLh7EtAX74weQ4pMZXlpdvJn9T7 ryozqijGkTZi86juV9RF1CI= X-Google-Smtp-Source: AA0mqf4wqo11j3YseWJivRlvVsI70evGtgCftTqnGdlt4+R5iI2+u8R1yWbvNTkV85f+uZLnMttwbg== X-Received: by 2002:a17:90a:d347:b0:219:9676:fef5 with SMTP id i7-20020a17090ad34700b002199676fef5mr2840489pjx.12.1670522606635; Thu, 08 Dec 2022 10:03:26 -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.21 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 08 Dec 2022 10:03:26 -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 3/5] swap: fold swap_ra_clamp_pfn into swap_ra_info Date: Fri, 9 Dec 2022 02:02:07 +0800 Message-Id: <20221208180209.50845-4-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Kairui Song This make the code cleaner. This helper is made of only two line of self explanational code and not reused anywhere else. And this actually make the compiled object smaller by a bit: text data bss dec hex filename Before: 9502 976 12 10490 28fa mm/swap_state.o After: 9470 976 12 10458 28da mm/swap_state.o Signed-off-by: Kairui Song Reviewed-by: "Huang, Ying" --- mm/swap_state.c | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index 60136bda78e3..19089417abd1 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -696,28 +696,15 @@ void exit_swap_address_space(unsigned int type) swapper_spaces[type] =3D NULL; } =20 -static inline void swap_ra_clamp_pfn(struct vm_area_struct *vma, - unsigned long faddr, - unsigned long lpfn, - unsigned long rpfn, - unsigned long *start, - unsigned long *end) -{ - *start =3D max3(lpfn, PFN_DOWN(vma->vm_start), - PFN_DOWN(faddr & PMD_MASK)); - *end =3D min3(rpfn, PFN_DOWN(vma->vm_end), - PFN_DOWN((faddr & PMD_MASK) + PMD_SIZE)); -} - static void swap_ra_info(struct vm_fault *vmf, - struct vma_swap_readahead *ra_info) + struct vma_swap_readahead *ra_info) { struct vm_area_struct *vma =3D vmf->vma; unsigned long ra_val; - unsigned long faddr, pfn, fpfn; + unsigned long faddr, pfn, fpfn, lpfn, rpfn; unsigned long start, end; pte_t *pte, *orig_pte; - unsigned int max_win, hits, prev_win, win, left; + unsigned int max_win, hits, prev_win, win; #ifndef CONFIG_64BIT pte_t *tpte; #endif @@ -745,16 +732,23 @@ static void swap_ra_info(struct vm_fault *vmf, =20 /* Copy the PTEs because the page table may be unmapped */ orig_pte =3D pte =3D pte_offset_map(vmf->pmd, faddr); - if (fpfn =3D=3D pfn + 1) - swap_ra_clamp_pfn(vma, faddr, fpfn, fpfn + win, &start, &end); - else if (pfn =3D=3D fpfn + 1) - swap_ra_clamp_pfn(vma, faddr, fpfn - win + 1, fpfn + 1, - &start, &end); - else { - left =3D (win - 1) / 2; - swap_ra_clamp_pfn(vma, faddr, fpfn - left, fpfn + win - left, - &start, &end); + if (fpfn =3D=3D pfn + 1) { + lpfn =3D fpfn; + rpfn =3D fpfn + win; + } else if (pfn =3D=3D fpfn + 1) { + lpfn =3D fpfn - win + 1; + rpfn =3D fpfn + 1; + } else { + unsigned int left =3D (win - 1) / 2; + + lpfn =3D fpfn - left; + rpfn =3D fpfn + win - left; } + start =3D max3(lpfn, PFN_DOWN(vma->vm_start), + PFN_DOWN(faddr & PMD_MASK)); + end =3D min3(rpfn, PFN_DOWN(vma->vm_end), + PFN_DOWN((faddr & PMD_MASK) + PMD_SIZE)); + ra_info->nr_pte =3D end - start; ra_info->offset =3D fpfn - start; pte -=3D ra_info->offset; --=20 2.35.2 From nobody Thu Sep 18 06:56:00 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5119C4332F for ; Thu, 8 Dec 2022 18:03:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229870AbiLHSDu (ORCPT ); Thu, 8 Dec 2022 13:03:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229948AbiLHSDf (ORCPT ); Thu, 8 Dec 2022 13:03:35 -0500 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD8B1AD99D for ; Thu, 8 Dec 2022 10:03:32 -0800 (PST) Received: by mail-pl1-x629.google.com with SMTP id y4so2276709plb.2 for ; Thu, 08 Dec 2022 10:03:32 -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=9xhnDKrjvRgwnAVDsuEDmotQGE1PCbtaJeEpVqamujQ=; b=fYS6yck+ReWd2uxK6tV27Mwp2zNKPzSSjv+tEjGFN0KC2KzjO1Lrmvll2fXcklu/xD 9vsO4te8pgwd8ikSvVhnd3cwQx6jAaJ+vsCjpJpWAcPWuMpTh4kMHQ9/dylSB1sgEc4o boBlj0P69dZgpYTOUuCtE/bC1caINvJKSvgBOFJhQlKFahGnFN6IKytmc7ep9fmjJmPD 3aA6IRu+YJpaJSgbmFX3HXbLKCae+EEYKDBMiBDanJtFjy9xSdATtmt6iO7JXOaXc9+c EalfcbAgPYUi2n4VnkKdxQR6xvSx4sNswYuOd7MIrzR2KOOO13g8om7jQ1QjQuO11Lmi /Jjg== 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=9xhnDKrjvRgwnAVDsuEDmotQGE1PCbtaJeEpVqamujQ=; b=0p5DgSfURVcPNlhHE7WBVAI9QDd2gexfawfrqC6/rM4QIcoe7iHNWUYahJ+tF8wsqP ZhK08EfMWQei/UelzFXalXzMfnDOXHsQn5rgcgPby3bRq9zGzoUvzJApJmjW4Mvnrhpo hBMaeMvDXbIIMm6SymfmPCnWVSAGL6CmIORqt5Pu4cuEY0p20/YK1qYs+lc0d4LLFF2r 2yCaX89nKjiEVkJnv9Mar3NNkDVB9obGw5bkdQ9D3fQMh9I0ZChDR1vJVSiwrWa+m1MH PGIt2lxerxriMRoBBskhx7SZqSd6zy8cjo9oBTHt5lvW0uf3BCHhGf+wa9UJyOs/H0wn Cgbg== X-Gm-Message-State: ANoB5pnFWgvyPhNxFpkndM9xYyQHMEibp9NoY62tQOCm2V0bYy9472mg Ro/r6A1LnPXOVOXddaMSjEw= X-Google-Smtp-Source: AA0mqf40eA3n9hLVOhVvV4ReS4Fw/NzKyEAVdLHHp5a3pHjwNFExX1/Jxq1YuAjRsdgd/lCwVPtNjw== X-Received: by 2002:a05:6a20:2d29:b0:a3:ba8a:43ea with SMTP id g41-20020a056a202d2900b000a3ba8a43eamr4354489pzl.58.1670522612163; Thu, 08 Dec 2022 10:03:32 -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.27 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 08 Dec 2022 10:03:31 -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 4/5] swap: remove the swap lock in swap_cache_get_folio Date: Fri, 9 Dec 2022 02:02:08 +0800 Message-Id: <20221208180209.50845-5-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Kairui Song There is only one caller not keep holding a reference or lock the swap device while calling this function. Just move the lock out of this function, it only used to prevent swapoff, and this helper function is very short so there is no performance regression issue. Help saves a few cycles. Signed-off-by: Kairui Song --- mm/shmem.c | 8 +++++++- mm/swap_state.c | 8 ++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index c1d8b8a1aa3b..0183b6678270 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1725,6 +1725,7 @@ static int shmem_swapin_folio(struct inode *inode, pg= off_t index, struct address_space *mapping =3D inode->i_mapping; struct shmem_inode_info *info =3D SHMEM_I(inode); struct mm_struct *charge_mm =3D vma ? vma->vm_mm : NULL; + struct swap_info_struct *si; struct folio *folio =3D NULL; swp_entry_t swap; int error; @@ -1737,7 +1738,12 @@ static int shmem_swapin_folio(struct inode *inode, p= goff_t index, return -EIO; =20 /* Look it up and read it in.. */ - folio =3D swap_cache_get_folio(swap, NULL, 0); + si =3D get_swap_device(swap); + if (si) { + folio =3D swap_cache_get_folio(swap, NULL, 0); + put_swap_device(si); + } + if (!folio) { /* Or update major stats only when swapin succeeds?? */ if (fault_type) { diff --git a/mm/swap_state.c b/mm/swap_state.c index 19089417abd1..eba388f67741 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -324,19 +324,15 @@ static inline bool swap_use_vma_readahead(void) * unlocked and with its refcount incremented - we rely on the kernel * lock getting page table operations atomic even if we drop the folio * lock before returning. + * + * Caller must lock the swap device or hold a reference to keep it valid. */ struct folio *swap_cache_get_folio(swp_entry_t entry, struct vm_area_struct *vma, unsigned long addr) { struct folio *folio; - struct swap_info_struct *si; =20 - si =3D get_swap_device(entry); - if (!si) - return NULL; folio =3D filemap_get_folio(swap_address_space(entry), swp_offset(entry)); - put_swap_device(si); - if (folio) { bool vma_ra =3D swap_use_vma_readahead(); bool readahead; --=20 2.35.2 From nobody Thu Sep 18 06:56:00 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3C8FBC4332F for ; Thu, 8 Dec 2022 18:04:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230020AbiLHSED (ORCPT ); Thu, 8 Dec 2022 13:04:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60184 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230007AbiLHSDq (ORCPT ); Thu, 8 Dec 2022 13:03:46 -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 38676AE4F4 for ; Thu, 8 Dec 2022 10:03:38 -0800 (PST) Received: by mail-pj1-x102b.google.com with SMTP id e7-20020a17090a77c700b00216928a3917so5464233pjs.4 for ; Thu, 08 Dec 2022 10:03:38 -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=ltPbBrCKZDS7mJn+bR+RqAKCWGPP/Hc1SQkH2hWVmXg=; b=hRgCXWOPDDo7RlbXFKoM69xuCbwt8WjzTaUsg01akGxMhNwABD12DyylexVFkD/7Mu M+qrq5HV/MDlhFQ2jxpJkr9HNBE7YMfEZaXVGRTNrwlZFE00eokpEecQWj3j6JrbPCbA 1zHxzkaBVjYkjr1s2oWBVUR/5nbzmiSc+/4+6qJl50hF8eFgPh9NnQuk0lo+FhfUqNe6 F3QleTfoMD2zNv1+SBcEq0q4ftrAofnh81ysKVSlErGBas3RbClmzj9QYTQztCMaIqID gUiM72cYqHlrpjl8RiTv3askqJOawjDqYKnOo2KlhUkvFGpDTtXyhTuqVoXIozJUmNZa Jslw== 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=ltPbBrCKZDS7mJn+bR+RqAKCWGPP/Hc1SQkH2hWVmXg=; b=S8GbmdEaaJ15ZfSu89GZnVlKJeE2tH/TE8XGzvRWNebysPFY7GUehDE84EXQCGVG0b xMTrBfJk1IVFdCYcz3hQJ1iNm/sqWerwxAIe7qr/Vui2ojXOX4JSfhEtSc9kTCeVcjfX MhDFvswpvCwi2DDSL7tekgo8hFF6Nq6RssRyabUHXKTZ1pM/k0G91jumERUvN6wCMWP3 rWznlWXlrfpl3DLU2DGtvFv4JW81F6nidahWbkln8/E4nx2zxFRK7us/Uo8AQPsy/bQp aSGrN6xI2iV7RoLL25JZ+mVNhmYuKN3CZEe7oavR9X7hQVl4wCaQZAIeqow+cOkR2zMz n3AQ== X-Gm-Message-State: ANoB5ply8ySWdXqsekZBmbo+21OrIE+1FfirJGQla69sU6ee9CF80pVJ VPNgRtyG6VGWik56JRWplvc= X-Google-Smtp-Source: AA0mqf7l35N5CcqRRUA9kCptWivxf7XeTYnN81QwD1VXUWMNcNSdS0Hq0NDpeaJYmfVtAUqrHE18Iw== X-Received: by 2002:a17:90a:7606:b0:219:823e:6726 with SMTP id s6-20020a17090a760600b00219823e6726mr2920444pjk.19.1670522617801; Thu, 08 Dec 2022 10:03:37 -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.32 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 08 Dec 2022 10:03:37 -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 5/5] swap: avoid ra statistic lost when swapin races Date: Fri, 9 Dec 2022 02:02:09 +0800 Message-Id: <20221208180209.50845-6-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Kairui Song __read_swap_cache_async should just call swap_cache_get_folio for trying to look up the swap cache. Because swap_cache_get_folio handles the readahead statistic, and clears the RA flag, looking up the cache directly will skip these parts. And the comment no longer applies after commit 442701e7058b ("mm/swap: remove swap_cache_info statistics"), just remove them. Fixes: 442701e7058b ("mm/swap: remove swap_cache_info statistics") Signed-off-by: Kairui Song --- mm/swap_state.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index eba388f67741..f39cfb62551d 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -418,15 +418,12 @@ struct page *__read_swap_cache_async(swp_entry_t entr= y, gfp_t gfp_mask, for (;;) { int err; /* - * First check the swap cache. Since this is normally - * called after swap_cache_get_folio() failed, re-calling - * that would confuse statistics. + * First check the swap cache in case of race. */ si =3D get_swap_device(entry); if (!si) return NULL; - folio =3D filemap_get_folio(swap_address_space(entry), - swp_offset(entry)); + folio =3D swap_cache_get_folio(entry, vma, addr); put_swap_device(si); if (folio) return folio_file_page(folio, swp_offset(entry)); --=20 2.35.2