From patchwork Tue Sep 25 09:14:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10613757 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 3A55D913 for ; Tue, 25 Sep 2018 09:15:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2FA2A299D5 for ; Tue, 25 Sep 2018 09:15:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2280E29B48; Tue, 25 Sep 2018 09:15:25 +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 021E1299D5 for ; Tue, 25 Sep 2018 09:15:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9691C8E0083; Tue, 25 Sep 2018 05:15:22 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 8FAFB8E0072; Tue, 25 Sep 2018 05:15:22 -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 747378E0083; Tue, 25 Sep 2018 05:15:22 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by kanga.kvack.org (Postfix) with ESMTP id 44D638E0072 for ; Tue, 25 Sep 2018 05:15:22 -0400 (EDT) Received: by mail-qk1-f198.google.com with SMTP id c22-v6so24939566qkb.18 for ; Tue, 25 Sep 2018 02:15:22 -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=7SY6jk9tjHds9qTUMo55sMn5de1C+eBqU2P4khIa5do=; b=EHB5DqSO0ww63hR30RrNMAtpT02+slywDZqJycfEOWp72rVu1QgvTzWhW0wTf3w23U gFZk8hpo+NBAWwUc/QSzRazA/8zxLwwhGC9SAMhmJmPGfyUaucLR+kHmRVwPIX8Zk9vR iNniBqOzGrRRIO4S1Se4JG7WfuxbVItYW7YsDsl23erHXoMMIDOZhWIJUyTtoIsegy8a 6e6zCeu/kWOJz9JnRwzQFbUBaALX+PsdyvJz3xi5tbtuFGoizIt0lKRgKXLt1iZcDFQh c7BM/pQmTnjmGmsgG7hq6xD/xOc7IHTGe3cypISo/qCxASXhxsj2oqCS9bBDazDC5Ycs Dvcg== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: ABuFfoieKU2xAlRu1TW4uTUfHBE4kHHeO+5CvN4nWzNpG/H4MXh0HuLr gdxUlDNL1yxeGEkcOSFsNkYUcaX5EC9z7M2UAqbYeyM/zLZIm6cF4HidrjRvVX2lmmqbtUljsnc lWYM5a4EL/s9zzSYWWysIwGdRWdA4Y8XgGIRfeOGJg6TwF9s0Z/7rdqOTmMqYPubgNg== X-Received: by 2002:aed:2e82:: with SMTP id k2-v6mr41322qtd.47.1537866922027; Tue, 25 Sep 2018 02:15:22 -0700 (PDT) X-Google-Smtp-Source: ACcGV61P2WDqwpi/0dgHrP20lyCYnGwGP+EP+B+RzsxkoZ/9nd1opTMXmI3QdjygP0L39s6KGzfS X-Received: by 2002:aed:2e82:: with SMTP id k2-v6mr41298qtd.47.1537866921438; Tue, 25 Sep 2018 02:15:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537866921; cv=none; d=google.com; s=arc-20160816; b=r+6PxBEGBQZ1ou8x99LNzvgjuo5soypT1bXfoIlM1w40xfknQxWQ0Bh4MKSbZpAn1N 1ZpPr2j/wYfewJfNnjRbV1BQ8Ko7UQBu4NMO07gKrruMSMc3S24pkM6RncP6MFyBLGwE NjsU4twhsUXSsedcLrPFgHZ31P3zIhSnEq3u+OW9qxrREAPyXcflMlukRN6/MJRDk55P YdPQUtwF/OWXjclxYNm66/75XZHyFrslbXc3LhSvqOlMsqEetRolnPk8VJQacCxs1fov A0ByVlq1Rvurg2Yf2OLb+ioT5a+brEjO29tBBXo7csbs/xsyzHQCSkM5Zi0OCCyffpA3 tqPA== 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; bh=7SY6jk9tjHds9qTUMo55sMn5de1C+eBqU2P4khIa5do=; b=FopPUBBkx8Gx1c/+k/qDuJ9XQp/8+E5fgIcTxfaqkDYe4LUJk700WHDVT/CisJ0i/J AVPIZ7RHzEWr0p3oHIj2y21Wu6XKxbi++daYHWitguMKiCTMCk1yE7brlS1Zv/dZdaKR /ZGvJTSCYJY8ucKcFUK1QnjVD4yUBX3UXBYozBAU3h6yPzj5d0fxhchX3dSRnXENiozG qnYkozxnkjH4v3JYKoortb103AMuPpoHuYtRIUqdDlOfazBukYNbE1VFUH01TxND+SJd 85smbxmn/qi7of5JrT8MkB3Gy1RHGo9jud3MbEfhD/kbJqye4GHHaaGLLUj2pqK6L0cI Rvlw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 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 (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id f96-v6si1326631qtb.199.2018.09.25.02.15.21 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Sep 2018 02:15:21 -0700 (PDT) Received-SPF: pass (google.com: domain of david@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 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-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2E786307D84A; Tue, 25 Sep 2018 09:15:20 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-161.ams2.redhat.com [10.36.117.161]) by smtp.corp.redhat.com (Postfix) with ESMTP id 76929308BDAA; Tue, 25 Sep 2018 09:15:14 +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 v2 1/6] mm/memory_hotplug: make remove_memory() take the device_hotplug_lock Date: Tue, 25 Sep 2018 11:14:52 +0200 Message-Id: <20180925091457.28651-2-david@redhat.com> In-Reply-To: <20180925091457.28651-1-david@redhat.com> References: <20180925091457.28651-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.48]); Tue, 25 Sep 2018 09:15:20 +0000 (UTC) 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 arch/powerpc/platforms/powernv/memtrace.c 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 Reviewed-by: Pavel Tatashin Reviewed-by: Rafael J. Wysocki Reviewed-by: Rashmica Gupta Signed-off-by: David Hildenbrand --- 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, 15 insertions(+), 7 deletions(-) diff --git a/arch/powerpc/platforms/powernv/memtrace.c b/arch/powerpc/platforms/powernv/memtrace.c index a29fdf8a2e56..773623f6bfb1 100644 --- a/arch/powerpc/platforms/powernv/memtrace.c +++ b/arch/powerpc/platforms/powernv/memtrace.c @@ -121,7 +121,7 @@ static u64 memtrace_alloc_node(u32 nid, u64 size) lock_device_hotplug(); end_pfn = base_pfn + nr_pages; for (pfn = base_pfn; pfn < end_pfn; pfn += bytes>> PAGE_SHIFT) { - remove_memory(nid, pfn << PAGE_SHIFT, bytes); + __remove_memory(nid, pfn << PAGE_SHIFT, bytes); } unlock_device_hotplug(); return base_pfn << PAGE_SHIFT; diff --git a/arch/powerpc/platforms/pseries/hotplug-memory.c b/arch/powerpc/platforms/pseries/hotplug-memory.c index 9a15d39995e5..dd0264c43f3e 100644 --- a/arch/powerpc/platforms/pseries/hotplug-memory.c +++ b/arch/powerpc/platforms/pseries/hotplug-memory.c @@ -305,7 +305,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; } @@ -394,7 +394,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); @@ -681,7 +681,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); invalidate_lmb_associativity_index(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 2698664bfd54..f6dbd5d8fffd 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -1839,7 +1839,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; @@ -1868,5 +1868,12 @@ void __ref remove_memory(int nid, u64 start, u64 size) mem_hotplug_done(); } + +void 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 Sep 25 09:14:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10613761 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 EBAE114BD for ; Tue, 25 Sep 2018 09:15:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DA758299D5 for ; Tue, 25 Sep 2018 09:15:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CE37129B3C; Tue, 25 Sep 2018 09:15:35 +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 554BE29B39 for ; Tue, 25 Sep 2018 09:15:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 301A68E0084; Tue, 25 Sep 2018 05:15:33 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 2888E8E0072; Tue, 25 Sep 2018 05:15:33 -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 12AAB8E0084; Tue, 25 Sep 2018 05:15:33 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by kanga.kvack.org (Postfix) with ESMTP id D755C8E0072 for ; Tue, 25 Sep 2018 05:15:32 -0400 (EDT) Received: by mail-qt1-f200.google.com with SMTP id b12-v6so78208qtp.16 for ; Tue, 25 Sep 2018 02:15:32 -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=5mniBN+wmKCQnbRK/mIJkq3MoJ1GwNYqjrffFHuZ9fo=; b=BYA67F1ANBbBEXOfqqnerWb2WTwyJkCgEr6sx5dss2yM4dCjHkvojTwYSoll54OYWW 9fD0yYWpShooZ46MeqLYopEyztWs+ha5VZnYgMxclIdonNonnnGIBPp1YqXci1hJUJNe NWzlhw0br2c2QcuSzW6HUGAl7j0FP2opnDCYWlO6nqoaVfDxAvuWLNDUtpgLF/wUyvU9 4wHLUgwSiBqSOfvYAUP7mdQAORA0cPYWmRfubzf/KrUqpJ4chRdAqkaTAETvFttiPtim dC6OJNgcnxsph27i/tYIIxHUhxCiURtG/WC+E0CRaw2+tWKEchkNq3syRLQ//PQlnTYo ELMQ== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: ABuFfojYByz2423mET1rgSeXRfYelpRXLN0yHmC/PlzVURQLccnhFd/Y /69i6mJHuggyufgqOHaGQUVq+crOtmpOser7YA0XRLLz1f22rNFv+bhGOsAgOkLLG4o+UUu1cOT 4K6b1xVhJ0ILbmfr1U2i9l1vE7xZjLAmZ5nW5q4jFt8LLhh95ZM4isbAjDJXlbbYKEQ== X-Received: by 2002:a37:1a93:: with SMTP id l19-v6mr55072qkh.43.1537866932598; Tue, 25 Sep 2018 02:15:32 -0700 (PDT) X-Google-Smtp-Source: ACcGV60jUdmAGV9NNZ10BaaPS5rzZ7O9u/hvC82VZgeg0g+Y4U9PT5ztnPbDF2aBdogEQdIOOD03 X-Received: by 2002:a37:1a93:: with SMTP id l19-v6mr55051qkh.43.1537866931869; Tue, 25 Sep 2018 02:15:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537866931; cv=none; d=google.com; s=arc-20160816; b=EQK5n+3/fsGVWFNn+m2Akoki6qKOz6QRUgSsfooZa/P5SVahdbhRT+dQTmvbDuuUKt CHD92aJdNcWPcr1Vk2csXZi/Krr51aty6hFMq8om+0jQhZLOEN2M6F7yd8rlJSadeuLt q7ZE2dPAP0Y9GAQy3WHt7Uoqe0P+gMtPckduNAeIWxIv5CHrV+Dwktb6BcBYyKfNg7x2 30iwD9vXoJ/I0e9w8YZ6CNZBcCG2nNG28wYFILjE8orEXkzUaLco/mcwkX/Gb/85+kLl qMx+ML4sqoZRYHqh4qRtSRILHUS21oVxaLijnPb3D9388d5WiEkEfmBGulbcPiTAA3aq hYOg== 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; bh=5mniBN+wmKCQnbRK/mIJkq3MoJ1GwNYqjrffFHuZ9fo=; b=kOf96ACPwYfU6i8KkJ4w0Waf6MEm79CLSZOe78a1W1yrcn9ykbXfEGvWDNmTS6nhiU 0CMfipbWoaIaJyZ1lbB6skbVnqqd3R6mOFQohqxCAldk3nMsG+ILxSdbz9yexcHEJEFq gbQrUX0RPKZexnRDLF/M/3ISeA30123chD1BGw3VqOkPlPrdiDAhR4O+GPqlksk2E3p0 /o4QmU577m14RkobOVaeGJWfr/dAQO9Zi2wVc8l2jURh2VrY3rARkvU8n3bvIXlBPHP2 0KRPY1pcx8G7mRxQmNiv1R2d7q9/99GUr5aTLdU0vBQ+pu7HDszA9uVNUiBAyDs9LekX ZdAA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 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 (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id r14-v6si1377112qvn.159.2018.09.25.02.15.31 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Sep 2018 02:15:31 -0700 (PDT) Received-SPF: pass (google.com: domain of david@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 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-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A52AF811AC; Tue, 25 Sep 2018 09:15:30 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-161.ams2.redhat.com [10.36.117.161]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8823A308BE75; Tue, 25 Sep 2018 09:15:20 +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 v2 2/6] mm/memory_hotplug: make add_memory() take the device_hotplug_lock Date: Tue, 25 Sep 2018 11:14:53 +0200 Message-Id: <20180925091457.28651-3-david@redhat.com> In-Reply-To: <20180925091457.28651-1-david@redhat.com> References: <20180925091457.28651-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Tue, 25 Sep 2018 09:15:31 +0000 (UTC) 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 Reviewed-by: Pavel Tatashin Reviewed-by: Rafael J. Wysocki Reviewed-by: Rashmica Gupta Signed-off-by: David Hildenbrand --- .../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 dd0264c43f3e..d26a771d985e 100644 --- a/arch/powerpc/platforms/pseries/hotplug-memory.c +++ b/arch/powerpc/platforms/pseries/hotplug-memory.c @@ -673,7 +673,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) { invalidate_lmb_associativity_index(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 817320c7c4c1..40cac122ec73 100644 --- a/drivers/base/memory.c +++ b/drivers/base/memory.c @@ -519,15 +519,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 a3f5cbfcd4a1..fdfc64f5acea 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 f6dbd5d8fffd..affb03e0dfef 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -1090,7 +1090,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; @@ -1159,9 +1164,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; @@ -1175,6 +1180,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 Sep 25 09:14:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10613765 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 4FA836CB for ; Tue, 25 Sep 2018 09:15:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 45D50299D5 for ; Tue, 25 Sep 2018 09:15:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 39E3529B3C; Tue, 25 Sep 2018 09:15:43 +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 F321F29B39 for ; Tue, 25 Sep 2018 09:15:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CC6B18E0085; Tue, 25 Sep 2018 05:15:40 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id C73278E0072; Tue, 25 Sep 2018 05:15:40 -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 AF03C8E0085; Tue, 25 Sep 2018 05:15:40 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by kanga.kvack.org (Postfix) with ESMTP id 7F5918E0072 for ; Tue, 25 Sep 2018 05:15:40 -0400 (EDT) Received: by mail-qt1-f199.google.com with SMTP id h26-v6so6767990qtp.18 for ; Tue, 25 Sep 2018 02:15:40 -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=M8OnhsX7AqKY0AfhmWqiVkUqDUfxMPNR7Ro4YkEuXx0=; b=s8HlgyC/xBjBR19+PKLT8uKjcF7OdItlTXf2mIZo6F285RrHlS/cBtUX5WYMFKbYz3 zItUaSJmDrf5MznKJ0PQG+2OPrrl/pzd9X2zBsYAERs7iOmElFFlEfAQwN1mJa11j3vL UAmgDwhmfFgfFvoSnoKUyF8P2hPpc3NFwQgyRdJ85BXrq7h3kRbSTokzsG2U7X2UBWlk THPG10C6kunvtwp6gpDCB5KoisKeRzmE1ZdVFBRz33hYkkjAg4LRVVfEOgwZRVcUKcav liTIle7nF1pvd2KeQNlniYhh7aLfX9rY8VXQKuXeyWNKd/6oHWYOF2K8Dli+1lN8UVt0 wpew== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: ABuFfojd3KAVp/3Zlf8q1VfhSCe0ceQ2HqZ4ucVOkuxO72Iiz08wrNgk tt3QnZ2nZDsKKIkBXBGVsuu/MlwU3lC1+97FqIoMrNBk0b6uFFXT4fPiLiflZEq/ZpERWaSbYR/ iXKc+IFpHIcBEzv5F/ZzrIB07B4bZUJlt6gNFRvF4g6p2TDPXk95gq7TUv6DUOcLZRA== X-Received: by 2002:a37:a753:: with SMTP id q80-v6mr74145qke.92.1537866940250; Tue, 25 Sep 2018 02:15:40 -0700 (PDT) X-Google-Smtp-Source: ACcGV62Gciyqaj6T7LGiaoWPS/aWPxXgg+5c07IR+o46Nfw3Y/upwcDvrszspt5u6Ev1w69jSZBt X-Received: by 2002:a37:a753:: with SMTP id q80-v6mr74107qke.92.1537866939449; Tue, 25 Sep 2018 02:15:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537866939; cv=none; d=google.com; s=arc-20160816; b=iMWGj/fdBpotpxbvCMhk/z/eiRthjoUYZFib3qrXXBqEwLTYC6JRpsbmHSdHSGfrrY 438DSWeATCup5IDlc0WLpIjf7VKhtcwEx/xT7FTqPUKIz93zJ8llxNBlDn6tKaG5Etd+ 5/MKwt9Hyq2X8OaQYK4ems/ZQoxIbI2w7vt1P4gyCJ4WnjYnTgE/elRtWRauTtVsZKeq NoC4bBToFtMF60aZ4P34oVSpK9Gw1X66kTJtlk9gp7GIXQs6f2QgQF1hQZaIyGwqiJRk F1sreDDL3TxElbG2k0KGQk2l/DOKF5ndZ3bpvKYNscSlxex+GBzYJm5Kg3UYz9eiuOrH Tu9A== 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; bh=M8OnhsX7AqKY0AfhmWqiVkUqDUfxMPNR7Ro4YkEuXx0=; b=vdSkoo/mdUQWWPbQdO9vNcoHEYdkQwgVsBwVTpXYX+ySXAUCXhZiGuYTheazTgICVr HSnFihAD3+fPVKW5Ar1ZInH3ImvpR2TrOyvvtRNEpGBqMozTLaf5odrAQelQCWBd3blP hTis4/mEeb0AifOWYp3a9uesINu4e+HJJtV0Dc6r/H/jCqmQYOd6s+ty4kPQFHawbihO TWBZNZhxY1wlDF6pjyS/JIyV/e4rVkSmbfjOXHFNddhgUzgNSFKPYJZd6+GkDtnPz2N6 j8o/cULNvEGS7yvM8ryzPzOuta0pK9/UJRlWuOyZYpMEZ2LYWj/Pi0v5Oy96QIec2frc ogAA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 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 (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id f25-v6si169393qvh.45.2018.09.25.02.15.39 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Sep 2018 02:15:39 -0700 (PDT) Received-SPF: pass (google.com: domain of david@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 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-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 34ABBC057E09; Tue, 25 Sep 2018 09:15:38 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-161.ams2.redhat.com [10.36.117.161]) by smtp.corp.redhat.com (Postfix) with ESMTP id 06857308BDAA; Tue, 25 Sep 2018 09:15:30 +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 v2 3/6] mm/memory_hotplug: fix online/offline_pages called w.o. mem_hotplug_lock Date: Tue, 25 Sep 2018 11:14:54 +0200 Message-Id: <20180925091457.28651-4-david@redhat.com> In-Reply-To: <20180925091457.28651-1-david@redhat.com> References: <20180925091457.28651-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 25 Sep 2018 09:15:38 +0000 (UTC) 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 a), followed by b), 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. This will be addressed in the following patches. 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 Reviewed-by: Pavel Tatashin Reviewed-by: Rashmica Gupta Signed-off-by: David Hildenbrand --- 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 40cac122ec73..0e5985682642 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 affb03e0dfef..d4c7e42e46f3 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -860,7 +860,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; @@ -872,6 +871,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. @@ -936,6 +937,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: @@ -943,6 +945,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 */ @@ -1147,20 +1150,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; } @@ -1588,10 +1591,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); @@ -1600,8 +1609,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; @@ -1672,6 +1683,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: @@ -1681,10 +1693,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 Sep 25 09:14:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10613767 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 C0352913 for ; Tue, 25 Sep 2018 09:15:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B42F9299D5 for ; Tue, 25 Sep 2018 09:15:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A87CA29B3C; Tue, 25 Sep 2018 09:15: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 3E270299D5 for ; Tue, 25 Sep 2018 09:15:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 60EBA8E0086; Tue, 25 Sep 2018 05:15:43 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 596538E0072; Tue, 25 Sep 2018 05:15:43 -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 45F6C8E0086; Tue, 25 Sep 2018 05:15:43 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by kanga.kvack.org (Postfix) with ESMTP id 173848E0072 for ; Tue, 25 Sep 2018 05:15:43 -0400 (EDT) Received: by mail-qk1-f200.google.com with SMTP id y130-v6so25485542qka.1 for ; Tue, 25 Sep 2018 02:15:43 -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=nz0n2L3JxULsBS/pUHRQ1cL3pjanD6P0YON1fc9C7I4=; b=bv/edRi5saUHfodP6LxYH52uUNVW2AgCOnhsC+sNeFaBg7orB0zFhF2oUxWtUdvuIe Mwjeuu1bmR83DijqWl8drftLm+QB3pdRdvER5ruScU4ax7ZkfzqTgGmjQAtS7c6fJ5qG YykgCli5bOHMpYsJpDn+bOHfAf9+KGGYoNt8hwVLI5udYu8vyw13NIn6+ktR6bizaeCa abA+rRNEmpVkUPK/J5jhduATUr1mlh799BHT9DMytZwpycRuEmaLPraZ9cy1Exajhpho p9PZRHKFwsdlTs8PknkPxxVEq8rxbFWzg3eeKbgVY9i48s7ugc/h0CtiGh6h0tBH+v65 Q9qg== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: ABuFfojsiEcIw+/e9f9MCtqMhzx0cckgd+q+hx60vujftasRuaJvQau3 EXpIcmghd2NjI+39//mOkDdZJzcN5T1gb70JI+meIWwG2nZWTQD9+twWiGrcUr5QTCVsTjrYEwH Gfj2JVvOMBnhj/DFjWwLfwB7W+FyWZ06oOAf7QiLEjqj3rQiQK/0p82NxXD/4J583Kg== X-Received: by 2002:a37:4a46:: with SMTP id x67-v6mr79394qka.60.1537866942868; Tue, 25 Sep 2018 02:15:42 -0700 (PDT) X-Google-Smtp-Source: ACcGV615lj4HrHSj/OxyMKGYqlxQKxuYB1cK7YKYVi6R+JJDMIdRRnPPKGxT1d3HUN+qWS9Sk6op X-Received: by 2002:a37:4a46:: with SMTP id x67-v6mr79381qka.60.1537866942426; Tue, 25 Sep 2018 02:15:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537866942; cv=none; d=google.com; s=arc-20160816; b=GYeE4ciSpT8FwnAmKdrmMAJLOubIGE/3JpyQF9rdnHZmXWmjtuTDcWUaX2VzUKDIIb DsvuLqd0KzBMinmkLv1wgLv1Pk6cuiwonaAFoStf5+M0XGVMO5VXD4R0wvUxptGyWWjL x6oT4GjgmuFJmE0SpqaBEzqjTcqXfhl8NFXUy06EW3Fxu0FGpcEhds6g8iHFMQKjdK3l HiXIidi+9br8Ln1TFHkQjgkYzyIfi8winLLiYUvmpqjvcB5LuU9/cJ5s+mqsAcmykYic tG6AIX+LnWXT9p5jtH5byZK3PqeJqlLxBVX2fCABbjEyVrhDJyS3/8GAQUKorvJeu+yT VNJg== 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; bh=nz0n2L3JxULsBS/pUHRQ1cL3pjanD6P0YON1fc9C7I4=; b=FQEFe7Cm6TYF/H4CXsbxJgmMr0044YmBqFd0aDRnKFvJePt0P/JUIy7YDzAVzivcsy kVAswxadJ0c1C8M0wHTkHdKbfSAtqTGBLii9YvG01R1xkmAUDAAC5/AztDRuL9icwKY7 GvSKXmLz6ctOKkMi0v4eKJcmdkxslqqqhp0uQNIXFzEUN7+Et107FykFJbFNVfEGvP4Z iUSc23VrGYym4vC0ekRK1/lSPNJFKM1+Hd5bPVlw4eWzYectZwJfBG3t4Ob1Yue7XYaY BBrYREF9YcTdS24LxOZ9BWqEa+OnwI25ahxinGuEiEy/2AcpjazsJ6OCHgSzOSOoKArj I5UQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 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 (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id k58-v6si1751755qtf.286.2018.09.25.02.15.42 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Sep 2018 02:15:42 -0700 (PDT) Received-SPF: pass (google.com: domain of david@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 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-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7D9595D68D; Tue, 25 Sep 2018 09:15:41 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-161.ams2.redhat.com [10.36.117.161]) by smtp.corp.redhat.com (Postfix) with ESMTP id 89AB4308BE75; Tue, 25 Sep 2018 09:15:38 +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 v2 4/6] powerpc/powernv: hold device_hotplug_lock when calling device_online() Date: Tue, 25 Sep 2018 11:14:55 +0200 Message-Id: <20180925091457.28651-5-david@redhat.com> In-Reply-To: <20180925091457.28651-1-david@redhat.com> References: <20180925091457.28651-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Tue, 25 Sep 2018 09:15:41 +0000 (UTC) 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 Reviewed-by: Pavel Tatashin Reviewed-by: Rashmica Gupta Signed-off-by: David Hildenbrand --- 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 773623f6bfb1..fdd48f1a39f7 100644 --- a/arch/powerpc/platforms/powernv/memtrace.c +++ b/arch/powerpc/platforms/powernv/memtrace.c @@ -242,9 +242,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 Sep 25 09:14:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10613771 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 9ACD06CB for ; Tue, 25 Sep 2018 09:15:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 900C0299D5 for ; Tue, 25 Sep 2018 09:15:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 841C929B3C; Tue, 25 Sep 2018 09:15:51 +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 19490299D5 for ; Tue, 25 Sep 2018 09:15:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E2BD98E0087; Tue, 25 Sep 2018 05:15:49 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id D849A8E0072; Tue, 25 Sep 2018 05:15:49 -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 C4F368E0087; Tue, 25 Sep 2018 05:15:49 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by kanga.kvack.org (Postfix) with ESMTP id 9641C8E0072 for ; Tue, 25 Sep 2018 05:15:49 -0400 (EDT) Received: by mail-qk1-f198.google.com with SMTP id y130-v6so25485711qka.1 for ; Tue, 25 Sep 2018 02:15:49 -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=uqGXcrSAuHaJugA2adZl27ACNF7YkUQ582WajK9llWg=; b=PF/QCp6ouOLglILVYo83aty0WAgDrLkd+on0xuVRh77cKFoKoYg+JT4CMfMZtneHs9 XX/9cgXTW6ztywfGlmzu5Zij/7OpPD0g1ydc6Mo44B0S3r74Cj3FHcj37fURv/2SrpP+ lLrLcqwrqDbec3FQkidOIF6wMEkSATM4FCGf7LcKv2uQ/CM+aDwCp1oXXZdZlxAlk4VH 6sZvtMMwwqTt83umK5I+Azr8XI20upZI8yOQmjnfFYt3rsulaQGy0CXw6VBZPaPNnQ5Y 1I8FpRPtFYw11zUrdZZkfFpkDQ8+6XSo1Zcg3aYqVi5MX5Ai48nUaQNbu3Qq1LHCOw3h s0pg== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: ABuFfogYrQDTMurSkMuW+EY1AVlhPEHbfYtI0gYfO3w5A0M44XIaW9b7 K5FaS1yjRmPYOWk7FYfGVBmhu4Ox/5pNeBXU3hblu2ssCFbwFE/hTtTdWZmyS2m5eeRvtQf8Rdd 8pOH1fqvsM1cQKR7VrTRQkQYNVuGMIEJhSuoBn5HJo3hwypLReXBRVhIGQDhmAYf/ZQ== X-Received: by 2002:a0c:e74c:: with SMTP id g12-v6mr74272qvn.118.1537866949394; Tue, 25 Sep 2018 02:15:49 -0700 (PDT) X-Google-Smtp-Source: ACcGV60Tk4aFKhHJ/4EO3475eUQPSl2PhQGx4PlHrecncMazTbUb38ukwFgaxueWUnBwSQAyePol X-Received: by 2002:a0c:e74c:: with SMTP id g12-v6mr74260qvn.118.1537866948942; Tue, 25 Sep 2018 02:15:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537866948; cv=none; d=google.com; s=arc-20160816; b=NLemigGZqHeCvysK1vzrFVMWoi4CO8OifILXXkUZW4FqL4Av8qOFbjwYzncPLmPqpY SEGo2eSh7zmo5l5INwaLmJkUtLaPlkY6geNkC7N/CQn/kqEF64hKUMP1zo4XWI2Q3l8/ HL39vKaFvL3rhotuK6UXjnxSnDiJiUwc7zT/GWyxz70S8E3Bqv5p0vPpUvQt+xUAtcS3 i7p0ySCB6dbX+wxeeTc0hebqP2SCstYd4XFypgM5dX7Fo+bx4c1eJ4j0xVDnyeeo4H+f k2HF1tzGAt4vy2+womOPLureAMOXlXRdXkxUkcDbuePEVUpVRDNHLv0FWPfIdJwDyuo6 J9hg== 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; bh=uqGXcrSAuHaJugA2adZl27ACNF7YkUQ582WajK9llWg=; b=MtNS1vFpgqHH1UJcGcR3g0B5YNLnIqS0+NqWtnQOAc4vlDIZa6mfpHXticy2gJFbj0 cMic3F1NQ+sMbIjyCLxsd1T0bFlCTsFOeJqKH7/9UBfvoRljzKdf1n8hir+BIfgzXpQq dEiYfyJl6Z+26vQQS0ohfelEx1V7NlJ9zfm+8nxav12m9VbVFMUHGeG01ANRXC2Pqll4 DyecTFActm5jNowiKDft2GdcxPeBlHoSv7thqa1Yhvja9ngzb/F2JcCIm2NY0xgI9YRK qEtKFRj3iI2lDNZxOA4Ld+BxUaaHRxAh9jH1YVVSuovYIhM3egtEKXQzszAkxfSyHbGv wWkQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 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 (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id x5-v6si1353666qte.88.2018.09.25.02.15.48 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Sep 2018 02:15:48 -0700 (PDT) Received-SPF: pass (google.com: domain of david@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 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-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 05889356F9; Tue, 25 Sep 2018 09:15:48 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-161.ams2.redhat.com [10.36.117.161]) by smtp.corp.redhat.com (Postfix) with ESMTP id C50CD308BE75; Tue, 25 Sep 2018 09:15:41 +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 v2 5/6] powerpc/powernv: hold device_hotplug_lock when calling memtrace_offline_pages() Date: Tue, 25 Sep 2018 11:14:56 +0200 Message-Id: <20180925091457.28651-6-david@redhat.com> In-Reply-To: <20180925091457.28651-1-david@redhat.com> References: <20180925091457.28651-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 25 Sep 2018 09:15:48 +0000 (UTC) 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 Reviewed-by: Pavel Tatashin Reviewed-by: Rashmica Gupta Signed-off-by: David Hildenbrand Acked-by: Balbir Singh --- arch/powerpc/platforms/powernv/memtrace.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/powerpc/platforms/powernv/memtrace.c b/arch/powerpc/platforms/powernv/memtrace.c index fdd48f1a39f7..d84d09c56af9 100644 --- a/arch/powerpc/platforms/powernv/memtrace.c +++ b/arch/powerpc/platforms/powernv/memtrace.c @@ -70,6 +70,7 @@ static int change_memblock_state(struct memory_block *mem, void *arg) return 0; } +/* called with device_hotplug_lock held */ static bool memtrace_offline_pages(u32 nid, u64 start_pfn, u64 nr_pages) { u64 end_pfn = start_pfn + nr_pages - 1; @@ -111,6 +112,7 @@ static u64 memtrace_alloc_node(u32 nid, u64 size) end_pfn = round_down(end_pfn - nr_pages, nr_pages); for (base_pfn = end_pfn; base_pfn > start_pfn; base_pfn -= nr_pages) { + lock_device_hotplug(); if (memtrace_offline_pages(nid, base_pfn, nr_pages) == true) { /* * Remove memory in memory block size chunks so that @@ -118,7 +120,6 @@ static u64 memtrace_alloc_node(u32 nid, u64 size) * we never try to remove memory that spans two iomem * resources. */ - lock_device_hotplug(); end_pfn = base_pfn + nr_pages; for (pfn = base_pfn; pfn < end_pfn; pfn += bytes>> PAGE_SHIFT) { __remove_memory(nid, pfn << PAGE_SHIFT, bytes); @@ -126,6 +127,7 @@ static u64 memtrace_alloc_node(u32 nid, u64 size) unlock_device_hotplug(); return base_pfn << PAGE_SHIFT; } + unlock_device_hotplug(); } return 0; From patchwork Tue Sep 25 09:14:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10613775 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 B57F2913 for ; Tue, 25 Sep 2018 09:15:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AB28A299D5 for ; Tue, 25 Sep 2018 09:15:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9F26929B3C; Tue, 25 Sep 2018 09:15:59 +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 32C24299D5 for ; Tue, 25 Sep 2018 09:15:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ACD2A8E0088; Tue, 25 Sep 2018 05:15:52 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A51728E0072; Tue, 25 Sep 2018 05:15:52 -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 8F4598E0088; Tue, 25 Sep 2018 05:15:52 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by kanga.kvack.org (Postfix) with ESMTP id 62CAD8E0072 for ; Tue, 25 Sep 2018 05:15:52 -0400 (EDT) Received: by mail-qt1-f198.google.com with SMTP id c14-v6so7974012qtc.7 for ; Tue, 25 Sep 2018 02:15: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=Q+AWfAQyFh1eB4temGrJ9RcRxkI6tEPl3FFKc4he+h8=; b=RWPBj/SYHO78p/poCyjaqcbVnKEt/fqUHP4ey+mzWJAvuvCQ28JHeMugcuKa8qkNXL 88LJxKWt4ZP0VUN2ciHsv2t7xt7fIs8C4itIcwDjQTkvkf8/JPs1kN9DR2GS+59dson2 UhACVrSXysyQhGh90Sl7ZitFIf4mxl0wz3vr2F9b6/sB52Tlru6QDk1ASMRHI31/Govd x8uk50O+SIbvrCFMZmPNTcWP02vv0sC4PO0qRDqQzNE0dc+1Q8ZYwqVT+I5KAGWP2Hot LmL9Zt6ODtSqah7amCFLA4fhYcmO1pOeFipTq0IvW9D33zrOySwINy4rsj1UjRnmfVcL 22Vg== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: ABuFfojqOpGsXoPyk6abgYcovkGra88gdzzoWEeZVDs4h6GIND9nHmWP OmVqqeZ8nbir1Y75/mKRWTWh1cJS1xvicNMBqZXVqwXIiPFyS1ngNd/D5/9HVgZVOm9n1jBTHw5 sPTseGBPAOC6kSiQk1Lz7+CDubq6fYUIIEAi4Zq68NrPaj5LQts0FGv73sWwifVzkVA== X-Received: by 2002:ae9:e314:: with SMTP id v20-v6mr58198qkf.69.1537866952157; Tue, 25 Sep 2018 02:15:52 -0700 (PDT) X-Google-Smtp-Source: ACcGV605KPadauPRPl2qA42QYJa20uocQ+Ih77mW+Ffc2Pswwa1WybhdrWwZj2pM7RegGBPoWDi2 X-Received: by 2002:ae9:e314:: with SMTP id v20-v6mr58179qkf.69.1537866951615; Tue, 25 Sep 2018 02:15:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537866951; cv=none; d=google.com; s=arc-20160816; b=CCdXfT19w7ouXuPdDThWq8bw7dmDzG8eNirivy/11D3qkQ3pWFqAjDc9Xw5Z832nGP Afg/xc/4DJCTL/3unOCCNPAgAggZuBS29nQCcsD21fbj8Yuf+hZy9DXWU/6PAedjINcG HFvPovV+ZIEk9ZbRZPoLo7ByE7oehtPqi6i7hDCofad78ygshjYADRBLvs5TaGH7Teq5 ljN1WLtMnNIkiku6DjXGzWLor/sGvN4SavVsu3CARuGrWLyDpNstU37kCgiArGnpg+Af BJsvULAtwdQkx5JKPL5zIwqf+wmuNEwviW1Umd4ji0wunV0PeDhgKD5M4b2ttUo0P17d KX7A== 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; bh=Q+AWfAQyFh1eB4temGrJ9RcRxkI6tEPl3FFKc4he+h8=; b=O758zSN6evifY4jbgddxEP19+G6UTtJ2PTi9wH8YeKwkEltpegj1BO4KH4pkAFqTmr 7WhyYj5ma85QoMr6xn/qjgmnVGX5Q7KZz8h5MgUTpxBxXUa+GvtNfl3NkaMwiOwjNulB 9KqCCVeadM7N6br1WcDeD9HKk11qDcykoNEHYnfA1Yk8boUnl7zfSrRlTZSnw7+8Fq+w 1kOpEiRpbZovd9YtlDkdMvp9KUfIfor0nyiVqotA7H+8w2E4yGpg1lu15ayfLmnb3BAo 2pGCXFWu+rwTcFERX3vErqipOHVwFf0Rf4MjVTxm0G5SIpAYtvwggxVLzSkfgmhaSuqg 9cHg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 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 (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id m18-v6si1297488qka.155.2018.09.25.02.15.51 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Sep 2018 02:15:51 -0700 (PDT) Received-SPF: pass (google.com: domain of david@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 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-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B32C4308213F; Tue, 25 Sep 2018 09:15:50 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-161.ams2.redhat.com [10.36.117.161]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5B832308BDBA; Tue, 25 Sep 2018 09:15:48 +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 v2 6/6] memory-hotplug.txt: Add some details about locking internals Date: Tue, 25 Sep 2018 11:14:57 +0200 Message-Id: <20180925091457.28651-7-david@redhat.com> In-Reply-To: <20180925091457.28651-1-david@redhat.com> References: <20180925091457.28651-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Tue, 25 Sep 2018 09:15:50 +0000 (UTC) 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 Reviewed-by: Pavel Tatashin Reviewed-by: Rashmica Gupta Signed-off-by: David Hildenbrand --- Documentation/memory-hotplug.txt | 42 +++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/Documentation/memory-hotplug.txt b/Documentation/memory-hotplug.txt index 7f49ebf3ddb2..ce4faa5530fa 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,46 @@ 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 in +write mode to serialise memory hotplug (e.g. access to global/zone +variables). + +In addition, mem_hotplug_lock (in contrast to device_hotplug_lock) in read +mode allows for a quite efficient get_online_mems/put_online_mems +implementation, so code accessing memory can protect from that memory +vanishing. + + Future Work ===========