From patchwork Tue Aug 21 10:44:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10571353 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 C1B181575 for ; Tue, 21 Aug 2018 10:44:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B6D8229DA1 for ; Tue, 21 Aug 2018 10:44:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A9F4829DA4; Tue, 21 Aug 2018 10:44:40 +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=-2.9 required=2.0 tests=BAYES_00,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 AD14F29DA3 for ; Tue, 21 Aug 2018 10:44:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 659A56B1E47; Tue, 21 Aug 2018 06:44:37 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 62FD06B1E48; Tue, 21 Aug 2018 06:44:37 -0400 (EDT) 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 4F9BD6B1E49; Tue, 21 Aug 2018 06:44:37 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt0-f200.google.com (mail-qt0-f200.google.com [209.85.216.200]) by kanga.kvack.org (Postfix) with ESMTP id 24B676B1E47 for ; Tue, 21 Aug 2018 06:44:37 -0400 (EDT) Received: by mail-qt0-f200.google.com with SMTP id d25-v6so15964658qtp.10 for ; Tue, 21 Aug 2018 03:44:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=cJRUAbK8jx13qkAxW02+FfNZ982ZkOEx9gdN+JPnNrU=; b=asDrWEghCcIyWTdQ/QI4sRcL1bkJ3H+MY3Mof1KzW7pc1VqSDbpETWQ2K0EEgBQZ8q Wn5qGeR132J3trnC1Lj7cERHFy80iweVraSfxoSRT9DcSdKBwF76rEj9qNa4B5w9q00b ogcVd8i8OLKWTOiINPNix9sBzDpuO7B5QMNlwtzQbZtaGb6XVum6pnm4NxCSd97lR7+v OiMsC1clx80/A5o2pPkNhG1oYQKzRTk+gKIRsYNia3ngf/KlxeJMpKo4TZfbvuANjXbx N7iujSSvV/DwMv09L7Q54sKAgORoh314EY9FFhcIjJ1GjLyVFNRGXFQtNB/KJS9c2Glj VCEw== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: AOUpUlGS3Y/Moe47SdBBYEUkiDE6CwgCvso0/zAVrnS1+XSAZ3NT70bg KgcT9IFDux70abL+KiuVE17SyJH8BWUdA+SX4diKVMTUMKPd8y3C0bhpfR1BpUXlbez+WTwc/1p WN/SwPlbwfIKnEFzwNAgwHqla8FBpHLpVw5rQI07drwzzJ0vnOSIT1htAgExNSuje1A== X-Received: by 2002:a0c:8a6f:: with SMTP id 44-v6mr44273688qvu.120.1534848276906; Tue, 21 Aug 2018 03:44:36 -0700 (PDT) X-Google-Smtp-Source: AA+uWPw/ttI1f4eY3dXr4sesOyIOGL/96J+et+bR9tjSHbIv7HQucacSbaBR8p7HtVTT0+ZbS3bw X-Received: by 2002:a0c:8a6f:: with SMTP id 44-v6mr44273652qvu.120.1534848276256; Tue, 21 Aug 2018 03:44:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534848276; cv=none; d=google.com; s=arc-20160816; b=0OODRaFGYUZhnJYbZ3yiFrrDNWuNum6JQjbKHgyOw3UmYNcR8yu1szVd+IZKVx9b9E B5nEvQRa9kx33iGRrr1HbDjHQyGkQwP0IJ4rlMQJbSgJkXuBgs91f3fCGP8+gE1qUhdT 26euoWxBfjn0Cy27lYoeE0BGZIm+73EE3mweJefnWCwYUH2qbLTjNgc25KVGcuckQE6o DtvtFa3yYZuQFQvJhphE6Pv7KzW3RW3xhoVMqt6/0C7GIBiXHPNu4DSW/QnwoV9FoPq+ 9Mn0YAuC5jrv96cld521cA07VLXVqs7Owyox31OwPOF69eDx0N3mD7J1KfczfqNh5MAo KUOw== 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 :arc-authentication-results; bh=cJRUAbK8jx13qkAxW02+FfNZ982ZkOEx9gdN+JPnNrU=; b=bPRDhXZ5qnZ3RyyvmZFgmgWEjWh1OX/1v2lKNXBJTTcHhMyoRGRNXBpDuzmipXbr43 EjbCAIiBWY1wZT5m82jBLOYaUvspr8yaBZ+4X5Bu3eaZUHwAVTxKE0U0s2eiGM4G2i7x XP7emS4WFTeK8bG+KITWrhI/ijciZK28E+AB/ea5uFBef1mVFhIy0ZFRmQ22gGQzq7i9 tquXr2q/O5NiUGjFcHwy+xBW6kh+vibjTWATsby1C8bQbrdvGaxm1k4ObpF1Tj1Up0TF 89CJux8ziwo61y237M2ENpQJNMBrTjREIW8YdYoDlpE3u2b1esHE6BByBIIWW5JC5WFq tyEQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx3-rdu2.redhat.com. [66.187.233.73]) by mx.google.com with ESMTPS id n85-v6si1781566qkh.273.2018.08.21.03.44.36 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Aug 2018 03:44:36 -0700 (PDT) Received-SPF: pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) client-ip=66.187.233.73; Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AD4F340216F2; Tue, 21 Aug 2018 10:44:35 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-96.ams2.redhat.com [10.36.117.96]) by smtp.corp.redhat.com (Postfix) with ESMTP id A5C5C94641; Tue, 21 Aug 2018 10:44:31 +0000 (UTC) From: David Hildenbrand To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-acpi@vger.kernel.org, xen-devel@lists.xenproject.org, devel@linuxdriverproject.org, David Hildenbrand , Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , "Rafael J. Wysocki" , Len Brown , Rashmica Gupta , Michael Neuling , Balbir Singh , Nathan Fontenot , John Allen , Andrew Morton , Michal Hocko , Dan Williams , Joonsoo Kim , Vlastimil Babka , Pavel Tatashin , Greg Kroah-Hartman , Oscar Salvador , YASUAKI ISHIMATSU , Mathieu Malaterre Subject: [PATCH RFCv2 1/6] mm/memory_hotplug: make remove_memory() take the device_hotplug_lock Date: Tue, 21 Aug 2018 12:44:13 +0200 Message-Id: <20180821104418.12710-2-david@redhat.com> In-Reply-To: <20180821104418.12710-1-david@redhat.com> References: <20180821104418.12710-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Tue, 21 Aug 2018 10:44:35 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Tue, 21 Aug 2018 10:44:35 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'david@redhat.com' RCPT:'' 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 remove_memory() is exported right now but requires the device_hotplug_lock, which is not exported. So let's provide a variant that takes the lock and only export that one. The lock is already held in arch/powerpc/platforms/pseries/hotplug-memory.c drivers/acpi/acpi_memhotplug.c So, let's use the locked variant. The lock is not held (but taken in) arch/powerpc/platforms/powernv/memtrace.c So let's keep using the (now) locked variant. Apart from that, there are not other users in the tree. Cc: Benjamin Herrenschmidt Cc: Paul Mackerras Cc: Michael Ellerman Cc: "Rafael J. Wysocki" Cc: Len Brown Cc: Rashmica Gupta Cc: Michael Neuling Cc: Balbir Singh Cc: Nathan Fontenot Cc: John Allen Cc: Andrew Morton Cc: Michal Hocko Cc: Dan Williams Cc: Joonsoo Kim Cc: Vlastimil Babka Cc: Pavel Tatashin Cc: Greg Kroah-Hartman Cc: Oscar Salvador Cc: YASUAKI ISHIMATSU Cc: Mathieu Malaterre Signed-off-by: David Hildenbrand Reviewed-by: Pavel Tatashin --- arch/powerpc/platforms/powernv/memtrace.c | 2 -- arch/powerpc/platforms/pseries/hotplug-memory.c | 6 +++--- drivers/acpi/acpi_memhotplug.c | 2 +- include/linux/memory_hotplug.h | 3 ++- mm/memory_hotplug.c | 9 ++++++++- 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/arch/powerpc/platforms/powernv/memtrace.c b/arch/powerpc/platforms/powernv/memtrace.c index 51dc398ae3f7..8f1cd4f3bfd5 100644 --- a/arch/powerpc/platforms/powernv/memtrace.c +++ b/arch/powerpc/platforms/powernv/memtrace.c @@ -90,9 +90,7 @@ static bool memtrace_offline_pages(u32 nid, u64 start_pfn, u64 nr_pages) walk_memory_range(start_pfn, end_pfn, (void *)MEM_OFFLINE, change_memblock_state); - lock_device_hotplug(); remove_memory(nid, start_pfn << PAGE_SHIFT, nr_pages << PAGE_SHIFT); - unlock_device_hotplug(); return true; } diff --git a/arch/powerpc/platforms/pseries/hotplug-memory.c b/arch/powerpc/platforms/pseries/hotplug-memory.c index c1578f54c626..b3f54466e25f 100644 --- a/arch/powerpc/platforms/pseries/hotplug-memory.c +++ b/arch/powerpc/platforms/pseries/hotplug-memory.c @@ -334,7 +334,7 @@ static int pseries_remove_memblock(unsigned long base, unsigned int memblock_siz nid = memory_add_physaddr_to_nid(base); for (i = 0; i < sections_per_block; i++) { - remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE); + __remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE); base += MIN_MEMORY_BLOCK_SIZE; } @@ -423,7 +423,7 @@ static int dlpar_remove_lmb(struct drmem_lmb *lmb) block_sz = pseries_memory_block_size(); nid = memory_add_physaddr_to_nid(lmb->base_addr); - remove_memory(nid, lmb->base_addr, block_sz); + __remove_memory(nid, lmb->base_addr, block_sz); /* Update memory regions for memory remove */ memblock_remove(lmb->base_addr, block_sz); @@ -710,7 +710,7 @@ static int dlpar_add_lmb(struct drmem_lmb *lmb) rc = dlpar_online_lmb(lmb); if (rc) { - remove_memory(nid, lmb->base_addr, block_sz); + __remove_memory(nid, lmb->base_addr, block_sz); dlpar_remove_device_tree_lmb(lmb); } else { lmb->flags |= DRCONF_MEM_ASSIGNED; diff --git a/drivers/acpi/acpi_memhotplug.c b/drivers/acpi/acpi_memhotplug.c index 6b0d3ef7309c..811148415993 100644 --- a/drivers/acpi/acpi_memhotplug.c +++ b/drivers/acpi/acpi_memhotplug.c @@ -282,7 +282,7 @@ static void acpi_memory_remove_memory(struct acpi_memory_device *mem_device) nid = memory_add_physaddr_to_nid(info->start_addr); acpi_unbind_memory_blocks(info); - remove_memory(nid, info->start_addr, info->length); + __remove_memory(nid, info->start_addr, info->length); list_del(&info->list); kfree(info); } diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h index 34a28227068d..1f096852f479 100644 --- a/include/linux/memory_hotplug.h +++ b/include/linux/memory_hotplug.h @@ -301,6 +301,7 @@ 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 void __remove_memory(int nid, u64 start, u64 size); #else static inline bool is_mem_section_removable(unsigned long pfn, @@ -317,6 +318,7 @@ static inline int offline_pages(unsigned long start_pfn, unsigned long nr_pages) } static inline void remove_memory(int nid, u64 start, u64 size) {} +static inline void __remove_memory(int nid, u64 start, u64 size) {} #endif /* CONFIG_MEMORY_HOTREMOVE */ extern void __ref free_area_init_core_hotplug(int nid); @@ -330,7 +332,6 @@ extern void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, unsigned long nr_pages, struct vmem_altmap *altmap); extern int offline_pages(unsigned long start_pfn, unsigned long nr_pages); extern bool is_memblock_offlined(struct memory_block *mem); -extern void remove_memory(int nid, u64 start, u64 size); extern int sparse_add_one_section(struct pglist_data *pgdat, unsigned long start_pfn, struct vmem_altmap *altmap); extern void sparse_remove_one_section(struct zone *zone, struct mem_section *ms, diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 9eea6e809a4e..898e13d4d87d 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -1872,7 +1872,7 @@ EXPORT_SYMBOL(try_offline_node); * and online/offline operations before this call, as required by * try_offline_node(). */ -void __ref remove_memory(int nid, u64 start, u64 size) +void __ref __remove_memory(int nid, u64 start, u64 size) { int ret; @@ -1901,5 +1901,12 @@ void __ref remove_memory(int nid, u64 start, u64 size) mem_hotplug_done(); } + +void __ref remove_memory(int nid, u64 start, u64 size) +{ + lock_device_hotplug(); + __remove_memory(nid, start, size); + unlock_device_hotplug(); +} EXPORT_SYMBOL_GPL(remove_memory); #endif /* CONFIG_MEMORY_HOTREMOVE */ From patchwork Tue Aug 21 10:44:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10571355 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 63B501575 for ; Tue, 21 Aug 2018 10:44:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 59CAD29DA1 for ; Tue, 21 Aug 2018 10:44:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4BCAB29DA7; Tue, 21 Aug 2018 10:44:45 +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=-2.9 required=2.0 tests=BAYES_00,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 2496229DA1 for ; Tue, 21 Aug 2018 10:44:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C736A6B1E49; Tue, 21 Aug 2018 06:44:41 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id C24E16B1E4B; Tue, 21 Aug 2018 06:44:41 -0400 (EDT) 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 B12866B1E4C; Tue, 21 Aug 2018 06:44:41 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt0-f198.google.com (mail-qt0-f198.google.com [209.85.216.198]) by kanga.kvack.org (Postfix) with ESMTP id 7FBAC6B1E49 for ; Tue, 21 Aug 2018 06:44:41 -0400 (EDT) Received: by mail-qt0-f198.google.com with SMTP id e14-v6so15847061qtp.17 for ; Tue, 21 Aug 2018 03:44:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=H6wxi0sGOqx0bKLWqqpuO7Gvhuzl26jJZhmT9a7LIjE=; b=KM4LLv7kluGX3a/6LXlCZz2Scq8uXswRGmknrj3BSdvuTpwN0wUYm0iVabFbQGxxnw XnhpZX0oxsLWb9Etz8dZToZqyg9F8VBnODEakLRmIusVOt47l9gecgpca0IqkEd4cu4X +vJUyzRpVCgBsItvPlWPruc8XoMwXQBb834z80QDkfbPFdFggKsBeIzFjX+YKdlqaJma X6y/o8oD8y7A9f9WiPNNSTFBHDzGaPOU5N/doQJLCeKtcEwMUMmbwJw89ComrSrWg8lI F2/SSoZnSp0B1WDL+k14L4TY/CSpy1XyqGkInzgyhuPv1pcG+Y0i9ev1qwplJEm2GtB2 NBLQ== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: AOUpUlGJQrUwQsVMx+d4nLvvAhZfFX4UpEUM5cFeFQmW3V7yrvlVvk+Z Nx7GkxxEmITaRLP+i/Zksj0TnFz6WNpaj0UVIUyT3j1wPxRV+wsXa7bbTjvhR+YJ6tKiiuI1Izl L2/Pzmur8zP3UFw8mR4USiyilP/07x+o/8KlG15S8GinxgnvwKe6ff5gkwCVEYzIx2Q== X-Received: by 2002:a37:7883:: with SMTP id t125-v6mr46844961qkc.334.1534848281200; Tue, 21 Aug 2018 03:44:41 -0700 (PDT) X-Google-Smtp-Source: AA+uWPxAb/p7dk4KYc/FcR4bSErhPccEiPjBDlrp/qpo4kMXux9WYpKg9yE2FM07ziEfNjKM1hdz X-Received: by 2002:a37:7883:: with SMTP id t125-v6mr46844919qkc.334.1534848280425; Tue, 21 Aug 2018 03:44:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534848280; cv=none; d=google.com; s=arc-20160816; b=jfazdx0ufF345xx0KYehV9m2VN0xOQwPlMDqvA9w43MMTB+QXPgWJ0Jjz24i94d9R4 vHltziyUWNp4xH8GF7dGx9Rrdlh55h3UDUvsjeeBJDMXMXL9H/V/+HSk0rARIvGUM4Tb b7Gc0Rtj+GXouRGSI8PXxpVIUwq/6+htor5S4mmcJNoRiRZODJuiyqE3LVh1sBwoYQtO D+AdlebFQ+wciqAuX1mFxncpHWhjQZRB0wE6MuVzOu4CnslwCqp9HBM9rQ8MyUlML24S tBt9c0SKzo2NoJgcI/1/LdcrPQYLRvc/tjgjU7hYPTgTBwefTfZpn/5uLGA7cXxq094M 96/g== 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 :arc-authentication-results; bh=H6wxi0sGOqx0bKLWqqpuO7Gvhuzl26jJZhmT9a7LIjE=; b=TLDua1Kv5miQEuevWCcXv7slHlvD81OaiYMBW+8Kh3LxNpMYoKRQ0GcTIlV7K/WzIp PpNg5aKmNh28vNfQRVRb4W7rwxkz4d+WXHyglclrUVDwkAHTYNncn0CLOuJ1eII2sq2Z nfmu1tsn50eNtE8wIjDYR/OhekKQ25jtJjjnojvTxn0yvDIyzXLOH2FxZeMUy2Mt6qPR zc0l3hxI6yKTW+WjorkjQBpBftuQWLGrHsrW/ZUHCFMEUo7hSd7pMBiMUqDGI5arPxGd XoMGpGseljQ9bnzIjBZexE1HJ4QQzZJmrZIE2SHp82NYP2PnuYUh86EHgfyI9b3vuAjI 6a3A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx3-rdu2.redhat.com. [66.187.233.73]) by mx.google.com with ESMTPS id f7-v6si8208209qvl.25.2018.08.21.03.44.40 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Aug 2018 03:44:40 -0700 (PDT) Received-SPF: pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) client-ip=66.187.233.73; Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DA86F40006EC; Tue, 21 Aug 2018 10:44:39 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-96.ams2.redhat.com [10.36.117.96]) by smtp.corp.redhat.com (Postfix) with ESMTP id F3B307D4C9; Tue, 21 Aug 2018 10:44:35 +0000 (UTC) From: David Hildenbrand To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-acpi@vger.kernel.org, xen-devel@lists.xenproject.org, devel@linuxdriverproject.org, David Hildenbrand , Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , "Rafael J. Wysocki" , Len Brown , Greg Kroah-Hartman , Boris Ostrovsky , Juergen Gross , Nathan Fontenot , John Allen , Andrew Morton , Michal Hocko , Dan Williams , Joonsoo Kim , Vlastimil Babka , Oscar Salvador , Mathieu Malaterre , Pavel Tatashin , YASUAKI ISHIMATSU Subject: [PATCH RFCv2 2/6] mm/memory_hotplug: make add_memory() take the device_hotplug_lock Date: Tue, 21 Aug 2018 12:44:14 +0200 Message-Id: <20180821104418.12710-3-david@redhat.com> In-Reply-To: <20180821104418.12710-1-david@redhat.com> References: <20180821104418.12710-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Tue, 21 Aug 2018 10:44:40 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Tue, 21 Aug 2018 10:44:40 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'david@redhat.com' RCPT:'' 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 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). 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: Andrew Morton Cc: Michal Hocko Cc: Dan Williams Cc: Joonsoo Kim Cc: Vlastimil Babka Cc: Oscar Salvador Cc: Mathieu Malaterre Cc: Pavel Tatashin Cc: YASUAKI ISHIMATSU Signed-off-by: David Hildenbrand Reviewed-by: Pavel Tatashin --- .../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 b3f54466e25f..2e6f41dc103a 100644 --- a/arch/powerpc/platforms/pseries/hotplug-memory.c +++ b/arch/powerpc/platforms/pseries/hotplug-memory.c @@ -702,7 +702,7 @@ static int dlpar_add_lmb(struct drmem_lmb *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 811148415993..8fe0960ea572 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 c8a1cb0b6136..5b0375be7f65 100644 --- a/drivers/base/memory.c +++ b/drivers/base/memory.c @@ -521,15 +521,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 e12bb256036f..6bab019a82b1 100644 --- a/drivers/xen/balloon.c +++ b/drivers/xen/balloon.c @@ -395,7 +395,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 1f096852f479..ffd9cd10fcf3 100644 --- a/include/linux/memory_hotplug.h +++ b/include/linux/memory_hotplug.h @@ -324,6 +324,7 @@ static inline void __remove_memory(int nid, u64 start, u64 size) {} extern void __ref free_area_init_core_hotplug(int nid); 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, diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 898e13d4d87d..e2b5c751e3ea 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -1111,7 +1111,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; @@ -1180,9 +1185,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; @@ -1196,6 +1201,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 From patchwork Tue Aug 21 10:44:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10571361 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 8BEA8139B for ; Tue, 21 Aug 2018 10:44:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8486D29DA3 for ; Tue, 21 Aug 2018 10:44:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7823029DC7; Tue, 21 Aug 2018 10:44:53 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable 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 13CD129DA3 for ; Tue, 21 Aug 2018 10:44:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D852B6B1E4B; Tue, 21 Aug 2018 06:44:48 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id D5C496B1E4C; Tue, 21 Aug 2018 06:44:48 -0400 (EDT) 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 C244E6B1E4D; Tue, 21 Aug 2018 06:44:48 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt0-f197.google.com (mail-qt0-f197.google.com [209.85.216.197]) by kanga.kvack.org (Postfix) with ESMTP id 94C2A6B1E4B for ; Tue, 21 Aug 2018 06:44:48 -0400 (EDT) Received: by mail-qt0-f197.google.com with SMTP id u13-v6so4410357qtb.18 for ; Tue, 21 Aug 2018 03:44:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=ezISOvc5dCk2vdD23jrvQFGiO9VD9CF+Fezi3KlQSak=; b=jYdu92r/rINwZ8v8BWF+SerIZsGQsjFItpvThk2Xjt2E/CWUkGZI6+Yfi78eR2gQKp /iYcmcl8+oWdcP/1WgRR7V5GrpU+f/LUGh9NFh6V4GO5mOZCWLsPiccOb63Tjh8HDP0P 8GaaKW753UHRIgP89jaxBvz+VudzLBlsB0On3IaaBMVCVQDNJ10UFUYgldiZxqGqho13 InXm4+2uu7FyUuniFnp7sAH6YSHM9HDuzSAdhPVReol8fC+0/sma5bqFTeERM3+4P074 iEBixwdIxm9MArIhZw0aIBdCEnnYmftrFX2x+BmSHtR3Gv1PhT4/5ffFN3bpXY/WDrjz Tbgw== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: AOUpUlEYZc+pt39eUYd77oN1npkG0xErkb/TaMgvHA8X7lQuS3MjSENl h+WPQckDW0TraB9ETt61PpTl9sdF7EclsyXPaT9zaTdrySleN3uEnC/Jbwb1EpMpb39ljWmnmTw v62z24aJTZMZ8rZp1MNM3B+wAMbmtPf49kgtYqJOXg6Ciwd9F/fNKTzk1ih7Acosl5Q== X-Received: by 2002:ac8:1a46:: with SMTP id q6-v6mr49767920qtk.5.1534848288342; Tue, 21 Aug 2018 03:44:48 -0700 (PDT) X-Google-Smtp-Source: AA+uWPzxV78W89AEaq1A3ol+TR5qnQbBM/nFOKe61YB+5CpEqeZGSHY20DJBZO9rY0m3qLyJ63Qm X-Received: by 2002:ac8:1a46:: with SMTP id q6-v6mr49767879qtk.5.1534848287497; Tue, 21 Aug 2018 03:44:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534848287; cv=none; d=google.com; s=arc-20160816; b=tFGQTR6NM28gNmzQTF6/yG/fkVRcwSy8pANBKpUpyKM1EA1ZuHhu+0godRv13CjTIZ Hdyzrw6R/2ZB+UOU4SeGZx2EAI3EJtAPZO0Fr4lP1brcODKwFAKAH9Hsx1WCX+UtlaiF mlATZzY8t7z0LwuPiNIBjJiwWSkt3lNrOkV49OYws+8RxG9yRtujNHzTq+MiKP9oVAQJ mz+RlpGqp38PWQEsR34lSOvfWOsXDTHsMmYNsbN2sJtX8OEfCLuzs4sd54+Hb2UooK0G WLhk5SsvsaUz7OlqNQoaojpb0GiX9m2Vy4XTxdoOuSTay/rHZ28oD5xvNpvG4uSbCOby zl5w== 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 :arc-authentication-results; bh=ezISOvc5dCk2vdD23jrvQFGiO9VD9CF+Fezi3KlQSak=; b=xSvsyPgSa9izJpNRF7EPa3ZSvHX6JSd041zRSNhdXEpNXrEYm1K3Y/xRyfEtSgdHJc gLhD0Hi+2Vs3kAbY+Pw34kMsCEU4EXWjVDg3md29CajKwcALQwWgB1gJKEU0354gktx7 gPHcFjAetE2ep7vJzR/BpBJ3/eUXh/niDAMg8oMgmLhJ7e87A1SDpu7fLT3pnwrUNV3S 3yBuRj+fpFxA4rWi6JX6hsScJu/V16ImxSs4tmWyDHTql2VyGAED1GvGnHfXveC1oub0 mJzGWMZb3heUyGcdTjBjEBUoulfJjHw4nGQAUP389bBd46j6ew4VGqrdG+7JRzwifZdb Eitg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx3-rdu2.redhat.com. [66.187.233.73]) by mx.google.com with ESMTPS id j8-v6si10467643qkm.293.2018.08.21.03.44.47 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Aug 2018 03:44:47 -0700 (PDT) Received-SPF: pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) client-ip=66.187.233.73; Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D872D8068B0B; Tue, 21 Aug 2018 10:44:46 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-96.ams2.redhat.com [10.36.117.96]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2AD0F9463B; Tue, 21 Aug 2018 10:44:40 +0000 (UTC) From: David Hildenbrand To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-acpi@vger.kernel.org, xen-devel@lists.xenproject.org, devel@linuxdriverproject.org, David Hildenbrand , Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , "Rafael J. Wysocki" , Len Brown , Greg Kroah-Hartman , "K. Y. Srinivasan" , Haiyang Zhang , Stephen Hemminger , Martin Schwidefsky , Heiko Carstens , Boris Ostrovsky , Juergen Gross , Rashmica Gupta , Michael Neuling , Balbir Singh , Kate Stewart , Thomas Gleixner , Philippe Ombredanne , Andrew Morton , Michal Hocko , Pavel Tatashin , Vlastimil Babka , Dan Williams , Oscar Salvador , YASUAKI ISHIMATSU , Mathieu Malaterre Subject: [PATCH RFCv2 3/6] mm/memory_hotplug: fix online/offline_pages called w.o. mem_hotplug_lock Date: Tue, 21 Aug 2018 12:44:15 +0200 Message-Id: <20180821104418.12710-4-david@redhat.com> In-Reply-To: <20180821104418.12710-1-david@redhat.com> References: <20180821104418.12710-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 21 Aug 2018 10:44:47 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 21 Aug 2018 10:44:47 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'david@redhat.com' RCPT:'' 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 There seem to be some problems as result of 30467e0b3be ("mm, hotplug: fix concurrent memory hot-add deadlock"), which tried to fix a possible lock inversion reported and discussed in [1] due to the two locks a) device_lock() b) mem_hotplug_lock While add_memory() first takes b), followed by a) during bus_probe_device(), onlining of memory from user space first took b), followed by a), exposing a possible deadlock. In [1], and it was decided to not make use of device_hotplug_lock, but rather to enforce a locking order. The problems I spotted related to this: 1. Memory block device attributes: While .state first calls mem_hotplug_begin() and the calls device_online() - which takes device_lock() - .online does no longer call mem_hotplug_begin(), so effectively calls online_pages() without mem_hotplug_lock. 2. device_online() should be called under device_hotplug_lock, however onlining memory during add_memory() does not take care of that. In addition, I think there is also something wrong about the locking in 3. arch/powerpc/platforms/powernv/memtrace.c calls offline_pages() without locks. This was introduced after 30467e0b3be. And skimming over the code, I assume it could need some more care in regards to locking (e.g. device_online() called without device_hotplug_lock - but I'll not touch that for now). Now that we hold the device_hotplug_lock when - adding memory (e.g. via add_memory()/add_memory_resource()) - removing memory (e.g. via remove_memory()) - device_online()/device_offline() We can move mem_hotplug_lock usage back into online_pages()/offline_pages(). Why is mem_hotplug_lock still needed? Essentially to make get_online_mems()/put_online_mems() be very fast (relying on device_hotplug_lock would be very slow), and to serialize against addition of memory that does not create memory block devices (hmm). [1] http://driverdev.linuxdriverproject.org/pipermail/ driverdev-devel/ 2015-February/065324.html This patch is partly based on a patch by Vitaly Kuznetsov. Cc: Benjamin Herrenschmidt Cc: Paul Mackerras Cc: Michael Ellerman Cc: "Rafael J. Wysocki" Cc: Len Brown Cc: Greg Kroah-Hartman Cc: "K. Y. Srinivasan" Cc: Haiyang Zhang Cc: Stephen Hemminger Cc: Martin Schwidefsky Cc: Heiko Carstens Cc: Boris Ostrovsky Cc: Juergen Gross Cc: Rashmica Gupta Cc: Michael Neuling Cc: Balbir Singh Cc: Kate Stewart Cc: Thomas Gleixner Cc: Philippe Ombredanne Cc: Andrew Morton Cc: Michal Hocko Cc: Pavel Tatashin Cc: Vlastimil Babka Cc: Dan Williams Cc: Oscar Salvador Cc: YASUAKI ISHIMATSU Cc: Mathieu Malaterre Signed-off-by: David Hildenbrand Reviewed-by: Pavel Tatashin Reviewed-by: Rashmica Gupta --- drivers/base/memory.c | 13 +------------ mm/memory_hotplug.c | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/drivers/base/memory.c b/drivers/base/memory.c index 5b0375be7f65..04be13539eb8 100644 --- a/drivers/base/memory.c +++ b/drivers/base/memory.c @@ -228,7 +228,6 @@ static bool pages_correctly_probed(unsigned long start_pfn) /* * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is * OK to have direct references to sparsemem variables in here. - * Must already be protected by mem_hotplug_begin(). */ static int memory_block_action(unsigned long phys_index, unsigned long action, int online_type) @@ -294,7 +293,6 @@ static int memory_subsys_online(struct device *dev) if (mem->online_type < 0) mem->online_type = MMOP_ONLINE_KEEP; - /* Already under protection of mem_hotplug_begin() */ ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE); /* clear online_type */ @@ -341,19 +339,11 @@ store_mem_state(struct device *dev, goto err; } - /* - * Memory hotplug needs to hold mem_hotplug_begin() for probe to find - * the correct memory block to online before doing device_online(dev), - * which will take dev->mutex. Take the lock early to prevent an - * inversion, memory_subsys_online() callbacks will be implemented by - * assuming it's already protected. - */ - mem_hotplug_begin(); - switch (online_type) { case MMOP_ONLINE_KERNEL: case MMOP_ONLINE_MOVABLE: case MMOP_ONLINE_KEEP: + /* mem->online_type is protected by device_hotplug_lock */ mem->online_type = online_type; ret = device_online(&mem->dev); break; @@ -364,7 +354,6 @@ store_mem_state(struct device *dev, ret = -EINVAL; /* should never happen */ } - mem_hotplug_done(); err: unlock_device_hotplug(); diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index e2b5c751e3ea..a2c6c87d83f3 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -881,7 +881,6 @@ static struct zone * __meminit move_pfn_range(int online_type, int nid, return zone; } -/* Must be protected by mem_hotplug_begin() or a device_lock */ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type) { unsigned long flags; @@ -893,6 +892,8 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_typ struct memory_notify arg; struct memory_block *mem; + mem_hotplug_begin(); + /* * We can't use pfn_to_nid() because nid might be stored in struct page * which is not yet initialized. Instead, we find nid from memory block. @@ -957,6 +958,7 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_typ if (onlined_pages) memory_notify(MEM_ONLINE, &arg); + mem_hotplug_done(); return 0; failed_addition: @@ -964,6 +966,7 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_typ (unsigned long long) pfn << PAGE_SHIFT, (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1); memory_notify(MEM_CANCEL_ONLINE, &arg); + mem_hotplug_done(); return ret; } #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ @@ -1168,20 +1171,20 @@ int __ref add_memory_resource(int nid, struct resource *res, bool online) /* create new memmap entry */ firmware_map_add_hotplug(start, start + size, "System RAM"); + /* device_online() will take the lock when calling online_pages() */ + mem_hotplug_done(); + /* online pages if requested */ if (online) walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL, online_memory_block); - goto out; - + return ret; error: /* rollback pgdat allocation and others */ if (new_node) rollback_node_hotadd(nid); memblock_remove(start, size); - -out: mem_hotplug_done(); return ret; } @@ -1621,10 +1624,16 @@ static int __ref __offline_pages(unsigned long start_pfn, return -EINVAL; if (!IS_ALIGNED(end_pfn, pageblock_nr_pages)) return -EINVAL; + + mem_hotplug_begin(); + /* This makes hotplug much easier...and readable. we assume this for now. .*/ - if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end)) + if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, + &valid_end)) { + mem_hotplug_done(); return -EINVAL; + } zone = page_zone(pfn_to_page(valid_start)); node = zone_to_nid(zone); @@ -1633,8 +1642,10 @@ static int __ref __offline_pages(unsigned long start_pfn, /* set above range as isolated */ ret = start_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE, true); - if (ret) + if (ret) { + mem_hotplug_done(); return ret; + } arg.start_pfn = start_pfn; arg.nr_pages = nr_pages; @@ -1705,6 +1716,7 @@ static int __ref __offline_pages(unsigned long start_pfn, writeback_set_ratelimit(); memory_notify(MEM_OFFLINE, &arg); + mem_hotplug_done(); return 0; failed_removal: @@ -1714,10 +1726,10 @@ static int __ref __offline_pages(unsigned long start_pfn, memory_notify(MEM_CANCEL_OFFLINE, &arg); /* pushback to free area */ undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE); + mem_hotplug_done(); return ret; } -/* Must be protected by mem_hotplug_begin() or a device_lock */ int offline_pages(unsigned long start_pfn, unsigned long nr_pages) { return __offline_pages(start_pfn, start_pfn + nr_pages); From patchwork Tue Aug 21 10:44:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10571363 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 C8F6E1803 for ; Tue, 21 Aug 2018 10:44:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BD3D829DA1 for ; Tue, 21 Aug 2018 10:44:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B106929DA3; Tue, 21 Aug 2018 10:44:53 +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=-2.9 required=2.0 tests=BAYES_00,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 E9EFE29DA7 for ; Tue, 21 Aug 2018 10:44:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 98EB36B1E4C; Tue, 21 Aug 2018 06:44:50 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 968BB6B1E4D; Tue, 21 Aug 2018 06:44:50 -0400 (EDT) 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 808716B1E4E; Tue, 21 Aug 2018 06:44:50 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt0-f199.google.com (mail-qt0-f199.google.com [209.85.216.199]) by kanga.kvack.org (Postfix) with ESMTP id 507DB6B1E4C for ; Tue, 21 Aug 2018 06:44:50 -0400 (EDT) Received: by mail-qt0-f199.google.com with SMTP id e14-v6so15847245qtp.17 for ; Tue, 21 Aug 2018 03:44:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=YOn2XEJJjUAF950/2+ACrSpbLnm+unK2V2ttVzDG1Lg=; b=PYOr+wYqPubqTUVKudoz2C67FBWaVs/YiGbR86TDZDorUJq3d9XiYgOYleAltSCo5L 6oQzcD2Sn/IsN7n/3Hf47XEny5ABWaqYyJA6FQ4sd1Pvqvp17V/L1BOsNve4gKdX3pjY 1tnt/eQaDdE0XwiTmZChnQ42znyfVKCYnR+hS8vptzfdGIfiQTgYy/BfjzaLbyhMSc0X 1wWc549VgUxxBtZyjbUkiSpuU9d5HwBfMY6d2oFi0YzEk4nrWQtCUKaNPvfEjkgceFN/ hFlVOOzCNdDqjfBO4aj7vZ6cmZInG2KdNg/2prFeQypie8dMZhUFP0P1ccFNCOa7omTN daNQ== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: AOUpUlH5gsVre5UVBnyqoPsPACBTg1OrmqEwTpt7JBqyNPjgWE6PQTMK 0UIm4VX1a/5pyolJaLRJsKhjObr48TV4hTd490c/80OsoG49RTrBi2kHCMGM+xRvy2D1GU+WnzC oeF6iH/H1SfoxKY9qIddfrD3x2ooW8aqA5tIxXDzEG8lbJ4B7Y84S1yzYNO/hRpGISw== X-Received: by 2002:ac8:29f6:: with SMTP id 51-v6mr48359441qtt.200.1534848290132; Tue, 21 Aug 2018 03:44:50 -0700 (PDT) X-Google-Smtp-Source: AA+uWPxZWxDteR3W9gMrf29yMpxL+D4QDMd0PTVSksSJSknkTniDzY09Q9RKfA585FtAlc8iIy/m X-Received: by 2002:ac8:29f6:: with SMTP id 51-v6mr48359416qtt.200.1534848289698; Tue, 21 Aug 2018 03:44:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534848289; cv=none; d=google.com; s=arc-20160816; b=aB9R7kh+YN9UlW1tkvGmLDNDhLLzr4xiqOxgo8lp3cdQW5qxnvPICidCBbhLzaXPRQ qS0dPt1QMUuVrPrUltnGURlgIpDUa3sqnM1NHg2sFCSq1zX/dD2++UdF55HFOxCc6xj/ 00/eJKj4Rc4zxgLsOgnfj5vQXnUXRhg0Nk9aTOxg94KQP7ap2q8jI/26RVxBmrOuSFDt eZlwaHIC5zXY9pqAOqhyXwooNbJ9Z5JPCuihvSK2O5Uq4x/IkprBwLO3UoxVlzqVjbEd upv92TIodz3Cd+T0HbpsKTDEaZOeH7FInmf6PoxXLqPJ7NUh0DqFRqzBzOPlMF0MbZSd uY4w== 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 :arc-authentication-results; bh=YOn2XEJJjUAF950/2+ACrSpbLnm+unK2V2ttVzDG1Lg=; b=hWOowVkkmUQkoPI3B4SukpsbB/pbEsK0XgBJfkZUXg+OZZk0pJfI6Tx3XcDLa9pTu0 1A+Sd10mWoXm/EoYrARweDJsCtUxPsS270nXev8LtsdKo0GbSkSP+RXychFh+/mDK7tO S6UkS5DgW0kr6AeaIfbPSVRnqB/Nmb6WWcHkwLDuh7Ilf0gClvSY6cSyie3S1/wCO94F BVqU7YRgcYByF4hwoAOoQM933vP54fpV6ksyYwwTTV6zZ5TNJPyEuqAGQh9st8VivRaR s3K1RGNPPsbDtg3gaqLcrKQFxd00u9xkA88CykGK3hPutzojGBT6e+JfsUKlQ6MHv+ie zN2g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx3-rdu2.redhat.com. [66.187.233.73]) by mx.google.com with ESMTPS id q11-v6si11179048qkl.387.2018.08.21.03.44.49 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Aug 2018 03:44:49 -0700 (PDT) Received-SPF: pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) client-ip=66.187.233.73; Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4E298402178A; Tue, 21 Aug 2018 10:44:49 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-96.ams2.redhat.com [10.36.117.96]) by smtp.corp.redhat.com (Postfix) with ESMTP id 280F37D4C9; Tue, 21 Aug 2018 10:44:47 +0000 (UTC) From: David Hildenbrand To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-acpi@vger.kernel.org, xen-devel@lists.xenproject.org, devel@linuxdriverproject.org, David Hildenbrand , Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , Rashmica Gupta , Balbir Singh , Michael Neuling Subject: [PATCH RFCv2 4/6] powerpc/powernv: hold device_hotplug_lock when calling device_online() Date: Tue, 21 Aug 2018 12:44:16 +0200 Message-Id: <20180821104418.12710-5-david@redhat.com> In-Reply-To: <20180821104418.12710-1-david@redhat.com> References: <20180821104418.12710-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Tue, 21 Aug 2018 10:44:49 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Tue, 21 Aug 2018 10:44:49 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'david@redhat.com' RCPT:'' 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 device_online() should be called with device_hotplug_lock() held. Cc: Benjamin Herrenschmidt Cc: Paul Mackerras Cc: Michael Ellerman Cc: Rashmica Gupta Cc: Balbir Singh Cc: Michael Neuling Signed-off-by: David Hildenbrand Reviewed-by: Pavel Tatashin --- arch/powerpc/platforms/powernv/memtrace.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/powerpc/platforms/powernv/memtrace.c b/arch/powerpc/platforms/powernv/memtrace.c index 8f1cd4f3bfd5..ef7181d4fe68 100644 --- a/arch/powerpc/platforms/powernv/memtrace.c +++ b/arch/powerpc/platforms/powernv/memtrace.c @@ -229,9 +229,11 @@ static int memtrace_online(void) * we need to online the memory ourselves. */ if (!memhp_auto_online) { + lock_device_hotplug(); walk_memory_range(PFN_DOWN(ent->start), PFN_UP(ent->start + ent->size - 1), NULL, online_mem_block); + unlock_device_hotplug(); } /* From patchwork Tue Aug 21 10:44:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10571365 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 3CFC21575 for ; Tue, 21 Aug 2018 10:44:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 338B629DA1 for ; Tue, 21 Aug 2018 10:44:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2805929DA4; Tue, 21 Aug 2018 10:44:56 +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=-2.9 required=2.0 tests=BAYES_00,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 9C12D29DA1 for ; Tue, 21 Aug 2018 10:44:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 34D7D6B1E4F; Tue, 21 Aug 2018 06:44:53 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 289426B1E50; Tue, 21 Aug 2018 06:44:53 -0400 (EDT) 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 102066B1E51; Tue, 21 Aug 2018 06:44:53 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt0-f198.google.com (mail-qt0-f198.google.com [209.85.216.198]) by kanga.kvack.org (Postfix) with ESMTP id D31686B1E4F for ; Tue, 21 Aug 2018 06:44:52 -0400 (EDT) Received: by mail-qt0-f198.google.com with SMTP id j9-v6so15842745qtn.22 for ; Tue, 21 Aug 2018 03:44:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=WZ73kplE7i691B4mAUaTbKLaXH7FOEpXaE1Zfovmct0=; b=aZPIi9QWbkLmxpEnu/QIiw05iQPF8QkwEUPIf8DJFFAquXfZb3ycm9mEs1JlQ8WwrJ YdoyNZ95ulnohjNXuWSpF93vZbiAyIuYLzAOw1yoxTbSEh+NqXKx/GnbPyoYN1RZQluF wm4CB3IuTALB+Nyv/y+BV6zUbb0QBdRzlGzPHrfaeA0MVFOlOzjkmFpJRCjH0lh2xLz+ Dhou7BgHIUkAJYhknwwZ2YcOjisppa7qKJ46ZQDkcH7tFOuqzI++haPFfUSATCJYC7Ml gGMZl7qncP6+JhS1LBnC/u7+/E7QRGaQQCSmgBV0hIUVm7EC6A6HVUJeYdqrAhjYiQzE bqww== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: APzg51AtP1QR/TY+VTOHtPfj2mI/Fogygfm6m2t4hJztIKoD+eS5sRn+ Pual27YanbfA1W8Wdn5RZ/Ke8OY7cBjZC19b6lK570cPyjYblqXBdEN9t8RA8JZh3OB+9yO9Zoh hWR/N2EibXbFoIRMrMa+g/ctU/byRzP0ceInjJmyEXp1uupPTat76oYgGtBc69kliig== X-Received: by 2002:a37:119e:: with SMTP id 30-v6mr69272qkr.136.1534848292660; Tue, 21 Aug 2018 03:44:52 -0700 (PDT) X-Google-Smtp-Source: ANB0VdaxR38tmVRY8G6y5+UUPfHwxVMpqXAhCLug5x5avUaZBVyI88B1M4bAyXlckeVn6ZAZX7gO X-Received: by 2002:a37:119e:: with SMTP id 30-v6mr69254qkr.136.1534848292134; Tue, 21 Aug 2018 03:44:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534848292; cv=none; d=google.com; s=arc-20160816; b=skXKefJ94/lMg06E81y55hUUtK9e6rwDEn3yeI69G3TV1Y6uVUL+/hl8kL2f25+EsI JbDSpblt+lFV//9Ryr88BpYHoFTfx+3wcVz8IWsDR3tzPkj8dzdOxcHo4LHVxlKas4sL fXZvyvEfXYyV62kjDrpqcNhWGE445ChIws7PxxzcV1LcZazhZW5xQMVUyVtakFk16P3p g5kITkn9rs1OI/n6J4+rOkbI6+psDbAdTywZBTchWlkz3+5eJaRhMIpStx6hqtW3lAEr quQrsfsodnVL7gtQG8iZ+PGhI7p+yEPp8VpJaDsuNXUTL4U2WayMjvrJz41n5Pzkehbx NqEw== 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 :arc-authentication-results; bh=WZ73kplE7i691B4mAUaTbKLaXH7FOEpXaE1Zfovmct0=; b=A4qlWVrCnfE2y/KOTruqTT/uy0QcuBzMrH8GrepTr0OzUsNzwJPI0KgI5NIjEijr5L rONWFBqHIq5iuNNftYlCOw37ktppWAq2fxKf0Z6hLEW1bRROtVktYN4xtOuVPoClJLgg 4+xMoEndHkOsUPb0z0ZowHh2CD/jStpXvgoZke1ZTnAc+A9tMkoIIeYc9kqGGE9RhMLv EK7l6b3LsMdB59d3xCAy+WLCOpMFZ3qpD/a0GBKogNWAArk/YQEh1Ul/IOjEeHMsPV4f z8OghoAdzGmhFbzu1x4FDTHa2lTWg5+ugjOmiq4ejdcQ7303JOFSYphzoeYy31rNagNU j2hA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx3-rdu2.redhat.com. [66.187.233.73]) by mx.google.com with ESMTPS id l62-v6si4390933qkb.0.2018.08.21.03.44.52 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Aug 2018 03:44:52 -0700 (PDT) Received-SPF: pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) client-ip=66.187.233.73; Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BC25C8076826; Tue, 21 Aug 2018 10:44:51 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-96.ams2.redhat.com [10.36.117.96]) by smtp.corp.redhat.com (Postfix) with ESMTP id 919A59463B; Tue, 21 Aug 2018 10:44:49 +0000 (UTC) From: David Hildenbrand To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-acpi@vger.kernel.org, xen-devel@lists.xenproject.org, devel@linuxdriverproject.org, David Hildenbrand , Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , Rashmica Gupta , Balbir Singh , Michael Neuling Subject: [PATCH RFCv2 5/6] powerpc/powernv: hold device_hotplug_lock in memtrace_offline_pages() Date: Tue, 21 Aug 2018 12:44:17 +0200 Message-Id: <20180821104418.12710-6-david@redhat.com> In-Reply-To: <20180821104418.12710-1-david@redhat.com> References: <20180821104418.12710-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 21 Aug 2018 10:44:51 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 21 Aug 2018 10:44:51 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'david@redhat.com' RCPT:'' 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 Let's perform all checking + offlining + removing under device_hotplug_lock, so nobody can mess with these devices via sysfs concurrently. Cc: Benjamin Herrenschmidt Cc: Paul Mackerras Cc: Michael Ellerman Cc: Rashmica Gupta Cc: Balbir Singh Cc: Michael Neuling Signed-off-by: David Hildenbrand Reviewed-by: Pavel Tatashin --- arch/powerpc/platforms/powernv/memtrace.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/arch/powerpc/platforms/powernv/memtrace.c b/arch/powerpc/platforms/powernv/memtrace.c index ef7181d4fe68..473e59842ec5 100644 --- a/arch/powerpc/platforms/powernv/memtrace.c +++ b/arch/powerpc/platforms/powernv/memtrace.c @@ -74,9 +74,13 @@ static bool memtrace_offline_pages(u32 nid, u64 start_pfn, u64 nr_pages) { u64 end_pfn = start_pfn + nr_pages - 1; + lock_device_hotplug(); + if (walk_memory_range(start_pfn, end_pfn, NULL, - check_memblock_online)) + check_memblock_online)) { + unlock_device_hotplug(); return false; + } walk_memory_range(start_pfn, end_pfn, (void *)MEM_GOING_OFFLINE, change_memblock_state); @@ -84,14 +88,16 @@ static bool memtrace_offline_pages(u32 nid, u64 start_pfn, u64 nr_pages) if (offline_pages(start_pfn, nr_pages)) { walk_memory_range(start_pfn, end_pfn, (void *)MEM_ONLINE, change_memblock_state); + unlock_device_hotplug(); return false; } walk_memory_range(start_pfn, end_pfn, (void *)MEM_OFFLINE, change_memblock_state); - remove_memory(nid, start_pfn << PAGE_SHIFT, nr_pages << PAGE_SHIFT); + __remove_memory(nid, start_pfn << PAGE_SHIFT, nr_pages << PAGE_SHIFT); + unlock_device_hotplug(); return true; } From patchwork Tue Aug 21 10:44:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10571369 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 4D5A8139B for ; Tue, 21 Aug 2018 10:45:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4511129DA1 for ; Tue, 21 Aug 2018 10:45:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3989429DA4; Tue, 21 Aug 2018 10:45:00 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable 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 5C4B729DA1 for ; Tue, 21 Aug 2018 10:44:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4D9F76B1E50; Tue, 21 Aug 2018 06:44:55 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 45DBC6B1E51; Tue, 21 Aug 2018 06:44:55 -0400 (EDT) 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 2FD4A6B1E52; Tue, 21 Aug 2018 06:44:55 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk0-f199.google.com (mail-qk0-f199.google.com [209.85.220.199]) by kanga.kvack.org (Postfix) with ESMTP id 06A4B6B1E51 for ; Tue, 21 Aug 2018 06:44:55 -0400 (EDT) Received: by mail-qk0-f199.google.com with SMTP id w8-v6so17429851qkf.8 for ; Tue, 21 Aug 2018 03:44:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=nnfVyljX6cxkryibLmAWL2uYypuiCLeIZVe8KdBz6Ns=; b=fdl2U9ZkzaQ+0yFsERXNjJfHurTVgdMLd0Zl+DQj6oOrwtba606dbZPUaT3+jAnR94 +IUaFNXzpuFc18xjCn35s1GZfKLjPFRkijSeE9SiMCfhyAgC1Ag/z6e9BKC6TRhiKhRV +JCNxG7KcTuCC/UjdW9W8Ua7aK8fO+6honzkVc4mNIJLP0PlBh7l/RE93ckP9J9f9nVG iJXevYzk8CzIWk1VrRCUZft8t0BUKxThKrg0I8svc3z2jb3g35e9PouOXJTCzp6OGb8u 3OSmKTeiHpQEEtSnYG0NHywlSTWg7nymeWCtah2TXgGub9TbYMyeZIHQtpT0W/kTmk+Z 2xIQ== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: AOUpUlG+hK4ebHYs04Bw1JPUrKWmmhDSY0WHD8VUls44iPMJJpHd9Vy+ pn20w9V2UoFZXupPmA8nZfPH653XDP769//Oj8fFhmeaxGB5KFgjNJ1MwxSloK5H0vlbHPvMgaB AospO7wexgxC67xj/NrlRjlVs5PLSVGzu5Pa2o7i/SrApOTWH0ARR+JhIwk80siAG0w== X-Received: by 2002:a0c:aa81:: with SMTP id f1-v6mr45047093qvb.80.1534848294760; Tue, 21 Aug 2018 03:44:54 -0700 (PDT) X-Google-Smtp-Source: AA+uWPxHnxOksGhBtowk26TcNYgj3Jq/JkE5KOD6gjXgfyF0DG1LvCKAn01VtqNhKBSBIRdIRZEl X-Received: by 2002:a0c:aa81:: with SMTP id f1-v6mr45047060qvb.80.1534848294259; Tue, 21 Aug 2018 03:44:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534848294; cv=none; d=google.com; s=arc-20160816; b=dFOh36GYY48o4sHlKmKIZgvkzzIIl90F4Y6VVkr3Mv0vuI9s1m7xGdjFI01bvgdS7W dV53oCzxgdPXJNgVNNjvh7lnKi5IoABVEp/gqBj7K3xMneIgr44XwHkl/yk6NPlE3Cyo nYrw7NV5riuXG0yQY/C/FwEJEip5LavFqb6zw9aZ0tx1wTIzF4hmsB45/sZm6pfl2nx5 u95Z8GAh/Pj98jJ7bo4Cp/R0oxRElUniqv4x8r37VJdDF9IfdULjUC8Eb/5/z3kXP9jq p9wKrWShA8wBm9FpuWDHMyWeCCla+cFHrdo0+qvN6uZceHnRKvhCBrnGi+QKRg8mST6G kWZw== 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 :arc-authentication-results; bh=nnfVyljX6cxkryibLmAWL2uYypuiCLeIZVe8KdBz6Ns=; b=o80336NS8nnaQbxSmJmG/nqByLHAc/LQ+iira0tqJrhm877kcNdgezi7yynuvi686q w9Ly4HN1aoCfH05vCe99ikFj6VJsnfkVWqHK7m+5/nIDzboQjUwMb3MkMuVyQtlEbOnI Vuu0w0utqgOAuRK2i6/fz7oFMPkarueoZV7ai0/q8nyuaHFmsDVD5XPXhlV3zWk5s3d5 TNFtxmob1s8VcETE5VA/2ER5pKUYfWna7kpNhyb8n6+UW2oArts6jlb6GrfItRU+67bc y6YGyuDr+i8IBAnYzyy6NsUJnXdFl/s48X5EgCirRDCgJCs2ZQmKmXLkMNUf6WiSZ55F op+A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx3-rdu2.redhat.com. [66.187.233.73]) by mx.google.com with ESMTPS id v8-v6si3315525qtp.169.2018.08.21.03.44.54 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Aug 2018 03:44:54 -0700 (PDT) Received-SPF: pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) client-ip=66.187.233.73; Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CC6F240216FA; Tue, 21 Aug 2018 10:44:53 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-96.ams2.redhat.com [10.36.117.96]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0B41E7D4C9; Tue, 21 Aug 2018 10:44:51 +0000 (UTC) From: David Hildenbrand To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-acpi@vger.kernel.org, xen-devel@lists.xenproject.org, devel@linuxdriverproject.org, David Hildenbrand , Jonathan Corbet , Michal Hocko , Andrew Morton Subject: [PATCH RFCv2 6/6] memory-hotplug.txt: Add some details about locking internals Date: Tue, 21 Aug 2018 12:44:18 +0200 Message-Id: <20180821104418.12710-7-david@redhat.com> In-Reply-To: <20180821104418.12710-1-david@redhat.com> References: <20180821104418.12710-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Tue, 21 Aug 2018 10:44:53 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Tue, 21 Aug 2018 10:44:53 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'david@redhat.com' RCPT:'' 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 Let's document the magic a bit, especially why device_hotplug_lock is required when adding/removing memory and how it all play together with requests to online/offline memory from user space. Cc: Jonathan Corbet Cc: Michal Hocko Cc: Andrew Morton Signed-off-by: David Hildenbrand Reviewed-by: Pavel Tatashin --- Documentation/memory-hotplug.txt | 39 +++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/Documentation/memory-hotplug.txt b/Documentation/memory-hotplug.txt index 7f49ebf3ddb2..03aaad7d7373 100644 --- a/Documentation/memory-hotplug.txt +++ b/Documentation/memory-hotplug.txt @@ -3,7 +3,7 @@ Memory Hotplug ============== :Created: Jul 28 2007 -:Updated: Add description of notifier of memory hotplug: Oct 11 2007 +:Updated: Add some details about locking internals: Aug 20 2018 This document is about memory hotplug including how-to-use and current status. Because Memory Hotplug is still under development, contents of this text will @@ -495,6 +495,43 @@ further processing of the notification queue. NOTIFY_STOP stops further processing of the notification queue. + +Locking Internals +================= + +When adding/removing memory that uses memory block devices (i.e. ordinary RAM), +the device_hotplug_lock should be held to: + +- synchronize against online/offline requests (e.g. via sysfs). This way, memory + block devices can only be accessed (.online/.state attributes) by user + space once memory has been fully added. And when removing memory, we + know nobody is in critical sections. +- synchronize against CPU hotplug and similar (e.g. relevant for ACPI and PPC) + +Especially, there is a possible lock inversion that is avoided using +device_hotplug_lock when adding memory and user space tries to online that +memory faster than expected: + +- device_online() will first take the device_lock(), followed by + mem_hotplug_lock +- add_memory_resource() will first take the mem_hotplug_lock, followed by + the device_lock() (while creating the devices, during bus_add_device()). + +As the device is visible to user space before taking the device_lock(), this +can result in a lock inversion. + +onlining/offlining of memory should be done via device_online()/ +device_offline() - to make sure it is properly synchronized to actions +via sysfs. Holding device_hotplug_lock is advised (to e.g. protect online_type) + +When adding/removing/onlining/offlining memory or adding/removing +heterogeneous/device memory, we should always hold the mem_hotplug_lock to +serialise memory hotplug (e.g. access to global/zone variables). + +In addition, mem_hotplug_lock (in contrast to device_hotplug_lock) allows +for a quite efficient get_online_mems/put_online_mems implementation. + + Future Work ===========