Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp2765648ybh; Mon, 5 Aug 2019 06:30:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqzwMZng/NRrZ7hmasMsWfzLDxM6NCwY1Mxy9Ay5twhpU7XKVlI7lWkl9n1qzHe07a78PB04 X-Received: by 2002:aa7:9481:: with SMTP id z1mr73492399pfk.92.1565011833067; Mon, 05 Aug 2019 06:30:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565011833; cv=none; d=google.com; s=arc-20160816; b=ipMzZw89q4XHAbV+pvMQA6KT0UHCW29uNZjKT+7LrwUCzgwMO9+tYYuwFP3VA/VMeE X+XoDCh0w/9N9G7cKzKcGu3X6B9rVxvp24eU2oXdPPGvbNLKoesiCDdbo7u8mVJ65+CP XNScmtSIpvcYdSsZGZVQkO8b2i0gHLbxygaQHdU3FtE9kbme9yF4e6rVlFp40WIp/piy 8KDYl04pVCNmo+hlIk4PcpYz+wUT5G/bXTJSaROSGZLEsXXpsiUdh0CmXu2RWvcINgw7 t3b0r7jpEQc3UFQviDmusyMzI+eeZA2HXZ2Ya6ZMVEvQM7cd8HLPjEKPWowXCdGmaniA ADvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=bAXzYICbS2VU8u/kCmxcpX11EPYrMgY4ZrhV+MD2sxk=; b=1LyFfbRtui99SGUZ0W1/PnrSdDtG38H+C6LMjO0Vtb9YM2m5QcwV9hh7b1gMuTThjH ToSd0pqtR+KEBDths1Hqah06uxoOjYGHpOlJz53vHx9AHqjgycEm6t93WWFb4jSWZSqA 7te38tKw6JpUSObImST+a67nwBJ5Xu3kYxJNrvqjTXNy4Pe6WPgULBGM8/5YCzlgPsl7 sVwMQK6BSbGqA0pRabTopUq82+eR+7sJ24POquxYxp4nFBZTS0z8TWxy97keNtBIUCTe o7ZPu1yLt54jMZGXSORkrxPM3GidMUuufGXQKTqb4ldoVy0G+cWyu0iehXzfXVkoV80R zu8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=dei+GJRS; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j4si45778946pfi.34.2019.08.05.06.30.17; Mon, 05 Aug 2019 06:30:33 -0700 (PDT) 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=@kernel.org header.s=default header.b=dei+GJRS; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730670AbfHEN2e (ORCPT + 99 others); Mon, 5 Aug 2019 09:28:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:59314 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730723AbfHENWm (ORCPT ); Mon, 5 Aug 2019 09:22:42 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 9CEFF20651; Mon, 5 Aug 2019 13:22:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1565011362; bh=WCr2C9SvA6NcF0TyMrkE44OCdazKei7aqczCmdnv1ac=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dei+GJRSHPxVOW1YNzO+hl0OHkmuTpOf1dtvCeAN73gg/vvieASbE3K1BOx70swP+ HnjXeTn+iuTGPRAi9vUoCGpRZL5RkezyDGW0hCzXz++IYJX/ZuG91NsQim9j4ImVCK UV8md/mSZQnceJkCOiQehRWvk9tpQKNolPsYjudc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Pavel Tatashin , David Hildenbrand , Michal Hocko , Bjorn Helgaas , Borislav Petkov , Dan Williams , Dave Hansen , Dave Hansen , Dave Jiang , Fengguang Wu , Huang Ying , James Morris , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Keith Busch , Ross Zwisler , Sasha Levin , Takashi Iwai , Tom Lendacky , Vishal Verma , Yaowei Bai , Andrew Morton , Linus Torvalds Subject: [PATCH 5.2 064/131] mm/hotplug: make remove_memory() interface usable Date: Mon, 5 Aug 2019 15:02:31 +0200 Message-Id: <20190805124955.739647249@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190805124951.453337465@linuxfoundation.org> References: <20190805124951.453337465@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit eca499ab3749a4537dee77ffead47a1a2c0dee19 ] Presently the remove_memory() interface is inherently broken. It tries to remove memory but panics if some memory is not offline. The problem is that it is impossible to ensure that all memory blocks are offline as this function also takes lock_device_hotplug that is required to change memory state via sysfs. So, between calling this function and offlining all memory blocks there is always a window when lock_device_hotplug is released, and therefore, there is always a chance for a panic during this window. Make this interface to return an error if memory removal fails. This way it is safe to call this function without panicking machine, and also makes it symmetric to add_memory() which already returns an error. Link: http://lkml.kernel.org/r/20190517215438.6487-3-pasha.tatashin@soleen.com Signed-off-by: Pavel Tatashin Reviewed-by: David Hildenbrand Acked-by: Michal Hocko Cc: Bjorn Helgaas Cc: Borislav Petkov Cc: Dan Williams Cc: Dave Hansen Cc: Dave Hansen Cc: Dave Jiang Cc: Fengguang Wu Cc: Huang Ying Cc: James Morris Cc: Jérôme Glisse Cc: Keith Busch Cc: Ross Zwisler Cc: Sasha Levin Cc: Takashi Iwai Cc: Tom Lendacky Cc: Vishal Verma Cc: Yaowei Bai Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- include/linux/memory_hotplug.h | 8 +++-- mm/memory_hotplug.c | 64 +++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 23 deletions(-) diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h index ae892eef8b825..988fde33cd7f5 100644 --- a/include/linux/memory_hotplug.h +++ b/include/linux/memory_hotplug.h @@ -324,7 +324,7 @@ static inline void pgdat_resize_init(struct pglist_data *pgdat) {} extern bool is_mem_section_removable(unsigned long pfn, unsigned long nr_pages); extern void try_offline_node(int nid); extern int offline_pages(unsigned long start_pfn, unsigned long nr_pages); -extern void remove_memory(int nid, u64 start, u64 size); +extern int remove_memory(int nid, u64 start, u64 size); extern void __remove_memory(int nid, u64 start, u64 size); #else @@ -341,7 +341,11 @@ static inline int offline_pages(unsigned long start_pfn, unsigned long nr_pages) return -EINVAL; } -static inline void remove_memory(int nid, u64 start, u64 size) {} +static inline int remove_memory(int nid, u64 start, u64 size) +{ + return -EBUSY; +} + static inline void __remove_memory(int nid, u64 start, u64 size) {} #endif /* CONFIG_MEMORY_HOTREMOVE */ diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index e096c987d2614..77d1f69cdeadd 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -1736,9 +1736,10 @@ static int check_memblock_offlined_cb(struct memory_block *mem, void *arg) endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1; pr_warn("removing memory fails, because memory [%pa-%pa] is onlined\n", &beginpa, &endpa); - } - return ret; + return -EBUSY; + } + return 0; } static int check_cpu_on_node(pg_data_t *pgdat) @@ -1821,19 +1822,9 @@ static void __release_memory_resource(resource_size_t start, } } -/** - * remove_memory - * @nid: the node ID - * @start: physical address of the region to remove - * @size: size of the region to remove - * - * NOTE: The caller must call lock_device_hotplug() to serialize hotplug - * and online/offline operations before this call, as required by - * try_offline_node(). - */ -void __ref __remove_memory(int nid, u64 start, u64 size) +static int __ref try_remove_memory(int nid, u64 start, u64 size) { - int ret; + int rc = 0; BUG_ON(check_hotplug_memory_range(start, size)); @@ -1841,13 +1832,13 @@ void __ref __remove_memory(int nid, u64 start, u64 size) /* * All memory blocks must be offlined before removing memory. Check - * whether all memory blocks in question are offline and trigger a BUG() + * whether all memory blocks in question are offline and return error * if this is not the case. */ - ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL, - check_memblock_offlined_cb); - if (ret) - BUG(); + rc = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL, + check_memblock_offlined_cb); + if (rc) + goto done; /* remove memmap entry */ firmware_map_remove(start, start + size, "System RAM"); @@ -1859,14 +1850,45 @@ void __ref __remove_memory(int nid, u64 start, u64 size) try_offline_node(nid); +done: mem_hotplug_done(); + return rc; } -void remove_memory(int nid, u64 start, u64 size) +/** + * remove_memory + * @nid: the node ID + * @start: physical address of the region to remove + * @size: size of the region to remove + * + * NOTE: The caller must call lock_device_hotplug() to serialize hotplug + * and online/offline operations before this call, as required by + * try_offline_node(). + */ +void __remove_memory(int nid, u64 start, u64 size) +{ + + /* + * trigger BUG() is some memory is not offlined prior to calling this + * function + */ + if (try_remove_memory(nid, start, size)) + BUG(); +} + +/* + * Remove memory if every memory block is offline, otherwise return -EBUSY is + * some memory is not offline + */ +int remove_memory(int nid, u64 start, u64 size) { + int rc; + lock_device_hotplug(); - __remove_memory(nid, start, size); + rc = try_remove_memory(nid, start, size); unlock_device_hotplug(); + + return rc; } EXPORT_SYMBOL_GPL(remove_memory); #endif /* CONFIG_MEMORY_HOTREMOVE */ -- 2.20.1