Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp763873ybh; Wed, 11 Mar 2020 10:18:19 -0700 (PDT) X-Google-Smtp-Source: ADFU+vsTcvmV5f/rptH0CyW7Y0PQCbRU4Ip8sSxX2/fU47p2sIObwXl4BUm8sZrjQjToPZ2cjS+N X-Received: by 2002:a9d:2c44:: with SMTP id f62mr3271155otb.7.1583947099267; Wed, 11 Mar 2020 10:18:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583947099; cv=none; d=google.com; s=arc-20160816; b=xRmshLjETBjbYeiF7rBE3SleysSuV3txd6HsSHWuFg+jxUV6M0rhbgy1AfJC2e2mpE G2KM+yLbM19gXDvApiHU48Tza9ayR/FBKqPnUSoFTolHguxhSkRC3gTbY7kBfzLspmgv /V00bISXECEjvM7M4P8cadGcp14Br9TARs9LHEvAXPVsaNG5dABpBYQkujRmV8TqUNe7 r4OKCDOHLD4uPsA3AOKMqz9LlgUjsrii8/rZBf1o89f1+brB26jSmEQtICKR7V1oC/VN UWrRPWYX1PCWpG6d+DNBpaON04tC2+RS0R8yMCJXiIIDtmlLNQT9vsgQYx7OIgYVDAZe mySQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=R9YlCAxZTLeLO624BWfRaiPaRVXS4Jlz4OonzSFN6/Y=; b=wBEi7SbOa8pYem3i0f5OVyHbzvrJZwXeF43NaL47b/kNMWz763XkOXHxiEwT0gamX4 rDSuB56gK02QzUBHG5wBn7t5quUsBwAkZpKLMarDip4ILs4ww2yeS+5abWJlZDnOInK9 Hpr46vrF2KL2qRUuBhOdUPVqgEC8oECbbxOzLhQWclh59Wu7GTjdopcDhK8s9taDy7yM ri0ry0xCQ+9EyBgPvot/62Pd5h4QQV11U6tlSurx0hvmPbOE2Hig7sYwVJ8M2vt4hmt+ ogJCfzYU4yn618dSWzjU0T8/78fRzTC9mDoy9uL5VL3pOUuHKyOufNwESWlPX7vVGbih 4JLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=McMmTybS; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q4si1416578oth.163.2020.03.11.10.18.06; Wed, 11 Mar 2020 10:18:19 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=McMmTybS; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730363AbgCKRQ3 (ORCPT + 99 others); Wed, 11 Mar 2020 13:16:29 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:47895 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730515AbgCKRQ1 (ORCPT ); Wed, 11 Mar 2020 13:16:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1583946985; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=R9YlCAxZTLeLO624BWfRaiPaRVXS4Jlz4OonzSFN6/Y=; b=McMmTybSJ1vCrmZfwO2N0lxXniCJ6wTYgPY5NWDHLxWOQbmYP8XY6WTXh9URggoqKVK4R7 QfeM5+RpTEJ1hC5xV0TP0qc9k3PAVm350SwjNthB6m5xLu5+a1Iuksgc8BHKqeIYRlxwFw /JZRwLkdZ4s4XGrPHJASKCh7i1yl9SU= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-248-cSO6Lp6oPDiaSdR8FyW8Rw-1; Wed, 11 Mar 2020 13:16:21 -0400 X-MC-Unique: cSO6Lp6oPDiaSdR8FyW8Rw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F0C6D107ACCA; Wed, 11 Mar 2020 17:16:19 +0000 (UTC) Received: from t480s.redhat.com (ovpn-116-132.ams2.redhat.com [10.36.116.132]) by smtp.corp.redhat.com (Postfix) with ESMTP id 26F5D60BEE; Wed, 11 Mar 2020 17:16:16 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, virtio-dev@lists.oasis-open.org, virtualization@lists.linux-foundation.org, kvm@vger.kernel.org, Michal Hocko , Andrew Morton , "Michael S . Tsirkin" , David Hildenbrand , Oscar Salvador , Michal Hocko , Pavel Tatashin , Wei Yang , Dan Williams , Qian Cai Subject: [PATCH v2 07/10] mm/memory_hotplug: Introduce offline_and_remove_memory() Date: Wed, 11 Mar 2020 18:14:19 +0100 Message-Id: <20200311171422.10484-8-david@redhat.com> In-Reply-To: <20200311171422.10484-1-david@redhat.com> References: <20200311171422.10484-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Content-Transfer-Encoding: quoted-printable 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_hotplu= g.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); =20 #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 de= vices + * that logically unplugged all memory (so it's no longer in use) and wa= nt to + * offline + remove the memory block. + */ +int offline_and_remove_memory(int nid, u64 start, u64 size) +{ + struct memory_block *mem; + int rc =3D -EINVAL; + + if (!IS_ALIGNED(start, memory_block_size_bytes()) || + size !=3D memory_block_size_bytes()) + return rc; + + lock_device_hotplug(); + mem =3D find_memory_block(__pfn_to_section(PFN_DOWN(start))); + if (mem) + rc =3D device_offline(&mem->dev); + /* Ignore if the device is already offline. */ + if (rc > 0) + rc =3D 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 =3D 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 */ --=20 2.24.1