* Re: [PATCH v2 3/3] usb: gadget: functionfs: Add DMABUF import interface
@ 2023-03-28 6:40 kernel test robot
0 siblings, 0 replies; 3+ messages in thread
From: kernel test robot @ 2023-03-28 6:40 UTC (permalink / raw)
To: oe-kbuild; +Cc: lkp
::::::
:::::: Manual check reason: "low confidence static check warning: drivers/usb/gadget/function/f_fs.c:1325:38: warning: Use of memory after it is freed [clang-analyzer-unix.Malloc]"
::::::
BCC: lkp@intel.com
CC: llvm@lists.linux.dev
CC: oe-kbuild-all@lists.linux.dev
In-Reply-To: <20230322092118.9213-4-paul@crapouillou.net>
References: <20230322092118.9213-4-paul@crapouillou.net>
TO: Paul Cercueil <paul@crapouillou.net>
TO: "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>
TO: Sumit Semwal <sumit.semwal@linaro.org>
TO: "Christian König" <christian.koenig@amd.com>
CC: Paul Cercueil <paul@crapouillou.net>
CC: michael.hennerich@analog.com
CC: linux-usb@vger.kernel.org
CC: linux-kernel@vger.kernel.org
CC: dri-devel@lists.freedesktop.org
CC: nuno.sa@analog.com
CC: linaro-mm-sig@lists.linaro.org
CC: linux-media@vger.kernel.org
Hi Paul,
I love your patch! Perhaps something to improve:
[auto build test WARNING on usb/usb-testing]
[also build test WARNING on usb/usb-next usb/usb-linus linus/master v6.3-rc4 next-20230327]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Paul-Cercueil/usb-gadget-Support-already-mapped-DMA-SGs/20230322-172408
base: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
patch link: https://lore.kernel.org/r/20230322092118.9213-4-paul%40crapouillou.net
patch subject: [PATCH v2 3/3] usb: gadget: functionfs: Add DMABUF import interface
:::::: branch date: 6 days ago
:::::: commit date: 6 days ago
config: riscv-randconfig-c006-20230326 (https://download.01.org/0day-ci/archive/20230328/202303281400.NHh6MgV2-lkp@intel.com/config)
compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project 67409911353323ca5edf2049ef0df54132fa1ca7)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install riscv cross compiling tool for clang build
# apt-get install binutils-riscv64-linux-gnu
# https://github.com/intel-lab-lkp/linux/commit/80c5660d9135823b24d5d7adf0e32e201189822e
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Paul-Cercueil/usb-gadget-Support-already-mapped-DMA-SGs/20230322-172408
git checkout 80c5660d9135823b24d5d7adf0e32e201189822e
# save the config file
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=riscv clang-analyzer olddefconfig
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=riscv clang-analyzer
If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>
| Link: https://lore.kernel.org/r/202303281400.NHh6MgV2-lkp@intel.com/
clang_analyzer warnings: (new ones prefixed by >>)
drivers/usb/gadget/function/rndis.c:1113:14: note: Value stored to 's' during its initialization is never read
({ char *s = "?";
^ ~~~
Suppressed 2 warnings (2 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
7 warnings generated.
drivers/usb/gadget/function/f_mass_storage.c:932:2: warning: Value stored to 'rc' is never read [clang-analyzer-deadcode.DeadStores]
rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/usb/gadget/function/f_mass_storage.c:932:2: note: Value stored to 'rc' is never read
rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/usb/gadget/function/f_mass_storage.c:1664:3: warning: Value stored to 'sdinfo' is never read [clang-analyzer-deadcode.DeadStores]
sdinfo = curlun->sense_data_info;
^ ~~~~~~~~~~~~~~~~~~~~~~~
drivers/usb/gadget/function/f_mass_storage.c:1664:3: note: Value stored to 'sdinfo' is never read
sdinfo = curlun->sense_data_info;
^ ~~~~~~~~~~~~~~~~~~~~~~~
drivers/usb/gadget/function/f_mass_storage.c:1673:3: warning: Value stored to 'sd' is never read [clang-analyzer-deadcode.DeadStores]
sd = SS_INVALID_COMMAND;
^
drivers/usb/gadget/function/f_mass_storage.c:1673:3: note: Value stored to 'sd' is never read
Suppressed 4 warnings (3 in non-user code, 1 with check filters).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
3 warnings generated.
Suppressed 3 warnings (3 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
11 warnings generated.
drivers/usb/gadget/function/f_fs.c:1267:3: warning: Argument to kfree() is the address of the local variable 'io_data', which is not memory allocated by malloc() [clang-analyzer-unix.Malloc]
kfree(p);
^ ~
drivers/usb/gadget/function/f_fs.c:1241:2: note: Loop condition is false. Exiting loop
ENTER();
^
drivers/usb/gadget/function/u_fs.h:35:20: note: expanded from macro 'ENTER'
#define ENTER() pr_vdebug("%s()\n", __func__)
^
drivers/usb/gadget/function/u_fs.h:30:42: note: expanded from macro 'pr_vdebug'
# define pr_vdebug(...) do { } while (0)
^
drivers/usb/gadget/function/f_fs.c:1243:2: note: Taking false branch
if (!is_sync_kiocb(kiocb)) {
^
drivers/usb/gadget/function/f_fs.c:1260:9: note: Field 'aio' is false
if (p->aio)
^
drivers/usb/gadget/function/f_fs.c:1260:2: note: Taking false branch
if (p->aio)
^
drivers/usb/gadget/function/f_fs.c:1264:2: note: Taking false branch
if (res == -EIOCBQUEUED)
^
drivers/usb/gadget/function/f_fs.c:1266:6: note: Assuming field 'aio' is true
if (p->aio)
^~~~~~
drivers/usb/gadget/function/f_fs.c:1266:2: note: Taking true branch
if (p->aio)
^
drivers/usb/gadget/function/f_fs.c:1267:3: note: Argument to kfree() is the address of the local variable 'io_data', which is not memory allocated by malloc()
kfree(p);
^ ~
drivers/usb/gadget/function/f_fs.c:1315:3: warning: Argument to kfree() is the address of the local variable 'io_data', which is not memory allocated by malloc() [clang-analyzer-unix.Malloc]
kfree(p);
^ ~
drivers/usb/gadget/function/f_fs.c:1278:2: note: Loop condition is false. Exiting loop
ENTER();
^
drivers/usb/gadget/function/u_fs.h:35:20: note: expanded from macro 'ENTER'
#define ENTER() pr_vdebug("%s()\n", __func__)
^
drivers/usb/gadget/function/u_fs.h:30:42: note: expanded from macro 'pr_vdebug'
# define pr_vdebug(...) do { } while (0)
^
drivers/usb/gadget/function/f_fs.c:1280:2: note: Taking false branch
if (!is_sync_kiocb(kiocb)) {
^
drivers/usb/gadget/function/f_fs.c:1292:9: note: Field 'aio' is false
if (p->aio) {
^
drivers/usb/gadget/function/f_fs.c:1292:2: note: Taking false branch
if (p->aio) {
^
drivers/usb/gadget/function/f_fs.c:1306:9: note: Field 'aio' is false
if (p->aio)
^
drivers/usb/gadget/function/f_fs.c:1306:2: note: Taking false branch
if (p->aio)
^
drivers/usb/gadget/function/f_fs.c:1310:2: note: Taking false branch
if (res == -EIOCBQUEUED)
^
drivers/usb/gadget/function/f_fs.c:1313:6: note: Assuming field 'aio' is true
if (p->aio) {
^~~~~~
drivers/usb/gadget/function/f_fs.c:1313:2: note: Taking true branch
if (p->aio) {
^
drivers/usb/gadget/function/f_fs.c:1315:3: note: Argument to kfree() is the address of the local variable 'io_data', which is not memory allocated by malloc()
kfree(p);
^ ~
>> drivers/usb/gadget/function/f_fs.c:1325:38: warning: Use of memory after it is freed [clang-analyzer-unix.Malloc]
struct dma_buf_attachment *attach = priv->attach;
^
drivers/usb/gadget/function/f_fs.c:1697:2: note: Loop condition is false. Exiting loop
ENTER();
^
drivers/usb/gadget/function/u_fs.h:35:20: note: expanded from macro 'ENTER'
#define ENTER() pr_vdebug("%s()\n", __func__)
^
drivers/usb/gadget/function/u_fs.h:30:42: note: expanded from macro 'pr_vdebug'
# define pr_vdebug(...) do { } while (0)
^
drivers/usb/gadget/function/f_fs.c:1699:14: note: Assuming field 'state' is equal to FFS_ACTIVE
if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
^
include/asm-generic/bug.h:122:25: note: expanded from macro 'WARN_ON'
int __ret_warn_on = !!(condition); \
^~~~~~~~~
drivers/usb/gadget/function/f_fs.c:1699:6: note: Taking false branch
if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
^
include/asm-generic/bug.h:123:2: note: expanded from macro 'WARN_ON'
if (unlikely(__ret_warn_on)) \
^
drivers/usb/gadget/function/f_fs.c:1699:2: note: Taking false branch
if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
^
drivers/usb/gadget/function/f_fs.c:1702:2: note: Control jumps to 'case 1074030468:' at line 1714
switch (code) {
^
drivers/usb/gadget/function/f_fs.c:1718:7: note: Assuming the condition is false
if (copy_from_user(&fd, (void __user *)value, sizeof(fd))) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/usb/gadget/function/f_fs.c:1718:3: note: Taking false branch
if (copy_from_user(&fd, (void __user *)value, sizeof(fd))) {
^
drivers/usb/gadget/function/f_fs.c:1723:10: note: Calling 'ffs_dmabuf_detach'
return ffs_dmabuf_detach(file, fd);
^~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/usb/gadget/function/f_fs.c:1519:2: note: Taking false branch
if (IS_ERR(dmabuf))
^
drivers/usb/gadget/function/f_fs.c:1524:2: note: Taking false branch
if (IS_ERR(attach)) {
^
drivers/usb/gadget/function/f_fs.c:1529:2: note: Calling 'ffs_dmabuf_put'
ffs_dmabuf_put(attach);
^~~~~~~~~~~~~~~~~~~~~~
drivers/usb/gadget/function/f_fs.c:1347:2: note: Calling 'kref_put'
kref_put(&priv->ref, ffs_dmabuf_release);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/kref.h:64:2: note: Taking true branch
if (refcount_dec_and_test(&kref->refcount)) {
^
include/linux/kref.h:65:3: note: Calling 'ffs_dmabuf_release'
release(kref);
^~~~~~~~~~~~~
drivers/usb/gadget/function/f_fs.c:1328:2: note: Taking false branch
pr_debug("FFS DMABUF release\n");
^
include/linux/printk.h:579:2: note: expanded from macro 'pr_debug'
dynamic_pr_debug(fmt, ##__VA_ARGS__)
^
include/linux/dynamic_debug.h:268:2: note: expanded from macro 'dynamic_pr_debug'
_dynamic_func_call(fmt, __dynamic_pr_debug, \
^
include/linux/dynamic_debug.h:249:2: note: expanded from macro '_dynamic_func_call'
_dynamic_func_call_cls(_DPRINTK_CLASS_DFLT, fmt, func, ##__VA_ARGS__)
^
note: (skipping 4 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
include/linux/compiler_types.h:397:2: note: expanded from macro 'compiletime_assert'
_compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
^
include/linux/compiler_types.h:385:2: note: expanded from macro '_compiletime_assert'
__compiletime_assert(condition, msg, prefix, suffix)
^
include/linux/compiler_types.h:377:3: note: expanded from macro '__compiletime_assert'
if (!(condition)) \
^
drivers/usb/gadget/function/f_fs.c:1328:2: note: Loop condition is false. Exiting loop
pr_debug("FFS DMABUF release\n");
^
include/linux/printk.h:579:2: note: expanded from macro 'pr_debug'
dynamic_pr_debug(fmt, ##__VA_ARGS__)
^
include/linux/dynamic_debug.h:268:2: note: expanded from macro 'dynamic_pr_debug'
_dynamic_func_call(fmt, __dynamic_pr_debug, \
^
include/linux/dynamic_debug.h:249:2: note: expanded from macro '_dynamic_func_call'
_dynamic_func_call_cls(_DPRINTK_CLASS_DFLT, fmt, func, ##__VA_ARGS__)
^
note: (skipping 4 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
include/linux/compiler_types.h:397:2: note: expanded from macro 'compiletime_assert'
_compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
^
include/linux/compiler_types.h:385:2: note: expanded from macro '_compiletime_assert'
__compiletime_assert(condition, msg, prefix, suffix)
^
include/linux/compiler_types.h:369:2: note: expanded from macro '__compiletime_assert'
do { \
^
drivers/usb/gadget/function/f_fs.c:1328:2: note: Taking true branch
pr_debug("FFS DMABUF release\n");
^
include/linux/printk.h:579:2: note: expanded from macro 'pr_debug'
dynamic_pr_debug(fmt, ##__VA_ARGS__)
^
include/linux/dynamic_debug.h:268:2: note: expanded from macro 'dynamic_pr_debug'
_dynamic_func_call(fmt, __dynamic_pr_debug, \
^
include/linux/dynamic_debug.h:249:2: note: expanded from macro '_dynamic_func_call'
_dynamic_func_call_cls(_DPRINTK_CLASS_DFLT, fmt, func, ##__VA_ARGS__)
^
include/linux/dynamic_debug.h:247:2: note: expanded from macro '_dynamic_func_call_cls'
__dynamic_func_call_cls(__UNIQUE_ID(ddebug), cls, fmt, func, ##__VA_ARGS__)
^
include/linux/dynamic_debug.h:222:2: note: expanded from macro '__dynamic_func_call_cls'
if (DYNAMIC_DEBUG_BRANCH(id)) \
^
drivers/usb/gadget/function/f_fs.c:1328:2: note: Loop condition is false. Exiting loop
pr_debug("FFS DMABUF release\n");
^
include/linux/printk.h:579:2: note: expanded from macro 'pr_debug'
dynamic_pr_debug(fmt, ##__VA_ARGS__)
^
include/linux/dynamic_debug.h:268:2: note: expanded from macro 'dynamic_pr_debug'
_dynamic_func_call(fmt, __dynamic_pr_debug, \
^
include/linux/dynamic_debug.h:249:2: note: expanded from macro '_dynamic_func_call'
_dynamic_func_call_cls(_DPRINTK_CLASS_DFLT, fmt, func, ##__VA_ARGS__)
^
include/linux/dynamic_debug.h:247:2: note: expanded from macro '_dynamic_func_call_cls'
__dynamic_func_call_cls(__UNIQUE_ID(ddebug), cls, fmt, func, ##__VA_ARGS__)
^
include/linux/dynamic_debug.h:220:58: note: expanded from macro '__dynamic_func_call_cls'
#define __dynamic_func_call_cls(id, cls, fmt, func, ...) do { \
^
drivers/usb/gadget/function/f_fs.c:1333:2: note: Memory is released
kfree(priv);
^~~~~~~~~~~
include/linux/kref.h:65:3: note: Returning; memory was released
release(kref);
^~~~~~~~~~~~~
drivers/usb/gadget/function/f_fs.c:1347:2: note: Returning; memory was released
kref_put(&priv->ref, ffs_dmabuf_release);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/usb/gadget/function/f_fs.c:1529:2: note: Returning; memory was released
ffs_dmabuf_put(attach);
^~~~~~~~~~~~~~~~~~~~~~
drivers/usb/gadget/function/f_fs.c:1530:2: note: Calling 'ffs_dmabuf_put'
ffs_dmabuf_put(attach);
^~~~~~~~~~~~~~~~~~~~~~
drivers/usb/gadget/function/f_fs.c:1347:2: note: Calling 'kref_put'
kref_put(&priv->ref, ffs_dmabuf_release);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/kref.h:64:2: note: Taking true branch
if (refcount_dec_and_test(&kref->refcount)) {
^
include/linux/kref.h:65:3: note: Calling 'ffs_dmabuf_release'
release(kref);
^~~~~~~~~~~~~
drivers/usb/gadget/function/f_fs.c:1325:38: note: Use of memory after it is freed
struct dma_buf_attachment *attach = priv->attach;
^~~~~~~~~~~~
>> drivers/usb/gadget/function/f_fs.c:1708:4: warning: Value stored to 'ret' is never read [clang-analyzer-deadcode.DeadStores]
ret = -EFAULT;
^ ~~~~~~~
drivers/usb/gadget/function/f_fs.c:1708:4: note: Value stored to 'ret' is never read
ret = -EFAULT;
^ ~~~~~~~
drivers/usb/gadget/function/f_fs.c:1719:4: warning: Value stored to 'ret' is never read [clang-analyzer-deadcode.DeadStores]
ret = -EFAULT;
^ ~~~~~~~
drivers/usb/gadget/function/f_fs.c:1719:4: note: Value stored to 'ret' is never read
ret = -EFAULT;
^ ~~~~~~~
drivers/usb/gadget/function/f_fs.c:1730:4: warning: Value stored to 'ret' is never read [clang-analyzer-deadcode.DeadStores]
ret = -EFAULT;
^ ~~~~~~~
drivers/usb/gadget/function/f_fs.c:1730:4: note: Value stored to 'ret' is never read
ret = -EFAULT;
^ ~~~~~~~
include/linux/spinlock.h:325:2: warning: Use of memory after it is freed [clang-analyzer-unix.Malloc]
return &lock->rlock;
^
drivers/usb/gadget/function/f_fs.c:4044:2: note: Loop condition is false. Exiting loop
ENTER();
^
drivers/usb/gadget/function/u_fs.h:35:20: note: expanded from macro 'ENTER'
#define ENTER() pr_vdebug("%s()\n", __func__)
^
drivers/usb/gadget/function/u_fs.h:30:42: note: expanded from macro 'pr_vdebug'
# define pr_vdebug(...) do { } while (0)
^
drivers/usb/gadget/function/f_fs.c:4045:6: note: Assuming 'func' is not equal to field 'func'
if (ffs->func == func) {
^~~~~~~~~~~~~~~~~
drivers/usb/gadget/function/f_fs.c:4045:2: note: Taking false branch
if (ffs->func == func) {
^
drivers/usb/gadget/function/f_fs.c:4053:6: note: Assuming the condition is true
if (!--opts->refcnt)
^~~~~~~~~~~~~~~
drivers/usb/gadget/function/f_fs.c:4053:2: note: Taking true branch
if (!--opts->refcnt)
^
drivers/usb/gadget/function/f_fs.c:4054:3: note: Calling 'functionfs_unbind'
functionfs_unbind(ffs);
^~~~~~~~~~~~~~~~~~~~~~
drivers/usb/gadget/function/f_fs.c:2323:2: note: Loop condition is false. Exiting loop
ENTER();
^
drivers/usb/gadget/function/u_fs.h:35:20: note: expanded from macro 'ENTER'
#define ENTER() pr_vdebug("%s()\n", __func__)
^
drivers/usb/gadget/function/u_fs.h:30:42: note: expanded from macro 'pr_vdebug'
# define pr_vdebug(...) do { } while (0)
^
drivers/usb/gadget/function/f_fs.c:2325:15: note: Assuming field 'gadget' is non-null
if (!WARN_ON(!ffs->gadget)) {
^
include/asm-generic/bug.h:122:25: note: expanded from macro 'WARN_ON'
int __ret_warn_on = !!(condition); \
^~~~~~~~~
drivers/usb/gadget/function/f_fs.c:2325:7: note: Taking false branch
if (!WARN_ON(!ffs->gadget)) {
^
include/asm-generic/bug.h:123:2: note: expanded from macro 'WARN_ON'
if (unlikely(__ret_warn_on)) \
^
drivers/usb/gadget/function/f_fs.c:2325:2: note: Taking true branch
if (!WARN_ON(!ffs->gadget)) {
^
drivers/usb/gadget/function/f_fs.c:2334:3: note: Calling 'ffs_data_put'
ffs_data_put(ffs);
^~~~~~~~~~~~~~~~~
drivers/usb/gadget/function/f_fs.c:2138:2: note: Loop condition is false. Exiting loop
ENTER();
^
drivers/usb/gadget/function/u_fs.h:35:20: note: expanded from macro 'ENTER'
#define ENTER() pr_vdebug("%s()\n", __func__)
^
drivers/usb/gadget/function/u_fs.h:30:42: note: expanded from macro 'pr_vdebug'
# define pr_vdebug(...) do { } while (0)
^
drivers/usb/gadget/function/f_fs.c:2140:2: note: Taking true branch
if (refcount_dec_and_test(&ffs->ref)) {
^
drivers/usb/gadget/function/f_fs.c:2141:3: note: Loop condition is false. Exiting loop
pr_info("%s(): freeing\n", __func__);
^
include/linux/printk.h:528:2: note: expanded from macro 'pr_info'
printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
^
include/linux/printk.h:455:26: note: expanded from macro 'printk'
#define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
^
include/linux/printk.h:426:3: note: expanded from macro 'printk_index_wrap'
__printk_index_emit(_fmt, NULL, NULL); \
^
include/linux/printk.h:401:34: note: expanded from macro '__printk_index_emit'
#define __printk_index_emit(...) do {} while (0)
^
drivers/usb/gadget/function/f_fs.c:2144:10: note: Assuming the condition is true
BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
vim +1325 drivers/usb/gadget/function/f_fs.c
2e4c7553cd6f9c6 drivers/usb/gadget/f_fs.c Robert Baldyga 2014-02-10 1321
80c5660d9135823 drivers/usb/gadget/function/f_fs.c Paul Cercueil 2023-03-22 1322 static void ffs_dmabuf_release(struct kref *ref)
80c5660d9135823 drivers/usb/gadget/function/f_fs.c Paul Cercueil 2023-03-22 1323 {
80c5660d9135823 drivers/usb/gadget/function/f_fs.c Paul Cercueil 2023-03-22 1324 struct ffs_dmabuf_priv *priv = container_of(ref, struct ffs_dmabuf_priv, ref);
80c5660d9135823 drivers/usb/gadget/function/f_fs.c Paul Cercueil 2023-03-22 @1325 struct dma_buf_attachment *attach = priv->attach;
80c5660d9135823 drivers/usb/gadget/function/f_fs.c Paul Cercueil 2023-03-22 1326 struct dma_buf *dmabuf = attach->dmabuf;
80c5660d9135823 drivers/usb/gadget/function/f_fs.c Paul Cercueil 2023-03-22 1327
80c5660d9135823 drivers/usb/gadget/function/f_fs.c Paul Cercueil 2023-03-22 1328 pr_debug("FFS DMABUF release\n");
80c5660d9135823 drivers/usb/gadget/function/f_fs.c Paul Cercueil 2023-03-22 1329 dma_buf_detach(attach->dmabuf, attach);
80c5660d9135823 drivers/usb/gadget/function/f_fs.c Paul Cercueil 2023-03-22 1330 dma_buf_put(dmabuf);
80c5660d9135823 drivers/usb/gadget/function/f_fs.c Paul Cercueil 2023-03-22 1331
80c5660d9135823 drivers/usb/gadget/function/f_fs.c Paul Cercueil 2023-03-22 1332 list_del(&priv->entry);
80c5660d9135823 drivers/usb/gadget/function/f_fs.c Paul Cercueil 2023-03-22 1333 kfree(priv);
80c5660d9135823 drivers/usb/gadget/function/f_fs.c Paul Cercueil 2023-03-22 1334 }
80c5660d9135823 drivers/usb/gadget/function/f_fs.c Paul Cercueil 2023-03-22 1335
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v2 3/3] usb: gadget: functionfs: Add DMABUF import interface
2023-03-22 9:21 [PATCH v2 0/3] usb: gadget: functionfs: " Paul Cercueil
@ 2023-03-22 9:21 ` Paul Cercueil
0 siblings, 0 replies; 3+ messages in thread
From: Paul Cercueil @ 2023-03-22 9:21 UTC (permalink / raw)
To: Greg Kroah-Hartman, Sumit Semwal, Christian König
Cc: Paul Cercueil, michael.hennerich, linux-usb, linux-kernel,
dri-devel, nuno.sa, linaro-mm-sig, linux-media
This patch introduces three new ioctls. They all should be called on a
data endpoint (ie. not ep0). They are:
- FUNCTIONFS_DMABUF_ATTACH, which takes the file descriptor of a DMABUF
object to attach to the endpoint.
- FUNCTIONFS_DMABUF_DETACH, which takes the file descriptor of the
DMABUF to detach from the endpoint. Note that closing the endpoint's
file descriptor will automatically detach all attached DMABUFs.
- FUNCTIONFS_DMABUF_TRANSFER, which requests a data transfer from / to
the given DMABUF. Its argument is a structure that packs the DMABUF's
file descriptor, the size in bytes to transfer (which should generally
be set to the size of the DMABUF), and a 'flags' field which is unused
for now.
Before this ioctl can be used, the related DMABUF must be attached
with FUNCTIONFS_DMABUF_ATTACH.
These three ioctls enable the FunctionFS code to transfer data between
the USB stack and a DMABUF object, which can be provided by a driver
from a completely different subsystem, in a zero-copy fashion.
Signed-off-by: Paul Cercueil <paul@crapouillou.net>
---
v2:
- Make ffs_dma_resv_lock() static
- Add MODULE_IMPORT_NS(DMA_BUF);
- The attach/detach functions are now performed without locking the
eps_lock spinlock. The transfer function starts with the spinlock
unlocked, then locks it before allocating and queueing the USB
transfer.
---
drivers/usb/gadget/function/f_fs.c | 421 ++++++++++++++++++++++++++++
include/uapi/linux/usb/functionfs.h | 14 +-
2 files changed, 434 insertions(+), 1 deletion(-)
diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index 8da64f0fdef0..f5c609c83b36 100644
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -15,6 +15,9 @@
/* #define VERBOSE_DEBUG */
#include <linux/blkdev.h>
+#include <linux/dma-buf.h>
+#include <linux/dma-fence.h>
+#include <linux/dma-resv.h>
#include <linux/pagemap.h>
#include <linux/export.h>
#include <linux/fs_parser.h>
@@ -43,6 +46,8 @@
#define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
+MODULE_IMPORT_NS(DMA_BUF);
+
/* Reference counter handling */
static void ffs_data_get(struct ffs_data *ffs);
static void ffs_data_put(struct ffs_data *ffs);
@@ -124,6 +129,26 @@ struct ffs_ep {
u8 num;
};
+struct ffs_dmabuf_priv {
+ struct list_head entry;
+ struct kref ref;
+ struct dma_buf_attachment *attach;
+ spinlock_t lock;
+ u64 context;
+};
+
+struct ffs_dma_fence {
+ struct dma_fence base;
+ struct ffs_dmabuf_priv *priv;
+ struct sg_table *sgt;
+ enum dma_data_direction dir;
+};
+
+static inline struct ffs_dma_fence *to_ffs_dma_fence(struct dma_fence *fence)
+{
+ return container_of(fence, struct ffs_dma_fence, base);
+}
+
struct ffs_epfile {
/* Protects ep->ep and ep->req. */
struct mutex mutex;
@@ -197,6 +222,8 @@ struct ffs_epfile {
unsigned char isoc; /* P: ffs->eps_lock */
unsigned char _pad;
+
+ struct list_head dmabufs;
};
struct ffs_buffer {
@@ -1292,19 +1319,374 @@ static ssize_t ffs_epfile_read_iter(struct kiocb *kiocb, struct iov_iter *to)
return res;
}
+static void ffs_dmabuf_release(struct kref *ref)
+{
+ struct ffs_dmabuf_priv *priv = container_of(ref, struct ffs_dmabuf_priv, ref);
+ struct dma_buf_attachment *attach = priv->attach;
+ struct dma_buf *dmabuf = attach->dmabuf;
+
+ pr_debug("FFS DMABUF release\n");
+ dma_buf_detach(attach->dmabuf, attach);
+ dma_buf_put(dmabuf);
+
+ list_del(&priv->entry);
+ kfree(priv);
+}
+
+static void ffs_dmabuf_get(struct dma_buf_attachment *attach)
+{
+ struct ffs_dmabuf_priv *priv = attach->importer_priv;
+
+ kref_get(&priv->ref);
+}
+
+static void ffs_dmabuf_put(struct dma_buf_attachment *attach)
+{
+ struct ffs_dmabuf_priv *priv = attach->importer_priv;
+
+ kref_put(&priv->ref, ffs_dmabuf_release);
+}
+
static int
ffs_epfile_release(struct inode *inode, struct file *file)
{
struct ffs_epfile *epfile = inode->i_private;
+ struct ffs_dmabuf_priv *priv, *tmp;
ENTER();
+ /* Close all attached DMABUFs */
+ list_for_each_entry_safe(priv, tmp, &epfile->dmabufs, entry) {
+ ffs_dmabuf_put(priv->attach);
+ }
+
__ffs_epfile_read_buffer_free(epfile);
ffs_data_closed(epfile->ffs);
return 0;
}
+static void ffs_dmabuf_signal_done(struct ffs_dma_fence *dma_fence, int ret)
+{
+ struct ffs_dmabuf_priv *priv = dma_fence->priv;
+ struct dma_fence *fence = &dma_fence->base;
+
+ dma_fence_get(fence);
+ fence->error = ret;
+ dma_fence_signal(fence);
+
+ dma_buf_unmap_attachment(priv->attach, dma_fence->sgt, dma_fence->dir);
+ dma_fence_put(fence);
+ ffs_dmabuf_put(priv->attach);
+}
+
+static void ffs_epfile_dmabuf_io_complete(struct usb_ep *ep,
+ struct usb_request *req)
+{
+ ENTER();
+
+ pr_debug("FFS: DMABUF transfer complete, status=%d\n", req->status);
+ ffs_dmabuf_signal_done(req->context, req->status);
+ usb_ep_free_request(ep, req);
+}
+
+static const char *ffs_dmabuf_get_driver_name(struct dma_fence *fence)
+{
+ return "functionfs";
+}
+
+static const char *ffs_dmabuf_get_timeline_name(struct dma_fence *fence)
+{
+ return "";
+}
+
+static void ffs_dmabuf_fence_release(struct dma_fence *fence)
+{
+ struct ffs_dma_fence *dma_fence = to_ffs_dma_fence(fence);
+
+ kfree(dma_fence);
+}
+
+static const struct dma_fence_ops ffs_dmabuf_fence_ops = {
+ .get_driver_name = ffs_dmabuf_get_driver_name,
+ .get_timeline_name = ffs_dmabuf_get_timeline_name,
+ .release = ffs_dmabuf_fence_release,
+};
+
+static int ffs_dma_resv_lock(struct dma_buf *dmabuf, bool nonblock)
+{
+ int ret;
+
+ ret = dma_resv_lock_interruptible(dmabuf->resv, NULL);
+ if (ret) {
+ if (ret != -EDEADLK)
+ goto out;
+ if (nonblock) {
+ ret = -EBUSY;
+ goto out;
+ }
+
+ ret = dma_resv_lock_slow_interruptible(dmabuf->resv, NULL);
+ }
+
+out:
+ return ret;
+}
+
+static struct dma_buf_attachment *
+ffs_dmabuf_find_attachment(struct device *dev, struct dma_buf *dmabuf,
+ bool nonblock)
+{
+ struct dma_buf_attachment *elm, *attach = NULL;
+ int ret;
+
+ ret = ffs_dma_resv_lock(dmabuf, nonblock);
+ if (ret)
+ return ERR_PTR(ret);
+
+ list_for_each_entry(elm, &dmabuf->attachments, node) {
+ if (elm->dev == dev) {
+ attach = elm;
+ break;
+ }
+ }
+
+ if (attach)
+ ffs_dmabuf_get(elm);
+
+ dma_resv_unlock(dmabuf->resv);
+
+ return attach ?: ERR_PTR(-EPERM);
+}
+
+static int ffs_dmabuf_attach(struct file *file, int fd)
+{
+ struct ffs_epfile *epfile = file->private_data;
+ struct usb_gadget *gadget = epfile->ffs->gadget;
+ struct dma_buf_attachment *attach;
+ struct ffs_dmabuf_priv *priv;
+ struct dma_buf *dmabuf;
+ int err;
+
+ if (!gadget || !gadget->sg_supported)
+ return -EPERM;
+
+ dmabuf = dma_buf_get(fd);
+ if (IS_ERR(dmabuf))
+ return PTR_ERR(dmabuf);
+
+ attach = dma_buf_attach(dmabuf, gadget->dev.parent);
+ if (IS_ERR(attach)) {
+ err = PTR_ERR(attach);
+ goto err_dmabuf_put;
+ }
+
+ priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+ if (!priv) {
+ err = -ENOMEM;
+ goto err_dmabuf_detach;
+ }
+
+ attach->importer_priv = priv;
+
+ priv->attach = attach;
+ spin_lock_init(&priv->lock);
+ kref_init(&priv->ref);
+ priv->context = dma_fence_context_alloc(1);
+
+ list_add(&priv->entry, &epfile->dmabufs);
+
+ return 0;
+
+err_dmabuf_detach:
+ dma_buf_detach(dmabuf, attach);
+err_dmabuf_put:
+ dma_buf_put(dmabuf);
+
+ return err;
+}
+
+static int ffs_dmabuf_detach(struct file *file, int fd)
+{
+ struct ffs_epfile *epfile = file->private_data;
+ struct usb_gadget *gadget = epfile->ffs->gadget;
+ bool nonblock = file->f_flags & O_NONBLOCK;
+ struct dma_buf_attachment *attach;
+ struct dma_buf *dmabuf;
+ int ret = 0;
+
+ dmabuf = dma_buf_get(fd);
+ if (IS_ERR(dmabuf))
+ return PTR_ERR(dmabuf);
+
+ attach = ffs_dmabuf_find_attachment(gadget->dev.parent,
+ dmabuf, nonblock);
+ if (IS_ERR(attach)) {
+ ret = PTR_ERR(attach);
+ goto out_dmabuf_put;
+ }
+
+ ffs_dmabuf_put(attach);
+ ffs_dmabuf_put(attach);
+
+out_dmabuf_put:
+ dma_buf_put(dmabuf);
+ return ret;
+}
+
+static int ffs_dmabuf_transfer(struct file *file,
+ const struct usb_ffs_dmabuf_transfer_req *req)
+{
+ bool dma_to_ram, nonblock = file->f_flags & O_NONBLOCK;
+ struct ffs_epfile *epfile = file->private_data;
+ struct usb_gadget *gadget = epfile->ffs->gadget;
+ struct dma_buf_attachment *attach;
+ struct ffs_dmabuf_priv *priv;
+ enum dma_data_direction dir;
+ struct ffs_dma_fence *fence;
+ struct usb_request *usb_req;
+ struct sg_table *sg_table;
+ struct dma_buf *dmabuf;
+ struct ffs_ep *ep;
+ int ret;
+
+ if (req->flags & ~USB_FFS_DMABUF_TRANSFER_MASK)
+ return -EINVAL;
+
+ dmabuf = dma_buf_get(req->fd);
+ if (IS_ERR(dmabuf))
+ return PTR_ERR(dmabuf);
+
+ if (req->length > dmabuf->size || req->length == 0) {
+ ret = -EINVAL;
+ goto err_dmabuf_put;
+ }
+
+ attach = ffs_dmabuf_find_attachment(gadget->dev.parent,
+ dmabuf, nonblock);
+ if (IS_ERR(attach)) {
+ ret = PTR_ERR(attach);
+ goto err_dmabuf_put;
+ }
+
+ priv = attach->importer_priv;
+
+ if (epfile->in)
+ dir = DMA_FROM_DEVICE;
+ else
+ dir = DMA_TO_DEVICE;
+
+ sg_table = dma_buf_map_attachment(attach, dir);
+ if (IS_ERR(sg_table)) {
+ ret = PTR_ERR(sg_table);
+ goto err_attachment_put;
+ }
+
+ ep = ffs_epfile_wait_ep(file);
+ if (IS_ERR(ep)) {
+ ret = PTR_ERR(ep);
+ goto err_unmap_attachment;
+ }
+
+ ret = ffs_dma_resv_lock(dmabuf, nonblock);
+ if (ret)
+ goto err_unmap_attachment;
+
+ /* Make sure we don't have writers */
+ if (!dma_resv_test_signaled(dmabuf->resv, DMA_RESV_USAGE_WRITE)) {
+ pr_debug("FFS WRITE fence is not signaled\n");
+ ret = -EBUSY;
+ goto err_resv_unlock;
+ }
+
+ dma_to_ram = dir == DMA_FROM_DEVICE;
+
+ /* If we're writing to the DMABUF, make sure we don't have readers */
+ if (dma_to_ram &&
+ !dma_resv_test_signaled(dmabuf->resv, DMA_RESV_USAGE_READ)) {
+ pr_debug("FFS READ fence is not signaled\n");
+ ret = -EBUSY;
+ goto err_resv_unlock;
+ }
+
+ ret = dma_resv_reserve_fences(dmabuf->resv, 1);
+ if (ret)
+ goto err_resv_unlock;
+
+ fence = kmalloc(sizeof(*fence), GFP_KERNEL);
+ if (!fence) {
+ ret = -ENOMEM;
+ goto err_resv_unlock;
+ }
+
+ fence->sgt = sg_table;
+ fence->dir = dir;
+ fence->priv = priv;
+
+ dma_fence_init(&fence->base, &ffs_dmabuf_fence_ops,
+ &priv->lock, priv->context, 0);
+
+ spin_lock_irq(&epfile->ffs->eps_lock);
+
+ /* In the meantime, endpoint got disabled or changed. */
+ if (epfile->ep != ep) {
+ ret = -ESHUTDOWN;
+ goto err_spin_unlock;
+ }
+
+ usb_req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC);
+ if (!usb_req) {
+ ret = -ENOMEM;
+ goto err_spin_unlock;
+ }
+
+ dma_resv_add_fence(dmabuf->resv, &fence->base,
+ dma_resv_usage_rw(dma_to_ram));
+ dma_resv_unlock(dmabuf->resv);
+
+ /* Now that the dma_fence is in place, queue the transfer. */
+
+ usb_req->length = req->length;
+ usb_req->buf = NULL;
+ usb_req->sg = sg_table->sgl;
+ usb_req->num_sgs = sg_nents_for_len(sg_table->sgl, req->length);
+ usb_req->sg_was_mapped = true;
+ usb_req->context = fence;
+ usb_req->complete = ffs_epfile_dmabuf_io_complete;
+
+ ret = usb_ep_queue(ep->ep, usb_req, GFP_ATOMIC);
+ if (ret) {
+ usb_ep_free_request(ep->ep, usb_req);
+
+ spin_unlock_irq(&epfile->ffs->eps_lock);
+
+ pr_warn("FFS: Failed to queue DMABUF: %d\n", ret);
+ ffs_dmabuf_signal_done(fence, ret);
+ goto out_dma_buf_put;
+ }
+
+ spin_unlock_irq(&epfile->ffs->eps_lock);
+
+out_dma_buf_put:
+ dma_buf_put(dmabuf);
+
+ return ret;
+
+err_spin_unlock:
+ spin_unlock_irq(&epfile->ffs->eps_lock);
+ dma_fence_put(&fence->base);
+err_resv_unlock:
+ dma_resv_unlock(dmabuf->resv);
+err_unmap_attachment:
+ dma_buf_unmap_attachment(attach, sg_table, dir);
+err_attachment_put:
+ ffs_dmabuf_put(attach);
+err_dmabuf_put:
+ dma_buf_put(dmabuf);
+
+ return ret;
+}
+
static long ffs_epfile_ioctl(struct file *file, unsigned code,
unsigned long value)
{
@@ -1317,6 +1699,44 @@ static long ffs_epfile_ioctl(struct file *file, unsigned code,
if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
return -ENODEV;
+ switch (code) {
+ case FUNCTIONFS_DMABUF_ATTACH:
+ {
+ int fd;
+
+ if (copy_from_user(&fd, (void __user *)value, sizeof(fd))) {
+ ret = -EFAULT;
+ break;
+ }
+
+ return ffs_dmabuf_attach(file, fd);
+ }
+ case FUNCTIONFS_DMABUF_DETACH:
+ {
+ int fd;
+
+ if (copy_from_user(&fd, (void __user *)value, sizeof(fd))) {
+ ret = -EFAULT;
+ break;
+ }
+
+ return ffs_dmabuf_detach(file, fd);
+ }
+ case FUNCTIONFS_DMABUF_TRANSFER:
+ {
+ struct usb_ffs_dmabuf_transfer_req req;
+
+ if (copy_from_user(&req, (void __user *)value, sizeof(req))) {
+ ret = -EFAULT;
+ break;
+ }
+
+ return ffs_dmabuf_transfer(file, &req);
+ }
+ default:
+ break;
+ }
+
/* Wait for endpoint to be enabled */
ep = ffs_epfile_wait_ep(file);
if (IS_ERR(ep))
@@ -1931,6 +2351,7 @@ static int ffs_epfiles_create(struct ffs_data *ffs)
for (i = 1; i <= count; ++i, ++epfile) {
epfile->ffs = ffs;
mutex_init(&epfile->mutex);
+ INIT_LIST_HEAD(&epfile->dmabufs);
if (ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
sprintf(epfile->name, "ep%02x", ffs->eps_addrmap[i]);
else
diff --git a/include/uapi/linux/usb/functionfs.h b/include/uapi/linux/usb/functionfs.h
index d77ee6b65328..1412ab9f8ccc 100644
--- a/include/uapi/linux/usb/functionfs.h
+++ b/include/uapi/linux/usb/functionfs.h
@@ -84,6 +84,15 @@ struct usb_ext_prop_desc {
__le16 wPropertyNameLength;
} __attribute__((packed));
+/* Flags for usb_ffs_dmabuf_transfer_req->flags (none for now) */
+#define USB_FFS_DMABUF_TRANSFER_MASK 0x0
+
+struct usb_ffs_dmabuf_transfer_req {
+ int fd;
+ __u32 flags;
+ __u64 length;
+} __attribute__((packed));
+
#ifndef __KERNEL__
/*
@@ -288,6 +297,9 @@ struct usb_functionfs_event {
#define FUNCTIONFS_ENDPOINT_DESC _IOR('g', 130, \
struct usb_endpoint_descriptor)
-
+#define FUNCTIONFS_DMABUF_ATTACH _IOW('g', 131, int)
+#define FUNCTIONFS_DMABUF_DETACH _IOW('g', 132, int)
+#define FUNCTIONFS_DMABUF_TRANSFER _IOW('g', 133, \
+ struct usb_ffs_dmabuf_transfer_req)
#endif /* _UAPI__LINUX_FUNCTIONFS_H__ */
--
2.39.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v2 3/3] usb: gadget: functionfs: Add DMABUF import interface
@ 2023-03-22 9:21 ` Paul Cercueil
0 siblings, 0 replies; 3+ messages in thread
From: Paul Cercueil @ 2023-03-22 9:21 UTC (permalink / raw)
To: Greg Kroah-Hartman, Sumit Semwal, Christian König
Cc: michael.hennerich, nuno.sa, linux-usb, linux-kernel, linux-media,
dri-devel, linaro-mm-sig, Paul Cercueil
This patch introduces three new ioctls. They all should be called on a
data endpoint (ie. not ep0). They are:
- FUNCTIONFS_DMABUF_ATTACH, which takes the file descriptor of a DMABUF
object to attach to the endpoint.
- FUNCTIONFS_DMABUF_DETACH, which takes the file descriptor of the
DMABUF to detach from the endpoint. Note that closing the endpoint's
file descriptor will automatically detach all attached DMABUFs.
- FUNCTIONFS_DMABUF_TRANSFER, which requests a data transfer from / to
the given DMABUF. Its argument is a structure that packs the DMABUF's
file descriptor, the size in bytes to transfer (which should generally
be set to the size of the DMABUF), and a 'flags' field which is unused
for now.
Before this ioctl can be used, the related DMABUF must be attached
with FUNCTIONFS_DMABUF_ATTACH.
These three ioctls enable the FunctionFS code to transfer data between
the USB stack and a DMABUF object, which can be provided by a driver
from a completely different subsystem, in a zero-copy fashion.
Signed-off-by: Paul Cercueil <paul@crapouillou.net>
---
v2:
- Make ffs_dma_resv_lock() static
- Add MODULE_IMPORT_NS(DMA_BUF);
- The attach/detach functions are now performed without locking the
eps_lock spinlock. The transfer function starts with the spinlock
unlocked, then locks it before allocating and queueing the USB
transfer.
---
drivers/usb/gadget/function/f_fs.c | 421 ++++++++++++++++++++++++++++
include/uapi/linux/usb/functionfs.h | 14 +-
2 files changed, 434 insertions(+), 1 deletion(-)
diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index 8da64f0fdef0..f5c609c83b36 100644
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -15,6 +15,9 @@
/* #define VERBOSE_DEBUG */
#include <linux/blkdev.h>
+#include <linux/dma-buf.h>
+#include <linux/dma-fence.h>
+#include <linux/dma-resv.h>
#include <linux/pagemap.h>
#include <linux/export.h>
#include <linux/fs_parser.h>
@@ -43,6 +46,8 @@
#define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
+MODULE_IMPORT_NS(DMA_BUF);
+
/* Reference counter handling */
static void ffs_data_get(struct ffs_data *ffs);
static void ffs_data_put(struct ffs_data *ffs);
@@ -124,6 +129,26 @@ struct ffs_ep {
u8 num;
};
+struct ffs_dmabuf_priv {
+ struct list_head entry;
+ struct kref ref;
+ struct dma_buf_attachment *attach;
+ spinlock_t lock;
+ u64 context;
+};
+
+struct ffs_dma_fence {
+ struct dma_fence base;
+ struct ffs_dmabuf_priv *priv;
+ struct sg_table *sgt;
+ enum dma_data_direction dir;
+};
+
+static inline struct ffs_dma_fence *to_ffs_dma_fence(struct dma_fence *fence)
+{
+ return container_of(fence, struct ffs_dma_fence, base);
+}
+
struct ffs_epfile {
/* Protects ep->ep and ep->req. */
struct mutex mutex;
@@ -197,6 +222,8 @@ struct ffs_epfile {
unsigned char isoc; /* P: ffs->eps_lock */
unsigned char _pad;
+
+ struct list_head dmabufs;
};
struct ffs_buffer {
@@ -1292,19 +1319,374 @@ static ssize_t ffs_epfile_read_iter(struct kiocb *kiocb, struct iov_iter *to)
return res;
}
+static void ffs_dmabuf_release(struct kref *ref)
+{
+ struct ffs_dmabuf_priv *priv = container_of(ref, struct ffs_dmabuf_priv, ref);
+ struct dma_buf_attachment *attach = priv->attach;
+ struct dma_buf *dmabuf = attach->dmabuf;
+
+ pr_debug("FFS DMABUF release\n");
+ dma_buf_detach(attach->dmabuf, attach);
+ dma_buf_put(dmabuf);
+
+ list_del(&priv->entry);
+ kfree(priv);
+}
+
+static void ffs_dmabuf_get(struct dma_buf_attachment *attach)
+{
+ struct ffs_dmabuf_priv *priv = attach->importer_priv;
+
+ kref_get(&priv->ref);
+}
+
+static void ffs_dmabuf_put(struct dma_buf_attachment *attach)
+{
+ struct ffs_dmabuf_priv *priv = attach->importer_priv;
+
+ kref_put(&priv->ref, ffs_dmabuf_release);
+}
+
static int
ffs_epfile_release(struct inode *inode, struct file *file)
{
struct ffs_epfile *epfile = inode->i_private;
+ struct ffs_dmabuf_priv *priv, *tmp;
ENTER();
+ /* Close all attached DMABUFs */
+ list_for_each_entry_safe(priv, tmp, &epfile->dmabufs, entry) {
+ ffs_dmabuf_put(priv->attach);
+ }
+
__ffs_epfile_read_buffer_free(epfile);
ffs_data_closed(epfile->ffs);
return 0;
}
+static void ffs_dmabuf_signal_done(struct ffs_dma_fence *dma_fence, int ret)
+{
+ struct ffs_dmabuf_priv *priv = dma_fence->priv;
+ struct dma_fence *fence = &dma_fence->base;
+
+ dma_fence_get(fence);
+ fence->error = ret;
+ dma_fence_signal(fence);
+
+ dma_buf_unmap_attachment(priv->attach, dma_fence->sgt, dma_fence->dir);
+ dma_fence_put(fence);
+ ffs_dmabuf_put(priv->attach);
+}
+
+static void ffs_epfile_dmabuf_io_complete(struct usb_ep *ep,
+ struct usb_request *req)
+{
+ ENTER();
+
+ pr_debug("FFS: DMABUF transfer complete, status=%d\n", req->status);
+ ffs_dmabuf_signal_done(req->context, req->status);
+ usb_ep_free_request(ep, req);
+}
+
+static const char *ffs_dmabuf_get_driver_name(struct dma_fence *fence)
+{
+ return "functionfs";
+}
+
+static const char *ffs_dmabuf_get_timeline_name(struct dma_fence *fence)
+{
+ return "";
+}
+
+static void ffs_dmabuf_fence_release(struct dma_fence *fence)
+{
+ struct ffs_dma_fence *dma_fence = to_ffs_dma_fence(fence);
+
+ kfree(dma_fence);
+}
+
+static const struct dma_fence_ops ffs_dmabuf_fence_ops = {
+ .get_driver_name = ffs_dmabuf_get_driver_name,
+ .get_timeline_name = ffs_dmabuf_get_timeline_name,
+ .release = ffs_dmabuf_fence_release,
+};
+
+static int ffs_dma_resv_lock(struct dma_buf *dmabuf, bool nonblock)
+{
+ int ret;
+
+ ret = dma_resv_lock_interruptible(dmabuf->resv, NULL);
+ if (ret) {
+ if (ret != -EDEADLK)
+ goto out;
+ if (nonblock) {
+ ret = -EBUSY;
+ goto out;
+ }
+
+ ret = dma_resv_lock_slow_interruptible(dmabuf->resv, NULL);
+ }
+
+out:
+ return ret;
+}
+
+static struct dma_buf_attachment *
+ffs_dmabuf_find_attachment(struct device *dev, struct dma_buf *dmabuf,
+ bool nonblock)
+{
+ struct dma_buf_attachment *elm, *attach = NULL;
+ int ret;
+
+ ret = ffs_dma_resv_lock(dmabuf, nonblock);
+ if (ret)
+ return ERR_PTR(ret);
+
+ list_for_each_entry(elm, &dmabuf->attachments, node) {
+ if (elm->dev == dev) {
+ attach = elm;
+ break;
+ }
+ }
+
+ if (attach)
+ ffs_dmabuf_get(elm);
+
+ dma_resv_unlock(dmabuf->resv);
+
+ return attach ?: ERR_PTR(-EPERM);
+}
+
+static int ffs_dmabuf_attach(struct file *file, int fd)
+{
+ struct ffs_epfile *epfile = file->private_data;
+ struct usb_gadget *gadget = epfile->ffs->gadget;
+ struct dma_buf_attachment *attach;
+ struct ffs_dmabuf_priv *priv;
+ struct dma_buf *dmabuf;
+ int err;
+
+ if (!gadget || !gadget->sg_supported)
+ return -EPERM;
+
+ dmabuf = dma_buf_get(fd);
+ if (IS_ERR(dmabuf))
+ return PTR_ERR(dmabuf);
+
+ attach = dma_buf_attach(dmabuf, gadget->dev.parent);
+ if (IS_ERR(attach)) {
+ err = PTR_ERR(attach);
+ goto err_dmabuf_put;
+ }
+
+ priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+ if (!priv) {
+ err = -ENOMEM;
+ goto err_dmabuf_detach;
+ }
+
+ attach->importer_priv = priv;
+
+ priv->attach = attach;
+ spin_lock_init(&priv->lock);
+ kref_init(&priv->ref);
+ priv->context = dma_fence_context_alloc(1);
+
+ list_add(&priv->entry, &epfile->dmabufs);
+
+ return 0;
+
+err_dmabuf_detach:
+ dma_buf_detach(dmabuf, attach);
+err_dmabuf_put:
+ dma_buf_put(dmabuf);
+
+ return err;
+}
+
+static int ffs_dmabuf_detach(struct file *file, int fd)
+{
+ struct ffs_epfile *epfile = file->private_data;
+ struct usb_gadget *gadget = epfile->ffs->gadget;
+ bool nonblock = file->f_flags & O_NONBLOCK;
+ struct dma_buf_attachment *attach;
+ struct dma_buf *dmabuf;
+ int ret = 0;
+
+ dmabuf = dma_buf_get(fd);
+ if (IS_ERR(dmabuf))
+ return PTR_ERR(dmabuf);
+
+ attach = ffs_dmabuf_find_attachment(gadget->dev.parent,
+ dmabuf, nonblock);
+ if (IS_ERR(attach)) {
+ ret = PTR_ERR(attach);
+ goto out_dmabuf_put;
+ }
+
+ ffs_dmabuf_put(attach);
+ ffs_dmabuf_put(attach);
+
+out_dmabuf_put:
+ dma_buf_put(dmabuf);
+ return ret;
+}
+
+static int ffs_dmabuf_transfer(struct file *file,
+ const struct usb_ffs_dmabuf_transfer_req *req)
+{
+ bool dma_to_ram, nonblock = file->f_flags & O_NONBLOCK;
+ struct ffs_epfile *epfile = file->private_data;
+ struct usb_gadget *gadget = epfile->ffs->gadget;
+ struct dma_buf_attachment *attach;
+ struct ffs_dmabuf_priv *priv;
+ enum dma_data_direction dir;
+ struct ffs_dma_fence *fence;
+ struct usb_request *usb_req;
+ struct sg_table *sg_table;
+ struct dma_buf *dmabuf;
+ struct ffs_ep *ep;
+ int ret;
+
+ if (req->flags & ~USB_FFS_DMABUF_TRANSFER_MASK)
+ return -EINVAL;
+
+ dmabuf = dma_buf_get(req->fd);
+ if (IS_ERR(dmabuf))
+ return PTR_ERR(dmabuf);
+
+ if (req->length > dmabuf->size || req->length == 0) {
+ ret = -EINVAL;
+ goto err_dmabuf_put;
+ }
+
+ attach = ffs_dmabuf_find_attachment(gadget->dev.parent,
+ dmabuf, nonblock);
+ if (IS_ERR(attach)) {
+ ret = PTR_ERR(attach);
+ goto err_dmabuf_put;
+ }
+
+ priv = attach->importer_priv;
+
+ if (epfile->in)
+ dir = DMA_FROM_DEVICE;
+ else
+ dir = DMA_TO_DEVICE;
+
+ sg_table = dma_buf_map_attachment(attach, dir);
+ if (IS_ERR(sg_table)) {
+ ret = PTR_ERR(sg_table);
+ goto err_attachment_put;
+ }
+
+ ep = ffs_epfile_wait_ep(file);
+ if (IS_ERR(ep)) {
+ ret = PTR_ERR(ep);
+ goto err_unmap_attachment;
+ }
+
+ ret = ffs_dma_resv_lock(dmabuf, nonblock);
+ if (ret)
+ goto err_unmap_attachment;
+
+ /* Make sure we don't have writers */
+ if (!dma_resv_test_signaled(dmabuf->resv, DMA_RESV_USAGE_WRITE)) {
+ pr_debug("FFS WRITE fence is not signaled\n");
+ ret = -EBUSY;
+ goto err_resv_unlock;
+ }
+
+ dma_to_ram = dir == DMA_FROM_DEVICE;
+
+ /* If we're writing to the DMABUF, make sure we don't have readers */
+ if (dma_to_ram &&
+ !dma_resv_test_signaled(dmabuf->resv, DMA_RESV_USAGE_READ)) {
+ pr_debug("FFS READ fence is not signaled\n");
+ ret = -EBUSY;
+ goto err_resv_unlock;
+ }
+
+ ret = dma_resv_reserve_fences(dmabuf->resv, 1);
+ if (ret)
+ goto err_resv_unlock;
+
+ fence = kmalloc(sizeof(*fence), GFP_KERNEL);
+ if (!fence) {
+ ret = -ENOMEM;
+ goto err_resv_unlock;
+ }
+
+ fence->sgt = sg_table;
+ fence->dir = dir;
+ fence->priv = priv;
+
+ dma_fence_init(&fence->base, &ffs_dmabuf_fence_ops,
+ &priv->lock, priv->context, 0);
+
+ spin_lock_irq(&epfile->ffs->eps_lock);
+
+ /* In the meantime, endpoint got disabled or changed. */
+ if (epfile->ep != ep) {
+ ret = -ESHUTDOWN;
+ goto err_spin_unlock;
+ }
+
+ usb_req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC);
+ if (!usb_req) {
+ ret = -ENOMEM;
+ goto err_spin_unlock;
+ }
+
+ dma_resv_add_fence(dmabuf->resv, &fence->base,
+ dma_resv_usage_rw(dma_to_ram));
+ dma_resv_unlock(dmabuf->resv);
+
+ /* Now that the dma_fence is in place, queue the transfer. */
+
+ usb_req->length = req->length;
+ usb_req->buf = NULL;
+ usb_req->sg = sg_table->sgl;
+ usb_req->num_sgs = sg_nents_for_len(sg_table->sgl, req->length);
+ usb_req->sg_was_mapped = true;
+ usb_req->context = fence;
+ usb_req->complete = ffs_epfile_dmabuf_io_complete;
+
+ ret = usb_ep_queue(ep->ep, usb_req, GFP_ATOMIC);
+ if (ret) {
+ usb_ep_free_request(ep->ep, usb_req);
+
+ spin_unlock_irq(&epfile->ffs->eps_lock);
+
+ pr_warn("FFS: Failed to queue DMABUF: %d\n", ret);
+ ffs_dmabuf_signal_done(fence, ret);
+ goto out_dma_buf_put;
+ }
+
+ spin_unlock_irq(&epfile->ffs->eps_lock);
+
+out_dma_buf_put:
+ dma_buf_put(dmabuf);
+
+ return ret;
+
+err_spin_unlock:
+ spin_unlock_irq(&epfile->ffs->eps_lock);
+ dma_fence_put(&fence->base);
+err_resv_unlock:
+ dma_resv_unlock(dmabuf->resv);
+err_unmap_attachment:
+ dma_buf_unmap_attachment(attach, sg_table, dir);
+err_attachment_put:
+ ffs_dmabuf_put(attach);
+err_dmabuf_put:
+ dma_buf_put(dmabuf);
+
+ return ret;
+}
+
static long ffs_epfile_ioctl(struct file *file, unsigned code,
unsigned long value)
{
@@ -1317,6 +1699,44 @@ static long ffs_epfile_ioctl(struct file *file, unsigned code,
if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
return -ENODEV;
+ switch (code) {
+ case FUNCTIONFS_DMABUF_ATTACH:
+ {
+ int fd;
+
+ if (copy_from_user(&fd, (void __user *)value, sizeof(fd))) {
+ ret = -EFAULT;
+ break;
+ }
+
+ return ffs_dmabuf_attach(file, fd);
+ }
+ case FUNCTIONFS_DMABUF_DETACH:
+ {
+ int fd;
+
+ if (copy_from_user(&fd, (void __user *)value, sizeof(fd))) {
+ ret = -EFAULT;
+ break;
+ }
+
+ return ffs_dmabuf_detach(file, fd);
+ }
+ case FUNCTIONFS_DMABUF_TRANSFER:
+ {
+ struct usb_ffs_dmabuf_transfer_req req;
+
+ if (copy_from_user(&req, (void __user *)value, sizeof(req))) {
+ ret = -EFAULT;
+ break;
+ }
+
+ return ffs_dmabuf_transfer(file, &req);
+ }
+ default:
+ break;
+ }
+
/* Wait for endpoint to be enabled */
ep = ffs_epfile_wait_ep(file);
if (IS_ERR(ep))
@@ -1931,6 +2351,7 @@ static int ffs_epfiles_create(struct ffs_data *ffs)
for (i = 1; i <= count; ++i, ++epfile) {
epfile->ffs = ffs;
mutex_init(&epfile->mutex);
+ INIT_LIST_HEAD(&epfile->dmabufs);
if (ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
sprintf(epfile->name, "ep%02x", ffs->eps_addrmap[i]);
else
diff --git a/include/uapi/linux/usb/functionfs.h b/include/uapi/linux/usb/functionfs.h
index d77ee6b65328..1412ab9f8ccc 100644
--- a/include/uapi/linux/usb/functionfs.h
+++ b/include/uapi/linux/usb/functionfs.h
@@ -84,6 +84,15 @@ struct usb_ext_prop_desc {
__le16 wPropertyNameLength;
} __attribute__((packed));
+/* Flags for usb_ffs_dmabuf_transfer_req->flags (none for now) */
+#define USB_FFS_DMABUF_TRANSFER_MASK 0x0
+
+struct usb_ffs_dmabuf_transfer_req {
+ int fd;
+ __u32 flags;
+ __u64 length;
+} __attribute__((packed));
+
#ifndef __KERNEL__
/*
@@ -288,6 +297,9 @@ struct usb_functionfs_event {
#define FUNCTIONFS_ENDPOINT_DESC _IOR('g', 130, \
struct usb_endpoint_descriptor)
-
+#define FUNCTIONFS_DMABUF_ATTACH _IOW('g', 131, int)
+#define FUNCTIONFS_DMABUF_DETACH _IOW('g', 132, int)
+#define FUNCTIONFS_DMABUF_TRANSFER _IOW('g', 133, \
+ struct usb_ffs_dmabuf_transfer_req)
#endif /* _UAPI__LINUX_FUNCTIONFS_H__ */
--
2.39.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-03-28 6:41 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-28 6:40 [PATCH v2 3/3] usb: gadget: functionfs: Add DMABUF import interface kernel test robot
-- strict thread matches above, loose matches on Subject: below --
2023-03-22 9:21 [PATCH v2 0/3] usb: gadget: functionfs: " Paul Cercueil
2023-03-22 9:21 ` [PATCH v2 3/3] usb: gadget: functionfs: Add " Paul Cercueil
2023-03-22 9:21 ` Paul Cercueil
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.