From patchwork Thu Sep 27 09:25:49 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10617701 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 B2B4A14BD for ; Thu, 27 Sep 2018 09:26:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A0F372AFF0 for ; Thu, 27 Sep 2018 09:26:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 944462AFFB; Thu, 27 Sep 2018 09:26:24 +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 E06092AFF0 for ; Thu, 27 Sep 2018 09:26:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D50608E0003; Thu, 27 Sep 2018 05:26:20 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id CFF478E0001; Thu, 27 Sep 2018 05:26:20 -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 BC8DE8E0003; Thu, 27 Sep 2018 05:26:20 -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 8EF7C8E0001 for ; Thu, 27 Sep 2018 05:26:20 -0400 (EDT) Received: by mail-qt1-f199.google.com with SMTP id s2-v6so1639676qth.0 for ; Thu, 27 Sep 2018 02:26:20 -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=G8FQ/ONziQlc+Ks5xgTQVkuD5EkeFaF05Ny9e5dFVs9qVAYWnKLjqbfnZJiUnvaLOP W84fcg9n7ocKcCwNddzGHXPdi89Huf6WydeK9u/NxoZuf8hi01M3+K5LESRbsb2vk7oz SvjM5Oj15dI1MQw09/A24epy1hq1GocAHrB9KG/1HeAr56zrZMR0CM4hFEP9Gd6/g1B/ QNniTrdjgaISbL/EzDlumK27jLbBRRJ+gG26WVUTfxgiZ8rI3OTTCkZTlHxI62ePaaDo OiscKaCTvc7Nu0+r185yhobzBxoEjdrfDHFBKgbosuGVzY8Qw8N2BCcyLfxCA42N3VNB WSkQ== 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: ABuFfogVkDNnq6KR8zrSWL45mxF+AZPtmxfUANbWQsXDGEgJ+6Wf/O+w 7nSw/XHhI+RhAPKwqvsdyhaJIjxTvXn0+moEx/oKZ/QJ+yh08MIPmAVDTYC3HWB2V0BZpUtSY5D 1bOWM00I7caeZClSrApyYHt8t2NiGas3QqmijC3+tf9LKhqKgW8TNN5K7n3W1SSF6SA== X-Received: by 2002:aed:3031:: with SMTP id 46-v6mr7623290qte.29.1538040380348; Thu, 27 Sep 2018 02:26:20 -0700 (PDT) X-Google-Smtp-Source: ACcGV63NR6EfiQsUf1SswICOrEqZ1SuGwppwXkO2ZNME3S0iu+fGGPmDdEm2t0qb4scaGK4/CDQ2 X-Received: by 2002:aed:3031:: with SMTP id 46-v6mr7623272qte.29.1538040379829; Thu, 27 Sep 2018 02:26:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538040379; cv=none; d=google.com; s=arc-20160816; b=hNl9j9cMglal4YgzyGu+ATXlA9bj9/dcXCMvHtuZS9ccbYelyNHhrcSk1ZmEodfX6i xINrGfLGwf+loZFOrMAWuqWwDZYEgWySwWYhCXiPq3yGygXgZeDkSrLOVsBFPqE+0tkq wCgd6aFGFruhYq6Qcttmqt3Tk0rT36E/13uYkOptpNA2bQWWFPMUyTtwm7GAjc8qptMa fCSvuWgck/EKrzFANnEM8VZ9pAAy/r2wU/iQJ/voy10CPRdsY9KC4UzGBEoPs7s4aA61 GUJjk/eWOqDuQMtocD4OODtVRIE0fIMJKKSkcfH/XHtMR8QMTozX9xC0TudcsLZ5BYSF u4ng== 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=uKkgotDeAry3S5TXaBuGljylZmneJQxRg3/UeGlQ0kHb1LJ5toySf925/ZrZSlWYk0 xAAofL0J8RRPvXx7kvT4pzL4UyzPboY1zc6tk8ZAYEsuhzWaUhQF3Cuew+HW9pQ+Nskk UqI5TkEFqo6hflt8E34lsfyUJRAB9H2Itn7o+qnf06IyhnFyhZs+Yz43I/TAPGwcne9O EEvl2W7ibYrvUrjnEkX1MmUBoEdt0rgZT/msQqN1dB0YthCgGE/ezRUI9NJneSFn2jZK DKd8njxx3MXt3RCoIswjdtA5N/3bt1Arn+Wkpmbj6jOX+DCOdBhaiHQnQwCNHDyubu8y xv/A== 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 n30-v6si1065691qte.228.2018.09.27.02.26.19 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Sep 2018 02:26:19 -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-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 96D7E30820C2; Thu, 27 Sep 2018 09:26:18 +0000 (UTC) Received: from t460s.redhat.com (ovpn-116-205.ams2.redhat.com [10.36.116.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7D32E2015C02; Thu, 27 Sep 2018 09:26:04 +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 v3 1/6] mm/memory_hotplug: make remove_memory() take the device_hotplug_lock Date: Thu, 27 Sep 2018 11:25:49 +0200 Message-Id: <20180927092554.13567-2-david@redhat.com> In-Reply-To: <20180927092554.13567-1-david@redhat.com> References: <20180927092554.13567-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.25 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Thu, 27 Sep 2018 09:26:19 +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 Reviewed-by: Oscar Salvador --- 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 Thu Sep 27 09:25:50 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10617703 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 5A6C414BD for ; Thu, 27 Sep 2018 09:26:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4A3BB2AFF0 for ; Thu, 27 Sep 2018 09:26:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3CA4F2AFFB; Thu, 27 Sep 2018 09:26:29 +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 DBB992AFF0 for ; Thu, 27 Sep 2018 09:26:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C19AF8E0005; Thu, 27 Sep 2018 05:26:26 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id BA0038E0001; Thu, 27 Sep 2018 05:26:26 -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 A8F428E0005; Thu, 27 Sep 2018 05:26:26 -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 789AF8E0001 for ; Thu, 27 Sep 2018 05:26:26 -0400 (EDT) Received: by mail-qk1-f198.google.com with SMTP id u195-v6so1900011qka.14 for ; Thu, 27 Sep 2018 02:26:26 -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=JV8b4TaUQMOFFByuRXpUJk39/aNnz0SZIYuD7pq1Oh8wavSLWm8uuIEEvbIKMkwQcn 1Vu8kQ3k0dI7kHfv3WDTmTuXNa8MRve2XIxwg94dmFGa0mAdCkiFn/Wt0vgeyKIa+W92 VbofUisRLlHakMKRDQRAX+pHiidEY5Lia9fsL5ctow8rXIQslBXzPU7wtCfkKSN6nH7Y 8zEE4IGaOcK8oEx49iQQktOgQWdUD105ngPowKCgzijaL7dtZCtOSkn/vl9QZShCQNxQ S/8sQYOUmK3aRrh90MuxVjw6qR2T9JqZIPUsRtT72XrY1f8XdaHpml1vJkqjD4f35suY asWw== 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: ABuFfohFUmSHbND+EEbLJpdANjKUPoCRUmMIlHGGOdzyeRviEXH/jq9x OE+AeAEhA1fJ9AKyJwvVYUIN6L+aG9d367dQY2U0geQrwszT/18qbHdqfyJjoeIZQMFizfolrQE RN8Yo2NMQcsKl0m4m9P7Ibce+plJ8mdj2smveSXyVC0ect0mVBoIluLLyKjG5P9IDwg== X-Received: by 2002:a37:5ac2:: with SMTP id o185-v6mr7318181qkb.115.1538040386187; Thu, 27 Sep 2018 02:26:26 -0700 (PDT) X-Google-Smtp-Source: ACcGV60R0Z/k4efCZCt02rvxRQ0CcAiXIkTf0kLW0n4uWzDwL/Qay0R+1VtSQX9Wx4CkoNPyN27I X-Received: by 2002:a37:5ac2:: with SMTP id o185-v6mr7318147qkb.115.1538040385341; Thu, 27 Sep 2018 02:26:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538040385; cv=none; d=google.com; s=arc-20160816; b=Nf4Sqy19+04trex3DelbArvpbw1UBxX76Qm183Lkvjztr1hOJZGyNQbBdhEPAPWNui IVlxXgcKbonkMbkhcJFXNKclD9Hf+48+lFGMBPmKOOZW14ktcx6Cut9pveYrknOC53c8 Z3KfZxGAYvrAh4cLjm7VSFnjJHBjy4SDHyl0O14iudp8OmhM2bu6ExdQUWGZZnMLyu4l yqMbjjvP5B7n+2dixkRsMbPZRROKetjr6iMafN3R8dv78dvWVpqTu2O95f4nld25Wmoi xqPBUp5c974CMu/6tD3W4V7y0Jag5UBr3cbVqElJwnxB7nLF1o9QPoY7JBsqUfmPjdws ne9A== 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=gbtlWhwTfhYGwtWkyr5McLHXhe5541P8VVrNxIhBT6YxhdzIT6CIEKXFS7Jl4Djcfz DyWgTk/BHU/pDzeuTRs/MZa0fKeqPi5X3/hCpzoh+eWueMpFT/XcRPUvniCQ9+7GmAFI 7egDWrlqJnGPAyIHoiF/tDU9Wsv8BZ6RV3c+mClLsv7bo7AqmZ6tOTm5tbr3IfyDuqqm FhxW33py6adDdJ6RWlH7o5r+Stzra3DDP0ktMCpwbXOLboFVQUpdKE+ySTnhMJSFkzWy RIihXpNTKttx41oj2P+OXDrEJvqlAFdYSlBHceseDTwNOc2IyqAXMuQdhf/974Y2RQu5 pWTA== 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 j125-v6si1108378qkc.143.2018.09.27.02.26.25 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Sep 2018 02:26:25 -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-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2D325308FC50; Thu, 27 Sep 2018 09:26:24 +0000 (UTC) Received: from t460s.redhat.com (ovpn-116-205.ams2.redhat.com [10.36.116.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id F196A2010D95; Thu, 27 Sep 2018 09:26:18 +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 v3 2/6] mm/memory_hotplug: make add_memory() take the device_hotplug_lock Date: Thu, 27 Sep 2018 11:25:50 +0200 Message-Id: <20180927092554.13567-3-david@redhat.com> In-Reply-To: <20180927092554.13567-1-david@redhat.com> References: <20180927092554.13567-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.25 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.43]); Thu, 27 Sep 2018 09:26:24 +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 Reviewed-by: Oscar Salvador --- .../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 Thu Sep 27 09:25:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10617705 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 AF2DB174A for ; Thu, 27 Sep 2018 09:26:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9C9022AFF0 for ; Thu, 27 Sep 2018 09:26:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8F53C2AFFB; Thu, 27 Sep 2018 09:26:38 +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 9DE592AFF3 for ; Thu, 27 Sep 2018 09:26:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 849358E0006; Thu, 27 Sep 2018 05:26:34 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 7A8EB8E0001; Thu, 27 Sep 2018 05:26:34 -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 64B118E0007; Thu, 27 Sep 2018 05:26:34 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by kanga.kvack.org (Postfix) with ESMTP id 32D608E0001 for ; Thu, 27 Sep 2018 05:26:34 -0400 (EDT) Received: by mail-qt1-f197.google.com with SMTP id k10-v6so1603721qtb.8 for ; Thu, 27 Sep 2018 02:26:34 -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=ks3Wo2pbVAQbPKus92hZ205lIywiYDKKfrIIBmcQzY6R+rl0VC8ieF2RBK2rhw1OJX Py2sP1vEyxlEBNpP+TLnR5lmo2oOG9uWMl6J4TUWwxlvBq3pOENHUz5DRe54bl3l29qI EXMpJ8yQ6E0QZmXjfYWcHX2Pp8jPYKlhZ5sz/uyLFaCW76xcf9Zujls43vOLQvTyoTb7 QrCRpgleT5bEeFKzSn84egQo49SIXF4GEN4eLPec2GvcTMtJs0eYVtR7J/rZwJaBV20B E6Pe5wt5YsWSPWHTECm6jMkGY8CEnVOkoVbasLECkQh942c92v0ERYUD3r0XZQrbaVlW 8H6Q== 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: ABuFfog+ZYVXm7bBhl2+OW9Hz2q1mCNg/Sfz/A/qrraQ23ooxeO7CdUt 6ziqSlVg3W/0+KrOI4rOtxo3XoEf5nHYewBE6NbzIVm8GLs+LBkwxZZzSX/mQZObPXMOm++zaTv vvXn/JQLHJCC9yM7Y9gJxNwbnt7eZ0aSxi/MuwrlSwgor5aFXx6uw0nfu+jzFuT655w== X-Received: by 2002:ac8:4993:: with SMTP id f19-v6mr7488485qtq.79.1538040393967; Thu, 27 Sep 2018 02:26:33 -0700 (PDT) X-Google-Smtp-Source: ACcGV61refMwrGZw5raNsr5BsA2ukCIJu1Oz/GJ3eUQNoFxUvrpfHQi6UoDzFlcVBO5My/bwq2ZS X-Received: by 2002:ac8:4993:: with SMTP id f19-v6mr7488463qtq.79.1538040393253; Thu, 27 Sep 2018 02:26:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538040393; cv=none; d=google.com; s=arc-20160816; b=tz01cvCqccePgoZcsvH8+TVw7MkRT7vKSsFkZM9Hvfk9BslPt61pBM4DLTmNcxU5pX 6lp6rEyxXihgO9/zbFENI6W4QnWnIriIHk9xBiVEYgB152f8BW91M2IWnz+hFsTSZLnA pVp0z7CTrtp4LW5YDem8V/B55K/QeTODx+/C5qLJrg5yvhDDSXyPDkch0P46Kk2ywk1G 4yi4Lb7Y+pX2srqa2bw/aRyVeWwvVWb5P8xUOQ76cbVvlOhuvTZa4qUap9lymhgPe07Z abEm/TH64qWyZNw1ew5eAEsgn/8Yus9xsEyVUO/vfgCAjFIO5DsFoehiRa94AY9Qzx3p 5wfg== 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=jmqfokzUqcVACjvTU3gllap3J+d1FKZqEuzYv038JWmEfm4VHTpkDIlMAiOcZySwX1 jUMwvvKYjmvhZ61sVf/AgsfFD/34wzPVz37eb2QPtQ7RBZVsl2SD85AGjMvREC/NW3D8 wie8VQ1qF91yM+dT5Ud5zqyQJlt8juw9XncbdZAiPeTo3WiQTgO61313AJaD9DzJSVeY Z5VEfxKTdy11XBkhk0RPk5uerstz4SC4LupiDXbtXDnkuvZDh+NnJ4Ib5JTXRa6brbvN E/Cc9jizd0togUZwGj1Qrue2wRpaAwEYNycyjONQYNd97nn/xqlCGG6yawwbsQ/T8iNu w2Ug== 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 p12-v6si1055179qtf.16.2018.09.27.02.26.33 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Sep 2018 02:26:33 -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-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1158467EBD; Thu, 27 Sep 2018 09:26:32 +0000 (UTC) Received: from t460s.redhat.com (ovpn-116-205.ams2.redhat.com [10.36.116.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id D5D052010D95; Thu, 27 Sep 2018 09:26:24 +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 v3 3/6] mm/memory_hotplug: fix online/offline_pages called w.o. mem_hotplug_lock Date: Thu, 27 Sep 2018 11:25:51 +0200 Message-Id: <20180927092554.13567-4-david@redhat.com> In-Reply-To: <20180927092554.13567-1-david@redhat.com> References: <20180927092554.13567-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.25 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 27 Sep 2018 09:26:32 +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 Reviewed-by: Oscar Salvador --- 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 Thu Sep 27 09:25: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: 10617711 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 C536F180E for ; Thu, 27 Sep 2018 09:26:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B80D22AFF0 for ; Thu, 27 Sep 2018 09:26:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AC5CB2AFF3; Thu, 27 Sep 2018 09:26: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=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 54ED82AFFB for ; Thu, 27 Sep 2018 09:26:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 169FC8E0002; Thu, 27 Sep 2018 05:26:39 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 13FEC8E0001; Thu, 27 Sep 2018 05:26:39 -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 F23EA8E0002; Thu, 27 Sep 2018 05:26:38 -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 C81168E0001 for ; Thu, 27 Sep 2018 05:26:38 -0400 (EDT) Received: by mail-qk1-f200.google.com with SMTP id u129-v6so1903959qkf.15 for ; Thu, 27 Sep 2018 02:26:38 -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=iXEDyoYlpmiRJ53EN2k+no1yiKBQc4VOTt1Yh1p8u5rTtZ12+STuh2eFp2ECioxNqx VWZUF2JHTmmaHGVBZRHXbSt8XJi5Rc1HU4mx6quWb13XfrrVMnD0bs5fB0V4itSLEBa6 eQuUQZKBFReBWiSqQQRx+6YR+o+lHBCrpV1XBq3Oc863/nqNaAEWrc0BoRl8p1F1b49K ptjJ5VNVX+Jml/cDkQKmlMUycXDtHbTfVeBuk2pl34mIzw95XQJ85qsZOnwwMebGF2YP FnwlsRGRS1brgW6GXDm8EQZYAXN6kS0A4ZjXMDG7kORqd+Wbkllwt1cy/9P4U4ffjaFz kRBQ== 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: ABuFfoggrmJeP6DOsmQxR7+jSt9SqmYi0lB1nKCAAladpiwMpKLzJ+c7 6i2cV4dUXKGcY4dMw0hAi9jdjTQDVt9rCdtTX9cvNTAoJsR7QuSx0BbEtrGgNTgukm/0harrHbo zUzYGBF3AUQrbwvRxdHoohqMHl0ar6ohwuzgXD9dGC7YpBeFyaDdgNfFeKoLtfGmb0g== X-Received: by 2002:a0c:ed44:: with SMTP id v4-v6mr7360394qvq.22.1538040398612; Thu, 27 Sep 2018 02:26:38 -0700 (PDT) X-Google-Smtp-Source: ACcGV62irA7HCKEMDKldaC1rwVgGZq4b9/scZxkWB8O5mhcHoNJYF0a7CA6kQY1YKw06dqADnmJa X-Received: by 2002:a0c:ed44:: with SMTP id v4-v6mr7360379qvq.22.1538040398137; Thu, 27 Sep 2018 02:26:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538040398; cv=none; d=google.com; s=arc-20160816; b=PQjzv31iT44UADi1vdI+75FUUB4CL1jD13257+Rhbx3sYVG90atq+gOXi2UWu6v5H+ l+e2v0cZsOe2bbLo+V8h1wfofilrmKJ6WwIGORSXXlMPNnoZEnxYRuzEMNO4XUw5QYmK dTCaf60f005U+/044mYOEISECXir/o27fQg4FS0eqlshA1CxaANTU+AYnRHZ/5RVr81n KYBkobr1+4Dh3k1RBUa+tMOZupIaK53FpGa94KPFjLCeARrfKMJUsbqkiN6p30OKJ1uT GP//gUyLD+T0ibCb1YnaiqfPIWj88doebtUgC+UBXS3HopGgc3c58zKrBeJg+wfRDstF 0/sw== 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=fjOTn+KoWPScgy8yKZYyKFFgkhC3eDPudFijrUcxjvmiRtWhYpohtMDo1q7JBGTj6f u3pe/38ukBEW9kqODBVzm7fGUCexJjqujyD9S1qU+tDdlu1mxQ96TCLa6hjKWoToOSSK Q/mPfWt14JIw8QKXs0Er49ILsNapNaNbb9t+Z23gnXg+netqZY/IREV60TUMIzxCjXIx HkXqpAvOxbaxTBm9rG6rYaUWshhMUjAo16pmgtZmM57R0ikz2qJm/y/3AzWAUVwDvqsj 0qjZ3X7/4jo7GQ9eJTcrYtupdMXxppHANpwnIX68kTB3AAFs/82X32JtJOOkVPN+t67D jFkw== 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 e12-v6si1048174qkm.237.2018.09.27.02.26.38 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Sep 2018 02:26:38 -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-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 48C0CA6DFD; Thu, 27 Sep 2018 09:26:37 +0000 (UTC) Received: from t460s.redhat.com (ovpn-116-205.ams2.redhat.com [10.36.116.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7ACF920154E7; Thu, 27 Sep 2018 09:26:32 +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 v3 4/6] powerpc/powernv: hold device_hotplug_lock when calling device_online() Date: Thu, 27 Sep 2018 11:25:52 +0200 Message-Id: <20180927092554.13567-5-david@redhat.com> In-Reply-To: <20180927092554.13567-1-david@redhat.com> References: <20180927092554.13567-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.25 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 27 Sep 2018 09:26:37 +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 Thu Sep 27 09:25: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: 10617713 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 BF30A14BD for ; Thu, 27 Sep 2018 09:26:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AEC8F2AFF0 for ; Thu, 27 Sep 2018 09:26:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A0F7B2AFFB; Thu, 27 Sep 2018 09:26:44 +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 2EEA02AFF0 for ; Thu, 27 Sep 2018 09:26:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9CDAD8E0007; Thu, 27 Sep 2018 05:26:42 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 9A3758E0001; Thu, 27 Sep 2018 05:26:42 -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 8BA928E0007; Thu, 27 Sep 2018 05:26:42 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by kanga.kvack.org (Postfix) with ESMTP id 5E9BC8E0001 for ; Thu, 27 Sep 2018 05:26:42 -0400 (EDT) Received: by mail-qk1-f199.google.com with SMTP id u195-v6so1900528qka.14 for ; Thu, 27 Sep 2018 02:26:42 -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=YaNu97MflHn/yt/ZRUeLi3WloF/HIoO1fgdEBWBwJm8=; b=NmvDdqvfxQ8FuFrJ635wHEU7myvzWTNWKTLzTv7lQ7ESQ2etcngb7zz3WgilnmuRIk iZsuDbBAiggkY7bmhCBrV/6xCxbN07q/fkVrHDWIq1PRpvWTetM0meYBaaPZi7d/ZRWJ 2x7xWQWUUYNt8oGzh2oYLoluMPJ0F7buG1dnwokWapXQ5WUPFJwSocTVL+oy8w8r1Q6W QwPod3ZZaw3cQ7RMD6i8SBVFs3v5ksjJYogBZ2ja+l7EyB+T8uWKIY4SnDCcq1ttt2w2 PS0aBuYqKpgJd5e9QTIPo0cYtvSMeVBu8t6pK8Rgg+gb0S2Sy/sh/SaI+1rdWQjfWyex HR0g== 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: ABuFfogBkdnmMibjhrvSsNrgMtZCYJrPJMChu/EO9CAetU3wTA35Uzff sMAfp2hBEtU75424Xe0QdjApd/8PeJ+6/4DanLNN9z8pCYlcC1/z/CuolmqV3rg6R6+kZ8FZ9SO Fa6wNdrh5/qb+mJRaZ6JejXBz/dKFNlr10BjOVKANzPKIlDRrQXB+FjTHl6I88TNQyQ== X-Received: by 2002:ac8:744a:: with SMTP id h10-v6mr7449752qtr.194.1538040402180; Thu, 27 Sep 2018 02:26:42 -0700 (PDT) X-Google-Smtp-Source: ACcGV6137tC/5OCEw+T1reeJqFsa4gaPOVmZvmtBf7U2WyER+LD6tHAHiagqBqgLRbrDIvUr/EDf X-Received: by 2002:ac8:744a:: with SMTP id h10-v6mr7449743qtr.194.1538040401759; Thu, 27 Sep 2018 02:26:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538040401; cv=none; d=google.com; s=arc-20160816; b=OXZ8yMpi1Rn0NxjUVRBypYTzds68CKMAI7QINZnUiVJcHBhUfoZaq1jaC89LAFDiNw wa6++d/gj6PuG1RzuXHseSxkMR+2thOjUMbGvMdz5MPHyCmZ/mtzDuI3fXVzSzlQFCRg uJz8/QLU2Esf3x0eOk9kO0MgDyzK1m/mFSzCH10NY/Yn81olq0MS7AZOs9s4+Gab72ZN 6mWEzXd70T+3uOl6xF5EdRZs/H58uLbD5HJ7ZiVf8TuJYPx/wN64SNsOLfe7F2sFyfkb NOrA7gFo8stgplvmYZ49u+tA2ESBgyu1mWuYx+U0hCT5eQENSvXiDCIs9JEaSlAVjdUc Engg== 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=YaNu97MflHn/yt/ZRUeLi3WloF/HIoO1fgdEBWBwJm8=; b=GI3d1swgeUMJzqqm5FKCjIs0JBmYiUBnrcnL3nN4XYwvrf0kEJXG8Qi26m2wr3gFuC ELFlGY8HK/EjBGIalwRvwsHzxKrC0op18fhANFGXxZ0uIvab7pg4+KbF33f1rb2x8Mfv XpWmWCHbnIScTST1onWEJqA0y7Ttohxayi+jS781xLu/C79EBYvrkxp1vBi9AQrjmdfZ xvWLpUBoBonxAR2D3GoHOHudEiIlN+HWTYlZ+75Uq6j2KA+V8N+APXDChasebnYp320i VhTvg1GXu/rFS0XxgV1D6k7CoZSTyLF36DvC4jUCePSJmlaMckMGn3GjTAftooqm9v5t GM2A== 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 u6-v6si1045603qkd.354.2018.09.27.02.26.41 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Sep 2018 02:26:41 -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-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DD78A30039BC; Thu, 27 Sep 2018 09:26:40 +0000 (UTC) Received: from t460s.redhat.com (ovpn-116-205.ams2.redhat.com [10.36.116.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id A85BE20154E7; Thu, 27 Sep 2018 09:26:37 +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 v3 5/6] powerpc/powernv: hold device_hotplug_lock when calling memtrace_offline_pages() Date: Thu, 27 Sep 2018 11:25:53 +0200 Message-Id: <20180927092554.13567-6-david@redhat.com> In-Reply-To: <20180927092554.13567-1-david@redhat.com> References: <20180927092554.13567-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.25 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.46]); Thu, 27 Sep 2018 09:26: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 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 Acked-by: Balbir Singh Signed-off-by: David Hildenbrand --- 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..84d038ed3882 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; @@ -110,6 +111,7 @@ static u64 memtrace_alloc_node(u32 nid, u64 size) /* Trace memory needs to be aligned to the size */ end_pfn = round_down(end_pfn - nr_pages, nr_pages); + lock_device_hotplug(); for (base_pfn = end_pfn; base_pfn > start_pfn; base_pfn -= nr_pages) { if (memtrace_offline_pages(nid, base_pfn, nr_pages) == true) { /* @@ -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); @@ -127,6 +128,7 @@ static u64 memtrace_alloc_node(u32 nid, u64 size) return base_pfn << PAGE_SHIFT; } } + unlock_device_hotplug(); return 0; } From patchwork Thu Sep 27 09:25: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: 10617715 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 2A15614BD for ; Thu, 27 Sep 2018 09:26:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1AA132AFF0 for ; Thu, 27 Sep 2018 09:26:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0E6792AFFB; Thu, 27 Sep 2018 09:26:48 +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 737542AFF0 for ; Thu, 27 Sep 2018 09:26:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 847D58E0008; Thu, 27 Sep 2018 05:26:45 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 81D508E0001; Thu, 27 Sep 2018 05:26:45 -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 6C41F8E0009; Thu, 27 Sep 2018 05:26:45 -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 3B6748E0008 for ; Thu, 27 Sep 2018 05:26:45 -0400 (EDT) Received: by mail-qt1-f198.google.com with SMTP id b12-v6so1591243qtp.16 for ; Thu, 27 Sep 2018 02:26:45 -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=Rd2I9G2Ic96FJbyZT2bUkX3kp820Ave9MgHnT+kuWYFaZ9lew6QlL8DTMraEnrsTEy WrS+FEYT4gNGRTaO/29VF7SSkJKmlobrdPLXF73LT9UYfPBdd1BnEbj3w0yB4D9jF7dd 58XO8B1NPSnMFyX4MnjpTp41axWdrjc0YM/PQ3Hl6bCaO5D7vA9nyaEJQ2i8YQzg5L6M V9oN3yXfhrJPXFH80K3zIEtKLgExqY1SgQpML3/UIUJyjlJI47+HFIay8qdfIz26GacL 6ImSEQyKLXxCGyl89pBC1KxUOeiCvKM2ryB3Z5d+FQnFoA7aLeyYwEFo2q4t7AIs/ufF 66Gw== 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: ABuFfoittoHOXoeMVlTg9RKfmF4W2ws8Oj/v9S68G6W1qRiPJzGuaOUl 6RQlW/3itowmkKIXmTu1jjD9L+OJxt0+KKi9oJ7YPJGbzgINb1Fn44F0crpX4gKu/VFeGfZ8q1j MvwB044cJt6sffNhDptsiLZt5nqnj/VD6+kZtUHQElNd5tYDaB/buMp5NL8QJGtXWHA== X-Received: by 2002:a0c:b88f:: with SMTP id y15-v6mr7267140qvf.203.1538040404998; Thu, 27 Sep 2018 02:26:44 -0700 (PDT) X-Google-Smtp-Source: ACcGV61vS7QreCR83itm/wcynIGrSRmyesw0N8/VZbJvMe9Z2UGn36Szwmoa1GNqDfSWwtrQapkt X-Received: by 2002:a0c:b88f:: with SMTP id y15-v6mr7267123qvf.203.1538040404514; Thu, 27 Sep 2018 02:26:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538040404; cv=none; d=google.com; s=arc-20160816; b=LCF+VkpqNbFRxdG/9FAQnGOPi1JwOmJnLvk77SC2nX59u8gZzIWp370bXP82+E4v+C j6Icg7gdcIdPZBbzCN34DFL8mQ/CRJ3vCMtweaAPqspet/gde9yKiI9/1v0SfdtOjsxO zwpacNiiX8FqA0inEzVCsEwiNT2YIMw/MtR186i0SmUJqHGE6b3BmF4U06PV5c9k4EsL iMr+vFQrplakY1LhT36u/B9ccaSEjAzD6NRUGfGzXx/Cf2syDI3+fYU1xI3EQL0n6rjB Z6SikqS4HoOkfjON+OILwbQ7SuY6tXHvBP6ZpSVhEzqVWPP5PHBQhpN08yM/32Ds4F0k Y9mA== 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=JrEkLg6vczKlXKmfOZuSuCqGZ72VbdAPgN74eSVT2Tc8WEcKzxdD8lNcBAbvv9ElK2 3G15EOk3S4Ei6xTRd8J4M6arGJlFrkKxMl0kANBV6pkUpOXyMmqoJQSIpU1s2k9rEWIg Oa2bQb8QzdriMgq6KGP6GBds+yRjqF6fO5cPX2ZQS6zKpHRwtUEZL2r8Fl79Acqiw8+j ts5f+MR8+FIKc1x0OSqMnO6SWRZYGkBhuICw2Mh2UsY5qiDRN4CVPOBo8/PxW7gpD2kh dBUr868rlNEoHF3btjYSXibCo5T1RPwFwjdOL/rqTWypgVYRWX41+fhG3ehpS9aFVQiP FfOg== 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 d1-v6si919600qtl.321.2018.09.27.02.26.44 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Sep 2018 02:26:44 -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-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 939AAA6E04; Thu, 27 Sep 2018 09:26:43 +0000 (UTC) Received: from t460s.redhat.com (ovpn-116-205.ams2.redhat.com [10.36.116.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3D8C62015AD1; Thu, 27 Sep 2018 09:26: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 , Jonathan Corbet , Michal Hocko , Andrew Morton Subject: [PATCH v3 6/6] memory-hotplug.txt: Add some details about locking internals Date: Thu, 27 Sep 2018 11:25:54 +0200 Message-Id: <20180927092554.13567-7-david@redhat.com> In-Reply-To: <20180927092554.13567-1-david@redhat.com> References: <20180927092554.13567-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.25 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 27 Sep 2018 09:26:43 +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 Reviewed-by: Oscar Salvador --- 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 ===========