From patchwork Tue Jul 23 23:31:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Finkel X-Patchwork-Id: 13740451 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 E818DC3DA63 for ; Tue, 23 Jul 2024 23:32:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CF8676B0083; Tue, 23 Jul 2024 19:32:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CA9B66B0088; Tue, 23 Jul 2024 19:32:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AAB356B0085; Tue, 23 Jul 2024 19:32:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 8A0726B0082 for ; Tue, 23 Jul 2024 19:32:01 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 419FD1A051F for ; Tue, 23 Jul 2024 23:32:01 +0000 (UTC) X-FDA: 82372617642.11.8E9DA33 Received: from m35-116.mailgun.net (m35-116.mailgun.net [69.72.35.116]) by imf19.hostedemail.com (Postfix) with ESMTP id 650351A001D for ; Tue, 23 Jul 2024 23:31:59 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=relay.vimeo.com header.s=mailo header.b=uyoFNzqF; spf=pass (imf19.hostedemail.com: domain of "bounce+ea57f2.9d2a1c-linux-mm=kvack.org@relay.vimeo.com" designates 69.72.35.116 as permitted sender) smtp.mailfrom="bounce+ea57f2.9d2a1c-linux-mm=kvack.org@relay.vimeo.com"; dmarc=pass (policy=reject) header.from=vimeo.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721777482; h=from:from:sender: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=NUJYaFjovU6GwUP38HkN+pKRx5PT/2fnNzR5UbDSWWk=; b=PjDv6JmDbGyHHtdFFrwYPExnqQpmbGm87ExAXfFGJnoUknm54tWbozD9vO1Bm/z0JJR/bx oHNdtZy35FYlzW4HPJCKrGjwU3y7hGBCVtMq/cBVjcKTaKnXAPQW5fJ9I5RSpPlgVvhqJH XjFcREWqgfNbmYvtY4+gBrvhm9oF2Q0= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=relay.vimeo.com header.s=mailo header.b=uyoFNzqF; spf=pass (imf19.hostedemail.com: domain of "bounce+ea57f2.9d2a1c-linux-mm=kvack.org@relay.vimeo.com" designates 69.72.35.116 as permitted sender) smtp.mailfrom="bounce+ea57f2.9d2a1c-linux-mm=kvack.org@relay.vimeo.com"; dmarc=pass (policy=reject) header.from=vimeo.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721777482; a=rsa-sha256; cv=none; b=pli7xOaErQTwNl6ry5ho5GpmPpgNKRwKqrRDQdQfHvMN5/SlAfJo9Ccv3lt5g9hNEZn7NN 2/eJwdq5xSOwKr10qOA5als1P02tMS+uJl5WNzjdptk76jvzEFH0lES3R1aMaaIzU5SWRt 4mLj0RZoiw/pGGWhMzM3kYSb2FDAI8A= DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=relay.vimeo.com; q=dns/txt; s=mailo; t=1721777518; x=1721784718; h=Content-Transfer-Encoding: MIME-Version: References: In-Reply-To: Message-Id: Date: Subject: Subject: Cc: To: To: From: From: Sender: Sender; bh=NUJYaFjovU6GwUP38HkN+pKRx5PT/2fnNzR5UbDSWWk=; b=uyoFNzqFLGf6BotlPhf6zgFgIR5MLn5sfVluYWjj2ZXhWSwZ8Os02KfgZfn0bUIbKdi1daufsar1qgro4XWmuYla6xd/Bhd6SLKI7tTo6e8Qxmguo6z7hkFTuWEgFZIkQTw6FPByV2w5L5SswJscInhm6TInqE4TNKzHe+MQGZU= X-Mailgun-Sending-Ip: 69.72.35.116 X-Mailgun-Sid: WyI5NTRmYiIsImxpbnV4LW1tQGt2YWNrLm9yZyIsIjlkMmExYyJd Received: from smtp.vimeo.com (215.71.185.35.bc.googleusercontent.com [35.185.71.215]) by f2beb1ae84b0 with SMTP id 66a03d6ecb8a873c2ea86260 (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Tue, 23 Jul 2024 23:31:58 GMT Received: from nutau (gke-sre-us-east1-main-4c35368b-n90a.c.vimeo-core.internal [10.56.27.205]) by smtp.vimeo.com (Postfix) with ESMTP id 43A4D64D5F; Tue, 23 Jul 2024 23:31:58 +0000 (UTC) Received: by nutau (Postfix, from userid 1001) id 25FC5B4133E; Tue, 23 Jul 2024 19:31:58 -0400 (EDT) From: David Finkel To: Muchun Song , Tejun Heo , Roman Gushchin , Andrew Morton Cc: core-services@vimeo.com, Jonathan Corbet , Michal Hocko , Shakeel Butt , Shuah Khan , Johannes Weiner , Zefan Li , cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, David Finkel , Waiman Long Subject: [PATCH 1/2] mm, memcg: cg2 memory{.swap,}.peak write handlers Date: Tue, 23 Jul 2024 19:31:48 -0400 Message-Id: <20240723233149.3226636-2-davidf@vimeo.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240723233149.3226636-1-davidf@vimeo.com> References: <20240723233149.3226636-1-davidf@vimeo.com> MIME-Version: 1.0 X-Stat-Signature: 7a58smpn75ro7b7ahs4twx1rsxeq8uwh X-Rspam-User: X-Rspamd-Queue-Id: 650351A001D X-Rspamd-Server: rspam02 X-HE-Tag: 1721777519-601053 X-HE-Meta: U2FsdGVkX1/R156EcqBO+db6wwtBpMbgsnrwCq6CP1XZqoWXw13DuBQzMrlwv3KRlxUg1OLc4iRy0ghJA8PpxpvV8B5loyzz4XfMcL6hnJVkT9DwQxf4VNKj5kVjrzGzMmMH3jGkCmgmyzvoJzuX5vFYiwCthOIiX4u0MPy0UCBM6NFoIZ/S+Qp74j942cBrFmtk+VR8qB3eLFEXWoVVPR/1VGgQm+jAwgXK5o2rWGydQKwnGcJeplezKbZUdLO2ke4X2Hbr7rfgoHkVkTGJrunyLX8mCwHNCM+5rgVBRyx/bovu5tbgcAB1mxJGFlUeVbVIPZ0LcojU6teJI5z51hKgeQzimvsoW/JpN1jWM6pk+9HYjnKJybHfeSZq675oUu/nLluzSgtHrhVMWx/PBmaZdE3Hb4Zhfve0ANzRqojMSJPz3hcR1WE3yjNbHYfoD+pYxFmrSvilcpC3D6gYczikzzDK+5jl2XD+cNEu3CzfCvmYcgUzrY57+6qmfYlthUkTGZnU5NRe85rJqAeBH7V/8nvrYHV5zWv/jV1uiEH3MWbjYeBNxrr/ORr3S/5a2ZhfLuoKzSv/iz/WtIa1yejAr53ho5FDq8nHDCr59/cU2V+gxtI7apVLsXJWRnwZGUvoQryZujnMRX/xlR/pVidpJaCRHVcWROYFY9pyoPoTo5zCuR+u46Ick42K+A/CvbhfCLA6ChemreJmEBkwZIgR3OV+Byd0OiRw6vfRa/6z2GxkVr9C538dmBGuIj0K3P2o8kB4qoT5D+14/8lpX42adH4diZPaQ+SLS3Taa+tYQwNnxLJzacSbk1+zNtqQxMeh4WfXNFhJ1P0+scKoYdtqSEJiEZWBKVJ4uB5nbx2bqTT/R3xZ+UR3vYhSuiGFugB7We1zdt97+47KfSKETwONoaYr67MA0CF5qLKq2+jMp/jONIPCw1DpN4zmu/9QThPfOG8fI8k14mP2F7l VK8eZM81 u3iNnFIq7Pu9QbaxPp0PQL/+8PpA2ulnKYnUvDgGRdx/UIL2IeFk0x2sdPW1/uzjdW8udrhq1rYWywj2n5r6EK3MP35E8faB0vUCxMYnklv+H/JmWqBkisGRN4qzgSH/T4UacoT8JX8I43py/psn+IrmTMxyPfFmqEUzxb3r1s1TAnF2MicKuHrWB+hmEF2E/Qx72WA15lqenX13TTfTe5KDACMfYgZcNk5EM0tjWQpCg4SqrUUzD0+jmYveTjqejHseKlhNMLQaPA1mc+tNkGvc0HjqZPy0xpA0mw3pN6wq9OVuHF397bNaXgptEXskqY37D4U2ZQAPwwFbcKwLot+Jzx2lvH+6a4tN1+ChO+nSfrGqHpAc+YYopJweslTXFSbIk0bKg2kNLT4xaIS91R8keqJAwL/gePQI2N4yomhNjr6O6I4m8XcDKkmhy5o5kaIZkbJBhzmTKO0jkYanQ7XwbfKSt78sd/u2/ITMX9CYN253cdDiFXPKjTjOqY7WYDid5E2pOI5aOE0jKSpbfEt0syLaTUBk8+bZ6huCdXr+s9q5C3qmHVIKO62Oi1kEgbxHD 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: Other mechanisms for querying the peak memory usage of either a process or v1 memory cgroup allow for resetting the high watermark. Restore parity with those mechanisms, but with a less racy API. For example: - Any write to memory.max_usage_in_bytes in a cgroup v1 mount resets the high watermark. - writing "5" to the clear_refs pseudo-file in a processes's proc directory resets the peak RSS. This change is an evolution of a previous patch, which mostly copied the cgroup v1 behavior, however, there were concerns about races/ownership issues with a global reset, so instead this change makes the reset filedescriptor-local. Writing a specific string to the memory.peak and memory.swap.peak pseudo-files reset the high watermark to the current usage for subsequent reads through that same fd. Notably, following Johannes's suggestion, this implementation moves the O(fds that have written) behavior onto the fd write(2) path. Instead, on the page-allocation path, we simply add one additional watermark to conditionally bump per-hierarchy level in the page-counter. Additionally, this takes Longman's suggestion of nesting the page-charging-path checks for the two watermarks to reduce the number of common-case comparisons. This behavior is particularly useful for work scheduling systems that need to track memory usage of worker processes/cgroups per-work-item. Since memory can't be squeezed like CPU can (the OOM-killer has opinions), these systems need to track the peak memory usage to compute system/container fullness when binpacking workitems. Most notably, Vimeo's use-case involves a system that's doing global binpacking across many Kubernetes pods/containers, and while we can use PSI for some local decisions about overload, we strive to avoid packing workloads too tightly in the first place. To facilitate this, we track the peak memory usage. However, since we run with long-lived workers (to amortize startup costs) we need a way to track the high watermark while a work-item is executing. Polling runs the risk of missing short spikes that last for timescales below the polling interval, and peak memory tracking at the cgroup level is otherwise perfect for this use-case. As this data is used to ensure that binpacked work ends up with sufficient headroom, this use-case mostly avoids the inaccuracies surrounding reclaimable memory. Suggested-by: Johannes Weiner Suggested-by: Waiman Long Signed-off-by: David Finkel --- Documentation/admin-guide/cgroup-v2.rst | 26 ++++-- include/linux/cgroup-defs.h | 5 + include/linux/cgroup.h | 3 + include/linux/memcontrol.h | 5 + include/linux/page_counter.h | 6 +- kernel/cgroup/cgroup-internal.h | 2 + kernel/cgroup/cgroup.c | 7 ++ mm/memcontrol.c | 117 ++++++++++++++++++++++-- mm/page_counter.c | 30 ++++-- 9 files changed, 174 insertions(+), 27 deletions(-) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst index 86311c2907cd3..01554cf6e55b4 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1333,11 +1333,16 @@ The following nested keys are defined. all the existing limitations and potential future extensions. memory.peak - A read-only single value file which exists on non-root - cgroups. + A read-write single value file which exists on non-root cgroups. + + The max memory usage recorded for the cgroup and its descendants since + either the creation of the cgroup or the most recent reset for that FD. - The max memory usage recorded for the cgroup and its - descendants since the creation of the cgroup. + A write of the string "reset" to this file resets it to the + current memory usage for subsequent reads through the same + file descriptor. + Attempts to write any other non-empty string will return EINVAL + (modulo leading and trailing whitespace). memory.oom.group A read-write single value file which exists on non-root @@ -1663,11 +1668,16 @@ The following nested keys are defined. Healthy workloads are not expected to reach this limit. memory.swap.peak - A read-only single value file which exists on non-root - cgroups. + A read-write single value file which exists on non-root cgroups. + + The max swap usage recorded for the cgroup and its descendants since + the creation of the cgroup or the most recent reset for that FD. - The max swap usage recorded for the cgroup and its - descendants since the creation of the cgroup. + A write of the string "reset" to this file resets it to the + current memory usage for subsequent reads through the same + file descriptor. + Attempts to write any other non-empty string will return EINVAL + (modulo leading and trailing whitespace). memory.swap.max A read-write single value file which exists on non-root diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h index ae04035b6cbe5..2188ea76ab327 100644 --- a/include/linux/cgroup-defs.h +++ b/include/linux/cgroup-defs.h @@ -775,6 +775,11 @@ struct cgroup_subsys { extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem; +struct cgroup_of_peak { + long value; + struct list_head list; +}; + /** * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups * @tsk: target task diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index c60ba0ab14627..3e0563753cc3e 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -854,4 +855,6 @@ static inline void cgroup_bpf_put(struct cgroup *cgrp) {} struct cgroup *task_get_cgroup1(struct task_struct *tsk, int hierarchy_id); +struct cgroup_of_peak *of_peak(struct kernfs_open_file *of); + #endif /* _LINUX_CGROUP_H */ diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 7e2eb091049a0..4fa4f0e931d26 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -192,6 +192,11 @@ struct mem_cgroup { struct page_counter memsw; /* v1 only */ }; + /* registered local peak watchers */ + struct list_head memory_peaks; + struct list_head swap_peaks; + spinlock_t peaks_lock; + /* Range enforcement for interrupt charges */ struct work_struct high_work; diff --git a/include/linux/page_counter.h b/include/linux/page_counter.h index 904c52f97284f..860f313182e77 100644 --- a/include/linux/page_counter.h +++ b/include/linux/page_counter.h @@ -26,6 +26,7 @@ struct page_counter { atomic_long_t children_low_usage; unsigned long watermark; + unsigned long local_watermark; /* track min of fd-local resets */ unsigned long failcnt; /* Keep all the read most fields in a separete cacheline. */ @@ -78,7 +79,10 @@ int page_counter_memparse(const char *buf, const char *max, static inline void page_counter_reset_watermark(struct page_counter *counter) { - counter->watermark = page_counter_read(counter); + unsigned long usage = page_counter_read(counter); + + counter->watermark = usage; + counter->local_watermark = usage; } void page_counter_calculate_protection(struct page_counter *root, diff --git a/kernel/cgroup/cgroup-internal.h b/kernel/cgroup/cgroup-internal.h index 520b90dd97eca..c964dd7ff967a 100644 --- a/kernel/cgroup/cgroup-internal.h +++ b/kernel/cgroup/cgroup-internal.h @@ -81,6 +81,8 @@ struct cgroup_file_ctx { struct { struct cgroup_pidlist *pidlist; } procs1; + + struct cgroup_of_peak peak; }; /* diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index c8e4b62b436a4..0a97cb2ef1245 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -1972,6 +1972,13 @@ static int cgroup2_parse_param(struct fs_context *fc, struct fs_parameter *param return -EINVAL; } +struct cgroup_of_peak *of_peak(struct kernfs_open_file *of) +{ + struct cgroup_file_ctx *ctx = of->priv; + + return &ctx->peak; +} + static void apply_cgroup_root_flags(unsigned int root_flags) { if (current->nsproxy->cgroup_ns == &init_cgroup_ns) { diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 9603717886877..2176a2da1aa83 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -25,6 +25,7 @@ * Copyright (C) 2020 Alibaba, Inc, Alex Shi */ +#include #include #include #include @@ -41,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -3558,6 +3560,9 @@ static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent) INIT_WORK(&memcg->high_work, high_work_func); vmpressure_init(&memcg->vmpressure); + INIT_LIST_HEAD(&memcg->memory_peaks); + INIT_LIST_HEAD(&memcg->swap_peaks); + spin_lock_init(&memcg->peaks_lock); memcg->socket_pressure = jiffies; memcg1_memcg_init(memcg); memcg->kmemcg_id = -1; @@ -3950,12 +3955,90 @@ static u64 memory_current_read(struct cgroup_subsys_state *css, return (u64)page_counter_read(&memcg->memory) * PAGE_SIZE; } -static u64 memory_peak_read(struct cgroup_subsys_state *css, - struct cftype *cft) +static int peak_show(struct seq_file *sf, void *v, struct page_counter *pc) { - struct mem_cgroup *memcg = mem_cgroup_from_css(css); + struct cgroup_of_peak *ofp = of_peak(sf->private); + s64 fd_peak = ofp->value, peak; + + /* User wants global or local peak? */ + if (fd_peak == -1) + peak = pc->watermark; + else + peak = max(fd_peak, (s64)pc->local_watermark); + + seq_printf(sf, "%lld\n", peak * PAGE_SIZE); + return 0; +} - return (u64)memcg->memory.watermark * PAGE_SIZE; +static int memory_peak_show(struct seq_file *sf, void *v) +{ + struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf)); + + return peak_show(sf, v, &memcg->memory); +} + +static int peak_open(struct kernfs_open_file *of) +{ + struct cgroup_of_peak *ofp = of_peak(of); + + ofp->value = -1; + return 0; +} + +static void peak_release(struct kernfs_open_file *of) +{ + struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); + struct cgroup_of_peak *ofp = of_peak(of); + + if (ofp->value == -1) { + /* fast path (no writes on this fd) */ + return; + } + spin_lock(&memcg->peaks_lock); + list_del(&ofp->list); + spin_unlock(&memcg->peaks_lock); +} + +static ssize_t peak_write(struct kernfs_open_file *of, char *buf, size_t nbytes, + loff_t off, struct page_counter *pc, + struct list_head *watchers) +{ + unsigned long usage; + struct cgroup_of_peak *peer_ctx; + struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); + struct cgroup_of_peak *ofp = of_peak(of); + + buf = strstrip(buf); + /* Only allow "reset" to keep the API clear */ + if (strcmp(buf, "reset")) + return -EINVAL; + + spin_lock(&memcg->peaks_lock); + + pc->local_watermark = page_counter_read(pc); + usage = pc->local_watermark; + + list_for_each_entry(peer_ctx, watchers, list) + if (usage > peer_ctx->value) + peer_ctx->value = usage; + + /* initial write, register watcher */ + if (ofp->value == -1) + list_add(&ofp->list, watchers); + + ofp->value = usage; + spin_unlock(&memcg->peaks_lock); + + return nbytes; +} + +static ssize_t memory_peak_write(struct kernfs_open_file *of, char *buf, + size_t nbytes, loff_t off) +{ + struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); + + return peak_write(of, buf, nbytes, off, &memcg->memory, + &memcg->memory_peaks); } static int memory_min_show(struct seq_file *m, void *v) @@ -4307,7 +4390,10 @@ static struct cftype memory_files[] = { { .name = "peak", .flags = CFTYPE_NOT_ON_ROOT, - .read_u64 = memory_peak_read, + .open = peak_open, + .release = peak_release, + .seq_show = memory_peak_show, + .write = memory_peak_write, }, { .name = "min", @@ -5099,12 +5185,20 @@ static u64 swap_current_read(struct cgroup_subsys_state *css, return (u64)page_counter_read(&memcg->swap) * PAGE_SIZE; } -static u64 swap_peak_read(struct cgroup_subsys_state *css, - struct cftype *cft) +static int swap_peak_show(struct seq_file *sf, void *v) { - struct mem_cgroup *memcg = mem_cgroup_from_css(css); + struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf)); + + return peak_show(sf, v, &memcg->swap); +} + +static ssize_t swap_peak_write(struct kernfs_open_file *of, char *buf, + size_t nbytes, loff_t off) +{ + struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); - return (u64)memcg->swap.watermark * PAGE_SIZE; + return peak_write(of, buf, nbytes, off, &memcg->swap, + &memcg->swap_peaks); } static int swap_high_show(struct seq_file *m, void *v) @@ -5188,7 +5282,10 @@ static struct cftype swap_files[] = { { .name = "swap.peak", .flags = CFTYPE_NOT_ON_ROOT, - .read_u64 = swap_peak_read, + .open = peak_open, + .release = peak_release, + .seq_show = swap_peak_show, + .write = swap_peak_write, }, { .name = "swap.events", diff --git a/mm/page_counter.c b/mm/page_counter.c index 0153f5bb31611..ad9bdde5d5d20 100644 --- a/mm/page_counter.c +++ b/mm/page_counter.c @@ -79,9 +79,22 @@ void page_counter_charge(struct page_counter *counter, unsigned long nr_pages) /* * This is indeed racy, but we can live with some * inaccuracy in the watermark. + * + * Notably, we have two watermarks to allow for both a globally + * visible peak and one that can be reset at a smaller scope. + * + * Since we reset both watermarks when the global reset occurs, + * we can guarantee that watermark >= local_watermark, so we + * don't need to do both comparisons every time. + * + * On systems with branch predictors, the inner condition should + * be almost free. */ - if (new > READ_ONCE(c->watermark)) - WRITE_ONCE(c->watermark, new); + if (new > READ_ONCE(c->local_watermark)) { + WRITE_ONCE(c->local_watermark, new); + if (new > READ_ONCE(c->watermark)) + WRITE_ONCE(c->watermark, new); + } } } @@ -129,12 +142,13 @@ bool page_counter_try_charge(struct page_counter *counter, goto failed; } propagate_protected_usage(c, new); - /* - * Just like with failcnt, we can live with some - * inaccuracy in the watermark. - */ - if (new > READ_ONCE(c->watermark)) - WRITE_ONCE(c->watermark, new); + + /* see comment on page_counter_charge */ + if (new > READ_ONCE(c->local_watermark)) { + WRITE_ONCE(c->local_watermark, new); + if (new > READ_ONCE(c->watermark)) + WRITE_ONCE(c->watermark, new); + } } return true;