Received: by 2002:a05:7412:1703:b0:e2:908c:2ebd with SMTP id dm3csp3067387rdb; Tue, 29 Aug 2023 04:37:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFLAlVRpAy8dNIEAnX/bVl9nd4e8YTdd+TZGfAO+L9akOIijA+KKXnTqqBnpjGjGkOv7EZZ X-Received: by 2002:a05:6a00:10c3:b0:68c:1a0:46ac with SMTP id d3-20020a056a0010c300b0068c01a046acmr12476902pfu.15.1693309028182; Tue, 29 Aug 2023 04:37:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693309028; cv=none; d=google.com; s=arc-20160816; b=GYAvVEr1/P5HQMYsSpWCwi2OgQVEeBSwHYNhMtElNyu0sAsnjMUyR00piGk52LF0Ki OGXCi5bIoirt4jpJMfPzjXlcs2fzkv5+5CnIrQMhTmV19OV2CIekq3FMQE+MFnNOxAfS enwS5qyIG+FIDBsa5bi3Mj48yGO9XK7u8dm6gFN7ElxVDu5/8n78/DjPRMgnj726E3SO MWWiufpKq9qzo/wzZuX4ddH/H0/V8ea1h5GPmfpGuFvNXO0XuERmpfkwWTPvYnjuaA9b zIHBKvtHODkPA/wbclsd5rvxb+i9BYNMzuFyNOScacsTlPuLYY3pBFI+6HHD5eychzM/ V5ng== 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=ZP8IWptwvbYki1UD0idEhyXBeR+2/3WCIYtRH7GpHZo=; fh=AfqDhcux+cHcjY8zJb7SgRyavqR4R1TNSBmAHM2uJk0=; b=Y2ENk9OvsldtwdYZU081ZIqzYXsiPfysD35axbi99WcRlc6ggBxyzttCeb9OZh0rCQ zSF8TLxxEYuU7xyxhZrviNXzjYoGXg908i1uYWHKTh0f0CttXDVVfOtcdbGduON+0+6S UIYShj1rUbqvND1D4mzExEhJUXUNN19uv4HWvwhe1TMUJk119i7CXDTrlECn6xRm8s4p h0gcSTKLaJKJMB3W5UkU6qslo8PqilR9ToRU0btn6leP+x9hCyk4x2W2iZCuwk/Mt5c8 mZXz3fAC5FUoQW9WUN/DuMc0sVnhqRgitNXCDH+Gow4DT9n+YQ21F/2KqA6VxSBYDtyl XL+Q== 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 t19-20020a056a0021d300b0068a3eaf68d3si9880575pfj.393.2023.08.29.04.36.51; Tue, 29 Aug 2023 04:37:08 -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 S230369AbjH2LO4 (ORCPT + 99 others); Tue, 29 Aug 2023 07:14:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230219AbjH2LOy (ORCPT ); Tue, 29 Aug 2023 07:14:54 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C4365BD; Tue, 29 Aug 2023 04:14:50 -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 230A32F4; Tue, 29 Aug 2023 04:15:29 -0700 (PDT) Received: from [10.57.3.159] (unknown [10.57.3.159]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A92603F738; Tue, 29 Aug 2023 04:14:46 -0700 (PDT) Message-ID: Date: Tue, 29 Aug 2023 12:14:44 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Thunderbird/102.14.0 Subject: Re: [PATCH] media: videobuf2-dma-sg: limit the sg segment size Content-Language: en-GB To: Tomasz Figa , Anle Pan , Christoph Hellwig Cc: m.szyprowski@samsung.com, mchehab@kernel.org, linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, hui.fang@nxp.com References: <20230828075420.2009568-1-anle.pan@nxp.com> From: Robin Murphy In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-5.4 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE 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 On 2023-08-29 11:03, Tomasz Figa wrote: > Hi Anle, > > On Mon, Aug 28, 2023 at 8:57 AM Anle Pan wrote: >> >> When allocating from pages, the size of the sg segment is unlimited and >> the default is UINT_MAX. This will cause the DMA stream mapping failed >> later with a "swiotlb buffer full" error. > > Thanks for the patch. Good catch. > >> The default maximum mapping >> size is 128 slots x 2K = 256K, determined by "IO_TLB_SEGSIZE". >> To fix the issue, limit the sg segment size according to >> "dma_max_mapping_size" to match the mapping limit. >> >> Signed-off-by: Anle Pan >> --- >> drivers/media/common/videobuf2/videobuf2-dma-sg.c | 9 +++++++-- >> 1 file changed, 7 insertions(+), 2 deletions(-) >> >> diff --git a/drivers/media/common/videobuf2/videobuf2-dma-sg.c b/drivers/media/common/videobuf2/videobuf2-dma-sg.c >> index fa69158a65b1..b608a7c5f240 100644 >> --- a/drivers/media/common/videobuf2/videobuf2-dma-sg.c >> +++ b/drivers/media/common/videobuf2/videobuf2-dma-sg.c >> @@ -105,6 +105,7 @@ static void *vb2_dma_sg_alloc(struct vb2_buffer *vb, struct device *dev, >> struct sg_table *sgt; >> int ret; >> int num_pages; >> + size_t max_segment = 0; >> >> if (WARN_ON(!dev) || WARN_ON(!size)) >> return ERR_PTR(-EINVAL); >> @@ -134,8 +135,12 @@ static void *vb2_dma_sg_alloc(struct vb2_buffer *vb, struct device *dev, >> if (ret) >> goto fail_pages_alloc; >> >> - ret = sg_alloc_table_from_pages(buf->dma_sgt, buf->pages, >> - buf->num_pages, 0, size, GFP_KERNEL); >> + if (dev) dev can't be NULL, see the context above. >> + max_segment = dma_max_mapping_size(dev); >> + if (max_segment == 0) >> + max_segment = UINT_MAX; >> + ret = sg_alloc_table_from_pages_segment(buf->dma_sgt, buf->pages, >> + buf->num_pages, 0, size, max_segment, GFP_KERNEL); > > One thing that I'm not sure about here is that we use > sg_alloc_table_from_pages_segment(), but we actually don't pass the > max segment size (as returned by dma_get_max_seg_size()) to it. > I'm also not exactly sure what's the difference between "max mapping > size" and "max seg size". > +Robin Murphy +Christoph Hellwig I think we could benefit from your > expertise here. dma_get_max_seg_size() represents a capability of the device itself, namely the largest contiguous range it can be programmed to access in a single DMA descriptor/register/whatever. Conversely, dma_max_mapping_size() is a capablity of the DMA API implementation, and represents the largest contiguous mapping it is guaranteed to be able to handle (each segment in the case of dma_map_sg(), or the whole thing for dma_map_page()). Most likely the thing you want here is min_not_zero(max_seg_size, max_mapping_size). > Generally looking at videobuf2-dma-sg, I feel like we would benefit > from some kind of dma_alloc_table_from_pages() that simply takes the > struct dev pointer and does everything necessary. Possibly; this code already looks lifted from drm_prime_pages_to_sg(), and if it's needed here then presumably vb2_dma_sg_get_userptr() also needs it, at the very least. Thanks, Robin.