Skip to contentSkip to conversation

Repair and clean up vm.zone_reclaim_mode sysctl ABI

PATCH In review Dave Hansen 5 participants 15 messages Jul 1, 2020 – Jul 2, 2020

Subsystems (10)
lore
Share & tools
lore
#0 lore raw

v2 cover Repair and clean up vm.zone_reclaim_mode sysctl ABI

A previous cleanup accidentally changed the vm.zone_reclaim_mode ABI.

This series restores the ABI and then reorganizes the code to make the ABI more obvious. Since the single-patch v1[1], I've:

* Restored the RECLAIM_ZONE naming, comment and Documentation now that the implicit checks for it are known.
* Move RECLAIM_* definitions to a uapi header
* Add a node_reclaim_enabled() helper

 Documentation/admin-guide/sysctl/vm.rst |   10 +++++-----
 include/linux/swap.h                    |    7 +++++++
 include/uapi/linux/mempolicy.h          |    7 +++++++
 mm/khugepaged.c                         |    2 +-
 mm/page_alloc.c                         |    2 +-
 mm/vmscan.c                             |    3 ---
 6 files changed, 21 insertions(+), 10 deletions(-)

1. https://lore.kernel.org/linux-mm/[email protected]/

Cc Ben Widawsky <[email protected]>
Cc Alex Shi <[email protected]>
Cc Daniel Wagner <[email protected]>
Cc "Tobin C. Harding" <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Huang Ying <[email protected]>
Cc Dan Williams <[email protected]>
Cc Qian Cai <[email protected]>
Cc Daniel Wagner <[email protected]>
#1 lore raw

PATCH v2 1/3 mm/vmscan: restore zone_reclaim_mode ABI

From: Dave Hansen <[email protected]>

I went to go add a new RECLAIM_* mode for the zone_reclaim_mode sysctl. Like a good kernel developer, I also went to go update the documentation. I noticed that the bits in the documentation didn't match the bits in the #defines.

The VM never explicitly checks the RECLAIM_ZONE bit. The bit is, however implicitly checked when checking 'node_reclaim_mode==0'. The RECLAIM_ZONE #define was removed in a cleanup. That, by itself is fine.

But, when the bit was removed (bit 0) the _other_ bit locations also got changed. That's not OK because the bit values are documented to mean one specific thing and users surely rely on them meaning that one thing and not changing from kernel to kernel. The end result is that if someone had a script that did:

sysctl vm.zone_reclaim_mode=1

That script went from doing nothing to writing out pages during node reclaim after the commit in question. That's not great.

Put the bits back the way they were and add a comment so something like this is a bit harder to do again. Update the documentation to make it clear that the first bit is ignored.

Fixes 648b5cf368e0 ("mm/vmscan: remove unused RECLAIM_OFF/RECLAIM_ZONE")
Cc Ben Widawsky <[email protected]>
Cc Alex Shi <[email protected]>
Cc Daniel Wagner <[email protected]>
Cc "Tobin C. Harding" <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Huang Ying <[email protected]>
Cc Dan Williams <[email protected]>
Cc Qian Cai <[email protected]>
Cc Daniel Wagner <[email protected]>

---

 b/Documentation/admin-guide/sysctl/vm.rst |   10 +++++-----
 b/mm/vmscan.c                             |    9 +++++++--
 2 files changed, 12 insertions(+), 7 deletions(-)
Documentation/admin-guide/sysctl/vm.rst renamed from Documentation/admin-guide/sysctl/vm.rst~mm-vmscan-restore-old-zone_reclaim_mode-abi +5 -5
diff -puN Documentation/admin-guide/sysctl/vm.rst~mm-vmscan-restore-old-zone_reclaim_mode-abi Documentation/admin-guide/sysctl/vm.rst
@@ -948,11 +948,11 @@ that benefit from having their data cach
 left disabled as the caching effect is likely to be more important than
 data locality.
 
