From patchwork Wed Jan 3 16:48:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Schatzberg X-Patchwork-Id: 13510248 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 2055AC3DA6E for ; Wed, 3 Jan 2024 16:49:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8C27C6B00B7; Wed, 3 Jan 2024 11:49:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 872118D006E; Wed, 3 Jan 2024 11:49:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 713F68D006C; Wed, 3 Jan 2024 11:49:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 5D6486B00B7 for ; Wed, 3 Jan 2024 11:49:19 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 289DD1207AA for ; Wed, 3 Jan 2024 16:49:19 +0000 (UTC) X-FDA: 81638585238.17.3F05D3E Received: from mail-yb1-f174.google.com (mail-yb1-f174.google.com [209.85.219.174]) by imf28.hostedemail.com (Postfix) with ESMTP id 670B8C000C for ; Wed, 3 Jan 2024 16:49:16 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=VbymiBEv; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf28.hostedemail.com: domain of schatzberg.dan@gmail.com designates 209.85.219.174 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=1704300556; 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=lwZZYWh+e0yk/cuTVxoiseyhi+aiBF9zz0tdKJ/4vMs=; b=Zci87C0J3irTr6k/iNbd/yTCTcALoanh7ukkMtRqZXxRILAV5sclL5g5QDzDqMJ7IgwBYP sowbKJPQVLqoX/C8g5FYs6gC3PCIEcj3J06+a5YedxipFJYQm0EEsRvDRst3F/g5lHD35/ gbTIlXpaLDgPAMkg0OiKX4Vzshc+pMQ= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=VbymiBEv; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf28.hostedemail.com: domain of schatzberg.dan@gmail.com designates 209.85.219.174 as permitted sender) smtp.mailfrom=schatzberg.dan@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704300556; a=rsa-sha256; cv=none; b=dR+jgX/1ds08QOWqKKHRYOdNjYIXmjPnBT6pSYvjJjomMkhLfc2ixZQk01dDEs+98F56vU oCAduwd7v7476nd4Q2RDrDcjEJK7TrD5cw9H5YEAQbfxQ8wpSaTsw8kEKHDsAnEu3bnrht yQkuniiyBeufA+WHkXxqbPyVUDhbI/I= Received: by mail-yb1-f174.google.com with SMTP id 3f1490d57ef6-dbdc52f2359so6672888276.3 for ; Wed, 03 Jan 2024 08:49:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704300555; x=1704905355; 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=lwZZYWh+e0yk/cuTVxoiseyhi+aiBF9zz0tdKJ/4vMs=; b=VbymiBEvOgoeOt101oaJzM+/Qo/BPKAgh/2Oc2iO0FUpFjljpzeNzyUr1wnZ3gs6JU JimS/pGDY3krJoLZoGVMALB3P6HSwjHbWtCx9JVV44PJEI/q5GyvinPeYQa4rmqMK6rv nIT0vMldfL2QPF5mQoQGKW+2H+kWW4Zw/NpQ2bLjMHaygvTUJkBihgtNF4RkDSZMZ4hq ApqK7qCnz9BtNRM5p9qb+cRq+EKaGMOQmnZNIi0X0P7USiuvtvvciVV1GCPQ0QixFnRT q5rm9pscQUWLFfQ8RLscyQMUM5ce3VK+V8PaDi+z7YNrJ+/J+VCI13X1nPUG2OH+l8/3 HHBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704300555; x=1704905355; 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=lwZZYWh+e0yk/cuTVxoiseyhi+aiBF9zz0tdKJ/4vMs=; b=RiUP3dsv7UobhtCzYvTvRCucp//UC2sJsjcQGx99VNk5iEEc6SKYvojKNUxS6iSwlB 7AHY/6QV/cl04a9RJbQUFqZKkArQ4VQcDZWB8mpUQ27IBc2bqbC5t632OwhfQ+2YVlcz ffUh1FZYaGKUBt/l26sosXrmnVheP0cI2iFl0qiTFtyfMcjXs6qyiWR53tHz9rdFWm2d XI9TbEWQO55zizoPJ/tJ/OrIhXv6iM9NLBwGktf207J+h1FWjkkj3lRH0N2gO3+JZQN7 gR27wif3p4wMYfnyj6B+yuSOkxeoqrg1QPYg5FeT7H2eo6bS0qP3QxfjDT3MgGgIBxes cORA== X-Gm-Message-State: AOJu0YxG0ASOJMjQlzUtGJf2j3DBEJHfgedYNtjkisr69+iq+Oj3ASCh gj2xjchOrNd6i/k4csRiguE= X-Google-Smtp-Source: AGHT+IGe4cxlLsn+e6P2JdgSr5WbR+2RuPxygGamlYxVp3FhQSYsh7zg529zAFbjlkOVWnvfCaAsxw== X-Received: by 2002:a25:ef52:0:b0:dbe:a003:a24a with SMTP id w18-20020a25ef52000000b00dbea003a24amr971520ybm.56.1704300555318; Wed, 03 Jan 2024 08:49:15 -0800 (PST) Received: from localhost ([2620:10d:c091:400::5:fcab]) by smtp.gmail.com with ESMTPSA id ci10-20020a05622a260a00b00427f1eeddccsm7362030qtb.38.2024.01.03.08.49.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jan 2024 08:49:15 -0800 (PST) From: Dan Schatzberg To: Andrew Morton Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, David Rientjes , Chris Li , Nhat Pham , Tejun Heo , Zefan Li , Johannes Weiner , Jonathan Corbet , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , David Hildenbrand , Matthew Wilcox , Kefeng Wang , Dan Schatzberg , Yue Zhao , Yosry Ahmed , Hugh Dickins Subject: [PATCH v6 1/2] mm: add defines for min/max swappiness Date: Wed, 3 Jan 2024 08:48:36 -0800 Message-Id: <20240103164841.2800183-2-schatzberg.dan@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240103164841.2800183-1-schatzberg.dan@gmail.com> References: <20240103164841.2800183-1-schatzberg.dan@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 670B8C000C X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 7sn699d85ozr1yjm4g1xtr3zfxtikyhx X-HE-Tag: 1704300556-513520 X-HE-Meta: U2FsdGVkX18lqHnrSF0J2y5L9WZmgRgz1W52dJlQY+wRPy2XbQB8SMhFroPjHjb24l0QtgbeFrEkC41kCps9+DlxU9f8nuurHIRhcAu4BJVWwWs244q8Vc/yeqB8JawQ7Hjg1qZz7quDmFXsTSSFt3Sjthss8LBamqpOufnrzjFts9R6kW8b424oLSb9W5IEAVYeMYBLNvOmjolwWueP2HeYAWw5N6w4I+Mom1Ww7m0eXMBTYXFmgDR7CLmMr1iiA//yv9HGquM5cqNx6CHXcL1ORNRpIHgts9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+m5I+A3sWYERzhlEZQYPGlIWmXoKtJzqY3cDPQc1TAbBU0HipkKKuG7XGVcdGHQ37RIcBpDdmj6cOCWnDeyQpaG9yfKk1HTw1JPQoPmKxtLKJhnNFYHgwpml/ZWkhv30noZnbDiVXmxCVPglOKGr0VoD8Sm6zDUu4J6n87x0AQgHuxtp1CPoWf2Ud3mfgPP7L6Vf2lg23Pr9InopfF+X70n1vp128i6xQEEix6jilYvBCwrrWQkuCfTJVtG9YkMJJ 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: We use the constants 0 and 200 in a few places in the mm code when referring to the min and max swappiness. This patch adds MIN_SWAPPINESS and MAX_SWAPPINESS #defines to improve clarity. There are no functional changes. Signed-off-by: Dan Schatzberg Acked-by: David Rientjes Acked-by: Chris Li Reviewed-by: Nhat Pham --- include/linux/swap.h | 2 ++ mm/memcontrol.c | 2 +- mm/vmscan.c | 14 +++++++------- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index f6dd6575b905..e2ab76c25b4a 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -407,6 +407,8 @@ extern unsigned long try_to_free_pages(struct zonelist *zonelist, int order, #define MEMCG_RECLAIM_MAY_SWAP (1 << 1) #define MEMCG_RECLAIM_PROACTIVE (1 << 2) +#define MIN_SWAPPINESS 0 +#define MAX_SWAPPINESS 200 extern unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, unsigned long nr_pages, gfp_t gfp_mask, diff --git a/mm/memcontrol.c b/mm/memcontrol.c index b226090fd906..fbe9f02dd206 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -4337,7 +4337,7 @@ static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css, { struct mem_cgroup *memcg = mem_cgroup_from_css(css); - if (val > 200) + if (val > MAX_SWAPPINESS) return -EINVAL; if (!mem_cgroup_is_root(memcg)) diff --git a/mm/vmscan.c b/mm/vmscan.c index 9dd8977de5a2..d91963e2d47f 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -183,7 +183,7 @@ struct scan_control { #endif /* - * From 0 .. 200. Higher means more swappy. + * From 0 .. MAX_SWAPPINESS. Higher means more swappy. */ int vm_swappiness = 60; @@ -2403,7 +2403,7 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, ap = swappiness * (total_cost + 1); ap /= anon_cost + 1; - fp = (200 - swappiness) * (total_cost + 1); + fp = (MAX_SWAPPINESS - swappiness) * (total_cost + 1); fp /= file_cost + 1; fraction[0] = ap; @@ -4400,7 +4400,7 @@ static int get_type_to_scan(struct lruvec *lruvec, int swappiness, int *tier_idx { int type, tier; struct ctrl_pos sp, pv; - int gain[ANON_AND_FILE] = { swappiness, 200 - swappiness }; + int gain[ANON_AND_FILE] = { swappiness, MAX_SWAPPINESS - swappiness }; /* * Compare the first tier of anon with that of file to determine which @@ -4436,7 +4436,7 @@ static int isolate_folios(struct lruvec *lruvec, struct scan_control *sc, int sw /* * Try to make the obvious choice first. When anon and file are both * available from the same generation, interpret swappiness 1 as file - * first and 200 as anon first. + * first and MAX_SWAPPINESS as anon first. */ if (!swappiness) type = LRU_GEN_FILE; @@ -4444,7 +4444,7 @@ static int isolate_folios(struct lruvec *lruvec, struct scan_control *sc, int sw type = LRU_GEN_ANON; else if (swappiness == 1) type = LRU_GEN_FILE; - else if (swappiness == 200) + else if (swappiness == MAX_SWAPPINESS) type = LRU_GEN_ANON; else type = get_type_to_scan(lruvec, swappiness, &tier); @@ -5398,9 +5398,9 @@ static int run_cmd(char cmd, int memcg_id, int nid, unsigned long seq, lruvec = get_lruvec(memcg, nid); - if (swappiness < 0) + if (swappiness < MIN_SWAPPINESS) swappiness = get_swappiness(lruvec, sc); - else if (swappiness > 200) + else if (swappiness > MAX_SWAPPINESS) goto done; switch (cmd) { From patchwork Wed Jan 3 16:48:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Schatzberg X-Patchwork-Id: 13510249 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 3C6DCC3DA6E for ; Wed, 3 Jan 2024 16:49:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A222C8D006E; Wed, 3 Jan 2024 11:49:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9D2788D006C; Wed, 3 Jan 2024 11:49:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 84B688D006E; Wed, 3 Jan 2024 11:49:23 -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 6DCD98D006C for ; Wed, 3 Jan 2024 11:49:23 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3233E80981 for ; Wed, 3 Jan 2024 16:49:23 +0000 (UTC) X-FDA: 81638585406.06.9BA6345 Received: from mail-oa1-f46.google.com (mail-oa1-f46.google.com [209.85.160.46]) by imf16.hostedemail.com (Postfix) with ESMTP id 52300180010 for ; Wed, 3 Jan 2024 16:49:21 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Vgjh7DYK; spf=pass (imf16.hostedemail.com: domain of schatzberg.dan@gmail.com designates 209.85.160.46 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=1704300561; a=rsa-sha256; cv=none; b=XUF7O0V/qml1EWHzzEhz+Xv5/kYMEgkoIFTAsTfSYeEtAJFm4QdqYftAnYWg/kxOWy1j66 FAOyUkzv7T5U8vS5oA3ajrwR7zJ9CtePYkSjtnFZTek5tcNUyQrx1Wj+AYWETzoiU9uMrG IByCjRgXdZcp11oD7C0EsgYbNDmspT0= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Vgjh7DYK; spf=pass (imf16.hostedemail.com: domain of schatzberg.dan@gmail.com designates 209.85.160.46 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=1704300561; 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=D3Trd70tjvozyYFyAqV83nWEjrGIqqcGlRP9h5UvVDg=; b=pOS8KYDK1nddrJsvRal5AGsk62zVGW8cO4rHRzHO4EUUSuxaeed1aYq1o22n99Ef6YUWWp vhHARHeL2TROIUji1vO5xusTjP6q3nGXNfraTnLZQSI7MM35BSZTGshu3dgL+mhrQ6dinr xbdkFxaSWIKnsSRr9hU7KjUeO4FWOKo= Received: by mail-oa1-f46.google.com with SMTP id 586e51a60fabf-2046dee3c14so4083283fac.1 for ; Wed, 03 Jan 2024 08:49:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704300560; x=1704905360; 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=D3Trd70tjvozyYFyAqV83nWEjrGIqqcGlRP9h5UvVDg=; b=Vgjh7DYKDFo+Ibk+QquCNhZqj6DLc7rlYmpR55/wdAwtvR+8tlKcGTgfSS+1fqhGjs dMTfoKlu7NUn3nu/j1riebvqxVuN9IyRTSD+hWrKHyNYGz7ftEecK93lLxVWOm4sJ0e1 3BbkDtdXgrv1IRJKzmZM/8WOCk9EsXSzVXwNM5fsyCl6+v/Ml1ugZby0RyXqlA8bMCjC mQK6U7/LDzllxTsEE3nGf7vmPo9Fmi0756+aA0YRsLzqzf30vflgdPKBNfNu3bou8W9A GCB/LDt8PAqVlX3NERoDU08uSv2b1n4oVQCwC5z7b/YfvFR/VSqYtIi+mx19TR+yqw5B iLXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704300560; x=1704905360; 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=D3Trd70tjvozyYFyAqV83nWEjrGIqqcGlRP9h5UvVDg=; b=p7EftrRCoDwhGMgFuKhX/6xl868l+9vFyZ6G9UiuZ0xm9TcWSr6Zfq0o9Sqw4IjLq7 IjczqR4Nvmx0GQKOmOQnFdsnG3BS5zEcN1WUq5Q0TlzyrB4PMVYGTjJ5lOza/2cv22As /Xr8gDVjBdhqZdDmmqO/tllgJezmox15I5pO9RcAZSb3MIPb8zwxJG0q2lpkFo1OiA0Q TrVj0PMpvQU44obvJE2PGaFWKotXZ2QeutEJDQvFpr/vv6O9kMBcvcxwhfo9F56cOnQF C4QW4PByoCO+bL/4z+YNUB4JdGC9CH3HbkShDBiWVulj+sZHb0Mzp8+MldTdd+eZ9DV2 jUlg== X-Gm-Message-State: AOJu0YyrI6sgfOHRF460Mtlt5FTloYZXD17lvd3HrAWpcRxUX+9INUWK CsybwM6at57KmgbyuU3jdnE= X-Google-Smtp-Source: AGHT+IGWRB8L2IIvPf9EnZ4lUQJp6LGFWjdNsZAx9VXs2gk8qcsXnGn53Hz5bzHQNB+7UwxdKCkwhQ== X-Received: by 2002:a05:6870:63a0:b0:204:4926:1824 with SMTP id t32-20020a05687063a000b0020449261824mr16093129oap.80.1704300560221; Wed, 03 Jan 2024 08:49:20 -0800 (PST) Received: from localhost ([2620:10d:c091:400::5:fcab]) by smtp.gmail.com with ESMTPSA id l19-20020a05620a28d300b0077d84f46d65sm10296915qkp.37.2024.01.03.08.49.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jan 2024 08:49:19 -0800 (PST) From: Dan Schatzberg To: Andrew Morton Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Yosry Ahmed , Michal Hocko , David Rientjes , Chris Li , Tejun Heo , Zefan Li , Johannes Weiner , Jonathan Corbet , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , David Hildenbrand , Matthew Wilcox , Kefeng Wang , Dan Schatzberg , Yue Zhao , Hugh Dickins Subject: [PATCH v6 2/2] mm: add swapiness= arg to memory.reclaim Date: Wed, 3 Jan 2024 08:48:37 -0800 Message-Id: <20240103164841.2800183-3-schatzberg.dan@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240103164841.2800183-1-schatzberg.dan@gmail.com> References: <20240103164841.2800183-1-schatzberg.dan@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 52300180010 X-Stat-Signature: pf1rra5ojjnapyq775ooiagqno9gnw68 X-Rspam-User: X-HE-Tag: 1704300561-234174 X-HE-Meta: U2FsdGVkX19L3ApNrb5rkHOcntiQaosZJ4ZJBIS/hjnhKuFyoxU3vljf7fL24ngEhCrcj8udFTbhOvId2RCslIPBgUnbKmF1wS5NIxZKCF13WQAS7ylQmpEoqahVVPNg8qWeVnO5nbdIgzLQ8lrlc66SNtJvMHsHVB4/zg9wiV7aky5jr8E/hQjkn3QprdH2mSS0243P16oYQEhMGK5wwSem7Wp07FrIR9E6EceiUb9ZbkZjz5F8JsKrEetOd2oa/CFq4epQJkgIBRbrsxPAlmuij+i/UZ6+0a1H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vXr8qC6Aoc/q9z6zQVNpifsozQ08f2qQOE9hnwTwYSCatTtcDVewdzCFosArBIfXmZPa8uyUVt+EGj0ZiBP6nKkdtrrCXBcP232TRMDBotahO766ZReHT/FY0oIBXWBQpfmuCeJpaZNRRgIspv4s9bixeY7UrUJGTAWhLkdGk7Giycxncj9ZwIPLyvuZ4MDkj2urK7cxocmXr4V0oDuEp97Cp6LTjSpnwB/bVXdeLFuiPeyBa0NHPXPPbUka8z7hq848vjOUuBGB4BiG1cK+4V4vzBZUQa/4/11Rvaz/MeSHo+umZyFx7ZAURJA278QTF4lnQVWWGHu1B3sspxJla/BhIac84eMFZCPcKet2s7H7ozWf8EBX4CMRjJhpIlxSAZ4o2k+yhFH3bMeQtIqjemvS3EhiUWljcqL6CISCExvq 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. Userspace proactive reclaimers use the memory.reclaim interface to trigger reclaim. The memory.reclaim interface does not allow for any way to effect the balance of file vs anon during proactive reclaim. The only approach is to adjust the vm.swappiness setting. However, there are a few reasons we look to control the balance of file vs anon during proactive reclaim, separately from reactive reclaim: * Swapout should be limited to manage SSD write endurance. In near-OOM situations we are fine with lots of swap-out to avoid OOMs. As these are typically rare events, they have relatively little impact on write endurance. However, proactive reclaim runs continuously and so its impact on SSD write endurance is more significant. Therefore it is desireable to control swap-out for proactive reclaim separately from reactive reclaim * Some userspace OOM killers like systemd-oomd[1] support OOM killing on swap exhaustion. This makes sense if the swap exhaustion is triggered due to reactive reclaim but less so if it is triggered due to proactive reclaim (e.g. one could see OOMs when free memory is ample but anon is just particularly cold). Therefore, it's desireable to have proactive reclaim reduce or stop swap-out before the threshold at which OOM killing occurs. In the case of Meta's Senpai proactive reclaimer, we adjust vm.swappiness before writes to memory.reclaim[2]. This has been in production for nearly two years and has addressed our needs to control proactive vs reactive reclaim behavior but is still not ideal for a number of reasons: * vm.swappiness is a global setting, adjusting it can race/interfere with other system administration that wishes to control vm.swappiness. In our case, we need to disable Senpai before adjusting vm.swappiness. * vm.swappiness is stateful - so a crash or restart of Senpai can leave a misconfigured setting. This requires some additional management to record the "desired" setting and ensure Senpai always adjusts to it. With this patch, we avoid these downsides of adjusting vm.swappiness globally. [1]https://www.freedesktop.org/software/systemd/man/latest/systemd-oomd.service.html [2]https://github.com/facebookincubator/oomd/blob/main/src/oomd/plugins/Senpai.cpp#L585-L598 Signed-off-by: Dan Schatzberg Suggested-by: Yosry Ahmed Acked-by: Michal Hocko Acked-by: David Rientjes Acked-by: Chris Li --- Documentation/admin-guide/cgroup-v2.rst | 18 ++++---- include/linux/swap.h | 3 +- mm/memcontrol.c | 56 ++++++++++++++++++++----- mm/vmscan.c | 25 +++++++++-- 4 files changed, 80 insertions(+), 22 deletions(-) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst index 3f85254f3cef..ee42f74e0765 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1282,17 +1282,10 @@ 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. - Example:: echo "1G" > memory.reclaim - The interface can be later extended with nested keys to - configure the reclaim behavior. For example, specify the - type of memory to reclaim from (anon, file, ..). - Please note that the kernel can over or under reclaim from the target cgroup. If less bytes are reclaimed than the specified amount, -EAGAIN is returned. @@ -1304,6 +1297,17 @@ PAGE_SIZE multiple when read back. This means that the networking layer will not adapt based on reclaim induced by memory.reclaim. +The following nested keys are defined. + + ========== ================================ + swappiness Swappiness value to reclaim with + ========== ================================ + + Specifying a swappiness value instructs the kernel to perform + the reclaim with that swappiness value. Note that this has the + same semantics as vm.swappiness applied to memcg reclaim with + all the existing limitations and potential future extensions. + 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 e2ab76c25b4a..8afdec40efe3 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -412,7 +412,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 fbe9f02dd206..6d627a754851 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #include @@ -2449,7 +2450,8 @@ 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 +2742,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 +3662,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 +3776,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 +6722,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 +6771,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,19 +6897,50 @@ 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 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) { struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); unsigned int nr_retries = MAX_RECLAIM_RETRIES; unsigned long nr_to_reclaim, nr_reclaimed = 0; + int swappiness = -1; unsigned int reclaim_options; - int err; + char *old_buf, *start; + substring_t args[MAX_OPT_ARGS]; 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, tokens, args)) { + case MEMORY_RECLAIM_SWAPPINESS: + if (match_int(&args[0], &swappiness)) + return -EINVAL; + if (swappiness < MIN_SWAPPINESS || swappiness > MAX_SWAPPINESS) + return -EINVAL; + break; + default: + return -EINVAL; + } + } reclaim_options = MEMCG_RECLAIM_MAY_SWAP | MEMCG_RECLAIM_PROACTIVE; while (nr_reclaimed < nr_to_reclaim) { @@ -6926,7 +6959,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 d91963e2d47f..394e0dd46b2e 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -92,6 +92,11 @@ struct scan_control { unsigned long anon_cost; unsigned long file_cost; +#ifdef CONFIG_MEMCG + /* Swappiness value for proactive reclaim. Always use sc_swappiness()! */ + int *proactive_swappiness; +#endif + /* Can active folios be deactivated as part of reclaim? */ #define DEACTIVATE_ANON 1 #define DEACTIVATE_FILE 2 @@ -227,6 +232,13 @@ static bool writeback_throttling_sane(struct scan_control *sc) #endif return false; } + +static int sc_swappiness(struct scan_control *sc, struct mem_cgroup *memcg) +{ + if (sc->proactive && sc->proactive_swappiness) + return *sc->proactive_swappiness; + return mem_cgroup_swappiness(memcg); +} #else static bool cgroup_reclaim(struct scan_control *sc) { @@ -242,6 +254,11 @@ static bool writeback_throttling_sane(struct scan_control *sc) { return true; } + +static int sc_swappiness(struct scan_control *sc, struct mem_cgroup *memcg) +{ + return READ_ONCE(vm_swappiness); +} #endif static void set_task_reclaim_state(struct task_struct *task, @@ -2327,7 +2344,7 @@ 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, memcg); u64 fraction[ANON_AND_FILE]; u64 denominator = 0; /* gcc */ enum scan_balance scan_balance; @@ -2608,7 +2625,7 @@ static int get_swappiness(struct lruvec *lruvec, struct scan_control *sc) mem_cgroup_get_nr_swap_pages(memcg) < MIN_LRU_BATCH) return 0; - return mem_cgroup_swappiness(memcg); + return sc_swappiness(sc, memcg); } static int get_nr_gens(struct lruvec *lruvec, int type) @@ -6463,12 +6480,14 @@ 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; struct scan_control sc = { .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), + .proactive_swappiness = swappiness, .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), .reclaim_idx = MAX_NR_ZONES - 1,