From patchwork Mon Dec 11 14:04:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Schatzberg X-Patchwork-Id: 13487231 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 002EEC10DC3 for ; Mon, 11 Dec 2023 14:04:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 84A7B6B00EA; Mon, 11 Dec 2023 09:04:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D43E6B00EC; Mon, 11 Dec 2023 09:04:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 626A46B00ED; Mon, 11 Dec 2023 09:04:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 4DFB56B00EA for ; Mon, 11 Dec 2023 09:04:42 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 0732F160623 for ; Mon, 11 Dec 2023 14:04:42 +0000 (UTC) X-FDA: 81554708004.13.02DE761 Received: from mail-qt1-f176.google.com (mail-qt1-f176.google.com [209.85.160.176]) by imf21.hostedemail.com (Postfix) with ESMTP id D68261C0032 for ; Mon, 11 Dec 2023 14:04:39 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=bWkAwngf; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf21.hostedemail.com: domain of schatzberg.dan@gmail.com designates 209.85.160.176 as permitted sender) smtp.mailfrom=schatzberg.dan@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702303479; 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=Fjv3xaAwIYR0bV7vWv+aqP2iMgmrLsGsg7noubEWJU0=; b=rJ9Tx9OCQ+SpV66twJ54n+jVyXyJtAh4JKG4TW1UrNn3QpRdym70qtfh1W9yMBd/keckaA 9xVFpkAhSTq+QN0w/jc1OqHqzCegnihWFJg3F+wyYFYpsJh4PO8yMmhNyxj+bO2jI4GzKA DmHF9YQjCGMuHTSJI89Rbys19WhTpN0= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=bWkAwngf; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf21.hostedemail.com: domain of schatzberg.dan@gmail.com designates 209.85.160.176 as permitted sender) smtp.mailfrom=schatzberg.dan@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702303479; a=rsa-sha256; cv=none; b=d6GVynkHM4KYZ913c8ZPMASUUjWebixD06KIK60+suu8J6acQ5/0yoh6Cx5z3rf1a4jwUa /X9cE3lfzj/NRTM8cUO0mmOH0+tAgNlzyIxDADiFMsARK1WPzdwg8rP6t/9MzJTxLHMZYd TjHJ7FDqjCeSdhUYAUyXV/RCDSCb8vQ= Received: by mail-qt1-f176.google.com with SMTP id d75a77b69052e-423db8ab6e0so25151721cf.1 for ; Mon, 11 Dec 2023 06:04:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702303479; x=1702908279; 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=Fjv3xaAwIYR0bV7vWv+aqP2iMgmrLsGsg7noubEWJU0=; b=bWkAwngfCwAaCgyL7G8F1PvLLn8XLdUdIfgauHEWO7OHFbFbCu2x0Ync2kkDGGEfNu r0C2yPxAxp9rXgnZqisLP3GwWhYspWDNH2t9Q8Z0MLq5z9IBQLC1Zcu/lKx41SFTIYyp sERqRhYUAAXurxqsIkt03zUTgaQhwy4B+wk1p23/Rm/RBeMGS0/m043VdcQRFRD66i5+ PSPipqbMCn6FG1KirQu8t+q2+NLXLgpsJIL9A9pI+NlTZl0l4JjJ21xEv0S4dSbG1Spg J+k3gbVysREQqCBZGjT9aILbBn1P4XuGSLubALT6iW2qumaJOuRt09mZLRdWovO6bhhr Ehzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702303479; x=1702908279; 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=Fjv3xaAwIYR0bV7vWv+aqP2iMgmrLsGsg7noubEWJU0=; b=ib/RMAbiwmiGcaKf8NmuKpKftEFCVgt6cGcXjkJgr2/W14CHguVKdXDwrZvknmAeIK 36wffbVl6JyMES8Hg50D+baiIQf4JADD4J6sgQa/x1ssVRI7bVelN6fDXMXDur9iVxFY 1U/gIqR1oSRqk7fKsuWmrh4lZFRTUcxemAuRToC/JHAENZ+tToTBMNimX4Zrhiu6CB3E M58rqzFqdX7dKIN5EPASUQrRbPPnQEMQKd/kwiGiRViExdj8dgDtSDzaHUO7n+zrwsLC hnCTtlzUjEm++Nf8jLAAejJMByG0BicbCibqVvPgdlWGpT1pX2vDk33jCbZD0B+8N6r6 wtVw== X-Gm-Message-State: AOJu0Yy8ieTWIDK0g8RqRE1wywEM4pHmrtTk2jasJDQPSNNw/Mh3Yn5H bFxUwAFk1ae7jaekyLixV78= X-Google-Smtp-Source: AGHT+IF1HX3JHdwq2GaOCTk9P4s+U/GdIY1YVtlNRng0dWHJJC3DxlF+r8Rtb94zL7GDHDFVqmzLjQ== X-Received: by 2002:a05:622a:1207:b0:425:a6dd:b997 with SMTP id y7-20020a05622a120700b00425a6ddb997mr2984093qtx.132.1702303478657; Mon, 11 Dec 2023 06:04:38 -0800 (PST) Received: from localhost ([2620:10d:c091:400::5:45f0]) by smtp.gmail.com with ESMTPSA id j25-20020ac874d9000000b004257e4c5e21sm3201635qtr.28.2023.12.11.06.04.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 06:04:38 -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, Tejun Heo , Zefan Li , Jonathan Corbet , Michal Hocko , Shakeel Butt , Muchun Song , Andrew Morton , David Hildenbrand , Matthew Wilcox , Chris Li , Kefeng Wang , "Vishal Moola (Oracle)" , Yue Zhao , Hugh Dickins Subject: [PATCH V3 1/1] mm: add swapiness= arg to memory.reclaim Date: Mon, 11 Dec 2023 06:04:15 -0800 Message-Id: <20231211140419.1298178-2-schatzberg.dan@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231211140419.1298178-1-schatzberg.dan@gmail.com> References: <20231211140419.1298178-1-schatzberg.dan@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: D68261C0032 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: j9pfz1ush1sf13t6u85wweigad4wp4iq X-HE-Tag: 1702303479-533547 X-HE-Meta: U2FsdGVkX18D5iC5X+VneYkQz6tDJmr4v4XlSgT/a2me8ZtRtWIojxQADOslHfv/2vvFURJFyHfdl42YbDm+43Yw3W5FUyXI0QjiUQkpATsx1JhlugRVDATYLgOFdNnWKmkiJWy5slTpDFQ+mPAY1yVhwvfbkLOAz7IDZzvlGE5TcY5A1NPwEWvxaQvt7SuxgJ3/kra3V1+U//Q8FNngJLnePc9E+KXfYe6Yj+KdNdo7hKYY9jy/ZyOKWHKwt3ejoTmKtLkMpVK24y/K7NkkHikaX6UF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LfP9yiPTEBmomUWCUf+gT0Z3f1Ut8f+ByaRBPbG8kvMTuebk1FCdq/Bb4re9Dkh7CyHQAn/e73KaPR6v5/vcM3hdNesOFynTw1Ie8UoiMUBX1noX+N8PO4XseyzowwCbjExtfc38y11roGTPzrrJoCqCnG2UqDMRXiYjZglBtg8ibSWhXqLH9/Hq+jzN+xVgcn9wKCrVsw== 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 --- Documentation/admin-guide/cgroup-v2.rst | 15 ++++++- include/linux/swap.h | 3 +- mm/memcontrol.c | 55 ++++++++++++++++++++----- mm/vmscan.c | 13 +++++- 4 files changed, 70 insertions(+), 16 deletions(-) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst index 3f85254f3cef..fc2b379dbd0f 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1282,8 +1282,8 @@ PAGE_SIZE multiple when read back. This is a simple interface to trigger memory reclaim in the target cgroup. - This file accepts a single key, the number of bytes to reclaim. - No nested keys are currently supported. + This file accepts a string which containers thhe number of bytes + to reclaim. Example:: @@ -1304,6 +1304,17 @@ PAGE_SIZE multiple when read back. This means that the networking layer will not adapt based on reclaim induced by memory.reclaim. + This file also allows the user to specify the swappiness value + to be used for the reclaim. For example: + + echo "1G swappiness=60" > memory.reclaim + + The above instructs the kernel to perform the reclaim with + a swappiness value of 60. Note that this has the same semantics + as the vm.swappiness sysctl - it sets the relative IO cost of + reclaiming anon vs file memory but does not allow for reclaiming + specific amounts of anon or file memory. + memory.peak A read-only single value file which exists on non-root cgroups. diff --git a/include/linux/swap.h b/include/linux/swap.h index f6dd6575b905..ebc20d094609 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..74598c17d3cc 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, -1); 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, -1); 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, -1)) { 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, -1)) 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, -1); 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, -1)) 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); if (!reclaimed && !nr_retries--) return -EAGAIN; diff --git a/mm/vmscan.c b/mm/vmscan.c index 506f8220c5fe..a20965b20177 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 -1 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 != -1 ? + 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 != -1) + 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