Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp623678ybz; Wed, 15 Apr 2020 15:21:15 -0700 (PDT) X-Google-Smtp-Source: APiQypIks5udgF4O97XYU34eVUfLsJLA81Fdln6OEGq5zOokjEHF6DqD5So/XD42ZbysrlDazdvE X-Received: by 2002:a50:ec11:: with SMTP id g17mr27784900edr.343.1586989274936; Wed, 15 Apr 2020 15:21:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586989274; cv=none; d=google.com; s=arc-20160816; b=cmXqQtH9sZKs4YOE/0j9j6QYaA8hEDAepISMURWlxANq3FLmWHRz3bezTfAb+fFvkh vls4JNmam0qJPcXQ7HibzMmacvyOZJj2xz8ykTSAIV7f/fzmqCIc9vbZxo2s+ZB1w2zN G2RBDlB2hNA/bJXsAEv3RFEf8qqaBAudPGOGR/LxoZ59VWiGPMpJjjQlvZ6nujBQhEKx NgGLkmZf1KptNiBZ1gJs+l08BSlPvD1VZv7Idz6VEerIdIEzxPFttxIyh+IkWg77/abr T/BbG1lQ07+cqrKaucMQkPv9tKo4kG+DV4dZ8lPVtb+tns7FsTm7iS3vHA29gEIlMtd9 M1SQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=2PnPX+IXLMxHQhocmFsN9EnKpVsBEOL6jEKQXmubIZY=; b=p24HDiQUEKTElqmkDPMNZ3WaC0pVbDhJMmKBnGNJetRpQpHi5yA6bgy8nQXeL5xVMv 4udQruu12u/LzpyAORD3etJWnm743n8jD7D+/ZGbCsd3QP2X4LVmX3CHeL5Rg846fxKi 0cAg5cMgVVqlaRpY6T3FYqrU66sSxIj9OpvdM3cqJSJhPv2Ba8O8UAzLLkYUPqIzEoCo V/flDYKA3Ry17/i8kqryi65OPbEfPc/1VEHPFqp8ThGwzRVfPAv2BbNu80La1X+XLFZC Ly0j2K/lZgghISAHAuW/JhZxE4OlNoFnDHW6CyGPCT9HlIMmX3ooKdx2h+H/T2ZbUNOL Qyrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=hO45c4hR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c6si7250663edq.393.2020.04.15.15.20.51; Wed, 15 Apr 2020 15:21:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=hO45c4hR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2635184AbgDOHg0 (ORCPT + 99 others); Wed, 15 Apr 2020 03:36:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2635170AbgDOHft (ORCPT ); Wed, 15 Apr 2020 03:35:49 -0400 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CC003C061A0C; Wed, 15 Apr 2020 00:35:48 -0700 (PDT) Received: by mail-wm1-x343.google.com with SMTP id a201so17289193wme.1; Wed, 15 Apr 2020 00:35:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=2PnPX+IXLMxHQhocmFsN9EnKpVsBEOL6jEKQXmubIZY=; b=hO45c4hR06tLylAL0NdxMtKX35mMSKm39HNyEbJ4X+oT9vzdlZ3GkfntRR6LqspePR IKc8WMTCwA3E5qPQbITy7bcS3eZ3ZzSXLF9he3yEgXJmUb1bS8OiyFSoVVYJe8vmZ4xv v+JaYUie5vpK2l31zccuEjSZrXkWiSj9grh8aLtKyyDUNM6z9Z3QRcxuolDT1LV8JFTx IR5O+n0hmqmjmRfvV2Am/ZjblhoR+TK8ETJW3YSq2K+zSeM1Z8rXHYecelaCLFHOr2Lk ksBtf2ry9h38/GD4NjzaJudx4c1jMY13R7S3eZuQpHR2+lI3l17b8PXOsMLKBxGRoXKZ n0IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=2PnPX+IXLMxHQhocmFsN9EnKpVsBEOL6jEKQXmubIZY=; b=f2P/O4ZnXpYKlH7KvP7z8xB4GnvPzslyA3LMgWN6dtRFC5xOR2SZewiIiedd38nZne mHJTdJKzLmbSXn4ijuUYxREywRBUEOZFoTr8fyauyJG8QuwGo4j3sDXPmQ2SdPwQoN5s R04UQEJO0u4fpA7mTUiOhuTesf5UMSkrpy3xlCpekcq8uSUoFo6sUaZfoEb/GgfX8mUd tdYGUjSGo6FvZ+r7XMPZBtUj9tPaINBEpKPtjExXrQYkOtPSOZ6PZC9gEx1PpY3/B6Xm 2YFVdzTKSwLxt7BTh5Jkk5cKBtoPaO3KnD03kQ4O2+oQ/QdYr1ynBHFC8hNQW1OQsEM6 QYyw== X-Gm-Message-State: AGi0Pub+0VV36dqu81oEEcJD5FoSmTFWnbT9H38P5t5BHhPE546heEZZ CUUTorO5Q3Raq3LNmSqPKWWES3Yq8U0AKo6Z5LOn7IHK X-Received: by 2002:a7b:c20f:: with SMTP id x15mr3638234wmi.2.1586936147523; Wed, 15 Apr 2020 00:35:47 -0700 (PDT) MIME-Version: 1.0 References: <20200311171422.10484-1-david@redhat.com> <20200311171422.10484-8-david@redhat.com> In-Reply-To: <20200311171422.10484-8-david@redhat.com> From: Pankaj Gupta Date: Wed, 15 Apr 2020 09:35:36 +0200 Message-ID: Subject: Re: [PATCH v2 07/10] mm/memory_hotplug: Introduce offline_and_remove_memory() To: David Hildenbrand Cc: LKML , Linux MM , virtio-dev@lists.oasis-open.org, virtualization@lists.linux-foundation.org, kvm@vger.kernel.org, Michal Hocko , Andrew Morton , "Michael S . Tsirkin" , Oscar Salvador , Michal Hocko , Pavel Tatashin , Wei Yang , Dan Williams , Qian Cai Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > virtio-mem wants to offline and remove a memory block once it unplugged > all subblocks (e.g., using alloc_contig_range()). Let's provide > an interface to do that from a driver. virtio-mem already supports to > offline partially unplugged memory blocks. Offlining a fully unplugged > memory block will not require to migrate any pages. All unplugged > subblocks are PageOffline() and have a reference count of 0 - so > offlining code will simply skip them. > > All we need is an interface to offline and remove the memory from kernel > module context, where we don't have access to the memory block devices > (esp. find_memory_block() and device_offline()) and the device hotplug > lock. > > To keep things simple, allow to only work on a single memory block. > > Cc: Andrew Morton > Cc: David Hildenbrand > Cc: Oscar Salvador > Cc: Michal Hocko > Cc: Pavel Tatashin > Cc: Wei Yang > Cc: Dan Williams > Cc: Qian Cai > Signed-off-by: David Hildenbrand > --- > include/linux/memory_hotplug.h | 1 + > mm/memory_hotplug.c | 37 ++++++++++++++++++++++++++++++++++ > 2 files changed, 38 insertions(+) > > diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h > index f4d59155f3d4..a98aa16dbfa1 100644 > --- a/include/linux/memory_hotplug.h > +++ b/include/linux/memory_hotplug.h > @@ -311,6 +311,7 @@ extern void try_offline_node(int nid); > extern int offline_pages(unsigned long start_pfn, unsigned long nr_pages); > extern int remove_memory(int nid, u64 start, u64 size); > extern void __remove_memory(int nid, u64 start, u64 size); > +extern int offline_and_remove_memory(int nid, u64 start, u64 size); > > #else > static inline bool is_mem_section_removable(unsigned long pfn, > diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c > index ab1c31e67fd1..d0d337918a15 100644 > --- a/mm/memory_hotplug.c > +++ b/mm/memory_hotplug.c > @@ -1818,4 +1818,41 @@ int remove_memory(int nid, u64 start, u64 size) > return rc; > } > EXPORT_SYMBOL_GPL(remove_memory); > + > +/* > + * Try to offline and remove a memory block. Might take a long time to > + * finish in case memory is still in use. Primarily useful for memory devices > + * that logically unplugged all memory (so it's no longer in use) and want to > + * offline + remove the memory block. > + */ > +int offline_and_remove_memory(int nid, u64 start, u64 size) > +{ > + struct memory_block *mem; > + int rc = -EINVAL; > + > + if (!IS_ALIGNED(start, memory_block_size_bytes()) || > + size != memory_block_size_bytes()) > + return rc; > + > + lock_device_hotplug(); > + mem = find_memory_block(__pfn_to_section(PFN_DOWN(start))); > + if (mem) > + rc = device_offline(&mem->dev); > + /* Ignore if the device is already offline. */ > + if (rc > 0) > + rc = 0; > + > + /* > + * In case we succeeded to offline the memory block, remove it. > + * This cannot fail as it cannot get onlined in the meantime. > + */ > + if (!rc) { > + rc = try_remove_memory(nid, start, size); > + WARN_ON_ONCE(rc); > + } > + unlock_device_hotplug(); > + > + return rc; > +} > +EXPORT_SYMBOL_GPL(offline_and_remove_memory); > #endif /* CONFIG_MEMORY_HOTREMOVE */ Acked-by: Pankaj Gupta