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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8404FC83030 for ; Mon, 30 Jun 2025 20:05:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 26BCA6B00B9; Mon, 30 Jun 2025 16:05:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 21C996B00BA; Mon, 30 Jun 2025 16:05:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 159076B00BE; Mon, 30 Jun 2025 16:05:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 048BE6B00B9 for ; Mon, 30 Jun 2025 16:05:59 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id A18B7C013C for ; Mon, 30 Jun 2025 20:05:58 +0000 (UTC) X-FDA: 83613147996.09.B76736F Received: from bout3.ijzerbout.nl (bout3.ijzerbout.nl [136.144.140.114]) by imf14.hostedemail.com (Postfix) with ESMTP id B357910000C for ; Mon, 30 Jun 2025 20:05:55 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=ijzerbout.nl header.s=key header.b=cbXEB+53; dmarc=none; spf=pass (imf14.hostedemail.com: domain of kees@ijzerbout.nl designates 136.144.140.114 as permitted sender) smtp.mailfrom=kees@ijzerbout.nl ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1751313957; a=rsa-sha256; cv=none; b=FWPcAo5lmBS+tDopb8Z8HLIs98uAQAtNTK/IHlO2T8ExaLaKDiaFaYXk8+oQPxgX6xI5Ph OYxNackeTGKOaSXkTFk0IyATkM01rM3DLfvzDrFrmdbOcM46ovl48phlKn15CXl4DPBgZD 8cL54yZiWoPTzFbsUmGJsrB4abJghAc= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=ijzerbout.nl header.s=key header.b=cbXEB+53; dmarc=none; spf=pass (imf14.hostedemail.com: domain of kees@ijzerbout.nl designates 136.144.140.114 as permitted sender) smtp.mailfrom=kees@ijzerbout.nl ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1751313957; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=vbOCzpzWVY9IMO8KvbmlBuMkyl7nY9wmcGhPiCi7TUM=; b=ZyIGD4jH/Ey+Kc7WSDosiB9fs6+geiIJHBmfl5a+A2y+fsa7M9iI+ncY128K6swYCpRvTV XtEtSh6oEo/XlQ4ULMxFypYuSD4B+duuJQiIUt7vf8OI1woOgpRNeJyp5IWawWs/YeEgk0 PauwLjn/z3nS/R4V46ev3apdDIJYcQw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ijzerbout.nl; s=key; t=1751313953; bh=yBg3GB3lhCpdzzmoDQuhGODKp6X+ylUuiDnqkO/v3S8=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=cbXEB+5313Qe3bFolzb3FEF0a0/NGTgKnULUlrUZpcne1NsIM3XXmoZo/hXDGFvHk ZLLUaDDJkXprkREqmt+AbxkLuyxI/9QpHPSFwiNyfVUKrVmIY9KJ+k5RSkgZ9mpRF2 vjTfcWU0Fg3qC4wMOGEdE/widwi4cyd2CgCyzEv0CrAhiXWiNbucR8lRIswaJ7ZpMY 1kJqQg83xSn9CCE+Fm230hskiQcNOJKb0mANaay4JsFJyi6z4i2XOVaq085wGZkDql f4gt6FJmYNihkdlCoLBMfDJ7ZEPMdQSL9qPMhQtzc0Xlz2ngSSkOKS6OGaNi9WGk2+ DrOgiuncYSG2+/hCul8lOG2AG6w/z1vTMYPsIo1jDEzLz97Wi8bMhTkax2rA6NnvHY CXL20edyECqgP5s7uFgMMG6S2k6Fc0ThjK61lwxA3iKHQLKIcY36dIPpo32MZ4yE0l wYEFizbjEQMmDIZ+Ju1Z60X96BOaKJz7WGQM2vy9fYhKV/XPF1QsCobQzr6ceOn847 04MNWGrJ3ZNVV/y9/3GaINBECfINYjkkIt7gWrUDP1oQgUy1HJDXnaXQL+SEGB+Eqm uWIQyjO2A5bVGmOG6nzjcXSKoS3ZrOv47Akkg0V96etBuU2m9nZ0e3VKQwpQWcFVhw ymK8xQ2v1kImlfuhjxwH6dsY= Received: from [IPV6:2a10:3781:99:1:1ac0:4dff:fea7:ec3a] (racer.ijzerbout.nl [IPv6:2a10:3781:99:1:1ac0:4dff:fea7:ec3a]) by bout3.ijzerbout.nl (Postfix) with ESMTPSA id 805C6162A20; Mon, 30 Jun 2025 22:05:51 +0200 (CEST) Message-ID: <7c1180f4-923c-4138-b756-618cb5d597ac@ijzerbout.nl> Date: Mon, 30 Jun 2025 22:05:48 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 2/2] mm/mempolicy: Skip extra call to __alloc_pages_bulk in weighted interleave To: Joshua Hahn , Gregory Price Cc: Andrew Morton , Alistair Popple , Byungchul Park , David Hildenbrand , Matthew Brost , Rakie Kim , Ying Huang , Zi Yan , linux-kernel@vger.kernel.org, linux-mm@kvack.org, kernel-team@meta.com References: <20250626200936.3974420-1-joshua.hahnjy@gmail.com> <20250626200936.3974420-3-joshua.hahnjy@gmail.com> Content-Language: en-US From: Kees Bakker In-Reply-To: <20250626200936.3974420-3-joshua.hahnjy@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: B357910000C X-Stat-Signature: rbdmyhet9t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NJtn4CXupklEbifATGLH1Ei8S1Pbda+lAJY2bwoa09s8AW3HW49W4kv/gWhOAAIr4OKv2zmGqYNPhwdzuTg1G+NHZ8fCwpPuTYu62AIWKCBkLnMTbx1C2/M0acU27g8+hB/4F8k1hxCNzCYc53h9gNtPNTjaV7MKMnXPtqkzSeOevTMFwKTOtndXRr29L0tKRLtdJz+LLW7e4aVuM7zDnEHNgX5bO4I7+MTURYpMKaEfqRLPVhr4XhWaBeExDWNkxoG1S/gkxX0aQLQNHXx143ivG5dr23rNsmjcydH7t7e83klTAhzR3KKdpkjFiDxkq36m6yoVfiXnyR/h/qmvqEi7ALIcujwyT3MvFHvQYyZw+O8S9JzR/q7yem9xsRgJ/TiRDFxnSeIQmc284pyI2wVwOysHAdw5uexKHZoGE7IbgA6VSd03KsIy8x7TWhDvL3ZTbPx+SDE 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: Op 26-06-2025 om 22:09 schreef Joshua Hahn: > Currently, alloc_pages_bulk_weighted_interleave can make up to nr_node_ids+1 > calls to __alloc_pages_bulk. The additional allocation can happen if the > previous call to this function finished the weighted round robin allocation > partially on a node. To make up for this, the next time this function is > called, an extra allocation is made to finish cleanly on the node boundaries > before performing the weighted round-robin cycles again. > > Instead of making an additional call, we can calculate how many additional > pages should be allocated from the first node (aka carryover) and add that > value to the number of pages that should be allocated as part of the current > round-robin cycle. > > Running a quick benchmark by compiling the kernel shows a small increase > in performance. These experiments were run on a machine with 2 nodes, each > with 125GB memory and 40 CPUs. > > time numactl -w 0,1 make -j$(nproc) > > +----------+---------+------------+---------+ > | Time (s) | 6.16 | With patch | % Delta | > +----------+---------+------------+---------+ > | Real | 88.374 | 88.3356 | -0.2019 | > | User | 3631.7 | 3636.263 | 0.0631 | > | Sys | 366.029 | 363.792 | -0.7534 | > +----------+---------+------------+---------+ > > Signed-off-by: Joshua Hahn > > --- > mm/mempolicy.c | 39 ++++++++++++++++++++------------------- > 1 file changed, 20 insertions(+), 19 deletions(-) > > diff --git a/mm/mempolicy.c b/mm/mempolicy.c > index 78ad74a0e249..0d693f96cf66 100644 > --- a/mm/mempolicy.c > +++ b/mm/mempolicy.c > @@ -2569,7 +2569,7 @@ static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp, > unsigned long node_pages, delta; > u8 *weights, weight; > unsigned int weight_total = 0; > - unsigned long rem_pages = nr_pages; > + unsigned long rem_pages = nr_pages, carryover = 0; > nodemask_t nodes; > int nnodes, node; > int resume_node = MAX_NUMNODES - 1; > @@ -2594,18 +2594,12 @@ static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp, > node = me->il_prev; > weight = me->il_weight; > if (weight && node_isset(node, nodes)) { > - node_pages = min(rem_pages, weight); > - nr_allocated = __alloc_pages_bulk(gfp, node, NULL, node_pages, > - page_array); > - page_array += nr_allocated; > - total_allocated += nr_allocated; > - /* if that's all the pages, no need to interleave */ > if (rem_pages <= weight) { > - me->il_weight -= rem_pages; > - return total_allocated; > + node_pages = rem_pages; > + me->il_weight -= node_pages; > + goto allocate; This is a goto into the middle of a for-loop. What do you think is going to happen at the end of that loop? I think (only tested with a small C program) it will go to the start of the loop, do the i++, check i } > - /* Otherwise we adjust remaining pages, continue from there */ > - rem_pages -= weight; > + carryover = weight; > } > /* clear active weight in case of an allocation failure */ > me->il_weight = 0; > @@ -2614,7 +2608,7 @@ static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp, > /* create a local copy of node weights to operate on outside rcu */ > weights = kzalloc(nr_node_ids, GFP_KERNEL); > if (!weights) > - return total_allocated; > + return 0; > > rcu_read_lock(); > state = rcu_dereference(wi_state); > @@ -2634,16 +2628,17 @@ static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp, > /* > * Calculate rounds/partial rounds to minimize __alloc_pages_bulk calls. > * Track which node weighted interleave should resume from. > + * Account for carryover. It is always allocated from the first node. > * > * if (rounds > 0) and (delta == 0), resume_node will always be > * the node following prev_node and its weight. > */ > - rounds = rem_pages / weight_total; > - delta = rem_pages % weight_total; > + rounds = (rem_pages - carryover) / weight_total; > + delta = (rem_pages - carryover) % weight_total; > resume_node = next_node_in(prev_node, nodes); > resume_weight = weights[resume_node]; > + node = carryover ? prev_node : next_node_in(prev_node, nodes); > for (i = 0; i < nnodes; i++) { > - node = next_node_in(prev_node, nodes); > weight = weights[node]; > /* when delta is depleted, resume from that node */ > if (delta && delta < weight) { > @@ -2651,12 +2646,14 @@ static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp, > resume_weight = weight - delta; > } > /* Add the node's portion of the delta, if there is one */ > - node_pages = weight * rounds + min(delta, weight); > + node_pages = weight * rounds + min(delta, weight) + carryover; > delta -= min(delta, weight); > + carryover = 0; > > /* node_pages can be 0 if an allocation fails and rounds == 0 */ > if (!node_pages) > break; > +allocate: > nr_allocated = __alloc_pages_bulk(gfp, node, NULL, node_pages, > page_array); > page_array += nr_allocated; > @@ -2664,10 +2661,14 @@ static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp, > if (total_allocated == nr_pages) > break; > prev_node = node; > + node = next_node_in(prev_node, nodes); > + } > + > + if (weights) { > + me->il_prev = resume_node; > + me->il_weight = resume_weight; > + kfree(weights); > } > - me->il_prev = resume_node; > - me->il_weight = resume_weight; > - kfree(weights); > return total_allocated; > } >