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=-16.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=ham 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 08E32C433EF for ; Fri, 3 Sep 2021 07:02:18 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8E3AF610A2 for ; Fri, 3 Sep 2021 07:02:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 8E3AF610A2 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id DBA198D0002; Fri, 3 Sep 2021 03:02:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D6A148D0001; Fri, 3 Sep 2021 03:02:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C592D8D0002; Fri, 3 Sep 2021 03:02:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0167.hostedemail.com [216.40.44.167]) by kanga.kvack.org (Postfix) with ESMTP id B4EBF8D0001 for ; Fri, 3 Sep 2021 03:02:16 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 14A91184B9569 for ; Fri, 3 Sep 2021 07:02:16 +0000 (UTC) X-FDA: 78545368272.20.C7B7E8C Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf25.hostedemail.com (Postfix) with ESMTP id 9E6A8B000180 for ; Fri, 3 Sep 2021 07:02:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630652535; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=07FgH7wT67po2w8bQGIw93/1x6mvSaBB2yNe5PGszL8=; b=HUthyU0Ca2JWdtgeUVicykfx3m7GAKqvuHJz3uvRCjEOEzdGgJ4Yhzeu9TBHigNiOAGiWF zt41OdstleHjbl+a1wabJmNDhAUtY05BP/8ED9GONwJ2wW2q7Ks1pDaMpXUeDGVzARMZ2k FZlwVV4b/oMl6hpvv5hW0NN9n4+T1f0= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-206-hZREEX3CMnGE0pwn9QL7gA-1; Fri, 03 Sep 2021 03:02:11 -0400 X-MC-Unique: hZREEX3CMnGE0pwn9QL7gA-1 Received: by mail-wr1-f69.google.com with SMTP id p18-20020a5d4e12000000b0015940dc586aso1266421wrt.6 for ; Fri, 03 Sep 2021 00:02:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=07FgH7wT67po2w8bQGIw93/1x6mvSaBB2yNe5PGszL8=; b=iX1YIW+lNEuv4TN5T1CKAyCRYdpZ4KeRTBLR6rKOd5xK72pvoPruhqmRTuQMqXbwRd DIdO+28wCVwWidi8wFChFZrqkX8NffI9MAsEk87IYSgQtOKREBAmDzLdGSwmnhuTEXAm 6fKG8NJHAPopuw/1AsFwbMxg3nDNV9r2kokJMi5IsdfJWJsrs75+vHorAeqLQkFH/kJO GbfO4zD7l85l/JfidbFPbQJ/RzTObWtKpBmb67NrbBQOHMYNpgi4dez8xGQOcL6VIcxD JlUaRTfqVP6hYhjWJ+nIbwb6QPZO4IaE0jZIc7MHbL7Rqkylt7OEzMePi/nVQDzr1mWi c7Ng== X-Gm-Message-State: AOAM5339140yM+vV2BTh2rV212Ngvix3AzdR35Sl10T8skpXP+Tieqx7 MrraF5Zy+EgRDJyXi/wrmLGLohiVNmsN564+EYl4+B0YG17qem7sxFmJR5uyKmElEAAl1F0qf8I F/295nieOkY4= X-Received: by 2002:a5d:51c6:: with SMTP id n6mr2223517wrv.402.1630652530715; Fri, 03 Sep 2021 00:02:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJydsWfnmMs9uEFqCpYm22oyb0W/h3tDknscsNUxRfbHoKaj/53aSdqrSr8X//PvyYQLUFL9pw== X-Received: by 2002:a5d:51c6:: with SMTP id n6mr2223485wrv.402.1630652530498; Fri, 03 Sep 2021 00:02:10 -0700 (PDT) Received: from redhat.com ([2a03:c5c0:207f:7f47:ccd3:7600:6a2d:c5a]) by smtp.gmail.com with ESMTPSA id x9sm3336534wmi.30.2021.09.03.00.02.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 00:02:08 -0700 (PDT) Date: Fri, 3 Sep 2021 03:02:04 -0400 From: "Michael S. Tsirkin" To: David Hildenbrand Cc: linux-kernel@vger.kernel.org, Arnd Bergmann , Greg Kroah-Hartman , Jason Wang , "Rafael J. Wysocki" , Andrew Morton , Dan Williams , Hanjun Guo , Andy Shevchenko , virtualization@lists.linux-foundation.org, linux-mm@kvack.org Subject: Re: [PATCH v4 3/3] virtio-mem: disallow mapping virtio-mem memory via /dev/mem Message-ID: <20210903025630-mutt-send-email-mst@kernel.org> References: <20210902160919.25683-1-david@redhat.com> <20210902160919.25683-4-david@redhat.com> MIME-Version: 1.0 In-Reply-To: <20210902160919.25683-4-david@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=HUthyU0C; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf25.hostedemail.com: domain of mst@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=mst@redhat.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 9E6A8B000180 X-Stat-Signature: pj7exed81zeb1xk6onxrnuizo31m1zm4 X-HE-Tag: 1630652535-982492 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, Sep 02, 2021 at 06:09:19PM +0200, David Hildenbrand wrote: > We don't want user space to be able to map virtio-mem device memory > directly (e.g., via /dev/mem) in order to have guarantees that in a sane > setup we'll never accidentially access unplugged memory within the > device-managed region of a virtio-mem device, just as required by the > virtio-spec. > > As soon as the virtio-mem driver is loaded, the device region is visible > in /proc/iomem via the parent device region. From that point on user space > is aware of the device region and we want to disallow mapping anything > inside that region (where we will dynamically (un)plug memory) until > the driver has been unloaded cleanly and e.g., another driver might take > over. > > By creating our parent IORESOURCE_SYSTEM_RAM resource with > IORESOURCE_EXCLUSIVE, we will disallow any /dev/mem access to our > device region until the driver was unloaded cleanly and removed the > parent region. This will work even though only some memory blocks are > actually currently added to Linux and appear as busy in the resource tree. > > So access to the region from user space is only possible > a) if we don't load the virtio-mem driver. > b) after unloading the virtio-mem driver cleanly. > > Don't build virtio-mem if access to /dev/mem cannot be restricticted -- > if we have CONFIG_DEVMEM=y but CONFIG_STRICT_DEVMEM is not set. > > Reviewed-by: Dan Williams > Signed-off-by: David Hildenbrand > --- > drivers/virtio/Kconfig | 1 + > drivers/virtio/virtio_mem.c | 4 +++- > 2 files changed, 4 insertions(+), 1 deletion(-) > > diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig > index ce1b3f6ec325..ff80cd03f1d1 100644 > --- a/drivers/virtio/Kconfig > +++ b/drivers/virtio/Kconfig > @@ -101,6 +101,7 @@ config VIRTIO_MEM > depends on MEMORY_HOTPLUG_SPARSE > depends on MEMORY_HOTREMOVE > depends on CONTIG_ALLOC > + depends on !DEVMEM || STRICT_DEVMEM > help > This driver provides access to virtio-mem paravirtualized memory > devices, allowing to hotplug and hotunplug memory. It would be nicer if there was a symbol in the MEMORY_ namespace we can depend on exported by mm and depending on !DEVMEM || STRICT_DEVMEM. E.g. config MEMORY_EXCLUSIVE def_bool y depends on !DEVMEM || STRICT_DEVMEM and then in virtio depends on MEMORY_EXCLUSIVE the virtio change itself is ok though: Acked-by: Michael S. Tsirkin for merging through -mm. > diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c > index b91bc810a87e..c2d93492cf0f 100644 > --- a/drivers/virtio/virtio_mem.c > +++ b/drivers/virtio/virtio_mem.c > @@ -2523,8 +2523,10 @@ static int virtio_mem_create_resource(struct virtio_mem *vm) > if (!name) > return -ENOMEM; > > + /* Disallow mapping device memory via /dev/mem completely. */ > vm->parent_resource = __request_mem_region(vm->addr, vm->region_size, > - name, IORESOURCE_SYSTEM_RAM); > + name, IORESOURCE_SYSTEM_RAM | > + IORESOURCE_EXCLUSIVE); > if (!vm->parent_resource) { > kfree(name); > dev_warn(&vm->vdev->dev, "could not reserve device region\n"); > -- > 2.31.1