Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp612759ybj; Thu, 7 May 2020 03:36:07 -0700 (PDT) X-Google-Smtp-Source: APiQypLUaxv2Px5aQSFzuViCHHh1vvbc0Ml66wUOeNzqaqzjJjDrqJ07iu9kUkxloicCHW2be7k2 X-Received: by 2002:a17:906:534b:: with SMTP id j11mr11045359ejo.142.1588847766957; Thu, 07 May 2020 03:36:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588847766; cv=none; d=google.com; s=arc-20160816; b=X9zl9WohL6Uc83idra4dGXX5xm7XaLMhT1MkKB6kngTGcev1s9DCq/NWeBtAA1PJMM V/PCNaNKo5y+Uk5h/h3gnSUCOKz+KBTQt8h09l2rkWbkmdTHKiXt8TQeXRVjLk/5Hmd7 akocwZ0Lk2aV3Lb01MjtUCt5QmF/wjfWM5WZ5zW7oTS4YQa+9lyrgXJ2wpwStnE6432T UKDcj+/AQvfx7Ukpcyrva7wIB4vDD0Jus6lokSc9JmyVS7P10/zQrvxXXxFn4ju9izUG BtVD1Gfu0LegudemOeMPQfkUY0ALY928E9DMh9qSb0+yCTuatp3jaI2YtjuYlrWu9g9M 4LVA== 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=I34kuI9zP9SIjkHokU9MyLfMs/Qz8/WCUk75Q38T3ns=; b=UIylgY026THNvNhOiTDaKiRc3WH2NAje0kY1tAfo7vauvPTxmoNcUpOmVD0Km/KqCH myZEPw3eIMf3ggdIuEMoYSomY2EqLCgMW5jVhOMQdPKTX0eCChNHNnT598svtDtFfYma wBH7l4oAits73kqtL+OEpNLLxE0B55fpy2xJAfv9m2q8QJT5ZKdkMPdkdyoaq+S5yj8m k+v8MFD8SvV/zXN09QzggTYAlqdwWg0cv5KpiCgfqnPmvozxZ8PwUHEoK4/BCDUlbTBv zqeFyrCbDdEKp/D3VqGYBGum1sM2EnAWTSJKt9337PFXQDiiYWSdxPuFlbPql8UvVw4r ijpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=g6RnKtLj; 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 a19si2830457edr.254.2020.05.07.03.35.43; Thu, 07 May 2020 03:36:06 -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=g6RnKtLj; 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 S1727824AbgEGKdc (ORCPT + 99 others); Thu, 7 May 2020 06:33:32 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:56605 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727808AbgEGKd3 (ORCPT ); Thu, 7 May 2020 06:33:29 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588847608; 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=I34kuI9zP9SIjkHokU9MyLfMs/Qz8/WCUk75Q38T3ns=; b=g6RnKtLjD5w97GvqRCM2jaoBmWOxPzbXwiF1BeoH6bEhrf5LzNLkd7JCdM/Dcg9lnNTCk0 oYfkM5XGSwkMd3aomykNlGgr+Vn5tJsdhyl2uDy//qTVMTr4lAcLjZOGNeLYqcwqGTDV/m McG2JUKMSathXFWe7gTFo/XAF16d9dw= 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-297-mTnmAXzkOxq8aWqGcm09yg-1; Thu, 07 May 2020 06:33:26 -0400 X-MC-Unique: mTnmAXzkOxq8aWqGcm09yg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 40967107ACCA; Thu, 7 May 2020 10:33:25 +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 3DD5F5D9C5; Thu, 7 May 2020 10:33:23 +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 Subject: [PATCH v3 11/15] virtio-mem: Add parent resource for all added "System RAM" Date: Thu, 7 May 2020 12:31:15 +0200 Message-Id: <20200507103119.11219-12-david@redhat.com> In-Reply-To: <20200507103119.11219-1-david@redhat.com> References: <20200507103119.11219-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 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 add a parent resource, named after the virtio device (inspired by drivers/dax/kmem.c). This allows user space to identify which memory belongs to which virtio-mem device. With this change and two virtio-mem devices: :/# cat /proc/iomem 00000000-00000fff : Reserved 00001000-0009fbff : System RAM [...] 140000000-333ffffff : virtio0 140000000-147ffffff : System RAM 148000000-14fffffff : System RAM 150000000-157ffffff : System RAM [...] 334000000-3033ffffff : virtio1 338000000-33fffffff : System RAM 340000000-347ffffff : System RAM 348000000-34fffffff : System RAM [...] Cc: "Michael S. Tsirkin" Cc: Pankaj Gupta Signed-off-by: David Hildenbrand --- drivers/virtio/virtio_mem.c | 52 ++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c index eb4c16d634e0..80cdb9e6b3c4 100644 --- a/drivers/virtio/virtio_mem.c +++ b/drivers/virtio/virtio_mem.c @@ -99,6 +99,9 @@ struct virtio_mem { /* Id of the next memory bock to prepare when needed. */ unsigned long next_mb_id; =20 + /* The parent resource for all memory added via this device. */ + struct resource *parent_resource; + /* Summary of all memory block states. */ unsigned long nb_mb_state[VIRTIO_MEM_MB_STATE_COUNT]; #define VIRTIO_MEM_NB_OFFLINE_THRESHOLD 10 @@ -1741,6 +1744,44 @@ static int virtio_mem_init(struct virtio_mem *vm) return 0; } =20 +static int virtio_mem_create_resource(struct virtio_mem *vm) +{ + /* + * When force-unloading the driver and removing the device, we + * could have a garbage pointer. Duplicate the string. + */ + const char *name =3D kstrdup(dev_name(&vm->vdev->dev), GFP_KERNEL); + + if (!name) + return -ENOMEM; + + vm->parent_resource =3D __request_mem_region(vm->addr, vm->region_size, + name, IORESOURCE_SYSTEM_RAM); + if (!vm->parent_resource) { + kfree(name); + dev_warn(&vm->vdev->dev, "could not reserve device region\n"); + return -EBUSY; + } + + /* The memory is not actually busy - make add_memory() work. */ + vm->parent_resource->flags &=3D ~IORESOURCE_BUSY; + return 0; +} + +static void virtio_mem_delete_resource(struct virtio_mem *vm) +{ + const char *name; + + if (!vm->parent_resource) + return; + + name =3D vm->parent_resource->name; + release_resource(vm->parent_resource); + kfree(vm->parent_resource); + kfree(name); + vm->parent_resource =3D NULL; +} + static int virtio_mem_probe(struct virtio_device *vdev) { struct virtio_mem *vm; @@ -1770,11 +1811,16 @@ static int virtio_mem_probe(struct virtio_device = *vdev) if (rc) goto out_del_vq; =20 + /* create the parent resource for all memory */ + rc =3D virtio_mem_create_resource(vm); + if (rc) + goto out_del_vq; + /* register callbacks */ vm->memory_notifier.notifier_call =3D virtio_mem_memory_notifier_cb; rc =3D register_memory_notifier(&vm->memory_notifier); if (rc) - goto out_del_vq; + goto out_del_resource; rc =3D register_virtio_mem_device(vm); if (rc) goto out_unreg_mem; @@ -1788,6 +1834,8 @@ static int virtio_mem_probe(struct virtio_device *v= dev) return 0; out_unreg_mem: unregister_memory_notifier(&vm->memory_notifier); +out_del_resource: + virtio_mem_delete_resource(vm); out_del_vq: vdev->config->del_vqs(vdev); out_free_vm: @@ -1848,6 +1896,8 @@ static void virtio_mem_remove(struct virtio_device = *vdev) vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL] || vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_MOVABLE]) dev_warn(&vdev->dev, "device still has system memory added\n"); + else + virtio_mem_delete_resource(vm); =20 /* remove all tracking data - no locking needed */ vfree(vm->mb_state); --=20 2.25.3