From patchwork Tue Nov 13 05:51:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 10679609 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 47ECE13B5 for ; Tue, 13 Nov 2018 05:52:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 370D12A312 for ; Tue, 13 Nov 2018 05:52:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 34CE52A3B1; Tue, 13 Nov 2018 05:52:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4231A2A312 for ; Tue, 13 Nov 2018 05:52:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 99A346B0287; Tue, 13 Nov 2018 00:52:02 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 948396B0288; Tue, 13 Nov 2018 00:52:02 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 80EC86B0289; Tue, 13 Nov 2018 00:52:02 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) by kanga.kvack.org (Postfix) with ESMTP id 3C4926B0287 for ; Tue, 13 Nov 2018 00:52:02 -0500 (EST) Received: by mail-pf1-f197.google.com with SMTP id i19-v6so9561502pfi.21 for ; Mon, 12 Nov 2018 21:52:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=E3pLyImoxCJ6IWs3GS5iyhrqwy74dhaAy+xDqAsgCok=; b=BC3pbjddrrYs5w0lLOJ5OK8S3zqfh4rCAEJPvDRTq91nOCTmj/5pKqe5W2AfVZ0XCu yx9mvAwzU6td9HJ8XdJX+06uSLGtUDNP1f001KNlfeDlO+t7fVaFwADtcBc9HkqDB+m/ pllLy4dcgfssWCDnbB7DGvogbHkR3fFj9NyVfgOkHHa6EbQ+EXgicAmPDFPk8Urs02ig zo/MJlTLUAG97mFgCISYj0Lzj+nkJPe24rMCnqYii8n3lmQsqS6lu24il0E6vr4JXw6G +Z+xYQh8co0ln7ZwIAPx6e8i/jgwZPYnwGd5xVD2FifV+tzBNYj0N+SXO6zLjHLfU0Cb BTUg== X-Gm-Message-State: AGRZ1gJos6Kh5Nk+IH9LY4yGb6uvb/kHNahpRarwrH00YCY91skO/dnX Fk30vTLfn7FLiv/0qnGZwgW6/tSzvaNMZxu0E5xqmi1OnfaibIPcaK6dDb1G/r+wAKca6h75u3h D/d1pWcfwL+kBMnquJC2MWgchvsbpCGfwlIrK35J1t2/pIrFX6Wqis7ntOH0GlQqcug== X-Received: by 2002:a63:6506:: with SMTP id z6-v6mr3305033pgb.219.1542088321889; Mon, 12 Nov 2018 21:52:01 -0800 (PST) X-Google-Smtp-Source: AJdET5eFXRhj9iuYowztrBu5jVFHp8xsCcs6zLgQHjz9sUqU4OFR/Hsz6daNZtO1DIdjbqJdHE/B X-Received: by 2002:a63:6506:: with SMTP id z6-v6mr3305006pgb.219.1542088321127; Mon, 12 Nov 2018 21:52:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542088321; cv=none; d=google.com; s=arc-20160816; b=KePsDP+g6/FsLyPmQYwDaSaRqBAJuYWwRrPd89bIq7EgFcjub0FOJWvrqjOM24mKY+ ZoCojdWGSUwBM9VcpLG0ej2idOc53p+BrR3jEekU1eudQgHwmKAeEBprcg93KG5NJY0a I6c433deU+mo77tjYJUnhA7O8zAkEjxwWf6bVb302uZjibQ/1knvq6vpiwriUqfVWRry ZG8XFyLPPnofZHzIH/t/eLNhF8aCVXgX8fC6cQUlWbEuOXXCD+gSUg+b27OHABF0afoH uWPClbFYSuQQbT1n/89u/QzTJfY6lHS4k2r5mJ9eICPERp9iWOByUTlHz6xCW8exzeND Fg5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=E3pLyImoxCJ6IWs3GS5iyhrqwy74dhaAy+xDqAsgCok=; b=Fhv4E97pX66n8kL2NjG32IhHZEUldfy9SGmaGd8MGGjcQFs2TYIRfMQCsF/o69JC7X AbFa2ZpmsGQxie+YLdP1wyWphqeAWJLdF2p6tlYPPI+9wg89DBzwaTVJO3tod8S99m7g t/kK8O4fcAhLCCwGTN4WjxbY9LW3xg3ic+leHAoDOSxpP/B6GL4CXYBBOR8eWG9vfSxD YhbESuKv4K2WZjtNUgx+BS77A0L8pDYjme8kNaDcUh0xAQMR/0sfcly2J2R1h7dwJr35 SLFbhSMXIQKOI7Ivmh5oDPEy2QXfw6kW4lnPTIwTncJBrSp1mHAO2LC+pD28Gt/lmokW Cogg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=OXfAn86H; spf=pass (google.com: domain of sashal@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=sashal@kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from mail.kernel.org (mail.kernel.org. [198.145.29.99]) by mx.google.com with ESMTPS id b13-v6si21459755pfc.156.2018.11.12.21.52.00 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Nov 2018 21:52:01 -0800 (PST) Received-SPF: pass (google.com: domain of sashal@kernel.org designates 198.145.29.99 as permitted sender) client-ip=198.145.29.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=OXfAn86H; spf=pass (google.com: domain of sashal@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=sashal@kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sasha-vm.mshome.net (unknown [64.114.255.114]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E35C72250E; Tue, 13 Nov 2018 05:51:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1542088320; bh=G0rtieCTs+2MB/XSLV2mduzEoQTupZgtep6myjSfa0A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OXfAn86HBfj3VKrU0ll77idA0aAxgxTFj2E2HkaLj7LbflYd/ZtWexyTf44BfPl6h N5/J4IfN8H9di06pzacoWLOT1GCk0z0UKroT78xFGVpI90hRcdkaJpOsLrQfYp2Ybu QXJ3k0cGMokjZDunfEb73PqpgcljO2qWeamAxnGM= From: Sasha Levin To: stable@vger.kernel.org, linux-kernel@vger.kernel.org Cc: David Hildenbrand , Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , "Rafael J. Wysocki" , Len Brown , Greg Kroah-Hartman , Boris Ostrovsky , Juergen Gross , Nathan Fontenot , John Allen , Michal Hocko , Dan Williams , Joonsoo Kim , Vlastimil Babka , Mathieu Malaterre , Pavel Tatashin , YASUAKI ISHIMATSU , Balbir Singh , Haiyang Zhang , Heiko Carstens , Jonathan Corbet , Kate Stewart , "K. Y. Srinivasan" , Martin Schwidefsky , Michael Neuling , Philippe Ombredanne , Stephen Hemminger , Thomas Gleixner , Andrew Morton , Linus Torvalds , Sasha Levin , linuxppc-dev@lists.ozlabs.org, linux-acpi@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH AUTOSEL 4.14 08/26] mm/memory_hotplug: make add_memory() take the device_hotplug_lock Date: Tue, 13 Nov 2018 00:51:32 -0500 Message-Id: <20181113055150.78773-8-sashal@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181113055150.78773-1-sashal@kernel.org> References: <20181113055150.78773-1-sashal@kernel.org> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: David Hildenbrand [ Upstream commit 8df1d0e4a265f25dc1e7e7624ccdbcb4a6630c89 ] add_memory() currently does not take the device_hotplug_lock, however is aleady called under the lock from arch/powerpc/platforms/pseries/hotplug-memory.c drivers/acpi/acpi_memhotplug.c to synchronize against CPU hot-remove and similar. In general, we should hold the device_hotplug_lock when adding memory to synchronize against online/offline request (e.g. from user space) - which already resulted in lock inversions due to device_lock() and mem_hotplug_lock - see 30467e0b3be ("mm, hotplug: fix concurrent memory hot-add deadlock"). add_memory()/add_memory_resource() will create memory block devices, so this really feels like the right thing to do. Holding the device_hotplug_lock makes sure that a memory block device can really only be accessed (e.g. via .online/.state) from user space, once the memory has been fully added to the system. The lock is not held yet in drivers/xen/balloon.c arch/powerpc/platforms/powernv/memtrace.c drivers/s390/char/sclp_cmd.c drivers/hv/hv_balloon.c So, let's either use the locked variants or take the lock. Don't export add_memory_resource(), as it once was exported to be used by XEN, which is never built as a module. If somebody requires it, we also have to export a locked variant (as device_hotplug_lock is never exported). Link: http://lkml.kernel.org/r/20180925091457.28651-3-david@redhat.com Signed-off-by: David Hildenbrand Reviewed-by: Pavel Tatashin Reviewed-by: Rafael J. Wysocki Reviewed-by: Rashmica Gupta Reviewed-by: Oscar Salvador Cc: Benjamin Herrenschmidt Cc: Paul Mackerras Cc: Michael Ellerman Cc: "Rafael J. Wysocki" Cc: Len Brown Cc: Greg Kroah-Hartman Cc: Boris Ostrovsky Cc: Juergen Gross Cc: Nathan Fontenot Cc: John Allen Cc: Michal Hocko Cc: Dan Williams Cc: Joonsoo Kim Cc: Vlastimil Babka Cc: Mathieu Malaterre Cc: Pavel Tatashin Cc: YASUAKI ISHIMATSU Cc: Balbir Singh Cc: Haiyang Zhang Cc: Heiko Carstens Cc: Jonathan Corbet Cc: Kate Stewart Cc: "K. Y. Srinivasan" Cc: Martin Schwidefsky Cc: Michael Neuling Cc: Philippe Ombredanne Cc: Stephen Hemminger Cc: Thomas Gleixner Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- .../platforms/pseries/hotplug-memory.c | 2 +- drivers/acpi/acpi_memhotplug.c | 2 +- drivers/base/memory.c | 9 ++++++-- drivers/xen/balloon.c | 3 +++ include/linux/memory_hotplug.h | 1 + mm/memory_hotplug.c | 22 ++++++++++++++++--- 6 files changed, 32 insertions(+), 7 deletions(-) diff --git a/arch/powerpc/platforms/pseries/hotplug-memory.c b/arch/powerpc/platforms/pseries/hotplug-memory.c index 1d48ab424bd9..c197ea8dde01 100644 --- a/arch/powerpc/platforms/pseries/hotplug-memory.c +++ b/arch/powerpc/platforms/pseries/hotplug-memory.c @@ -784,7 +784,7 @@ static int dlpar_add_lmb(struct of_drconf_cell *lmb) nid = memory_add_physaddr_to_nid(lmb->base_addr); /* Add the memory */ - rc = add_memory(nid, lmb->base_addr, block_sz); + rc = __add_memory(nid, lmb->base_addr, block_sz); if (rc) { dlpar_remove_device_tree_lmb(lmb); return rc; diff --git a/drivers/acpi/acpi_memhotplug.c b/drivers/acpi/acpi_memhotplug.c index 6b0d3ef7309c..2ccfbb61ca89 100644 --- a/drivers/acpi/acpi_memhotplug.c +++ b/drivers/acpi/acpi_memhotplug.c @@ -228,7 +228,7 @@ static int acpi_memory_enable_device(struct acpi_memory_device *mem_device) if (node < 0) node = memory_add_physaddr_to_nid(info->start_addr); - result = add_memory(node, info->start_addr, info->length); + result = __add_memory(node, info->start_addr, info->length); /* * If the memory block has been used by the kernel, add_memory() diff --git a/drivers/base/memory.c b/drivers/base/memory.c index 1d60b58a8c19..9a9aebb4bd19 100644 --- a/drivers/base/memory.c +++ b/drivers/base/memory.c @@ -517,15 +517,20 @@ memory_probe_store(struct device *dev, struct device_attribute *attr, if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1)) return -EINVAL; + ret = lock_device_hotplug_sysfs(); + if (ret) + goto out; + nid = memory_add_physaddr_to_nid(phys_addr); - ret = add_memory(nid, phys_addr, - MIN_MEMORY_BLOCK_SIZE * sections_per_block); + ret = __add_memory(nid, phys_addr, + MIN_MEMORY_BLOCK_SIZE * sections_per_block); if (ret) goto out; ret = count; out: + unlock_device_hotplug(); return ret; } diff --git a/drivers/xen/balloon.c b/drivers/xen/balloon.c index 065f0b607373..6e39a0c10f93 100644 --- a/drivers/xen/balloon.c +++ b/drivers/xen/balloon.c @@ -401,7 +401,10 @@ static enum bp_state reserve_additional_memory(void) * callers drop the mutex before trying again. */ mutex_unlock(&balloon_mutex); + /* add_memory_resource() requires the device_hotplug lock */ + lock_device_hotplug(); rc = add_memory_resource(nid, resource, memhp_auto_online); + unlock_device_hotplug(); mutex_lock(&balloon_mutex); if (rc) { diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h index 58e110aee7ab..d36a02935391 100644 --- a/include/linux/memory_hotplug.h +++ b/include/linux/memory_hotplug.h @@ -316,6 +316,7 @@ static inline void remove_memory(int nid, u64 start, u64 size) {} extern int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn, void *arg, int (*func)(struct memory_block *, void *)); +extern int __add_memory(int nid, u64 start, u64 size); extern int add_memory(int nid, u64 start, u64 size); extern int add_memory_resource(int nid, struct resource *resource, bool online); extern int arch_add_memory(int nid, u64 start, u64 size, bool want_memblock); diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index d4b5f29906b9..dc60f2f27389 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -1109,7 +1109,12 @@ static int online_memory_block(struct memory_block *mem, void *arg) return device_online(&mem->dev); } -/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ +/* + * NOTE: The caller must call lock_device_hotplug() to serialize hotplug + * and online/offline operations (triggered e.g. by sysfs). + * + * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG + */ int __ref add_memory_resource(int nid, struct resource *res, bool online) { u64 start, size; @@ -1202,9 +1207,9 @@ int __ref add_memory_resource(int nid, struct resource *res, bool online) mem_hotplug_done(); return ret; } -EXPORT_SYMBOL_GPL(add_memory_resource); -int __ref add_memory(int nid, u64 start, u64 size) +/* requires device_hotplug_lock, see add_memory_resource() */ +int __ref __add_memory(int nid, u64 start, u64 size) { struct resource *res; int ret; @@ -1218,6 +1223,17 @@ int __ref add_memory(int nid, u64 start, u64 size) release_memory_resource(res); return ret; } + +int add_memory(int nid, u64 start, u64 size) +{ + int rc; + + lock_device_hotplug(); + rc = __add_memory(nid, start, size); + unlock_device_hotplug(); + + return rc; +} EXPORT_SYMBOL_GPL(add_memory); #ifdef CONFIG_MEMORY_HOTREMOVE