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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF92CC7EE2D for ; Thu, 25 May 2023 15:58:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 60FE1900002; Thu, 25 May 2023 11:58:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C09B6B0075; Thu, 25 May 2023 11:58:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 487F0900002; Thu, 25 May 2023 11:58:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 357FF6B0074 for ; Thu, 25 May 2023 11:58:00 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id F32271A03AF for ; Thu, 25 May 2023 15:57:59 +0000 (UTC) X-FDA: 80829233478.17.B1A001F Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf10.hostedemail.com (Postfix) with ESMTP id 0DFD5C0011 for ; Thu, 25 May 2023 15:57:57 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf10.hostedemail.com: domain of robin.murphy@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=robin.murphy@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685030278; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=riR0s4HOGI6ldcIcMeBqcBSXH1NUMmXFxwgIOTmiiNM=; b=ODYQxC3q/U8hokymiYkO9qybzibprEjHJyZbkPSOSwe9/uWRuWJ6oA96vtF6ZCt6u39u8m v4CUWm9KlS1wFkazysAFYZE8200Z9zDj2BfNp3VsBRJBSnJXXgps8B0gX+6SEx3MWAMLyx vYWPKA7f1btt/KY6HDjSB7B4Zksn/Jg= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf10.hostedemail.com: domain of robin.murphy@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=robin.murphy@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685030278; a=rsa-sha256; cv=none; b=J9M5SIEpMW259U5+1r0eKpZ9Q/mb6W0taEJFGKkJwlMZbFtOTxmiW3iaUuXF/7aqnXYj0T i6O9Ebl3MS/2pNzwcj2UBFR58k90oeIW2eyikGXMQdO9q+L+0NFACGcfMwqih8ST8Ivaot qgT1p8ILDEsyue1Xbb/zL4SvnpJCwWk= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 003CAD75; Thu, 25 May 2023 08:58:42 -0700 (PDT) Received: from [10.1.196.40] (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 729503F762; Thu, 25 May 2023 08:57:54 -0700 (PDT) Message-ID: Date: Thu, 25 May 2023 16:57:53 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux aarch64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [PATCH v5 13/15] iommu/dma: Force bouncing if the size is not cacheline-aligned Content-Language: en-GB To: Catalin Marinas , Linus Torvalds , Christoph Hellwig Cc: Arnd Bergmann , Greg Kroah-Hartman , Will Deacon , Marc Zyngier , Andrew Morton , Herbert Xu , Ard Biesheuvel , Isaac Manjarres , Saravana Kannan , Alasdair Kergon , Daniel Vetter , Joerg Roedel , Mark Brown , Mike Snitzer , "Rafael J. Wysocki" , linux-mm@kvack.org, iommu@lists.linux.dev, linux-arm-kernel@lists.infradead.org References: <20230524171904.3967031-1-catalin.marinas@arm.com> <20230524171904.3967031-14-catalin.marinas@arm.com> From: Robin Murphy In-Reply-To: <20230524171904.3967031-14-catalin.marinas@arm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 0DFD5C0011 X-Stat-Signature: 4drx4piqmkou4wh51eu6wymw9rg5u1fg X-Rspam-User: X-HE-Tag: 1685030277-790966 X-HE-Meta: U2FsdGVkX1/7dbdBqT+wpXp4cMmyh9PqRWuAwXJbcoevdndHGWjVbexs4C+bn5J2SmUqxDDq9cVOBhmB6vLU7Lvmn3b+U3gjn7+LweVu07FgqrtvoAAUM1/QwU78KC5ZSEzQ0RdpRndMH+6c6qucRm6ywgwP+K7ZcwmM+0Zasmdw4KOjrbfx5wsQPVOSO03PGXD66V6pFaCAfJuiNw5eZbbsaiJz3HqCdnBt7LYvzCnpkjmTub8PlbjjnvNrmagWtWsEDAYl+yahszW8OI5f8d29d2+oGAZlwf+6Obq7wLzVhE3mj7ip2XQhXk1oyjnmbviJodvw7KngsXnQmO8uHD5yWkh1Tc5UKKOXrOi7Z685EGD62eYQirHnH1NKebBd+X+UrXsB9dqs49dNbPNm8NFMDDSp9cx5jB0HtPPV0kJ8zup24Ar9eRTjE9Pb3QA3Zjz/vG05jW12wdA71PxFOqxwZHfPHqs9i4Z4b5DAn0f414ylpfrX7hEoqnHoAkKyliBBYThsaQgKYNgbc4kXKNBHNQtA3QlZShzshkNN1ZTfSuLN3Gy+OidSI/4n6hyy0NUwWJdoTLLfyD8/kc32HJBZ3CqBz0eXcl6Fxv4pcwfZ+mAiQGxJrtYP7Dh30UmcPhwsJJKKpYItfYgdDwghXqSb7t6QR57i13HGFIDWgoIlrdH+oNW+yW0NNC71oF8cG8/gjzXMaO8VInqzaxQAvUKRfDJ+ELhWPLc3R5Tfhpo3S9E3EOVcuJ+2WEkvkAqT+siijaQX/Bq9mI/EcveCjBuSzCxDBIVxCXNY4Ou60kB9zlagJPoJ3InwHsXFncVSEKtiJ0eESvVA0XsnmQmyHLlHKHx2ASfkyD+XTfTDHD6jj+zta+gl9n6f+e1awq5FwUU5K7+xbNcm1VY34Z5yEyGHT4Uk+ST1IEGZjr7pcX9GYsuTCy95oeks41Kzl20UgK56ymlAW+rc+xBMAiF Vrwh1KPP 6I9TR4JAqMAUp61XDTFErcCfVe0b5onJ69q4eS7+SRkBc2VAyqak36cnpAqs9ESV7GvOjd05HIH7FTmawQP0VYXpk5Rr/KNJY4gCkrJmckiAT/5uIGo+La/L36ZzP1RdmBPi6NmT16G02dwwWu+hRqOrLIxjesl3tS/DQe4oUIIHoWsqfvsTsG/OcrhHdCamhI5qTGrv2qi7CiGvAxwvoyjYC0biPGez0Gq98 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 24/05/2023 6:19 pm, Catalin Marinas wrote: > Similarly to the direct DMA, bounce small allocations as they may have > originated from a kmalloc() cache not safe for DMA. Unlike the direct > DMA, iommu_dma_map_sg() cannot call iommu_dma_map_sg_swiotlb() for all > non-coherent devices as this would break some cases where the iova is > expected to be contiguous (dmabuf). Instead, scan the scatterlist for > any small sizes and only go the swiotlb path if any element of the list > needs bouncing (note that iommu_dma_map_page() would still only bounce > those buffers which are not DMA-aligned). > > To avoid scanning the scatterlist on the 'sync' operations, introduce an > SG_DMA_USE_SWIOTLB flag set by iommu_dma_map_sg_swiotlb(). The > dev_use_swiotlb() function together with the newly added > dev_use_sg_swiotlb() now check for both untrusted devices and unaligned > kmalloc() buffers (suggested by Robin Murphy). > > Signed-off-by: Catalin Marinas > Cc: Joerg Roedel > Cc: Christoph Hellwig > Cc: Robin Murphy > --- [...] > diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h > index 87aaf8b5cdb4..330a157c5501 100644 > --- a/include/linux/scatterlist.h > +++ b/include/linux/scatterlist.h > @@ -248,6 +248,29 @@ static inline void sg_unmark_end(struct scatterlist *sg) > sg->page_link &= ~SG_END; > } > > +#define SG_DMA_BUS_ADDRESS (1 << 0) > +#define SG_DMA_USE_SWIOTLB (1 << 1) > + > +#ifdef CONFIG_SWIOTLB > +static inline bool sg_is_dma_use_swiotlb(struct scatterlist *sg) Nit: can we decide whether this API is named sg__ or sg_dma__? I'm leaning towards the latter, which would be consistent with the existing kerneldoc if not all the code (which I shall probably now send a patch to fix). However, my internal grammar parser just cannot cope with the "is use" double-verb construct, so I would be inclined to collapse this particular one to "sg_dma_use_swiotlb" either way. Yes, it breaks the pattern, but that's what the English language does best :) Otherwise, Reviewed-by: Robin Murphy I don't seem to have got round to giving this a spin on my Juno today, but will try to do so soon. Cheers, Robin. > +{ > + return sg->dma_flags & SG_DMA_USE_SWIOTLB; > +} > + > +static inline void sg_dma_mark_use_swiotlb(struct scatterlist *sg) > +{ > + sg->dma_flags |= SG_DMA_USE_SWIOTLB; > +} > +#else > +static inline bool sg_is_dma_use_swiotlb(struct scatterlist *sg) > +{ > + return false; > +} > +static inline void sg_dma_mark_use_swiotlb(struct scatterlist *sg) > +{ > +} > +#endif > + > /* > * CONFIG_PCI_P2PDMA depends on CONFIG_64BIT which means there is 4 bytes > * in struct scatterlist (assuming also CONFIG_NEED_SG_DMA_LENGTH is set). > @@ -256,8 +279,6 @@ static inline void sg_unmark_end(struct scatterlist *sg) > */ > #ifdef CONFIG_PCI_P2PDMA > > -#define SG_DMA_BUS_ADDRESS (1 << 0) > - > /** > * sg_dma_is_bus address - Return whether a given segment was marked > * as a bus address