Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp8036133rdb; Thu, 4 Jan 2024 16:58:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IFF9Gsz44Sh0oPyJvye70PXpKu+YvYIEoVWVVuEJqQIGlEiqzNJu6XF6RvXJJ+qsa+D7xWV X-Received: by 2002:ac2:42cd:0:b0:50e:4a24:28b1 with SMTP id n13-20020ac242cd000000b0050e4a2428b1mr665488lfl.123.1704416337814; Thu, 04 Jan 2024 16:58:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704416337; cv=none; d=google.com; s=arc-20160816; b=UvZYSJljOYMtj2iBtd3m3F5aqHB60+0qRTZ3m9PaAxnr1atyz+cY6KnuTQZPO3Iht5 JWzcUMUWODD3tSq40SOU9Sb3IlMP2/EeJkBg6PS8LURPX2nD11eiBDnBg0+1YhCTwuD8 aV1ibPIVXWMF8BoYX0CDvMXgZDbNB6TD/DtwVgEa6cx0iWj47LBgZHl+71qB975cXmBv D6KXE/Lywan+plFMdHSVhD+Q6llANA63nYU4JEbOrzXpc3IdRoWZIocF1C+T9sS7nZ1L LEimEiWWKvd8M96/0MUBjYtbNAKGUSCUYqQdNZMf57kEa8s4u/4/pnkG8hWlNhrl9S5p ahHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :user-agent:references:message-id:in-reply-to:subject:cc:to:from :date:dkim-signature; bh=qVDso5Oy4a+PJDyn7GfqU4nGZuaiVw69q4EZxprg4DA=; fh=22ga+chDUQk3PLateJBrIOZt1LCpZO61rL9wjU+3KG0=; b=R+3TUJt3rL311TpENqdvSbIw6+mzJbahmJoMfH9kH8wvMTslZ1tJXnHzHDMIUO1+tW dghvIOFNjGDiGyS1eFqt9p/LVRMEKm0qzCxHbmxs4FTMIp/j2hvOAC07Em/ivBOczAnT G4Cf/RBFc2UGAgt/LS8lGxEKzT/SYafEmDR4VDmOr/sOkql/bArxHFVWVyNNFwgnRRgR P5Q9gN5lVCh8oXZ1QUKwKjEiGkM7DsJ93QnaESbHAKRhzYA+wPjWGAOsjILA/FRf59DZ cRVV5q4EADA4DXgVNOYd1z55higxtNL3zAUCSrwiyDe7Pl20vy3EAScFClU9axWWi9gC vcOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZJqR8yDP; spf=pass (google.com: domain of linux-kernel+bounces-17362-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17362-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a20-20020a509b54000000b0055590da3330si211847edj.623.2024.01.04.16.58.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 16:58:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-17362-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZJqR8yDP; spf=pass (google.com: domain of linux-kernel+bounces-17362-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17362-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 8D8341F22BB0 for ; Fri, 5 Jan 2024 00:58:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 46AEC1FA5; Fri, 5 Jan 2024 00:58:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZJqR8yDP" X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7A1B3185B for ; Fri, 5 Jan 2024 00:58:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D7803C433C7; Fri, 5 Jan 2024 00:58:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1704416330; bh=LbWufgpY2x1b9pPxEw8zyOci1BCSYD9ImieN+zxIa/w=; h=Date:From:To:cc:Subject:In-Reply-To:References:From; b=ZJqR8yDPn9AzVbQuyqNZlCBakm6CAC5NDXhDq160wUqttEUt7OXwRWhdZJnEz3ZQk 8TGt8ZEqVFjK1jiPYS6usSVop+XpLuDOjaqD8NdlSJkYVsT4LMVhSQB0w2HvLZhuTQ B/qSSuDx270axwXqk7Kb7kzVKMZ7hkJB4XLuWcwmNzZutXmHambwqQeTV0beo7ffNk 7jpZwzDkIfpbsnMOIRl4M/MmmqaU/O6Q952Baay+cibrSbxqeCOPtKhDSXAHQkCwsf t1S3lMxn57wz1UMqqLqZvQrr3B9DyreCWn9DUdzk5tMAQrbmu2AVPHaZAI856N+PvD Zm6U3+d9Mh7dQ== Date: Thu, 4 Jan 2024 16:58:47 -0800 (PST) From: Stefano Stabellini X-X-Sender: sstabellini@ubuntu-linux-20-04-desktop To: Oleksandr Tyshchenko cc: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, Oleksandr Tyshchenko , =?UTF-8?Q?Christian_K=C3=B6nig?= , Daniel Vetter , Juergen Gross , Stefano Stabellini Subject: Re: [PATCH] xen/gntdev: Fix the abuse of underlying struct page in DMA-buf import In-Reply-To: <20240104185327.177376-1-olekstysh@gmail.com> Message-ID: References: <20240104185327.177376-1-olekstysh@gmail.com> User-Agent: Alpine 2.22 (DEB 394 2020-01-19) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII On Thu, 4 Jan 2024, Oleksandr Tyshchenko wrote: > From: Oleksandr Tyshchenko > > DO NOT access the underlying struct page of an sg table exported > by DMA-buf in dmabuf_imp_to_refs(), this is not allowed. > Please see drivers/dma-buf/dma-buf.c:mangle_sg_table() for details. > > Fortunately, here (for special Xen device) we can avoid using > pages and calculate gfns directly from dma addresses provided by > the sg table. > > Suggested-by: Daniel Vetter > Signed-off-by: Oleksandr Tyshchenko Reviewed-by: Stefano Stabellini > --- > Please note, I didn't manage to test the patch against the latest master branch > on real HW (patch was only build tested there). Patch was tested on Arm64 > guests using Linux v5.10.41 from vendor's BSP, this is the environment where > running this use-case is possible and to which I have an access (Xen PV display > with zero-copy and backend domain as a buffer provider - be-alloc=1, so dma-buf > import part was involved). A little bit old, but the dma-buf import code > in gntdev-dmabuf.c hasn't been changed much since that time, all context > remains allmost the same according to my code inspection. > --- > --- > drivers/xen/gntdev-dmabuf.c | 42 +++++++++++++++---------------------- > 1 file changed, 17 insertions(+), 25 deletions(-) > > diff --git a/drivers/xen/gntdev-dmabuf.c b/drivers/xen/gntdev-dmabuf.c > index 4440e626b797..0dde49fca9a5 100644 > --- a/drivers/xen/gntdev-dmabuf.c > +++ b/drivers/xen/gntdev-dmabuf.c > @@ -11,6 +11,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -50,7 +51,7 @@ struct gntdev_dmabuf { > > /* Number of pages this buffer has. */ > int nr_pages; > - /* Pages of this buffer. */ > + /* Pages of this buffer (only for dma-buf export). */ > struct page **pages; > }; > > @@ -484,7 +485,7 @@ static int dmabuf_exp_from_refs(struct gntdev_priv *priv, int flags, > /* DMA buffer import support. */ > > static int > -dmabuf_imp_grant_foreign_access(struct page **pages, u32 *refs, > +dmabuf_imp_grant_foreign_access(unsigned long *gfns, u32 *refs, > int count, int domid) > { > grant_ref_t priv_gref_head; > @@ -507,7 +508,7 @@ dmabuf_imp_grant_foreign_access(struct page **pages, u32 *refs, > } > > gnttab_grant_foreign_access_ref(cur_ref, domid, > - xen_page_to_gfn(pages[i]), 0); > + gfns[i], 0); > refs[i] = cur_ref; > } > > @@ -529,7 +530,6 @@ static void dmabuf_imp_end_foreign_access(u32 *refs, int count) > > static void dmabuf_imp_free_storage(struct gntdev_dmabuf *gntdev_dmabuf) > { > - kfree(gntdev_dmabuf->pages); > kfree(gntdev_dmabuf->u.imp.refs); > kfree(gntdev_dmabuf); > } > @@ -549,12 +549,6 @@ static struct gntdev_dmabuf *dmabuf_imp_alloc_storage(int count) > if (!gntdev_dmabuf->u.imp.refs) > goto fail; > > - gntdev_dmabuf->pages = kcalloc(count, > - sizeof(gntdev_dmabuf->pages[0]), > - GFP_KERNEL); > - if (!gntdev_dmabuf->pages) > - goto fail; > - > gntdev_dmabuf->nr_pages = count; > > for (i = 0; i < count; i++) > @@ -576,7 +570,8 @@ dmabuf_imp_to_refs(struct gntdev_dmabuf_priv *priv, struct device *dev, > struct dma_buf *dma_buf; > struct dma_buf_attachment *attach; > struct sg_table *sgt; > - struct sg_page_iter sg_iter; > + struct sg_dma_page_iter sg_iter; > + unsigned long *gfns; > int i; > > dma_buf = dma_buf_get(fd); > @@ -624,26 +619,23 @@ dmabuf_imp_to_refs(struct gntdev_dmabuf_priv *priv, struct device *dev, > > gntdev_dmabuf->u.imp.sgt = sgt; > > - /* Now convert sgt to array of pages and check for page validity. */ > + gfns = kcalloc(count, sizeof(*gfns), GFP_KERNEL); > + if (!gfns) > + goto fail_unmap; > + > + /* Now convert sgt to array of gfns without accessing underlying pages. */ > i = 0; > - for_each_sgtable_page(sgt, &sg_iter, 0) { > - struct page *page = sg_page_iter_page(&sg_iter); > - /* > - * Check if page is valid: this can happen if we are given > - * a page from VRAM or other resources which are not backed > - * by a struct page. > - */ > - if (!pfn_valid(page_to_pfn(page))) { > - ret = ERR_PTR(-EINVAL); > - goto fail_unmap; > - } > + for_each_sgtable_dma_page(sgt, &sg_iter, 0) { > + dma_addr_t addr = sg_page_iter_dma_address(&sg_iter); > + unsigned long pfn = bfn_to_pfn(XEN_PFN_DOWN(dma_to_phys(dev, addr))); > > - gntdev_dmabuf->pages[i++] = page; > + gfns[i++] = pfn_to_gfn(pfn); > } > > - ret = ERR_PTR(dmabuf_imp_grant_foreign_access(gntdev_dmabuf->pages, > + ret = ERR_PTR(dmabuf_imp_grant_foreign_access(gfns, > gntdev_dmabuf->u.imp.refs, > count, domid)); > + kfree(gfns); > if (IS_ERR(ret)) > goto fail_end_access; > > -- > 2.34.1 >