Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp1073326ybl; Thu, 12 Dec 2019 09:13:41 -0800 (PST) X-Google-Smtp-Source: APXvYqybe6Azu2oE1xJdshTnB1ncRvI+Hif/xFWY/Hn2oOBeqjt8O/Vhrn0sblhZhrBV0cKqAYUH X-Received: by 2002:a9d:7a46:: with SMTP id z6mr9542848otm.194.1576170821214; Thu, 12 Dec 2019 09:13:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576170821; cv=none; d=google.com; s=arc-20160816; b=II3AJdzNmyUv8p2GWNUW/HSGVcrFbDthnigZAWVV3LmEtrWmq+hY4/9fhVl0sNdDce Ah6/8P2o5Wtpkw8Yq9cl/l45cDuk3xrnpyJHIEaZXjkeVBXkSBQBWSeTBDgWgPFZgqCj nqgDN33soyOCvbSa3MgZfcUfjhxnZbOuBOWgvqIYzfvWcOD5UskZsa0AFomlpQJAsnWj 4CWUFEjL5cHjl00VHpsqj1DGYH8puyKC8U5SOWdG+jRgBe7zEyS8oOv0CAKimRPsbCvh u+Y4SOyETC+2zzjgrzut4Wkd2bHNjftQK114/krUGkyd8B8uOXel4LMwRwKVJ0QLM95u O6eQ== 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=dzz5SKB6UhLw20LV56TWJK/KT32BykuRTQ7b1mPtnTk=; b=NCGEayoxdyBVz4jzN4s1xz6AqgHMilc/CHVgbIBlB3GlVAZpxjZOHD1WGAZ5L9m4VD bYullPZiZyEYGdoyS5TSWQGQRBnvzPfKlp/hc4qQmeJtH1Y3lWldQoHk3xt/JTTvH0sg 7uU8RTzx1VPg/IU3Cu0FTqI0Emc9Qa8KXJZhKhdi5hSX0igxSmNFJeXuDq6Po0WqfUK5 QxvBKR0VUamSGJ3jF3BWZcqv25PGq1yFdYHFe8XDxCMa05T52VpuBiPR7VeRMQcmGWY5 hQcjYde9EzNtcsiLxZvIfQ4pahg+k9kw8YUHCXjSe68FKejy6VIWGcn8tHYoRr4po13W e7tA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=KTsQLEoj; 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 16si4687255otk.151.2019.12.12.09.13.27; Thu, 12 Dec 2019 09:13:41 -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=KTsQLEoj; 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 S1730141AbfLLRMb (ORCPT + 99 others); Thu, 12 Dec 2019 12:12:31 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:20581 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730135AbfLLRM0 (ORCPT ); Thu, 12 Dec 2019 12:12:26 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1576170744; 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=dzz5SKB6UhLw20LV56TWJK/KT32BykuRTQ7b1mPtnTk=; b=KTsQLEojonPQcMUVRE6L+IdMb81CgCW6p38dNeHzavwQcvQpsv+Kiwnrdx5r7nx+ImPENp 3/w8BTqSdp6VwIp/fOHJtAhsbkIuBb83sae46+xzIJhOvJsgi0i6mZT6C8XilTCG97r8Wa PvQ27QP5k2xZ3c4lIVCenanHvXfyYH0= 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-252-1cgmeF64NHa8lbKwCDDpnQ-1; Thu, 12 Dec 2019 12:12:21 -0500 X-MC-Unique: 1cgmeF64NHa8lbKwCDDpnQ-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 4D5BC18A6EC0; Thu, 12 Dec 2019 17:12:19 +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 C88F35C1C3; Thu, 12 Dec 2019 17:12:13 +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 03/13] virtio-mem: Paravirtualized memory hotunplug part 1 Date: Thu, 12 Dec 2019 18:11:27 +0100 Message-Id: <20191212171137.13872-4-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 Unplugging subblocks of memory blocks that are offline is easy. All we have to do is watch out for concurrent onlining acticity. 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 | 109 +++++++++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 2 deletions(-) diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c index 8f4ceeab3d7c..f1af05def5df 100644 --- a/drivers/virtio/virtio_mem.c +++ b/drivers/virtio/virtio_mem.c @@ -119,7 +119,7 @@ struct virtio_mem { * * When this lock is held the pointers can't change, ONLINE and * OFFLINE blocks can't change the state and no subblocks will get - * plugged. + * plugged/unplugged. */ struct mutex hotplug_mutex; bool hotplug_active; @@ -321,6 +321,19 @@ static bool virtio_mem_mb_test_sb_plugged(struct vir= tio_mem *vm, bit + count; } =20 +/* + * Test if all selected subblocks are unplugged. + */ +static bool virtio_mem_mb_test_sb_unplugged(struct virtio_mem *vm, + unsigned long mb_id, int sb_id, + int count) +{ + const int bit =3D (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb + sb_id; + + /* TODO: Helper similar to bitmap_set() */ + return find_next_bit(vm->sb_bitmap, bit + count, bit) >=3D bit + count; +} + /* * Find the first plugged subblock. Returns vm->nb_sb_per_mb in case the= re is * none. @@ -511,6 +524,9 @@ static void virtio_mem_notify_offline(struct virtio_m= em *vm, BUG(); break; } + + /* trigger the workqueue, maybe we can now unplug memory. */ + virtio_mem_retry(vm); } =20 static void virtio_mem_notify_online(struct virtio_mem *vm, unsigned lon= g mb_id, @@ -1123,6 +1139,93 @@ static int virtio_mem_plug_request(struct virtio_m= em *vm, uint64_t diff) return rc; } =20 +/* + * Unplug the desired number of plugged subblocks of an offline memory b= lock. + * Will fail if any subblock cannot get unplugged (instead of skipping i= t). + * + * Will modify the state of the memory block. Might temporarily drop the + * hotplug_mutex. + * + * Note: Can fail after some subblocks were successfully unplugged. + */ +static int virtio_mem_mb_unplug_any_sb_offline(struct virtio_mem *vm, + unsigned long mb_id, + uint64_t *nb_sb) +{ + int rc; + + rc =3D virtio_mem_mb_unplug_any_sb(vm, mb_id, nb_sb); + + /* some subblocks might have been unplugged even on failure */ + if (!virtio_mem_mb_test_sb_plugged(vm, mb_id, 0, vm->nb_sb_per_mb)) + virtio_mem_mb_set_state(vm, mb_id, + VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL); + if (rc) + return rc; + + if (virtio_mem_mb_test_sb_unplugged(vm, mb_id, 0, vm->nb_sb_per_mb)) { + /* + * Remove the block from Linux - this should never fail. + * Hinder the block from getting onlined by marking it + * unplugged. Temporarily drop the mutex, so + * any pending GOING_ONLINE requests can be serviced/rejected. + */ + virtio_mem_mb_set_state(vm, mb_id, + VIRTIO_MEM_MB_STATE_UNUSED); + + mutex_unlock(&vm->hotplug_mutex); + rc =3D virtio_mem_mb_remove(vm, mb_id); + BUG_ON(rc); + mutex_lock(&vm->hotplug_mutex); + } + return 0; +} + +/* + * Try to unplug the requested amount of memory. + */ +static int virtio_mem_unplug_request(struct virtio_mem *vm, uint64_t dif= f) +{ + uint64_t nb_sb =3D diff / vm->subblock_size; + unsigned long mb_id; + int rc; + + if (!nb_sb) + return 0; + + /* + * We'll drop the mutex a couple of times when it is safe to do so. + * This might result in some blocks switching the state (online/offline= ) + * and we could miss them in this run - we will retry again later. + */ + mutex_lock(&vm->hotplug_mutex); + + /* Try to unplug subblocks of partially plugged offline blocks. */ + virtio_mem_for_each_mb_state(vm, mb_id, + VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL) { + rc =3D virtio_mem_mb_unplug_any_sb_offline(vm, mb_id, + &nb_sb); + if (rc || !nb_sb) + goto out_unlock; + cond_resched(); + } + + /* Try to unplug subblocks of plugged offline blocks. */ + virtio_mem_for_each_mb_state(vm, mb_id, VIRTIO_MEM_MB_STATE_OFFLINE) { + rc =3D virtio_mem_mb_unplug_any_sb_offline(vm, mb_id, + &nb_sb); + if (rc || !nb_sb) + goto out_unlock; + cond_resched(); + } + + mutex_unlock(&vm->hotplug_mutex); + return 0; +out_unlock: + mutex_unlock(&vm->hotplug_mutex); + return rc; +} + /* * Try to unplug all blocks that couldn't be unplugged before, for examp= le, * because the hypervisor was busy. @@ -1203,8 +1306,10 @@ static void virtio_mem_run_wq(struct work_struct *= work) if (vm->requested_size > vm->plugged_size) { diff =3D vm->requested_size - vm->plugged_size; rc =3D virtio_mem_plug_request(vm, diff); + } else { + diff =3D vm->plugged_size - vm->requested_size; + rc =3D virtio_mem_unplug_request(vm, diff); } - /* TODO: try to unplug memory */ } =20 switch (rc) { --=20 2.23.0