Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp5626967pxb; Mon, 28 Mar 2022 15:09:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwcstSpyId2fEGaFnD5CQXb/NZ1ohGCZPohjIkyS533jhAL1Rvs7nLXvXwyhvCm4GUm/HlY X-Received: by 2002:a17:90a:d584:b0:1b8:7864:1735 with SMTP id v4-20020a17090ad58400b001b878641735mr1253634pju.126.1648505389419; Mon, 28 Mar 2022 15:09:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648505389; cv=none; d=google.com; s=arc-20160816; b=fNxAEqaZ6Z6icbPZHRfBbJpu0CHIQ0pRVI5b7ZB734Ger3fjsfQWCCzCPc1hj5ZTxQ pMum/+9WXZiR64g8QhJIAHhbWMb76JzQ/xSJ6szrbxKx6/Frsdr4YE+MYnp4aJEES4Qk koNC1jDH0TgmzBzYIAud8oSZ7O5gs4kWaiWx98HgjfBKWMm5CABQ8IHy/9PizBKnbjF6 4KNq12IoZLfa4/lfL/X5fOCtOJTWJhzXtHf9K/DGh28+i19BGEx82mDHYQYW4a12JSmm rZtr6Vzj39Qe7wmIRUnW7CjllOAm6zbR0SsQzaOVXU543E1qGvEvM2ggykplZ7yIONQL A9RQ== 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 :references:in-reply-to:message-id:cc:to:subject:from:date :dkim-signature; bh=E+cqGEZiCyNFXb1DlqK39vg2G0PuPMX9KR99cDdWHLk=; b=Gx20mrp2/f9o3ct2hj8piuKamY4DAq1n561cOjVx+GM/VF6kJ66Vk/dW/m2X+eldYf mGgz+Ixzoe+wxGeRcSXDB6XrNc+Elp4LHbt2d+XPM+GSWV9SnQ9fRODfOdCa5gJaslYr jStjWPzRNuTzNstTY9uxc7IyB4F2arj9eDjok92NX8h7iprr19TwEIWQ2galcBZiwPa1 OXnIl2kUkndslBqGkYmrs8y5OAugGDZS0umdxlu7ms+ZrEdYrgblTYNwMC7ix/f6fES8 kU854+czeN3PcvlR7QHZXClBCvHJxOK0WkaRtjDLpx1/XkkLShbfvhhVa/zIDUpKl8Zq cdkg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b=LL0gwwmF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id 34-20020a630c62000000b003816043f178si15291574pgm.877.2022.03.28.15.09.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Mar 2022 15:09:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b=LL0gwwmF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3F33A15405A; Mon, 28 Mar 2022 14:30:25 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245300AbiC1SqN (ORCPT + 99 others); Mon, 28 Mar 2022 14:46:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234951AbiC1SqM (ORCPT ); Mon, 28 Mar 2022 14:46:12 -0400 Received: from aposti.net (aposti.net [89.234.176.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E97499FF3; Mon, 28 Mar 2022 11:44:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1648493069; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=E+cqGEZiCyNFXb1DlqK39vg2G0PuPMX9KR99cDdWHLk=; b=LL0gwwmFPaVCDLN63nj97SV8oE9td/YB8pb3c+SHc3niSGoqrU73qGtBVCy+MmUIG6nSiM 0DgiIhhqd4jQGqqqYcMpBLuTjcmSF1lY+v7zj9KoBa3PN+vEP7wJYRlwegAxZaTZViBB0G 5/0i5FSBvM/BXDB7WyJHu0xdGDAvGkc= Date: Mon, 28 Mar 2022 19:44:19 +0100 From: Paul Cercueil Subject: Re: [PATCH v2 05/12] iio: core: Add new DMABUF interface infrastructure To: Jonathan Cameron Cc: Michael Hennerich , Lars-Peter Clausen , Christian =?iso-8859-1?b?S/ZuaWc=?= , Sumit Semwal , Jonathan Corbet , Alexandru Ardelean , dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org Message-Id: In-Reply-To: <20220328183701.02884cc3@jic23-huawei> References: <20220207125933.81634-1-paul@crapouillou.net> <20220207125933.81634-6-paul@crapouillou.net> <20220328183701.02884cc3@jic23-huawei> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 Hi Jonathan, Le lun., mars 28 2022 at 18:37:01 +0100, Jonathan Cameron=20 a =E9crit : > On Mon, 7 Feb 2022 12:59:26 +0000 > Paul Cercueil wrote: >=20 >> Add the necessary infrastructure to the IIO core to support a new >> optional DMABUF based interface. >>=20 >> The advantage of this new DMABUF based interface vs. the read() >> interface, is that it avoids an extra copy of the data between the >> kernel and userspace. This is particularly userful for high-speed >=20 > useful >=20 >> devices which produce several megabytes or even gigabytes of data=20 >> per >> second. >>=20 >> The data in this new DMABUF interface is managed at the granularity=20 >> of >> DMABUF objects. Reducing the granularity from byte level to block=20 >> level >> is done to reduce the userspace-kernelspace synchronization overhead >> since performing syscalls for each byte at a few Mbps is just not >> feasible. >>=20 >> This of course leads to a slightly increased latency. For this=20 >> reason an >> application can choose the size of the DMABUFs as well as how many=20 >> it >> allocates. E.g. two DMABUFs would be a traditional double buffering >> scheme. But using a higher number might be necessary to avoid >> underflow/overflow situations in the presence of scheduling=20 >> latencies. >>=20 >> As part of the interface, 2 new IOCTLs have been added: >>=20 >> IIO_BUFFER_DMABUF_ALLOC_IOCTL(struct iio_dmabuf_alloc_req *): >> Each call will allocate a new DMABUF object. The return value (if=20 >> not >> a negative errno value as error) will be the file descriptor of=20 >> the new >> DMABUF. >>=20 >> IIO_BUFFER_DMABUF_ENQUEUE_IOCTL(struct iio_dmabuf *): >> Place the DMABUF object into the queue pending for hardware=20 >> process. >>=20 >> These two IOCTLs have to be performed on the IIO buffer's file >> descriptor, obtained using the IIO_BUFFER_GET_FD_IOCTL() ioctl. >=20 > Just to check, do they work on the old deprecated chardev route?=20 > Normally > we can directly access the first buffer without the ioctl. They do not. I think it's fine this way, since as you said, the old=20 chardev route is deprecated. But I can add support for it with enough=20 peer pressure. >>=20 >> To access the data stored in a block by userspace the block must be >> mapped to the process's memory. This is done by calling mmap() on=20 >> the >> DMABUF's file descriptor. >>=20 >> Before accessing the data through the map, you must use the >> DMA_BUF_IOCTL_SYNC(struct dma_buf_sync *) ioctl, with the >> DMA_BUF_SYNC_START flag, to make sure that the data is available. >> This call may block until the hardware is done with this block. Once >> you are done reading or writing the data, you must use this ioctl=20 >> again >> with the DMA_BUF_SYNC_END flag, before enqueueing the DMABUF to the >> kernel's queue. >>=20 >> If you need to know when the hardware is done with a DMABUF, you can >> poll its file descriptor for the EPOLLOUT event. >>=20 >> Finally, to destroy a DMABUF object, simply call close() on its file >> descriptor. >>=20 >> A typical workflow for the new interface is: >>=20 >> for block in blocks: >> DMABUF_ALLOC block >> mmap block >>=20 >> enable buffer >>=20 >> while !done >> for block in blocks: >> DMABUF_ENQUEUE block >>=20 >> DMABUF_SYNC_START block >> process data >> DMABUF_SYNC_END block >>=20 >> disable buffer >>=20 >> for block in blocks: >> close block >=20 > Given my very limited knowledge of dma-buf, I'll leave commenting > on the flow to others who know if this looks 'standards' or not ;) >=20 > Code looks sane to me.. Thanks. Cheers, -Paul >>=20 >> v2: Only allow the new IOCTLs on the buffer FD created with >> IIO_BUFFER_GET_FD_IOCTL(). >>=20 >> Signed-off-by: Paul Cercueil >> --- >> drivers/iio/industrialio-buffer.c | 55=20 >> +++++++++++++++++++++++++++++++ >> include/linux/iio/buffer_impl.h | 8 +++++ >> include/uapi/linux/iio/buffer.h | 29 ++++++++++++++++ >> 3 files changed, 92 insertions(+) >>=20 >> diff --git a/drivers/iio/industrialio-buffer.c=20 >> b/drivers/iio/industrialio-buffer.c >> index 94eb9f6cf128..72f333a519bc 100644 >> --- a/drivers/iio/industrialio-buffer.c >> +++ b/drivers/iio/industrialio-buffer.c >> @@ -17,6 +17,7 @@ >> #include >> #include >> #include >> +#include >> #include >> #include >>=20 >> @@ -1520,11 +1521,65 @@ static int iio_buffer_chrdev_release(struct=20 >> inode *inode, struct file *filep) >> return 0; >> } >>=20 >> +static int iio_buffer_enqueue_dmabuf(struct iio_buffer *buffer, >> + struct iio_dmabuf __user *user_buf) >> +{ >> + struct iio_dmabuf dmabuf; >> + >> + if (!buffer->access->enqueue_dmabuf) >> + return -EPERM; >> + >> + if (copy_from_user(&dmabuf, user_buf, sizeof(dmabuf))) >> + return -EFAULT; >> + >> + if (dmabuf.flags & ~IIO_BUFFER_DMABUF_SUPPORTED_FLAGS) >> + return -EINVAL; >> + >> + return buffer->access->enqueue_dmabuf(buffer, &dmabuf); >> +} >> + >> +static int iio_buffer_alloc_dmabuf(struct iio_buffer *buffer, >> + struct iio_dmabuf_alloc_req __user *user_req) >> +{ >> + struct iio_dmabuf_alloc_req req; >> + >> + if (!buffer->access->alloc_dmabuf) >> + return -EPERM; >> + >> + if (copy_from_user(&req, user_req, sizeof(req))) >> + return -EFAULT; >> + >> + if (req.resv) >> + return -EINVAL; >> + >> + return buffer->access->alloc_dmabuf(buffer, &req); >> +} >> + >> +static long iio_buffer_chrdev_ioctl(struct file *filp, >> + unsigned int cmd, unsigned long arg) >> +{ >> + struct iio_dev_buffer_pair *ib =3D filp->private_data; >> + struct iio_buffer *buffer =3D ib->buffer; >> + void __user *_arg =3D (void __user *)arg; >> + >> + switch (cmd) { >> + case IIO_BUFFER_DMABUF_ALLOC_IOCTL: >> + return iio_buffer_alloc_dmabuf(buffer, _arg); >> + case IIO_BUFFER_DMABUF_ENQUEUE_IOCTL: >> + /* TODO: support non-blocking enqueue operation */ >> + return iio_buffer_enqueue_dmabuf(buffer, _arg); >> + default: >> + return IIO_IOCTL_UNHANDLED; >> + } >> +} >> + >> static const struct file_operations iio_buffer_chrdev_fileops =3D { >> .owner =3D THIS_MODULE, >> .llseek =3D noop_llseek, >> .read =3D iio_buffer_read, >> .write =3D iio_buffer_write, >> + .unlocked_ioctl =3D iio_buffer_chrdev_ioctl, >> + .compat_ioctl =3D compat_ptr_ioctl, >> .poll =3D iio_buffer_poll, >> .release =3D iio_buffer_chrdev_release, >> }; >> diff --git a/include/linux/iio/buffer_impl.h=20 >> b/include/linux/iio/buffer_impl.h >> index e2ca8ea23e19..728541bc2c63 100644 >> --- a/include/linux/iio/buffer_impl.h >> +++ b/include/linux/iio/buffer_impl.h >> @@ -39,6 +39,9 @@ struct iio_buffer; >> * device stops sampling. Calles are balanced=20 >> with @enable. >> * @release: called when the last reference to the buffer is=20 >> dropped, >> * should free all resources allocated by the buffer. >> + * @alloc_dmabuf: called from userspace via ioctl to allocate one=20 >> DMABUF. >> + * @enqueue_dmabuf: called from userspace via ioctl to queue this=20 >> DMABUF >> + * object to this buffer. Requires a valid DMABUF fd. >> * @modes: Supported operating modes by this buffer type >> * @flags: A bitmask combination of INDIO_BUFFER_FLAG_* >> * >> @@ -68,6 +71,11 @@ struct iio_buffer_access_funcs { >>=20 >> void (*release)(struct iio_buffer *buffer); >>=20 >> + int (*alloc_dmabuf)(struct iio_buffer *buffer, >> + struct iio_dmabuf_alloc_req *req); >> + int (*enqueue_dmabuf)(struct iio_buffer *buffer, >> + struct iio_dmabuf *block); >> + >> unsigned int modes; >> unsigned int flags; >> }; >> diff --git a/include/uapi/linux/iio/buffer.h=20 >> b/include/uapi/linux/iio/buffer.h >> index 13939032b3f6..e4621b926262 100644 >> --- a/include/uapi/linux/iio/buffer.h >> +++ b/include/uapi/linux/iio/buffer.h >> @@ -5,6 +5,35 @@ >> #ifndef _UAPI_IIO_BUFFER_H_ >> #define _UAPI_IIO_BUFFER_H_ >>=20 >> +#include >> + >> +#define IIO_BUFFER_DMABUF_SUPPORTED_FLAGS 0x00000000 >> + >> +/** >> + * struct iio_dmabuf_alloc_req - Descriptor for allocating IIO=20 >> DMABUFs >> + * @size: the size of a single DMABUF >> + * @resv: reserved >> + */ >> +struct iio_dmabuf_alloc_req { >> + __u64 size; >> + __u64 resv; >> +}; >> + >> +/** >> + * struct iio_dmabuf - Descriptor for a single IIO DMABUF object >> + * @fd: file descriptor of the DMABUF object >> + * @flags: one or more IIO_BUFFER_DMABUF_* flags >> + * @bytes_used: number of bytes used in this DMABUF for the data=20 >> transfer. >> + * If zero, the full buffer is used. >> + */ >> +struct iio_dmabuf { >> + __u32 fd; >> + __u32 flags; >> + __u64 bytes_used; >> +}; >> + >> #define IIO_BUFFER_GET_FD_IOCTL _IOWR('i', 0x91, int) >> +#define IIO_BUFFER_DMABUF_ALLOC_IOCTL _IOW('i', 0x92, struct=20 >> iio_dmabuf_alloc_req) >> +#define IIO_BUFFER_DMABUF_ENQUEUE_IOCTL _IOW('i', 0x93, struct=20 >> iio_dmabuf) >>=20 >> #endif /* _UAPI_IIO_BUFFER_H_ */ >=20