From nobody Tue May 7 06:47:24 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1623405371; cv=none; d=zohomail.com; s=zohoarc; b=XhgDXwOg1C/Z4T7jCaiDdsLVH4naHZ5eV8nvd8cpbDTcSWl3a53NEFniGx8xd2VKDz8Ws7ZQVnzwMI8W0N9i+Cbyg+BQ0+DgftyA0tiQOTnhf3yQoASHVTSeDb+Jk65pE/nmE6wc5RH0MWzXH52UmYcGtFil1Y5WudEVy0WoyG0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1623405371; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=L6Nq/3eMJ61O2ppaiV+K5WCKa4wxPQ8pA+EactXa5/Q=; b=evzmoa8Yq5Ohp46/X1wpYRNohLQU725b36FEyHAVHlsRsiuoWMC+alLmVU1SpppsY0+y/oXHQlSHN2KGasoyJjKDQJd0LbgdGZxjUy2MB/uqt/xDHpzs1EHxpE9BgDx+14lL4WdSEi2edn1zrppZZsHywe4m6VkzF14hF0LCGck= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1623405371389854.3506602081544; Fri, 11 Jun 2021 02:56:11 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.140328.259312 (Exim 4.92) (envelope-from ) id 1lrdtG-0007BL-Jm; Fri, 11 Jun 2021 09:55:50 +0000 Received: by outflank-mailman (output) from mailman id 140328.259312; Fri, 11 Jun 2021 09:55:50 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lrdtG-0007BE-EY; Fri, 11 Jun 2021 09:55:50 +0000 Received: by outflank-mailman (input) for mailman id 140328; Fri, 11 Jun 2021 09:55:49 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lrdtF-0007B8-RM for xen-devel@lists.xenproject.org; Fri, 11 Jun 2021 09:55:49 +0000 Received: from mail-lf1-x132.google.com (unknown [2a00:1450:4864:20::132]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id b2946a7b-4725-4054-861a-dff63ae2b86e; Fri, 11 Jun 2021 09:55:48 +0000 (UTC) Received: by mail-lf1-x132.google.com with SMTP id v22so7728501lfa.3 for ; Fri, 11 Jun 2021 02:55:48 -0700 (PDT) Received: from localhost ([178.151.124.169]) by smtp.gmail.com with ESMTPSA id r17sm651828ljp.40.2021.06.11.02.55.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 11 Jun 2021 02:55:47 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: b2946a7b-4725-4054-861a-dff63ae2b86e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=L6Nq/3eMJ61O2ppaiV+K5WCKa4wxPQ8pA+EactXa5/Q=; b=ph/XomxAPqIvxyU2lBvMH35+Kz6l15GgxHvCZPiV1EKnsNbwv18k8r3CfcXnDTadvY 0HnwK+tCN0fm+FLw08qSlxbWGgLd6GqEV8gP9+/y3mC8mcoEseYaHD7LCYroqq3CWIuc rvV1kJGEsSsa+H0Bu3TBsRdiyQHI4toAKrUIEEeDeNkbUJRQmjgiwHPiHMItUrOZc1Jp W4prezTF9Lefwdpg90MeiDy/5zo0TrVfeIoYveukEK7APT3Exak8EMxXVAcMlp+QDfko P6XaKRDFXiSX8yq0rw+oLRrog118hw6/Wc5k0gg7+VdGPRxrUvhmGPfxUDfyLjcQfPpN E6/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=L6Nq/3eMJ61O2ppaiV+K5WCKa4wxPQ8pA+EactXa5/Q=; b=nLPpMC9vkrEwbDCXbtbppNT6dZAUFunhpLIRU7dESK2EEeKjasKQLwNGijIjQCvqYB nCIGtSYUZMUK4c9uYls5ChhLZkWvWBW1pLkHU3tSFYD4JgOghx5wFTo/ls2+Qb4CfNbz sl19wrcxacqDCLS+MNos2VQcfTFjFNEMlecn9mX9P+MAK8ixxNFua0l117gGIsAIBqwK sjlo/WHXFWCWqGd9rJjD9BgFg8mmreOIQdtg053uui9xYKR23+uGDoyJp151iP+pgE/C CTWwTlLvryjfUMmSoJFO5p56LIDssUSiBPG86luDlgSGNKauNh3ZCtLryffv3phTZ5Kp avQw== X-Gm-Message-State: AOAM532Q3PO9B1b8AUqu9jASZ6myKPwGibKDNdt4Mn9NF6RAzvpCaAAn YS1twyxIhYOyNJcxdkGarN0= X-Google-Smtp-Source: ABdhPJy8EDHCE1f4s4ZC6KDj2A/UHmJhDH+REBsIFZnu4F/CM9rWslxdJXdDy1hMj7lLIs/rkF7G0w== X-Received: by 2002:a19:dc02:: with SMTP id t2mr2158836lfg.261.1623405347760; Fri, 11 Jun 2021 02:55:47 -0700 (PDT) From: Roman Skakun X-Google-Original-From: Roman Skakun To: Konrad Rzeszutek Wilk , Boris Ostrovsky , Juergen Gross , Stefano Stabellini , xen-devel@lists.xenproject.org, iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org Cc: Oleksandr Tyshchenko , Oleksandr Andrushchenko , Volodymyr Babchuk , Roman Skakun , Roman Skakun , Andrii Anisov Subject: [PATCH] swiotlb-xen: override common mmap and get_sgtable dma ops Date: Fri, 11 Jun 2021 12:55:28 +0300 Message-Id: <20210611095528.9230-1-roman_skakun@epam.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) Content-Type: text/plain; charset="utf-8" This commit is dedicated to fix incorrect convertion from cpu_addr to page address in cases when we get virtual address which allocated throught xen_swiotlb_alloc_coherent() and can be mapped in the vmalloc range. As the result, virt_to_page() cannot convert this address properly and return incorrect page address. Need to detect such cases and obtains the page address using vmalloc_to_page() instead. The reference code was copied from kernel/dma/ops_helpers.c and modified to provide additional detections as described above. Signed-off-by: Roman Skakun Reviewed-by: Andrii Anisov --- Also, I have observed that the original common code didn't=20 make additional checks about contiguity of the memory region represented by cpu_addr and size May be, this means that these functions can get only physically=20 contiguous memory. Is this correct? Cheers! --- drivers/xen/swiotlb-xen.c | 51 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/drivers/xen/swiotlb-xen.c b/drivers/xen/swiotlb-xen.c index 2b385c1b4a99..f99c98472927 100644 --- a/drivers/xen/swiotlb-xen.c +++ b/drivers/xen/swiotlb-xen.c @@ -563,6 +563,53 @@ xen_swiotlb_dma_supported(struct device *hwdev, u64 ma= sk) return xen_virt_to_bus(hwdev, xen_io_tlb_end - 1) <=3D mask; } =20 +static int +xen_swiotlb_dma_mmap(struct device *dev, struct vm_area_struct *vma, + void *cpu_addr, dma_addr_t dma_addr, size_t size, + unsigned long attrs) +{ + unsigned long user_count =3D vma_pages(vma); + unsigned long count =3D PAGE_ALIGN(size) >> PAGE_SHIFT; + unsigned long off =3D vma->vm_pgoff; + struct page *page; + + if (is_vmalloc_addr(cpu_addr)) + page =3D vmalloc_to_page(cpu_addr); + else + page =3D virt_to_page(cpu_addr); + + vma->vm_page_prot =3D dma_pgprot(dev, vma->vm_page_prot, attrs); + + if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret)) + return -ENXIO; + + if (off >=3D count || user_count > count - off) + return -ENXIO; + + return remap_pfn_range(vma, vma->vm_start, + page_to_pfn(page) + vma->vm_pgoff, + user_count << PAGE_SHIFT, vma->vm_page_prot); +} + +static int +xen_swiotlb_dma_get_sgtable(struct device *dev, struct sg_table *sgt, + void *cpu_addr, dma_addr_t dma_addr, size_t size, + unsigned long attrs) +{ + struct page *page; + int ret; + + if (is_vmalloc_addr(cpu_addr)) + page =3D vmalloc_to_page(cpu_addr); + else + page =3D virt_to_page(cpu_addr); + + ret =3D sg_alloc_table(sgt, 1, GFP_KERNEL); + if (!ret) + sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0); + return ret; +} + const struct dma_map_ops xen_swiotlb_dma_ops =3D { .alloc =3D xen_swiotlb_alloc_coherent, .free =3D xen_swiotlb_free_coherent, @@ -575,8 +622,8 @@ const struct dma_map_ops xen_swiotlb_dma_ops =3D { .map_page =3D xen_swiotlb_map_page, .unmap_page =3D xen_swiotlb_unmap_page, .dma_supported =3D xen_swiotlb_dma_supported, - .mmap =3D dma_common_mmap, - .get_sgtable =3D dma_common_get_sgtable, + .mmap =3D xen_swiotlb_dma_mmap, + .get_sgtable =3D xen_swiotlb_dma_get_sgtable, .alloc_pages =3D dma_common_alloc_pages, .free_pages =3D dma_common_free_pages, }; --=20 2.27.0