From patchwork Fri Oct 8 09:23:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasily Averin X-Patchwork-Id: 12544717 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EFD77C433EF for ; Fri, 8 Oct 2021 09:23:54 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 73E5D60F70 for ; Fri, 8 Oct 2021 09:23:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 73E5D60F70 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=virtuozzo.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 03802900002; Fri, 8 Oct 2021 05:23:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F27CF6B0072; Fri, 8 Oct 2021 05:23:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E16EF900002; Fri, 8 Oct 2021 05:23:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0063.hostedemail.com [216.40.44.63]) by kanga.kvack.org (Postfix) with ESMTP id D553E6B0071 for ; Fri, 8 Oct 2021 05:23:53 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 96BBC39491 for ; Fri, 8 Oct 2021 09:23:53 +0000 (UTC) X-FDA: 78672733146.20.8D08529 Received: from relay.sw.ru (relay.sw.ru [185.231.240.75]) by imf16.hostedemail.com (Postfix) with ESMTP id 09BD0F0017D6 for ; Fri, 8 Oct 2021 09:23:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=Content-Type:MIME-Version:Date:Message-ID:Subject :From; bh=dpPiV3bJCSO+Krvm6GpN+2Uo7e56DJCpPVOsUghiKmE=; b=tqgKapK8JdPzur2w3X/ 6pSaHgtdkh0ouvn4j+ofbPaYDXCmaXkKg1LQwfrCq3EEpsUAhT6+2m3Y/euudRVCyKoy3SWFTb272 eFW0FMx4A0IIjtdoqVrilXf+dVFn1+5Bq5B/gKKfJ6gJbdQBmMd48pW37dTXtMY1WdKCJ4hHbtA=; Received: from [10.93.0.56] by relay.sw.ru with esmtp (Exim 4.94.2) (envelope-from ) id 1mYm6U-005QDB-A5; Fri, 08 Oct 2021 12:23:46 +0300 From: Vasily Averin Subject: [PATCH memcg] memcg: enable memory accounting in __alloc_pages_bulk To: Michal Hocko Cc: Johannes Weiner , Vladimir Davydov , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel@openvz.org, Mel Gorman , Uladzislau Rezki , Vlastimil Babka References: <6411d3f7-b3a4-23a8-14fc-fcf6b9c5b73a@virtuozzo.com> Message-ID: Date: Fri, 8 Oct 2021 12:23:43 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: <6411d3f7-b3a4-23a8-14fc-fcf6b9c5b73a@virtuozzo.com> Content-Language: en-US X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 09BD0F0017D6 X-Stat-Signature: u54g8xyjfzspao176xyp1qe7mw9go69d Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=virtuozzo.com header.s=relay header.b=tqgKapK8; spf=pass (imf16.hostedemail.com: domain of vvs@virtuozzo.com designates 185.231.240.75 as permitted sender) smtp.mailfrom=vvs@virtuozzo.com; dmarc=pass (policy=quarantine) header.from=virtuozzo.com X-HE-Tag: 1633685032-122343 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: Enable memory accounting for bulk page allocator. Fixes: 387ba26fb1cb ("mm/page_alloc: add a bulk page allocator") Cc: Signed-off-by: Vasily Averin --- mm/page_alloc.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index b37435c274cf..602819a232e5 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -5172,6 +5172,55 @@ static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order, return true; } +#ifdef CONFIG_MEMCG_KMEM +static bool memcg_bulk_pre_charge_hook(struct obj_cgroup **objcgp, gfp_t gfp, + unsigned int nr_pages) +{ + struct obj_cgroup *objcg = NULL; + + if (!memcg_kmem_enabled() || !(gfp & __GFP_ACCOUNT)) + return true; + + objcg = get_obj_cgroup_from_current(); + + if (objcg && obj_cgroup_charge(objcg, gfp, nr_pages << PAGE_SHIFT)) { + obj_cgroup_put(objcg); + return false; + } + obj_cgroup_get_many(objcg, nr_pages); + *objcgp = objcg; + return true; +} + +static void memcg_bulk_charge_hook(struct obj_cgroup *objcg, + struct page *page) +{ + page->memcg_data = (unsigned long)objcg | MEMCG_DATA_KMEM; +} + +static void memcg_bulk_post_charge_hook(struct obj_cgroup *objcg, + unsigned int nr_pages) +{ + obj_cgroup_uncharge(objcg, nr_pages << PAGE_SHIFT); + percpu_ref_put_many(&objcg->refcnt, nr_pages + 1); +} +#else +static bool memcg_bulk_pre_charge_hook(struct obj_cgroup **objcgp, gfp_t gfp, + unsigned int nr_pages) +{ + return true; +} + +static void memcg_bulk_charge_hook(struct obj_cgroup *objcgp, + struct page *page) +{ +} + +static void memcg_bulk_post_charge_hook(struct obj_cgroup *objcg, + unsigned int nr_pages) +{ +} +#endif /* * __alloc_pages_bulk - Allocate a number of order-0 pages to a list or array * @gfp: GFP flags for the allocation @@ -5207,6 +5256,8 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, gfp_t alloc_gfp; unsigned int alloc_flags = ALLOC_WMARK_LOW; int nr_populated = 0, nr_account = 0; + unsigned int nr_pre_charge = 0; + struct obj_cgroup *objcg = NULL; /* * Skip populated array elements to determine if any pages need @@ -5275,6 +5326,10 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, if (unlikely(!zone)) goto failed; + nr_pre_charge = nr_pages - nr_populated; + if (!memcg_bulk_pre_charge_hook(&objcg, gfp, nr_pre_charge)) + goto failed; + /* Attempt the batch allocation */ local_lock_irqsave(&pagesets.lock, flags); pcp = this_cpu_ptr(zone->per_cpu_pageset); @@ -5287,9 +5342,9 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, nr_populated++; continue; } - page = __rmqueue_pcplist(zone, 0, ac.migratetype, alloc_flags, pcp, pcp_list); + if (unlikely(!page)) { /* Try and get at least one page */ if (!nr_populated) @@ -5297,6 +5352,8 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, break; } nr_account++; + if (objcg) + memcg_bulk_charge_hook(objcg, page); prep_new_page(page, 0, gfp, 0); if (page_list) @@ -5310,13 +5367,16 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, __count_zid_vm_events(PGALLOC, zone_idx(zone), nr_account); zone_statistics(ac.preferred_zoneref->zone, zone, nr_account); + if (objcg) + memcg_bulk_post_charge_hook(objcg, nr_pre_charge - nr_account); out: return nr_populated; failed_irq: local_unlock_irqrestore(&pagesets.lock, flags); - + if (objcg) + memcg_bulk_post_charge_hook(objcg, nr_pre_charge); failed: page = __alloc_pages(gfp, 0, preferred_nid, nodemask); if (page) {