Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp1074601ybl; Thu, 12 Dec 2019 09:14:44 -0800 (PST) X-Google-Smtp-Source: APXvYqwc2BzT19KDmxL598oizUYMoPO4plxxMBP0CuZ5fwH25HFo/t6A1S8XHYkEc617lADJvbFd X-Received: by 2002:aca:52c4:: with SMTP id g187mr5794521oib.76.1576170884804; Thu, 12 Dec 2019 09:14:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576170884; cv=none; d=google.com; s=arc-20160816; b=bV3iS1Ftm2J53RjpXoQBa8oQZ+gdvHingoHSSc6VPJQU78kRIJ6b/vs/onC/yalCxd jUSUirReJFJ8zbpS2FWAphlmYypmZhjjxAy3yPaGyHfSHwa7UktGIVTJuMEwLxDpNyqY YcPAHJq0DnUffasi8YdXoYZ8gaIGl6dPaKTatBr1IRCF/fIPPCvCTtngbbmRXBagkkLB TFSFyKykLDi8BUl2Dst/o7fGbijEGa7zSa/wpcDdBxGk7u/MR+8/D1gfJgQBujYxxBGi 5zGzUTm8wDE74NHZ+9Mci0rbJ2XcvUkJmFLrY18+NVeBGytqY5cLaA3lcORllaDPVil5 rfMg== 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=Q8aOIkOkK1yTkK/LZIwF+2RpIBIYKbx1CgOlql+T67s=; b=oQi20s8PW3BdpEPoeYQAKZHumP34RxW6qYGZV46bO1aklt66t1civcAS4Z1ERd1oM+ tCjR0vfrj4Ubv+jgqfxN51AdU4t301uHKn3uwv1SUDx3C6HZknsR1Jp0oL3g+0o/KzqU CohDaY4KGEzAkkOEtoGC18AM8XPs4TUM96i1xNS84aNs4EXSvyZedt+SJoznaZ+VCaMR o5EBm+cKFhMejRPfHoFPATXNRS1GjJu7NZzU3Vlgr+UlZtfEGBRwhnNmKhNj7fowiZ1k mpMKHP1V7Lpj/vzlVN+53YpOSFOLS0VMwGIg5iUC5ZMIWE7prCT/j8mNdXpXzrWoYhIX kveA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OxGhcbJD; 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 h139si3423216oib.85.2019.12.12.09.14.32; Thu, 12 Dec 2019 09:14:44 -0800 (PST) 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=OxGhcbJD; 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 S1730223AbfLLRNP (ORCPT + 99 others); Thu, 12 Dec 2019 12:13:15 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:25721 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730214AbfLLRNO (ORCPT ); Thu, 12 Dec 2019 12:13:14 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1576170793; 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=Q8aOIkOkK1yTkK/LZIwF+2RpIBIYKbx1CgOlql+T67s=; b=OxGhcbJDp1jXG4ncfOys8SFr4VcLmv1f1J9cu3eoT97Q3lHpb+OLtM5zDxnaysqnQudqkm 7pbHTdmyIZrBqZCq3QWs6TGy45CX73i7EC++LsoZfKCANdGt8fBENLgKQebkcm9p7k7gdX 3nACjykl4dw+65Vb2HLzGYeo67dX8jw= 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-302-VFP4204LOCqgDjYWOQ92WA-1; Thu, 12 Dec 2019 12:13:09 -0500 X-MC-Unique: VFP4204LOCqgDjYWOQ92WA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AE2CA800EB5; Thu, 12 Dec 2019 17:13:07 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-65.ams2.redhat.com [10.36.117.65]) by smtp.corp.redhat.com (Postfix) with ESMTP id A064B5C1C3; Thu, 12 Dec 2019 17:12:59 +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 , Jason Wang , Oscar Salvador , Igor Mammedov , Dave Young , Dan Williams , Pavel Tatashin , Stefan Hajnoczi , Vlastimil Babka Subject: [PATCH RFC v4 09/13] virtio-mem: Offline and remove completely unplugged memory blocks Date: Thu, 12 Dec 2019 18:11:33 +0100 Message-Id: <20191212171137.13872-10-david@redhat.com> In-Reply-To: <20191212171137.13872-1-david@redhat.com> References: <20191212171137.13872-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Let's offline+remove memory blocks once all subblocks are unplugged. We can use the new Linux MM interface for that. As no memory is in use anymore, this shouldn't take a long time and shouldn't fail. There might be corner cases where the offlining could still fail (especially, if another notifier NACKs the offlining request). Cc: "Michael S. Tsirkin" Cc: Jason Wang Cc: Oscar Salvador Cc: Michal Hocko Cc: Igor Mammedov Cc: Dave Young Cc: Andrew Morton Cc: Dan Williams Cc: Pavel Tatashin Cc: Stefan Hajnoczi Cc: Vlastimil Babka Signed-off-by: David Hildenbrand --- drivers/virtio/virtio_mem.c | 47 +++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c index a12a0f9c076b..807d4e393427 100644 --- a/drivers/virtio/virtio_mem.c +++ b/drivers/virtio/virtio_mem.c @@ -436,6 +436,28 @@ static int virtio_mem_mb_remove(struct virtio_mem *v= m, unsigned long mb_id) return remove_memory(nid, addr, memory_block_size_bytes()); } =20 +/* + * Try to offline and remove a memory block from Linux. + * + * Must not be called with the vm->hotplug_mutex held (possible deadlock= with + * onlining code). + * + * Will not modify the state of the memory block. + */ +static int virtio_mem_mb_offline_and_remove(struct virtio_mem *vm, + unsigned long mb_id) +{ + const uint64_t addr =3D virtio_mem_mb_id_to_phys(mb_id); + int nid =3D vm->nid; + + if (nid =3D=3D NUMA_NO_NODE) + nid =3D memory_add_physaddr_to_nid(addr); + + dev_dbg(&vm->vdev->dev, "offlining and removing memory block: %lu\n", + mb_id); + return offline_and_remove_memory(nid, addr, memory_block_size_bytes()); +} + /* * Trigger the workqueue so the device can perform its magic. */ @@ -529,7 +551,13 @@ static void virtio_mem_notify_offline(struct virtio_= mem *vm, break; } =20 - /* trigger the workqueue, maybe we can now unplug memory. */ + /* + * Trigger the workqueue, maybe we can now unplug memory. Also, + * when we offline and remove a memory block, this will re-trigger + * us immediately - which is often nice because the removal of + * the memory block (e.g., memmap) might have freed up memory + * on other memory blocks we manage. + */ virtio_mem_retry(vm); } =20 @@ -1275,7 +1303,8 @@ static int virtio_mem_mb_unplug_any_sb_offline(stru= ct virtio_mem *vm, * Unplug the desired number of plugged subblocks of an online memory bl= ock. * Will skip subblock that are busy. * - * Will modify the state of the memory block. + * Will modify the state of the memory block. Might temporarily drop the + * hotplug_mutex. * * Note: Can fail after some subblocks were successfully unplugged. Can * return 0 even if subblocks were busy and could not get unplugge= d. @@ -1331,9 +1360,19 @@ static int virtio_mem_mb_unplug_any_sb_online(stru= ct virtio_mem *vm, } =20 /* - * TODO: Once all subblocks of a memory block were unplugged, we want - * to offline the memory block and remove it. + * Once all subblocks of a memory block were unplugged, offline and + * remove it. This will usually not fail, as no memory is in use + * anymore - however some other notifiers might NACK the request. */ + if (virtio_mem_mb_test_sb_unplugged(vm, mb_id, 0, vm->nb_sb_per_mb)) { + mutex_unlock(&vm->hotplug_mutex); + rc =3D virtio_mem_mb_offline_and_remove(vm, mb_id); + mutex_lock(&vm->hotplug_mutex); + if (!rc) + virtio_mem_mb_set_state(vm, mb_id, + VIRTIO_MEM_MB_STATE_UNUSED); + } + return 0; } =20 --=20 2.23.0