Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp760794ybj; Thu, 7 May 2020 07:05:52 -0700 (PDT) X-Google-Smtp-Source: APiQypIMYKyUv/e4tHKuWAo8uxTNwF9h0m0C2R15IR5AGeTeyT7NpieNAM50UiO6YP2BdEN0whha X-Received: by 2002:aa7:d894:: with SMTP id u20mr11281356edq.205.1588860352358; Thu, 07 May 2020 07:05:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588860352; cv=none; d=google.com; s=arc-20160816; b=QozMzL0KiZBoI+qO8Qbqn/XmeDf8ziDnxdTpNM6xY6I2MrQYwrE3GQAjbmKXaCA7fc 0hfg7xTdnMQmaEeAYZIrAoJtxLd6RJuEtm4YQ0a0j6iRNbDNDGN9euN0ixgLONvbfbgL csGFy8fLQ0pdmQFBndB+rDo0WTvUQrj+XA4jn7qULbbOVrCGbN6mc5cphMqlyAW5qSJZ Fj663Zaw3BzXqILo/AyPRmj/EfVzev89HpXZBwWHuDNYIzXSjDp6jGrnotReb82qGBoq +jw9nM9KJeQ6aPHbwzht9mhDMkX3QWbtvs4NZxbcfv1b1D8cer10GfHv8zbedM9APo/2 ZZRw== 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=6AeTXBUWraUGOO3RfZQVHeXymOvT6+H0zVdHTC1hVsA=; b=zThchGlYjBPYNL+a09gSYkv8JSJgIE5a8dhxJdSbFC9qao5e9mR94kmGYVf3Vez5ng Z8xGiMA1D2PiWgMKITurp6ZbE4jQYBtJsR6RekZNxfUQgNH7WL++aEZtEWGeisL+yrvF //Ap6lJHzqNWyS82++F0g99QhEE0tjztOf997cbwPEQPouKND2GUyJJxz+fxBH/8qBAC KyZ3pcrC8eFH6qykabmu8vWxN2PX+LaAIQMtnajA8o+C1NF4/qLX9d2bkj4W5yvZAHLK p9jdjrjAAzSYJlVp9nBHGllImY/TtSaEAFtufJ7A9dAeKE70h1WPeZOW8nwbkSCF2isc RwJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Ny/6qot4"; 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=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n8si3392041edb.372.2020.05.07.07.05.24; Thu, 07 May 2020 07:05:52 -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=@redhat.com header.s=mimecast20190719 header.b="Ny/6qot4"; 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=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727111AbgEGOCq (ORCPT + 99 others); Thu, 7 May 2020 10:02:46 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:20597 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727096AbgEGOCp (ORCPT ); Thu, 7 May 2020 10:02:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588860163; 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=6AeTXBUWraUGOO3RfZQVHeXymOvT6+H0zVdHTC1hVsA=; b=Ny/6qot4Pf5yt2xkVzaBuwXDdyJX2Qpj2EOc1foBSs2N/3M4lAGkbu8KdRH4GziucdRmhw V8PLuKwFtsuSjVWC9/2vIggQHI9AuqBZ46lsmvh2EObtuU1BsPxA3Fc+qCnIwbQmRcesrJ P2tw7oZ9dRqDFwvwXIqcFA52lLjm3tU= 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-98-GR6CGQAuPhGTi2DnZ_knSg-1; Thu, 07 May 2020 10:02:36 -0400 X-MC-Unique: GR6CGQAuPhGTi2DnZ_knSg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D3B548014D9; Thu, 7 May 2020 14:02:34 +0000 (UTC) Received: from t480s.redhat.com (ovpn-113-245.ams2.redhat.com [10.36.113.245]) by smtp.corp.redhat.com (Postfix) with ESMTP id 29CAA60BEC; Thu, 7 May 2020 14:02:21 +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 , Pankaj Gupta , Jason Wang , Oscar Salvador , Igor Mammedov , Dave Young , Dan Williams , Pavel Tatashin , Stefan Hajnoczi , Vlastimil Babka Subject: [PATCH v4 04/15] virtio-mem: Paravirtualized memory hotunplug part 1 Date: Thu, 7 May 2020 16:01:28 +0200 Message-Id: <20200507140139.17083-5-david@redhat.com> In-Reply-To: <20200507140139.17083-1-david@redhat.com> References: <20200507140139.17083-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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 activity. Tested-by: Pankaj Gupta 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 | 116 +++++++++++++++++++++++++++++++++++- 1 file changed, 114 insertions(+), 2 deletions(-) diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c index 270ddeaec059..a3ec795be8be 100644 --- a/drivers/virtio/virtio_mem.c +++ b/drivers/virtio/virtio_mem.c @@ -123,7 +123,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; @@ -280,6 +280,12 @@ static int virtio_mem_mb_state_prepare_next_mb(struct virtio_mem *vm) _mb_id++) \ if (virtio_mem_mb_get_state(_vm, _mb_id) == _state) +#define virtio_mem_for_each_mb_state_rev(_vm, _mb_id, _state) \ + for (_mb_id = _vm->next_mb_id - 1; \ + _mb_id >= _vm->first_mb_id && _vm->nb_mb_state[_state]; \ + _mb_id--) \ + if (virtio_mem_mb_get_state(_vm, _mb_id) == _state) + /* * Mark all selected subblocks plugged. * @@ -325,6 +331,19 @@ static bool virtio_mem_mb_test_sb_plugged(struct virtio_mem *vm, bit + count; } +/* + * 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 = (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) >= bit + count; +} + /* * Find the first plugged subblock. Returns vm->nb_sb_per_mb in case there is * none. @@ -513,6 +532,9 @@ static void virtio_mem_notify_offline(struct virtio_mem *vm, BUG(); break; } + + /* trigger the workqueue, maybe we can now unplug memory. */ + virtio_mem_retry(vm); } static void virtio_mem_notify_online(struct virtio_mem *vm, unsigned long mb_id, @@ -1122,6 +1144,94 @@ static int virtio_mem_plug_request(struct virtio_mem *vm, uint64_t diff) return rc; } +/* + * Unplug the desired number of plugged subblocks of an offline memory block. + * Will fail if any subblock cannot get unplugged (instead of skipping it). + * + * 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 = 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 = 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 diff) +{ + uint64_t nb_sb = 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_rev(vm, mb_id, + VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL) { + rc = 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_rev(vm, mb_id, + VIRTIO_MEM_MB_STATE_OFFLINE) { + rc = 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 example, * because the hypervisor was busy. @@ -1204,8 +1314,10 @@ static void virtio_mem_run_wq(struct work_struct *work) if (vm->requested_size > vm->plugged_size) { diff = vm->requested_size - vm->plugged_size; rc = virtio_mem_plug_request(vm, diff); + } else { + diff = vm->plugged_size - vm->requested_size; + rc = virtio_mem_unplug_request(vm, diff); } - /* TODO: try to unplug memory */ } switch (rc) { -- 2.25.3