@@ -820,7 +820,7 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx,
820820 imu -> release = io_release_ubuf ;
821821 imu -> priv = imu ;
822822 imu -> flags = 0 ;
823- imu -> dir = IO_IMU_DEST | IO_IMU_SOURCE ;
823+ imu -> dir = IO_BUF_DEST | IO_BUF_SOURCE ;
824824 if (coalesced )
825825 imu -> folio_shift = data .folio_shift ;
826826 refcount_set (& imu -> refs , 1 );
@@ -924,72 +924,125 @@ int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
924924 return ret ;
925925}
926926
927- int io_buffer_register_bvec (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 ;
1003+ unlock :
1004+ io_ring_submit_unlock (ctx , issue_flags );
1005+ return ret ;
1006+ }
1007+ EXPORT_SYMBOL_GPL (io_buffer_register_request );
1008+
1009+ /*
1010+ * bvs is copied internally. caller may free it on return.
1011+ */
1012+ int io_buffer_register_bvec (struct io_uring_cmd * cmd , const struct bio_vec * bvs ,
1013+ unsigned int nr_bvecs , void (* release )(void * ),
1014+ void * priv , u8 dir , unsigned int index ,
1015+ unsigned int issue_flags )
1016+ {
1017+ struct io_ring_ctx * ctx = cmd_to_io_kiocb (cmd )-> ctx ;
1018+ struct io_mapped_ubuf * imu ;
1019+ struct bio_vec * bvec ;
1020+ unsigned int i , total_bytes = 0 ;
1021+ int ret = 0 ;
1022+
1023+ for (i = 0 ; i < nr_bvecs ; i ++ )
1024+ total_bytes += bvs [i ].bv_len ;
1025+
1026+ io_ring_submit_lock (ctx , issue_flags );
1027+ imu = io_kernel_buffer_init (ctx , nr_bvecs , total_bytes , dir , release ,
1028+ priv , index );
1029+ if (IS_ERR (imu )) {
1030+ ret = PTR_ERR (imu );
1031+ goto unlock ;
1032+ }
1033+
1034+ bvec = imu -> bvec ;
1035+ for (i = 0 ; i < nr_bvecs ; i ++ )
1036+ bvec [i ] = bvs [i ];
1037+
9851038unlock :
9861039 io_ring_submit_unlock (ctx , issue_flags );
9871040 return ret ;
9881041}
9891042EXPORT_SYMBOL_GPL (io_buffer_register_bvec );
9901043
991- int io_buffer_unregister_bvec (struct io_uring_cmd * cmd , unsigned int index ,
992- unsigned int issue_flags )
1044+ int io_buffer_unregister (struct io_uring_cmd * cmd , unsigned int index ,
1045+ unsigned int issue_flags )
9931046{
9941047 struct io_ring_ctx * ctx = cmd_to_io_kiocb (cmd )-> ctx ;
9951048 struct io_rsrc_data * data = & ctx -> buf_table ;
@@ -1019,7 +1072,7 @@ int io_buffer_unregister_bvec(struct io_uring_cmd *cmd, unsigned int index,
10191072 io_ring_submit_unlock (ctx , issue_flags );
10201073 return ret ;
10211074}
1022- EXPORT_SYMBOL_GPL (io_buffer_unregister_bvec );
1075+ EXPORT_SYMBOL_GPL (io_buffer_unregister );
10231076
10241077static int validate_fixed_range (u64 buf_addr , size_t len ,
10251078 const struct io_mapped_ubuf * imu )
0 commit comments