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 9CD7EC54E4A for ; Mon, 11 May 2020 22:55:33 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6339A2070B for ; Mon, 11 May 2020 22:55:33 +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="W2dLXmEh" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6339A2070B 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 7C4BF90008B; Mon, 11 May 2020 18:55:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 74E49900036; Mon, 11 May 2020 18:55:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 63C5990008B; Mon, 11 May 2020 18:55:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0224.hostedemail.com [216.40.44.224]) by kanga.kvack.org (Postfix) with ESMTP id 49637900036 for ; Mon, 11 May 2020 18:55:32 -0400 (EDT) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 0E4DF8248047 for ; Mon, 11 May 2020 22:55:32 +0000 (UTC) X-FDA: 76805946504.23.earth54_6a79e570b8b3a X-HE-Tag: earth54_6a79e570b8b3a X-Filterd-Recvd-Size: 4678 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf10.hostedemail.com (Postfix) with ESMTP for ; Mon, 11 May 2020 22:55:31 +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 6F11B2075E; Mon, 11 May 2020 22:55:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589237730; bh=hPqe/05rnU2qdGjFk4enrQ1+qhpSIPRruaw+FWrv/LY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=W2dLXmEh0CKBx9VFApsDss2e5KCl8DyX4XBB2a6yvgq6nUKiDvzxgzmhPxH2P0HVv y/u/H8BUsrJfjDRZlulnyIFUOa1t2633inD5FBCT8CxVxzw98RN10xChEdedn+TyIO hD/9mMEX97ho/kShv/lNAlH7Mo04AX65nsEH3Otk= 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 v2 1/3] mm: prepare for swap over-high accounting and penalty calculation Date: Mon, 11 May 2020 15:55:14 -0700 Message-Id: <20200511225516.2431921-2-kuba@kernel.org> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20200511225516.2431921-1-kuba@kernel.org> References: <20200511225516.2431921-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 --- mm/memcontrol.c | 62 ++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 05dcb72314b5..8a9b671c3249 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2321,41 +2321,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 @@ -2411,7 +2418,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