Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3920046pxv; Tue, 13 Jul 2021 06:57:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwi0Pub4ht7f9UByTGN2m0KG1N2WriwoTYK9uwVmOb79F6Hi5h8dLxWMXBJvbyaVQMn0rYB X-Received: by 2002:a50:c111:: with SMTP id l17mr5826616edf.56.1626184674911; Tue, 13 Jul 2021 06:57:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626184674; cv=none; d=google.com; s=arc-20160816; b=KHOkdsOivwxEfEaf9t876dNCVrcMoKIi4igDbN1YOIImeFvieOaGD8u9ChgTQBCSAP JwYWZGfrflUDyGVlqvwzCemmHfAnxUzmTxZbMsChEE90UlUqoXai6Y3B51aJs5ybu/dK IYYgwHABQUbXkzR3wo+loW8qEmvbSezG6UDv2r9rrWrBGJPoauOs9frsEZtaZfbVmp5y JYAwzPpTx4kdM3tn+6FTVkNcTfb7EGAA+Aih3HR0+GIu7BOtlosQ0ENm8ieTbSrct4Ri uD5l9uNVsTQg334bAdLQ6Z23XwyC8GfconP8FHtNKtzbMjB2PvhWZiPzvpvQOJiuQXUB 2zGQ== 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=X9hvsot7svXnD1GJDIWvV/m0O5Ra/mVu81Yf2imSOSw=; b=aPG+SCEW3pVd9XKgcldr8jjHLTcC8aNR7qeiiof9LU0hFjwI0/H7Fwz9uu/8XoOxZI CSiKiASFmkyHkCktp4dmA+Q5BdLSwoVYcG0azl7Q0LLqqD9oXyx7nWAPFBYNInZd/wbP CHm5pkVj6Qp8ouXsFWn4M4T97AlPADFCq2eXNlpzjAyKbig70Paoyhs2gth3YEJsjrqo f4oHAbcXo/HJxiS5rSjQzTPx1ZvAC0DOuVvdi2iiTmPZA+5XWLtUsoHdAu08If9fh7vZ f+ypTa6+z/GXNqxm1n1lqQ8j2f9BTdfNvEm3zDDVG25jyGHm1sPGFqm5UUxlKd3cB9gT Aiig== 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 c16si22128835edw.522.2021.07.13.06.57.31; Tue, 13 Jul 2021 06:57:54 -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 S236840AbhGMN7U (ORCPT + 99 others); Tue, 13 Jul 2021 09:59:20 -0400 Received: from outbound-smtp22.blacknight.com ([81.17.249.190]:44498 "EHLO outbound-smtp22.blacknight.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236548AbhGMN7S (ORCPT ); Tue, 13 Jul 2021 09:59:18 -0400 Received: from mail.blacknight.com (pemlinmail06.blacknight.ie [81.17.255.152]) by outbound-smtp22.blacknight.com (Postfix) with ESMTPS id F31F3148019 for ; Tue, 13 Jul 2021 14:56:26 +0100 (IST) Received: (qmail 30402 invoked from network); 13 Jul 2021 13:56:26 -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 13:56:26 -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 14:56:24 +0100 Message-Id: <20210713135625.7615-4-mgorman@techsingularity.net> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210713135625.7615-1-mgorman@techsingularity.net> References: <20210713135625.7615-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 | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 803414ce9264..e0eeb7391ec7 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) @@ -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