@@ -68,7 +68,6 @@ struct loop_device {
6868 struct rb_root worker_tree ;
6969 struct timer_list timer ;
7070 bool sysfs_inited ;
71- unsigned lo_nr_blocking_writes ;
7271
7372 struct request_queue * lo_queue ;
7473 struct blk_mq_tag_set tag_set ;
@@ -91,8 +90,6 @@ struct loop_cmd {
9190#define LOOP_IDLE_WORKER_TIMEOUT (60 * HZ)
9291#define LOOP_DEFAULT_HW_Q_DEPTH 128
9392
94- static void loop_queue_work (struct loop_device * lo , struct loop_cmd * cmd );
95-
9693static DEFINE_IDR (loop_index_idr );
9794static DEFINE_MUTEX (loop_ctl_mutex );
9895static DEFINE_MUTEX (loop_validate_mutex );
@@ -324,15 +321,6 @@ static void lo_rw_aio_do_completion(struct loop_cmd *cmd)
324321
325322 if (!atomic_dec_and_test (& cmd -> ref ))
326323 return ;
327-
328- /* -EAGAIN could be returned from bdev's ->ki_complete */
329- if (cmd -> ret == - EAGAIN ) {
330- struct loop_device * lo = rq -> q -> queuedata ;
331-
332- loop_queue_work (lo , cmd );
333- return ;
334- }
335-
336324 kfree (cmd -> bvec );
337325 cmd -> bvec = NULL ;
338326 if (req_op (rq ) == REQ_OP_WRITE )
@@ -349,28 +337,24 @@ static void lo_rw_aio_complete(struct kiocb *iocb, long ret)
349337 lo_rw_aio_do_completion (cmd );
350338}
351339
352- static inline unsigned lo_cmd_nr_bvec (struct loop_cmd * cmd )
340+ static int lo_rw_aio (struct loop_device * lo , struct loop_cmd * cmd ,
341+ loff_t pos , int rw )
353342{
354- struct request * rq = blk_mq_rq_from_pdu ( cmd ) ;
343+ struct iov_iter iter ;
355344 struct req_iterator rq_iter ;
345+ struct bio_vec * bvec ;
346+ struct request * rq = blk_mq_rq_from_pdu (cmd );
347+ struct bio * bio = rq -> bio ;
348+ struct file * file = lo -> lo_backing_file ;
356349 struct bio_vec tmp ;
350+ unsigned int offset ;
357351 int nr_bvec = 0 ;
352+ int ret ;
358353
359354 rq_for_each_bvec (tmp , rq , rq_iter )
360355 nr_bvec ++ ;
361356
362- return nr_bvec ;
363- }
364-
365- static int lo_rw_aio_prep (struct loop_device * lo , struct loop_cmd * cmd ,
366- unsigned nr_bvec , loff_t pos )
367- {
368- struct request * rq = blk_mq_rq_from_pdu (cmd );
369-
370357 if (rq -> bio != rq -> biotail ) {
371- struct req_iterator rq_iter ;
372- struct bio_vec * bvec ;
373- struct bio_vec tmp ;
374358
375359 bvec = kmalloc_array (nr_bvec , sizeof (struct bio_vec ),
376360 GFP_NOIO );
@@ -388,12 +372,24 @@ static int lo_rw_aio_prep(struct loop_device *lo, struct loop_cmd *cmd,
388372 * bvec = tmp ;
389373 bvec ++ ;
390374 }
375+ bvec = cmd -> bvec ;
376+ offset = 0 ;
391377 } else {
392- cmd -> bvec = NULL ;
378+ /*
379+ * Same here, this bio may be started from the middle of the
380+ * 'bvec' because of bio splitting, so offset from the bvec
381+ * must be passed to iov iterator
382+ */
383+ offset = bio -> bi_iter .bi_bvec_done ;
384+ bvec = __bvec_iter_bvec (bio -> bi_io_vec , bio -> bi_iter );
393385 }
386+ atomic_set (& cmd -> ref , 2 );
387+
388+ iov_iter_bvec (& iter , rw , bvec , nr_bvec , blk_rq_bytes (rq ));
389+ iter .iov_offset = offset ;
394390
395391 cmd -> iocb .ki_pos = pos ;
396- cmd -> iocb .ki_filp = lo -> lo_backing_file ;
392+ cmd -> iocb .ki_filp = file ;
397393 cmd -> iocb .ki_ioprio = req_get_ioprio (rq );
398394 if (cmd -> use_aio ) {
399395 cmd -> iocb .ki_complete = lo_rw_aio_complete ;
@@ -402,35 +398,6 @@ static int lo_rw_aio_prep(struct loop_device *lo, struct loop_cmd *cmd,
402398 cmd -> iocb .ki_complete = NULL ;
403399 cmd -> iocb .ki_flags = 0 ;
404400 }
405- return 0 ;
406- }
407-
408- static int lo_submit_rw_aio (struct loop_device * lo , struct loop_cmd * cmd ,
409- int nr_bvec , int rw )
410- {
411- struct request * rq = blk_mq_rq_from_pdu (cmd );
412- struct file * file = lo -> lo_backing_file ;
413- struct iov_iter iter ;
414- int ret ;
415-
416- if (cmd -> bvec ) {
417- iov_iter_bvec (& iter , rw , cmd -> bvec , nr_bvec , blk_rq_bytes (rq ));
418- iter .iov_offset = 0 ;
419- } else {
420- struct bio * bio = rq -> bio ;
421- struct bio_vec * bvec = __bvec_iter_bvec (bio -> bi_io_vec ,
422- bio -> bi_iter );
423-
424- /*
425- * Same here, this bio may be started from the middle of the
426- * 'bvec' because of bio splitting, so offset from the bvec
427- * must be passed to iov iterator
428- */
429- iov_iter_bvec (& iter , rw , bvec , nr_bvec , blk_rq_bytes (rq ));
430- iter .iov_offset = bio -> bi_iter .bi_bvec_done ;
431- }
432- atomic_set (& cmd -> ref , 2 );
433-
434401
435402 if (rw == ITER_SOURCE ) {
436403 kiocb_start_write (& cmd -> iocb );
@@ -439,84 +406,12 @@ static int lo_submit_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
439406 ret = file -> f_op -> read_iter (& cmd -> iocb , & iter );
440407
441408 lo_rw_aio_do_completion (cmd );
442- return ret ;
443- }
444-
445- static bool lo_backfile_support_nowait (const struct loop_device * lo )
446- {
447- return lo -> lo_backing_file -> f_mode & FMODE_NOWAIT ;
448- }
449409
450- static int lo_rw_aio (struct loop_device * lo , struct loop_cmd * cmd ,
451- loff_t pos , int rw )
452- {
453- int nr_bvec = lo_cmd_nr_bvec (cmd );
454- int ret ;
455-
456- /* prepared already if we have tried nowait */
457- if (!cmd -> use_aio || !lo_backfile_support_nowait (lo )) {
458- ret = lo_rw_aio_prep (lo , cmd , nr_bvec , pos );
459- if (unlikely (ret ))
460- goto fail ;
461- }
462-
463- cmd -> iocb .ki_flags &= ~IOCB_NOWAIT ;
464- ret = lo_submit_rw_aio (lo , cmd , nr_bvec , rw );
465- fail :
466410 if (ret != - EIOCBQUEUED )
467411 lo_rw_aio_complete (& cmd -> iocb , ret );
468412 return - EIOCBQUEUED ;
469413}
470414
471- static inline bool lo_aio_try_nowait (struct loop_device * lo ,
472- struct loop_cmd * cmd )
473- {
474- struct file * file = lo -> lo_backing_file ;
475- struct inode * inode = file -> f_mapping -> host ;
476- struct request * rq = blk_mq_rq_from_pdu (cmd );
477-
478- /* NOWAIT works fine for backing block device */
479- if (S_ISBLK (inode -> i_mode ))
480- return true;
481-
482- /*
483- * NOWAIT is supposed to be fine for READ without contending with
484- * blocking WRITE
485- */
486- if (req_op (rq ) == REQ_OP_READ )
487- return true;
488-
489- /*
490- * If there is any queued non-NOWAIT async WRITE , don't try new
491- * NOWAIT WRITE for avoiding contention
492- *
493- * Here we focus on handling stable FS block mapping via NOWAIT
494- */
495- return READ_ONCE (lo -> lo_nr_blocking_writes ) == 0 ;
496- }
497-
498- static int lo_rw_aio_nowait (struct loop_device * lo , struct loop_cmd * cmd ,
499- int rw )
500- {
501- struct request * rq = blk_mq_rq_from_pdu (cmd );
502- loff_t pos = ((loff_t ) blk_rq_pos (rq ) << 9 ) + lo -> lo_offset ;
503- int nr_bvec = lo_cmd_nr_bvec (cmd );
504- int ret = lo_rw_aio_prep (lo , cmd , nr_bvec , pos );
505-
506- if (unlikely (ret ))
507- goto fail ;
508-
509- if (!lo_aio_try_nowait (lo , cmd ))
510- return - EAGAIN ;
511-
512- cmd -> iocb .ki_flags |= IOCB_NOWAIT ;
513- ret = lo_submit_rw_aio (lo , cmd , nr_bvec , rw );
514- fail :
515- if (ret != - EIOCBQUEUED && ret != - EAGAIN )
516- lo_rw_aio_complete (& cmd -> iocb , ret );
517- return ret ;
518- }
519-
520415static int do_req_filebacked (struct loop_device * lo , struct request * rq )
521416{
522417 struct loop_cmd * cmd = blk_mq_rq_to_pdu (rq );
@@ -811,19 +706,12 @@ static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf)
811706 return sysfs_emit (buf , "%s\n" , dio ? "1" : "0" );
812707}
813708
814- static ssize_t loop_attr_nr_blocking_writes_show (struct loop_device * lo ,
815- char * buf )
816- {
817- return sysfs_emit (buf , "%u\n" , lo -> lo_nr_blocking_writes );
818- }
819-
820709LOOP_ATTR_RO (backing_file );
821710LOOP_ATTR_RO (offset );
822711LOOP_ATTR_RO (sizelimit );
823712LOOP_ATTR_RO (autoclear );
824713LOOP_ATTR_RO (partscan );
825714LOOP_ATTR_RO (dio );
826- LOOP_ATTR_RO (nr_blocking_writes );
827715
828716static struct attribute * loop_attrs [] = {
829717 & loop_attr_backing_file .attr ,
@@ -832,7 +720,6 @@ static struct attribute *loop_attrs[] = {
832720 & loop_attr_autoclear .attr ,
833721 & loop_attr_partscan .attr ,
834722 & loop_attr_dio .attr ,
835- & loop_attr_nr_blocking_writes .attr ,
836723 NULL ,
837724};
838725
@@ -908,48 +795,13 @@ static inline int queue_on_root_worker(struct cgroup_subsys_state *css)
908795}
909796#endif
910797
911- static inline void loop_inc_blocking_writes (struct loop_device * lo ,
912- struct loop_cmd * cmd )
913- {
914- lockdep_assert_held (& lo -> lo_work_lock );
915-
916- if (req_op (blk_mq_rq_from_pdu (cmd )) == REQ_OP_WRITE )
917- lo -> lo_nr_blocking_writes += 1 ;
918- }
919-
920- static inline void loop_dec_blocking_writes (struct loop_device * lo ,
921- struct loop_cmd * cmd )
922- {
923- lockdep_assert_held (& lo -> lo_work_lock );
924-
925- if (req_op (blk_mq_rq_from_pdu (cmd )) == REQ_OP_WRITE )
926- lo -> lo_nr_blocking_writes -= 1 ;
927- }
928-
929798static void loop_queue_work (struct loop_device * lo , struct loop_cmd * cmd )
930799{
931- struct request __maybe_unused * rq = blk_mq_rq_from_pdu (cmd );
932800 struct rb_node * * node , * parent = NULL ;
933801 struct loop_worker * cur_worker , * worker = NULL ;
934802 struct work_struct * work ;
935803 struct list_head * cmd_list ;
936804
937- /* always use the first bio's css */
938- cmd -> blkcg_css = NULL ;
939- cmd -> memcg_css = NULL ;
940- #ifdef CONFIG_BLK_CGROUP
941- if (rq -> bio ) {
942- cmd -> blkcg_css = bio_blkcg_css (rq -> bio );
943- #ifdef CONFIG_MEMCG
944- if (cmd -> blkcg_css ) {
945- cmd -> memcg_css =
946- cgroup_get_e_css (cmd -> blkcg_css -> cgroup ,
947- & memory_cgrp_subsys );
948- }
949- #endif
950- }
951- #endif
952-
953805 spin_lock_irq (& lo -> lo_work_lock );
954806
955807 if (queue_on_root_worker (cmd -> blkcg_css ))
@@ -1008,8 +860,6 @@ static void loop_queue_work(struct loop_device *lo, struct loop_cmd *cmd)
1008860 work = & lo -> rootcg_work ;
1009861 cmd_list = & lo -> rootcg_cmd_list ;
1010862 }
1011- if (cmd -> use_aio )
1012- loop_inc_blocking_writes (lo , cmd );
1013863 list_add_tail (& cmd -> list_entry , cmd_list );
1014864 queue_work (lo -> workqueue , work );
1015865 spin_unlock_irq (& lo -> lo_work_lock );
@@ -2006,7 +1856,6 @@ static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,
20061856 struct request * rq = bd -> rq ;
20071857 struct loop_cmd * cmd = blk_mq_rq_to_pdu (rq );
20081858 struct loop_device * lo = rq -> q -> queuedata ;
2009- int rw = 0 ;
20101859
20111860 blk_mq_start_request (rq );
20121861
@@ -2019,27 +1868,26 @@ static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,
20191868 case REQ_OP_WRITE_ZEROES :
20201869 cmd -> use_aio = false;
20211870 break ;
2022- case REQ_OP_READ :
2023- rw = ITER_DEST ;
2024- cmd -> use_aio = lo -> lo_flags & LO_FLAGS_DIRECT_IO ;
2025- break ;
2026- case REQ_OP_WRITE :
2027- rw = ITER_SOURCE ;
1871+ default :
20281872 cmd -> use_aio = lo -> lo_flags & LO_FLAGS_DIRECT_IO ;
20291873 break ;
2030- default :
2031- return BLK_STS_IOERR ;
20321874 }
20331875
2034- /* try NOWAIT if the backing file supports the mode */
2035- if (cmd -> use_aio && lo_backfile_support_nowait (lo )) {
2036- int res = lo_rw_aio_nowait (lo , cmd , rw );
2037-
2038- if (res != - EAGAIN && res != - EOPNOTSUPP )
2039- return BLK_STS_OK ;
2040- /* fallback to workqueue for handling aio */
1876+ /* always use the first bio's css */
1877+ cmd -> blkcg_css = NULL ;
1878+ cmd -> memcg_css = NULL ;
1879+ #ifdef CONFIG_BLK_CGROUP
1880+ if (rq -> bio ) {
1881+ cmd -> blkcg_css = bio_blkcg_css (rq -> bio );
1882+ #ifdef CONFIG_MEMCG
1883+ if (cmd -> blkcg_css ) {
1884+ cmd -> memcg_css =
1885+ cgroup_get_e_css (cmd -> blkcg_css -> cgroup ,
1886+ & memory_cgrp_subsys );
1887+ }
1888+ #endif
20411889 }
2042-
1890+ #endif
20431891 loop_queue_work (lo , cmd );
20441892
20451893 return BLK_STS_OK ;
@@ -2115,8 +1963,6 @@ static void loop_process_work(struct loop_worker *worker,
21151963 cond_resched ();
21161964
21171965 spin_lock_irq (& lo -> lo_work_lock );
2118- if (cmd -> use_aio )
2119- loop_dec_blocking_writes (lo , cmd );
21201966 }
21211967
21221968 /*
@@ -2195,8 +2041,7 @@ static int loop_add(int i)
21952041 lo -> tag_set .queue_depth = hw_queue_depth ;
21962042 lo -> tag_set .numa_node = NUMA_NO_NODE ;
21972043 lo -> tag_set .cmd_size = sizeof (struct loop_cmd );
2198- lo -> tag_set .flags = BLK_MQ_F_STACKING | BLK_MQ_F_NO_SCHED_BY_DEFAULT |
2199- BLK_MQ_F_BLOCKING ;
2044+ lo -> tag_set .flags = BLK_MQ_F_STACKING | BLK_MQ_F_NO_SCHED_BY_DEFAULT ;
22002045 lo -> tag_set .driver_data = lo ;
22012046
22022047 err = blk_mq_alloc_tag_set (& lo -> tag_set );
0 commit comments