mm: Use pte marker for swapin errors
Subsystems (4)
cover mm: Use pte marker for swapin errors
Hi,
This series uses the pte marker to replace the swapin error swap entry, then we safe one more swap entry. A new pte marker bit is defined.
One thing worth mentioning: we need the pte marker to be always built to make sure it works like before, so I made an attempt in patch 1 to drop CONFIG_PTE_MARKER and always compile pte marker in. Since pte markers are enabled in many distributions by default already, meanwhile the codeset is really small I assume it's fine. But still I'm tagging with RFC for this initial version.
Smoke tested on anonymous mem on a fake swap failure. Please have a look, thanks.
Peter Xu (2):
mm: Always compile in pte markers
mm: Use pte markers for swap errors
include/linux/swap.h | 16 ++++---------- include/linux/swapops.h | 49 +++++++++-------------------------------- mm/Kconfig | 7 ------ mm/memory.c | 13 ++++++----- mm/shmem.c | 2 +- mm/swapfile.c | 2 +- 6 files changed, 23 insertions(+), 66 deletions(-)
Signature · 1 line
2.37.3
RFC PATCH 1/2 mm: Always compile in pte markers
The PTE markers code is tiny and now it's enabled for most of the distributions. It's fine to keep it as-is, but to make a broader use of it (e.g. replacing read error swap entry) it needs to be there always otherwise we need special code path to take care of !PTE_MARKER case.
It'll be easier just make pte marker always exist. Use this chance to extend its usage to anonymous too by simply touching up some of the old comments, because it'll be used for anonymous pages in the follow up patches.
--- include/linux/swap.h | 10 +++------- include/linux/swapops.h | 31 ------------------------------- mm/Kconfig | 7 ------- mm/memory.c | 7 +++---- 4 files changed, 6 insertions(+), 49 deletions(-)
include/linux/swap.h +3 -7
@@ -60,17 +60,13 @@ static inline int current_is_kswapd(void) SWP_MIGRATION_NUM + SWP_DEVICE_NUM + \ SWP_PTE_MARKER_NUM)/* -* PTE markers are used to persist information onto PTEs that are mapped with -* file-backed memories. As its name "PTE" hints, it should only be applied to -* the leaves of pgtables. +* PTE markers are used to persist information onto PTEs that otherwise +* should be a none pte. As its name "PTE" hints, it should only be +* applied to the leaves of pgtables. */ -#ifdef CONFIG_PTE_MARKER #define SWP_PTE_MARKER_NUM 1#define SWP_PTE_MARKER (MAX_SWAPFILES + SWP_HWPOISON_NUM + \ SWP_MIGRATION_NUM + SWP_DEVICE_NUM) -#else -#define SWP_PTE_MARKER_NUM 0 -#endif /* * Unaddressable device memory support. See include/linux/hmm.h and
include/linux/swapops.h +0 -31
@@ -412,8 +412,6 @@ typedef unsigned long pte_marker;#define PTE_MARKER_UFFD_WP BIT(0)#define PTE_MARKER_MASK (PTE_MARKER_UFFD_WP) -#ifdef CONFIG_PTE_MARKER -static inline swp_entry_t make_pte_marker_entry (pte_marker marker) {return swp_entry (SWP_PTE_MARKER , marker);
@@ -434,32 +432,6 @@ static inline bool is_pte_marker(pte_t pte)return is_swap_pte (pte) &&is_pte_marker_entry (pte_to_swp_entry (pte)); } -#else /* CONFIG_PTE_MARKER */ - -static inline swp_entry_t make_pte_marker_entry (pte_marker marker) -{ -/* This should never be called if !CONFIG_PTE_MARKER */ -WARN_ON_ONCE (1 ); -return swp_entry (0 ,0 ); -} - -static inline bool is_pte_marker_entry (swp_entry_t entry) -{ -return false; -} - -static inline pte_marker pte_marker_get (swp_entry_t entry) -{ -return 0 ; -} - -static inline bool is_pte_marker (pte_t pte) -{ -return false; -} - -#endif /* CONFIG_PTE_MARKER */ -static inline pte_t make_pte_marker (pte_marker marker) {return swp_entry_to_pte (make_pte_marker_entry (marker));
@@ -477,9 +449,6 @@ static inline pte_t make_pte_marker(pte_marker marker)* memory, kernel-only memory (including when the system is during-boot), * non-ram based generic file-system. It's fine to be used even there, but the * extra pte marker check will be pure overhead. -* -* For systems configured with !CONFIG_PTE_MARKER this will be automatically -* optimized to pte_none(). */ static inline int pte_none_mostly (pte_t pte) {
mm/Kconfig +0 -7
@@ -1107,17 +1107,10 @@ config HAVE_ARCH_USERFAULTFD_MINOR help Arch has userfaultfd minor fault support -config PTE_MARKER - bool - - help - Allows to create marker PTEs for file-backed memory. - config PTE_MARKER_UFFD_WP bool "Userfaultfd write protection support for shmem/hugetlbfs" default y depends on HAVE_ARCH_USERFAULTFD_WP - select PTE_MARKER help Allows to create marker PTEs for userfaultfd write protection
mm/memory.c +3 -4
@@ -3699,11 +3699,10 @@ static vm_fault_t handle_pte_marker(struct vm_fault *vmf)unsigned long marker =pte_marker_get (entry);/* -* PTE markers should always be with file-backed memories, and the -* marker should never be empty. If anything weird happened, the best -* thing to do is to kill the process along with its mm. +* PTE markers should never be empty. If anything weird happened, +* the best thing to do is to kill the process along with its mm. */ -if (WARN_ON_ONCE (vma_is_anonymous (vmf->vma) || !marker)) +if (WARN_ON_ONCE (!marker))return VM_FAULT_SIGBUS ;if (pte_marker_entry_uffd_wp (entry))
Signature · 1 line
2.37.3
Re: [PATCH RFC 1/2] mm: Always compile in pte markers
Quoted 11 lines · Peter Xu
The PTE markers code is tiny and now it's enabled for most of the distributions. It's fine to keep it as-is, but to make a broader use of it (e.g. replacing read error swap entry) it needs to be there always otherwise we need special code path to take care of !PTE_MARKER case.
It'll be easier just make pte marker always exist. Use this chance to extend its usage to anonymous too by simply touching up some of the old comments, because it'll be used for anonymous pages in the follow up patches.
Signed-off-by Peter Xu <[email protected]>
Many thanks for your work.
Thanks,
Miaohe Lin
RFC PATCH 2/2 mm: Use pte markers for swap errors
In reply to Peter XuPTE markers are ideal mechanism for things like SWP_SWAPIN_ERROR. Using a whole swap entry type for this purpose can be an overkill, especially if we already have PTE markers. Define a new bit for swapin error and replace it with pte markers. Then we can safely drop SWP_SWAPIN_ERROR and give one device slot back to swap.
We used to have SWP_SWAPIN_ERROR taking the page pfn as part of the swap entry, but it's never used. Neither do I see how it can be useful because normally the swapin failure should not be caused by a bad page but bad swap device. Drop it alongside.
--- include/linux/swap.h | 6 +----- include/linux/swapops.h | 26 ++++++++++++++------------ mm/memory.c | 6 ++++-- mm/shmem.c | 2 +- mm/swapfile.c | 2 +- 5 files changed, 21 insertions(+), 21 deletions(-)
include/linux/swap.h +1 -5
@@ -55,10 +55,6 @@ static inline int current_is_kswapd(void)* actions on faults. */ -#define SWP_SWAPIN_ERROR_NUM 1 -#define SWP_SWAPIN_ERROR (MAX_SWAPFILES + SWP_HWPOISON_NUM + \ - SWP_MIGRATION_NUM + SWP_DEVICE_NUM + \ - SWP_PTE_MARKER_NUM)/* * PTE markers are used to persist information onto PTEs that otherwise * should be a none pte. As its name "PTE" hints, it should only be
@@ -121,7 +117,7 @@ static inline int current_is_kswapd(void)#define MAX_SWAPFILES \ ((1 << MAX_SWAPFILES_SHIFT) - SWP_DEVICE_NUM - \ SWP_MIGRATION_NUM - SWP_HWPOISON_NUM - \ - SWP_PTE_MARKER_NUM - SWP_SWAPIN_ERROR_NUM) + SWP_PTE_MARKER_NUM)/* * Magic header for a swap area. The first part of the union is
include/linux/swapops.h +14 -12
@@ -162,16 +162,6 @@ static inline void *swp_to_radix_entry(swp_entry_t entry)return xa_mk_value (entry.val); } -static inline swp_entry_t make_swapin_error_entry (struct page *page) -{ -return swp_entry (SWP_SWAPIN_ERROR ,page_to_pfn (page)); -} - -static inline int is_swapin_error_entry (swp_entry_t entry) -{ -return swp_type (entry) ==SWP_SWAPIN_ERROR ; -} -#if IS_ENABLED (CONFIG_DEVICE_PRIVATE )static inline swp_entry_t make_readable_device_private_entry (pgoff_t offset) {
@@ -409,8 +399,9 @@ static inline bool is_migration_entry_dirty(swp_entry_t entry)typedef unsigned long pte_marker ; -#define PTE_MARKER_UFFD_WP BIT(0) -#define PTE_MARKER_MASK (PTE_MARKER_UFFD_WP) +#define PTE_MARKER_UFFD_WP BIT(0) +#define PTE_MARKER_SWAP_ERROR BIT(1) +#define PTE_MARKER_MASK (BIT(2) - 1)static inline swp_entry_t make_pte_marker_entry (pte_marker marker) {
@@ -437,6 +428,17 @@ static inline pte_t make_pte_marker(pte_marker marker)return swp_entry_to_pte (make_pte_marker_entry (marker)); } +static inline swp_entry_t make_swapin_error_entry (void ) +{ +return make_pte_marker_entry (PTE_MARKER_SWAP_ERROR ); +} + +static inline int is_swapin_error_entry (swp_entry_t entry) +{ +return is_pte_marker_entry (entry) && + (pte_marker_get (entry) &PTE_MARKER_SWAP_ERROR ); +} +/* * This is a special version to check pte_none() just to cover the case when * the pte is a pte marker. It existed because in many cases the pte marker
mm/memory.c +4 -2
@@ -3705,6 +3705,10 @@ static vm_fault_t handle_pte_marker(struct vm_fault *vmf)if (WARN_ON_ONCE (!marker))return VM_FAULT_SIGBUS ; +/* Higher priority than uffd-wp when data corrupted */ +if (marker &PTE_MARKER_SWAP_ERROR ) +return VM_FAULT_SIGBUS ; +if (pte_marker_entry_uffd_wp (entry))return pte_marker_handle_uffd_wp (vmf);
@@ -3764,8 +3768,6 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)put_page (vmf->page); }else if (is_hwpoison_entry (entry)) { ret =VM_FAULT_HWPOISON ; - }else if (is_swapin_error_entry (entry)) { - ret =VM_FAULT_SIGBUS ; }else if (is_pte_marker_entry (entry)) { ret =handle_pte_marker (vmf); }else {
mm/shmem.c +1 -1
@@ -1682,7 +1682,7 @@ static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index,swp_entry_t swapin_error;void *old; - swapin_error =make_swapin_error_entry (&folio->page); + swapin_error =make_swapin_error_entry (); old =xa_cmpxchg_irq (&mapping->i_pages, index,swp_to_radix_entry (swap),swp_to_radix_entry (swapin_error),0 );
mm/swapfile.c +1 -1
@@ -1781,7 +1781,7 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,pte_t pteval;dec_mm_counter (vma->vm_mm,MM_SWAPENTS ); - pteval =swp_entry_to_pte (make_swapin_error_entry (page)); + pteval =swp_entry_to_pte (make_swapin_error_entry ());set_pte_at (vma->vm_mm, addr, pte, pteval);swap_free (entry); ret =0 ;
Signature · 1 line
2.37.3
Re: [PATCH RFC 2/2] mm: Use pte markers for swap errors
Quoted 10 lines · Peter Xu
PTE markers are ideal mechanism for things like
SWP_SWAPIN_ERROR. Using a whole swap entry type for this purpose can be an overkill, especially if we already have PTE markers. Define a new bit for swapin error and replace it with pte markers. Then we can safely dropSWP_SWAPIN_ERRORand give one device slot back to swap.We used to have
SWP_SWAPIN_ERRORtaking the page pfn as part of the swap entry, but it's never used. Neither do I see how it can be useful because normally the swapin failure should not be caused by a bad page but bad swap device. Drop it alongside.
[...]
include/linux/swapops.h static inline bool is_migration_entry_dirty(swp_entry_t entry) · line 403
Quoted 5 lines
include/linux/swapops.h +2
-2-#define PTE_MARKER_UFFD_WP BIT(0) -#define PTE_MARKER_MASK (PTE_MARKER_UFFD_WP) +#define PTE_MARKER_UFFD_WP BIT(0) +#define PTE_MARKER_SWAP_ERROR BIT(1)
I'd suggest to keep the term SWAPIN. An error happened during swapin, which is why the page is corrupted.
(I remember that we discussed naming details in the original series and SWAPIN was the conclusion)
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH RFC 2/2] mm: Use pte markers for swap errors
Quoted 7 lines · David Hildenbrand
include/linux/swapops.h static inline bool is_migration_entry_dirty(swp_entry_t entry) · line 403
Quoted 4 lines
include/linux/swapops.h +2
-2-#define PTE_MARKER_UFFD_WP BIT(0) -#define PTE_MARKER_MASK (PTE_MARKER_UFFD_WP) +#define PTE_MARKER_UFFD_WP BIT(0) +#define PTE_MARKER_SWAP_ERROR BIT(1)I'd suggest to keep the term SWAPIN. An error happened during swapin, which is why the page is corrupted.
Sure thing.
Signature · 1 line
Peter Xu
Re: [PATCH RFC 2/2] mm: Use pte markers for swap errors
In reply to Peter XuQuoted 6 lines · Peter Xu
PTE markers are ideal mechanism for things like
SWP_SWAPIN_ERROR. Using a whole swap entry type for this purpose can be an overkill, especially if we already have PTE markers. Define a new bit for swapin error and replace it with pte markers. Then we can safely dropSWP_SWAPIN_ERRORand give one device slot back to swap.
Quoted 6 lines
We used to have
SWP_SWAPIN_ERRORtaking the page pfn as part of the swap entry, but it's never used. Neither do I see how it can be useful because normally the swapin failure should not be caused by a bad page but bad swap device. Drop it alongside.Signed-off-by Peter Xu <[email protected]>
Thanks for doing this. I have been waiting this for a "long" time. ;) With changing the name of PTE_MARKER_SWAP_ERROR proposed by David, this patch looks good to me.
Thanks,
Miaohe Lin
Quoted 4 lines · Peter Xu
Hi,
This series uses the pte marker to replace the swapin error swap entry, then we safe one more swap entry. A new pte marker bit is defined.
~~~~ ~~~~~
save? file?It's good to save one swap file. Thanks for your patch! Please feel free to add my
for the whole series.
Best Regards,
Huang, Ying
Quoted 21 lines
One thing worth mentioning: we need the pte marker to be always built to make sure it works like before, so I made an attempt in patch 1 to drop
CONFIG_PTE_MARKERand always compile pte marker in. Since pte markers are enabled in many distributions by default already, meanwhile the codeset is really small I assume it's fine. But still I'm tagging with RFC for this initial version.Smoke tested on anonymous mem on a fake swap failure. Please have a look, thanks.
Peter Xu (2):
mm: Always compile in pte markers
mm: Use pte markers for swap errorsinclude/linux/swap.h | 16 ++++---------- include/linux/swapops.h | 49 +++++++++-------------------------------- mm/Kconfig | 7 ------ mm/memory.c | 13 ++++++----- mm/shmem.c | 2 +- mm/swapfile.c | 2 +- 6 files changed, 23 insertions(+), 66 deletions(-)
Quoted 9 lines · Ying
Quoted 4 lines · Peter Xu
Hi,
This series uses the pte marker to replace the swapin error swap entry, then we safe one more swap entry. A new pte marker bit is defined.
~~~~ ~~~~~ save? file?
Quoted 6 lines
It's good to save one swap file. Thanks for your patch! Please feel free to add my
Reviewed-by "Huang, Ying" <[email protected]>for the whole series.
Thanks for the quick feedback, Ying. I'll wait for a few more days before a repost.
Signature · 1 line
Peter Xu