From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 62264C3601E for ; Thu, 10 Apr 2025 04:51:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5C74D2800C3; Thu, 10 Apr 2025 00:51:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5773F2800C0; Thu, 10 Apr 2025 00:51:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 43E8C2800C3; Thu, 10 Apr 2025 00:51:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 26CC52800C0 for ; Thu, 10 Apr 2025 00:51:27 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id BF052AFB96 for ; Thu, 10 Apr 2025 04:51:27 +0000 (UTC) X-FDA: 83316910614.14.625E95A Received: from mail-lf1-f41.google.com (mail-lf1-f41.google.com [209.85.167.41]) by imf15.hostedemail.com (Postfix) with ESMTP id 39DD2A0005 for ; Thu, 10 Apr 2025 04:51:24 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=JpZcjf8h; spf=pass (imf15.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.167.41 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1744260685; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NCICr8Y1TyleAjVDsOU8scGztgtriadImesn1Q8G2jE=; b=lF6+RwEiKFTxy2NwA0BhjgwTTn29AWTABMxFx0m3T0K9cfqm0pXrW8VD2RSY9r1K2zoUcG xkQNm2cdJoH9o0USX9Spw4EO15kf7IGolUwDV5la/V7Myo9ILnldb8s9UmW/5TfLtrSEzH 40JryCGgUzo8J/GruRewpWo7nlt8C4E= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1744260685; a=rsa-sha256; cv=none; b=0DY+J4h4ZP0mL11dsKrsmnuIkx83EvmXVeUXqxH1A2zA1cvYwZXxH0v2HeH9maC63iGhnv ioMb7STI+CnrErlgJ+XwBnjY7ah0IpVyH8ApLiOsFsZVIRdhtbnZ/Fd08w/waC+2IgjijG xwXYCVU9Etv5NZPHlEplGBXIR9GUMRE= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=JpZcjf8h; spf=pass (imf15.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.167.41 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com Received: by mail-lf1-f41.google.com with SMTP id 2adb3069b0e04-54addb5a139so433255e87.0 for ; Wed, 09 Apr 2025 21:51:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1744260683; x=1744865483; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=NCICr8Y1TyleAjVDsOU8scGztgtriadImesn1Q8G2jE=; b=JpZcjf8hZm7xqEt/JRrC3xEx6CIOiF7DMs3nINykrg6FXH5HIynzUVGZ6gf6veRx02 xweutvBFDP6+lvmYiCredqvL8Zbx3DKnpxbRYHgmkP+rQbo3PPxG650XzkgJLU/BYlIF CVBG8XG62rHdwq03s4aqRrNq56k8QFCPcfG1UJsT/bcCukAvFSRZaNDix/mL7dlStt0d sF99sslOsNYlei2fopue7gDkBmv1Rd/2lUS4gkeGqi3DT8bIIoQbI46PjkgjGZ2hCyYh rTFkgG+0FBktoDT0O56QKFhQEoPG2eJFOFcv7Co1lw/DBq3vqAHUvt1bU+dqCxvy/bWL 205w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744260683; x=1744865483; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NCICr8Y1TyleAjVDsOU8scGztgtriadImesn1Q8G2jE=; b=L9W+gSN04IQ0Hb/OihZhnmB1pasZGNzeNAaBteSoZLwAmyAiM+tGuQWJ0/+o5iGwOI 7QoKIVMN9UBQFKwbk7rTTtcFIQIBZE67G7Y0pmOEgGMX2aLnv8XIBiOunyOn5ugPYALz 2w2BOLrdwc0OVLrEwdcV4rJpYU6Rm6E2/zn721TQmJCiTJAd9UYsToAq+AEW3QLiRQp9 MJkiQ1msUQijA8sFI5FOO4HtKcusq/z7CUDiqKq4G3VJ2pGJJwnQIkVu7L0RLu+Gl+LM s4ijm4WT/o7u6BO5qVjXb9LHgq/miY2LWrW27hDjR13/bB/fgGFzY5x/Zg4+599m30w2 rJ0g== X-Forwarded-Encrypted: i=1; AJvYcCUMW9tTc2gyaoevYt6BzOtYKxdxOZLhfahHLjWoEeNXZDQfVR8C8LNHlWYH28wyRWl9WUddJoqfQw==@kvack.org X-Gm-Message-State: AOJu0YyFBqHALkQVW0SpU6E85NRObcH8MRSDGND/1AQdQEbynljM4FF1 CGLVJ4irofdJzpEOn40RGqijcgzo1SNsFCBNlkWjdDTwIwRzthnJD5PmKauPDdvDU2cd//M3qXU WaV17Nob2rkokbrfBVf3WSL8ItN1pAn8O7nn4GQ== X-Gm-Gg: ASbGncvNVptKRNRzUJPIVv+pQo619FLbd+u2rlWRU/fJH9Jc7IFuevX8E2vihZa/KKv KhP7FW6h1SMvtuj902cvQjlBnS3znEYVv5Y/m1GCycM5d34/sl36rnVoGo8xj82zPh07lmbLnXU yEIU/EN9TdzIZnCrSLdHyl1cf3Sw/HHOgRLbA= X-Google-Smtp-Source: AGHT+IHVCj2wRJMmbo6RZNbl02DjTtI3TJ70CFKiTUTRR9/3TqJXXoa9siW2YcLob+ml2kZdmIqaw4wvzv4K5mehWjs= X-Received: by 2002:a05:6512:3183:b0:54b:104b:dfcd with SMTP id 2adb3069b0e04-54cb68a5549mr357294e87.53.1744260682907; Wed, 09 Apr 2025 21:51:22 -0700 (PDT) MIME-Version: 1.0 References: <20250409190938.f6befeeb9e86ad72f46503a5@linux-foundation.org> In-Reply-To: <20250409190938.f6befeeb9e86ad72f46503a5@linux-foundation.org> From: Zhongkun He Date: Thu, 10 Apr 2025 12:50:45 +0800 X-Gm-Features: ATxdqUF6D0bvdq2QF7OAMnQbYJVJY8B87-DpNhIZNZl3NEtn10Kry7mFtqaq8jE Message-ID: Subject: Re: [External] Re: [PATCH V3 2/3] mm: add max swappiness arg to lru_gen for anonymous memory only To: Andrew Morton 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Stat-Signature: p6mw9qfx5pjjurgu3qhgy4sox3rh9x61 X-Rspam-User: X-Rspamd-Queue-Id: 39DD2A0005 X-Rspamd-Server: rspam08 X-HE-Tag: 1744260684-551444 X-HE-Meta: U2FsdGVkX1+CAM4Dwix5s2rxPjqX71AcSxJTaN0H5GBgtalJMwoaa9uq9vgw3Db7QLvvrF3UcVgaASUh/EV90qM2Bd4QtE8GTT4yM8zmVJM9o+24BoVJETAIZICVswVAifzP1yvNaDbr8inTTJjiXxuIV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2PB0nzZI6HFqP53C2wRaN00X+rDoPquNakDKDqlr6Xp2fDfEQSZZJK6ttPbrAH8Fz7S4d0lh+68DC1Ci707DrbJq5xQ7e/ZoKfUcrA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Thu, Apr 10, 2025 at 10:10=E2=80=AFAM Andrew Morton wrote: > > On Wed, 9 Apr 2025 15:06:19 +0800 Zhongkun He wrote: > > > The MGLRU > > paging yuzhao? I have cc yuzhao and look forward to the relay. > > > already supports reclaiming only from anonymous memory > > via the /sys/kernel/debug/lru_gen interface. Now, memory.reclaim > > also supports the swappiness=3Dmax 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) !=3D SWAPPINESS_ANON_ONLY) > > + > > #define evictable_min_seq(min_seq, swappiness) = \ > > - min((min_seq)[!(swappiness)], (min_seq)[(swappiness) <=3D MAX_SWA= PPINESS]) > > + min((min_seq)[!(swappiness)], (min_seq)[max_evictable_type(swappi= ness)]) > > Why oh why did we implement these in cpp? Just want to make the code more clear. Maybe we should do more like this /* The range of swappiness is [0,1-200,201], 0 means file type only; * 1-200 anon and file type; 201 anon type only */ #define max_type(swappiness) ((swappiness) !=3D SWAPPINESS_ANON_ONLY) #define min_type(swappiness) !(swappiness) #define evictable_min_seq(min_seq, swappiness) \ min((min_seq)[min_type(swappiness)], (min_seq)[max_type(swappiness)]) #define for_each_evictable_type(type, swappiness) \ - for ((type) =3D !(swappiness); (type) <=3D ((swappiness) <=3D MAX_SWAPPINESS); (type)++) + for ((type) =3D min_type(swappiness) ; (type) <=3D max_type(swappiness); (type)++) > > > > > @@ -3857,7 +3860,7 @@ static bool inc_min_seq(struct lruvec *lruvec, in= t type, int swappiness) > > int hist =3D lru_hist_from_seq(lrugen->min_seq[type]); > > int new_gen, old_gen =3D lru_gen_from_seq(lrugen->min_seq[type]); > > > > - if (type ? swappiness > MAX_SWAPPINESS : !swappiness) > > + if (type ? (swappiness =3D=3D SWAPPINESS_ANON_ONLY) : !swappiness= ) > > This expression makes my brain bleed. > > if (type) { > if (swappiness =3D=3D 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 =3D=3D 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? > Yes, I agree. Will do, thanks. > > 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 ni= d, unsigned long seq, > > > > if (swappiness < MIN_SWAPPINESS) > > swappiness =3D 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 *fil= e, const char __user *src, > > while ((cur =3D 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 *f= ile, const char __user *src, > > if (!*cur) > > continue; > > > > - n =3D sscanf(cur, "%c %u %u %lu %n %u %n %lu %n", &cmd, &= memcg_id, &nid, > > - &seq, &end, &swappiness, &end, &opt, &end); > > + n =3D 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? > IICC, the arg in sscanf is %4s meaning the length of the string will not be allowed to overrun the swap_string, thanks. > > if (n < 4 || cur[end]) { > > err =3D -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? > No, the sscanf will add '\0' to the end, so the maximum number of input characters is 4, and the length of swap_string is 5, with one character reserved for the null terminator '\0' for sscanf. Thanks for your time. > > + swappiness =3D SWAPPINESS_ANON_ONLY; > > + } else { > > + err =3D kstrtouint(swap_string, 0, &swappiness); > > + if (err) > > + break; > > + } > > + > > err =3D run_cmd(cmd, memcg_id, nid, seq, &sc, swappiness,= opt); > > if (err) > > break; >