Received: by 10.223.185.116 with SMTP id b49csp2701059wrg; Mon, 12 Feb 2018 14:27:09 -0800 (PST) X-Google-Smtp-Source: AH8x225A7DUow02X2kyId5j3lQN1d62kECegvP1rK04OHqIuTUk6Ly7afxNgBhkVyMUaNkSeYJXw X-Received: by 2002:a17:902:7c86:: with SMTP id y6-v6mr11944998pll.24.1518474429106; Mon, 12 Feb 2018 14:27:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518474429; cv=none; d=google.com; s=arc-20160816; b=hi93UawYmE9+VVIZym7JvuGRFHrkxBrYrE2rxTnZlpoOsHjtZFsl75pHg4342mQMEo ItQdLoX2s6XM4IN0s92NsV/vtfVCXLj+2xZfWt8eaEkHiUy0suRaHUbtApS8X8A4IzCf gunEMN8TDGCQloR98qI9UiQ7b/8jmX9cHH9Nq0rCiWrFOpZnoUyDQRrd/tiHkKsi2dNg Ambh8ZFdMDJ0C3jlhZvixrgOvB4KvvUd3tiHAKk8Z4j4LDk8P9pozsF/A1UJcCMDNuM5 P9RgoeKurfLELaFZIvF97m3zgVmA6Nvum4WiB4IIKFgs5dIFAa0mY46BiKV9Lh5z9wmk /YnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=oryjSGQ3MtHx7orAYt4JG39Kc72jiWyVZgTPHxn01Bw=; b=JA5qg7mYk4p2H9e8IHOOjhgprv+abFAcd3bbpZ42fcL3zuisuVyAxO1w5GDwmh7DdD ALcqUoawBJC8odtI79LIhjYKdFyotvxbevqO6lJOVw106tQVyjztD1oLsDp9wGe4Z0q1 gemEzilwxv/ryRgB/sIeWVWTQfHB1VdFkXX/Qm2h5yhdabIe9VJoPNkej80FWKVrJRR4 26qDSZKdf74ThdOyR7Wg+HIEo49Ly6X5QUbbVRiteKiS6c3CmcckFiQAB6Eesp/BZcr0 MqbsQCygHgkjc2SHOgD8hcHrd7Gfgdiospv18UJxUi5Z3pqpBgKzztTYtySUjLkDrPBd GQew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2017-10-26 header.b=WwyT/ESH; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p8-v6si777284pll.84.2018.02.12.14.26.54; Mon, 12 Feb 2018 14:27:09 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2017-10-26 header.b=WwyT/ESH; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932751AbeBLWXt (ORCPT + 99 others); Mon, 12 Feb 2018 17:23:49 -0500 Received: from aserp2130.oracle.com ([141.146.126.79]:58354 "EHLO aserp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932627AbeBLWVs (ORCPT ); Mon, 12 Feb 2018 17:21:48 -0500 Received: from pps.filterd (aserp2130.oracle.com [127.0.0.1]) by aserp2130.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w1CMH5tV166182; Mon, 12 Feb 2018 22:21:21 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2017-10-26; bh=oryjSGQ3MtHx7orAYt4JG39Kc72jiWyVZgTPHxn01Bw=; b=WwyT/ESH7R6+SfCbd49oyS22OeeSIO50hz7sWoqc/IHz66PEEwaE4TSAPOx80hsnfbFG 8vRVRpVlnbCO5O9SDlQJfwO9HmoqV8I3HgnOtUujSK8qG9VVTRfEH3UPKnEvlfYifFa5 2RhsAru4Z/ch94KQyMVyaQ/HhZDR180Anp8IesE27ZxWw5uF/KjpTVJ1yvJkiIC85Ei+ d6rfCCmIgbuB8vdlV8PR46zFhiH9BDNzOgwZf7C2HXaOM6pB5l/+cwSHAiv0W8Dx3iGD iTw9K+qoheOYzvJ+FJxR5XEG7149BupM5VmCysy0aJ9F+7Hck6UWPQsKQsAngRoTCY7l 8A== Received: from userv0021.oracle.com (userv0021.oracle.com [156.151.31.71]) by aserp2130.oracle.com with ESMTP id 2g3jw0r7bq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 12 Feb 2018 22:21:21 +0000 Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by userv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w1CMLJvi019942 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Mon, 12 Feb 2018 22:21:20 GMT Received: from abhmp0011.oracle.com (abhmp0011.oracle.com [141.146.116.17]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id w1CMLJL3012620; Mon, 12 Feb 2018 22:21:19 GMT Received: from monkey.oracle.com (/98.246.252.205) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 12 Feb 2018 14:21:19 -0800 From: Mike Kravetz To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Michal Hocko , Christopher Lameter , Guy Shattah , Anshuman Khandual , Michal Nazarewicz , Vlastimil Babka , David Nellans , Laura Abbott , Pavel Machek , Dave Hansen , Mike Kravetz Subject: [RFC PATCH 2/3] mm: add find_alloc_contig_pages() interface Date: Mon, 12 Feb 2018 14:20:55 -0800 Message-Id: <20180212222056.9735-3-mike.kravetz@oracle.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20180212222056.9735-1-mike.kravetz@oracle.com> References: <20180212222056.9735-1-mike.kravetz@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8803 signatures=668668 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=2 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1711220000 definitions=main-1802120282 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org find_alloc_contig_pages() is a new interface that attempts to locate and allocate a contiguous range of pages. It is provided as a more convenient interface to the existing alloc_contig_range() interface which is used by CMA, memory hotplug and gigantic huge pages. When attempting to allocate a range of pages, migration is employed if possible. There is no guarantee that the routine will succeed. So, the user must be prepared for failure and have a fall back plan. Signed-off-by: Mike Kravetz --- include/linux/gfp.h | 12 ++++++++ mm/page_alloc.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 99 insertions(+), 2 deletions(-) diff --git a/include/linux/gfp.h b/include/linux/gfp.h index 1a4582b44d32..456979022956 100644 --- a/include/linux/gfp.h +++ b/include/linux/gfp.h @@ -573,6 +573,18 @@ static inline bool pm_suspended_storage(void) extern int alloc_contig_range(unsigned long start, unsigned long end, unsigned migratetype, gfp_t gfp_mask); extern void free_contig_range(unsigned long pfn, unsigned nr_pages); +extern struct page *find_alloc_contig_pages(unsigned int order, gfp_t gfp, + int nid, nodemask_t *nodemask); +extern void free_contig_pages(struct page *page, unsigned nr_pages); +#else +static inline page *find_alloc_contig_pages(unsigned int order, gfp_t gfp, + int nid, nodemask_t *nodemask) +{ + return NULL; +} +static void free_contig_pages(struct page *page, unsigned nr_pages) +{ +} #endif #ifdef CONFIG_CMA diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 064458f317bf..0a5a547acdbf 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -67,6 +67,7 @@ #include #include #include +#include #include #include @@ -1873,9 +1874,13 @@ static __always_inline struct page *__rmqueue_cma_fallback(struct zone *zone, { return __rmqueue_smallest(zone, order, MIGRATE_CMA); } +#define contig_alloc_migratetype_ok(migratetype) \ + ((migratetype) == MIGRATE_CMA || (migratetype) == MIGRATE_MOVABLE) #else static inline struct page *__rmqueue_cma_fallback(struct zone *zone, unsigned int order) { return NULL; } +#define contig_alloc_migratetype_ok(migratetype) \ + ((migratetype) == MIGRATE_MOVABLE) #endif /* @@ -7633,6 +7638,9 @@ int alloc_contig_range(unsigned long start, unsigned long end, }; INIT_LIST_HEAD(&cc.migratepages); + if (!contig_alloc_migratetype_ok(migratetype)) + return -EINVAL; + /* * What we do here is we mark all pageblocks in range as * MIGRATE_ISOLATE. Because pageblock and max order pages may @@ -7723,8 +7731,9 @@ int alloc_contig_range(unsigned long start, unsigned long end, /* Make sure the range is really isolated. */ if (test_pages_isolated(outer_start, end, false)) { - pr_info_ratelimited("%s: [%lx, %lx) PFNs busy\n", - __func__, outer_start, end); + if (!(migratetype == MIGRATE_MOVABLE)) /* only print for CMA */ + pr_info_ratelimited("%s: [%lx, %lx) PFNs busy\n", + __func__, outer_start, end); ret = -EBUSY; goto done; } @@ -7760,6 +7769,82 @@ void free_contig_range(unsigned long pfn, unsigned nr_pages) } WARN(count != 0, "%d pages are still in use!\n", count); } + +static bool contig_pfn_range_valid(struct zone *z, unsigned long start_pfn, + unsigned long nr_pages) +{ + unsigned long i, end_pfn = start_pfn + nr_pages; + struct page *page; + + for (i = start_pfn; i < end_pfn; i++) { + if (!pfn_valid(i)) + return false; + + page = pfn_to_page(i); + + if (page_zone(page) != z) + return false; + + } + + return true; +} + +/** + * find_alloc_contig_pages() -- attempt to find and allocate a contiguous + * range of pages + * @order: number of pages + * @gfp: gfp mask used to limit search as well as during compaction + * @nid: target node + * @nodemask: mask of other possible nodes + * + * Returns pointer to 'order' pages on success, or NULL if not successful. + * + * Pages can be freed with a call to free_contig_pages(), or by manually + * calling __free_page() for each page allocated. + */ +struct page *find_alloc_contig_pages(unsigned int order, gfp_t gfp, + int nid, nodemask_t *nodemask) +{ + unsigned long pfn, nr_pages, flags; + struct page *ret_page = NULL; + struct zonelist *zonelist; + struct zoneref *z; + struct zone *zone; + int rc; + + nr_pages = 1 << order; + zonelist = node_zonelist(nid, gfp); + for_each_zone_zonelist_nodemask(zone, z, zonelist, gfp_zone(gfp), + nodemask) { + spin_lock_irqsave(&zone->lock, flags); + pfn = ALIGN(zone->zone_start_pfn, nr_pages); + while (zone_spans_pfn(zone, pfn + nr_pages - 1)) { + if (contig_pfn_range_valid(zone, pfn, nr_pages)) { + spin_unlock_irqrestore(&zone->lock, flags); + + rc = alloc_contig_range(pfn, pfn + nr_pages, + MIGRATE_MOVABLE, gfp); + if (!rc) { + ret_page = pfn_to_page(pfn); + return ret_page; + } + spin_lock_irqsave(&zone->lock, flags); + } + pfn += nr_pages; + } + spin_unlock_irqrestore(&zone->lock, flags); + } + + return ret_page; +} +EXPORT_SYMBOL_GPL(find_alloc_contig_pages); + +void free_contig_pages(struct page *page, unsigned nr_pages) +{ + free_contig_range(page_to_pfn(page), nr_pages); +} +EXPORT_SYMBOL_GPL(free_contig_pages); #endif #ifdef CONFIG_MEMORY_HOTPLUG -- 2.13.6