Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp21079752rwd; Thu, 29 Jun 2023 10:37:20 -0700 (PDT) X-Google-Smtp-Source: APBJJlENlMdvIdejKif7k905810phan8pioHfqqV+A9y5PrejxnWywxHPa8qyTNLPw8NzpZ/9QY6 X-Received: by 2002:a17:90a:be14:b0:260:aa73:e406 with SMTP id a20-20020a17090abe1400b00260aa73e406mr54336pjs.38.1688060240339; Thu, 29 Jun 2023 10:37:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688060240; cv=none; d=google.com; s=arc-20160816; b=MWMF/mSzMTLHYzF8iTOTnkZrel8y20zqPYEmEvvln+oKrnIM05DdEg+JRx4FGY0kKk 2u+BlAaeutM2TBuubaRctrut/gq9pWPylZn41cfe/j4zqojGpwJP+R90zV+xgCr7pZsl uvnkvwxmAZvOHm3IIhO3J7FsQPevGsLMXlCzFxEKVOQ4gjBq8C3KiYqFl26/4dElVWdC DNlMMmoIBA4kT3soMpHkyDBf/lzJQalcq+pVoMK9aFQGQtdFpK695yqGLDInol7pQt2/ 1PsOPJMd+KGHON0vDUIm8GK/b1Uemmnwjlg4rSG8OVEMRQKWrUwKkHizsShSTgLbgjqA j6Jg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=/IdOnearN4GuUppTPukyhmdRhO335i7NZ+c80wc0HRk=; fh=EWR9KIckwvrHGXvymsjn93c0zaplVwU92afJVx+K2ng=; b=jJW3mUzHtzvwmVmsSXdjLLiuUfGv7St2TKK6o2uyQzi+7FIRaNgXJ2xiV87TMdEgdi 6UNuXaQ0yLsZai4J9B3aQ2eCGPzekA4pdFMusC8RcXfKnyC9ihnoC8zPJB/jPXQZ0QQ8 CbVZIhV4RlxZziBAxnqPHCofJkJEx5GyA5ynDzuA2ykD02s5VymGVcWWmjP0JG4aZu/0 8J+YTklFnTn5/oEE99NaQ4/jKH0TvkP0n80ZhZYm1xBmFEVyoj3pIoLTtP7ZlQhNFQYn JTEllBwQPBCzOFCGhWFpgGWhIbKrL9TKncWnrLFTeelagIgQTW00fSoZ1IDrSQKFO5mI 7CtQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 25-20020a17090a195900b0025bf9851cfdsi10998549pjh.13.2023.06.29.10.37.05; Thu, 29 Jun 2023 10:37:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232235AbjF2RVQ (ORCPT + 99 others); Thu, 29 Jun 2023 13:21:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231430AbjF2RVK (ORCPT ); Thu, 29 Jun 2023 13:21:10 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 11D573595; Thu, 29 Jun 2023 10:21:07 -0700 (PDT) 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 08712C14; Thu, 29 Jun 2023 10:21:51 -0700 (PDT) Received: from [10.57.33.98] (unknown [10.57.33.98]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E3C073F64C; Thu, 29 Jun 2023 10:21:04 -0700 (PDT) Message-ID: <94d4b082-7b08-82e0-bb42-6ac36821ea61@arm.com> Date: Thu, 29 Jun 2023 18:20:58 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Subject: Re: [PATCH 3/6] dma-mapping: add a dma_alloc_noncontiguous API Content-Language: en-GB To: Christoph Hellwig , Mauro Carvalho Chehab , Marek Szyprowski , Tomasz Figa , Ricardo Ribalda , Sergey Senozhatsky , Laurent Pinchart , iommu@lists.linux-foundation.org Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org References: <20210301085236.947011-1-hch@lst.de> <20210301085236.947011-4-hch@lst.de> From: Robin Murphy In-Reply-To: <20210301085236.947011-4-hch@lst.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [Archaeology ensues...] On 2021-03-01 08:52, Christoph Hellwig wrote: [...] > +static struct sg_table *alloc_single_sgt(struct device *dev, size_t size, > + enum dma_data_direction dir, gfp_t gfp) > +{ > + struct sg_table *sgt; > + struct page *page; > + > + sgt = kmalloc(sizeof(*sgt), gfp); > + if (!sgt) > + return NULL; > + if (sg_alloc_table(sgt, 1, gfp)) > + goto out_free_sgt; > + page = __dma_alloc_pages(dev, size, &sgt->sgl->dma_address, dir, gfp); > + if (!page) > + goto out_free_table; > + sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0); > + sg_dma_len(sgt->sgl) = sgt->sgl->length; > + return sgt; > +out_free_table: > + sg_free_table(sgt); > +out_free_sgt: > + kfree(sgt); > + return NULL; > +} > + > +struct sg_table *dma_alloc_noncontiguous(struct device *dev, size_t size, > + enum dma_data_direction dir, gfp_t gfp, unsigned long attrs) > +{ > + const struct dma_map_ops *ops = get_dma_ops(dev); > + struct sg_table *sgt; > + > + if (WARN_ON_ONCE(attrs & ~DMA_ATTR_ALLOC_SINGLE_PAGES)) > + return NULL; > + > + if (ops && ops->alloc_noncontiguous) > + sgt = ops->alloc_noncontiguous(dev, size, dir, gfp, attrs); > + else > + sgt = alloc_single_sgt(dev, size, dir, gfp); > + > + if (sgt) { > + sgt->nents = 1; > + debug_dma_map_sg(dev, sgt->sgl, sgt->orig_nents, 1, dir); It turns out this is liable to trip up DMA_API_DEBUG_SG (potentially even in the alloc_single_sgt() case), since we've filled in sgt without paying attention to the device's segment boundary/size parameters. Now, it would be entirely possible to make the allocators "properly" partition the pages into multiple segments per those constraints, but given that there's no actual dma_map_sg() operation involved, and AFAIR the intent here is really only to describe a single DMA-contiguous buffer as pages, rather than represent a true scatter-gather operation, I'm now wondering whether it makes more sense to just make dma-debug a bit cleverer instead. Any other opinions? Thanks, Robin. > + } > + return sgt; > +}