Skip to content

Commit 0d206b5

Browse files
xzpeterakpm00
authored andcommitted
mm/swap: add swp_offset_pfn() to fetch PFN from swap entry
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. Link: https://lkml.kernel.org/r/20220811161331.37055-4-peterx@redhat.com Signed-off-by: Peter Xu <peterx@redhat.com> Reviewed-by: "Huang, Ying" <ying.huang@intel.com> Cc: Alistair Popple <apopple@nvidia.com> Cc: Andi Kleen <andi.kleen@intel.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: David Hildenbrand <david@redhat.com> Cc: Hugh Dickins <hughd@google.com> Cc: "Kirill A . Shutemov" <kirill@shutemov.name> Cc: Minchan Kim <minchan@kernel.org> Cc: Nadav Amit <nadav.amit@gmail.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Dave Hansen <dave.hansen@intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent eba4d77 commit 0d206b5

File tree

6 files changed

+52
-15
lines changed

6 files changed

+52
-15
lines changed

arch/arm64/mm/hugetlbpage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ static inline struct folio *hugetlb_swap_entry_to_folio(swp_entry_t entry)
245245
{
246246
VM_BUG_ON(!is_migration_entry(entry) && !is_hwpoison_entry(entry));
247247

248-
return page_folio(pfn_to_page(swp_offset(entry)));
248+
return page_folio(pfn_to_page(swp_offset_pfn(entry)));
249249
}
250250

251251
void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,

fs/proc/task_mmu.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,9 +1418,19 @@ static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
14181418
if (pte_swp_uffd_wp(pte))
14191419
flags |= PM_UFFD_WP;
14201420
entry = pte_to_swp_entry(pte);
1421-
if (pm->show_pfn)
1421+
if (pm->show_pfn) {
1422+
pgoff_t offset;
1423+
/*
1424+
* For PFN swap offsets, keeping the offset field
1425+
* to be PFN only to be compatible with old smaps.
1426+
*/
1427+
if (is_pfn_swap_entry(entry))
1428+
offset = swp_offset_pfn(entry);
1429+
else
1430+
offset = swp_offset(entry);
14221431
frame = swp_type(entry) |
1423-
(swp_offset(entry) << MAX_SWAPFILES_SHIFT);
1432+
(offset << MAX_SWAPFILES_SHIFT);
1433+
}
14241434
flags |= PM_SWAP;
14251435
migration = is_migration_entry(entry);
14261436
if (is_pfn_swap_entry(entry))
@@ -1477,7 +1487,11 @@ static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
14771487
unsigned long offset;
14781488

