dev_uring.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * FUSE: Filesystem in Userspace
  4. * Copyright (c) 2023-2024 DataDirect Networks.
  5. */
  6. #include "fuse_i.h"
  7. #include "dev_uring_i.h"
  8. #include "fuse_dev_i.h"
  9. #include "fuse_trace.h"
  10. #include <linux/fs.h>
  11. #include <linux/io_uring/cmd.h>
  12. static bool __read_mostly enable_uring;
  13. module_param(enable_uring, bool, 0644);
  14. MODULE_PARM_DESC(enable_uring,
  15. "Enable userspace communication through io-uring");
  16. #define FUSE_URING_IOV_SEGS 2 /* header and payload */
  17. bool fuse_uring_enabled(void)
  18. {
  19. return enable_uring;
  20. }
  21. struct fuse_uring_pdu {
  22. struct fuse_ring_ent *ent;
  23. };
  24. static const struct fuse_iqueue_ops fuse_io_uring_ops;
  25. static void uring_cmd_set_ring_ent(struct io_uring_cmd *cmd,
  26. struct fuse_ring_ent *ring_ent)
  27. {
  28. struct fuse_uring_pdu *pdu =
  29. io_uring_cmd_to_pdu(cmd, struct fuse_uring_pdu);
  30. pdu->ent = ring_ent;
  31. }
  32. static struct fuse_ring_ent *uring_cmd_to_ring_ent(struct io_uring_cmd *cmd)
  33. {
  34. struct fuse_uring_pdu *pdu =
  35. io_uring_cmd_to_pdu(cmd, struct fuse_uring_pdu);
  36. return pdu->ent;
  37. }
  38. static void fuse_uring_flush_bg(struct fuse_ring_queue *queue)
  39. {
  40. struct fuse_ring *ring = queue->ring;
  41. struct fuse_conn *fc = ring->fc;
  42. lockdep_assert_held(&queue->lock);
  43. lockdep_assert_held(&fc->bg_lock);
  44. /*
  45. * Allow one bg request per queue, ignoring global fc limits.
  46. * This prevents a single queue from consuming all resources and
  47. * eliminates the need for remote queue wake-ups when global
  48. * limits are met but this queue has no more waiting requests.
  49. */
  50. while ((fc->active_background < fc->max_background ||
  51. !queue->active_background) &&
  52. (!list_empty(&queue->fuse_req_bg_queue))) {
  53. struct fuse_req *req;
  54. req = list_first_entry(&queue->fuse_req_bg_queue,
  55. struct fuse_req, list);
  56. fc->active_background++;
  57. queue->active_background++;
  58. list_move_tail(&req->list, &queue->fuse_req_queue);
  59. }
  60. }
  61. static void fuse_uring_req_end(struct fuse_ring_ent *ent, struct fuse_req *req,
  62. int error)
  63. {
  64. struct fuse_ring_queue *queue = ent->queue;
  65. struct fuse_ring *ring = queue->ring;
  66. struct fuse_conn *fc = ring->fc;
  67. lockdep_assert_not_held(&queue->lock);
  68. spin_lock(&queue->lock);
  69. ent->fuse_req = NULL;
  70. list_del_init(&req->list);
  71. if (test_bit(FR_BACKGROUND, &req->flags)) {
  72. queue->active_background--;
  73. spin_lock(&fc->bg_lock);
  74. fuse_uring_flush_bg(queue);
  75. spin_unlock(&fc->bg_lock);
  76. }
  77. spin_unlock(&queue->lock);
  78. if (error)
  79. req->out.h.error = error;
  80. clear_bit(FR_SENT, &req->flags);
  81. fuse_request_end(req);
  82. }
  83. /* Abort all list queued request on the given ring queue */
  84. static void fuse_uring_abort_end_queue_requests(struct fuse_ring_queue *queue)
  85. {
  86. struct fuse_req *req;
  87. LIST_HEAD(req_list);
  88. spin_lock(&queue->lock);
  89. list_for_each_entry(req, &queue->fuse_req_queue, list)
  90. clear_bit(FR_PENDING, &req->flags);
  91. list_splice_init(&queue->fuse_req_queue, &req_list);
  92. spin_unlock(&queue->lock);
  93. /* must not hold queue lock to avoid order issues with fi->lock */
  94. fuse_dev_end_requests(&req_list);
  95. }
  96. void fuse_uring_abort_end_requests(struct fuse_ring *ring)
  97. {
  98. int qid;
  99. struct fuse_ring_queue *queue;
  100. struct fuse_conn *fc = ring->fc;
  101. for (qid = 0; qid < ring->nr_queues; qid++) {
  102. queue = READ_ONCE(ring->queues[qid]);
  103. if (!queue)
  104. continue;
  105. queue->stopped = true;
  106. WARN_ON_ONCE(ring->fc->max_background != UINT_MAX);
  107. spin_lock(&queue->lock);
  108. spin_lock(&fc->bg_lock);
  109. fuse_uring_flush_bg(queue);
  110. spin_unlock(&fc->bg_lock);
  111. spin_unlock(&queue->lock);
  112. fuse_uring_abort_end_queue_requests(queue);
  113. }
  114. }
  115. static bool ent_list_request_expired(struct fuse_conn *fc, struct list_head *list)
  116. {
  117. struct fuse_ring_ent *ent;
  118. struct fuse_req *req;
  119. ent = list_first_entry_or_null(list, struct fuse_ring_ent, list);
  120. if (!ent)
  121. return false;
  122. req = ent->fuse_req;
  123. return time_is_before_jiffies(req->create_time +
  124. fc->timeout.req_timeout);
  125. }
  126. bool fuse_uring_request_expired(struct fuse_conn *fc)
  127. {
  128. struct fuse_ring *ring = fc->ring;
  129. struct fuse_ring_queue *queue;
  130. int qid;
  131. if (!ring)
  132. return false;
  133. for (qid = 0; qid < ring->nr_queues; qid++) {
  134. queue = READ_ONCE(ring->queues[qid]);
  135. if (!queue)
  136. continue;
  137. spin_lock(&queue->lock);
  138. if (fuse_request_expired(fc, &queue->fuse_req_queue) ||
  139. fuse_request_expired(fc, &queue->fuse_req_bg_queue) ||
  140. ent_list_request_expired(fc, &queue->ent_w_req_queue) ||
  141. ent_list_request_expired(fc, &queue->ent_in_userspace)) {
  142. spin_unlock(&queue->lock);
  143. return true;
  144. }
  145. spin_unlock(&queue->lock);
  146. }
  147. return false;
  148. }
  149. void fuse_uring_destruct(struct fuse_conn *fc)
  150. {
  151. struct fuse_ring *ring = fc->ring;
  152. int qid;
  153. if (!ring)
  154. return;
  155. for (qid = 0; qid < ring->nr_queues; qid++) {
  156. struct fuse_ring_queue *queue = ring->queues[qid];
  157. struct fuse_ring_ent *ent, *next;
  158. if (!queue)
  159. continue;
  160. WARN_ON(!list_empty(&queue->ent_avail_queue));
  161. WARN_ON(!list_empty(&queue->ent_w_req_queue));
  162. WARN_ON(!list_empty(&queue->ent_commit_queue));
  163. WARN_ON(!list_empty(&queue->ent_in_userspace));
  164. list_for_each_entry_safe(ent, next, &queue->ent_released,
  165. list) {
  166. list_del_init(&ent->list);
  167. kfree(ent);
  168. }
  169. kfree(queue->fpq.processing);
  170. kfree(queue);
  171. ring->queues[qid] = NULL;
  172. }
  173. kfree(ring->queues);
  174. kfree(ring);
  175. fc->ring = NULL;
  176. }
  177. /*
  178. * Basic ring setup for this connection based on the provided configuration
  179. */
  180. static struct fuse_ring *fuse_uring_create(struct fuse_conn *fc)
  181. {
  182. struct fuse_ring *ring;
  183. size_t nr_queues = num_possible_cpus();
  184. struct fuse_ring *res = NULL;
  185. size_t max_payload_size;
  186. ring = kzalloc_obj(*fc->ring, GFP_KERNEL_ACCOUNT);
  187. if (!ring)
  188. return NULL;
  189. ring->queues = kzalloc_objs(struct fuse_ring_queue *, nr_queues,
  190. GFP_KERNEL_ACCOUNT);
  191. if (!ring->queues)
  192. goto out_err;
  193. max_payload_size = max(FUSE_MIN_READ_BUFFER, fc->max_write);
  194. max_payload_size = max(max_payload_size, fc->max_pages * PAGE_SIZE);
  195. spin_lock(&fc->lock);
  196. if (fc->ring) {
  197. /* race, another thread created the ring in the meantime */
  198. spin_unlock(&fc->lock);
  199. res = fc->ring;
  200. goto out_err;
  201. }
  202. init_waitqueue_head(&ring->stop_waitq);
  203. ring->nr_queues = nr_queues;
  204. ring->fc = fc;
  205. ring->max_payload_sz = max_payload_size;
  206. smp_store_release(&fc->ring, ring);
  207. spin_unlock(&fc->lock);
  208. return ring;
  209. out_err:
  210. kfree(ring->queues);
  211. kfree(ring);
  212. return res;
  213. }
  214. static struct fuse_ring_queue *fuse_uring_create_queue(struct fuse_ring *ring,
  215. int qid)
  216. {
  217. struct fuse_conn *fc = ring->fc;
  218. struct fuse_ring_queue *queue;
  219. struct list_head *pq;
  220. queue = kzalloc_obj(*queue, GFP_KERNEL_ACCOUNT);
  221. if (!queue)
  222. return NULL;
  223. pq = kzalloc_objs(struct list_head, FUSE_PQ_HASH_SIZE);
  224. if (!pq) {
  225. kfree(queue);
  226. return NULL;
  227. }
  228. queue->qid = qid;
  229. queue->ring = ring;
  230. spin_lock_init(&queue->lock);
  231. INIT_LIST_HEAD(&queue->ent_avail_queue);
  232. INIT_LIST_HEAD(&queue->ent_commit_queue);
  233. INIT_LIST_HEAD(&queue->ent_w_req_queue);
  234. INIT_LIST_HEAD(&queue->ent_in_userspace);
  235. INIT_LIST_HEAD(&queue->fuse_req_queue);
  236. INIT_LIST_HEAD(&queue->fuse_req_bg_queue);
  237. INIT_LIST_HEAD(&queue->ent_released);
  238. queue->fpq.processing = pq;
  239. fuse_pqueue_init(&queue->fpq);
  240. spin_lock(&fc->lock);
  241. if (ring->queues[qid]) {
  242. spin_unlock(&fc->lock);
  243. kfree(queue->fpq.processing);
  244. kfree(queue);
  245. return ring->queues[qid];
  246. }
  247. /*
  248. * write_once and lock as the caller mostly doesn't take the lock at all
  249. */
  250. WRITE_ONCE(ring->queues[qid], queue);
  251. spin_unlock(&fc->lock);
  252. return queue;
  253. }
  254. static void fuse_uring_stop_fuse_req_end(struct fuse_req *req)
  255. {
  256. clear_bit(FR_SENT, &req->flags);
  257. req->out.h.error = -ECONNABORTED;
  258. fuse_request_end(req);
  259. }
  260. /*
  261. * Release a request/entry on connection tear down
  262. */
  263. static void fuse_uring_entry_teardown(struct fuse_ring_ent *ent)
  264. {
  265. struct fuse_req *req;
  266. struct io_uring_cmd *cmd;
  267. struct fuse_ring_queue *queue = ent->queue;
  268. spin_lock(&queue->lock);
  269. cmd = ent->cmd;
  270. ent->cmd = NULL;
  271. req = ent->fuse_req;
  272. ent->fuse_req = NULL;
  273. if (req) {
  274. /* remove entry from queue->fpq->processing */
  275. list_del_init(&req->list);
  276. }
  277. /*
  278. * The entry must not be freed immediately, due to access of direct
  279. * pointer access of entries through IO_URING_F_CANCEL - there is a risk
  280. * of race between daemon termination (which triggers IO_URING_F_CANCEL
  281. * and accesses entries without checking the list state first
  282. */
  283. list_move(&ent->list, &queue->ent_released);
  284. ent->state = FRRS_RELEASED;
  285. spin_unlock(&queue->lock);
  286. if (cmd)
  287. io_uring_cmd_done(cmd, -ENOTCONN, IO_URING_F_UNLOCKED);
  288. if (req)
  289. fuse_uring_stop_fuse_req_end(req);
  290. }
  291. static void fuse_uring_stop_list_entries(struct list_head *head,
  292. struct fuse_ring_queue *queue,
  293. enum fuse_ring_req_state exp_state)
  294. {
  295. struct fuse_ring *ring = queue->ring;
  296. struct fuse_ring_ent *ent, *next;
  297. ssize_t queue_refs = SSIZE_MAX;
  298. LIST_HEAD(to_teardown);
  299. spin_lock(&queue->lock);
  300. list_for_each_entry_safe(ent, next, head, list) {
  301. if (ent->state != exp_state) {
  302. pr_warn("entry teardown qid=%d state=%d expected=%d",
  303. queue->qid, ent->state, exp_state);
  304. continue;
  305. }
  306. ent->state = FRRS_TEARDOWN;
  307. list_move(&ent->list, &to_teardown);
  308. }
  309. spin_unlock(&queue->lock);
  310. /* no queue lock to avoid lock order issues */
  311. list_for_each_entry_safe(ent, next, &to_teardown, list) {
  312. fuse_uring_entry_teardown(ent);
  313. queue_refs = atomic_dec_return(&ring->queue_refs);
  314. WARN_ON_ONCE(queue_refs < 0);
  315. }
  316. }
  317. static void fuse_uring_teardown_entries(struct fuse_ring_queue *queue)
  318. {
  319. fuse_uring_stop_list_entries(&queue->ent_in_userspace, queue,
  320. FRRS_USERSPACE);
  321. fuse_uring_stop_list_entries(&queue->ent_avail_queue, queue,
  322. FRRS_AVAILABLE);
  323. }
  324. /*
  325. * Log state debug info
  326. */
  327. static void fuse_uring_log_ent_state(struct fuse_ring *ring)
  328. {
  329. int qid;
  330. struct fuse_ring_ent *ent;
  331. for (qid = 0; qid < ring->nr_queues; qid++) {
  332. struct fuse_ring_queue *queue = ring->queues[qid];
  333. if (!queue)
  334. continue;
  335. spin_lock(&queue->lock);
  336. /*
  337. * Log entries from the intermediate queue, the other queues
  338. * should be empty
  339. */
  340. list_for_each_entry(ent, &queue->ent_w_req_queue, list) {
  341. pr_info(" ent-req-queue ring=%p qid=%d ent=%p state=%d\n",
  342. ring, qid, ent, ent->state);
  343. }
  344. list_for_each_entry(ent, &queue->ent_commit_queue, list) {
  345. pr_info(" ent-commit-queue ring=%p qid=%d ent=%p state=%d\n",
  346. ring, qid, ent, ent->state);
  347. }
  348. spin_unlock(&queue->lock);
  349. }
  350. ring->stop_debug_log = 1;
  351. }
  352. static void fuse_uring_async_stop_queues(struct work_struct *work)
  353. {
  354. int qid;
  355. struct fuse_ring *ring =
  356. container_of(work, struct fuse_ring, async_teardown_work.work);
  357. /* XXX code dup */
  358. for (qid = 0; qid < ring->nr_queues; qid++) {
  359. struct fuse_ring_queue *queue = READ_ONCE(ring->queues[qid]);
  360. if (!queue)
  361. continue;
  362. fuse_uring_teardown_entries(queue);
  363. }
  364. /*
  365. * Some ring entries might be in the middle of IO operations,
  366. * i.e. in process to get handled by file_operations::uring_cmd
  367. * or on the way to userspace - we could handle that with conditions in
  368. * run time code, but easier/cleaner to have an async tear down handler
  369. * If there are still queue references left
  370. */
  371. if (atomic_read(&ring->queue_refs) > 0) {
  372. if (time_after(jiffies,
  373. ring->teardown_time + FUSE_URING_TEARDOWN_TIMEOUT))
  374. fuse_uring_log_ent_state(ring);
  375. schedule_delayed_work(&ring->async_teardown_work,
  376. FUSE_URING_TEARDOWN_INTERVAL);
  377. } else {
  378. wake_up_all(&ring->stop_waitq);
  379. }
  380. }
  381. /*
  382. * Stop the ring queues
  383. */
  384. void fuse_uring_stop_queues(struct fuse_ring *ring)
  385. {
  386. int qid;
  387. for (qid = 0; qid < ring->nr_queues; qid++) {
  388. struct fuse_ring_queue *queue = READ_ONCE(ring->queues[qid]);
  389. if (!queue)
  390. continue;
  391. fuse_uring_teardown_entries(queue);
  392. }
  393. if (atomic_read(&ring->queue_refs) > 0) {
  394. ring->teardown_time = jiffies;
  395. INIT_DELAYED_WORK(&ring->async_teardown_work,
  396. fuse_uring_async_stop_queues);
  397. schedule_delayed_work(&ring->async_teardown_work,
  398. FUSE_URING_TEARDOWN_INTERVAL);
  399. } else {
  400. wake_up_all(&ring->stop_waitq);
  401. }
  402. }
  403. /*
  404. * Handle IO_URING_F_CANCEL, typically should come on daemon termination.
  405. *
  406. * Releasing the last entry should trigger fuse_dev_release() if
  407. * the daemon was terminated
  408. */
  409. static void fuse_uring_cancel(struct io_uring_cmd *cmd,
  410. unsigned int issue_flags)
  411. {
  412. struct fuse_ring_ent *ent = uring_cmd_to_ring_ent(cmd);
  413. struct fuse_ring_queue *queue;
  414. bool need_cmd_done = false;
  415. /*
  416. * direct access on ent - it must not be destructed as long as
  417. * IO_URING_F_CANCEL might come up
  418. */
  419. queue = ent->queue;
  420. spin_lock(&queue->lock);
  421. if (ent->state == FRRS_AVAILABLE) {
  422. ent->state = FRRS_USERSPACE;
  423. list_move_tail(&ent->list, &queue->ent_in_userspace);
  424. need_cmd_done = true;
  425. ent->cmd = NULL;
  426. }
  427. spin_unlock(&queue->lock);
  428. if (need_cmd_done) {
  429. /* no queue lock to avoid lock order issues */
  430. io_uring_cmd_done(cmd, -ENOTCONN, issue_flags);
  431. }
  432. }
  433. static void fuse_uring_prepare_cancel(struct io_uring_cmd *cmd, int issue_flags,
  434. struct fuse_ring_ent *ring_ent)
  435. {
  436. uring_cmd_set_ring_ent(cmd, ring_ent);
  437. io_uring_cmd_mark_cancelable(cmd, issue_flags);
  438. }
  439. /*
  440. * Checks for errors and stores it into the request
  441. */
  442. static int fuse_uring_out_header_has_err(struct fuse_out_header *oh,
  443. struct fuse_req *req,
  444. struct fuse_conn *fc)
  445. {
  446. int err;
  447. err = -EINVAL;
  448. if (oh->unique == 0) {
  449. /* Not supported through io-uring yet */
  450. pr_warn_once("notify through fuse-io-uring not supported\n");
  451. goto err;
  452. }
  453. if (oh->error <= -ERESTARTSYS || oh->error > 0)
  454. goto err;
  455. if (oh->error) {
  456. err = oh->error;
  457. goto err;
  458. }
  459. err = -ENOENT;
  460. if ((oh->unique & ~FUSE_INT_REQ_BIT) != req->in.h.unique) {
  461. pr_warn_ratelimited("unique mismatch, expected: %llu got %llu\n",
  462. req->in.h.unique,
  463. oh->unique & ~FUSE_INT_REQ_BIT);
  464. goto err;
  465. }
  466. /*
  467. * Is it an interrupt reply ID?
  468. * XXX: Not supported through fuse-io-uring yet, it should not even
  469. * find the request - should not happen.
  470. */
  471. WARN_ON_ONCE(oh->unique & FUSE_INT_REQ_BIT);
  472. err = 0;
  473. err:
  474. return err;
  475. }
  476. static int fuse_uring_copy_from_ring(struct fuse_ring *ring,
  477. struct fuse_req *req,
  478. struct fuse_ring_ent *ent)
  479. {
  480. struct fuse_copy_state cs;
  481. struct fuse_args *args = req->args;
  482. struct iov_iter iter;
  483. int err;
  484. struct fuse_uring_ent_in_out ring_in_out;
  485. err = copy_from_user(&ring_in_out, &ent->headers->ring_ent_in_out,
  486. sizeof(ring_in_out));
  487. if (err)
  488. return -EFAULT;
  489. err = import_ubuf(ITER_SOURCE, ent->payload, ring->max_payload_sz,
  490. &iter);
  491. if (err)
  492. return err;
  493. fuse_copy_init(&cs, false, &iter);
  494. cs.is_uring = true;
  495. cs.req = req;
  496. err = fuse_copy_out_args(&cs, args, ring_in_out.payload_sz);
  497. fuse_copy_finish(&cs);
  498. return err;
  499. }
  500. /*
  501. * Copy data from the req to the ring buffer
  502. */
  503. static int fuse_uring_args_to_ring(struct fuse_ring *ring, struct fuse_req *req,
  504. struct fuse_ring_ent *ent)
  505. {
  506. struct fuse_copy_state cs;
  507. struct fuse_args *args = req->args;
  508. struct fuse_in_arg *in_args = args->in_args;
  509. int num_args = args->in_numargs;
  510. int err;
  511. struct iov_iter iter;
  512. struct fuse_uring_ent_in_out ent_in_out = {
  513. .flags = 0,
  514. .commit_id = req->in.h.unique,
  515. };
  516. err = import_ubuf(ITER_DEST, ent->payload, ring->max_payload_sz, &iter);
  517. if (err) {
  518. pr_info_ratelimited("fuse: Import of user buffer failed\n");
  519. return err;
  520. }
  521. fuse_copy_init(&cs, true, &iter);
  522. cs.is_uring = true;
  523. cs.req = req;
  524. if (num_args > 0) {
  525. /*
  526. * Expectation is that the first argument is the per op header.
  527. * Some op code have that as zero size.
  528. */
  529. if (args->in_args[0].size > 0) {
  530. err = copy_to_user(&ent->headers->op_in, in_args->value,
  531. in_args->size);
  532. if (err) {
  533. pr_info_ratelimited(
  534. "Copying the header failed.\n");
  535. return -EFAULT;
  536. }
  537. }
  538. in_args++;
  539. num_args--;
  540. }
  541. /* copy the payload */
  542. err = fuse_copy_args(&cs, num_args, args->in_pages,
  543. (struct fuse_arg *)in_args, 0);
  544. fuse_copy_finish(&cs);
  545. if (err) {
  546. pr_info_ratelimited("%s fuse_copy_args failed\n", __func__);
  547. return err;
  548. }
  549. ent_in_out.payload_sz = cs.ring.copied_sz;
  550. err = copy_to_user(&ent->headers->ring_ent_in_out, &ent_in_out,
  551. sizeof(ent_in_out));
  552. return err ? -EFAULT : 0;
  553. }
  554. static int fuse_uring_copy_to_ring(struct fuse_ring_ent *ent,
  555. struct fuse_req *req)
  556. {
  557. struct fuse_ring_queue *queue = ent->queue;
  558. struct fuse_ring *ring = queue->ring;
  559. int err;
  560. err = -EIO;
  561. if (WARN_ON(ent->state != FRRS_FUSE_REQ)) {
  562. pr_err("qid=%d ring-req=%p invalid state %d on send\n",
  563. queue->qid, ent, ent->state);
  564. return err;
  565. }
  566. err = -EINVAL;
  567. if (WARN_ON(req->in.h.unique == 0))
  568. return err;
  569. /* copy the request */
  570. err = fuse_uring_args_to_ring(ring, req, ent);
  571. if (unlikely(err)) {
  572. pr_info_ratelimited("Copy to ring failed: %d\n", err);
  573. return err;
  574. }
  575. /* copy fuse_in_header */
  576. err = copy_to_user(&ent->headers->in_out, &req->in.h,
  577. sizeof(req->in.h));
  578. if (err) {
  579. err = -EFAULT;
  580. return err;
  581. }
  582. return 0;
  583. }
  584. static int fuse_uring_prepare_send(struct fuse_ring_ent *ent,
  585. struct fuse_req *req)
  586. {
  587. int err;
  588. err = fuse_uring_copy_to_ring(ent, req);
  589. if (!err)
  590. set_bit(FR_SENT, &req->flags);
  591. else
  592. fuse_uring_req_end(ent, req, err);
  593. return err;
  594. }
  595. /*
  596. * Write data to the ring buffer and send the request to userspace,
  597. * userspace will read it
  598. * This is comparable with classical read(/dev/fuse)
  599. */
  600. static int fuse_uring_send_next_to_ring(struct fuse_ring_ent *ent,
  601. struct fuse_req *req,
  602. unsigned int issue_flags)
  603. {
  604. struct fuse_ring_queue *queue = ent->queue;
  605. int err;
  606. struct io_uring_cmd *cmd;
  607. err = fuse_uring_prepare_send(ent, req);
  608. if (err)
  609. return err;
  610. spin_lock(&queue->lock);
  611. cmd = ent->cmd;
  612. ent->cmd = NULL;
  613. ent->state = FRRS_USERSPACE;
  614. list_move_tail(&ent->list, &queue->ent_in_userspace);
  615. spin_unlock(&queue->lock);
  616. io_uring_cmd_done(cmd, 0, issue_flags);
  617. return 0;
  618. }
  619. /*
  620. * Make a ring entry available for fuse_req assignment
  621. */
  622. static void fuse_uring_ent_avail(struct fuse_ring_ent *ent,
  623. struct fuse_ring_queue *queue)
  624. {
  625. WARN_ON_ONCE(!ent->cmd);
  626. list_move(&ent->list, &queue->ent_avail_queue);
  627. ent->state = FRRS_AVAILABLE;
  628. }
  629. /* Used to find the request on SQE commit */
  630. static void fuse_uring_add_to_pq(struct fuse_ring_ent *ent,
  631. struct fuse_req *req)
  632. {
  633. struct fuse_ring_queue *queue = ent->queue;
  634. struct fuse_pqueue *fpq = &queue->fpq;
  635. unsigned int hash;
  636. req->ring_entry = ent;
  637. hash = fuse_req_hash(req->in.h.unique);
  638. list_move_tail(&req->list, &fpq->processing[hash]);
  639. }
  640. /*
  641. * Assign a fuse queue entry to the given entry
  642. */
  643. static void fuse_uring_add_req_to_ring_ent(struct fuse_ring_ent *ent,
  644. struct fuse_req *req)
  645. {
  646. struct fuse_ring_queue *queue = ent->queue;
  647. lockdep_assert_held(&queue->lock);
  648. if (WARN_ON_ONCE(ent->state != FRRS_AVAILABLE &&
  649. ent->state != FRRS_COMMIT)) {
  650. pr_warn("%s qid=%d state=%d\n", __func__, ent->queue->qid,
  651. ent->state);
  652. }
  653. clear_bit(FR_PENDING, &req->flags);
  654. ent->fuse_req = req;
  655. ent->state = FRRS_FUSE_REQ;
  656. list_move_tail(&ent->list, &queue->ent_w_req_queue);
  657. fuse_uring_add_to_pq(ent, req);
  658. }
  659. /* Fetch the next fuse request if available */
  660. static struct fuse_req *fuse_uring_ent_assign_req(struct fuse_ring_ent *ent)
  661. __must_hold(&queue->lock)
  662. {
  663. struct fuse_req *req;
  664. struct fuse_ring_queue *queue = ent->queue;
  665. struct list_head *req_queue = &queue->fuse_req_queue;
  666. lockdep_assert_held(&queue->lock);
  667. /* get and assign the next entry while it is still holding the lock */
  668. req = list_first_entry_or_null(req_queue, struct fuse_req, list);
  669. if (req)
  670. fuse_uring_add_req_to_ring_ent(ent, req);
  671. return req;
  672. }
  673. /*
  674. * Read data from the ring buffer, which user space has written to
  675. * This is comparible with handling of classical write(/dev/fuse).
  676. * Also make the ring request available again for new fuse requests.
  677. */
  678. static void fuse_uring_commit(struct fuse_ring_ent *ent, struct fuse_req *req,
  679. unsigned int issue_flags)
  680. {
  681. struct fuse_ring *ring = ent->queue->ring;
  682. struct fuse_conn *fc = ring->fc;
  683. ssize_t err = 0;
  684. err = copy_from_user(&req->out.h, &ent->headers->in_out,
  685. sizeof(req->out.h));
  686. if (err) {
  687. req->out.h.error = -EFAULT;
  688. goto out;
  689. }
  690. err = fuse_uring_out_header_has_err(&req->out.h, req, fc);
  691. if (err) {
  692. /* req->out.h.error already set */
  693. goto out;
  694. }
  695. err = fuse_uring_copy_from_ring(ring, req, ent);
  696. out:
  697. fuse_uring_req_end(ent, req, err);
  698. }
  699. /*
  700. * Get the next fuse req and send it
  701. */
  702. static void fuse_uring_next_fuse_req(struct fuse_ring_ent *ent,
  703. struct fuse_ring_queue *queue,
  704. unsigned int issue_flags)
  705. {
  706. int err;
  707. struct fuse_req *req;
  708. retry:
  709. spin_lock(&queue->lock);
  710. fuse_uring_ent_avail(ent, queue);
  711. req = fuse_uring_ent_assign_req(ent);
  712. spin_unlock(&queue->lock);
  713. if (req) {
  714. err = fuse_uring_send_next_to_ring(ent, req, issue_flags);
  715. if (err)
  716. goto retry;
  717. }
  718. }
  719. static int fuse_ring_ent_set_commit(struct fuse_ring_ent *ent)
  720. {
  721. struct fuse_ring_queue *queue = ent->queue;
  722. lockdep_assert_held(&queue->lock);
  723. if (WARN_ON_ONCE(ent->state != FRRS_USERSPACE))
  724. return -EIO;
  725. ent->state = FRRS_COMMIT;
  726. list_move(&ent->list, &queue->ent_commit_queue);
  727. return 0;
  728. }
  729. /* FUSE_URING_CMD_COMMIT_AND_FETCH handler */
  730. static int fuse_uring_commit_fetch(struct io_uring_cmd *cmd, int issue_flags,
  731. struct fuse_conn *fc)
  732. {
  733. const struct fuse_uring_cmd_req *cmd_req = io_uring_sqe128_cmd(cmd->sqe,
  734. struct fuse_uring_cmd_req);
  735. struct fuse_ring_ent *ent;
  736. int err;
  737. struct fuse_ring *ring = fc->ring;
  738. struct fuse_ring_queue *queue;
  739. uint64_t commit_id = READ_ONCE(cmd_req->commit_id);
  740. unsigned int qid = READ_ONCE(cmd_req->qid);
  741. struct fuse_pqueue *fpq;
  742. struct fuse_req *req;
  743. err = -ENOTCONN;
  744. if (!ring)
  745. return err;
  746. if (qid >= ring->nr_queues)
  747. return -EINVAL;
  748. queue = ring->queues[qid];
  749. if (!queue)
  750. return err;
  751. fpq = &queue->fpq;
  752. if (!READ_ONCE(fc->connected) || READ_ONCE(queue->stopped))
  753. return err;
  754. spin_lock(&queue->lock);
  755. /* Find a request based on the unique ID of the fuse request
  756. * This should get revised, as it needs a hash calculation and list
  757. * search. And full struct fuse_pqueue is needed (memory overhead).
  758. * As well as the link from req to ring_ent.
  759. */
  760. req = fuse_request_find(fpq, commit_id);
  761. err = -ENOENT;
  762. if (!req) {
  763. pr_info("qid=%d commit_id %llu not found\n", queue->qid,
  764. commit_id);
  765. spin_unlock(&queue->lock);
  766. return err;
  767. }
  768. list_del_init(&req->list);
  769. ent = req->ring_entry;
  770. req->ring_entry = NULL;
  771. err = fuse_ring_ent_set_commit(ent);
  772. if (err != 0) {
  773. pr_info_ratelimited("qid=%d commit_id %llu state %d",
  774. queue->qid, commit_id, ent->state);
  775. spin_unlock(&queue->lock);
  776. req->out.h.error = err;
  777. clear_bit(FR_SENT, &req->flags);
  778. fuse_request_end(req);
  779. return err;
  780. }
  781. ent->cmd = cmd;
  782. spin_unlock(&queue->lock);
  783. /* without the queue lock, as other locks are taken */
  784. fuse_uring_prepare_cancel(cmd, issue_flags, ent);
  785. fuse_uring_commit(ent, req, issue_flags);
  786. /*
  787. * Fetching the next request is absolutely required as queued
  788. * fuse requests would otherwise not get processed - committing
  789. * and fetching is done in one step vs legacy fuse, which has separated
  790. * read (fetch request) and write (commit result).
  791. */
  792. fuse_uring_next_fuse_req(ent, queue, issue_flags);
  793. return 0;
  794. }
  795. static bool is_ring_ready(struct fuse_ring *ring, int current_qid)
  796. {
  797. int qid;
  798. struct fuse_ring_queue *queue;
  799. bool ready = true;
  800. for (qid = 0; qid < ring->nr_queues && ready; qid++) {
  801. if (current_qid == qid)
  802. continue;
  803. queue = ring->queues[qid];
  804. if (!queue) {
  805. ready = false;
  806. break;
  807. }
  808. spin_lock(&queue->lock);
  809. if (list_empty(&queue->ent_avail_queue))
  810. ready = false;
  811. spin_unlock(&queue->lock);
  812. }
  813. return ready;
  814. }
  815. /*
  816. * fuse_uring_req_fetch command handling
  817. */
  818. static void fuse_uring_do_register(struct fuse_ring_ent *ent,
  819. struct io_uring_cmd *cmd,
  820. unsigned int issue_flags)
  821. {
  822. struct fuse_ring_queue *queue = ent->queue;
  823. struct fuse_ring *ring = queue->ring;
  824. struct fuse_conn *fc = ring->fc;
  825. struct fuse_iqueue *fiq = &fc->iq;
  826. fuse_uring_prepare_cancel(cmd, issue_flags, ent);
  827. spin_lock(&queue->lock);
  828. ent->cmd = cmd;
  829. fuse_uring_ent_avail(ent, queue);
  830. spin_unlock(&queue->lock);
  831. if (!ring->ready) {
  832. bool ready = is_ring_ready(ring, queue->qid);
  833. if (ready) {
  834. WRITE_ONCE(fiq->ops, &fuse_io_uring_ops);
  835. WRITE_ONCE(ring->ready, true);
  836. wake_up_all(&fc->blocked_waitq);
  837. }
  838. }
  839. }
  840. /*
  841. * sqe->addr is a ptr to an iovec array, iov[0] has the headers, iov[1]
  842. * the payload
  843. */
  844. static int fuse_uring_get_iovec_from_sqe(const struct io_uring_sqe *sqe,
  845. struct iovec iov[FUSE_URING_IOV_SEGS])
  846. {
  847. struct iovec __user *uiov = u64_to_user_ptr(READ_ONCE(sqe->addr));
  848. struct iov_iter iter;
  849. ssize_t ret;
  850. if (sqe->len != FUSE_URING_IOV_SEGS)
  851. return -EINVAL;
  852. /*
  853. * Direction for buffer access will actually be READ and WRITE,
  854. * using write for the import should include READ access as well.
  855. */
  856. ret = import_iovec(WRITE, uiov, FUSE_URING_IOV_SEGS,
  857. FUSE_URING_IOV_SEGS, &iov, &iter);
  858. if (ret < 0)
  859. return ret;
  860. return 0;
  861. }
  862. static struct fuse_ring_ent *
  863. fuse_uring_create_ring_ent(struct io_uring_cmd *cmd,
  864. struct fuse_ring_queue *queue)
  865. {
  866. struct fuse_ring *ring = queue->ring;
  867. struct fuse_ring_ent *ent;
  868. size_t payload_size;
  869. struct iovec iov[FUSE_URING_IOV_SEGS];
  870. int err;
  871. err = fuse_uring_get_iovec_from_sqe(cmd->sqe, iov);
  872. if (err) {
  873. pr_info_ratelimited("Failed to get iovec from sqe, err=%d\n",
  874. err);
  875. return ERR_PTR(err);
  876. }
  877. err = -EINVAL;
  878. if (iov[0].iov_len < sizeof(struct fuse_uring_req_header)) {
  879. pr_info_ratelimited("Invalid header len %zu\n", iov[0].iov_len);
  880. return ERR_PTR(err);
  881. }
  882. payload_size = iov[1].iov_len;
  883. if (payload_size < ring->max_payload_sz) {
  884. pr_info_ratelimited("Invalid req payload len %zu\n",
  885. payload_size);
  886. return ERR_PTR(err);
  887. }
  888. err = -ENOMEM;
  889. ent = kzalloc_obj(*ent, GFP_KERNEL_ACCOUNT);
  890. if (!ent)
  891. return ERR_PTR(err);
  892. INIT_LIST_HEAD(&ent->list);
  893. ent->queue = queue;
  894. ent->headers = iov[0].iov_base;
  895. ent->payload = iov[1].iov_base;
  896. atomic_inc(&ring->queue_refs);
  897. return ent;
  898. }
  899. /*
  900. * Register header and payload buffer with the kernel and puts the
  901. * entry as "ready to get fuse requests" on the queue
  902. */
  903. static int fuse_uring_register(struct io_uring_cmd *cmd,
  904. unsigned int issue_flags, struct fuse_conn *fc)
  905. {
  906. const struct fuse_uring_cmd_req *cmd_req = io_uring_sqe128_cmd(cmd->sqe,
  907. struct fuse_uring_cmd_req);
  908. struct fuse_ring *ring = smp_load_acquire(&fc->ring);
  909. struct fuse_ring_queue *queue;
  910. struct fuse_ring_ent *ent;
  911. int err;
  912. unsigned int qid = READ_ONCE(cmd_req->qid);
  913. err = -ENOMEM;
  914. if (!ring) {
  915. ring = fuse_uring_create(fc);
  916. if (!ring)
  917. return err;
  918. }
  919. if (qid >= ring->nr_queues) {
  920. pr_info_ratelimited("fuse: Invalid ring qid %u\n", qid);
  921. return -EINVAL;
  922. }
  923. queue = ring->queues[qid];
  924. if (!queue) {
  925. queue = fuse_uring_create_queue(ring, qid);
  926. if (!queue)
  927. return err;
  928. }
  929. /*
  930. * The created queue above does not need to be destructed in
  931. * case of entry errors below, will be done at ring destruction time.
  932. */
  933. ent = fuse_uring_create_ring_ent(cmd, queue);
  934. if (IS_ERR(ent))
  935. return PTR_ERR(ent);
  936. fuse_uring_do_register(ent, cmd, issue_flags);
  937. return 0;
  938. }
  939. /*
  940. * Entry function from io_uring to handle the given passthrough command
  941. * (op code IORING_OP_URING_CMD)
  942. */
  943. int fuse_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
  944. {
  945. struct fuse_dev *fud;
  946. struct fuse_conn *fc;
  947. u32 cmd_op = cmd->cmd_op;
  948. int err;
  949. if ((unlikely(issue_flags & IO_URING_F_CANCEL))) {
  950. fuse_uring_cancel(cmd, issue_flags);
  951. return 0;
  952. }
  953. /* This extra SQE size holds struct fuse_uring_cmd_req */
  954. if (!(issue_flags & IO_URING_F_SQE128))
  955. return -EINVAL;
  956. fud = fuse_get_dev(cmd->file);
  957. if (IS_ERR(fud)) {
  958. pr_info_ratelimited("No fuse device found\n");
  959. return PTR_ERR(fud);
  960. }
  961. fc = fud->fc;
  962. /* Once a connection has io-uring enabled on it, it can't be disabled */
  963. if (!enable_uring && !fc->io_uring) {
  964. pr_info_ratelimited("fuse-io-uring is disabled\n");
  965. return -EOPNOTSUPP;
  966. }
  967. if (fc->aborted)
  968. return -ECONNABORTED;
  969. if (!fc->connected)
  970. return -ENOTCONN;
  971. /*
  972. * fuse_uring_register() needs the ring to be initialized,
  973. * we need to know the max payload size
  974. */
  975. if (!fc->initialized)
  976. return -EAGAIN;
  977. switch (cmd_op) {
  978. case FUSE_IO_URING_CMD_REGISTER:
  979. err = fuse_uring_register(cmd, issue_flags, fc);
  980. if (err) {
  981. pr_info_once("FUSE_IO_URING_CMD_REGISTER failed err=%d\n",
  982. err);
  983. fc->io_uring = 0;
  984. wake_up_all(&fc->blocked_waitq);
  985. return err;
  986. }
  987. break;
  988. case FUSE_IO_URING_CMD_COMMIT_AND_FETCH:
  989. err = fuse_uring_commit_fetch(cmd, issue_flags, fc);
  990. if (err) {
  991. pr_info_once("FUSE_IO_URING_COMMIT_AND_FETCH failed err=%d\n",
  992. err);
  993. return err;
  994. }
  995. break;
  996. default:
  997. return -EINVAL;
  998. }
  999. return -EIOCBQUEUED;
  1000. }
  1001. static void fuse_uring_send(struct fuse_ring_ent *ent, struct io_uring_cmd *cmd,
  1002. ssize_t ret, unsigned int issue_flags)
  1003. {
  1004. struct fuse_ring_queue *queue = ent->queue;
  1005. spin_lock(&queue->lock);
  1006. ent->state = FRRS_USERSPACE;
  1007. list_move_tail(&ent->list, &queue->ent_in_userspace);
  1008. ent->cmd = NULL;
  1009. spin_unlock(&queue->lock);
  1010. io_uring_cmd_done(cmd, ret, issue_flags);
  1011. }
  1012. /*
  1013. * This prepares and sends the ring request in fuse-uring task context.
  1014. * User buffers are not mapped yet - the application does not have permission
  1015. * to write to it - this has to be executed in ring task context.
  1016. */
  1017. static void fuse_uring_send_in_task(struct io_tw_req tw_req, io_tw_token_t tw)
  1018. {
  1019. unsigned int issue_flags = IO_URING_CMD_TASK_WORK_ISSUE_FLAGS;
  1020. struct io_uring_cmd *cmd = io_uring_cmd_from_tw(tw_req);
  1021. struct fuse_ring_ent *ent = uring_cmd_to_ring_ent(cmd);
  1022. struct fuse_ring_queue *queue = ent->queue;
  1023. int err;
  1024. if (!tw.cancel) {
  1025. err = fuse_uring_prepare_send(ent, ent->fuse_req);
  1026. if (err) {
  1027. fuse_uring_next_fuse_req(ent, queue, issue_flags);
  1028. return;
  1029. }
  1030. } else {
  1031. err = -ECANCELED;
  1032. }
  1033. fuse_uring_send(ent, cmd, err, issue_flags);
  1034. }
  1035. static struct fuse_ring_queue *fuse_uring_task_to_queue(struct fuse_ring *ring)
  1036. {
  1037. unsigned int qid;
  1038. struct fuse_ring_queue *queue;
  1039. qid = task_cpu(current);
  1040. if (WARN_ONCE(qid >= ring->nr_queues,
  1041. "Core number (%u) exceeds nr queues (%zu)\n", qid,
  1042. ring->nr_queues))
  1043. qid = 0;
  1044. queue = ring->queues[qid];
  1045. WARN_ONCE(!queue, "Missing queue for qid %d\n", qid);
  1046. return queue;
  1047. }
  1048. static void fuse_uring_dispatch_ent(struct fuse_ring_ent *ent)
  1049. {
  1050. struct io_uring_cmd *cmd = ent->cmd;
  1051. uring_cmd_set_ring_ent(cmd, ent);
  1052. io_uring_cmd_complete_in_task(cmd, fuse_uring_send_in_task);
  1053. }
  1054. /* queue a fuse request and send it if a ring entry is available */
  1055. void fuse_uring_queue_fuse_req(struct fuse_iqueue *fiq, struct fuse_req *req)
  1056. {
  1057. struct fuse_conn *fc = req->fm->fc;
  1058. struct fuse_ring *ring = fc->ring;
  1059. struct fuse_ring_queue *queue;
  1060. struct fuse_ring_ent *ent = NULL;
  1061. int err;
  1062. err = -EINVAL;
  1063. queue = fuse_uring_task_to_queue(ring);
  1064. if (!queue)
  1065. goto err;
  1066. fuse_request_assign_unique(fiq, req);
  1067. spin_lock(&queue->lock);
  1068. err = -ENOTCONN;
  1069. if (unlikely(queue->stopped))
  1070. goto err_unlock;
  1071. set_bit(FR_URING, &req->flags);
  1072. req->ring_queue = queue;
  1073. ent = list_first_entry_or_null(&queue->ent_avail_queue,
  1074. struct fuse_ring_ent, list);
  1075. if (ent)
  1076. fuse_uring_add_req_to_ring_ent(ent, req);
  1077. else
  1078. list_add_tail(&req->list, &queue->fuse_req_queue);
  1079. spin_unlock(&queue->lock);
  1080. if (ent)
  1081. fuse_uring_dispatch_ent(ent);
  1082. return;
  1083. err_unlock:
  1084. spin_unlock(&queue->lock);
  1085. err:
  1086. req->out.h.error = err;
  1087. clear_bit(FR_PENDING, &req->flags);
  1088. fuse_request_end(req);
  1089. }
  1090. bool fuse_uring_queue_bq_req(struct fuse_req *req)
  1091. {
  1092. struct fuse_conn *fc = req->fm->fc;
  1093. struct fuse_ring *ring = fc->ring;
  1094. struct fuse_ring_queue *queue;
  1095. struct fuse_ring_ent *ent = NULL;
  1096. queue = fuse_uring_task_to_queue(ring);
  1097. if (!queue)
  1098. return false;
  1099. spin_lock(&queue->lock);
  1100. if (unlikely(queue->stopped)) {
  1101. spin_unlock(&queue->lock);
  1102. return false;
  1103. }
  1104. set_bit(FR_URING, &req->flags);
  1105. req->ring_queue = queue;
  1106. list_add_tail(&req->list, &queue->fuse_req_bg_queue);
  1107. ent = list_first_entry_or_null(&queue->ent_avail_queue,
  1108. struct fuse_ring_ent, list);
  1109. spin_lock(&fc->bg_lock);
  1110. fc->num_background++;
  1111. if (fc->num_background == fc->max_background)
  1112. fc->blocked = 1;
  1113. fuse_uring_flush_bg(queue);
  1114. spin_unlock(&fc->bg_lock);
  1115. /*
  1116. * Due to bg_queue flush limits there might be other bg requests
  1117. * in the queue that need to be handled first. Or no further req
  1118. * might be available.
  1119. */
  1120. req = list_first_entry_or_null(&queue->fuse_req_queue, struct fuse_req,
  1121. list);
  1122. if (ent && req) {
  1123. fuse_uring_add_req_to_ring_ent(ent, req);
  1124. spin_unlock(&queue->lock);
  1125. fuse_uring_dispatch_ent(ent);
  1126. } else {
  1127. spin_unlock(&queue->lock);
  1128. }
  1129. return true;
  1130. }
  1131. bool fuse_uring_remove_pending_req(struct fuse_req *req)
  1132. {
  1133. struct fuse_ring_queue *queue = req->ring_queue;
  1134. return fuse_remove_pending_req(req, &queue->lock);
  1135. }
  1136. static const struct fuse_iqueue_ops fuse_io_uring_ops = {
  1137. /* should be send over io-uring as enhancement */
  1138. .send_forget = fuse_dev_queue_forget,
  1139. /*
  1140. * could be send over io-uring, but interrupts should be rare,
  1141. * no need to make the code complex
  1142. */
  1143. .send_interrupt = fuse_dev_queue_interrupt,
  1144. .send_req = fuse_uring_queue_fuse_req,
  1145. };