From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 240F4C433E1 for ; Fri, 15 May 2020 20:20:39 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D6AE120727 for ; Fri, 15 May 2020 20:20:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="vHWjxZXS" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D6AE120727 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9F4EF8E0001; Fri, 15 May 2020 16:20:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9A5718E0005; Fri, 15 May 2020 16:20:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8714C8E0001; Fri, 15 May 2020 16:20:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0234.hostedemail.com [216.40.44.234]) by kanga.kvack.org (Postfix) with ESMTP id 63E008E0005 for ; Fri, 15 May 2020 16:20:37 -0400 (EDT) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 26A7982499B9 for ; Fri, 15 May 2020 20:20:37 +0000 (UTC) X-FDA: 76820071314.03.cord39_55c045a8ca3e X-HE-Tag: cord39_55c045a8ca3e X-Filterd-Recvd-Size: 4733 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf28.hostedemail.com (Postfix) with ESMTP for ; Fri, 15 May 2020 20:20:36 +0000 (UTC) Received: from kicinski-fedora-PC1C0HJN.thefacebook.com (unknown [163.114.132.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 9EBAE2076A; Fri, 15 May 2020 20:20:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589574036; bh=uUYnL0+fJok3BexlfimUWqnRhI/oNnrMTerMh0Dm7KU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vHWjxZXS7n52mqhR4OSjPS9eIBLi9vONjOlO6FKe0mx2PFICX2s4gJM4Mt2lAa/EG eGGtNvDLTyz9/IJjkrSyctuEYSd9DnEF5LLY66zgkPsRptS5rH/UnH/wpoUbYZHlD+ gyK7JnHcxc4/qsDAqiBzdMU4dHt5jPscxqo6jHIQ= From: Jakub Kicinski To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, kernel-team@fb.com, tj@kernel.org, hannes@cmpxchg.org, chris@chrisdown.name, cgroups@vger.kernel.org, shakeelb@google.com, mhocko@kernel.org, Jakub Kicinski Subject: [PATCH mm v3 1/3] mm: prepare for swap over-high accounting and penalty calculation Date: Fri, 15 May 2020 13:20:25 -0700 Message-Id: <20200515202027.3217470-2-kuba@kernel.org> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20200515202027.3217470-1-kuba@kernel.org> References: <20200515202027.3217470-1-kuba@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: Slice the memory overage calculation logic a little bit so we can reuse it to apply a similar penalty to the swap. The logic which accesses the memory-specific fields (use and high values) has to be taken out of calculate_high_delay(). Signed-off-by: Jakub Kicinski --- v4: calculate max outside of calculate_overage() --- mm/memcontrol.c | 62 ++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index fe4f4d96ae3e..851f4d033e8f 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2300,41 +2300,48 @@ static void high_work_func(struct work_struct *wo= rk) #define MEMCG_DELAY_PRECISION_SHIFT 20 #define MEMCG_DELAY_SCALING_SHIFT 14 =20 -/* - * Get the number of jiffies that we should penalise a mischievous cgrou= p which - * is exceeding its memory.high by checking both it and its ancestors. - */ -static unsigned long calculate_high_delay(struct mem_cgroup *memcg, - unsigned int nr_pages) +static u64 calculate_overage(unsigned long usage, unsigned long high) { - unsigned long penalty_jiffies; - u64 max_overage =3D 0; - - do { - unsigned long usage, high; - u64 overage; + u64 overage; =20 - usage =3D page_counter_read(&memcg->memory); - high =3D READ_ONCE(memcg->high); + if (usage <=3D high) + return 0; =20 - if (usage <=3D high) - continue; + /* + * Prevent division by 0 in overage calculation by acting as if + * it was a threshold of 1 page + */ + high =3D max(high, 1UL); =20 - /* - * Prevent division by 0 in overage calculation by acting as if - * it was a threshold of 1 page - */ - high =3D max(high, 1UL); + overage =3D usage - high; + overage <<=3D MEMCG_DELAY_PRECISION_SHIFT; + return div64_u64(overage, high); +} =20 - overage =3D usage - high; - overage <<=3D MEMCG_DELAY_PRECISION_SHIFT; - overage =3D div64_u64(overage, high); +static u64 mem_find_max_overage(struct mem_cgroup *memcg) +{ + u64 overage, max_overage =3D 0; =20 - if (overage > max_overage) - max_overage =3D overage; + do { + overage =3D calculate_overage(page_counter_read(&memcg->memory), + READ_ONCE(memcg->high)); + max_overage =3D max(overage, max_overage); } while ((memcg =3D parent_mem_cgroup(memcg)) && !mem_cgroup_is_root(memcg)); =20 + return max_overage; +} + +/* + * Get the number of jiffies that we should penalise a mischievous cgrou= p which + * is exceeding its memory.high by checking both it and its ancestors. + */ +static unsigned long calculate_high_delay(struct mem_cgroup *memcg, + unsigned int nr_pages, + u64 max_overage) +{ + unsigned long penalty_jiffies; + if (!max_overage) return 0; =20 @@ -2390,7 +2397,8 @@ void mem_cgroup_handle_over_high(void) * memory.high is breached and reclaim is unable to keep up. Throttle * allocators proactively to slow down excessive growth. */ - penalty_jiffies =3D calculate_high_delay(memcg, nr_pages); + penalty_jiffies =3D calculate_high_delay(memcg, nr_pages, + mem_find_max_overage(memcg)); =20 /* * Don't sleep if the amount of jiffies this memcg owes us is so low --=20 2.25.4