14791489
if (pm->show_pfn) {
1480-
offset = swp_offset(entry) +
1490+
if (is_pfn_swap_entry(entry))
1491+
offset = swp_offset_pfn(entry);
1492+
else
1493+
offset = swp_offset(entry);
1494+
offset = offset +
14811495
((addr & ~PMD_MASK) >> PAGE_SHIFT);
14821496
frame = swp_type(entry) |
14831497
(offset << MAX_SWAPFILES_SHIFT);

include/linux/swapops.h

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@
2323
#define SWP_TYPE_SHIFT (BITS_PER_XA_VALUE - MAX_SWAPFILES_SHIFT)
2424
#define SWP_OFFSET_MASK ((1UL << SWP_TYPE_SHIFT) - 1)
2525

26+
/*
27+
* Definitions only for PFN swap entries (see is_pfn_swap_entry()). To
28+
* store PFN, we only need SWP_PFN_BITS bits. Each of the pfn swap entries
29+
* can use the extra bits to store other information besides PFN.
30+
*/
31+
#ifdef MAX_PHYSMEM_BITS
32+
#define SWP_PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
33+
#else /* MAX_PHYSMEM_BITS */
34+
#define SWP_PFN_BITS (BITS_PER_LONG - PAGE_SHIFT)
35+
#endif /* MAX_PHYSMEM_BITS */
36+
#define SWP_PFN_MASK (BIT(SWP_PFN_BITS) - 1)
37+
38+
static inline bool is_pfn_swap_entry(swp_entry_t entry);
39+
2640
/* Clear all flags but only keep swp_entry_t related information */
2741
static inline pte_t pte_swp_clear_flags(pte_t pte)
2842
{
@@ -64,6 +78,17 @@ static inline pgoff_t swp_offset(swp_entry_t entry)
6478
return entry.val & SWP_OFFSET_MASK;
6579
}
6680

81+
/*
82+
* This should only be called upon a pfn swap entry to get the PFN stored
83+
* in the swap entry. Please refers to is_pfn_swap_entry() for definition
84+
* of pfn swap entry.
85+
*/
86+
static inline unsigned long swp_offset_pfn(swp_entry_t entry)
87+
{
88+
VM_BUG_ON(!is_pfn_swap_entry(entry));
89+
return swp_offset(entry) & SWP_PFN_MASK;
90+
}
91+
6792
/* check whether a pte points to a swap entry */
6893
static inline int is_swap_pte(pte_t pte)
6994
{
@@ -369,7 +394,7 @@ static inline int pte_none_mostly(pte_t pte)
369394

370395
static inline struct page *pfn_swap_entry_to_page(swp_entry_t entry)
371396
{
372-
struct page *p = pfn_to_page(swp_offset(entry));
397+
struct page *p = pfn_to_page(swp_offset_pfn(entry));
373398

374399
/*
375400
* Any use of migration entries may only occur while the
@@ -387,6 +412,9 @@ static inline struct page *pfn_swap_entry_to_page(swp_entry_t entry)
387412
*/
388413
static inline bool is_pfn_swap_entry(swp_entry_t entry)
389414
{
415+
/* Make sure the swp offset can always store the needed fields */
416+
BUILD_BUG_ON(SWP_TYPE_SHIFT < SWP_PFN_BITS);
417+
390418
return is_migration_entry(entry) || is_device_private_entry(entry) ||
391419
is_device_exclusive_entry(entry);
392420
}
@@ -475,11 +503,6 @@ static inline int is_hwpoison_entry(swp_entry_t entry)
475503
return swp_type(entry) == SWP_HWPOISON;
476504
}
477505

478-
static inline unsigned long hwpoison_entry_to_pfn(swp_entry_t entry)
479-
{
480-
return swp_offset(entry);
481-
}
482-
483506
static inline void num_poisoned_pages_inc(void)
484507
{
485508
atomic_long_inc(&num_poisoned_pages);

mm/hmm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
253253
cpu_flags = HMM_PFN_VALID;
254254
if (is_writable_device_private_entry(entry))
255255
cpu_flags |= HMM_PFN_WRITE;
256-
*hmm_pfn = swp_offset(entry) | cpu_flags;
256+
*hmm_pfn = swp_offset_pfn(entry) | cpu_flags;
257257
return 0;
258258
}
259259

mm/memory-failure.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ static int check_hwpoisoned_entry(pte_t pte, unsigned long addr, short shift,
635635
swp_entry_t swp = pte_to_swp_entry(pte);
636636

637637
if (is_hwpoison_entry(swp))
638-
pfn = hwpoison_entry_to_pfn(swp);
638+
pfn = swp_offset_pfn(swp);
639639
}
640640

641641
if (!pfn || pfn != poisoned_pfn)

mm/page_vma_mapped.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw)
8686
!is_device_exclusive_entry(entry))
8787
return false;
8888

89-
pfn = swp_offset(entry);
89+
pfn = swp_offset_pfn(entry);
9090
} else if (is_swap_pte(*pvmw->pte)) {
9191
swp_entry_t entry;
9292

@@ -96,7 +96,7 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw)
9696
!is_device_exclusive_entry(entry))
9797
return false;
9898

99-
pfn = swp_offset(entry);
99+
pfn = swp_offset_pfn(entry);
100100
} else {
101101
if (!pte_present(*pvmw->pte))
102102
return false;
@@ -221,7 +221,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
221221
return not_found(pvmw);
222222
entry = pmd_to_swp_entry(pmde);
223223
if (!is_migration_entry(entry) ||
224-
!check_pmd(swp_offset(entry), pvmw))
224+
!check_pmd(swp_offset_pfn(entry), pvmw))
225225
return not_found(pvmw);
226226
return true;
227227
}

0 commit comments

Comments
 (0)