From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5EF95C5ACBF for ; Wed, 11 Mar 2020 17:15:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id AF52120763 for ; Wed, 11 Mar 2020 17:15:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="OTatO4vR" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730490AbgCKRPt (ORCPT ); Wed, 11 Mar 2020 13:15:49 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:54194 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730068AbgCKRPt (ORCPT ); Wed, 11 Mar 2020 13:15:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1583946947; 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=2Il+nsakWqayV1I872rbc5vd5eg8q9WbG05kQyarYxI=; b=OTatO4vR7YZb/3IrufIT99nKMXXSowWGyLMKwTCo3drlwRaELXO096I+MbOtxlp0GHevoo MvZkti9z3+KNqCt/fBSKobcXg0/ofL8FC8sr3Y9nKlJWPJqUTl/UqxtXFARk9MgBBNEnhz +5A21IO8ppxoU2xLJDfLcPkkiBuwVgA= 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-89-IF_SqGinNeSkwy3ug9D2iw-1; Wed, 11 Mar 2020 13:15:43 -0400 X-MC-Unique: IF_SqGinNeSkwy3ug9D2iw-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 D83CEDBA5; Wed, 11 Mar 2020 17:15:41 +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 9EB5392D32; Wed, 11 Mar 2020 17:15:38 +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 v2 03/10] virtio-mem: Paravirtualized memory hotunplug part 1 Date: Wed, 11 Mar 2020 18:14:15 +0100 Message-Id: <20200311171422.10484-4-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 Unplugging subblocks of memory blocks that are offline is easy. All we have to do is watch out for concurrent onlining activity. 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 d8da656c9145..c1fc7f9c4acf 100644 --- a/drivers/virtio/virtio_mem.c +++ b/drivers/virtio/virtio_mem.c @@ -120,7 +120,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; @@ -277,6 +277,12 @@ static int virtio_mem_mb_state_prepare_next_mb(struc= t virtio_mem *vm) _mb_id++) \ if (virtio_mem_mb_get_state(_vm, _mb_id) =3D=3D _state) =20 +#define virtio_mem_for_each_mb_state_rev(_vm, _mb_id, _state) \ + for (_mb_id =3D _vm->next_mb_id - 1; \ + _mb_id >=3D _vm->first_mb_id && _vm->nb_mb_state[_state]; \ + _mb_id--) \ + if (virtio_mem_mb_get_state(_vm, _mb_id) =3D=3D _state) + /* * Mark all selected subblocks plugged. * @@ -322,6 +328,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 +530,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, @@ -1120,6 +1142,94 @@ 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_rev(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_rev(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. @@ -1202,8 +1312,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.24.1 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: virtio-dev-return-6924-cohuck=redhat.com@lists.oasis-open.org Sender: List-Post: List-Help: List-Unsubscribe: List-Subscribe: Received: from lists.oasis-open.org (oasis-open.org [10.110.1.242]) by lists.oasis-open.org (Postfix) with ESMTP id 7568D985FE4 for ; Wed, 11 Mar 2020 17:15:48 +0000 (UTC) From: David Hildenbrand Date: Wed, 11 Mar 2020 18:14:15 +0100 Message-Id: <20200311171422.10484-4-david@redhat.com> In-Reply-To: <20200311171422.10484-1-david@redhat.com> References: <20200311171422.10484-1-david@redhat.com> MIME-Version: 1.0 Subject: [virtio-dev] [PATCH v2 03/10] virtio-mem: Paravirtualized memory hotunplug part 1 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable 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 List-ID: Unplugging subblocks of memory blocks that are offline is easy. All we have to do is watch out for concurrent onlining activity. 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 d8da656c9145..c1fc7f9c4acf 100644 --- a/drivers/virtio/virtio_mem.c +++ b/drivers/virtio/virtio_mem.c @@ -120,7 +120,7 @@ struct virtio_mem { =09 * =09 * When this lock is held the pointers can't change, ONLINE and =09 * OFFLINE blocks can't change the state and no subblocks will get -=09 * plugged. +=09 * plugged/unplugged. =09 */ =09struct mutex hotplug_mutex; =09bool hotplug_active; @@ -277,6 +277,12 @@ static int virtio_mem_mb_state_prepare_next_mb(struct = virtio_mem *vm) =09 _mb_id++) \ =09=09if (virtio_mem_mb_get_state(_vm, _mb_id) =3D=3D _state) =20 +#define virtio_mem_for_each_mb_state_rev(_vm, _mb_id, _state) \ +=09for (_mb_id =3D _vm->next_mb_id - 1; \ +=09 _mb_id >=3D _vm->first_mb_id && _vm->nb_mb_state[_state]; \ +=09 _mb_id--) \ +=09=09if (virtio_mem_mb_get_state(_vm, _mb_id) =3D=3D _state) + /* * Mark all selected subblocks plugged. * @@ -322,6 +328,19 @@ static bool virtio_mem_mb_test_sb_plugged(struct virti= o_mem *vm, =09 bit + count; } =20 +/* + * Test if all selected subblocks are unplugged. + */ +static bool virtio_mem_mb_test_sb_unplugged(struct virtio_mem *vm, +=09=09=09=09=09 unsigned long mb_id, int sb_id, +=09=09=09=09=09 int count) +{ +=09const int bit =3D (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb + sb_id; + +=09/* TODO: Helper similar to bitmap_set() */ +=09return 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 there= is * none. @@ -511,6 +530,9 @@ static void virtio_mem_notify_offline(struct virtio_mem= *vm, =09=09BUG(); =09=09break; =09} + +=09/* trigger the workqueue, maybe we can now unplug memory. */ +=09virtio_mem_retry(vm); } =20 static void virtio_mem_notify_online(struct virtio_mem *vm, unsigned long = mb_id, @@ -1120,6 +1142,94 @@ static int virtio_mem_plug_request(struct virtio_mem= *vm, uint64_t diff) =09return rc; } =20 +/* + * Unplug the desired number of plugged subblocks of an offline memory blo= ck. + * 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, +=09=09=09=09=09 unsigned long mb_id, +=09=09=09=09=09 uint64_t *nb_sb) +{ +=09int rc; + +=09rc =3D virtio_mem_mb_unplug_any_sb(vm, mb_id, nb_sb); + +=09/* some subblocks might have been unplugged even on failure */ +=09if (!virtio_mem_mb_test_sb_plugged(vm, mb_id, 0, vm->nb_sb_per_mb)) +=09=09virtio_mem_mb_set_state(vm, mb_id, +=09=09=09=09=09VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL); +=09if (rc) +=09=09return rc; + +=09if (virtio_mem_mb_test_sb_unplugged(vm, mb_id, 0, vm->nb_sb_per_mb)) { +=09=09/* +=09=09 * Remove the block from Linux - this should never fail. +=09=09 * Hinder the block from getting onlined by marking it +=09=09 * unplugged. Temporarily drop the mutex, so +=09=09 * any pending GOING_ONLINE requests can be serviced/rejected. +=09=09 */ +=09=09virtio_mem_mb_set_state(vm, mb_id, +=09=09=09=09=09VIRTIO_MEM_MB_STATE_UNUSED); + +=09=09mutex_unlock(&vm->hotplug_mutex); +=09=09rc =3D virtio_mem_mb_remove(vm, mb_id); +=09=09BUG_ON(rc); +=09=09mutex_lock(&vm->hotplug_mutex); +=09} +=09return 0; +} + +/* + * Try to unplug the requested amount of memory. + */ +static int virtio_mem_unplug_request(struct virtio_mem *vm, uint64_t diff) +{ +=09uint64_t nb_sb =3D diff / vm->subblock_size; +=09unsigned long mb_id; +=09int rc; + +=09if (!nb_sb) +=09=09return 0; + +=09/* +=09 * We'll drop the mutex a couple of times when it is safe to do so. +=09 * This might result in some blocks switching the state (online/offline= ) +=09 * and we could miss them in this run - we will retry again later. +=09 */ +=09mutex_lock(&vm->hotplug_mutex); + +=09/* Try to unplug subblocks of partially plugged offline blocks. */ +=09virtio_mem_for_each_mb_state_rev(vm, mb_id, +=09=09=09=09=09 VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL) { +=09=09rc =3D virtio_mem_mb_unplug_any_sb_offline(vm, mb_id, +=09=09=09=09=09=09=09 &nb_sb); +=09=09if (rc || !nb_sb) +=09=09=09goto out_unlock; +=09=09cond_resched(); +=09} + +=09/* Try to unplug subblocks of plugged offline blocks. */ +=09virtio_mem_for_each_mb_state_rev(vm, mb_id, +=09=09=09=09=09 VIRTIO_MEM_MB_STATE_OFFLINE) { +=09=09rc =3D virtio_mem_mb_unplug_any_sb_offline(vm, mb_id, +=09=09=09=09=09=09=09 &nb_sb); +=09=09if (rc || !nb_sb) +=09=09=09goto out_unlock; +=09=09cond_resched(); +=09} + +=09mutex_unlock(&vm->hotplug_mutex); +=09return 0; +out_unlock: +=09mutex_unlock(&vm->hotplug_mutex); +=09return rc; +} + /* * Try to unplug all blocks that couldn't be unplugged before, for example= , * because the hypervisor was busy. @@ -1202,8 +1312,10 @@ static void virtio_mem_run_wq(struct work_struct *wo= rk) =09=09if (vm->requested_size > vm->plugged_size) { =09=09=09diff =3D vm->requested_size - vm->plugged_size; =09=09=09rc =3D virtio_mem_plug_request(vm, diff); +=09=09} else { +=09=09=09diff =3D vm->plugged_size - vm->requested_size; +=09=09=09rc =3D virtio_mem_unplug_request(vm, diff); =09=09} -=09=09/* TODO: try to unplug memory */ =09} =20 =09switch (rc) { --=20 2.24.1 --------------------------------------------------------------------- To unsubscribe, e-mail: virtio-dev-unsubscribe@lists.oasis-open.org For additional commands, e-mail: virtio-dev-help@lists.oasis-open.org