Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3991534pxv; Tue, 13 Jul 2021 08:24:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzPcQJeHOZPnLDh8cxaoeqtJSQPQ5yOxV6L93YfPHBGOs9uW0OYFuxPThVb1fueakB7tltu X-Received: by 2002:a02:93a3:: with SMTP id z32mr4572701jah.33.1626189852448; Tue, 13 Jul 2021 08:24:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626189852; cv=none; d=google.com; s=arc-20160816; b=FK1hajOL2gTdme71Av2efI35RE5rNPGYRW5/Dd9oJUEQZyaOjXAZ2WJzX4tWF/i1yo 4IHs0THm82IZDNtlv5UeMOUwyVNB5ioS9Oxjp26DOTPGDGbTCT59+dbb6qzCXK8OQ0WP GXGEGHihRJ5kU4IGS9vt+aIf9wJrJ4jF+9azPyFunXVQfQeJc943u/IvtVDlm1SoSDcy zIZkMpOXuu06aRl2ln3LO8TJ+4tWJ/TRt8wd4kbeIu74YlIsg2VrV2w3aXksWfTKfM67 nR84aFwXs76PaaPgfXLBhVPckTUfXW+gLwcoxScuUmVrWOhuwOIl712AOZGgdre6qeoh nPIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=PXY3WkNV7buc48b8Sc2WzGPLeGS+HzQcAM1fhNw1AH0=; b=ypQf82PpKIw58vGSmYPn49BiEYvZG+VxtC9iKmdyF5Lwp2UCh/8aJ8nnP5UBiENWus paaVwB4ZFkRDY6IVv4woKghsuXtQPKgZjlNXamSAahJUggqyF9cxVndclWZEtN77xsi7 /BRKr0flGfzQUx8to2gkJFQbRdSa7AOe3ch0Pk1fIazCNiLoV+Fm318fpddNwG7N9nJ+ WVVJ//bTUj3L27cS2dB5YTW+Kbcy+va7e0e4kaOz4cUwPzwxtBjt4wbSTJm9h0yiILYr AtzSN18ydyPIEkiwVOXaenHQ5f9/v8CXEy/OqlbPc5uTMwX9nwj8fYyltFO7rrpuLszz e7WQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n24si258892ioz.5.2021.07.13.08.23.55; Tue, 13 Jul 2021 08:24:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237066AbhGMPXz (ORCPT + 99 others); Tue, 13 Jul 2021 11:23:55 -0400 Received: from outbound-smtp29.blacknight.com ([81.17.249.32]:56330 "EHLO outbound-smtp29.blacknight.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236986AbhGMPXw (ORCPT ); Tue, 13 Jul 2021 11:23:52 -0400 Received: from mail.blacknight.com (pemlinmail01.blacknight.ie [81.17.254.10]) by outbound-smtp29.blacknight.com (Postfix) with ESMTPS id 816B1BEC92 for ; Tue, 13 Jul 2021 16:21:01 +0100 (IST) Received: (qmail 14885 invoked from network); 13 Jul 2021 15:21:01 -0000 Received: from unknown (HELO stampy.112glenside.lan) (mgorman@techsingularity.net@[84.203.17.255]) by 81.17.254.9 with ESMTPA; 13 Jul 2021 15:21:01 -0000 From: Mel Gorman To: Andrew Morton Cc: Desmond Cheong Zhi Xi , Zhang Qiang , Yanfei Xu , Chuck Lever , Jesper Dangaard Brouer , Matteo Croce , Linux-MM , LKML , Mel Gorman Subject: [PATCH 3/4] mm/page_alloc: Further fix __alloc_pages_bulk() return value Date: Tue, 13 Jul 2021 16:20:59 +0100 Message-Id: <20210713152100.10381-4-mgorman@techsingularity.net> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210713152100.10381-1-mgorman@techsingularity.net> References: <20210713152100.10381-1-mgorman@techsingularity.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Chuck Lever The author of commit b3b64ebd3822 ("mm/page_alloc: do bulk array bounds check after checking populated elements") was possibly confused by the mixture of return values throughout the function. The API contract is clear that the function "Returns the number of pages on the list or array." It does not list zero as a unique return value with a special meaning. Therefore zero is a plausible return value only if @nr_pages is zero or less. Clean up the return logic to make it clear that the returned value is always the total number of pages in the array/list, not the number of pages that were allocated during this call. The only change in behavior with this patch is the value returned if prepare_alloc_pages() fails. To match the API contract, the number of pages currently in the array/list is returned in this case. The call site in __page_pool_alloc_pages_slow() also seems to be confused on this matter. It should be attended to by someone who is familiar with that code. [mel@techsingularity.net: Return nr_populated if 0 pages are requested] Signed-off-by: Chuck Lever Acked-by: Jesper Dangaard Brouer Signed-off-by: Mel Gorman --- mm/page_alloc.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 803414ce9264..c66f1e6204c2 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -5221,9 +5221,6 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, unsigned int alloc_flags = ALLOC_WMARK_LOW; int nr_populated = 0, nr_account = 0; - if (unlikely(nr_pages <= 0)) - return 0; - /* * Skip populated array elements to determine if any pages need * to be allocated before disabling IRQs. @@ -5231,9 +5228,13 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, while (page_array && nr_populated < nr_pages && page_array[nr_populated]) nr_populated++; + /* No pages requested? */ + if (unlikely(nr_pages <= 0)) + goto out; + /* Already populated array? */ if (unlikely(page_array && nr_pages - nr_populated == 0)) - return nr_populated; + goto out; /* Use the single page allocator for one page. */ if (nr_pages - nr_populated == 1) @@ -5255,7 +5256,7 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, gfp &= gfp_allowed_mask; alloc_gfp = gfp; if (!prepare_alloc_pages(gfp, 0, preferred_nid, nodemask, &ac, &alloc_gfp, &alloc_flags)) - return nr_populated; + goto out; gfp = alloc_gfp; /* Find an allowed local zone that meets the low watermark. */ @@ -5323,6 +5324,7 @@ 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); +out: return nr_populated; failed_irq: @@ -5338,7 +5340,7 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, nr_populated++; } - return nr_populated; + goto out; } EXPORT_SYMBOL_GPL(__alloc_pages_bulk); -- 2.26.2