Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp761653ybj; Thu, 7 May 2020 07:06:46 -0700 (PDT) X-Google-Smtp-Source: APiQypKdvTmilLrzQxZ6o5Lb+eR+953I9xcHyJ2HSnoZUl/PATWRxmktGIjZWqbNZCULWhBVQT1z X-Received: by 2002:a17:906:30c3:: with SMTP id b3mr12469280ejb.126.1588860406679; Thu, 07 May 2020 07:06:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588860406; cv=none; d=google.com; s=arc-20160816; b=CxsvKSvSi389A0IiaZZ9JwxHSY7MCM/GgUugfGvOi5lue8+gxJz/hHhgPfIqmFi7Zi DvF/hUvpEFvZZYbV2V+2Q97/7t0ZEG2YUbRSFXLOM13LWPtsFKX5VQy1rhkhHOWiSv7l b0Td5zLUaZEsBIujvu4k2x69EDj8imkJ5ZR/SvCvOqQ+WvDIe4rf4qis8G/TEAR/tztX pWYumnV0L+5Xfl04+I+ePaC2GLA0gtRFC4tVO5geY4Z0/8YtWoKoh/0oPePzRdXbUZLn JcizF8gkXapqDjNnw7HiG+pVDOlQVUlwGrha8qGsXJj9TFvFT09g3xh4LAZsva/upPJD MiQA== 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=8pO3JHY1hAu4Su9QfIAK9KliBSj8I09PIxlH0fBNT4A=; b=b2MHnfDdCJD4d1hTPFkNPdAJFHeSjdA9zRF/DUiUuHrHJTGjOZXIqbtOc72smmihe+ RTs0Zl/ivRnksah//xyDxTKx4FM1MSjvpsNhMqbgHDnddbsoE5bysoQn/DweMc/q29xE bJ4H//5lCvMppK7VDTW6TlohWsevfV5yWgwN6sF7oyS5a/CwlExOvKkZcgVLumHLb2/T jiC6R+EtRawqAaJh7BV6raAW+gxtxn3+6UFwuXV07jZe4Ryl6S+UhWGoIGocDA8Uan3K X1xx5SW7c6I4gp3UVK278e8B34N+81tjiP7NEcIbgWYGc+GN9khbQGz4/CaqflVuXQN6 V2zQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iV8HkdBM; 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 d4si3131795edp.87.2020.05.07.07.06.20; Thu, 07 May 2020 07:06:46 -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=iV8HkdBM; 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 S1727840AbgEGODc (ORCPT + 99 others); Thu, 7 May 2020 10:03:32 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:22804 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727104AbgEGODc (ORCPT ); Thu, 7 May 2020 10:03:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588860208; 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=8pO3JHY1hAu4Su9QfIAK9KliBSj8I09PIxlH0fBNT4A=; b=iV8HkdBMscb/qZ2B3ahRUj+w5mECpqDPbUF6WZktjW1WdWL/FloK4jy+N2cAejjxkV//r/ 3MmO7cbxAF43/Kk75gW/Yun2qRQmN4tYa2Kewr+3DxAL5riSttmhrg+P4OuvJi2Z49X1B2 dgCIybsxRIACByKam3Ydgl0C0YrLuaY= 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-55-J5nlHLpzNAuk756jZsup_A-1; Thu, 07 May 2020 10:03:22 -0400 X-MC-Unique: J5nlHLpzNAuk756jZsup_A-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 295A6835B43; Thu, 7 May 2020 14:03:20 +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 232186109E; Thu, 7 May 2020 14:03: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 , Pankaj Gupta , Jason Wang , Oscar Salvador , Igor Mammedov , Dave Young , Dan Williams , Pavel Tatashin , Stefan Hajnoczi , Vlastimil Babka Subject: [PATCH v4 09/15] virtio-mem: Offline and remove completely unplugged memory blocks Date: Thu, 7 May 2020 16:01:33 +0200 Message-Id: <20200507140139.17083-10-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 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). Acked-by: Pankaj Gupta 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 | 47 +++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c index b0b41c73ce89..a2edb87e5ed8 100644 --- a/drivers/virtio/virtio_mem.c +++ b/drivers/virtio/virtio_mem.c @@ -446,6 +446,28 @@ static int virtio_mem_mb_remove(struct virtio_mem *vm, unsigned long mb_id) return remove_memory(nid, addr, memory_block_size_bytes()); } +/* + * 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 = virtio_mem_mb_id_to_phys(mb_id); + int nid = vm->nid; + + if (nid == NUMA_NO_NODE) + nid = 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. */ @@ -537,7 +559,13 @@ static void virtio_mem_notify_offline(struct virtio_mem *vm, break; } - /* 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); } @@ -1284,7 +1312,8 @@ static int virtio_mem_mb_unplug_any_sb_offline(struct virtio_mem *vm, * Unplug the desired number of plugged subblocks of an online memory block. * 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 unplugged. @@ -1340,9 +1369,19 @@ static int virtio_mem_mb_unplug_any_sb_online(struct virtio_mem *vm, } /* - * 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 = 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; } -- 2.25.3