From patchwork Fri Feb 11 06:49:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 12742925 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 28719C433F5 for ; Fri, 11 Feb 2022 06:49:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 99F426B0075; Fri, 11 Feb 2022 01:49:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 94DA66B0078; Fri, 11 Feb 2022 01:49:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7EEF16B007B; Fri, 11 Feb 2022 01:49:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0242.hostedemail.com [216.40.44.242]) by kanga.kvack.org (Postfix) with ESMTP id 56F3E6B0075 for ; Fri, 11 Feb 2022 01:49:38 -0500 (EST) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id EC9AD181AC9C6 for ; Fri, 11 Feb 2022 06:49:37 +0000 (UTC) X-FDA: 79129573194.18.B0C4E4F Received: from mail-ot1-f73.google.com (mail-ot1-f73.google.com [209.85.210.73]) by imf28.hostedemail.com (Postfix) with ESMTP id 7BC5DC0002 for ; Fri, 11 Feb 2022 06:49:37 +0000 (UTC) Received: by mail-ot1-f73.google.com with SMTP id j2-20020a9d7d82000000b005a12a0fb4b0so4843344otn.5 for ; Thu, 10 Feb 2022 22:49:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=nDUa4nh0VfMB33PiEe/+b8nW+DWgiuCX8+o5RVf1oHI=; b=ThzYMOIDPOykUabiB3RlRAijrJSIHNXOSUgcmlKL4c2HDkyKp7qaJwrCrOB5Cj7/Sn b+6g0BlcsH3JRNO1HyBb0LIliKk840gqIqgFjHZyfba+0Rzygu2PqymxQgM8gR9yQfI2 UsYHiVjl75cre377AqUX4YDmZQbFTuUf3922omgktt3jvQUKmwr3eB7X7/Z19Ju1H4ex PjrES1T9lJwMXV5jyWjXRhmiR+2EhxtUnHDoQdtmgniawP0Vl+SgjVunv4BNLBp8DepV bZ7c1Ro3WZr7RdhwOGLkLCIGOQPDT8YXV3AJpris0GtceJxuIoOltOYgpfluZ3rotD5S G5vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=nDUa4nh0VfMB33PiEe/+b8nW+DWgiuCX8+o5RVf1oHI=; b=CWwXSfoqc+YU1EOn5wePtpTYt+bScOubweRWtEYwlSJtACuiYZQtVbxmPfLMoHZpNm WOVh5tb5d3y06Y1FRRTKzWTIGyCiMe7Q42bgo0ILtuhM4A222aiBifR30OUcDBW9fWbf 69Gd3/NX8Dg+fIcnwLsb1qhMis4wqdx1+lnatWCLdQ9eXVA09qca33RNEZ0fCUQTR1tM wMUj0HFuX99XC1U/xEAkYRrdVOQx6fg5koV8eT4XgyXVIG6WfpzceY66bAkCFfhR7oD9 HjT/KfI5dkF6lTcBUC1ax12iuTSADY2aKSl1puYwqqTDlqCe2FXE+OE+hCLfnUwA+eD3 z4dw== X-Gm-Message-State: AOAM531CFIRyigGvTh0j2xgKoM7IuPY2URW8k/SXr3lCIf3ewTq2coYV 6hcV54o+g3mH+UdhIG5JM38ru+BrXqkgVA== X-Google-Smtp-Source: ABdhPJx5gc39Is8agdmSFvKE0SI6NhFXD2uOpMQyKeFvk0V2rSxi71LnZdIohEe32baoaxn1gC4VmeeF0Z5JzQ== X-Received: from shakeelb.svl.corp.google.com ([2620:15c:2cd:202:9a07:ef1a:2fee:57f1]) (user=shakeelb job=sendgmr) by 2002:a05:6870:6288:: with SMTP id s8mr53946oan.269.1644562176594; Thu, 10 Feb 2022 22:49:36 -0800 (PST) Date: Thu, 10 Feb 2022 22:49:14 -0800 In-Reply-To: <20220211064917.2028469-1-shakeelb@google.com> Message-Id: <20220211064917.2028469-2-shakeelb@google.com> Mime-Version: 1.0 References: <20220211064917.2028469-1-shakeelb@google.com> X-Mailer: git-send-email 2.35.1.265.g69c8d7142f-goog Subject: [PATCH v2 1/4] memcg: refactor mem_cgroup_oom From: Shakeel Butt To: Johannes Weiner , Michal Hocko , Roman Gushchin Cc: Chris Down , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Shakeel Butt X-Rspamd-Queue-Id: 7BC5DC0002 X-Rspam-User: Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=ThzYMOID; spf=pass (imf28.hostedemail.com: domain of 3AAcGYggKCD0rgZjddkafnnfkd.bnlkhmtw-lljuZbj.nqf@flex--shakeelb.bounces.google.com designates 209.85.210.73 as permitted sender) smtp.mailfrom=3AAcGYggKCD0rgZjddkafnnfkd.bnlkhmtw-lljuZbj.nqf@flex--shakeelb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: xm5zh5tyicaihsj87jopjrog918rne16 X-Rspamd-Server: rspam03 X-HE-Tag: 1644562177-513710 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: The function mem_cgroup_oom returns enum which has four possible values but the caller does not care about such values and only cares if the return value is OOM_SUCCESS or not. So, remove the enum altogether and make mem_cgroup_oom returns a simple bool. Signed-off-by: Shakeel Butt Reviewed-by: Roman Gushchin --- Changes since v1: - Added comment for mem_cgroup_oom as suggested by Roman mm/memcontrol.c | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index a0e9d9f12cf5..f12e489ba9b8 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1795,20 +1795,16 @@ static void memcg_oom_recover(struct mem_cgroup *memcg) __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg); } -enum oom_status { - OOM_SUCCESS, - OOM_FAILED, - OOM_ASYNC, - OOM_SKIPPED -}; - -static enum oom_status mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order) +/* + * Returns true if successfully killed one or more processes. Though in some + * corner cases it can return true even without killing any process. + */ +static bool mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order) { - enum oom_status ret; - bool locked; + bool locked, ret; if (order > PAGE_ALLOC_COSTLY_ORDER) - return OOM_SKIPPED; + return false; memcg_memory_event(memcg, MEMCG_OOM); @@ -1831,14 +1827,13 @@ static enum oom_status mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int * victim and then we have to bail out from the charge path. */ if (memcg->oom_kill_disable) { - if (!current->in_user_fault) - return OOM_SKIPPED; - css_get(&memcg->css); - current->memcg_in_oom = memcg; - current->memcg_oom_gfp_mask = mask; - current->memcg_oom_order = order; - - return OOM_ASYNC; + if (current->in_user_fault) { + css_get(&memcg->css); + current->memcg_in_oom = memcg; + current->memcg_oom_gfp_mask = mask; + current->memcg_oom_order = order; + } + return false; } mem_cgroup_mark_under_oom(memcg); @@ -1849,10 +1844,7 @@ static enum oom_status mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int mem_cgroup_oom_notify(memcg); mem_cgroup_unmark_under_oom(memcg); - if (mem_cgroup_out_of_memory(memcg, mask, order)) - ret = OOM_SUCCESS; - else - ret = OOM_FAILED; + ret = mem_cgroup_out_of_memory(memcg, mask, order); if (locked) mem_cgroup_oom_unlock(memcg); @@ -2545,7 +2537,6 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, int nr_retries = MAX_RECLAIM_RETRIES; struct mem_cgroup *mem_over_limit; struct page_counter *counter; - enum oom_status oom_status; unsigned long nr_reclaimed; bool passed_oom = false; bool may_swap = true; @@ -2648,9 +2639,8 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, * a forward progress or bypass the charge if the oom killer * couldn't make any progress. */ - oom_status = mem_cgroup_oom(mem_over_limit, gfp_mask, - get_order(nr_pages * PAGE_SIZE)); - if (oom_status == OOM_SUCCESS) { + if (mem_cgroup_oom(mem_over_limit, gfp_mask, + get_order(nr_pages * PAGE_SIZE))) { passed_oom = true; nr_retries = MAX_RECLAIM_RETRIES; goto retry; From patchwork Fri Feb 11 06:49:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 12742926 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 90ED4C433EF for ; Fri, 11 Feb 2022 06:49:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 24AF66B0078; Fri, 11 Feb 2022 01:49:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1FB5E6B007B; Fri, 11 Feb 2022 01:49:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0C2D56B007D; Fri, 11 Feb 2022 01:49:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0195.hostedemail.com [216.40.44.195]) by kanga.kvack.org (Postfix) with ESMTP id F26976B0078 for ; Fri, 11 Feb 2022 01:49:44 -0500 (EST) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id ADE6B181AC9C6 for ; Fri, 11 Feb 2022 06:49:44 +0000 (UTC) X-FDA: 79129573488.11.2758CF2 Received: from mail-oo1-f74.google.com (mail-oo1-f74.google.com [209.85.161.74]) by imf19.hostedemail.com (Postfix) with ESMTP id 432CF1A0006 for ; Fri, 11 Feb 2022 06:49:44 +0000 (UTC) Received: by mail-oo1-f74.google.com with SMTP id r12-20020a4aea8c000000b002fd5bc5d365so5073859ooh.18 for ; Thu, 10 Feb 2022 22:49:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=Gp3MzKjywklApxzwU7bA0E+IdrMiraVuIp3bbtkpPwY=; b=pZFidtCwL5VMsnJRP/9b9wHwk02Whf8q/difXoIKsdZUTOmtfykRIOjUqweW4jqnAY 8AEQsIf7BJ3qv1w8Ny3VBvWoNOmFTRKN3MfOMCY09iJzuIfqwdWFh7mMh1dOoqJ4ANRG kk6Yyu5VN4IsdIxQHdbPDB8jrS384vnBeQ0zTXmINULyixuihlw86O9M7+bkOQkq5FCF Lrhhz8wMvYt8tbAjV4cuO7v+/OfmJKzHuP1Xt8E8P3AuoqUne3R/7i1gblDPs3CiLF4I Lu2aeoDUQLQNqiMdUjzemZ+fOLmEn6qSvVpIVsT10mL7z1OHdnOzDR/O/QikLdqga1Ns Ahuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Gp3MzKjywklApxzwU7bA0E+IdrMiraVuIp3bbtkpPwY=; b=41846UTQ8pxRC43mWVnBhOgV0/mtt50ebJ5eXyR76I/WIimej6zojaU+2WOg1RzFWB 7Q/xT1zzmxq2JD12ojnQ+IYGttIeetoQK3/NRJ9L00RmyWdkO7skZFT4+jNogOyLz/j/ gelTaPwr9buUEf6qXGzltPb8ZG8E8jPcUbS6QU1hG9okluSWlGSIjs1khvdzkYRqBRNV 0bu/vXoTGRWmATjnY2JLS50cry3A02HLeZMSHI7csArwmW/TX4t9MHCRMzFlpoT5mOzU COJ9ndCkb03TZC/5GmwOqrarsUJ4HUV3gOK+n4VNYEnwUolWx8kSn/x3WEociPdPGP1l 8ObQ== X-Gm-Message-State: AOAM53327/IhxJJnuqF7WXpUHmwCg+bj+YbdGiUj4h547AdNiD3qL15W TyZyn+sU8IXjskqYQ/Bwydwlqd8jHiL6Bg== X-Google-Smtp-Source: ABdhPJw3ASDB+yZZwSrQEVhj4dACHJ86v54Lv8O291KKTat+VzaAivyC/3ZBnBIy1wraZJwv+qm5+0amn6FRHw== X-Received: from shakeelb.svl.corp.google.com ([2620:15c:2cd:202:9a07:ef1a:2fee:57f1]) (user=shakeelb job=sendgmr) by 2002:a05:6808:1918:: with SMTP id bf24mr473270oib.253.1644562183473; Thu, 10 Feb 2022 22:49:43 -0800 (PST) Date: Thu, 10 Feb 2022 22:49:15 -0800 In-Reply-To: <20220211064917.2028469-1-shakeelb@google.com> Message-Id: <20220211064917.2028469-3-shakeelb@google.com> Mime-Version: 1.0 References: <20220211064917.2028469-1-shakeelb@google.com> X-Mailer: git-send-email 2.35.1.265.g69c8d7142f-goog Subject: [PATCH v2 2/4] memcg: unify force charging conditions From: Shakeel Butt To: Johannes Weiner , Michal Hocko , Roman Gushchin Cc: Chris Down , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Shakeel Butt X-Rspamd-Queue-Id: 432CF1A0006 X-Stat-Signature: b3hit47obmyq3xt7e36mimhd4npq84r8 X-Rspam-User: Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=pZFidtCw; spf=pass (imf19.hostedemail.com: domain of 3BwcGYggKCEQyngqkkrhmuumrk.iusrot03-ssq1giq.uxm@flex--shakeelb.bounces.google.com designates 209.85.161.74 as permitted sender) smtp.mailfrom=3BwcGYggKCEQyngqkkrhmuumrk.iusrot03-ssq1giq.uxm@flex--shakeelb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam06 X-HE-Tag: 1644562184-676178 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: Currently the kernel force charges the allocations which have __GFP_HIGH flag without triggering the memory reclaim. __GFP_HIGH indicates that the caller is high priority and since commit 869712fd3de5 ("mm: memcontrol: fix network errors from failing __GFP_ATOMIC charges") the kernel lets such allocations do force charging. Please note that __GFP_ATOMIC has been replaced by __GFP_HIGH. __GFP_HIGH does not tell if the caller can block or can trigger reclaim. There are separate checks to determine that. So, there is no need to skip reclaiming for __GFP_HIGH allocations. So, handle __GFP_HIGH together with __GFP_NOFAIL which also does force charging. Please note that this is a noop change as there are no __GFP_HIGH allocators in the kernel which also have __GFP_ACCOUNT (or SLAB_ACCOUNT) and does not allow reclaim for now. Signed-off-by: Shakeel Butt Reviewed-by: Roman Gushchin --- Changes since v1: - None mm/memcontrol.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index f12e489ba9b8..292b0b99a2c7 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2564,15 +2564,6 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, goto retry; } - /* - * Memcg doesn't have a dedicated reserve for atomic - * allocations. But like the global atomic pool, we need to - * put the burden of reclaim on regular allocation requests - * and let these go through as privileged allocations. - */ - if (gfp_mask & __GFP_HIGH) - goto force; - /* * Prevent unbounded recursion when reclaim operations need to * allocate memory. This might exceed the limits temporarily, @@ -2646,7 +2637,13 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, goto retry; } nomem: - if (!(gfp_mask & __GFP_NOFAIL)) + /* + * Memcg doesn't have a dedicated reserve for atomic + * allocations. But like the global atomic pool, we need to + * put the burden of reclaim on regular allocation requests + * and let these go through as privileged allocations. + */ + if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH))) return -ENOMEM; force: /* From patchwork Fri Feb 11 06:49:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 12742927 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 BAB39C433EF for ; Fri, 11 Feb 2022 06:49:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 24ACB6B007B; Fri, 11 Feb 2022 01:49:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1FA8D6B007D; Fri, 11 Feb 2022 01:49:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 09BC96B007E; Fri, 11 Feb 2022 01:49:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0235.hostedemail.com [216.40.44.235]) by kanga.kvack.org (Postfix) with ESMTP id EFF956B007B for ; Fri, 11 Feb 2022 01:49:52 -0500 (EST) Received: from smtpin31.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id A1DB6901C3 for ; Fri, 11 Feb 2022 06:49:52 +0000 (UTC) X-FDA: 79129573824.31.49D9D52 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf08.hostedemail.com (Postfix) with ESMTP id 3A5BD160008 for ; Fri, 11 Feb 2022 06:49:52 +0000 (UTC) Received: by mail-yb1-f202.google.com with SMTP id b12-20020a056902030c00b0061d720e274aso16944024ybs.20 for ; Thu, 10 Feb 2022 22:49:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=wSeYGbecgd4wcC85bjYc+Wf+OozOTN6ZFdjrqmxfx+w=; b=kgBNOs1WBseAbsRFMyzbv5MpUWXKAyzUAXftaQWeiVTYBMreznuOjzljFOI2hrN1yk aHQaCo+UhNqqwVRVTPBr7NM6NjjOEyVjuGyn+/MKv8P8ZNfn9x9nZJdD56raIihE0qyl cW+hk+/99lgXtJF1xLanqpAjJOO5g1YltZrYCaTDb+a2pqp7OAkhVEBR4eWnfXN+2ezT bDpg5N6KoEKtlQPiLB0AZhqLQR2bCasbsdIbjhbtS7a5XPCObdJ8Qbf6KIpdngCRFy4O yT/VmCbaWYaftIYqDgJzRfXpr9DXGJdeGzjvHXl9HegA/BTq6/R6BBz32ycj+VoCOw/2 UF1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=wSeYGbecgd4wcC85bjYc+Wf+OozOTN6ZFdjrqmxfx+w=; b=Mm0QP9T4AJdBoZv6LCSH6inON1qZq8eEv8aBI/JGjhwwtI7BNfDHc3ugCvXlea3gso 4O30xlpfFSLd5sFOZHtubtSVVrp5Q2+JSofbzo8mDk3IRBAn82OLioe5M4CoOUGoKh7I z1vBmAysm61aL2wuR6hy3enqHerhIWiqsFT0xNkTAyAJx1D/BnKrFgE71R/jcbAQi4eT NU+Jp+Y1zZx57Exg3noNzylHiycKkQmuM7B37f68WnXi8uSLqZPoxP6u4L/8GDI9g98M cUoRpgdfpLEBOEvHnB7XGOSp17dEHKjZQcDa3JQqvuYvR+XEKVFFkBF9j+SJ4+jmJ2v4 /AmA== X-Gm-Message-State: AOAM533E3gTSVcFA/L05M3FYVlJscz16he5adUBwVeY4Ic24wIinb7/k mkNpcwqD1uiBQm1L7EJYvFms7kzyGyJjEA== X-Google-Smtp-Source: ABdhPJwKpmkurBJZDlS/fuYlvpdKg9WPvDaf+agCb8KXEOQoRBet3SsqP1VSya2bsK2552ZipDH4CRMFgYBezw== X-Received: from shakeelb.svl.corp.google.com ([2620:15c:2cd:202:9a07:ef1a:2fee:57f1]) (user=shakeelb job=sendgmr) by 2002:a81:a304:: with SMTP id a4mr337510ywh.516.1644562191536; Thu, 10 Feb 2022 22:49:51 -0800 (PST) Date: Thu, 10 Feb 2022 22:49:16 -0800 In-Reply-To: <20220211064917.2028469-1-shakeelb@google.com> Message-Id: <20220211064917.2028469-4-shakeelb@google.com> Mime-Version: 1.0 References: <20220211064917.2028469-1-shakeelb@google.com> X-Mailer: git-send-email 2.35.1.265.g69c8d7142f-goog Subject: [PATCH v2 3/4] selftests: memcg: test high limit for single entry allocation From: Shakeel Butt To: Johannes Weiner , Michal Hocko , Roman Gushchin Cc: Chris Down , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Shakeel Butt Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=kgBNOs1W; spf=pass (imf08.hostedemail.com: domain of 3DwcGYggKCEw6voysszpu22uzs.q20zw18B-00y9oqy.25u@flex--shakeelb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3DwcGYggKCEw6voysszpu22uzs.q20zw18B-00y9oqy.25u@flex--shakeelb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam07 X-Rspam-User: X-Rspamd-Queue-Id: 3A5BD160008 X-Stat-Signature: b1q9pxzjsty9qu1nnu5u3qcik79qy5oy X-HE-Tag: 1644562192-545711 X-Bogosity: Ham, tests=bogofilter, spamicity=0.006606, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Test the enforcement of memory.high limit for large amount of memory allocation within a single kernel entry. There are valid use-cases where the application can trigger large amount of memory allocation within a single syscall e.g. mlock() or mmap(MAP_POPULATE). Make sure memory.high limit enforcement works for such use-cases. Signed-off-by: Shakeel Butt Reviewed-by: Roman Gushchin --- Changes since v1: - None tools/testing/selftests/cgroup/cgroup_util.c | 15 +++- tools/testing/selftests/cgroup/cgroup_util.h | 1 + .../selftests/cgroup/test_memcontrol.c | 78 +++++++++++++++++++ 3 files changed, 91 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/cgroup/cgroup_util.c b/tools/testing/selftests/cgroup/cgroup_util.c index 0cf7e90c0052..dbaa7aabbb4a 100644 --- a/tools/testing/selftests/cgroup/cgroup_util.c +++ b/tools/testing/selftests/cgroup/cgroup_util.c @@ -583,7 +583,7 @@ int clone_into_cgroup_run_wait(const char *cgroup) return 0; } -int cg_prepare_for_wait(const char *cgroup) +static int __prepare_for_wait(const char *cgroup, const char *filename) { int fd, ret = -1; @@ -591,8 +591,7 @@ int cg_prepare_for_wait(const char *cgroup) if (fd == -1) return fd; - ret = inotify_add_watch(fd, cg_control(cgroup, "cgroup.events"), - IN_MODIFY); + ret = inotify_add_watch(fd, cg_control(cgroup, filename), IN_MODIFY); if (ret == -1) { close(fd); fd = -1; @@ -601,6 +600,16 @@ int cg_prepare_for_wait(const char *cgroup) return fd; } +int cg_prepare_for_wait(const char *cgroup) +{ + return __prepare_for_wait(cgroup, "cgroup.events"); +} + +int memcg_prepare_for_wait(const char *cgroup) +{ + return __prepare_for_wait(cgroup, "memory.events"); +} + int cg_wait_for(int fd) { int ret = -1; diff --git a/tools/testing/selftests/cgroup/cgroup_util.h b/tools/testing/selftests/cgroup/cgroup_util.h index 4f66d10626d2..628738532ac9 100644 --- a/tools/testing/selftests/cgroup/cgroup_util.h +++ b/tools/testing/selftests/cgroup/cgroup_util.h @@ -55,4 +55,5 @@ extern int clone_reap(pid_t pid, int options); extern int clone_into_cgroup_run_wait(const char *cgroup); extern int dirfd_open_opath(const char *dir); extern int cg_prepare_for_wait(const char *cgroup); +extern int memcg_prepare_for_wait(const char *cgroup); extern int cg_wait_for(int fd); diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c index c19a97dd02d4..36ccf2322e21 100644 --- a/tools/testing/selftests/cgroup/test_memcontrol.c +++ b/tools/testing/selftests/cgroup/test_memcontrol.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "../kselftest.h" #include "cgroup_util.h" @@ -628,6 +629,82 @@ static int test_memcg_high(const char *root) return ret; } +static int alloc_anon_mlock(const char *cgroup, void *arg) +{ + size_t size = (size_t)arg; + void *buf; + + buf = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, + 0, 0); + if (buf == MAP_FAILED) + return -1; + + mlock(buf, size); + munmap(buf, size); + return 0; +} + +/* + * This test checks that memory.high is able to throttle big single shot + * allocation i.e. large allocation within one kernel entry. + */ +static int test_memcg_high_sync(const char *root) +{ + int ret = KSFT_FAIL, pid, fd = -1; + char *memcg; + long pre_high, pre_max; + long post_high, post_max; + + memcg = cg_name(root, "memcg_test"); + if (!memcg) + goto cleanup; + + if (cg_create(memcg)) + goto cleanup; + + pre_high = cg_read_key_long(memcg, "memory.events", "high "); + pre_max = cg_read_key_long(memcg, "memory.events", "max "); + if (pre_high < 0 || pre_max < 0) + goto cleanup; + + if (cg_write(memcg, "memory.swap.max", "0")) + goto cleanup; + + if (cg_write(memcg, "memory.high", "30M")) + goto cleanup; + + if (cg_write(memcg, "memory.max", "140M")) + goto cleanup; + + fd = memcg_prepare_for_wait(memcg); + if (fd < 0) + goto cleanup; + + pid = cg_run_nowait(memcg, alloc_anon_mlock, (void *)MB(200)); + if (pid < 0) + goto cleanup; + + cg_wait_for(fd); + + post_high = cg_read_key_long(memcg, "memory.events", "high "); + post_max = cg_read_key_long(memcg, "memory.events", "max "); + if (post_high < 0 || post_max < 0) + goto cleanup; + + if (pre_high == post_high || pre_max != post_max) + goto cleanup; + + ret = KSFT_PASS; + +cleanup: + if (fd >= 0) + close(fd); + cg_destroy(memcg); + free(memcg); + + return ret; +} + /* * This test checks that memory.max limits the amount of * memory which can be consumed by either anonymous memory @@ -1180,6 +1257,7 @@ struct memcg_test { T(test_memcg_min), T(test_memcg_low), T(test_memcg_high), + T(test_memcg_high_sync), T(test_memcg_max), T(test_memcg_oom_events), T(test_memcg_swap_max), From patchwork Fri Feb 11 06:49:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 12742928 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 85880C433F5 for ; Fri, 11 Feb 2022 06:50:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0EB7C6B007D; Fri, 11 Feb 2022 01:50:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 09BCF6B007E; Fri, 11 Feb 2022 01:50:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EA6336B0080; Fri, 11 Feb 2022 01:49:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.26]) by kanga.kvack.org (Postfix) with ESMTP id DB2896B007D for ; Fri, 11 Feb 2022 01:49:59 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A1B2623B94 for ; Fri, 11 Feb 2022 06:49:59 +0000 (UTC) X-FDA: 79129574118.05.9837FC0 Received: from mail-ot1-f74.google.com (mail-ot1-f74.google.com [209.85.210.74]) by imf06.hostedemail.com (Postfix) with ESMTP id 2059E180004 for ; Fri, 11 Feb 2022 06:49:58 +0000 (UTC) Received: by mail-ot1-f74.google.com with SMTP id e21-20020a9d7315000000b005a6d56ac70bso4811171otk.21 for ; Thu, 10 Feb 2022 22:49:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=+8rN+YKf6ok+x08l31cPg9Z1f+kTrw1cXJDsx6RQzpQ=; b=BSR393aKc6QG8wWOZWMfrPQRJdGzUlEYw+Xm72Fj+l1HA0QTOlh+tjEKyBgFzMW5dh yMc5fn9Y/A3FfgNSdOvbIJTLWXMoYdbIp/vvqzQXMLbWsG9nJxhfo+fKE3NGJifj+L55 3LoH8v37N4wzh6cZ6ZelxTm1tTnyY0hKMYUvTMKIKyIycqhn6OLw7MJT5WtKpDt36DPf wCMwOA8QiMTaXEP2zn6vCRFegcEHws0MuunzFrr6o1UAxCnt9V9kQaR9FJs/brjoGool gPl1ffvPeA1Ff7JE3BrPzFA4t8pldf9Fk2M0Op9O4qxcn/XyTm8w2rz+eUBX0/iLhkxa f4vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=+8rN+YKf6ok+x08l31cPg9Z1f+kTrw1cXJDsx6RQzpQ=; b=ZQpQWDFN83fsVqj4wrjsoT52oc8uJrz2W482msUdc6XpkmhiiCxBEC/BNOujyYqxxh y0ziVo2Gb5C/qIuXjOwJIG4LxI9Oo/ubnV+NIUkuNoMnFp7zuA0/sqBm8wguE9P4p/qB IwcCKNm4YtJGsw+2SY1VoGInA5Bus8geYTQXaoPlL7bWWaBQLnEW1Upw9eUcKzSvf4gP kgD/FMqmgWIchaR4t2phmG5f8dyufCqiHOkeK55czR9wTPZ+ADLysFV2kKiSHpttMt3G K52payQg7fXFvpGnqLPmhYrg9sSYzTr9mQ+CnO63NwJTppSQXTGBaHUvVPWKQ0o/YBBE ns/w== X-Gm-Message-State: AOAM532l8/vAhrn0Yjh/ktENrS5TGJDymP3Ib3t0O0iYhC6smdA2IDdD jHZrN/oTqQ4hcK0+dOciRJOlC2xfV25FUQ== X-Google-Smtp-Source: ABdhPJx+FPelF0U4y0S0ru7GypuLQN66L/vIwUT5nqHv0csa/+RSKZQiVXDUKNso1ijCAHAvOmABsg8qgMR59A== X-Received: from shakeelb.svl.corp.google.com ([2620:15c:2cd:202:9a07:ef1a:2fee:57f1]) (user=shakeelb job=sendgmr) by 2002:a05:6870:8682:: with SMTP id p2mr257108oam.205.1644562198389; Thu, 10 Feb 2022 22:49:58 -0800 (PST) Date: Thu, 10 Feb 2022 22:49:17 -0800 In-Reply-To: <20220211064917.2028469-1-shakeelb@google.com> Message-Id: <20220211064917.2028469-5-shakeelb@google.com> Mime-Version: 1.0 References: <20220211064917.2028469-1-shakeelb@google.com> X-Mailer: git-send-email 2.35.1.265.g69c8d7142f-goog Subject: [PATCH v2 4/4] memcg: synchronously enforce memory.high for large overcharges From: Shakeel Butt To: Johannes Weiner , Michal Hocko , Roman Gushchin Cc: Chris Down , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Shakeel Butt Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=BSR393aK; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 3FgcGYggKCFMD2v5zz6w19916z.x97638FI-775Gvx5.9C1@flex--shakeelb.bounces.google.com designates 209.85.210.74 as permitted sender) smtp.mailfrom=3FgcGYggKCFMD2v5zz6w19916z.x97638FI-775Gvx5.9C1@flex--shakeelb.bounces.google.com X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 2059E180004 X-Stat-Signature: btsgtj835siak6ecxoanjamhr3cwfccn X-HE-Tag: 1644562198-801475 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: The high limit is used to throttle the workload without invoking the oom-killer. Recently we tried to use the high limit to right size our internal workloads. More specifically dynamically adjusting the limits of the workload without letting the workload get oom-killed. However due to the limitation of the implementation of high limit enforcement, we observed the mechanism fails for some real workloads. The high limit is enforced on return-to-userspace i.e. the kernel let the usage goes over the limit and when the execution returns to userspace, the high reclaim is triggered and the process can get throttled as well. However this mechanism fails for workloads which do large allocations in a single kernel entry e.g. applications that mlock() a large chunk of memory in a single syscall. Such applications bypass the high limit and can trigger the oom-killer. To make high limit enforcement more robust, this patch makes the limit enforcement synchronous only if the accumulated overcharge becomes larger than MEMCG_CHARGE_BATCH. So, most of the allocations would still be throttled on the return-to-userspace path but only the extreme allocations which accumulates large amount of overcharge without returning to the userspace will be throttled synchronously. The value MEMCG_CHARGE_BATCH is a bit arbitrary but most of other places in the memcg codebase uses this constant therefore for now uses the same one. Signed-off-by: Shakeel Butt Reviewed-by: Roman Gushchin Acked-by: Chris Down --- Changes since v1: - Based on Roman's comment simply the sync enforcement and only target the extreme cases. mm/memcontrol.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 292b0b99a2c7..0da4be4798e7 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2703,6 +2703,11 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, } } while ((memcg = parent_mem_cgroup(memcg))); + if (current->memcg_nr_pages_over_high > MEMCG_CHARGE_BATCH && + !(current->flags & PF_MEMALLOC) && + gfpflags_allow_blocking(gfp_mask)) { + mem_cgroup_handle_over_high(); + } return 0; }