@@ -924,64 +924,82 @@ int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
924924 return ret ;
925925}
926926
927- int io_buffer_register_request (struct io_uring_cmd * cmd , struct request * rq ,
928- void (* release )(void * ), unsigned int index ,
929- unsigned int issue_flags )
927+ static struct io_mapped_ubuf * io_kernel_buffer_init (struct io_ring_ctx * ctx ,
928+ unsigned int nr_bvecs ,
929+ unsigned int total_bytes ,
930+ u8 dir ,
931+ void (* release )(void * ),
932+ void * priv ,
933+ unsigned int index )
930934{
931- struct io_ring_ctx * ctx = cmd_to_io_kiocb (cmd )-> ctx ;
932935 struct io_rsrc_data * data = & ctx -> buf_table ;
933- struct req_iterator rq_iter ;
934936 struct io_mapped_ubuf * imu ;
935937 struct io_rsrc_node * node ;
936- struct bio_vec bv ;
937- unsigned int nr_bvecs = 0 ;
938- int ret = 0 ;
939938
940- io_ring_submit_lock (ctx , issue_flags );
941- if (index >= data -> nr ) {
942- ret = - EINVAL ;
943- goto unlock ;
944- }
939+ if (index >= data -> nr )
940+ return ERR_PTR (- EINVAL );
945941 index = array_index_nospec (index , data -> nr );
946942
947- if (data -> nodes [index ]) {
948- ret = - EBUSY ;
949- goto unlock ;
950- }
943+ if (data -> nodes [index ])
944+ return ERR_PTR (- EBUSY );
951945
952946 node = io_rsrc_node_alloc (ctx , IORING_RSRC_BUFFER );
953- if (!node ) {
954- ret = - ENOMEM ;
955- goto unlock ;
956- }
947+ if (!node )
948+ return ERR_PTR (- ENOMEM );
957949
958- /*
959- * blk_rq_nr_phys_segments() may overestimate the number of bvecs
960- * but avoids needing to iterate over the bvecs
961- */
962- imu = io_alloc_imu (ctx , blk_rq_nr_phys_segments (rq ));
950+ imu = io_alloc_imu (ctx , nr_bvecs );
963951 if (!imu ) {
964952 io_cache_free (& ctx -> node_cache , node );
965- ret = - ENOMEM ;
966- goto unlock ;
953+ return ERR_PTR (- ENOMEM );
967954 }
968955
969956 imu -> ubuf = 0 ;
970- imu -> len = blk_rq_bytes ( rq ) ;
957+ imu -> len = total_bytes ;
971958 imu -> acct_pages = 0 ;
972959 imu -> folio_shift = PAGE_SHIFT ;
960+ imu -> nr_bvecs = nr_bvecs ;
973961 refcount_set (& imu -> refs , 1 );
974962 imu -> release = release ;
975- imu -> priv = rq ;
963+ imu -> priv = priv ;
964+ imu -> dir = dir ;
976965 imu -> flags = IO_REGBUF_F_KBUF ;
977- imu -> dir = 1 << rq_data_dir (rq );
978966
967+ node -> buf = imu ;
968+ data -> nodes [index ] = node ;
969+
970+ return imu ;
971+ }
972+
973+ int io_buffer_register_request (struct io_uring_cmd * cmd , struct request * rq ,
974+ void (* release )(void * ), unsigned int index ,
975+ unsigned int issue_flags )
976+ {
977+ struct io_ring_ctx * ctx = cmd_to_io_kiocb (cmd )-> ctx ;
978+ struct req_iterator rq_iter ;
979+ struct io_mapped_ubuf * imu ;
980+ struct bio_vec bv ;
981+ /*
982+ * blk_rq_nr_phys_segments() may overestimate the number of bvecs
983+ * but avoids needing to iterate over the bvecs
984+ */
985+ unsigned int nr_bvecs = blk_rq_nr_phys_segments (rq );
986+ unsigned int total_bytes = blk_rq_bytes (rq );
987+ int ret = 0 ;
988+
989+ io_ring_submit_lock (ctx , issue_flags );
990+
991+ imu = io_kernel_buffer_init (ctx , nr_bvecs , total_bytes ,
992+ 1 << rq_data_dir (rq ), release , rq , index );
993+ if (IS_ERR (imu )) {
994+ ret = PTR_ERR (imu );
995+ goto unlock ;
996+ }
997+
998+ nr_bvecs = 0 ;
979999 rq_for_each_bvec (bv , rq , rq_iter )
9801000 imu -> bvec [nr_bvecs ++ ] = bv ;
9811001 imu -> nr_bvecs = nr_bvecs ;
9821002
983- node -> buf = imu ;
984- data -> nodes [index ] = node ;
9851003unlock :
9861004 io_ring_submit_unlock (ctx , issue_flags );
9871005 return ret ;
0 commit comments