-zone_reclaim may be enabled if it's known that the workload is partitioned
-such that each partition fits within a NUMA node and that accessing remote
-memory would cause a measurable performance reduction.  The page allocator
-will then reclaim easily reusable pages (those page cache pages that are
-currently not used) before allocating off node pages.
+Consider enabling one or more zone_reclaim mode bits if it's known that the
+workload is partitioned such that each partition fits within a NUMA node
+and that accessing remote memory would cause a measurable performance
+reduction.  The page allocator will take additional actions before
+allocating off node pages.
 
 Allowing zone reclaim to write out pages stops processes that are
 writing large amounts of data from dirtying pages on other nodes. Zone
mm/vmscan.c renamed from mm/vmscan.c~mm-vmscan-restore-old-zone_reclaim_mode-abi +7 -2
diff -puN mm/vmscan.c~mm-vmscan-restore-old-zone_reclaim_mode-abi mm/vmscan.c
@@ -4090,8 +4090,13 @@ module_init(kswapd_init)
  */
 int node_reclaim_mode __read_mostly;
 
-#define RECLAIM_WRITE (1<<0)	/* Writeout pages during reclaim */
-#define RECLAIM_UNMAP (1<<1)	/* Unmap pages during reclaim */
+/*
+ * These bit locations are exposed in the vm.zone_reclaim_mode sysctl
+ * ABI.  New bits are OK, but existing bits can never change.
+ */
+#define RECLAIM_ZONE  (1<<0)	/* Run shrink_inactive_list on the zone */
+#define RECLAIM_WRITE (1<<1)	/* Writeout pages during reclaim */
+#define RECLAIM_UNMAP (1<<2)	/* Unmap pages during reclaim */
 
 /*
  * Priority for NODE_RECLAIM. This determines the fraction of pages

_

Acked-by on v2 1/3
#2 lore raw

Re: [PATCH 1/3] mm/vmscan: restore zone_reclaim_mode ABI

Quoted 28 lines · Dave Hansen

From: Dave Hansen <[email protected]>

I went to go add a new RECLAIM_* mode for the zone_reclaim_mode sysctl. Like a good kernel developer, I also went to go update the documentation. I noticed that the bits in the documentation didn't match the bits in the #defines.

The VM never explicitly checks the RECLAIM_ZONE bit. The bit is, however implicitly checked when checking 'node_reclaim_mode==0'. The RECLAIM_ZONE #define was removed in a cleanup. That, by itself is fine.

But, when the bit was removed (bit 0) the _other_ bit locations also got changed. That's not OK because the bit values are documented to mean one specific thing and users surely rely on them meaning that one thing and not changing from kernel to kernel. The end result is that if someone had a script that did:

sysctl vm.zone_reclaim_mode=1

That script went from doing nothing to writing out pages during node reclaim after the commit in question. That's not great.

Put the bits back the way they were and add a comment so something like this is a bit harder to do again. Update the documentation to make it clear that the first bit is ignored.

Quoted 13 lines
Fixes 648b5cf368e0 ("mm/vmscan: remove unused RECLAIM_OFF/RECLAIM_ZONE")
Cc Ben Widawsky <[email protected]>
Cc Alex Shi <[email protected]>
Cc Daniel Wagner <[email protected]>
Cc "Tobin C. Harding" <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Huang Ying <[email protected]>
Cc Dan Williams <[email protected]>
Cc Qian Cai <[email protected]>
Cc Daniel Wagner <[email protected]>
#3 lore raw

Re: [PATCH 1/3] mm/vmscan: restore zone_reclaim_mode ABI

In reply to Dave Hansen
Quoted 18 lines · Dave Hansen

From: Dave Hansen <[email protected]>

I went to go add a new RECLAIM_* mode for the zone_reclaim_mode sysctl. Like a good kernel developer, I also went to go update the documentation. I noticed that the bits in the documentation didn't match the bits in the #defines.

The VM never explicitly checks the RECLAIM_ZONE bit. The bit is, however implicitly checked when checking 'node_reclaim_mode==0'. The RECLAIM_ZONE #define was removed in a cleanup. That, by itself is fine.

But, when the bit was removed (bit 0) the _other_ bit locations also got changed. That's not OK because the bit values are documented to mean one specific thing and users surely rely on them meaning that one thing and not changing from kernel to kernel. The end result is that if someone had a script that did:

Quoted 3 lines

sysctl vm.zone_reclaim_mode=1

That script went from doing nothing

Per my understanding, this script would have enabled node reclaim for clean unmapped pages before commit 648b5cf368e0 ("mm/vmscan: remove unused RECLAIM_OFF/RECLAIM_ZONE"). So we should revise the description here?

Quoted 7 lines

to writing out pages during
node reclaim after the commit in question. That's not great.

Put the bits back the way they were and add a comment so something like this is a bit harder to do again. Update the documentation to make it clear that the first bit is ignored.

Best Regards,
Huang, Ying

#4 lore raw

Re: [PATCH 1/3] mm/vmscan: restore zone_reclaim_mode ABI

Quoted 6 lines · Ying
Quoted 6 lines

But, when the bit was removed (bit 0) the _other_ bit locations also got changed. That's not OK because the bit values are documented to mean one specific thing and users surely rely on them meaning that one thing and not changing from kernel to kernel. The end result is that if someone had a script that did:

Quoted 7 lines
Quoted 3 lines

sysctl vm.zone_reclaim_mode=1

That script went from doing nothing

Per my understanding, this script would have enabled node reclaim for clean unmapped pages before commit 648b5cf368e0 ("mm/vmscan: remove unused RECLAIM_OFF/RECLAIM_ZONE"). So we should revise the description here?

Yes, you're right. I updated the patch with the updated understanding about the implicit use of the bit but didn't update the changelog. I'll do that for v3.

#5 lore raw

PATCH v2 2/3 mm/vmscan: move RECLAIM* bits to uapi header

In reply to Dave Hansen

From: Dave Hansen <[email protected]>

It is currently not obvious that the RECLAIM_* bits are part of the uapi since they are defined in vmscan.c. Move them to a uapi header to make it obvious.

This should have no functional impact.

Cc Ben Widawsky <[email protected]>
Cc Alex Shi <[email protected]>
Cc Daniel Wagner <[email protected]>
Cc "Tobin C. Harding" <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Huang Ying <[email protected]>
Cc Dan Williams <[email protected]>
Cc Qian Cai <[email protected]>
Cc Daniel Wagner <[email protected]>

--

Note: This is not cc'd to stable. It does not fix any bugs.
---

 b/include/uapi/linux/mempolicy.h |    7 +++++++
 b/mm/vmscan.c                    |    8 --------
 2 files changed, 7 insertions(+), 8 deletions(-)
include/uapi/linux/mempolicy.h renamed from include/uapi/linux/mempolicy.h~mm-vmscan-move-RECLAIM-bits-to-uapi +7 -0
diff -puN include/uapi/linux/mempolicy.h~mm-vmscan-move-RECLAIM-bits-to-uapi include/uapi/linux/mempolicy.h
@@ -62,5 +62,12 @@ enum {
 #define MPOL_F_MOF	(1 << 3) /* this policy wants migrate on fault */
 #define MPOL_F_MORON	(1 << 4) /* Migrate On protnone Reference On Node */
 
