Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp28912395rwd; Wed, 5 Jul 2023 04:44:16 -0700 (PDT) X-Google-Smtp-Source: APBJJlFhEntBlSmFvlwDHCp55PnQrV/bSYxYdw7mH5YOv0/sg3OlAc7jlpFCwqUoK4QT6zcTxdhb X-Received: by 2002:a17:90b:3c4:b0:262:ece1:5fd0 with SMTP id go4-20020a17090b03c400b00262ece15fd0mr11711305pjb.12.1688557456315; Wed, 05 Jul 2023 04:44:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688557456; cv=none; d=google.com; s=arc-20160816; b=ujB+Dbq+d7u2tqN4NB95aFr9CkcjWdejlM/EbZYwrWighg0QHzbJ8oIy4UksNmH/0Y jYgzbdHVLy/JFa2B0yDxGjeihCcu3/TdqGMybQB/UrCwb2gxlJPBOBmBwx1GsP1Yp6Sm UUvdfSFl9t+25gHIku4yqrvuHPZTKnjsCxw+q630HXykxplduI5nZu6iTHUPjUFhchuX VHJ4fqsnuhrenCSavORMFPGZX0HGLFEJWv9j3wEx1aXH94SlCaXf/Lvc9OPjz/Y/JArY +hBhbv1ec5OhzfQ1MO45Zebue24k6rPjEZrqu1Ufr12PAFvGuwDqMyx/+uDS/R0uYQQS x62A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=gLYbyyTNl2Zb/qjIIe6Qi+9jxk/pFwwBytcz2lbXZbY=; fh=lCtYapCp93/NkF9MqJT4YA4p/E58lADqXaq4zjUMV2I=; b=r4QgYag2iVO5A0NaX62OOtVRRGzv6ownIDo3eKv1Dwtangb+w4wDtXP3DcPwd5+pbC AWIJknHq5hVIWwbB7uvHYE3NEnxvXZinoSizKRCbBSQ4mlPhqmw8RAYoN6cACtkC4JKT Z4Cu9xM9K1tQjwd+ySP71+GHtOLOfS3mSPXr+NEafuCXDHUY24Sq7Ppc7RSteMjXCHtt /y8yrCwJtxUzcpMzMFgcwEoze5NyyrnlAKEgTWRFEw4fMnY4swINkdIeiFQUl69en/dg mO37OUVWB3/xprW4TkXXJRFLTziTTUBJiyLvoGbw25yN+toII67rJeNvB+uxRuoFayyF S2Ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=n9q+aZJ9; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x3-20020a634a03000000b0054fd77fe97asi22460434pga.267.2023.07.05.04.44.03; Wed, 05 Jul 2023 04:44:16 -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; dkim=pass header.i=@chromium.org header.s=google header.b=n9q+aZJ9; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231819AbjGELRK (ORCPT + 99 others); Wed, 5 Jul 2023 07:17:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38494 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231463AbjGELRJ (ORCPT ); Wed, 5 Jul 2023 07:17:09 -0400 Received: from mail-qt1-x82e.google.com (mail-qt1-x82e.google.com [IPv6:2607:f8b0:4864:20::82e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61405DC for ; Wed, 5 Jul 2023 04:17:08 -0700 (PDT) Received: by mail-qt1-x82e.google.com with SMTP id d75a77b69052e-4036e428de0so14116561cf.3 for ; Wed, 05 Jul 2023 04:17:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1688555826; x=1691147826; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=gLYbyyTNl2Zb/qjIIe6Qi+9jxk/pFwwBytcz2lbXZbY=; b=n9q+aZJ92FmViihc3PgbYyXboMxqgX5yjUrMehgL2YkOKQS8Q124VGBlFT6SrvnHQ+ FablCQ64bt4FPIsq+6T+zM1U2uSZk+mykldx4YDRN4HB7hyl7mrTutmoG2joPgBVdX6S LqgbcrUJ30u/txmD+jETyZSSFXwImstD/opJ0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688555826; x=1691147826; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gLYbyyTNl2Zb/qjIIe6Qi+9jxk/pFwwBytcz2lbXZbY=; b=HbR0ANxXz9xe03ImJamEoXVK14piyNu/wSfEKX9V9LyxqRooUW4tzTJi1bggk++bmW OvlbtNoyv9xWP2u2v5v/GCHw7CKgFj7qI9xmaFzUOj2EniKOiPEbEOfg3RDMcrOzGUYl JTcesKlVGSKezXseKd6SoEqLE4KN5SjmXaWVimuXY8d3lqRUuw2ziQvRQSVDXpSP/O/2 nnSQ1op0MKs+mjIL7o49ChrG+J/NDQy9CwXX9x3epQk1xS4b2xN4AV/4TzT8+YFf3u/J H4D+bJAb9DrNdzyLHHwKhABoNhqrbHNaRz6S9fz+RA3midA8SIp330NWaStWqMJsazHZ yqEg== X-Gm-Message-State: AC+VfDwckDlTzdUpM0/la4QgG1gqaAtGXcVYh39qw8gXdSDGQdg0Q0zO O0GR2KEEZ/l2GnLM5iNgjTPhc1KU3MaWhASGJ3u5vw== X-Received: by 2002:ac8:5e48:0:b0:3ff:2cc2:3f54 with SMTP id i8-20020ac85e48000000b003ff2cc23f54mr19210287qtx.9.1688555825892; Wed, 05 Jul 2023 04:17:05 -0700 (PDT) Received: from mail-qv1-f47.google.com (mail-qv1-f47.google.com. [209.85.219.47]) by smtp.gmail.com with ESMTPSA id g11-20020ac8580b000000b003f3937c16c4sm2522989qtg.5.2023.07.05.04.17.05 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 05 Jul 2023 04:17:05 -0700 (PDT) Received: by mail-qv1-f47.google.com with SMTP id 6a1803df08f44-6348a8045a2so45407926d6.1 for ; Wed, 05 Jul 2023 04:17:05 -0700 (PDT) X-Received: by 2002:a05:6214:15ce:b0:626:2e65:cb2b with SMTP id p14-20020a05621415ce00b006262e65cb2bmr15434310qvz.4.1688555824535; Wed, 05 Jul 2023 04:17:04 -0700 (PDT) MIME-Version: 1.0 References: <20210301085236.947011-1-hch@lst.de> <20210301085236.947011-4-hch@lst.de> <94d4b082-7b08-82e0-bb42-6ac36821ea61@arm.com> In-Reply-To: <94d4b082-7b08-82e0-bb42-6ac36821ea61@arm.com> From: Tomasz Figa Date: Wed, 5 Jul 2023 20:16:54 +0900 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 3/6] dma-mapping: add a dma_alloc_noncontiguous API To: Robin Murphy Cc: Christoph Hellwig , Mauro Carvalho Chehab , Marek Szyprowski , Ricardo Ribalda , Sergey Senozhatsky , Laurent Pinchart , iommu@lists.linux-foundation.org, linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable 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 On Fri, Jun 30, 2023 at 2:21=E2=80=AFAM Robin Murphy = wrote: > > [Archaeology ensues...] > > On 2021-03-01 08:52, Christoph Hellwig wrote: > [...] > > +static struct sg_table *alloc_single_sgt(struct device *dev, size_t si= ze, > > + enum dma_data_direction dir, gfp_t gfp) > > +{ > > + struct sg_table *sgt; > > + struct page *page; > > + > > + sgt =3D kmalloc(sizeof(*sgt), gfp); > > + if (!sgt) > > + return NULL; > > + if (sg_alloc_table(sgt, 1, gfp)) > > + goto out_free_sgt; > > + page =3D __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) =3D 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 si= ze, > > + enum dma_data_direction dir, gfp_t gfp, unsigned long att= rs) > > +{ > > + const struct dma_map_ops *ops =3D 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 =3D ops->alloc_noncontiguous(dev, size, dir, gfp, att= rs); > > + else > > + sgt =3D alloc_single_sgt(dev, size, dir, gfp); > > + > > + if (sgt) { > > + sgt->nents =3D 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, Yeah, the name noncontiguous comes from potentially allocating non-contiguous physical pages, which based on a few people I talked with, ended up being quite confusing, but I can't really think of a better name either. Do we know how common devices with segment boundary/size constraints are and how likely they are to use this API? > I'm now wondering whether it makes more sense to just make dma-debug a > bit cleverer instead. Any other opinions? If we could assume that drivers for those devices shouldn't use this API, we could just fail if the segment boundary/size are set to something other than unlimited. Best regards, Tomasz > > Thanks, > Robin. > > > + } > > + return sgt; > > +}