From patchwork Thu Nov 30 15:36:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Schatzberg X-Patchwork-Id: 13474636 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 3F019C4167B for ; Thu, 30 Nov 2023 15:38:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B6F166B00D1; Thu, 30 Nov 2023 10:38:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AFDB56B00F9; Thu, 30 Nov 2023 10:38:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96DB56B00D1; Thu, 30 Nov 2023 10:38:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 7F0706B00B1 for ; Thu, 30 Nov 2023 10:38:26 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 3999FA023C for ; Thu, 30 Nov 2023 15:38:26 +0000 (UTC) X-FDA: 81515027412.12.1B2C269 Received: from mail-ot1-f44.google.com (mail-ot1-f44.google.com [209.85.210.44]) by imf12.hostedemail.com (Postfix) with ESMTP id 4CA2240016 for ; Thu, 30 Nov 2023 15:38:23 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cO18WshD; spf=pass (imf12.hostedemail.com: domain of schatzberg.dan@gmail.com designates 209.85.210.44 as permitted sender) smtp.mailfrom=schatzberg.dan@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701358704; a=rsa-sha256; cv=none; b=wvJ5bAOUd0vGoDr74ooMp0O3QyVz2EZWXU6Tf0X0zIjUEj+S/G5+jceyLtU+5EpT/DI6Et xQMj8Ho8+tPWTCmf41u0EgaMr9LmvStwqau1KsW6l0OneQrdqqh5TE4XSFchy6Nx8uWds5 myqDIBtfDLV6nX4ioFbm7o43wFWNhSY= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cO18WshD; spf=pass (imf12.hostedemail.com: domain of schatzberg.dan@gmail.com designates 209.85.210.44 as permitted sender) smtp.mailfrom=schatzberg.dan@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701358704; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=eD53ftY/yUsvZFmbSQF35aJZQqcYDgG+Hgt2jQSH4fA=; b=TzV3mgIfYmEUt3hvBBJGwnRhjv5JqA/pycsdqHG7JRyib5Y08xvD8YrJZU7exyQl4Dp6yY MRGFs3ZU9PPVBLnKhvQRfFQFIvf3Orcl6EFOgR9KV1K3BYcWZHUgFDpgA7r4yJXZEUmcAT 8wPAzI4KMnweXtAetuS8tZ29nmaSCMk= Received: by mail-ot1-f44.google.com with SMTP id 46e09a7af769-6d7e8da5e99so654656a34.2 for ; Thu, 30 Nov 2023 07:38:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701358703; x=1701963503; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eD53ftY/yUsvZFmbSQF35aJZQqcYDgG+Hgt2jQSH4fA=; b=cO18WshDyY1Hq7Wvk8cewwQmn/Mn9/pBnwfROY6erhoaJX3vM3d69V8ArhmntIjiqF 0RIxdf7SKOyx3Gu8gAh9/yd1yVQKfZ5+Jb/fcKi2BZVjeWJHS5NRbEoARP3QTskFYR5T sifkO1UAF7S9toH7aIpt5KeduYYD4CSI4P5M372YACsXQ7ppC0Qc0uV4D6smZZ1c1GXt Zk8DBRH54YPqiz/S/BrfleGjHiYov2Ed1Fho2V8oVKYoAgtdHlGm5s9Qzh6hI5KIvGo3 JL0fuU80HMLtMZSiK/mJTE9K+L105GYvG1xLqn1HUTwuAoKfLnGAwH0kQV8QpOGaIPHN z8pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701358703; x=1701963503; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eD53ftY/yUsvZFmbSQF35aJZQqcYDgG+Hgt2jQSH4fA=; b=HzqqrShnqggZ6jBODUz4W+dfzpHK91AkufzgAPPL7s4/bgG95UeyCvgRErKxeBZvWn eegV0DVbahOxbmvPB1HNEBxv0v9pGqLceHLEvKFDDIf0NPsdDAH4SSNCtazRZPjMru6k FI24sHmGQ8/aFRK1NM6l7iPqMnN3bS01bFfceJNlcGvbhppG2SeoqT1v2RcgS2wumygr tXX0PQDq/YWf2tfKcIISMj1+j06+p/dJj8yfFN9Q5G4+f9dD5I0uRb+/17AIiLlTTp00 CcqURPe5u3BFA9NZpa6LyARNpwTs3ZU+VPX7AKRfAtuZP5k8Jfvwqnt+zmYCTWqGmG8y lIkA== X-Gm-Message-State: AOJu0Yx8nr6TBdRrUBtfKp9eO12HXsp7s0qDDDR77l52Fq1F0C/MKgGJ ZvsIet564s3P4/qT/zTVOBI= X-Google-Smtp-Source: AGHT+IGr5SX9jWRJyNlzTzhL/+Qe4hyEpSvcjizSBmvGiziK+hPA6OPpzFTVE6mc1PCtxIAryW3BWA== X-Received: by 2002:a05:6830:1412:b0:6d7:f8f6:5406 with SMTP id v18-20020a056830141200b006d7f8f65406mr23629246otp.0.1701358703108; Thu, 30 Nov 2023 07:38:23 -0800 (PST) Received: from localhost ([2620:10d:c091:500::4:edc5]) by smtp.gmail.com with ESMTPSA id r8-20020a0c8d08000000b0067a18167533sm592780qvb.83.2023.11.30.07.38.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 07:38:22 -0800 (PST) From: Dan Schatzberg To: Johannes Weiner , Roman Gushchin , Yosry Ahmed , Huan Yang Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Michal Hocko , Shakeel Butt , Muchun Song , Andrew Morton , David Hildenbrand , Matthew Wilcox , Huang Ying , Kefeng Wang , Peter Xu , "Vishal Moola (Oracle)" , Yue Zhao , Hugh Dickins Subject: [PATCH 1/1] mm: add swapiness= arg to memory.reclaim Date: Thu, 30 Nov 2023 07:36:54 -0800 Message-Id: <20231130153658.527556-2-schatzberg.dan@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231130153658.527556-1-schatzberg.dan@gmail.com> References: <20231130153658.527556-1-schatzberg.dan@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 4CA2240016 X-Stat-Signature: r91joh3xhbnrnfweyankbda35nhyq6ua X-Rspam-User: X-HE-Tag: 1701358703-347488 X-HE-Meta: U2FsdGVkX1/2YtMcBBFsrqqHuiwTae965gKJHm65rUtcE4wtNz9pIKz7cvTmEAlQNQJXuxKhPtzB+1LbqinSls+o7Cl3GLX0M+GqK7o70cIcLQjPuBxjF6MdLkMQf5IbSfggmYLboquvd7qTYRLVWCvcyOsFFi3jkmgsAQ3C0LH4rglPzj/W8JklEboWEIvA3DvQG+EkBGIOpYtfkmPSBsDAOguZc7E8MP2fLYpZRq+DT6mhlbCiyzUo1s6sVxM17ZEVdHEI+J3CdgfGwH+ScS33GFeAhUkhwQDwOCYHUGHI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WWvUcqdmP5bqJp38WMX/A5rv6xxZQsGvLtOvRRKcMdikLGPJ/eSIfZRT8sQS4Mzo31fXv0RGNMwgvWM5/+6Il/K6fD/lJYedKQZvnkvykkaMtt2zBdYZdX+nzLBiwgGBPFzXfrToJqcXz9tL3/YPQJDi59/oze6YYlYtZIFzPLRL6FONgS9HzhI8lWiGQ== 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: Allow proactive reclaimers to submit an additional swappiness= argument to memory.reclaim. This overrides the global or per-memcg swappiness setting for that reclaim attempt. For example: echo "2M swappiness=0" > /sys/fs/cgroup/memory.reclaim will perform reclaim on the rootcg with a swappiness setting of 0 (no swap) regardless of the vm.swappiness sysctl setting. Signed-off-by: Dan Schatzberg --- include/linux/swap.h | 3 ++- mm/memcontrol.c | 55 +++++++++++++++++++++++++++++++++++--------- mm/vmscan.c | 13 +++++++++-- 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index f6dd6575b905..c6e309199f10 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -410,7 +410,8 @@ extern unsigned long try_to_free_pages(struct zonelist *zonelist, int order, extern unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, unsigned long nr_pages, gfp_t gfp_mask, - unsigned int reclaim_options); + unsigned int reclaim_options, + int *swappiness); extern unsigned long mem_cgroup_shrink_node(struct mem_cgroup *mem, gfp_t gfp_mask, bool noswap, pg_data_t *pgdat, diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 1c1061df9cd1..ba1c89455ab0 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -63,6 +63,7 @@ #include #include #include +#include #include #include "internal.h" #include @@ -2449,7 +2450,7 @@ static unsigned long reclaim_high(struct mem_cgroup *memcg, psi_memstall_enter(&pflags); nr_reclaimed += try_to_free_mem_cgroup_pages(memcg, nr_pages, gfp_mask, - MEMCG_RECLAIM_MAY_SWAP); + MEMCG_RECLAIM_MAY_SWAP, NULL); psi_memstall_leave(&pflags); } while ((memcg = parent_mem_cgroup(memcg)) && !mem_cgroup_is_root(memcg)); @@ -2740,7 +2741,7 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, psi_memstall_enter(&pflags); nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages, - gfp_mask, reclaim_options); + gfp_mask, reclaim_options, NULL); psi_memstall_leave(&pflags); if (mem_cgroup_margin(mem_over_limit) >= nr_pages) @@ -3660,7 +3661,7 @@ static int mem_cgroup_resize_max(struct mem_cgroup *memcg, } if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL, - memsw ? 0 : MEMCG_RECLAIM_MAY_SWAP)) { + memsw ? 0 : MEMCG_RECLAIM_MAY_SWAP, NULL)) { ret = -EBUSY; break; } @@ -3774,7 +3775,7 @@ static int mem_cgroup_force_empty(struct mem_cgroup *memcg) return -EINTR; if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL, - MEMCG_RECLAIM_MAY_SWAP)) + MEMCG_RECLAIM_MAY_SWAP, NULL)) nr_retries--; } @@ -6720,7 +6721,7 @@ static ssize_t memory_high_write(struct kernfs_open_file *of, } reclaimed = try_to_free_mem_cgroup_pages(memcg, nr_pages - high, - GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP); + GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP, NULL); if (!reclaimed && !nr_retries--) break; @@ -6769,7 +6770,7 @@ static ssize_t memory_max_write(struct kernfs_open_file *of, if (nr_reclaims) { if (!try_to_free_mem_cgroup_pages(memcg, nr_pages - max, - GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP)) + GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP, NULL)) nr_reclaims--; continue; } @@ -6895,6 +6896,16 @@ static ssize_t memory_oom_group_write(struct kernfs_open_file *of, return nbytes; } +enum { + MEMORY_RECLAIM_SWAPPINESS = 0, + MEMORY_RECLAIM_NULL, +}; + +static const match_table_t if_tokens = { + { MEMORY_RECLAIM_SWAPPINESS, "swappiness=%d"}, + { MEMORY_RECLAIM_NULL, NULL }, +}; + static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf, size_t nbytes, loff_t off) { @@ -6902,12 +6913,33 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf, unsigned int nr_retries = MAX_RECLAIM_RETRIES; unsigned long nr_to_reclaim, nr_reclaimed = 0; unsigned int reclaim_options; - int err; + char *old_buf, *start; + substring_t args[MAX_OPT_ARGS]; + int swappiness = -1; buf = strstrip(buf); - err = page_counter_memparse(buf, "", &nr_to_reclaim); - if (err) - return err; + + old_buf = buf; + nr_to_reclaim = memparse(buf, &buf) / PAGE_SIZE; + if (buf == old_buf) + return -EINVAL; + + buf = strstrip(buf); + + while ((start = strsep(&buf, " ")) != NULL) { + if (!strlen(start)) + continue; + switch (match_token(start, if_tokens, args)) { + case MEMORY_RECLAIM_SWAPPINESS: + if (match_int(&args[0], &swappiness)) + return -EINVAL; + if (swappiness < 0 || swappiness > 200) + return -EINVAL; + break; + default: + return -EINVAL; + } + } reclaim_options = MEMCG_RECLAIM_MAY_SWAP | MEMCG_RECLAIM_PROACTIVE; while (nr_reclaimed < nr_to_reclaim) { @@ -6926,7 +6958,8 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf, reclaimed = try_to_free_mem_cgroup_pages(memcg, min(nr_to_reclaim - nr_reclaimed, SWAP_CLUSTER_MAX), - GFP_KERNEL, reclaim_options); + GFP_KERNEL, reclaim_options, + swappiness == -1 ? NULL : &swappiness); if (!reclaimed && !nr_retries--) return -EAGAIN; diff --git a/mm/vmscan.c b/mm/vmscan.c index 506f8220c5fe..546704ea01e1 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -136,6 +136,9 @@ struct scan_control { /* Always discard instead of demoting to lower tier memory */ unsigned int no_demotion:1; + /* Swappiness value for reclaim, if NULL use memcg/global value */ + int *swappiness; + /* Allocation order */ s8 order; @@ -2327,7 +2330,8 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, struct pglist_data *pgdat = lruvec_pgdat(lruvec); struct mem_cgroup *memcg = lruvec_memcg(lruvec); unsigned long anon_cost, file_cost, total_cost; - int swappiness = mem_cgroup_swappiness(memcg); + int swappiness = sc->swappiness ? + *sc->swappiness : mem_cgroup_swappiness(memcg); u64 fraction[ANON_AND_FILE]; u64 denominator = 0; /* gcc */ enum scan_balance scan_balance; @@ -2608,6 +2612,9 @@ static int get_swappiness(struct lruvec *lruvec, struct scan_control *sc) mem_cgroup_get_nr_swap_pages(memcg) < MIN_LRU_BATCH) return 0; + if (sc->swappiness) + return *sc->swappiness; + return mem_cgroup_swappiness(memcg); } @@ -6433,7 +6440,8 @@ unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, unsigned long nr_pages, gfp_t gfp_mask, - unsigned int reclaim_options) + unsigned int reclaim_options, + int *swappiness) { unsigned long nr_reclaimed; unsigned int noreclaim_flag; @@ -6448,6 +6456,7 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, .may_unmap = 1, .may_swap = !!(reclaim_options & MEMCG_RECLAIM_MAY_SWAP), .proactive = !!(reclaim_options & MEMCG_RECLAIM_PROACTIVE), + .swappiness = swappiness, }; /* * Traverse the ZONELIST_FALLBACK zonelist of the current node to put