+/*
+ * These bit locations are exposed in the vm.zone_reclaim_mode sysctl
+ * ABI.  New bits are OK, but existing bits can never change.
+ */
+#define RECLAIM_ZONE  (1<<0)	/* Run shrink_inactive_list on the zone */
+#define RECLAIM_WRITE (1<<1)	/* Writeout pages during reclaim */
+#define RECLAIM_UNMAP (1<<2)	/* Unmap pages during reclaim */
 
 #endif /* _UAPI_LINUX_MEMPOLICY_H */
mm/vmscan.c renamed from mm/vmscan.c~mm-vmscan-move-RECLAIM-bits-to-uapi +0 -8
diff -puN mm/vmscan.c~mm-vmscan-move-RECLAIM-bits-to-uapi mm/vmscan.c
@@ -4091,14 +4091,6 @@ module_init(kswapd_init)
 int node_reclaim_mode __read_mostly;
 
 /*
- * These bit locations are exposed in the vm.zone_reclaim_mode sysctl
- * ABI.  New bits are OK, but existing bits can never change.
- */
-#define RECLAIM_ZONE  (1<<0)	/* Run shrink_inactive_list on the zone */
-#define RECLAIM_WRITE (1<<1)	/* Writeout pages during reclaim */
-#define RECLAIM_UNMAP (1<<2)	/* Unmap pages during reclaim */
-
-/*
  * Priority for NODE_RECLAIM. This determines the fraction of pages
  * of a node considered for each zone_reclaim. 4 scans 1/16th of
  * a zone.

_

#6 lore raw

Re: [PATCH 2/3] mm/vmscan: move RECLAIM* bits to uapi header

Quoted 34 lines · Dave Hansen

From: Dave Hansen <[email protected]>

It is currently not obvious that the RECLAIM_* bits are part of the uapi since they are defined in vmscan.c. Move them to a uapi header to make it obvious.

This should have no functional impact.

Cc Ben Widawsky <[email protected]>
Cc Alex Shi <[email protected]>
Cc Daniel Wagner <[email protected]>
Cc "Tobin C. Harding" <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Huang Ying <[email protected]>
Cc Dan Williams <[email protected]>
Cc Qian Cai <[email protected]>
Cc Daniel Wagner <[email protected]>

--

Note: This is not cc'd to stable. It does not fix any bugs.
---

 b/include/uapi/linux/mempolicy.h |    7 +++++++
 b/mm/vmscan.c                    |    8 --------
 2 files changed, 7 insertions(+), 8 deletions(-)

diff -puN include/uapi/linux/mempolicy.h~mm-vmscan-move-RECLAIM-bits-to-uapi include/uapi/linux/mempolicy.h

include/uapi/linux/mempolicy.h enum { · line 71

Quoted 10 lines
include/uapi/linux/mempolicy.h renamed from include/uapi/linux/mempolicy.h~mm-vmscan-move-RECLAIM-bits-to-uapi +7 -0
@@ -62,5 +62,12 @@ enum {
 #define MPOL_F_MOF	(1 << 3) /* this policy wants migrate on fault */
 #define MPOL_F_MORON	(1 << 4) /* Migrate On protnone Reference On Node */
 
