From patchwork Wed Jan 23 16:30:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joerg Roedel X-Patchwork-Id: 10777557 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A9D2C913 for ; Wed, 23 Jan 2019 16:30:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 76D392D3C8 for ; Wed, 23 Jan 2019 16:30:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 582832D376; Wed, 23 Jan 2019 16:30:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C9CE82D3A0 for ; Wed, 23 Jan 2019 16:30:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726200AbfAWQaw (ORCPT ); Wed, 23 Jan 2019 11:30:52 -0500 Received: from 8bytes.org ([81.169.241.247]:59178 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725994AbfAWQaw (ORCPT ); Wed, 23 Jan 2019 11:30:52 -0500 Received: by theia.8bytes.org (Postfix, from userid 1000) id 424A6273; Wed, 23 Jan 2019 17:30:51 +0100 (CET) From: Joerg Roedel To: "Michael S . Tsirkin" , Jason Wang , Konrad Rzeszutek Wilk , Christoph Hellwig Cc: Jens Axboe , virtualization@lists.linux-foundation.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, jfehlig@suse.com, jon.grimm@amd.com, brijesh.singh@amd.com, joro@8bytes.org, jroedel@suse.de Subject: [PATCH 1/5] swiotlb: Introduce swiotlb_max_mapping_size() Date: Wed, 23 Jan 2019 17:30:45 +0100 Message-Id: <20190123163049.24863-2-joro@8bytes.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190123163049.24863-1-joro@8bytes.org> References: <20190123163049.24863-1-joro@8bytes.org> Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Joerg Roedel The function returns the maximum size that can be remapped by the SWIOTLB implementation. This function will be later exposed to users through the DMA-API. Signed-off-by: Joerg Roedel --- include/linux/swiotlb.h | 5 +++++ kernel/dma/swiotlb.c | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index 7c007ed7505f..ceb623321f38 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -62,6 +62,7 @@ extern void swiotlb_tbl_sync_single(struct device *hwdev, extern int swiotlb_dma_supported(struct device *hwdev, u64 mask); +extern size_t swiotlb_max_mapping_size(struct device *dev); #ifdef CONFIG_SWIOTLB extern enum swiotlb_force swiotlb_force; @@ -95,6 +96,10 @@ static inline unsigned int swiotlb_max_segment(void) { return 0; } +static inline size_t swiotlb_max_mapping_size(struct device *dev) +{ + return SIZE_MAX; +} #endif /* CONFIG_SWIOTLB */ extern void swiotlb_print_info(void); diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 1fb6fd68b9c7..9cb21259cb0b 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -662,3 +662,8 @@ swiotlb_dma_supported(struct device *hwdev, u64 mask) { return __phys_to_dma(hwdev, io_tlb_end - 1) <= mask; } + +size_t swiotlb_max_mapping_size(struct device *dev) +{ + return ((size_t)1 << IO_TLB_SHIFT) * IO_TLB_SEGSIZE; +} From patchwork Wed Jan 23 16:30:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joerg Roedel X-Patchwork-Id: 10777565 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B31C61399 for ; Wed, 23 Jan 2019 16:31:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A193F2D3AB for ; Wed, 23 Jan 2019 16:31:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 95CB82D3B2; Wed, 23 Jan 2019 16:31:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2829F2D3AB for ; Wed, 23 Jan 2019 16:31:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726322AbfAWQbP (ORCPT ); Wed, 23 Jan 2019 11:31:15 -0500 Received: from 8bytes.org ([81.169.241.247]:59196 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726156AbfAWQax (ORCPT ); Wed, 23 Jan 2019 11:30:53 -0500 Received: by theia.8bytes.org (Postfix, from userid 1000) id 6761243A; Wed, 23 Jan 2019 17:30:51 +0100 (CET) From: Joerg Roedel To: "Michael S . Tsirkin" , Jason Wang , Konrad Rzeszutek Wilk , Christoph Hellwig Cc: Jens Axboe , virtualization@lists.linux-foundation.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, jfehlig@suse.com, jon.grimm@amd.com, brijesh.singh@amd.com, joro@8bytes.org, jroedel@suse.de Subject: [PATCH 2/5] swiotlb: Add is_swiotlb_active() function Date: Wed, 23 Jan 2019 17:30:46 +0100 Message-Id: <20190123163049.24863-3-joro@8bytes.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190123163049.24863-1-joro@8bytes.org> References: <20190123163049.24863-1-joro@8bytes.org> Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Joerg Roedel This function will be used from dma_direct code to determine the maximum segment size of a dma mapping. Signed-off-by: Joerg Roedel --- include/linux/swiotlb.h | 6 ++++++ kernel/dma/swiotlb.c | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index ceb623321f38..5c087d330b4b 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -63,6 +63,7 @@ extern void swiotlb_tbl_sync_single(struct device *hwdev, extern int swiotlb_dma_supported(struct device *hwdev, u64 mask); extern size_t swiotlb_max_mapping_size(struct device *dev); +bool is_swiotlb_active(void); #ifdef CONFIG_SWIOTLB extern enum swiotlb_force swiotlb_force; @@ -100,6 +101,11 @@ static inline size_t swiotlb_max_mapping_size(struct device *dev) { return SIZE_MAX; } + +static inline bool is_swiotlb_active(void) +{ + return false; +} #endif /* CONFIG_SWIOTLB */ extern void swiotlb_print_info(void); diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 9cb21259cb0b..9fbd075081d9 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -667,3 +667,8 @@ size_t swiotlb_max_mapping_size(struct device *dev) { return ((size_t)1 << IO_TLB_SHIFT) * IO_TLB_SEGSIZE; } + +bool is_swiotlb_active(void) +{ + return !no_iotlb_memory; +} From patchwork Wed Jan 23 16:30:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joerg Roedel X-Patchwork-Id: 10777561 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B8F56913 for ; Wed, 23 Jan 2019 16:31:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A6E2A2D384 for ; Wed, 23 Jan 2019 16:31:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9AE462D3A6; Wed, 23 Jan 2019 16:31:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 24CF02D384 for ; Wed, 23 Jan 2019 16:31:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726385AbfAWQax (ORCPT ); Wed, 23 Jan 2019 11:30:53 -0500 Received: from 8bytes.org ([81.169.241.247]:59214 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726178AbfAWQax (ORCPT ); Wed, 23 Jan 2019 11:30:53 -0500 Received: by theia.8bytes.org (Postfix, from userid 1000) id A3193451; Wed, 23 Jan 2019 17:30:51 +0100 (CET) From: Joerg Roedel To: "Michael S . Tsirkin" , Jason Wang , Konrad Rzeszutek Wilk , Christoph Hellwig Cc: Jens Axboe , virtualization@lists.linux-foundation.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, jfehlig@suse.com, jon.grimm@amd.com, brijesh.singh@amd.com, joro@8bytes.org, jroedel@suse.de Subject: [PATCH 3/5] dma: Introduce dma_max_mapping_size() Date: Wed, 23 Jan 2019 17:30:47 +0100 Message-Id: <20190123163049.24863-4-joro@8bytes.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190123163049.24863-1-joro@8bytes.org> References: <20190123163049.24863-1-joro@8bytes.org> Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Joerg Roedel The function returns the maximum size that can be mapped using DMA-API functions. The patch also adds the implementation for direct DMA and a new dma_map_ops pointer so that other implementations can expose their limit. Signed-off-by: Joerg Roedel --- include/linux/dma-mapping.h | 16 ++++++++++++++++ kernel/dma/direct.c | 11 +++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index f6ded992c183..a3ca8a71a704 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -130,6 +130,7 @@ struct dma_map_ops { enum dma_data_direction direction); int (*dma_supported)(struct device *dev, u64 mask); u64 (*get_required_mask)(struct device *dev); + size_t (*max_mapping_size)(struct device *dev); }; #define DMA_MAPPING_ERROR (~(dma_addr_t)0) @@ -257,6 +258,8 @@ static inline void dma_direct_sync_sg_for_cpu(struct device *dev, } #endif +size_t dma_direct_max_mapping_size(struct device *dev); + #ifdef CONFIG_HAS_DMA #include @@ -440,6 +443,19 @@ static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr) return 0; } +static inline size_t dma_max_mapping_size(struct device *dev) +{ + const struct dma_map_ops *ops = get_dma_ops(dev); + size_t size = SIZE_MAX; + + if (dma_is_direct(ops)) + size = dma_direct_max_mapping_size(dev); + else if (ops && ops->max_mapping_size) + size = ops->max_mapping_size(dev); + + return size; +} + void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flag, unsigned long attrs); void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr, diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c index 355d16acee6d..6310ad01f915 100644 --- a/kernel/dma/direct.c +++ b/kernel/dma/direct.c @@ -380,3 +380,14 @@ int dma_direct_supported(struct device *dev, u64 mask) */ return mask >= __phys_to_dma(dev, min_mask); } + +size_t dma_direct_max_mapping_size(struct device *dev) +{ + size_t size = SIZE_MAX; + + /* If SWIOTLB is active, use its maximum mapping size */ + if (is_swiotlb_active()) + size = swiotlb_max_mapping_size(dev); + + return size; +} From patchwork Wed Jan 23 16:30:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joerg Roedel X-Patchwork-Id: 10777563 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2767F913 for ; Wed, 23 Jan 2019 16:31:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 166AD2D376 for ; Wed, 23 Jan 2019 16:31:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0A2862D36E; Wed, 23 Jan 2019 16:31:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B094B2D376 for ; Wed, 23 Jan 2019 16:31:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726398AbfAWQbG (ORCPT ); Wed, 23 Jan 2019 11:31:06 -0500 Received: from 8bytes.org ([81.169.241.247]:59242 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726197AbfAWQax (ORCPT ); Wed, 23 Jan 2019 11:30:53 -0500 Received: by theia.8bytes.org (Postfix, from userid 1000) id C807B450; Wed, 23 Jan 2019 17:30:51 +0100 (CET) From: Joerg Roedel To: "Michael S . Tsirkin" , Jason Wang , Konrad Rzeszutek Wilk , Christoph Hellwig Cc: Jens Axboe , virtualization@lists.linux-foundation.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, jfehlig@suse.com, jon.grimm@amd.com, brijesh.singh@amd.com, joro@8bytes.org, jroedel@suse.de Subject: [PATCH 4/5] virtio: Introduce virtio_max_dma_size() Date: Wed, 23 Jan 2019 17:30:48 +0100 Message-Id: <20190123163049.24863-5-joro@8bytes.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190123163049.24863-1-joro@8bytes.org> References: <20190123163049.24863-1-joro@8bytes.org> Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Joerg Roedel This function returns the maximum segment size for a single dma transaction of a virtio device. The possible limit comes from the SWIOTLB implementation in the Linux kernel, that has an upper limit of (currently) 256kb of contiguous memory it can map. Other DMA-API implementations might also have limits. Use the new dma_max_mapping_size() function to determine the maximum mapping size when DMA-API is in use for virtio. Signed-off-by: Joerg Roedel --- drivers/virtio/virtio_ring.c | 10 ++++++++++ include/linux/virtio.h | 2 ++ 2 files changed, 12 insertions(+) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index cd7e755484e3..9ca3fe6af9fa 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -266,6 +266,16 @@ static bool vring_use_dma_api(struct virtio_device *vdev) return false; } +size_t virtio_max_dma_size(struct virtio_device *vdev) +{ + size_t max_segment_size = SIZE_MAX; + + if (vring_use_dma_api(vdev)) + max_segment_size = dma_max_mapping_size(&vdev->dev); + + return max_segment_size; +} + static void *vring_alloc_queue(struct virtio_device *vdev, size_t size, dma_addr_t *dma_handle, gfp_t flag) { diff --git a/include/linux/virtio.h b/include/linux/virtio.h index fa1b5da2804e..673fe3ef3607 100644 --- a/include/linux/virtio.h +++ b/include/linux/virtio.h @@ -157,6 +157,8 @@ int virtio_device_freeze(struct virtio_device *dev); int virtio_device_restore(struct virtio_device *dev); #endif +size_t virtio_max_dma_size(struct virtio_device *vdev); + #define virtio_device_for_each_vq(vdev, vq) \ list_for_each_entry(vq, &vdev->vqs, list) From patchwork Wed Jan 23 16:30:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joerg Roedel X-Patchwork-Id: 10777559 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C615B1399 for ; Wed, 23 Jan 2019 16:31:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B63342D324 for ; Wed, 23 Jan 2019 16:31:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AA6FD2D394; Wed, 23 Jan 2019 16:31:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 59D6E2D36F for ; Wed, 23 Jan 2019 16:31:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726009AbfAWQay (ORCPT ); Wed, 23 Jan 2019 11:30:54 -0500 Received: from 8bytes.org ([81.169.241.247]:59262 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726398AbfAWQay (ORCPT ); Wed, 23 Jan 2019 11:30:54 -0500 Received: by theia.8bytes.org (Postfix, from userid 1000) id F317946A; Wed, 23 Jan 2019 17:30:51 +0100 (CET) From: Joerg Roedel To: "Michael S . Tsirkin" , Jason Wang , Konrad Rzeszutek Wilk , Christoph Hellwig Cc: Jens Axboe , virtualization@lists.linux-foundation.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, jfehlig@suse.com, jon.grimm@amd.com, brijesh.singh@amd.com, joro@8bytes.org, jroedel@suse.de Subject: [PATCH 5/5] virtio-blk: Consider virtio_max_dma_size() for maximum segment size Date: Wed, 23 Jan 2019 17:30:49 +0100 Message-Id: <20190123163049.24863-6-joro@8bytes.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190123163049.24863-1-joro@8bytes.org> References: <20190123163049.24863-1-joro@8bytes.org> Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Joerg Roedel Segments can't be larger than the maximum DMA mapping size supported on the platform. Take that into account when setting the maximum segment size for a block device. Signed-off-by: Joerg Roedel --- drivers/block/virtio_blk.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c index b16a887bbd02..4bc083b7c9b5 100644 --- a/drivers/block/virtio_blk.c +++ b/drivers/block/virtio_blk.c @@ -723,7 +723,7 @@ static int virtblk_probe(struct virtio_device *vdev) struct request_queue *q; int err, index; - u32 v, blk_size, sg_elems, opt_io_size; + u32 v, blk_size, max_size, sg_elems, opt_io_size; u16 min_io_size; u8 physical_block_exp, alignment_offset; @@ -826,14 +826,16 @@ static int virtblk_probe(struct virtio_device *vdev) /* No real sector limit. */ blk_queue_max_hw_sectors(q, -1U); + max_size = virtio_max_dma_size(vdev); + /* Host can optionally specify maximum segment size and number of * segments. */ err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX, struct virtio_blk_config, size_max, &v); if (!err) - blk_queue_max_segment_size(q, v); - else - blk_queue_max_segment_size(q, -1U); + max_size = min(max_size, v); + + blk_queue_max_segment_size(q, max_size); /* Host can optionally specify the block size of the device */ err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,