linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: Zhongkun He <hezhongkun.hzk@bytedance.com>
Cc: hannes@cmpxchg.org, mhocko@suse.com, yosry.ahmed@linux.dev,
	muchun.song@linux.dev, yuzhao@google.com, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH V3 2/3] mm: add max swappiness arg to lru_gen for anonymous memory only
Date: Wed, 9 Apr 2025 19:09:38 -0700	[thread overview]
Message-ID: <20250409190938.f6befeeb9e86ad72f46503a5@linux-foundation.org> (raw)
In-Reply-To: <b3af3747daefa00177b48f4666780da58177f7c0.1744169302.git.hezhongkun.hzk@bytedance.com>

On Wed,  9 Apr 2025 15:06:19 +0800 Zhongkun He <hezhongkun.hzk@bytedance.com> wrote:

> The MGLRU

paging yuzhao?

> already supports reclaiming only from anonymous memory
> via the /sys/kernel/debug/lru_gen interface. Now, memory.reclaim
> also supports the swappiness=max parameter to enable reclaiming
> solely from anonymous memory. To unify the semantics of proactive
> reclaiming from anonymous folios, the max parameter is introduced.
> 
> Additionally, the use of SWAPPINESS_ANON_ONLY in place of
> 'MAX_SWAPPINESS + 1' improves code clarity and makes the intention
> more explicit.
> 
> --- a/mm/vmscan.c
> +++ b/mm/vmscan.c
> @@ -2697,8 +2697,11 @@ static bool should_clear_pmd_young(void)
>  		READ_ONCE((lruvec)->lrugen.min_seq[LRU_GEN_FILE]),	\
>  	}
>  
> +#define max_evictable_type(swappiness)						\
> +	((swappiness) != SWAPPINESS_ANON_ONLY)
> +
>  #define evictable_min_seq(min_seq, swappiness)				\
> -	min((min_seq)[!(swappiness)], (min_seq)[(swappiness) <= MAX_SWAPPINESS])
> +	min((min_seq)[!(swappiness)], (min_seq)[max_evictable_type(swappiness)])

Why oh why did we implement these in cpp?

>  
> @@ -3857,7 +3860,7 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness)
>  	int hist = lru_hist_from_seq(lrugen->min_seq[type]);
>  	int new_gen, old_gen = lru_gen_from_seq(lrugen->min_seq[type]);
>  
> -	if (type ? swappiness > MAX_SWAPPINESS : !swappiness)
> +	if (type ? (swappiness == SWAPPINESS_ANON_ONLY) : !swappiness)

This expression makes my brain bleed.

	if (type) {
		if (swappiness == SWAPPINESS_ANON_ONLY) {
			/*
			 * Nice comment explaining why we're doing this
			 */
			goto done;;
		}
	} else {
		if (!swappiness) {
			/*
			 * Nice comment explaining why we're doing this
			 */
			goto done;
		}
	}

or

	if (type && (swappiness == SWAPPINESS_ANON_ONLY)) {
		/*
		 * Nice comment explaining why we're doing this
		 */
		goto done;
	}

	if (!type && !swappiness) {
		/*
		 * Nice comment explaining why we're doing this
		 */
		goto done;
	}

It's much more verbose, but it has the huge advantage that it creates
locations where we can add comments which tell readers what's going on.
Which is pretty important, no?
	
>  		goto done;
>  
>  	/* prevent cold/hot inversion if the type is evictable */
> @@ -5523,7 +5526,7 @@ static int run_cmd(char cmd, int memcg_id, int nid, unsigned long seq,
>  
>  	if (swappiness < MIN_SWAPPINESS)
>  		swappiness = get_swappiness(lruvec, sc);
> -	else if (swappiness > MAX_SWAPPINESS + 1)
> +	else if (swappiness > SWAPPINESS_ANON_ONLY)
>  		goto done;
>  
>  	switch (cmd) {
> @@ -5580,7 +5583,7 @@ static ssize_t lru_gen_seq_write(struct file *file, const char __user *src,
>  	while ((cur = strsep(&next, ",;\n"))) {
>  		int n;
>  		int end;
> -		char cmd;
> +		char cmd, swap_string[5];
>  		unsigned int memcg_id;
>  		unsigned int nid;
>  		unsigned long seq;
> @@ -5591,13 +5594,22 @@ static ssize_t lru_gen_seq_write(struct file *file, const char __user *src,
>  		if (!*cur)
>  			continue;
>  
> -		n = sscanf(cur, "%c %u %u %lu %n %u %n %lu %n", &cmd, &memcg_id, &nid,
> -			   &seq, &end, &swappiness, &end, &opt, &end);
> +		n = sscanf(cur, "%c %u %u %lu %n %4s %n %lu %n", &cmd, &memcg_id, &nid,
> +			   &seq, &end, swap_string, &end, &opt, &end);

Permits userspace to easily overrun swap_string[].  OK, it's root-only,
but still, why permit this?

>  		if (n < 4 || cur[end]) {
>  			err = -EINVAL;
>  			break;
>  		}
>  
> +		/* set by userspace for anonymous memory only */
> +		if (!strncmp("max", swap_string, sizeof("max"))) {

Can sscanf() give us a non null-terminated string?

> +			swappiness = SWAPPINESS_ANON_ONLY;
> +		} else {
> +			err = kstrtouint(swap_string, 0, &swappiness);
> +			if (err)
> +				break;
> +		}
> +
>  		err = run_cmd(cmd, memcg_id, nid, seq, &sc, swappiness, opt);
>  		if (err)
>  			break;



  reply	other threads:[~2025-04-10  2:09 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-09  7:06 [PATCH V3 0/3] add max arg to swappiness in memory.reclaim and lru_gen Zhongkun He
2025-04-09  7:06 ` [PATCH V3 1/3] mm: add swappiness=max arg to memory.reclaim for only anon reclaim Zhongkun He
2025-04-10  2:09   ` Andrew Morton
2025-04-10  3:48     ` [External] " Zhongkun He
2025-04-09  7:06 ` [PATCH V3 2/3] mm: add max swappiness arg to lru_gen for anonymous memory only Zhongkun He
2025-04-10  2:09   ` Andrew Morton [this message]
2025-04-10  4:50     ` [External] " Zhongkun He
2025-04-30  7:59   ` Dan Carpenter
2025-05-01  1:56     ` [External] " Zhongkun He
2025-05-02  6:58       ` Dan Carpenter
2025-05-07  3:27         ` Zhongkun He
2025-04-09  7:06 ` [PATCH V3 3/3] mm: vmscan: add more comments about cache_trim_mode Zhongkun He

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250409190938.f6befeeb9e86ad72f46503a5@linux-foundation.org \
    --to=akpm@linux-foundation.org \
    --cc=hannes@cmpxchg.org \
    --cc=hezhongkun.hzk@bytedance.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=yosry.ahmed@linux.dev \
    --cc=yuzhao@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox