From patchwork Thu Nov 17 04:58:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukasz Wiecaszek X-Patchwork-Id: 21432 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp216627wrr; Wed, 16 Nov 2022 21:02:17 -0800 (PST) X-Google-Smtp-Source: AA0mqf6FGUL1NarrCY/04GC8EdYJNvJhNxCS8oaxsWY+4HKQNBJqSJHYXkQdvyI5Ns8rW6hx1cne X-Received: by 2002:a17:903:3052:b0:174:af35:4b90 with SMTP id u18-20020a170903305200b00174af354b90mr1257447pla.8.1668661337354; Wed, 16 Nov 2022 21:02:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668661337; cv=none; d=google.com; s=arc-20160816; b=04CncpBu0GssScY8zYqoz0IpgZytvkWyNhjRCRs7cIB6CPIjnrtvnhN00SBA/HusyQ sGOnRpDYWJxbUHMbu8U9bvhWnAmFAztSob/Pk185KQ7ZdF8hj0D79P5YR8g7V0CohaRN LfeIwrTxuLtD4fv6bkGQ+CFfPWad/ZQtXsMSrC5inxDHgRjX89PToJDQWhZE3Ys0mqvc 9X+zBmjnkFC1/2ECQOZ5KPr++1xvg3Qyj6TE5iv3wlm3WgS8IXVSKg9RcA5L72t6J8E7 hB2rjN4VQQp8BhhtQpYWFfkEH/i/XjjUvUimRCwrdhto5NgtjN4hBJlH9N0w49cnXg5U 4BaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=a0T3rjzlpFW1dw7fmW7g8x4Gy61zgYU1JKH4cxrgdu0=; b=H3ZOGSMyr+2wvMJQ9627W5ndHm46um3zbu77WQhpJYNn3u5F1HeQ5ORjxH8vWKwCdd Z9LLnjQIjRct2Zadi8eAyZ/eGYsJ1mUYT8sKtqvrSvzH2gYaP9NHiUhlzDuQmPlYV5ET XH5c6CSnoJUFvABKawsy7nm9QcpFIjvctskUwOLjGw60je4SXcaKr2j/PpwTacRO/ung D1EuEKp1PS1IthdfgM+9ZVr8iTG5JtotvGDMDbbAwjdFl9OlWZitbgaFhaLlR89k3WTh DGDpOzT+Ip7JO9T56eLsGg6o0JkB4/m3z7ix2IlDxjfdtpMErO7dH+TJgGYqSSFAcGPY dxDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@googlemail.com header.s=20210112 header.b=JY4NfYAI; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x186-20020a6386c3000000b00474673969f8si78074pgd.154.2022.11.16.21.02.03; Wed, 16 Nov 2022 21:02:17 -0800 (PST) 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=@googlemail.com header.s=20210112 header.b=JY4NfYAI; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229943AbiKQFAQ (ORCPT + 99 others); Thu, 17 Nov 2022 00:00:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229451AbiKQFAM (ORCPT ); Thu, 17 Nov 2022 00:00:12 -0500 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE7D0326EC; Wed, 16 Nov 2022 21:00:10 -0800 (PST) Received: by mail-wm1-x32e.google.com with SMTP id t4so538487wmj.5; Wed, 16 Nov 2022 21:00:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=a0T3rjzlpFW1dw7fmW7g8x4Gy61zgYU1JKH4cxrgdu0=; b=JY4NfYAIbFCUofUSLplovnKa/5072s2jFLYAMFlFQTFUbXFj+lybMXgGHevupGBX7p QaVDIl/1Y2+6v9KgYyGo6gU/j0kak1gX8EHWtw8/22y1EJlPQ84PglSV4mSujDZPqPfE bp3FcH5y1InMVpXhOZp7npliwhHmefhHL9noUixdN5Sxh1zcfHcs4Ndrddj8P1q1gfC+ SZn3/z4rgmLG2ve4UMuQKtujwgYniEUEo4KGPjXNrROT68k7dYpw7KdSh8Blzxu9QjMn OkZWoM17fmFmuMiE1wkw+1FKoBbY4E9MJROa6sEbJkaC9L+ADXsgWz84KfEITl3HmSqP iojA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=a0T3rjzlpFW1dw7fmW7g8x4Gy61zgYU1JKH4cxrgdu0=; b=GhNr99AFOY4XTOZ1e57jfv0v220NmM3W0t8lKFPc/0/SSk3qPJDPJfIi6WI2tTNnGn cfQOuLigs1v52iwPep1A8Cp6E+M9f2dfRtf9+5wKotcS+2LoEQqjTCfx1AMDW2tworVE TtqV3LKL2LawMYyz31UfSSVBDVC5lYLL74PrQV4a5NmNPaF6OwEXyjOWgTMtcs4vUEDT EQlr55HpKNv2q2NeEwJmqbZS6OcyYZEFn6pExCujCjtXMQAuf2bD+oOTJ5sum+OWcaVw IpUN7jZi6C1Em/6Sg/Bo3WC4IarfbC+FSiCN89rGS+xBktoBAGHnIL5M2BZnBDGDeeRu JNsw== X-Gm-Message-State: ANoB5pmlZcR9FuagEhByEKzolon0pW5H0UurUpZ90GcsRHeMpnom8TuW yFPS5BMfTD5wlYzLul7M0Rk= X-Received: by 2002:a05:600c:5010:b0:3cf:b067:416c with SMTP id n16-20020a05600c501000b003cfb067416cmr4064198wmr.134.1668661209301; Wed, 16 Nov 2022 21:00:09 -0800 (PST) Received: from localhost.localdomain (user-5-173-65-115.play-internet.pl. [5.173.65.115]) by smtp.googlemail.com with ESMTPSA id q10-20020adff94a000000b0022cc6b8df5esm16512143wrr.7.2022.11.16.21.00.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Nov 2022 21:00:08 -0800 (PST) From: Lukasz Wiecaszek X-Google-Original-From: Lukasz Wiecaszek To: Gerd Hoffmann Cc: Dmitry Osipenko , Lukasz Wiecaszek , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= , dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org Subject: [PATCH v4] udmabuf: add vmap and vunmap methods to udmabuf_ops Date: Thu, 17 Nov 2022 05:58:41 +0100 Message-Id: <20221117045842.27161-1-lukasz.wiecaszek@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749594020901766981?= X-GMAIL-MSGID: =?utf-8?q?1749718230257910508?= The reason behind that patch is associated with videobuf2 subsystem (or more genrally with v4l2 framework) and user created dma buffers (udmabuf). In some circumstances when dealing with V4L2_MEMORY_DMABUF buffers videobuf2 subsystem wants to use dma_buf_vmap() method on the attached dma buffer. As udmabuf does not have .vmap operation implemented, such dma_buf_vmap() natually fails. videobuf2_common: __vb2_queue_alloc: allocated 3 buffers, 1 plane(s) each videobuf2_common: __prepare_dmabuf: buffer for plane 0 changed videobuf2_common: __prepare_dmabuf: failed to map dmabuf for plane 0 videobuf2_common: __buf_prepare: buffer preparation failed: -14 The patch itself seems to be strighforward. It adds implementation of .vmap and .vunmap methods to 'struct dma_buf_ops udmabuf_ops'. .vmap method itself uses vm_map_ram() to map pages linearly into the kernel virtual address space. .vunmap removes mapping created earlier by .vmap. All locking and 'vmapping counting' is done in dma_buf.c so it seems to be redundant/unnecessary in .vmap/.vunmap. Signed-off-by: Lukasz Wiecaszek --- v1: https://lore.kernel.org/linux-media/202211120352.G7WPASoP-lkp@intel.com/T/#t v2: https://lore.kernel.org/linux-media/20221114052944.GA7264@thinkpad-p72/T/#t v3: https://lore.kernel.org/linux-media/4f92e95f-a0dc-4eac-4c08-0df85de78ae7@collabora.com/T/#t v3 -> v4: Removed line/info 'reported by kernel test robot' v2 -> v3: Added .vunmap to 'struct dma_buf_ops udmabuf_ops' v1 -> v2: Patch prepared and tested against 6.1.0-rc2+ drivers/dma-buf/udmabuf.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 283816fbd72f..740d6e426ee9 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -13,6 +13,8 @@ #include #include #include +#include +#include static int list_limit = 1024; module_param(list_limit, int, 0644); @@ -60,6 +62,30 @@ static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma) return 0; } +static int vmap_udmabuf(struct dma_buf *buf, struct iosys_map *map) +{ + struct udmabuf *ubuf = buf->priv; + void *vaddr; + + dma_resv_assert_held(buf->resv); + + vaddr = vm_map_ram(ubuf->pages, ubuf->pagecount, -1); + if (!vaddr) + return -EINVAL; + + iosys_map_set_vaddr(map, vaddr); + return 0; +} + +static void vunmap_udmabuf(struct dma_buf *buf, struct iosys_map *map) +{ + struct udmabuf *ubuf = buf->priv; + + dma_resv_assert_held(buf->resv); + + vm_unmap_ram(map->vaddr, ubuf->pagecount); +} + static struct sg_table *get_sg_table(struct device *dev, struct dma_buf *buf, enum dma_data_direction direction) { @@ -162,6 +188,8 @@ static const struct dma_buf_ops udmabuf_ops = { .unmap_dma_buf = unmap_udmabuf, .release = release_udmabuf, .mmap = mmap_udmabuf, + .vmap = vmap_udmabuf, + .vunmap = vunmap_udmabuf, .begin_cpu_access = begin_cpu_udmabuf, .end_cpu_access = end_cpu_udmabuf, };