Received: by 10.223.185.111 with SMTP id b44csp807064wrg; Fri, 9 Mar 2018 14:12:05 -0800 (PST) X-Google-Smtp-Source: AG47ELuphTpviAWqX63aiKZIrUY/AC4yDjyxy8xzmwyW7jdvEt4IYwN/rzLGN1KQ4A0OuVQAjlhm X-Received: by 2002:a17:902:7798:: with SMTP id o24-v6mr37591pll.52.1520633524905; Fri, 09 Mar 2018 14:12:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520633524; cv=none; d=google.com; s=arc-20160816; b=cmHAb5RdiJUzKigTOh03ryAfvRpYVA/e7Gyb8gvG4gp3gALutYfaTzKcZvKNTmL+rK O6fXT3l1kYAsZ5mRBFIFtK3+5tUrHJy9OyEqUaw6UmMkk7vSSHYOOeZzbLjM7Zq4QNBk 76dtamvA/O3VMfzhjk0/WPn5lsKYPKYomAqRvI3FBUyi3ZTLflgYspl+M5Y6CPqqbCMW 0aD1ryJrlf2A3QDVNl+XhmpfIat7hwPqZX3TNz/Ure5MJhTQ57kUHAwXl6HjMTbwGlNu kCJcl82knd4sQYvCY4RFDD/7Nb35TaP5Ai21hgGy6yv+sSxx2VbZCB7KQsS9kOjlCtaF xPTg== 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:to:from:dkim-signature:arc-authentication-results; bh=Vx8LzKPdbwMU2VQFUsNebemPrDQawUVbMHbNsYWvNSU=; b=Dm3jMSZXfYQpY36Ds921LqimCshDZ5BWFZjqhE7pYsz/a9Df/WkiNtxh4mVZE5trR6 bcVY0NIMiYHOQhRbcRI8wbZvMIlzJaYqvXK+osO2xIgnWifBSYGZ3f/FRI3UnSQAanxy +faaWrRv2ogAcJK0/V3QJNYtG4QE4YBc52HqeVyy/AcHS3IXNlN5xD/ctEkD5YkqDjH7 N/oooxA+HP0I98aDFBowADsHVV2uA8508au7ZvAe0x1bOYrIu1iQmasL4PtiDhkgCAEl lcweqZQHaTnjWR6wCM1JPJ2AMlhDBfCFrXWgFKKJefc9yZf/78IGw3LN+4M2P1tI5eAJ 9Lng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2017-10-26 header.b=YUaP68Ep; 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 r3-v6si1578064plb.197.2018.03.09.14.11.50; Fri, 09 Mar 2018 14:12:04 -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=YUaP68Ep; 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 S932857AbeCIWJf (ORCPT + 99 others); Fri, 9 Mar 2018 17:09:35 -0500 Received: from userp2130.oracle.com ([156.151.31.86]:53328 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932276AbeCIWJd (ORCPT ); Fri, 9 Mar 2018 17:09:33 -0500 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w29M2I2j194941; Fri, 9 Mar 2018 22:08:23 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : subject : date : message-id : in-reply-to : references; s=corp-2017-10-26; bh=Vx8LzKPdbwMU2VQFUsNebemPrDQawUVbMHbNsYWvNSU=; b=YUaP68EpEopFrcDAC7S8MvN1w1B96VEKHzJWjGcDF+OWBQkHrqyFjW7jFuJvbER+tq7R n6pkg4UW3KGAYWJSEwyy2BNiYqqpREs2Bq6wMbIvzoxujRuB25lgAm2ILtSL3HW86lIK /uklbLjWi0nZS5t5fcDb8h7VDB7lU8sICgM7ipB4f2pPMvGmRQnorKxsb/Li3Vkf7zbz qpmH0UimPLuO7bLiX4CKt+e9tBVkQelw7WtzCHhS3McMjn3J4ekdbGneCImf+UgJVpyD ORCdVax6MakCEcUOp+qAPA1LUsNkSDJwCfCbt2U8/0FJMBA18tVPrAQHdAtunujJ3r5o Jw== Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by userp2130.oracle.com with ESMTP id 2gm2pm81su-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 09 Mar 2018 22:08:23 +0000 Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w29M8MN8004696 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 9 Mar 2018 22:08:22 GMT Received: from abhmp0016.oracle.com (abhmp0016.oracle.com [141.146.116.22]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id w29M8HNT026901; Fri, 9 Mar 2018 22:08:17 GMT Received: from localhost.localdomain (/98.216.35.41) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 09 Mar 2018 14:08:17 -0800 From: Pavel Tatashin To: steven.sistare@oracle.com, daniel.m.jordan@oracle.com, pasha.tatashin@oracle.com, m.mizuma@jp.fujitsu.com, akpm@linux-foundation.org, mhocko@suse.com, catalin.marinas@arm.com, takahiro.akashi@linaro.org, gi-oh.kim@profitbricks.com, heiko.carstens@de.ibm.com, baiyaowei@cmss.chinamobile.com, richard.weiyang@gmail.com, paul.burton@mips.com, miles.chen@mediatek.com, vbabka@suse.cz, mgorman@suse.de, hannes@cmpxchg.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [v5 1/2] mm: disable interrupts while initializing deferred pages Date: Fri, 9 Mar 2018 17:08:06 -0500 Message-Id: <20180309220807.24961-2-pasha.tatashin@oracle.com> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180309220807.24961-1-pasha.tatashin@oracle.com> References: <20180309220807.24961-1-pasha.tatashin@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8827 signatures=668688 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-1803090261 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Vlastimil Babka reported about a window issue during which when deferred pages are initialized, and the current version of on-demand initialization is finished, allocations may fail. While this is highly unlikely scenario, since this kind of allocation request must be large, and must come from interrupt handler, we still want to cover it. We solve this by initializing deferred pages with interrupts disabled, and holding node_size_lock spin lock while pages in the node are being initialized. The on-demand deferred page initialization that comes later will use the same lock, and thus synchronize with deferred_init_memmap(). It is unlikely for threads that initialize deferred pages to be interrupted. They run soon after smp_init(), but before modules are initialized, and long before user space programs. This is why there is no adverse effect of having these threads running with interrupts disabled. Signed-off-by: Pavel Tatashin --- include/linux/memory_hotplug.h | 73 +++++++++++++++++++++++++++--------------- include/linux/mmzone.h | 5 +-- mm/page_alloc.c | 22 ++++++------- 3 files changed, 62 insertions(+), 38 deletions(-) diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h index aba5f86eb038..02bfb04823c4 100644 --- a/include/linux/memory_hotplug.h +++ b/include/linux/memory_hotplug.h @@ -51,24 +51,6 @@ enum { MMOP_ONLINE_MOVABLE, }; -/* - * pgdat resizing functions - */ -static inline -void pgdat_resize_lock(struct pglist_data *pgdat, unsigned long *flags) -{ - spin_lock_irqsave(&pgdat->node_size_lock, *flags); -} -static inline -void pgdat_resize_unlock(struct pglist_data *pgdat, unsigned long *flags) -{ - spin_unlock_irqrestore(&pgdat->node_size_lock, *flags); -} -static inline -void pgdat_resize_init(struct pglist_data *pgdat) -{ - spin_lock_init(&pgdat->node_size_lock); -} /* * Zone resizing functions * @@ -246,13 +228,6 @@ extern void clear_zone_contiguous(struct zone *zone); ___page; \ }) -/* - * Stub functions for when hotplug is off - */ -static inline void pgdat_resize_lock(struct pglist_data *p, unsigned long *f) {} -static inline void pgdat_resize_unlock(struct pglist_data *p, unsigned long *f) {} -static inline void pgdat_resize_init(struct pglist_data *pgdat) {} - static inline unsigned zone_span_seqbegin(struct zone *zone) { return 0; @@ -293,6 +268,54 @@ static inline bool movable_node_is_enabled(void) } #endif /* ! CONFIG_MEMORY_HOTPLUG */ +#if defined(CONFIG_MEMORY_HOTPLUG) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT) +/* + * pgdat resizing functions + */ +static inline +void pgdat_resize_lock(struct pglist_data *pgdat, unsigned long *flags) +{ + spin_lock_irqsave(&pgdat->node_size_lock, *flags); +} +static inline +void pgdat_resize_unlock(struct pglist_data *pgdat, unsigned long *flags) +{ + spin_unlock_irqrestore(&pgdat->node_size_lock, *flags); +} +static inline +void pgdat_resize_init(struct pglist_data *pgdat) +{ + spin_lock_init(&pgdat->node_size_lock); +} + +/* Disable interrupts and save previous IRQ state in flags before locking */ +static inline +void pgdat_resize_lock_irq(struct pglist_data *pgdat, unsigned long *flags) +{ + unsigned long tmp_flags; + + local_irq_save(*flags); + local_irq_disable(); + pgdat_resize_lock(pgdat, &tmp_flags); +} + +static inline +void pgdat_resize_unlock_irq(struct pglist_data *pgdat, unsigned long *flags) +{ + pgdat_resize_unlock(pgdat, flags); +} + +#else /* !(CONFIG_MEMORY_HOTPLUG || CONFIG_DEFERRED_STRUCT_PAGE_INIT) */ +/* + * Stub functions for when hotplug is off + */ +static inline void pgdat_resize_lock(struct pglist_data *p, unsigned long *f) {} +static inline void pgdat_resize_unlock(struct pglist_data *p, unsigned long *f) {} +static inline void pgdat_resize_lock_irq(struct pglist_data *p, unsigned long *f) {} +static inline void pgdat_resize_unlock_irq(struct pglist_data *p, unsigned long *f) {} +static inline void pgdat_resize_init(struct pglist_data *pgdat) {} +#endif /* !(CONFIG_MEMORY_HOTPLUG || CONFIG_DEFERRED_STRUCT_PAGE_INIT) */ + #ifdef CONFIG_MEMORY_HOTREMOVE extern bool is_mem_section_removable(unsigned long pfn, unsigned long nr_pages); diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 7522a6987595..d14168da66a7 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -633,14 +633,15 @@ typedef struct pglist_data { #ifndef CONFIG_NO_BOOTMEM struct bootmem_data *bdata; #endif -#ifdef CONFIG_MEMORY_HOTPLUG +#if defined(CONFIG_MEMORY_HOTPLUG) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT) /* * Must be held any time you expect node_start_pfn, node_present_pages * or node_spanned_pages stay constant. Holding this will also * guarantee that any pfn_valid() stays that way. * * pgdat_resize_lock() and pgdat_resize_unlock() are provided to - * manipulate node_size_lock without checking for CONFIG_MEMORY_HOTPLUG. + * manipulate node_size_lock without checking for CONFIG_MEMORY_HOTPLUG + * or CONFIG_DEFERRED_STRUCT_PAGE_INIT. * * Nests above zone->lock and zone->span_seqlock */ diff --git a/mm/page_alloc.c b/mm/page_alloc.c index cb416723538f..55b98df0be39 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1506,7 +1506,6 @@ static void __init deferred_free_pages(int nid, int zid, unsigned long pfn, } else if (!(pfn & nr_pgmask)) { deferred_free_range(pfn - nr_free, nr_free); nr_free = 1; - cond_resched(); } else { nr_free++; } @@ -1533,12 +1532,10 @@ static unsigned long __init deferred_init_pages(int nid, int zid, if (!deferred_pfn_valid(nid, pfn, &nid_init_state)) { page = NULL; continue; - } else if (!page || !(pfn & nr_pgmask)) { + } else if (!page || !(pfn & nr_pgmask)) page = pfn_to_page(pfn); - cond_resched(); - } else { + else page++; - } __init_single_page(page, pfn, zid, nid, true); nr_pages++; } @@ -1552,23 +1549,25 @@ static int __init deferred_init_memmap(void *data) int nid = pgdat->node_id; unsigned long start = jiffies; unsigned long nr_pages = 0; - unsigned long spfn, epfn; + unsigned long spfn, epfn, first_init_pfn, flags; phys_addr_t spa, epa; int zid; struct zone *zone; - unsigned long first_init_pfn = pgdat->first_deferred_pfn; const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); u64 i; + /* Bind memory initialisation thread to a local node if possible */ + if (!cpumask_empty(cpumask)) + set_cpus_allowed_ptr(current, cpumask); + + pgdat_resize_lock_irq(pgdat, &flags); + first_init_pfn = pgdat->first_deferred_pfn; if (first_init_pfn == ULONG_MAX) { + pgdat_resize_unlock_irq(pgdat, &flags); pgdat_init_report_one_done(); return 0; } - /* Bind memory initialisation thread to a local node if possible */ - if (!cpumask_empty(cpumask)) - set_cpus_allowed_ptr(current, cpumask); - /* Sanity check boundaries */ BUG_ON(pgdat->first_deferred_pfn < pgdat->node_start_pfn); BUG_ON(pgdat->first_deferred_pfn > pgdat_end_pfn(pgdat)); @@ -1598,6 +1597,7 @@ static int __init deferred_init_memmap(void *data) epfn = min_t(unsigned long, zone_end_pfn(zone), PFN_DOWN(epa)); deferred_free_pages(nid, zid, spfn, epfn); } + pgdat_resize_unlock_irq(pgdat, &flags); /* Sanity check that the next zone really is unpopulated */ WARN_ON(++zid < MAX_NR_ZONES && populated_zone(++zone)); -- 2.16.2