Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp4346128pxa; Mon, 10 Aug 2020 07:06:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwzP8Xx2/lFZ63A3l1+8wT2dok4AovahnBhyJgiJQVkojKc7HvgyYqNEwtrBSiLb7bdowH8 X-Received: by 2002:a17:906:2490:: with SMTP id e16mr21376670ejb.386.1597068397245; Mon, 10 Aug 2020 07:06:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597068397; cv=none; d=google.com; s=arc-20160816; b=akQiddYfFhnXq3CqHmd0vZDrD21BwfsG9mOIIIqjmPFR2jnovlyACzBbFJfFSKEh+W RyDI9YovBPGq5f2rsZUeVgKSIzesumSfN3SreF2xdnhPiLW9L0s8ylbSYoH5rI6SLpg3 D2voP1cVGKtpkK0r9FAZYnybTVvyWx1+fPS4n6AY8KafRZjqh2d5YAoPOa0lAXDSvZcb fqmmL7p9HLMJOHjxhCVuz/YTvYv9cKun2JkCmJMUfs5WXljJLtv9md+DAGyz2nUP07YA XUs2kOagZk4TeGqGrwWNHWiSGWDXvReGGfsovCRnM642/Up9k/m4bzTND9wJp7LOZ+cx raCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=ye/STDc8dVw5OUpY6piPnmPc1y/hoA9IwVw3n1eaj/I=; b=0XqPHa+Y2b/UJZ2LA8bAeGWbcICe8qzU8qv7JTRmnyCqho2rPF9H5ozHFJQNmzB4l3 aTrBNQlwSP7x4TPD9PY3WnW+phYG/N0ZkGMAo860w0hV6cKN3HFX9EU3QcuKq+eLt13W t5OQI8ZRUYA9mJ6pnLxY7wmmKYipjWn2fQZ4gBEP5Sn1f1CLPjr0xAgcwyey6/5L9jKs Y6GJLu0HS0CcvbnbGfHj4+spBsbjYtA+aqSEGkSX5V7T6OMSvgD6m4CAJDM4H4Tx1Va6 RAXgDD8Aetnc3gdkSiaxJ16MIh2FkiMrhejERI0Kawu1P4yFcEyVq0N0KMTcuInPQxol NTUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ND7pd+We; 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 h21si10284376edw.233.2020.08.10.07.06.14; Mon, 10 Aug 2020 07:06:37 -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=ND7pd+We; 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 S1726949AbgHJOFd (ORCPT + 99 others); Mon, 10 Aug 2020 10:05:33 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:26355 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726888AbgHJOF2 (ORCPT ); Mon, 10 Aug 2020 10:05:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1597068326; 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=ye/STDc8dVw5OUpY6piPnmPc1y/hoA9IwVw3n1eaj/I=; b=ND7pd+WeH//mBFyCYUZtTdc/IWAhBIV0EA1PyEEKdE7rrfNnK5wSNQOfQz0U8n900gOTxb +aWJ+m9x75FQO4J8W9QOiOJyeCA/Tb18f3iO/azp5v8bPmV53++4spQPRMXwELn2UyOnJH zRA3S2Mr0ECoBg2d6Lwg/LuYfKVkGL0= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-195-U4D3LQpdOBm8KmeZQGpZgQ-1; Mon, 10 Aug 2020 10:05:22 -0400 X-MC-Unique: U4D3LQpdOBm8KmeZQGpZgQ-1 Received: by mail-wr1-f71.google.com with SMTP id w7so4250297wre.11 for ; Mon, 10 Aug 2020 07:05:22 -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=ye/STDc8dVw5OUpY6piPnmPc1y/hoA9IwVw3n1eaj/I=; b=Qj5FZgvQjhBZYZ4/lRjdxZGDFV7nuyEPnBibF3LHmAIoPRPaJQTk2Sz5RHyYc0ftM5 TyOONhhr5m1CWsby4N8tz3m/t0OoiczAxwMdnDz0vEWkIi99Ng3PkN0TuH0JHZda2uTU O3kdMcjIsCcUi8xupLNhLaIPvjkLvzJNs51fI9qCWIOobnsDl+A44SmefRZiqb/VaOLW hDw6W9CpwTUoziyIl6qYPSyheb1+w//sjnd6dXqCc5RhfXESWFo1p8w07TKipZjCkDpB hq1yMN+6q0ZijEjh/HlSiEzjPZb5sv+6HNYDulG7YafxNfPS2DDZxHttycfE/vjAwNW5 Op+A== X-Gm-Message-State: AOAM532QCLxctvQVd/z1rLRyYClbm6iDqkFUuUsuyTV+20WXr+7ndIvF 1bIIMxycQ3ugfbo4PEsKZ6xhHnzvRbMctICcTb8Mx6A3pdCrwLW1g6NO6EOabwepRsRrKpY/ber vUjH2K+6R9X31ITY/bl1DG4pB X-Received: by 2002:a1c:c1:: with SMTP id 184mr27136033wma.105.1597068321550; Mon, 10 Aug 2020 07:05:21 -0700 (PDT) X-Received: by 2002:a1c:c1:: with SMTP id 184mr27136011wma.105.1597068321319; Mon, 10 Aug 2020 07:05:21 -0700 (PDT) Received: from redhat.com (bzq-79-180-0-181.red.bezeqint.net. [79.180.0.181]) by smtp.gmail.com with ESMTPSA id r3sm21185627wro.1.2020.08.10.07.05.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Aug 2020 07:05:20 -0700 (PDT) Date: Mon, 10 Aug 2020 10:05:17 -0400 From: "Michael S. Tsirkin" To: Vivek Goyal Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, virtio-fs@redhat.com, miklos@szeredi.hu, stefanha@redhat.com, dgilbert@redhat.com, Sebastien Boeuf , kbuild test robot , kvm@vger.kernel.org Subject: Re: [PATCH v2 04/20] virtio: Implement get_shm_region for PCI transport Message-ID: <20200810100327-mutt-send-email-mst@kernel.org> References: <20200807195526.426056-1-vgoyal@redhat.com> <20200807195526.426056-5-vgoyal@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200807195526.426056-5-vgoyal@redhat.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Aug 07, 2020 at 03:55:10PM -0400, Vivek Goyal wrote: > From: Sebastien Boeuf > > On PCI the shm regions are found using capability entries; > find a region by searching for the capability. > > Signed-off-by: Sebastien Boeuf > Signed-off-by: Dr. David Alan Gilbert > Signed-off-by: kbuild test robot > Cc: kvm@vger.kernel.org > Cc: "Michael S. Tsirkin" Acked-by: Michael S. Tsirkin > --- > drivers/virtio/virtio_pci_modern.c | 96 ++++++++++++++++++++++++++++++ > include/uapi/linux/virtio_pci.h | 11 +++- > 2 files changed, 106 insertions(+), 1 deletion(-) > > diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c > index db93cedd262f..3fc0cd848fe9 100644 > --- a/drivers/virtio/virtio_pci_modern.c > +++ b/drivers/virtio/virtio_pci_modern.c > @@ -444,6 +444,100 @@ static void del_vq(struct virtio_pci_vq_info *info) > vring_del_virtqueue(vq); > } > > +static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id, > + u8 *bar, u64 *offset, u64 *len) > +{ > + int pos; > + > + for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); pos > 0; > + pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) { > + u8 type, cap_len, id; > + u32 tmp32; > + u64 res_offset, res_length; > + > + pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap, > + cfg_type), &type); > + if (type != VIRTIO_PCI_CAP_SHARED_MEMORY_CFG) > + continue; > + > + pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap, > + cap_len), &cap_len); > + if (cap_len != sizeof(struct virtio_pci_cap64)) { > + printk(KERN_ERR "%s: shm cap with bad size offset: %d" > + "size: %d\n", __func__, pos, cap_len); > + continue; > + } > + > + pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap, > + id), &id); > + if (id != required_id) > + continue; > + > + /* Type, and ID match, looks good */ > + pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap, > + bar), bar); > + > + /* Read the lower 32bit of length and offset */ > + pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap, > + offset), &tmp32); > + res_offset = tmp32; > + pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap, > + length), &tmp32); > + res_length = tmp32; > + > + /* and now the top half */ > + pci_read_config_dword(dev, > + pos + offsetof(struct virtio_pci_cap64, > + offset_hi), &tmp32); > + res_offset |= ((u64)tmp32) << 32; > + pci_read_config_dword(dev, > + pos + offsetof(struct virtio_pci_cap64, > + length_hi), &tmp32); > + res_length |= ((u64)tmp32) << 32; > + > + *offset = res_offset; > + *len = res_length; > + > + return pos; > + } > + return 0; > +} > + > +static bool vp_get_shm_region(struct virtio_device *vdev, > + struct virtio_shm_region *region, u8 id) > +{ > + struct virtio_pci_device *vp_dev = to_vp_device(vdev); > + struct pci_dev *pci_dev = vp_dev->pci_dev; > + u8 bar; > + u64 offset, len; > + phys_addr_t phys_addr; > + size_t bar_len; > + > + if (!virtio_pci_find_shm_cap(pci_dev, id, &bar, &offset, &len)) { > + return false; > + } > + > + phys_addr = pci_resource_start(pci_dev, bar); > + bar_len = pci_resource_len(pci_dev, bar); > + > + if ((offset + len) < offset) { > + dev_err(&pci_dev->dev, "%s: cap offset+len overflow detected\n", > + __func__); > + return false; > + } > + > + if (offset + len > bar_len) { > + dev_err(&pci_dev->dev, "%s: bar shorter than cap offset+len\n", > + __func__); > + return false; > + } Maybe move this to a common header so the checks can be reused by other transports? Can be a patch on top. > + > + region->len = len; > + region->addr = (u64) phys_addr + offset; > + > + return true; > +} > + > static const struct virtio_config_ops virtio_pci_config_nodev_ops = { > .get = NULL, > .set = NULL, > @@ -458,6 +552,7 @@ static const struct virtio_config_ops virtio_pci_config_nodev_ops = { > .bus_name = vp_bus_name, > .set_vq_affinity = vp_set_vq_affinity, > .get_vq_affinity = vp_get_vq_affinity, > + .get_shm_region = vp_get_shm_region, > }; > > static const struct virtio_config_ops virtio_pci_config_ops = { > @@ -474,6 +569,7 @@ static const struct virtio_config_ops virtio_pci_config_ops = { > .bus_name = vp_bus_name, > .set_vq_affinity = vp_set_vq_affinity, > .get_vq_affinity = vp_get_vq_affinity, > + .get_shm_region = vp_get_shm_region, > }; > > /** > diff --git a/include/uapi/linux/virtio_pci.h b/include/uapi/linux/virtio_pci.h > index 90007a1abcab..fe9f43680a1d 100644 > --- a/include/uapi/linux/virtio_pci.h > +++ b/include/uapi/linux/virtio_pci.h > @@ -113,6 +113,8 @@ > #define VIRTIO_PCI_CAP_DEVICE_CFG 4 > /* PCI configuration access */ > #define VIRTIO_PCI_CAP_PCI_CFG 5 > +/* Additional shared memory capability */ > +#define VIRTIO_PCI_CAP_SHARED_MEMORY_CFG 8 > > /* This is the PCI capability header: */ > struct virtio_pci_cap { > @@ -121,11 +123,18 @@ struct virtio_pci_cap { > __u8 cap_len; /* Generic PCI field: capability length */ > __u8 cfg_type; /* Identifies the structure. */ > __u8 bar; /* Where to find it. */ > - __u8 padding[3]; /* Pad to full dword. */ > + __u8 id; /* Multiple capabilities of the same type */ > + __u8 padding[2]; /* Pad to full dword. */ > __le32 offset; /* Offset within bar. */ > __le32 length; /* Length of the structure, in bytes. */ > }; > > +struct virtio_pci_cap64 { > + struct virtio_pci_cap cap; > + __le32 offset_hi; /* Most sig 32 bits of offset */ > + __le32 length_hi; /* Most sig 32 bits of length */ > +}; > + > struct virtio_pci_notify_cap { > struct virtio_pci_cap cap; > __le32 notify_off_multiplier; /* Multiplier for queue_notify_off. */ > -- > 2.25.4 >