From patchwork Wed Jan 3 16:48:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Schatzberg X-Patchwork-Id: 13510247 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 88B76C3DA6E for ; Wed, 3 Jan 2024 16:49:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D56FF6B037C; Wed, 3 Jan 2024 11:49:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D07EA6B03B1; Wed, 3 Jan 2024 11:49:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BA7C96B03B2; Wed, 3 Jan 2024 11:49:10 -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 A45076B037C for ; Wed, 3 Jan 2024 11:49:10 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 5F2801C12A1 for ; Wed, 3 Jan 2024 16:49:10 +0000 (UTC) X-FDA: 81638584860.09.6B2D3F1 Received: from mail-ot1-f47.google.com (mail-ot1-f47.google.com [209.85.210.47]) by imf30.hostedemail.com (Postfix) with ESMTP id 936998001A for ; Wed, 3 Jan 2024 16:49:08 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=NvI9eZbL; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of schatzberg.dan@gmail.com designates 209.85.210.47 as permitted sender) smtp.mailfrom=schatzberg.dan@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704300548; a=rsa-sha256; cv=none; b=j4guguH/sejHtg6+RWrmzeGgp9QqLgoTo0asPe9xWTIfcFQ648JrYEUFXY9Q2A9U7/ePcQ btLqEwQHoUgv3w9HqlzIf/wProxesc4Wz6oaioAbss3pA1kgPI6aiTZUOtu4KVLP/eGygQ C1ElJ/yoDSi98OtVopeuq89Co5qFgLg= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=NvI9eZbL; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of schatzberg.dan@gmail.com designates 209.85.210.47 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=1704300548; 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:references:dkim-signature; bh=7YOxkBSsy8gVCBj2wU1um2RFxkUEv6eBM3fBzmTk6ZQ=; b=oDCwUhah61DqrqVMvdNVJncjvYDhxtrnFJ5b8F6SKdTCp4H0mqUF6p8z5iLaDkukv/1fyI YEFdzf3HjAcR9yFHkU/dZquIuaqnSiDFeuslmDYM9sLNUodZ7g7whdRs97eteJf0RMBrHr DEaXDCvPrfOCgMKYMJQZglNvwhgQPbA= Received: by mail-ot1-f47.google.com with SMTP id 46e09a7af769-6dc20b4595bso3252237a34.0 for ; Wed, 03 Jan 2024 08:49:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704300547; x=1704905347; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=7YOxkBSsy8gVCBj2wU1um2RFxkUEv6eBM3fBzmTk6ZQ=; b=NvI9eZbL5VqMofuU4eM5glM6OVftwm6rVmGCLpT4wzkDyILIoPkscWM+zJTei+WYGr DNMQCVFIerkrxgU4tBNqbj7vYSjGKm6NuLi+uJSVeT7vZhvE8va/l2jF8m6fAfYU86oc lH7f9vl5k0q9kPe2LcvSI/+PIN+6ubhHnR9ganx5pz99ZrnEwu2gDedeqg3xTjatO5U+ QNRc2/fUyJdcBzKIbWHsHWYZXEOvcauJ8JaLC/2oVROi1eoZPqy0YOs1ZFxAigdH1uKF tTCd6A4uI0X9A5doCYKf4okygnj0rBwAWGI5bpdUjf6w4CjHz9CQedPb2YG4Dgqi5a95 rJbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704300547; x=1704905347; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7YOxkBSsy8gVCBj2wU1um2RFxkUEv6eBM3fBzmTk6ZQ=; b=XpxhLA7hyE9Cp1THq3vyjxxKCnAT1OW6fCFw8OiwJDZRwoccHvwKsMyo5CGGu4bLpy XMDog7BiTnRISLoqMxx7jsaSilmJW38qxZGPbFcRQFFqlU4isxszWM+N91U/e5EHwDeK KAdtbIOOWdmFdCGScIvuIegUaYGw63Tq64FKiQKx17yWiaa9S/rI9d3R8O7hHv5E8sYm yyDy23kkjxdtqkYyGnV6Q7NL/MxF+vMWG/4wcx038dd5BghNzdm/GKvACsuD2tVk8IjI bCGz4e/WBPcJymld37DGnJolVf8v43nr9zFpqQTKLXaz486lfi4lT5mzmJivF2gmX0Sl cR5A== X-Gm-Message-State: AOJu0Yx0sGKaCEsnRdhBuiWtCf7QJXj0V2GMwAqnqXtiSatCKSAgZTqV dJL0WwFSa3uFVNqZzq3835g= X-Google-Smtp-Source: AGHT+IFLNd6r4sLlBUDIfHH5k+tFiPZb3cLEvcFpPURN1hrp6Tu+pnN5zSuJjq33t8jB9TXNfanpxg== X-Received: by 2002:a9d:63d6:0:b0:6db:b08d:67b6 with SMTP id e22-20020a9d63d6000000b006dbb08d67b6mr12936091otl.53.1704300547484; Wed, 03 Jan 2024 08:49:07 -0800 (PST) Received: from localhost ([2620:10d:c091:400::5:fcab]) by smtp.gmail.com with ESMTPSA id k15-20020ac8478f000000b004240481cee0sm14234406qtq.58.2024.01.03.08.49.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jan 2024 08:49:07 -0800 (PST) From: Dan Schatzberg To: Andrew Morton Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Tejun Heo , Zefan Li , Johannes Weiner , Jonathan Corbet , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , David Hildenbrand , Chris Li , Matthew Wilcox , Kefeng Wang , Dan Schatzberg , Yosry Ahmed , Yue Zhao , Hugh Dickins Subject: [PATCH v6 0/2] Add swappiness argument to memory.reclaim Date: Wed, 3 Jan 2024 08:48:35 -0800 Message-Id: <20240103164841.2800183-1-schatzberg.dan@gmail.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 936998001A X-Stat-Signature: 5cmj5ixizsepw73twx8xzm9ddsghnonk X-HE-Tag: 1704300548-67838 X-HE-Meta: U2FsdGVkX1/wtOnQygIYtcX9SHg9cyJMWOexHcvo34yM0VtkUhoz2RK2xl7B5NIUTrLbhb2E9zbjEFdChC2oeFbw4X5I1lb5uxighopEb+c47nMcgAYH3ZYgpC5OGdGC1+JeQDyCHEjNjfM/crII52Av3saFrjYdzPT2I07Al4dI+af6s7ifRuCE5frxp/Ez/c4SnVXRvdyWQMKmKfzxyfQqIK/HxuuXtrT3oeRc3h5JsPY890A89A8vDJtEn8rPEDrtjfMv3OSSPyA7czYvdLMaqb632wZrXeMOdOh/7kiVWILe7mqQg33+bC5WaeEw7rgabHC6OCZdzfJL1X07EAqwJHsP6zhTeF+DrntdidIdkTAHl4G8sExvuKxvfPPfXhKXpA4Y5wjwz9u85Yt/1gFDQu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DcwV3YyzNw6Ntw0i9TcqmaVy7mpgMHkM5k3yF73ShNnTdiY1riMoEeZ4IVJ0bUyLgzdNsDFP5S2sb1byCN+VNxB9LFIwBODM3D5q/skFHQgouw9dXdpEvqV1PvIK0QQ7Q4U6fijInMe7KIRAeXPKAlHHwNm6NE7xwl9XRJ6XfSs0vfoZa8YkVfZRRqT2nop5IDKMbNFsj7UlccEo+cvOH004BX+Jje6xj+IZKBbzuzSde/R6xVDVB9ZaCqEGE1yy0YiIU9rvHTZu3yFZe308KQ5ps0rYCNj8= 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: Changes since V5: * Made the scan_control behavior limited to proactive reclaim explicitly * created sc_swappiness helper to reduce chance of mis-use Changes since V4: * Fixed some initialization bugs by reverting back to a pointer for swappiness * Added some more caveats to the behavior of swappiness in documentation Changes since V3: * Added #define for MIN_SWAPPINESS and MAX_SWAPPINESS * Added explicit calls to mem_cgroup_swappiness Changes since V2: * No functional change * Used int consistently rather than a pointer Changes since V1: * Added documentation This patch proposes augmenting the memory.reclaim interface with a swappiness= argument that overrides the swappiness value for that instance of proactive reclaim. 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. Previously, this exact interface addition was proposed by Yosry[3]. In response, Roman proposed instead an interface to specify precise file/anon/slab reclaim amounts[4]. More recently Huan also proposed this as well[5] and others similarly questioned if this was the proper interface. Previous proposals sought to use this to allow proactive reclaimers to effectively perform a custom reclaim algorithm by issuing proactive reclaim with different settings to control file vs anon reclaim (e.g. to only reclaim anon from some applications). Responses argued that adjusting swappiness is a poor interface for custom reclaim. In contrast, I argue in favor of a swappiness setting not as a way to implement custom reclaim algorithms but rather to bias the balance of anon vs file due to differences of proactive vs reactive reclaim. In this context, swappiness is the existing interface for controlling this balance and this patch simply allows for it to be configured differently for proactive vs reactive reclaim. Specifying explicit amounts of anon vs file pages to reclaim feels inappropriate for this prupose. Proactive reclaimers are un-aware of the relative age of file vs anon for a cgroup which makes it difficult to manage proactive reclaim of different memory pools. A proactive reclaimer would need some amount of anon reclaim attempts separate from the amount of file reclaim attempts which seems brittle given that it's difficult to observe the impact. [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 [3]https://lore.kernel.org/linux-mm/CAJD7tkbDpyoODveCsnaqBBMZEkDvshXJmNdbk51yKSNgD7aGdg@mail.gmail.com/ [4]https://lore.kernel.org/linux-mm/YoPHtHXzpK51F%2F1Z@carbon/ [5]https://lore.kernel.org/lkml/20231108065818.19932-1-link@vivo.com/ Dan Schatzberg (2): mm: add defines for min/max swappiness mm: add swapiness= arg to memory.reclaim Documentation/admin-guide/cgroup-v2.rst | 18 +++++--- include/linux/swap.h | 5 ++- mm/memcontrol.c | 58 ++++++++++++++++++++----- mm/vmscan.c | 39 ++++++++++++----- 4 files changed, 90 insertions(+), 30 deletions(-)