+/*
+ * These bit locations are exposed in the vm.zone_reclaim_mode sysctl
+ * ABI.  New bits are OK, but existing bits can never change.
+ */
+#define RECLAIM_ZONE  (1<<0)	/* Run shrink_inactive_list on the zone */
+#define RECLAIM_WRITE (1<<1)	/* Writeout pages during reclaim */
+#define RECLAIM_UNMAP (1<<2)	/* Unmap pages during reclaim */

Have you considered turning this into an enum while moving it?

Quoted 21 lines
include/uapi/linux/mempolicy.h renamed from include/uapi/linux/mempolicy.h~mm-vmscan-move-RECLAIM-bits-to-uapi +0 -0
 #endif /* _UAPI_LINUX_MEMPOLICY_H */

diff -puN mm/vmscan.c~mm-vmscan-move-RECLAIM-bits-to-uapi mm/vmscan.c

mm/vmscan.c renamed from mm/vmscan.c~mm-vmscan-move-RECLAIM-bits-to-uapi +0 -8
@@ -4091,14 +4091,6 @@ module_init(kswapd_init)
 int node_reclaim_mode __read_mostly;
 
 /*
- * These bit locations are exposed in the vm.zone_reclaim_mode sysctl
- * ABI.  New bits are OK, but existing bits can never change.
- */
-#define RECLAIM_ZONE  (1<<0)	/* Run shrink_inactive_list on the zone */
-#define RECLAIM_WRITE (1<<1)	/* Writeout pages during reclaim */
-#define RECLAIM_UNMAP (1<<2)	/* Unmap pages during reclaim */
-
-/*
  * Priority for NODE_RECLAIM. This determines the fraction of pages
  * of a node considered for each zone_reclaim. 4 scans 1/16th of
  * a zone.

_

#7 lore raw

Re: [PATCH 2/3] mm/vmscan: move RECLAIM* bits to uapi header

Quoted 8 lines · Ben Widawsky

include/uapi/linux/mempolicy.h enum { · line 71

Quoted 7 lines
include/uapi/linux/mempolicy.h renamed from include/uapi/linux/mempolicy.h~mm-vmscan-move-RECLAIM-bits-to-uapi +7 -0
+/*
+ * These bit locations are exposed in the vm.zone_reclaim_mode sysctl
+ * ABI.  New bits are OK, but existing bits can never change.
+ */
+#define RECLAIM_ZONE  (1<<0)	/* Run shrink_inactive_list on the zone */
+#define RECLAIM_WRITE (1<<1)	/* Writeout pages during reclaim */
+#define RECLAIM_UNMAP (1<<2)	/* Unmap pages during reclaim */

