From nobody Wed Apr 15 00:02:43 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 AB288C00140 for ; Fri, 29 Jul 2022 01:40:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233694AbiG2Bkx (ORCPT ); Thu, 28 Jul 2022 21:40:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233625AbiG2Bkt (ORCPT ); Thu, 28 Jul 2022 21:40:49 -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 040751AD81 for ; Thu, 28 Jul 2022 18:40:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659058846; 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=ByVAEoHvRqEiYcceU8xxkCrxysohxn623txuIswhCFQ=; b=AGnBBDNawTY9n89dkIUkjnn3f1/h4NEr317fX45excq7z8DMljQxZ/Skv0Q64cfGP6zA+m MrkNeptRxDkY3rfyRTJ+BTLbWGQ82Ptzt+kZoYcoeuW6uJUPn6xhRKSlRKzOMwogMvbTa9 kSrNOfguMLmK9PN1gqM7xLHYNLolrrU= 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-7-JSecT2YCNHaKi-wn9a09Dw-1; Thu, 28 Jul 2022 21:40:45 -0400 X-MC-Unique: JSecT2YCNHaKi-wn9a09Dw-1 Received: by mail-qv1-f70.google.com with SMTP id ok7-20020a0562143c8700b00472f0b33853so2419261qvb.12 for ; Thu, 28 Jul 2022 18:40:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ByVAEoHvRqEiYcceU8xxkCrxysohxn623txuIswhCFQ=; b=iRpM/eW/YvAceDtJEhmcQ778ohqKs/4Y2lqMl/TPZ3R16SEnA7UHQPumQ4fbCxtzbt DMSJtHPyLMrletVtUNYdZpNOTh7K2u4BdUyjYcBPXfpON7jwf62FhxQYlYeQGoPVhJiP oFYK5yP85xPZ65O56F6ZqZPTykmiBsr4i7Pe1Sy2wOFFEQV1IZvf6wRYDb3TRDHR1RjR PRO3QyqsJL1OMpMcHsSC8mG2zJO5V/ULbq0VfC5/sCjqtUidKj3QJ7hv5AzNWAKRaP5x yDocM6gW4iIw7dUZZYfN+44IaW+LRWue6efpU8XJnlQr+ED87/f9F7VU66nqDTXxdpT2 PutA== X-Gm-Message-State: AJIora/VGYikd5kln2s/DYZ1UqRaJALfCYGkkcm8kkqFk1YAwTZjmyTv H3IE/BnNOSvZr/IynqAOUViWFlHRtXAr+xyhp++kMBXuJOpp0HG5jajMLEFQuEBo7fCZqpA504Y W4Gkb1ZrvEAvHdSLOiuAYF9L2 X-Received: by 2002:a05:622a:1a0d:b0:31e:da67:7cb4 with SMTP id f13-20020a05622a1a0d00b0031eda677cb4mr1543902qtb.643.1659058844956; Thu, 28 Jul 2022 18:40:44 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vm3fD51DEm5b8XSQBVK347L4yufDVjbS4SrwnC/QphKpN4yRq5nbIIELe355N7kwGIcwdXgA== X-Received: by 2002:a05:622a:1a0d:b0:31e:da67:7cb4 with SMTP id f13-20020a05622a1a0d00b0031eda677cb4mr1543886qtb.643.1659058844712; Thu, 28 Jul 2022 18:40:44 -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 u9-20020a05620a454900b006b259b5dd12sm1584531qkp.53.2022.07.28.18.40.43 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 28 Jul 2022 18:40:44 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Huang Ying , peterx@redhat.com, Andrea Arcangeli , Andrew Morton , "Kirill A . Shutemov" , Nadav Amit , Hugh Dickins , David Hildenbrand , Vlastimil Babka Subject: [PATCH RFC 1/4] mm/swap: Add swp_offset_pfn() to fetch PFN from swap entry Date: Thu, 28 Jul 2022 21:40:38 -0400 Message-Id: <20220729014041.21292-2-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220729014041.21292-1-peterx@redhat.com> References: <20220729014041.21292-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 | 28 ++++++++++++++++++++++------ mm/hmm.c | 2 +- mm/memory-failure.c | 2 +- mm/page_vma_mapped.c | 6 +++--- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 7430060cb0d6..f897d40821dd 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -242,7 +242,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 a3d435bf9f97..5378f77860fb 100644 --- a/include/linux/swapops.h +++ b/include/linux/swapops.h @@ -23,6 +23,14 @@ #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. + */ +#define SWP_PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT) +#define SWP_PFN_MASK ((1UL << SWP_PFN_BITS) - 1) + /* Clear all flags but only keep swp_entry_t related information */ static inline pte_t pte_swp_clear_flags(pte_t pte) { @@ -64,6 +72,16 @@ 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) +{ + 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 +387,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 +405,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 +496,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 cc6fc9be8d22..e451219124dd 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 Wed Apr 15 00:02:43 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 B568DC00140 for ; Fri, 29 Jul 2022 01:40:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233706AbiG2Bk5 (ORCPT ); Thu, 28 Jul 2022 21:40:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232005AbiG2Bku (ORCPT ); Thu, 28 Jul 2022 21:40:50 -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 3EDF01B7BA for ; Thu, 28 Jul 2022 18:40:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659058848; 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=1Pdq3GP5K2kZBYouzoKDRpFce2r59+NT7hzfgEhUpfY=; b=LN4DbB7wFQlmk1Ax2mYy2jguonAZHqeC9umsChtd4Hqi6CrTq+Od2tTmumfcycHpUWBoMv XAd9jUp+c72AGFtK7qT7ROLfY98Rfi2mZWm67hir76gpmgC2/iRQGNjaTPK4qMSoDjQqyb 7thTz7bIdngZb/UwSHsTNvP+QdnllM8= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-631-MdSueNcYPOCQeuklAageSw-1; Thu, 28 Jul 2022 21:40:47 -0400 X-MC-Unique: MdSueNcYPOCQeuklAageSw-1 Received: by mail-qt1-f199.google.com with SMTP id r5-20020ac85c85000000b0031ecf611c64so2094473qta.23 for ; Thu, 28 Jul 2022 18:40:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1Pdq3GP5K2kZBYouzoKDRpFce2r59+NT7hzfgEhUpfY=; b=fXUlsx6HH1AgjAo+tEEdnU3RmSffGnnSCXdkiXn5oSt5qszzfwOSOA7XANIwQ47gGc 7nvx8c4JYXScx+Wy/1TH1mMZ/Ui1m4w9RJC4UcJ6rdWvHoGuhLoYgIjSK9RF2tsytlJr sChP1yhR1I8MlMuqFoJbfT8JNRxVoeXUYrA9CmcHc/3EaV2m6xKcJLQbElKd0ngpWVAd LUHEo0IHdhJK2dM4kTdIiKdvbeisyCk6zA8SwB/xpJaItNgHPwWJcEJ85O2i0mQy/S3v IbsKELZHSBfMfEh6CVeGIumAfhHYp9M+YJoBlrbN2NaUi30f+Q02LFu5pYghw5ywu+HD vDwg== X-Gm-Message-State: AJIora//F3EweO2bHAMSCT9wbVRDbWEGU+5K8DFkZD3EfAiB7LfSV2j+ Bv7DU6jym9tHNsKauLKwkDwYxG5i27Tj+wVUgcSzdrCrBlIl1x+pL8P349xgzXlMiyTWXAca5gx D/mSNajn8fzEpB26G/PqncY3S X-Received: by 2002:a37:3c3:0:b0:6b5:cd61:cef with SMTP id 186-20020a3703c3000000b006b5cd610cefmr1179617qkd.368.1659058846481; Thu, 28 Jul 2022 18:40:46 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uTTF+MpDAe6hgcAbyvybxSoNRT6Fd6ul/QwMr7NbcBL/2EhxKg35/KJlM9ChukfGVxvuJLdw== X-Received: by 2002:a37:3c3:0:b0:6b5:cd61:cef with SMTP id 186-20020a3703c3000000b006b5cd610cefmr1179603qkd.368.1659058846218; Thu, 28 Jul 2022 18:40:46 -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 u9-20020a05620a454900b006b259b5dd12sm1584531qkp.53.2022.07.28.18.40.44 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 28 Jul 2022 18:40:45 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Huang Ying , peterx@redhat.com, Andrea Arcangeli , Andrew Morton , "Kirill A . Shutemov" , Nadav Amit , Hugh Dickins , David Hildenbrand , Vlastimil Babka Subject: [PATCH RFC 2/4] mm: Remember young bit for page migrations Date: Thu, 28 Jul 2022 21:40:39 -0400 Message-Id: <20220729014041.21292-3-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220729014041.21292-1-peterx@redhat.com> References: <20220729014041.21292-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 bit settings in the old pgtable, and marking the page as old in the new page table using either pte_mkold() or pmd_mkold(). 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. Actually we can easily remember the young bit configuration and make that information recovered when the page is migrated. To achieve it, define a new bit in the migration swap offset field to show whether the old pte has young bit set or not. Then when removing/recovering the migration entry, we can recover the young bit even if the page changed. One thing to mention is that the whole feature is based on an arch specific macro __ARCH_SWP_OFFSET_BITS that needs to be defined per-arch. The macro tells how many bits are available for the arch specific swp offset field. When that macro is not defined, we'll assume we don't have free bits in the migration swap entry offset, so we can't persist the young bit. So until now, there should have no functional change at all with this patch, since no arch has yet defined __ARCH_SWP_OFFSET_BITS. Signed-off-by: Peter Xu --- include/linux/swapops.h | 57 +++++++++++++++++++++++++++++++++++++++++ mm/huge_memory.c | 10 ++++++-- mm/migrate.c | 4 ++- mm/migrate_device.c | 2 ++ mm/rmap.c | 3 ++- 5 files changed, 72 insertions(+), 4 deletions(-) diff --git a/include/linux/swapops.h b/include/linux/swapops.h index 5378f77860fb..3bbb57aa6742 100644 --- a/include/linux/swapops.h +++ b/include/linux/swapops.h @@ -31,6 +31,28 @@ #define SWP_PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT) #define SWP_PFN_MASK ((1UL << SWP_PFN_BITS) - 1) =20 +#ifdef __ARCH_SWP_OFFSET_BITS +#define SWP_PFN_OFFSET_FREE_BITS (__ARCH_SWP_OFFSET_BITS - SWP_PFN_BITS) +#else +/* + * If __ARCH_SWP_OFFSET_BITS not defined, assuming we don't have free bits + * to be on the safe side. + */ +#define SWP_PFN_OFFSET_FREE_BITS 0 +#endif + +/** + * Migration swap entry specific bitfield definitions. + * + * @SWP_MIG_YOUNG_BIT: Whether the page used to have young bit set + * + * Note: these bits will be used only if there're free bits in arch + * specific swp offset field. Arch needs __ARCH_SWP_OFFSET_BITS defined to + * use the bits/features. + */ +#define SWP_MIG_YOUNG_BIT (1UL << SWP_PFN_BITS) +#define SWP_MIG_OFFSET_BITS (SWP_PFN_BITS + 1) + /* Clear all flags but only keep swp_entry_t related information */ static inline pte_t pte_swp_clear_flags(pte_t pte) { @@ -258,6 +280,30 @@ static inline swp_entry_t make_writable_migration_entr= y(pgoff_t offset) return swp_entry(SWP_MIGRATION_WRITE, offset); } =20 +static inline swp_entry_t make_migration_entry_young(swp_entry_t entry) +{ + /* + * Due to a limitation on x86_64 we can't use #ifdef, as + * SWP_PFN_OFFSET_FREE_BITS value can be changed dynamically for + * 4/5 level pgtables. For all the non-x86_64 archs (where the + * macro MAX_PHYSMEM_BITS is constant) this branching should be + * optimized out by the compiler. + */ + if (SWP_PFN_OFFSET_FREE_BITS) + return swp_entry(swp_type(entry), + swp_offset(entry) | SWP_MIG_YOUNG_BIT); + return entry; +} + +static inline bool is_migration_entry_young(swp_entry_t entry) +{ + /* Please refer to comment in make_migration_entry_young() */ + if (SWP_PFN_OFFSET_FREE_BITS) + return swp_offset(entry) & SWP_MIG_YOUNG_BIT; + /* Keep the old behavior of aging 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, @@ -304,6 +350,16 @@ 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; +} + #endif =20 typedef unsigned long pte_marker; @@ -407,6 +463,7 @@ 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); + BUILD_BUG_ON(SWP_TYPE_SHIFT < SWP_MIG_OFFSET_BITS); =20 return is_migration_entry(entry) || is_device_private_entry(entry) || is_device_exclusive_entry(entry); diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 29e3628687a6..131fe5754d8f 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2088,7 +2088,7 @@ 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); soft_dirty =3D pmd_swp_soft_dirty(old_pmd); uffd_wp =3D pmd_swp_uffd_wp(old_pmd); } else { @@ -2146,6 +2146,8 @@ static void __split_huge_pmd_locked(struct vm_area_st= ruct *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); entry =3D swp_entry_to_pte(swp_entry); if (soft_dirty) entry =3D pte_swp_mksoft_dirty(entry); @@ -3148,6 +3150,8 @@ int set_pmd_migration_entry(struct page_vma_mapped_wa= lk *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); pmdswp =3D swp_entry_to_pmd(entry); if (pmd_soft_dirty(pmdval)) pmdswp =3D pmd_swp_mksoft_dirty(pmdswp); @@ -3173,13 +3177,15 @@ 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); =20 if (PageAnon(new)) { rmap_t rmap_flags =3D RMAP_COMPOUND; diff --git a/mm/migrate.c b/mm/migrate.c index 1649270bc1a7..62cb3a9451de 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -199,7 +199,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 @@ -207,6 +207,8 @@ 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 (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 7feeb447e3b9..fd8daf45c1a6 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -221,6 +221,8 @@ 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); 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..605fb37ae95e 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2065,7 +2065,8 @@ static bool try_to_migrate_one(struct folio *folio, s= truct 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); 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 Wed Apr 15 00:02:43 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 5C238C00140 for ; Fri, 29 Jul 2022 01:41:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233676AbiG2BlG (ORCPT ); Thu, 28 Jul 2022 21:41:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233678AbiG2Bkv (ORCPT ); Thu, 28 Jul 2022 21:40:51 -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 989611B7BA for ; Thu, 28 Jul 2022 18:40:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659058849; 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=MNwWCoZOuwPxSYyDQ6YzxKBvZEYHs5K0G6f2+t5kiFc=; b=Hei0UT4Xd2mE/KvgiXPoUYwRf8LcHMnsIAYyKx3VYOUDowJ4sIG4/NCdm4sHIbbDP68BPh simTkoYPrE9PrlvnvIpuaj3IWCqck0EGcKDSKxBxHO06O7DFqmTh3GINtDFFVX6HX2FcYL tlWqYIaDMgP1r/wcHzWy4yQeFZkfcFQ= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-605-n5qBaePBMnONndw_VG8pPA-1; Thu, 28 Jul 2022 21:40:48 -0400 X-MC-Unique: n5qBaePBMnONndw_VG8pPA-1 Received: by mail-qk1-f199.google.com with SMTP id bj26-20020a05620a191a00b006b5c4e2dc77so2622011qkb.16 for ; Thu, 28 Jul 2022 18:40:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=MNwWCoZOuwPxSYyDQ6YzxKBvZEYHs5K0G6f2+t5kiFc=; b=Euq+szraIul2ixTE5lXtzEtGxYe2VGYjtYT19bzQzAp5Imo3izoSapG/8AqQyofUND oHWPzi23G0VKENuQkvHaAu+Ihwng4ZHAtgUWwf4Zf+hxkCXEEW3N6nnmCPE/F+ytHw+b VJnc573aqsMkCYF2B5zdMSl6WjJxrrPJSigsRtPfLCTa2J2U7+uPQ/6Fvn365KSe35HD HIiCobvVoaJBbicahM8SkJXO0Pj4e6vay0XHnrvKiE0s6MZyGzp6c1IKTOZuB7LKisj5 UoYJ2RifaNOrD97/Yt9YqBAEuOxg3zVp/mOjm5+YSqrcHd0LWGR0rj83eNQiK7SDqmBc yGQQ== X-Gm-Message-State: AJIora/whCMiLqW6rtlxK41ykBs3GQ68Oqd5GVb44xAMx5VmLC2jv9jC 8dnc/sAgb+t+TBVY2ulYdQ1rpQFXxTyP/szmgaOgThMS8j+g96Cb2hlk9OYvYIoI9BnTNIrLn6M Ad5XlxXmhO5POMEIl3lOK5pZ9 X-Received: by 2002:a05:620a:19a7:b0:6b6:b88:3c77 with SMTP id bm39-20020a05620a19a700b006b60b883c77mr1212517qkb.128.1659058848047; Thu, 28 Jul 2022 18:40:48 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uZayzexwXnoLh2k6d3k24YaRhYz1essZsiXQQVUsnUpgPU7CF4SN2TaMXPZPqXjes7ezd3dQ== X-Received: by 2002:a05:620a:19a7:b0:6b6:b88:3c77 with SMTP id bm39-20020a05620a19a700b006b60b883c77mr1212495qkb.128.1659058847825; Thu, 28 Jul 2022 18:40:47 -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 u9-20020a05620a454900b006b259b5dd12sm1584531qkp.53.2022.07.28.18.40.46 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 28 Jul 2022 18:40:47 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Huang Ying , peterx@redhat.com, Andrea Arcangeli , Andrew Morton , "Kirill A . Shutemov" , Nadav Amit , Hugh Dickins , David Hildenbrand , Vlastimil Babka Subject: [PATCH RFC 3/4] mm/x86: Use SWP_TYPE_BITS in 3-level swap macros Date: Thu, 28 Jul 2022 21:40:40 -0400 Message-Id: <20220729014041.21292-4-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220729014041.21292-1-peterx@redhat.com> References: <20220729014041.21292-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. Signed-off-by: Peter Xu --- 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 Wed Apr 15 00:02:43 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 3AF8DC00140 for ; Fri, 29 Jul 2022 01:41:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232851AbiG2BlL (ORCPT ); Thu, 28 Jul 2022 21:41:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233691AbiG2Bkx (ORCPT ); Thu, 28 Jul 2022 21:40:53 -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 307091EC58 for ; Thu, 28 Jul 2022 18:40:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659058851; 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=uHCAtjKZb2F/RWm6an0jiLe9Ivmk/LTJJW/gUBH3IyE=; b=UMtlD86tCEgq4P5alY/2+yqc4dUS0/WHWGAy0si/33cKjFYIA6aiRjwijjt9O1aPx4PHIM RiafF0BqwnaucU88bHOvKZh96pse6zh/glNZyLZjYwdPvC7PUdPUOqiwsuI8P6fAFPBxkd WQ4M0+HLqD+F1goX/NP8WoKKhGgGhnA= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-612-2up3VAdDNV6kIKSK3g5i8A-1; Thu, 28 Jul 2022 21:40:50 -0400 X-MC-Unique: 2up3VAdDNV6kIKSK3g5i8A-1 Received: by mail-qt1-f197.google.com with SMTP id ay25-20020a05622a229900b0031ef5fdf8f8so2064316qtb.7 for ; Thu, 28 Jul 2022 18:40:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=uHCAtjKZb2F/RWm6an0jiLe9Ivmk/LTJJW/gUBH3IyE=; b=gNLMprhRm0psrNX8cdUYPh1DK26BBg2sySTsXW5cVHy4MDMz54z48kUYzHnmawtlJv TWGPIaiJnHvO9GNx8Di7E23ZQZ7MaUBGqSubUnSMSw3RWsLNyA56oMBqoHjn1E0rhkAB xqQf6Z/NeYr/7fAo8gMatlEXxs8C2Nk6Rj/3CAKWcwPgbzS7eJVoplYaxODmL2bpA1Nn 8y2GK+fujTmJmdBuW3n87QSrNiUZyB8GKNUtp051Oq3t4nLsLwBtaNzEiiT2Wb4gRlZb L1PRfUP+SSa5w1INvJnMXqoFLNfy89rzHvqJQfsY4DPkD0mYcbNdMYJOAgRm8uIuTPfv Qi6Q== X-Gm-Message-State: AJIora+zWRSKDjD2kgM8z9bB0SRo3UWJsfJyWWpAgovyFES+Gmp0A7nS IGt7QgcrCOItojTVjrRi4+EbspR541EhCyQR/BPYGAKSjKEoO4H8Pih5NaYRypCNz4g6cDmkQyv 8UnKn6G18KlTqeC7g484pkflL X-Received: by 2002:a05:620a:440e:b0:6b2:82cf:d7f9 with SMTP id v14-20020a05620a440e00b006b282cfd7f9mr1167965qkp.761.1659058849549; Thu, 28 Jul 2022 18:40:49 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tfN//pDoVVznadr+/YqP0HOgEHhd9oPJGkyGyuWdvXw2h2kgiA9ytnZN60UnD+9V24+DCcyQ== X-Received: by 2002:a05:620a:440e:b0:6b2:82cf:d7f9 with SMTP id v14-20020a05620a440e00b006b282cfd7f9mr1167951qkp.761.1659058849320; Thu, 28 Jul 2022 18:40:49 -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 u9-20020a05620a454900b006b259b5dd12sm1584531qkp.53.2022.07.28.18.40.48 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 28 Jul 2022 18:40:48 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Huang Ying , peterx@redhat.com, Andrea Arcangeli , Andrew Morton , "Kirill A . Shutemov" , Nadav Amit , Hugh Dickins , David Hildenbrand , Vlastimil Babka Subject: [PATCH RFC 4/4] mm/x86: Define __ARCH_SWP_OFFSET_BITS Date: Thu, 28 Jul 2022 21:40:41 -0400 Message-Id: <20220729014041.21292-5-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220729014041.21292-1-peterx@redhat.com> References: <20220729014041.21292-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" This will enable the new migration young bit for all x86 systems for both 32 bits and 64 bits systems (including PAE). Signed-off-by: Peter Xu --- arch/x86/include/asm/pgtable-2level.h | 6 ++++++ arch/x86/include/asm/pgtable-3level.h | 7 +++++++ arch/x86/include/asm/pgtable_64.h | 5 +++++ 3 files changed, 18 insertions(+) diff --git a/arch/x86/include/asm/pgtable-2level.h b/arch/x86/include/asm/p= gtable-2level.h index 60d0f9015317..6e70833feb69 100644 --- a/arch/x86/include/asm/pgtable-2level.h +++ b/arch/x86/include/asm/pgtable-2level.h @@ -95,6 +95,12 @@ static inline unsigned long pte_bitop(unsigned long valu= e, unsigned int rightshi #define __pte_to_swp_entry(pte) ((swp_entry_t) { (pte).pte_low }) #define __swp_entry_to_pte(x) ((pte_t) { .pte =3D (x).val }) =20 +/* + * This defines how many bits we have in the arch specific swp offset. + * For 32 bits vanilla systems the pte and swap entry has the same size. + */ +#define __ARCH_SWP_OFFSET_BITS (sizeof(swp_entry_t) - SWP_TYPE_BITS) + /* No inverted PFNs on 2 level page tables */ =20 static inline u64 protnone_mask(u64 val) diff --git a/arch/x86/include/asm/pgtable-3level.h b/arch/x86/include/asm/p= gtable-3level.h index 28421a887209..8dbf29b51f8b 100644 --- a/arch/x86/include/asm/pgtable-3level.h +++ b/arch/x86/include/asm/pgtable-3level.h @@ -287,6 +287,13 @@ static inline pud_t native_pudp_get_and_clear(pud_t *p= udp) #define __pte_to_swp_entry(pte) (__swp_entry(__pteval_swp_type(pte), \ __pteval_swp_offset(pte))) =20 +/* + * This defines how many bits we have in the arch specific swp offset. + * Here since we're putting the 32 bits swap entry into 64 bits pte, the + * limitation is the 32 bits swap entry minus the swap type field. + */ +#define __ARCH_SWP_OFFSET_BITS (sizeof(swp_entry_t) - SWP_TYPE_BITS) + #include =20 #endif /* _ASM_X86_PGTABLE_3LEVEL_H */ diff --git a/arch/x86/include/asm/pgtable_64.h b/arch/x86/include/asm/pgtab= le_64.h index e479491da8d5..1714f0ded1db 100644 --- a/arch/x86/include/asm/pgtable_64.h +++ b/arch/x86/include/asm/pgtable_64.h @@ -217,6 +217,11 @@ static inline void native_pgd_clear(pgd_t *pgd) =20 /* 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) +/* + * This defines how many bits we have in the arch specific swp offset. 64 + * bits systems have both swp_entry_t and pte in 64 bits. + */ +#define __ARCH_SWP_OFFSET_BITS (BITS_PER_LONG - SWP_OFFSET_SHIFT) =20 #define MAX_SWAPFILES_CHECK() BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > SWP_TYPE_= BITS) =20 --=20 2.32.0