2023-04-25 16:00:25

by Wen Yang

[permalink] [raw]
Subject: [PATCH v2] mm: compaction: optimize compact_memory to comply with the admin-guide

From: Wen Yang <[email protected]>

For the /proc/sys/vm/compact_memory file, the admin-guide states:
When 1 is written to the file, all zones are compacted such that free
memory is available in contiguous blocks where possible. This can be
important for example in the allocation of huge pages although processes
will also directly compact memory as required

But it was not strictly followed, writing any value would cause all
zones to be compacted.
It has been slightly optimized to comply with the admin-guide.
Enforce the 1 on the unlikely chance that the sysctl handler is ever
extended to do something different.

Commit ef4984384172 ("mm/compaction: remove unused variable sysctl_compact_memory")
has also been optimized a bit here, as the declaration in the external header
file has been eliminated, and sysctl_compact_memory also needs to be verified.

Signed-off-by: Wen Yang <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Mel Gorman <[email protected]>
Cc: Oscar Salvador <[email protected]>
Cc: William Lam <[email protected]>
Cc: Pintu Kumar <[email protected]>
Cc: Fu Wei <[email protected]>
Cc: [email protected]
Cc: [email protected]
---
mm/compaction.c | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/mm/compaction.c b/mm/compaction.c
index c8bcdea15f5f..ba57d4178866 100644
--- a/mm/compaction.c
+++ b/mm/compaction.c
@@ -1736,6 +1736,7 @@ static int sysctl_compact_unevictable_allowed __read_mostly = CONFIG_COMPACT_UNE
*/
static unsigned int __read_mostly sysctl_compaction_proactiveness = 20;
static int sysctl_extfrag_threshold = 500;
+static int sysctl_compact_memory;

static inline void
update_fast_start_pfn(struct compact_control *cc, unsigned long pfn)
@@ -2780,6 +2781,15 @@ static int compaction_proactiveness_sysctl_handler(struct ctl_table *table, int
static int sysctl_compaction_handler(struct ctl_table *table, int write,
void *buffer, size_t *length, loff_t *ppos)
{
+ int ret;
+
+ ret = proc_dointvec(table, write, buffer, length, ppos);
+ if (ret)
+ return ret;
+
+ if (sysctl_compact_memory != 1)
+ return -EINVAL;
+
if (write)
compact_nodes();

@@ -3095,7 +3105,7 @@ static int proc_dointvec_minmax_warn_RT_change(struct ctl_table *table,
static struct ctl_table vm_compaction[] = {
{
.procname = "compact_memory",
- .data = NULL,
+ .data = &sysctl_compact_memory,
.maxlen = sizeof(int),
.mode = 0200,
.proc_handler = sysctl_compaction_handler,
--
2.37.2


2023-04-28 11:02:02

by Mel Gorman

[permalink] [raw]
Subject: Re: [PATCH v2] mm: compaction: optimize compact_memory to comply with the admin-guide

On Tue, Apr 25, 2023 at 11:52:35PM +0800, [email protected] wrote:
> From: Wen Yang <[email protected]>
>
> For the /proc/sys/vm/compact_memory file, the admin-guide states:
> When 1 is written to the file, all zones are compacted such that free
> memory is available in contiguous blocks where possible. This can be
> important for example in the allocation of huge pages although processes
> will also directly compact memory as required
>
> But it was not strictly followed, writing any value would cause all
> zones to be compacted.
> It has been slightly optimized to comply with the admin-guide.
> Enforce the 1 on the unlikely chance that the sysctl handler is ever
> extended to do something different.
>
> Commit ef4984384172 ("mm/compaction: remove unused variable sysctl_compact_memory")
> has also been optimized a bit here, as the declaration in the external header
> file has been eliminated, and sysctl_compact_memory also needs to be verified.
>
> Signed-off-by: Wen Yang <[email protected]>
> Cc: Andrew Morton <[email protected]>
> Cc: Mel Gorman <[email protected]>
> Cc: Oscar Salvador <[email protected]>
> Cc: William Lam <[email protected]>
> Cc: Pintu Kumar <[email protected]>
> Cc: Fu Wei <[email protected]>
> Cc: [email protected]
> Cc: [email protected]
> ---
> mm/compaction.c | 12 +++++++++++-
> 1 file changed, 11 insertions(+), 1 deletion(-)
>
> diff --git a/mm/compaction.c b/mm/compaction.c
> index c8bcdea15f5f..ba57d4178866 100644
> --- a/mm/compaction.c
> +++ b/mm/compaction.c
> @@ -1736,6 +1736,7 @@ static int sysctl_compact_unevictable_allowed __read_mostly = CONFIG_COMPACT_UNE
> */
> static unsigned int __read_mostly sysctl_compaction_proactiveness = 20;
> static int sysctl_extfrag_threshold = 500;
> +static int sysctl_compact_memory;
>

__read_mostly but that aside, it's only used in
sysctl_compaction_handler so could also be declared as static within
that function. That way if CONFIG_SYSCTL is not set, it should be
guaranteed that the compiler does not save storage for it.

It's minor enough that With or without another version;

Acked-by: Mel Gorman <[email protected]>

--
Mel Gorman
SUSE Labs

2023-05-02 21:04:02

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH v2] mm: compaction: optimize compact_memory to comply with the admin-guide

On Fri, 28 Apr 2023 11:58:48 +0100 Mel Gorman <[email protected]> wrote:

> > --- a/mm/compaction.c
> > +++ b/mm/compaction.c
> > @@ -1736,6 +1736,7 @@ static int sysctl_compact_unevictable_allowed __read_mostly = CONFIG_COMPACT_UNE
> > */
> > static unsigned int __read_mostly sysctl_compaction_proactiveness = 20;
> > static int sysctl_extfrag_threshold = 500;
> > +static int sysctl_compact_memory;
> >
>
> __read_mostly but that aside, it's only used in
> sysctl_compaction_handler so could also be declared as static within
> that function. That way if CONFIG_SYSCTL is not set, it should be
> guaranteed that the compiler does not save storage for it.

but

static struct ctl_table vm_compaction[] = {
{
.procname = "compact_memory",
.data = &sysctl_compact_memory,
.maxlen = sizeof(int),
.mode = 0200,
.proc_handler = sysctl_compaction_handler,
},


I'll add the __read_mostly, thanks.