Have you considered turning this into an enum while moving it?

The thought occurred to me, but all of the other bits in the uapi file were defined this way. I decided to not not attempt to buck the trend in their new home.

Acked-by on v2 2/3
#8 lore raw

Re: [PATCH 2/3] mm/vmscan: move RECLAIM* bits to uapi header

In reply to Dave Hansen
Quoted 9 lines · Dave Hansen

From: Dave Hansen <[email protected]>

It is currently not obvious that the RECLAIM_* bits are part of the uapi since they are defined in vmscan.c. Move them to a uapi header to make it obvious.

This should have no functional impact.

Quoted 11 lines
Cc Ben Widawsky <[email protected]>
Cc Alex Shi <[email protected]>
Cc Daniel Wagner <[email protected]>
Cc "Tobin C. Harding" <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Huang Ying <[email protected]>
Cc Dan Williams <[email protected]>
Cc Qian Cai <[email protected]>
Cc Daniel Wagner <[email protected]>
#9 lore raw

PATCH v2 3/3 mm/vmscan: replace implicit RECLAIM_ZONE checks with explicit checks

In reply to Dave Hansen

From: Dave Hansen <[email protected]>

RECLAIM_ZONE was assumed to be unused because it was never explicitly used in the kernel. However, there were a number of places where it was checked implicitly by checking 'node_reclaim_mode' for a zero value.

These zero checks are not great because it is not obvious what a zero mode *means* in the code. Replace them with a helper which makes it more obvious: node_reclaim_enabled().

This helper also provides a handy place to explicitly check the RECLAIM_ZONE bit itself. Check it explicitly there to make it more obvious where the bit can affect behavior.

This should have no functional impact.

Cc Ben Widawsky <[email protected]>
Cc Alex Shi <[email protected]>
Cc Daniel Wagner <[email protected]>
Cc "Tobin C. Harding" <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Huang Ying <[email protected]>
Cc Dan Williams <[email protected]>
Cc Qian Cai <[email protected]>
Cc Daniel Wagner <[email protected]>

--

Note: This is not cc'd to stable. It does not fix any bugs.
---

 b/include/linux/swap.h |    7 +++++++
 b/mm/khugepaged.c      |    2 +-
 b/mm/page_alloc.c      |    2 +-
 3 files changed, 9 insertions(+), 2 deletions(-)
