From nobody Sat Apr 11 15:28:08 2026 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 B3202C25B08 for ; Tue, 9 Aug 2022 22:02:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229657AbiHIWBn (ORCPT ); Tue, 9 Aug 2022 18:01:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229482AbiHIWBJ (ORCPT ); Tue, 9 Aug 2022 18:01:09 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1C48DE06 for ; Tue, 9 Aug 2022 15:01:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660082466; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ViWF5oZvZDnfPWYKxgTTuTS7ZUI2Vs+P8BNbSIRNDkc=; b=Wtx9ntMVoao6aSfVQwtkLU6f3kgNof32gE01lYNzjtbXaad5b65BSH0SQc9lObdlVr9ErG QyNdDX0H6a9FJwg3A25sIXJH1/HjzyeYhN7t6Q1bsWrqcFWYcYjsw8aOE1h/dmTEpRS95l w3R0PyIGHncqnJrsT3iqo1hi4geM6Bk= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-263-B9-a_gNlORGc0xyM_TTrtQ-1; Tue, 09 Aug 2022 18:01:05 -0400 X-MC-Unique: B9-a_gNlORGc0xyM_TTrtQ-1 Received: by mail-qv1-f70.google.com with SMTP id cz12-20020a056214088c00b004763e7e7d81so7006852qvb.21 for ; Tue, 09 Aug 2022 15:01:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=ViWF5oZvZDnfPWYKxgTTuTS7ZUI2Vs+P8BNbSIRNDkc=; b=uezud8FCXyFM9JD+W7M/1Snj//sv2xM4+FEVUZEVk5qfRUJu7VKmsGxAbHCmCemHRP sH/GHYItVz7kPBIRd+3Yt8uQ55oquUq/P1TYkhk8Rlw3udiA+ByNGaYIvCodcebxTyeo ZpvXtZafJFzHCrA7nUntKLVnJ/cbZ7d5SS5qUrlvYweQlekM2DJDgquJTtwifSLTjAMw MZW66vlLZmDfjOTU7LuEbVp9+CXMZeuKIRrf7LHqZ3h82r3kw/mcCAyVwu68iJadzjOm 6UJ6DM4u+nyja2xc8F66YgMwjZdY0GNf4CORfS/9DPuxcqpnyvs16wzH9oiSv5ImYk+n j01g== X-Gm-Message-State: ACgBeo1DEcAJqNk5OqY9eqLEmv/vY323C1zLTW4WJX4rpETs/eqtIGES um80k0K9wi93ltx6QWTJYQPIQrBROQgpi0YAH9hMLM6jEDgH3G9dMKDrBNJ38nVCDX5uGlbEork vKWUh/SiWGpihy+txQcywYjbw X-Received: by 2002:a05:620a:2552:b0:6b4:8dbf:8992 with SMTP id s18-20020a05620a255200b006b48dbf8992mr18881455qko.109.1660082464662; Tue, 09 Aug 2022 15:01:04 -0700 (PDT) X-Google-Smtp-Source: AA6agR6dluwUjmka3W+U32hW/DI5Wis2lr4liBhxv8a6zycyIUy5OEofCGKK4slj2jFFitTD8LOwnA== X-Received: by 2002:a05:620a:2552:b0:6b4:8dbf:8992 with SMTP id s18-20020a05620a255200b006b48dbf8992mr18881433qko.109.1660082464471; Tue, 09 Aug 2022 15:01:04 -0700 (PDT) Received: from localhost.localdomain (bras-base-aurron9127w-grc-35-70-27-3-10.dsl.bell.ca. [70.27.3.10]) by smtp.gmail.com with ESMTPSA id b16-20020a05622a021000b0031eddc83560sm10563383qtx.90.2022.08.09.15.01.03 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 09 Aug 2022 15:01:04 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: peterx@redhat.com, Minchan Kim , David Hildenbrand , Nadav Amit , Andrew Morton , Hugh Dickins , Vlastimil Babka , Andrea Arcangeli , Andi Kleen , Huang Ying , "Kirill A . Shutemov" Subject: [PATCH v3 1/7] mm/x86: Use SWP_TYPE_BITS in 3-level swap macros Date: Tue, 9 Aug 2022 18:00:54 -0400 Message-Id: <20220809220100.20033-2-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220809220100.20033-1-peterx@redhat.com> References: <20220809220100.20033-1-peterx@redhat.com> 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" Replace all the magic "5" with the macro. Reviewed-by: David Hildenbrand Signed-off-by: Peter Xu Reviewed-by: "Huang, Ying" --- arch/x86/include/asm/pgtable-3level.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/pgtable-3level.h b/arch/x86/include/asm/p= gtable-3level.h index e896ebef8c24..28421a887209 100644 --- a/arch/x86/include/asm/pgtable-3level.h +++ b/arch/x86/include/asm/pgtable-3level.h @@ -256,10 +256,10 @@ static inline pud_t native_pudp_get_and_clear(pud_t *= pudp) /* We always extract/encode the offset by shifting it all the way up, and = then down again */ #define SWP_OFFSET_SHIFT (SWP_OFFSET_FIRST_BIT + SWP_TYPE_BITS) =20 -#define MAX_SWAPFILES_CHECK() BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > 5) -#define __swp_type(x) (((x).val) & 0x1f) -#define __swp_offset(x) ((x).val >> 5) -#define __swp_entry(type, offset) ((swp_entry_t){(type) | (offset) << 5}) +#define MAX_SWAPFILES_CHECK() BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > SWP_TYPE_= BITS) +#define __swp_type(x) (((x).val) & ((1UL << SWP_TYPE_BITS) - 1)) +#define __swp_offset(x) ((x).val >> SWP_TYPE_BITS) +#define __swp_entry(type, offset) ((swp_entry_t){(type) | (offset) << SWP_= TYPE_BITS}) =20 /* * Normally, __swp_entry() converts from arch-independent swp_entry_t to --=20 2.32.0 From nobody Sat Apr 11 15:28:08 2026 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 CDFC6C25B06 for ; Tue, 9 Aug 2022 22:02:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229832AbiHIWBr (ORCPT ); Tue, 9 Aug 2022 18:01:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229548AbiHIWBJ (ORCPT ); Tue, 9 Aug 2022 18:01:09 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6FE32E16 for ; Tue, 9 Aug 2022 15:01:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660082467; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KJCqPuuo957wqhSKd7LcKTP/opuSiey74Ok7HcmJ/F0=; b=NoLT7q6oCXqtVwFy7b8KU5j5J5Bf1jrwczrbWLOy0pO6ke3GjCeith3sxsgjjvtLzo0Aj0 NFlWsYyhkOxZ4zVchYaJk51A5V2TRRAd1YuWgjOqA7/gxAeQrDw84VWxzAaKH+UB2qZcly Gip6xEla+YxzhJIa5IVG0MvLOikhU44= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-394-g4aZRk8SONqVMZ5TJRz_og-1; Tue, 09 Aug 2022 18:01:06 -0400 X-MC-Unique: g4aZRk8SONqVMZ5TJRz_og-1 Received: by mail-qv1-f72.google.com with SMTP id p14-20020a0cfd8e000000b004747f93486bso6958236qvr.13 for ; Tue, 09 Aug 2022 15:01:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=KJCqPuuo957wqhSKd7LcKTP/opuSiey74Ok7HcmJ/F0=; b=nyr5zICaIqGIQn3uhLA85m0BIdKTIxZoLrYr9DEBDeiVokEkNgxl73D8lkOReLHbOS DbFX0s9aCjLoeKPNTY9+vEw0LpZdv6GO1KVT5E4k3fVvkGDhzk4sEJhJr9mBNt9L3xm2 e4Qg4aMac6o3/jYJimKv38wLojkEiWJvcWkwx/xBlNruMCfjVhOQIU6AcDHQ2E8peeCd Wsa1Cr2GkS738neCF9ckgi0UCtGVLrsCZVRJ7nuMIRwBlgLl/19qy7tTELU1aHVTcw1B 9RzuiDX4ltiyVJORe0Tr8WJbiH7hlIl9pJuQt3Ad8JJPvAPiQnKt2zP7wZ92qtGH36zS OfaA== X-Gm-Message-State: ACgBeo0/HZ3itruqon3VYu9EqDFofd85gwpxuHioNVgh17NdI5LYuRiQ 7k6Ki0KVZmfZKi+O3PMRidFs+OaBVDIZ86I+KXEuqnGNFaq7YV4h7MonQYKIGtuvfoDibVpFLDB oBrIhHvzE+ntCOqHEcYT8evyy X-Received: by 2002:a05:622a:100d:b0:31f:25e3:7a45 with SMTP id d13-20020a05622a100d00b0031f25e37a45mr21707219qte.365.1660082465949; Tue, 09 Aug 2022 15:01:05 -0700 (PDT) X-Google-Smtp-Source: AA6agR6NiOVfmdP51FutgyMM1ys4/L82L2FgMCUHx+9da7UIEtdC/XWDWNPnpXmCoiTlsJ6ASHYdnw== X-Received: by 2002:a05:622a:100d:b0:31f:25e3:7a45 with SMTP id d13-20020a05622a100d00b0031f25e37a45mr21707192qte.365.1660082465744; Tue, 09 Aug 2022 15:01:05 -0700 (PDT) Received: from localhost.localdomain (bras-base-aurron9127w-grc-35-70-27-3-10.dsl.bell.ca. [70.27.3.10]) by smtp.gmail.com with ESMTPSA id b16-20020a05622a021000b0031eddc83560sm10563383qtx.90.2022.08.09.15.01.04 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 09 Aug 2022 15:01:05 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: peterx@redhat.com, Minchan Kim , David Hildenbrand , Nadav Amit , Andrew Morton , Hugh Dickins , Vlastimil Babka , Andrea Arcangeli , Andi Kleen , Huang Ying , "Kirill A . Shutemov" Subject: [PATCH v3 2/7] mm/swap: Comment all the ifdef in swapops.h Date: Tue, 9 Aug 2022 18:00:55 -0400 Message-Id: <20220809220100.20033-3-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220809220100.20033-1-peterx@redhat.com> References: <20220809220100.20033-1-peterx@redhat.com> 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" swapops.h contains quite a few layers of ifdef, some of the "else" and "endif" doesn't get proper comment on the macro so it's hard to follow on what are they referring to. Add the comments. Suggested-by: Nadav Amit Signed-off-by: Peter Xu Reviewed-by: "Huang, Ying" --- include/linux/swapops.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/include/linux/swapops.h b/include/linux/swapops.h index a3d435bf9f97..3a2901ff4f1e 100644 --- a/include/linux/swapops.h +++ b/include/linux/swapops.h @@ -247,8 +247,8 @@ extern void migration_entry_wait(struct mm_struct *mm, = pmd_t *pmd, #ifdef CONFIG_HUGETLB_PAGE extern void __migration_entry_wait_huge(pte_t *ptep, spinlock_t *ptl); extern void migration_entry_wait_huge(struct vm_area_struct *vma, pte_t *p= te); -#endif -#else +#endif /* CONFIG_HUGETLB_PAGE */ +#else /* CONFIG_MIGRATION */ static inline swp_entry_t make_readable_migration_entry(pgoff_t offset) { return swp_entry(0, 0); @@ -276,7 +276,7 @@ static inline void migration_entry_wait(struct mm_struc= t *mm, pmd_t *pmd, #ifdef CONFIG_HUGETLB_PAGE static inline void __migration_entry_wait_huge(pte_t *ptep, spinlock_t *pt= l) { } static inline void migration_entry_wait_huge(struct vm_area_struct *vma, p= te_t *pte) { } -#endif +#endif /* CONFIG_HUGETLB_PAGE */ static inline int is_writable_migration_entry(swp_entry_t entry) { return 0; @@ -286,7 +286,7 @@ static inline int is_readable_migration_entry(swp_entry= _t entry) return 0; } =20 -#endif +#endif /* CONFIG_MIGRATION */ =20 typedef unsigned long pte_marker; =20 @@ -426,7 +426,7 @@ static inline int is_pmd_migration_entry(pmd_t pmd) { return is_swap_pmd(pmd) && is_migration_entry(pmd_to_swp_entry(pmd)); } -#else +#else /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ static inline int set_pmd_migration_entry(struct page_vma_mapped_walk *pvm= w, struct page *page) { @@ -455,7 +455,7 @@ static inline int is_pmd_migration_entry(pmd_t pmd) { return 0; } -#endif +#endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ =20 #ifdef CONFIG_MEMORY_FAILURE =20 @@ -495,7 +495,7 @@ static inline void num_poisoned_pages_sub(long i) atomic_long_sub(i, &num_poisoned_pages); } =20 -#else +#else /* CONFIG_MEMORY_FAILURE */ =20 static inline swp_entry_t make_hwpoison_entry(struct page *page) { @@ -514,7 +514,7 @@ static inline void num_poisoned_pages_inc(void) static inline void num_poisoned_pages_sub(long i) { } -#endif +#endif /* CONFIG_MEMORY_FAILURE */ =20 static inline int non_swap_entry(swp_entry_t entry) { --=20 2.32.0 From nobody Sat Apr 11 15:28:08 2026 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 E5CE6C3F6B0 for ; Tue, 9 Aug 2022 22:02:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229899AbiHIWBw (ORCPT ); Tue, 9 Aug 2022 18:01:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229621AbiHIWBL (ORCPT ); Tue, 9 Aug 2022 18:01:11 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6CEC0E18 for ; Tue, 9 Aug 2022 15:01:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660082469; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xPnoJX1BpuaXYPt2aIEkvm8MVF1Jb7/8BoIuTWGPV+k=; b=ijjhNIm4zfMVU/kyrYGzvt1ADLCcXvEcG/TB3JNDrI/4C/URV6rUTZhFOLTE+sQA1/YSpA ujs80jBmT7d26TO778tABT3vLZK+aMfyIkgBUQyyJ1iJAE49kUeZ9o9EzQcuo/ddN6wh/x cEfZDy/0TGnc8CvyoOLF63qzzQhOUvY= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-45-PEwqWQx0PQGTSNKsyJY-9w-1; Tue, 09 Aug 2022 18:01:08 -0400 X-MC-Unique: PEwqWQx0PQGTSNKsyJY-9w-1 Received: by mail-qt1-f200.google.com with SMTP id hj2-20020a05622a620200b0034286e2a191so9577292qtb.2 for ; Tue, 09 Aug 2022 15:01:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=xPnoJX1BpuaXYPt2aIEkvm8MVF1Jb7/8BoIuTWGPV+k=; b=lVj7PMBnx39ERlFex1U4cq0oCivYEz2tHtXU94404CPHvpFIaWxr235zigpWR1M6qa +jq1PJYbzSbFRROVNgGeJYsTu0mh+6rchEIhhCKfxTZepayaalTj1qPpoBdI8zRIoCzQ wN7Q4d6R8GJ9/5LE+nN8F38RD5b4G8NoPhp4lTHeRhyKEF3np/ksFsZbjiBj3HIXKgBc 4d3FwzhO6iBWOHHolzaYUyvpMs1rpiJFDezpwR6oR0OZ1MUMdwzllVG3jvWfpfXbcgc+ 3j1rpV0wZltxC61vYTo7goTVsftggWBEzZrz3J8YcHGzRWX1yovqkgS4AhdZcVnMBB2k 0iYA== X-Gm-Message-State: ACgBeo1i+qrSfWk4Ar/bslzV78154Nq/59icsyXBPr4ATkh3ZDsw8w/0 T29o17gI23zBxksSUJH4JI3qncSE2KzESh9xcLmQCb/VvCpUfYgNJr7MMA34UM3gggMjDAFiC5N 9ueX0F9PR5r229mqqL8y6vyXD X-Received: by 2002:ac8:5ad0:0:b0:342:fc73:2aa4 with SMTP id d16-20020ac85ad0000000b00342fc732aa4mr8242498qtd.207.1660082467365; Tue, 09 Aug 2022 15:01:07 -0700 (PDT) X-Google-Smtp-Source: AA6agR7OkYIB1b3FqUM56/3uXh2sqRYh6V2WKfpCSssEbSfHcPbhC6taRal2aT6/tKNRyYns5FB3Og== X-Received: by 2002:ac8:5ad0:0:b0:342:fc73:2aa4 with SMTP id d16-20020ac85ad0000000b00342fc732aa4mr8242456qtd.207.1660082467089; Tue, 09 Aug 2022 15:01:07 -0700 (PDT) Received: from localhost.localdomain (bras-base-aurron9127w-grc-35-70-27-3-10.dsl.bell.ca. [70.27.3.10]) by smtp.gmail.com with ESMTPSA id b16-20020a05622a021000b0031eddc83560sm10563383qtx.90.2022.08.09.15.01.05 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 09 Aug 2022 15:01:06 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: peterx@redhat.com, Minchan Kim , David Hildenbrand , Nadav Amit , Andrew Morton , Hugh Dickins , Vlastimil Babka , Andrea Arcangeli , Andi Kleen , Huang Ying , "Kirill A . Shutemov" Subject: [PATCH v3 3/7] mm/swap: Add swp_offset_pfn() to fetch PFN from swap entry Date: Tue, 9 Aug 2022 18:00:56 -0400 Message-Id: <20220809220100.20033-4-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220809220100.20033-1-peterx@redhat.com> References: <20220809220100.20033-1-peterx@redhat.com> 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" We've got a bunch of special swap entries that stores PFN inside the swap offset fields. To fetch the PFN, normally the user just calls swp_offset() assuming that'll be the PFN. Add a helper swp_offset_pfn() to fetch the PFN instead, fetching only the max possible length of a PFN on the host, meanwhile doing proper check with MAX_PHYSMEM_BITS to make sure the swap offsets can actually store the PFNs properly always using the BUILD_BUG_ON() in is_pfn_swap_entry(). One reason to do so is we never tried to sanitize whether swap offset can really fit for storing PFN. At the meantime, this patch also prepares us with the future possibility to store more information inside the swp offset field, so assuming "swp_offset(entry)" to be the PFN will not stand any more very soon. Replace many of the swp_offset() callers to use swp_offset_pfn() where proper. Note that many of the existing users are not candidates for the replacement, e.g.: (1) When the swap entry is not a pfn swap entry at all, or, (2) when we wanna keep the whole swp_offset but only change the swp type. For the latter, it can happen when fork() triggered on a write-migration swap entry pte, we may want to only change the migration type from write->read but keep the rest, so it's not "fetching PFN" but "changing swap type only". They're left aside so that when there're more information within the swp offset they'll be carried over naturally in those cases. Since at it, dropping hwpoison_entry_to_pfn() because that's exactly what the new swp_offset_pfn() is about. Signed-off-by: Peter Xu --- arch/arm64/mm/hugetlbpage.c | 2 +- include/linux/swapops.h | 35 +++++++++++++++++++++++++++++------ mm/hmm.c | 2 +- mm/memory-failure.c | 2 +- mm/page_vma_mapped.c | 6 +++--- 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 0795028f017c..35e9a468d13e 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -245,7 +245,7 @@ static inline struct folio *hugetlb_swap_entry_to_folio= (swp_entry_t entry) { VM_BUG_ON(!is_migration_entry(entry) && !is_hwpoison_entry(entry)); =20 - return page_folio(pfn_to_page(swp_offset(entry))); + return page_folio(pfn_to_page(swp_offset_pfn(entry))); } =20 void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, diff --git a/include/linux/swapops.h b/include/linux/swapops.h index 3a2901ff4f1e..e1accbcd1136 100644 --- a/include/linux/swapops.h +++ b/include/linux/swapops.h @@ -23,6 +23,20 @@ #define SWP_TYPE_SHIFT (BITS_PER_XA_VALUE - MAX_SWAPFILES_SHIFT) #define SWP_OFFSET_MASK ((1UL << SWP_TYPE_SHIFT) - 1) =20 +/* + * Definitions only for PFN swap entries (see is_pfn_swap_entry()). To + * store PFN, we only need SWP_PFN_BITS bits. Each of the pfn swap entries + * can use the extra bits to store other information besides PFN. + */ +#ifdef MAX_PHYSMEM_BITS +#define SWP_PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT) +#else /* MAX_PHYSMEM_BITS */ +#define SWP_PFN_BITS (BITS_PER_LONG - PAGE_SHIFT) +#endif /* MAX_PHYSMEM_BITS */ +#define SWP_PFN_MASK ((1UL << SWP_PFN_BITS) - 1) + +static inline bool is_pfn_swap_entry(swp_entry_t entry); + /* Clear all flags but only keep swp_entry_t related information */ static inline pte_t pte_swp_clear_flags(pte_t pte) { @@ -64,6 +78,17 @@ static inline pgoff_t swp_offset(swp_entry_t entry) return entry.val & SWP_OFFSET_MASK; } =20 +/* + * This should only be called upon a pfn swap entry to get the PFN stored + * in the swap entry. Please refers to is_pfn_swap_entry() for definition + * of pfn swap entry. + */ +static inline unsigned long swp_offset_pfn(swp_entry_t entry) +{ + VM_BUG_ON(!is_pfn_swap_entry(entry)); + return swp_offset(entry) & SWP_PFN_MASK; +} + /* check whether a pte points to a swap entry */ static inline int is_swap_pte(pte_t pte) { @@ -369,7 +394,7 @@ static inline int pte_none_mostly(pte_t pte) =20 static inline struct page *pfn_swap_entry_to_page(swp_entry_t entry) { - struct page *p =3D pfn_to_page(swp_offset(entry)); + struct page *p =3D pfn_to_page(swp_offset_pfn(entry)); =20 /* * Any use of migration entries may only occur while the @@ -387,6 +412,9 @@ static inline struct page *pfn_swap_entry_to_page(swp_e= ntry_t entry) */ static inline bool is_pfn_swap_entry(swp_entry_t entry) { + /* Make sure the swp offset can always store the needed fields */ + BUILD_BUG_ON(SWP_TYPE_SHIFT < SWP_PFN_BITS); + return is_migration_entry(entry) || is_device_private_entry(entry) || is_device_exclusive_entry(entry); } @@ -475,11 +503,6 @@ static inline int is_hwpoison_entry(swp_entry_t entry) return swp_type(entry) =3D=3D SWP_HWPOISON; } =20 -static inline unsigned long hwpoison_entry_to_pfn(swp_entry_t entry) -{ - return swp_offset(entry); -} - static inline void num_poisoned_pages_inc(void) { atomic_long_inc(&num_poisoned_pages); diff --git a/mm/hmm.c b/mm/hmm.c index f2aa63b94d9b..3850fb625dda 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -253,7 +253,7 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, uns= igned long addr, cpu_flags =3D HMM_PFN_VALID; if (is_writable_device_private_entry(entry)) cpu_flags |=3D HMM_PFN_WRITE; - *hmm_pfn =3D swp_offset(entry) | cpu_flags; + *hmm_pfn =3D swp_offset_pfn(entry) | cpu_flags; return 0; } =20 diff --git a/mm/memory-failure.c b/mm/memory-failure.c index 14439806b5ef..c8a7d38c1da4 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -632,7 +632,7 @@ static int check_hwpoisoned_entry(pte_t pte, unsigned l= ong addr, short shift, swp_entry_t swp =3D pte_to_swp_entry(pte); =20 if (is_hwpoison_entry(swp)) - pfn =3D hwpoison_entry_to_pfn(swp); + pfn =3D swp_offset_pfn(swp); } =20 if (!pfn || pfn !=3D poisoned_pfn) diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c index 8e9e574d535a..93e13fc17d3c 100644 --- a/mm/page_vma_mapped.c +++ b/mm/page_vma_mapped.c @@ -86,7 +86,7 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw) !is_device_exclusive_entry(entry)) return false; =20 - pfn =3D swp_offset(entry); + pfn =3D swp_offset_pfn(entry); } else if (is_swap_pte(*pvmw->pte)) { swp_entry_t entry; =20 @@ -96,7 +96,7 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw) !is_device_exclusive_entry(entry)) return false; =20 - pfn =3D swp_offset(entry); + pfn =3D swp_offset_pfn(entry); } else { if (!pte_present(*pvmw->pte)) return false; @@ -221,7 +221,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *= pvmw) return not_found(pvmw); entry =3D pmd_to_swp_entry(pmde); if (!is_migration_entry(entry) || - !check_pmd(swp_offset(entry), pvmw)) + !check_pmd(swp_offset_pfn(entry), pvmw)) return not_found(pvmw); return true; } --=20 2.32.0 From nobody Sat Apr 11 15:28:08 2026 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 030FCC25B0D for ; Tue, 9 Aug 2022 22:02:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229908AbiHIWBy (ORCPT ); Tue, 9 Aug 2022 18:01:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229637AbiHIWBM (ORCPT ); Tue, 9 Aug 2022 18:01:12 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 19C28E08 for ; Tue, 9 Aug 2022 15:01:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660082470; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KrXV1mhD8NFSPZwAf9c4FL2hL9qvLlZq2jcNtWuZzdA=; b=YWQlnY9GGa44oSDEKV+26Vjp8Ia44VGC1qVFzT6lr+WhlET05t0YRfya4RNVcyjEkSOXju bI3FY4OiuoGNUquJ13Yz/SDv2WDzL2hX0fQw/eYNcuPGnSdJMYd/XaYbYgc2M93Gc2l507 LZ9jrkmiOXE2diKDFvwZSuSDGD8FUAE= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-189-37XCWSq8ORS9edrlrLQvMA-1; Tue, 09 Aug 2022 18:01:09 -0400 X-MC-Unique: 37XCWSq8ORS9edrlrLQvMA-1 Received: by mail-qv1-f72.google.com with SMTP id oh2-20020a056214438200b0047bd798af75so1659534qvb.6 for ; Tue, 09 Aug 2022 15:01:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=KrXV1mhD8NFSPZwAf9c4FL2hL9qvLlZq2jcNtWuZzdA=; b=IzIz+Q0XRDpRPAoVRUqwLSTzcOdi1d1N7iIHg+zesc1McScH+ZRhvZoDRx2Akz1r8L MPwmvXJpsMWb/9pPMgJied5RcMfe2nvXKldUdc5261Ar017x8Y1dK5osyRD8Iy0+/iOO d9q98TIAsirukNdWtUkfNSdQxX9mlWzktuPBo/273XL32M4sUMKWa1T+XPqL2XQCOYOR MxgCZWWNTPKzC6UZIW1QpEIO2rs7DLNJFvgoMwKF+UT9VFCTW8nRtGGsS45wgeNR7SxD f/oOzX9Y3ltukhem32gohx3FzI+7p9BkJ6IjArctzokrWpkQViWgDJ+fGK/MmrElIIJd SI9g== X-Gm-Message-State: ACgBeo39L7d+zfvwKycYT5GCE0G/7m6IzgWAj3BvZwgvr+3Hq6zYcqwu yAhJzXo3lzFWJw1aTmyLWFIIy4ZtM93izrevGAeGa4IKAnGpEd+JcMEDFtRJ96gs66wSxiFnDFu xJ3KJ0FujuHhHrJ/qPpp8NN5L X-Received: by 2002:a05:6214:f24:b0:476:72bb:2473 with SMTP id iw4-20020a0562140f2400b0047672bb2473mr21833890qvb.82.1660082468620; Tue, 09 Aug 2022 15:01:08 -0700 (PDT) X-Google-Smtp-Source: AA6agR53TiwBc9GpPqRdwAZDQe2UaVr8TnIHRxStNg8j6qBByy+9hjyDW7m38tabiWtpuwTjDL75Bw== X-Received: by 2002:a05:6214:f24:b0:476:72bb:2473 with SMTP id iw4-20020a0562140f2400b0047672bb2473mr21833862qvb.82.1660082468439; Tue, 09 Aug 2022 15:01:08 -0700 (PDT) Received: from localhost.localdomain (bras-base-aurron9127w-grc-35-70-27-3-10.dsl.bell.ca. [70.27.3.10]) by smtp.gmail.com with ESMTPSA id b16-20020a05622a021000b0031eddc83560sm10563383qtx.90.2022.08.09.15.01.07 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 09 Aug 2022 15:01:08 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: peterx@redhat.com, Minchan Kim , David Hildenbrand , Nadav Amit , Andrew Morton , Hugh Dickins , Vlastimil Babka , Andrea Arcangeli , Andi Kleen , Huang Ying , "Kirill A . Shutemov" Subject: [PATCH v3 4/7] mm/thp: Carry over dirty bit when thp splits on pmd Date: Tue, 9 Aug 2022 18:00:57 -0400 Message-Id: <20220809220100.20033-5-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220809220100.20033-1-peterx@redhat.com> References: <20220809220100.20033-1-peterx@redhat.com> 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" Carry over the dirty bit from pmd to pte when a huge pmd splits. It shouldn't be a correctness issue since when pmd_dirty() we'll have the page marked dirty anyway, however having dirty bit carried over helps the next initial writes of split ptes on some archs like x86. Signed-off-by: Peter Xu Reviewed-by: "Huang, Ying" --- mm/huge_memory.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 0611b2fd145a..e8e78d1bac5f 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2005,7 +2005,7 @@ static void __split_huge_pmd_locked(struct vm_area_st= ruct *vma, pmd_t *pmd, pgtable_t pgtable; pmd_t old_pmd, _pmd; bool young, write, soft_dirty, pmd_migration =3D false, uffd_wp =3D false; - bool anon_exclusive =3D false; + bool anon_exclusive =3D false, dirty =3D false; unsigned long addr; int i; =20 @@ -2098,6 +2098,7 @@ static void __split_huge_pmd_locked(struct vm_area_st= ruct *vma, pmd_t *pmd, SetPageDirty(page); write =3D pmd_write(old_pmd); young =3D pmd_young(old_pmd); + dirty =3D pmd_dirty(old_pmd); soft_dirty =3D pmd_soft_dirty(old_pmd); uffd_wp =3D pmd_uffd_wp(old_pmd); =20 @@ -2161,6 +2162,9 @@ static void __split_huge_pmd_locked(struct vm_area_st= ruct *vma, pmd_t *pmd, entry =3D pte_wrprotect(entry); if (!young) entry =3D pte_mkold(entry); + /* NOTE: this may set soft-dirty too on some archs */ + if (dirty) + entry =3D pte_mkdirty(entry); if (soft_dirty) entry =3D pte_mksoft_dirty(entry); if (uffd_wp) --=20 2.32.0 From nobody Sat Apr 11 15:28:08 2026 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 63C6FC25B0F for ; Tue, 9 Aug 2022 22:02:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229914AbiHIWB6 (ORCPT ); Tue, 9 Aug 2022 18:01:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229841AbiHIWBO (ORCPT ); Tue, 9 Aug 2022 18:01:14 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8A538E16 for ; Tue, 9 Aug 2022 15:01:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660082471; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1RwmKRYJuvTG/btGRX62JpRnXDc5C3jxVCpX+grs9Ro=; b=KIPM2jJQZaYSTPaCLsWHotXRAjDekjJACRB/oocteg3EL9KBUgHICq1VZOOji2MeVsumSC EA+LjnuEYbvhLlXO8TZkcOxrkUC2FHZE6q6Ah5dF3atTJ8rYGVKCD6QmVv7NPS+mb4alot T7QCEI7B4I8jbMqPRj0//fjUwRngrJ4= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-479-jexWONQNOLK_NBMpslu0PA-1; Tue, 09 Aug 2022 18:01:10 -0400 X-MC-Unique: jexWONQNOLK_NBMpslu0PA-1 Received: by mail-qv1-f70.google.com with SMTP id f10-20020a05621400ca00b0047752ce4c5cso6885066qvs.5 for ; Tue, 09 Aug 2022 15:01:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=1RwmKRYJuvTG/btGRX62JpRnXDc5C3jxVCpX+grs9Ro=; b=RCuah3f+SRlUAYQFkjNtKO8ljoRC2BoPDHcrUVxt+qSMuYpcjZ7DVPFAJyP3x5RheU UWNPRxci3i1fT84foXjd6py+debpBGmvI0MUNoAIOGxZM97tQLEub93Ll2APqX9R8Fpp LK/gKqY1J3hHcf130u+fCjc9BvRtmz5ktua38Z3NUNTXZWGHoYnWKqPOgEq7o/ou/1OC 8BVxq19By8LTs4+SgOocOEpWT+kizSmmdPxmiAgnfU4e7ggXw77LeZum5RsCxltnifuR BxbjzQx8neKFrtJKMQxhj0SSbqsL5ahJNdn5UB1xa83LOtBv9QBlRz23LuUyWDPeU/CD OVbQ== X-Gm-Message-State: ACgBeo1XACkARgvy4CBW9fYERLQlf6MnAs1y8QSZywL/ebsOX8dqPe9p Oj7VzZ+4ZjxfExNDRRKH+5s6rIMXEjjhF2RyXjd69e6bsBHcpKaHjUEuhqxtZ4szplnzeyOAd9J wlRsRC2XZseZlpQF36bHV1qqJ X-Received: by 2002:ac8:5a51:0:b0:342:e917:f25b with SMTP id o17-20020ac85a51000000b00342e917f25bmr16799574qta.284.1660082470077; Tue, 09 Aug 2022 15:01:10 -0700 (PDT) X-Google-Smtp-Source: AA6agR7F8awg0xQpCbkyT+nfZ6Y4i6eUOSmTv7BxMbnhF6Tnq2rHRG8SF8KC3nPcS8vkXeC9hjxxmA== X-Received: by 2002:ac8:5a51:0:b0:342:e917:f25b with SMTP id o17-20020ac85a51000000b00342e917f25bmr16799543qta.284.1660082469795; Tue, 09 Aug 2022 15:01:09 -0700 (PDT) Received: from localhost.localdomain (bras-base-aurron9127w-grc-35-70-27-3-10.dsl.bell.ca. [70.27.3.10]) by smtp.gmail.com with ESMTPSA id b16-20020a05622a021000b0031eddc83560sm10563383qtx.90.2022.08.09.15.01.08 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 09 Aug 2022 15:01:09 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: peterx@redhat.com, Minchan Kim , David Hildenbrand , Nadav Amit , Andrew Morton , Hugh Dickins , Vlastimil Babka , Andrea Arcangeli , Andi Kleen , Huang Ying , "Kirill A . Shutemov" Subject: [PATCH v3 5/7] mm: Remember young/dirty bit for page migrations Date: Tue, 9 Aug 2022 18:00:58 -0400 Message-Id: <20220809220100.20033-6-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220809220100.20033-1-peterx@redhat.com> References: <20220809220100.20033-1-peterx@redhat.com> 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" When page migration happens, we always ignore the young/dirty bit settings in the old pgtable, and marking the page as old in the new page table using either pte_mkold() or pmd_mkold(), and keeping the pte clean. That's fine from functional-wise, but that's not friendly to page reclaim because the moving page can be actively accessed within the procedure. Not to mention hardware setting the young bit can bring quite some overhead on some systems, e.g. x86_64 needs a few hundreds nanoseconds to set the bit. The same slowdown problem to dirty bits when the memory is first written after page migration happened. Actually we can easily remember the A/D bit configuration and recover the information after the page is migrated. To achieve it, define a new set of bits in the migration swap offset field to cache the A/D bits for old pte. Then when removing/recovering the migration entry, we can recover the A/D bits even if the page changed. One thing to mention is that here we used max_swapfile_size() to detect how many swp offset bits we have, and we'll only enable this feature if we know the swp offset can be big enough to store both the PFN value and the young bit. Otherwise the A/D bits are dropped like before. Signed-off-by: Peter Xu --- include/linux/swapops.h | 99 +++++++++++++++++++++++++++++++++++++++++ mm/huge_memory.c | 18 +++++++- mm/migrate.c | 6 ++- mm/migrate_device.c | 4 ++ mm/rmap.c | 5 ++- 5 files changed, 128 insertions(+), 4 deletions(-) diff --git a/include/linux/swapops.h b/include/linux/swapops.h index e1accbcd1136..0e9579b90659 100644 --- a/include/linux/swapops.h +++ b/include/linux/swapops.h @@ -8,6 +8,10 @@ =20 #ifdef CONFIG_MMU =20 +#ifdef CONFIG_SWAP +#include +#endif /* CONFIG_SWAP */ + /* * swapcache pages are stored in the swapper_space radix tree. We want to * get good packing density in that tree, so the index should be dense in @@ -35,6 +39,31 @@ #endif /* MAX_PHYSMEM_BITS */ #define SWP_PFN_MASK ((1UL << SWP_PFN_BITS) - 1) =20 +/** + * Migration swap entry specific bitfield definitions. Layout: + * + * |----------+--------------------| + * | swp_type | swp_offset | + * |----------+--------+-+-+-------| + * | | resv |D|A| PFN | + * |----------+--------+-+-+-------| + * + * @SWP_MIG_YOUNG_BIT: Whether the page used to have young bit set (bit A) + * @SWP_MIG_DIRTY_BIT: Whether the page used to have dirty bit set (bit D) + * + * Note: A/D bits will be stored in migration entries iff there're enough + * free bits in arch specific swp offset. By default we'll ignore A/D bits + * when migrating a page. Please refer to migration_entry_supports_ad() + * for more information. If there're more bits besides PFN and A/D bits, + * they should be reserved and always be zeros. + */ +#define SWP_MIG_YOUNG_BIT (SWP_PFN_BITS) +#define SWP_MIG_DIRTY_BIT (SWP_PFN_BITS + 1) +#define SWP_MIG_TOTAL_BITS (SWP_PFN_BITS + 2) + +#define SWP_MIG_YOUNG BIT(SWP_MIG_YOUNG_BIT) +#define SWP_MIG_DIRTY BIT(SWP_MIG_DIRTY_BIT) + static inline bool is_pfn_swap_entry(swp_entry_t entry); =20 /* Clear all flags but only keep swp_entry_t related information */ @@ -265,6 +294,57 @@ static inline swp_entry_t make_writable_migration_entr= y(pgoff_t offset) return swp_entry(SWP_MIGRATION_WRITE, offset); } =20 +/* + * Returns whether the host has large enough swap offset field to support + * carrying over pgtable A/D bits for page migrations. The result is + * pretty much arch specific. + */ +static inline bool migration_entry_supports_ad(void) +{ + /* + * max_swapfile_size() returns the max supported swp-offset plus 1. + * We can support the migration A/D bits iff the pfn swap entry has + * the offset large enough to cover all of them (PFN, A & D bits). + */ +#ifdef CONFIG_SWAP + return max_swapfile_size() >=3D (1UL << SWP_MIG_TOTAL_BITS); +#else /* CONFIG_SWAP */ + return false; +#endif /* CONFIG_SWAP */ +} + +static inline swp_entry_t make_migration_entry_young(swp_entry_t entry) +{ + if (migration_entry_supports_ad()) + return swp_entry(swp_type(entry), + swp_offset(entry) | SWP_MIG_YOUNG); + return entry; +} + +static inline bool is_migration_entry_young(swp_entry_t entry) +{ + if (migration_entry_supports_ad()) + return swp_offset(entry) & SWP_MIG_YOUNG; + /* Keep the old behavior of aging page after migration */ + return false; +} + +static inline swp_entry_t make_migration_entry_dirty(swp_entry_t entry) +{ + if (migration_entry_supports_ad()) + return swp_entry(swp_type(entry), + swp_offset(entry) | SWP_MIG_DIRTY); + return entry; +} + +static inline bool is_migration_entry_dirty(swp_entry_t entry) +{ + if (migration_entry_supports_ad()) + return swp_offset(entry) & SWP_MIG_DIRTY; + /* Keep the old behavior of clean page after migration */ + return false; +} + extern void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep, spinlock_t *ptl); extern void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd, @@ -311,6 +391,25 @@ static inline int is_readable_migration_entry(swp_entr= y_t entry) return 0; } =20 +static inline swp_entry_t make_migration_entry_young(swp_entry_t entry) +{ + return entry; +} + +static inline bool is_migration_entry_young(swp_entry_t entry) +{ + return false; +} + +static inline swp_entry_t make_migration_entry_dirty(swp_entry_t entry) +{ + return entry; +} + +static inline bool is_migration_entry_dirty(swp_entry_t entry) +{ + return false; +} #endif /* CONFIG_MIGRATION */ =20 typedef unsigned long pte_marker; diff --git a/mm/huge_memory.c b/mm/huge_memory.c index e8e78d1bac5f..1644e9f59d73 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2089,7 +2089,8 @@ static void __split_huge_pmd_locked(struct vm_area_st= ruct *vma, pmd_t *pmd, write =3D is_writable_migration_entry(entry); if (PageAnon(page)) anon_exclusive =3D is_readable_exclusive_migration_entry(entry); - young =3D false; + young =3D is_migration_entry_young(entry); + dirty =3D is_migration_entry_dirty(entry); soft_dirty =3D pmd_swp_soft_dirty(old_pmd); uffd_wp =3D pmd_swp_uffd_wp(old_pmd); } else { @@ -2148,6 +2149,10 @@ static void __split_huge_pmd_locked(struct vm_area_s= truct *vma, pmd_t *pmd, else swp_entry =3D make_readable_migration_entry( page_to_pfn(page + i)); + if (young) + swp_entry =3D make_migration_entry_young(swp_entry); + if (dirty) + swp_entry =3D make_migration_entry_dirty(swp_entry); entry =3D swp_entry_to_pte(swp_entry); if (soft_dirty) entry =3D pte_swp_mksoft_dirty(entry); @@ -3157,6 +3162,10 @@ int set_pmd_migration_entry(struct page_vma_mapped_w= alk *pvmw, entry =3D make_readable_exclusive_migration_entry(page_to_pfn(page)); else entry =3D make_readable_migration_entry(page_to_pfn(page)); + if (pmd_young(pmdval)) + entry =3D make_migration_entry_young(entry); + if (pmd_dirty(pmdval)) + entry =3D make_migration_entry_dirty(entry); pmdswp =3D swp_entry_to_pmd(entry); if (pmd_soft_dirty(pmdval)) pmdswp =3D pmd_swp_mksoft_dirty(pmdswp); @@ -3182,13 +3191,18 @@ void remove_migration_pmd(struct page_vma_mapped_wa= lk *pvmw, struct page *new) =20 entry =3D pmd_to_swp_entry(*pvmw->pmd); get_page(new); - pmde =3D pmd_mkold(mk_huge_pmd(new, READ_ONCE(vma->vm_page_prot))); + pmde =3D mk_huge_pmd(new, READ_ONCE(vma->vm_page_prot)); if (pmd_swp_soft_dirty(*pvmw->pmd)) pmde =3D pmd_mksoft_dirty(pmde); if (is_writable_migration_entry(entry)) pmde =3D maybe_pmd_mkwrite(pmde, vma); if (pmd_swp_uffd_wp(*pvmw->pmd)) pmde =3D pmd_wrprotect(pmd_mkuffd_wp(pmde)); + if (!is_migration_entry_young(entry)) + pmde =3D pmd_mkold(pmde); + /* NOTE: this may contain setting soft-dirty on some archs */ + if (PageDirty(new) && is_migration_entry_dirty(entry)) + pmde =3D pmd_mkdirty(pmde); =20 if (PageAnon(new)) { rmap_t rmap_flags =3D RMAP_COMPOUND; diff --git a/mm/migrate.c b/mm/migrate.c index 6a1597c92261..0433a71d2bee 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -198,7 +198,7 @@ static bool remove_migration_pte(struct folio *folio, #endif =20 folio_get(folio); - pte =3D pte_mkold(mk_pte(new, READ_ONCE(vma->vm_page_prot))); + pte =3D mk_pte(new, READ_ONCE(vma->vm_page_prot)); if (pte_swp_soft_dirty(*pvmw.pte)) pte =3D pte_mksoft_dirty(pte); =20 @@ -206,6 +206,10 @@ static bool remove_migration_pte(struct folio *folio, * Recheck VMA as permissions can change since migration started */ entry =3D pte_to_swp_entry(*pvmw.pte); + if (!is_migration_entry_young(entry)) + pte =3D pte_mkold(pte); + if (folio_test_dirty(folio) && is_migration_entry_dirty(entry)) + pte =3D pte_mkdirty(pte); if (is_writable_migration_entry(entry)) pte =3D maybe_mkwrite(pte, vma); else if (pte_swp_uffd_wp(*pvmw.pte)) diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 27fb37d65476..699f821b8443 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -221,6 +221,10 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, else entry =3D make_readable_migration_entry( page_to_pfn(page)); + if (pte_young(pte)) + entry =3D make_migration_entry_young(entry); + if (pte_dirty(pte)) + entry =3D make_migration_entry_dirty(entry); swp_pte =3D swp_entry_to_pte(entry); if (pte_present(pte)) { if (pte_soft_dirty(pte)) diff --git a/mm/rmap.c b/mm/rmap.c index af775855e58f..28aef434ea41 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2065,7 +2065,10 @@ static bool try_to_migrate_one(struct folio *folio, = struct vm_area_struct *vma, else entry =3D make_readable_migration_entry( page_to_pfn(subpage)); - + if (pte_young(pteval)) + entry =3D make_migration_entry_young(entry); + if (pte_dirty(pteval)) + entry =3D make_migration_entry_dirty(entry); swp_pte =3D swp_entry_to_pte(entry); if (pte_soft_dirty(pteval)) swp_pte =3D pte_swp_mksoft_dirty(swp_pte); --=20 2.32.0 From nobody Sat Apr 11 15:28:08 2026 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 7AC75C25B0E for ; Tue, 9 Aug 2022 22:02:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229923AbiHIWCC (ORCPT ); Tue, 9 Aug 2022 18:02:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229848AbiHIWBR (ORCPT ); Tue, 9 Aug 2022 18:01:17 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E3653E2D for ; Tue, 9 Aug 2022 15:01:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660082473; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QP8/u5AChrIXiwIzqFnG/IHURDZ5n0i12x48YvKEviU=; b=HOUX84jyi/ddHHVWOUINEqsZ4eFjZrwsUSYVk+mYSAAeR6WhsZeEzbfOe4YtnvvQZ3JaVU dKDCvHwYk5cFJp5PBBonwq0gsyrwdajL5eJEDj8kN7Ccn+s5twSxQGQqwbO2xekGulFAKf 9Enq4uALLUOIm5kAA0iKiIfNV2CDt2w= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-215-bxTjDqLBN0mCheNzFtgSdg-1; Tue, 09 Aug 2022 18:01:11 -0400 X-MC-Unique: bxTjDqLBN0mCheNzFtgSdg-1 Received: by mail-qk1-f198.google.com with SMTP id bk21-20020a05620a1a1500b006b5c24695a4so11236634qkb.15 for ; Tue, 09 Aug 2022 15:01:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=QP8/u5AChrIXiwIzqFnG/IHURDZ5n0i12x48YvKEviU=; b=ejk+r1B/hdRG8kNtZm2uYB/omquDrbhUwqHNFnkp3JI6EceyUToML57aw1m63HFNO4 TiWNgFRujieiOErieoVqmRIVw2sLd5Q2q80rzjwjR0CEQXJMROp7UogFS9p/O124utpO 3ut2aSUIK5AXQtJ/KmxH2S/svZj+pSvXuirvYwgbsoiCIpcJdV8txcB4Php9jd4KogsT 7rt6lxogtTzCmjKFKEiGEKfx4I3aOXQl8gvGmtFp5dNx+uW5rGOWlBujUY8NP1b13AOw cl3yqPYuA47MaiK6iSDWnb9TSSTg9OrQkp+4zX1MNqyOOR54DBngySHtX0I61JO5JlhE HNCA== X-Gm-Message-State: ACgBeo30GxyshY+sJw8ErRE3nYTvQMZzETy4yGCor4RE7nH8QdF7KB8/ RQUeGjpC0oNaApqK4P4sAHIcbTCcRsjaRtNjd9gs0YHZVNo3rWF16xeuZ4Ez7LXE1jJU65r3ykI bHoSnn3PcuZFMHzjY3lwMXsnT X-Received: by 2002:a05:622a:184:b0:31e:d9a2:1e7 with SMTP id s4-20020a05622a018400b0031ed9a201e7mr21769870qtw.600.1660082471359; Tue, 09 Aug 2022 15:01:11 -0700 (PDT) X-Google-Smtp-Source: AA6agR6rCoAdukBJ919s0iooFl+Su6+6WLl8fX28qz4Ljb6jLzuDkULdmT6xOGx8775yUr2wJWOu3Q== X-Received: by 2002:a05:622a:184:b0:31e:d9a2:1e7 with SMTP id s4-20020a05622a018400b0031ed9a201e7mr21769841qtw.600.1660082471059; Tue, 09 Aug 2022 15:01:11 -0700 (PDT) Received: from localhost.localdomain (bras-base-aurron9127w-grc-35-70-27-3-10.dsl.bell.ca. [70.27.3.10]) by smtp.gmail.com with ESMTPSA id b16-20020a05622a021000b0031eddc83560sm10563383qtx.90.2022.08.09.15.01.09 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 09 Aug 2022 15:01:10 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: peterx@redhat.com, Minchan Kim , David Hildenbrand , Nadav Amit , Andrew Morton , Hugh Dickins , Vlastimil Babka , Andrea Arcangeli , Andi Kleen , Huang Ying , "Kirill A . Shutemov" Subject: [PATCH v3 6/7] mm/swap: Cache maximum swapfile size when init swap Date: Tue, 9 Aug 2022 18:00:59 -0400 Message-Id: <20220809220100.20033-7-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220809220100.20033-1-peterx@redhat.com> References: <20220809220100.20033-1-peterx@redhat.com> 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" We used to have swapfile_maximum_size() fetching a maximum value of swapfile size per-arch. As the caller of max_swapfile_size() grows, this patch introduce a variable "swapfile_maximum_size" and cache the value of old max_swapfile_size(), so that we don't need to calculate the value every time. Caching the value in swapfile_init() is safe because when reaching the phase we should have initialized all the relevant information. Here the major arch to look after is x86, which defines the max size based on L1TF mitigation. Here both X86_BUG_L1TF or l1tf_mitigation should have been setup properly when reaching swapfile_init(). As a reference, the code path looks like this for x86: - start_kernel - setup_arch - early_cpu_init - early_identify_cpu --> setup X86_BUG_L1TF - parse_early_param - l1tf_cmdline --> set l1tf_mitigation - check_bugs - l1tf_select_mitigation --> set l1tf_mitigation - arch_call_rest_init - rest_init - kernel_init - kernel_init_freeable - do_basic_setup - do_initcalls --> calls swapfile_init() (initcall level 4) The swapfile size only depends on swp pte format on non-x86 archs, so caching it is safe too. Since at it, rename max_swapfile_size() to arch_max_swapfile_size() because arch can define its own function, so it's more straightforward to have "arch_" as its prefix. At the meantime, keep the swapfile_maximum_size() function to fetch the value from the cache initialized in swapfile_init(). Signed-off-by: Peter Xu --- arch/x86/mm/init.c | 2 +- mm/swapfile.c | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c index 82a042c03824..9121bc1b9453 100644 --- a/arch/x86/mm/init.c +++ b/arch/x86/mm/init.c @@ -1054,7 +1054,7 @@ void update_cache_mode_entry(unsigned entry, enum pag= e_cache_mode cache) } =20 #ifdef CONFIG_SWAP -unsigned long max_swapfile_size(void) +unsigned long arch_max_swapfile_size(void) { unsigned long pages; =20 diff --git a/mm/swapfile.c b/mm/swapfile.c index 1fdccd2f1422..794fa37bd0c3 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -63,6 +63,7 @@ EXPORT_SYMBOL_GPL(nr_swap_pages); /* protected with swap_lock. reading in vm_swap_full() doesn't need lock */ long total_swap_pages; static int least_priority =3D -1; +static unsigned long swapfile_maximum_size; =20 static const char Bad_file[] =3D "Bad swap file entry "; static const char Unused_file[] =3D "Unused swap file entry "; @@ -2816,11 +2817,16 @@ unsigned long generic_max_swapfile_size(void) } =20 /* Can be overridden by an architecture for additional checks. */ -__weak unsigned long max_swapfile_size(void) +__weak unsigned long arch_max_swapfile_size(void) { return generic_max_swapfile_size(); } =20 +unsigned long max_swapfile_size(void) +{ + return swapfile_maximum_size; +} + static unsigned long read_swap_header(struct swap_info_struct *p, union swap_header *swap_header, struct inode *inode) @@ -3677,6 +3683,8 @@ static int __init swapfile_init(void) for_each_node(nid) plist_head_init(&swap_avail_heads[nid]); =20 + swapfile_maximum_size =3D arch_max_swapfile_size(); + return 0; } subsys_initcall(swapfile_init); --=20 2.32.0 From nobody Sat Apr 11 15:28:08 2026 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 9030BC28B2B for ; Tue, 9 Aug 2022 22:02:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229931AbiHIWCE (ORCPT ); Tue, 9 Aug 2022 18:02:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229867AbiHIWBS (ORCPT ); Tue, 9 Aug 2022 18:01:18 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0D0FFE08 for ; Tue, 9 Aug 2022 15:01:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660082474; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8oTmLlquT/JmXxP88HUbcjeMqANq5xLJcNEKCKe+fKE=; b=S69FzfOWOVq7JyjaS/1d3hshovEm2x5unC1AWQ3zrK02vGC1wDgkd/NW2qMYCGJ8QHfuqJ +bf9aMoUtk21uhmD/QK6tvXkIEo6jJfMcNGaiBUx/hDkPqKuvb9hwrN/605NnzgNevRBBj gQikm7ehjiQLwgyjpJylN+gf90rHmdM= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-672-k0QSOui-N8mT0mAZqRKEzA-1; Tue, 09 Aug 2022 18:01:13 -0400 X-MC-Unique: k0QSOui-N8mT0mAZqRKEzA-1 Received: by mail-qk1-f197.google.com with SMTP id s9-20020a05620a254900b006b54dd4d6deso11149622qko.3 for ; Tue, 09 Aug 2022 15:01:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=8oTmLlquT/JmXxP88HUbcjeMqANq5xLJcNEKCKe+fKE=; b=uKcuoSlndzA42B/EIVgdsQVfQSoy1QAnWAVnxL0Idg/GejLgVy5Ld2yy6RWPJXjuR1 bN+GN4/Gmeh6Ahcw7bsY7iOsE+cwZr5Qu7wEqH9skm4v8c2UQzkDm2AkKTduFihg1ape n1Ba+8GkEJBrfFwnAS1ZP5MP2s0gIpAI8Kux2ejcy0+VoZfFig/Cw1nPBpKeJj+zcVSO SCZ20aO1E4JUtYN09xP/fAskQdMDwS7rmSokmpPr5UqrX5Jos7Yy3lhmAp3f2UKDwBE0 TsrHSorAuC+KdcS7sE8AfhPw2l62sFrl9BHxTIWrTnYb1ESW9lwsWKgb2JfFLp1G9TAl 9fBA== X-Gm-Message-State: ACgBeo2gjijv+ecG6smb2gtKVaD/ESLWkcYGGPMZLYe2qYzcxWgSNm7p I8yzx/7tE5pApTWPv7XiFKfjDP15B9M9Wg/TxtQX7DKGCYkd77BSb1Top/QpbGKRHBgGYoMJwfT /kLDtWVxJJ4TnsXzqEgDTk23J X-Received: by 2002:ad4:5745:0:b0:474:73b3:8e6b with SMTP id q5-20020ad45745000000b0047473b38e6bmr21943586qvx.30.1660082472645; Tue, 09 Aug 2022 15:01:12 -0700 (PDT) X-Google-Smtp-Source: AA6agR7zy2JuWvJjf9RyNp22pt6EAQPYAGfW6c3P8B5Al6S1qhuoVayGnuKePcXRi5AkcG0+83tbPw== X-Received: by 2002:ad4:5745:0:b0:474:73b3:8e6b with SMTP id q5-20020ad45745000000b0047473b38e6bmr21943562qvx.30.1660082472384; Tue, 09 Aug 2022 15:01:12 -0700 (PDT) Received: from localhost.localdomain (bras-base-aurron9127w-grc-35-70-27-3-10.dsl.bell.ca. [70.27.3.10]) by smtp.gmail.com with ESMTPSA id b16-20020a05622a021000b0031eddc83560sm10563383qtx.90.2022.08.09.15.01.11 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 09 Aug 2022 15:01:12 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: peterx@redhat.com, Minchan Kim , David Hildenbrand , Nadav Amit , Andrew Morton , Hugh Dickins , Vlastimil Babka , Andrea Arcangeli , Andi Kleen , Huang Ying , "Kirill A . Shutemov" Subject: [PATCH v3 7/7] mm/swap: Cache swap migration A/D bits support Date: Tue, 9 Aug 2022 18:01:00 -0400 Message-Id: <20220809220100.20033-8-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220809220100.20033-1-peterx@redhat.com> References: <20220809220100.20033-1-peterx@redhat.com> 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" Introduce a variable swap_migration_ad_supported to cache whether the arch supports swap migration A/D bits. Here one thing to mention is that SWP_MIG_TOTAL_BITS will internally reference the other macro MAX_PHYSMEM_BITS, which is a function call on x86 (constant on all the rest of archs). It's safe to reference it in swapfile_init() because when reaching here we're already during initcalls level 4 so we must have initialized 5-level pgtable for x86_64 (right after early_identify_cpu() finishes). - start_kernel - setup_arch - early_cpu_init - get_cpu_cap --> fetch from CPUID (including X86_FEATURE_LA57) - early_identify_cpu --> clear X86_FEATURE_LA57 (if early lvl5 not en= abled (USE_EARLY_PGTABLE_L5)) - arch_call_rest_init - rest_init - kernel_init - kernel_init_freeable - do_basic_setup - do_initcalls --> calls swapfile_init() (initcall level 4) This should slightly speed up the migration swap entry handlings. Signed-off-by: Peter Xu --- include/linux/swapfile.h | 1 + include/linux/swapops.h | 7 +------ mm/swapfile.c | 8 ++++++++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/include/linux/swapfile.h b/include/linux/swapfile.h index 54078542134c..87ec5e2cdb02 100644 --- a/include/linux/swapfile.h +++ b/include/linux/swapfile.h @@ -9,5 +9,6 @@ extern struct swap_info_struct *swap_info[]; extern unsigned long generic_max_swapfile_size(void); extern unsigned long max_swapfile_size(void); +extern bool swap_migration_ad_supported; =20 #endif /* _LINUX_SWAPFILE_H */ diff --git a/include/linux/swapops.h b/include/linux/swapops.h index 0e9579b90659..e6afc77c51ad 100644 --- a/include/linux/swapops.h +++ b/include/linux/swapops.h @@ -301,13 +301,8 @@ static inline swp_entry_t make_writable_migration_entr= y(pgoff_t offset) */ static inline bool migration_entry_supports_ad(void) { - /* - * max_swapfile_size() returns the max supported swp-offset plus 1. - * We can support the migration A/D bits iff the pfn swap entry has - * the offset large enough to cover all of them (PFN, A & D bits). - */ #ifdef CONFIG_SWAP - return max_swapfile_size() >=3D (1UL << SWP_MIG_TOTAL_BITS); + return swap_migration_ad_supported; #else /* CONFIG_SWAP */ return false; #endif /* CONFIG_SWAP */ diff --git a/mm/swapfile.c b/mm/swapfile.c index 794fa37bd0c3..c49cf25f0d08 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -64,6 +64,9 @@ EXPORT_SYMBOL_GPL(nr_swap_pages); long total_swap_pages; static int least_priority =3D -1; static unsigned long swapfile_maximum_size; +#ifdef CONFIG_MIGRATION +bool swap_migration_ad_supported; +#endif /* CONFIG_MIGRATION */ =20 static const char Bad_file[] =3D "Bad swap file entry "; static const char Unused_file[] =3D "Unused swap file entry "; @@ -3685,6 +3688,11 @@ static int __init swapfile_init(void) =20 swapfile_maximum_size =3D arch_max_swapfile_size(); =20 +#ifdef CONFIG_MIGRATION + if (swapfile_maximum_size >=3D (1UL << SWP_MIG_TOTAL_BITS)) + swap_migration_ad_supported =3D true; +#endif /* CONFIG_MIGRATION */ + return 0; } subsys_initcall(swapfile_init); --=20 2.32.0