include/linux/swap.h renamed from include/linux/swap.h~mm-vmscan-node_reclaim_mode_helper +7 -0
diff -puN include/linux/swap.h~mm-vmscan-node_reclaim_mode_helper include/linux/swap.h
@@ -12,6 +12,7 @@
 #include <linux/fs.h>
 #include <linux/atomic.h>
 #include <linux/page-flags.h>
+#include <uapi/linux/mempolicy.h>
 #include <asm/page.h>
 
 struct notifier_block;
@@ -374,6 +375,12 @@ extern int sysctl_min_slab_ratio;
 #define node_reclaim_mode 0
 #endif
 
+static inline bool node_reclaim_enabled(void)
+{
+	/* Is any node_reclaim_mode bit set? */
+	return node_reclaim_mode & (RECLAIM_ZONE|RECLAIM_WRITE|RECLAIM_UNMAP);
+}
+
 extern void check_move_unevictable_pages(struct pagevec *pvec);
 
 extern int kswapd_run(int nid);
mm/khugepaged.c renamed from mm/khugepaged.c~mm-vmscan-node_reclaim_mode_helper +1 -1
diff -puN mm/khugepaged.c~mm-vmscan-node_reclaim_mode_helper mm/khugepaged.c
@@ -709,7 +709,7 @@ static bool khugepaged_scan_abort(int ni
 	 * If node_reclaim_mode is disabled, then no extra effort is made to
 	 * allocate memory locally.
 	 */
-	if (!node_reclaim_mode)
+	if (!node_reclaim_enabled())
 		return false;
 
 	/* If there is a count for this node already, it must be acceptable */
mm/page_alloc.c renamed from mm/page_alloc.c~mm-vmscan-node_reclaim_mode_helper +1 -1
diff -puN mm/page_alloc.c~mm-vmscan-node_reclaim_mode_helper mm/page_alloc.c
@@ -3733,7 +3733,7 @@ retry:
 			if (alloc_flags & ALLOC_NO_WATERMARKS)
 				goto try_this_zone;
 
-			if (node_reclaim_mode == 0 ||
+			if (!node_reclaim_enabled() ||
 			    !zone_allows_reclaim(ac->preferred_zoneref->zone, zone))
 				continue;
 

_

#10 lore raw

Re: [PATCH 3/3] mm/vmscan: replace implicit RECLAIM_ZONE checks with explicit checks

Quoted 14 lines · Dave Hansen

diff -puN include/linux/swap.h~mm-vmscan-node_reclaim_mode_helper include/linux/swap.h

include/linux/swap.h renamed from include/linux/swap.h~mm-vmscan-node_reclaim_mode_helper +1 -0
@@ -12,6 +12,7 @@
 #include <linux/fs.h>
 #include <linux/atomic.h>
 #include <linux/page-flags.h>
+#include <uapi/linux/mempolicy.h>
 #include <asm/page.h>
 
 struct notifier_block;
@@ -374,6 +375,12 @@ extern int sysctl_min_slab_ratio;
 #define node_reclaim_mode 0
 #endif

include/linux/swap.h extern int sysctl_min_slab_ratio; · line 386

Quoted 10 lines
include/linux/swap.h renamed from include/linux/swap.h~mm-vmscan-node_reclaim_mode_helper +6 -0
 
+static inline bool node_reclaim_enabled(void)
+{
+	/* Is any node_reclaim_mode bit set? */
+	return node_reclaim_mode & (RECLAIM_ZONE|RECLAIM_WRITE|RECLAIM_UNMAP);
+}
+
 extern void check_move_unevictable_pages(struct pagevec *pvec);
 
 extern int kswapd_run(int nid);

If a user writes a bit that isn't a RECLAIM_* bit to vm.zone_reclaim_mode today, it acts as though RECLAIM_ZONE is enabled: we try to reclaim in zonelist order before falling back to the next zone in the page allocator. The sysctl doesn't enforce any max value :/ I dont know if there is any such user, but this would break them if there is.

Should this simply be return !!node_reclaim_mode?

#11 lore raw

Re: [PATCH 3/3] mm/vmscan: replace implicit RECLAIM_ZONE checks with explicit checks

Quoted 33 lines · David Rientjes
Quoted 14 lines · Dave Hansen

diff -puN include/linux/swap.h~mm-vmscan-node_reclaim_mode_helper include/linux/swap.h

include/linux/swap.h renamed from include/linux/swap.h~mm-vmscan-node_reclaim_mode_helper +1 -0
@@ -12,6 +12,7 @@
 #include <linux/fs.h>
 #include <linux/atomic.h>
 #include <linux/page-flags.h>
+#include <uapi/linux/mempolicy.h>
 #include <asm/page.h>
 
 struct notifier_block;
@@ -374,6 +375,12 @@ extern int sysctl_min_slab_ratio;
 #define node_reclaim_mode 0
 #endif

include/linux/swap.h extern int sysctl_min_slab_ratio; · line 386

Quoted 10 lines
include/linux/swap.h renamed from include/linux/swap.h~mm-vmscan-node_reclaim_mode_helper +6 -0
 
+static inline bool node_reclaim_enabled(void)
+{
+	/* Is any node_reclaim_mode bit set? */
+	return node_reclaim_mode & (RECLAIM_ZONE|RECLAIM_WRITE|RECLAIM_UNMAP);
+}
+
 extern void check_move_unevictable_pages(struct pagevec *pvec);
 
 extern int kswapd_run(int nid);

If a user writes a bit that isn't a RECLAIM_* bit to vm.zone_reclaim_mode today, it acts as though RECLAIM_ZONE is enabled: we try to reclaim in zonelist order before falling back to the next zone in the page allocator. The sysctl doesn't enforce any max value :/ I dont know if there is any such user, but this would break them if there is.

Quoted 2 lines

Should this simply be return !!node_reclaim_mode?

I don't think so because I don't think anything else validates the unused bits remain unused.

#12 lore raw

Re: [PATCH 3/3] mm/vmscan: replace implicit RECLAIM_ZONE checks with explicit checks

Quoted 8 lines · Ben Widawsky
Quoted 8 lines
include/linux/swap.h renamed from include/linux/swap.h~mm-vmscan-node_reclaim_mode_helper +6 -0
+static inline bool node_reclaim_enabled(void)
+{
+	/* Is any node_reclaim_mode bit set? */
+	return node_reclaim_mode & (RECLAIM_ZONE|RECLAIM_WRITE|RECLAIM_UNMAP);
+}
+
 extern void check_move_unevictable_pages(struct pagevec *pvec);
Quoted 8 lines
Quoted 1 line

extern int kswapd_run(int nid);

If a user writes a bit that isn't a RECLAIM_* bit to vm.zone_reclaim_mode today, it acts as though RECLAIM_ZONE is enabled: we try to reclaim in zonelist order before falling back to the next zone in the page allocator. The sysctl doesn't enforce any max value :/ I dont know if there is any such user, but this would break them if there is.

Should this simply be return !!node_reclaim_mode?

You're right that there _could_ be a user-visible behavior change here.
But, if there were a change it would be for a bit which wasn't even
mentioned in the documentation. Somebody would have had to look at the doc mentioning 1,2,4 and written an 8. If they did that, they're asking for trouble because we could have defined the '8' bit to do nasty things like auto-demote all your memory. :)

I'll mention it in the changelog, but I still think we should check the actual, known bits rather than check for 0.

BTW, in the hardware, they almost invariably make unused bits "reserved" and do mean things like #GP if someone tries to set them. This is a case where the kernel probably should have done the same. It would have saved us the trouble of asking these questions now. Maybe we should even do that going forward.

#13 lore raw

Re: [PATCH 3/3] mm/vmscan: replace implicit RECLAIM_ZONE checks with explicit checks

Quoted 25 lines · Dave Hansen
Quoted 8 lines · Ben Widawsky
Quoted 8 lines
include/linux/swap.h renamed from include/linux/swap.h~mm-vmscan-node_reclaim_mode_helper +6 -0
+static inline bool node_reclaim_enabled(void)
+{
+	/* Is any node_reclaim_mode bit set? */
+	return node_reclaim_mode & (RECLAIM_ZONE|RECLAIM_WRITE|RECLAIM_UNMAP);
+}
+
 extern void check_move_unevictable_pages(struct pagevec *pvec);
Quoted 8 lines
Quoted 1 line

extern int kswapd_run(int nid);

If a user writes a bit that isn't a RECLAIM_* bit to vm.zone_reclaim_mode today, it acts as though RECLAIM_ZONE is enabled: we try to reclaim in zonelist order before falling back to the next zone in the page allocator. The sysctl doesn't enforce any max value :/ I dont know if there is any such user, but this would break them if there is.

Should this simply be return !!node_reclaim_mode?

You're right that there _could_ be a user-visible behavior change here.
But, if there were a change it would be for a bit which wasn't even
mentioned in the documentation. Somebody would have had to look at the doc mentioning 1,2,4 and written an 8. If they did that, they're asking for trouble because we could have defined the '8' bit to do nasty things like auto-demote all your memory. :)

Quoted 9 lines

I'll mention it in the changelog, but I still think we should check the actual, known bits rather than check for 0.

BTW, in the hardware, they almost invariably make unused bits "reserved" and do mean things like #GP if someone tries to set them. This is a case where the kernel probably should have done the same. It would have saved us the trouble of asking these questions now. Maybe we should even do that going forward.

Maybe enforce it in a sysctl handler so the user catches any errors, which would be better than silently accepting some policy that doesn't exist?

RECLAIM_UNMAP and/or RECLAIM_WRITE should likely get -EINVAL if attempted to be set without RECLAIM_ZONE as well: they are no-ops without RECLAIM_ZONE. This would likely have caught something wrong with commit 648b5cf368e0 ("mm/vmscan: remove unused RECLAIM_OFF/RECLAIM_ZONE") if it would have already been in place.

I don't feel strongly about this, so feel free to ignore.

Reviewed-by on the v2 series
#14 lore raw
In reply to Dave Hansen
Quoted 20 lines · Dave Hansen

A previous cleanup accidentally changed the vm.zone_reclaim_mode ABI.

This series restores the ABI and then reorganizes the code to make the ABI more obvious. Since the single-patch v1[1], I've:

* Restored the RECLAIM_ZONE naming, comment and Documentation now that the implicit checks for it are known.
* Move RECLAIM_* definitions to a uapi header
* Add a node_reclaim_enabled() helper

 Documentation/admin-guide/sysctl/vm.rst |   10 +++++-----
 include/linux/swap.h                    |    7 +++++++
 include/uapi/linux/mempolicy.h          |    7 +++++++
 mm/khugepaged.c                         |    2 +-
 mm/page_alloc.c                         |    2 +-
 mm/vmscan.c                             |    3 ---
 6 files changed, 21 insertions(+), 10 deletions(-)

1. https://lore.kernel.org/linux-mm/[email protected]/

Quoted 10 lines
Cc Ben Widawsky <[email protected]>
Cc Alex Shi <[email protected]>
Cc Daniel Wagner <[email protected]>
Cc "Tobin C. Harding" <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Huang Ying <[email protected]>
Cc Dan Williams <[email protected]>
Cc Qian Cai <[email protected]>
Cc Daniel Wagner <[email protected]>

Series is:

I was more thorough this time in checking all uses of node_reclaim_mode :-). I do think in patch 2/3, using an enum would be a little better, which I've mentioned there.