rbd: check for EOD after exclusive lock is ensured to be held
Subsystems (2)
Ilya Dryomov
maintainerPATCH 1/1 rbd: check for EOD after exclusive lock is ensured to be held
Similar to commit 870611e4877e ("rbd: get snapshot context after exclusive lock is ensured to be held"), move the "beyond EOD" check into the image request state machine so that it's performed after exclusive lock is ensured to be held. This avoids various race conditions which can arise when the image is shrunk under I/O (in practice, mostly readahead). In one such scenario
rbd_assert(objno < rbd_dev->object_map_size);
can be triggered if a close-to-EOD read gets queued right before the shrink is initiated and the EOD check is performed against an outdated mapping_size. After the resize is done on the server side and exclusive lock is (re)acquired bringing along the new (now shrunk) object map, the read starts going through the state machine and rbd_obj_may_exist() gets invoked on an object that is out of bounds of rbd_dev->object_map array.
--- drivers/block/rbd.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-)
drivers/block/rbd.c +17 -11
@@ -3490,16 +3490,31 @@ static void rbd_img_object_requests(struct rbd_img_request *img_req) {struct rbd_device *rbd_dev = img_req->rbd_dev;struct rbd_obj_request *obj_req; +u64 mapping_size;rbd_assert (!img_req->pending.result && !img_req->pending.num_pending);rbd_assert (!need_exclusive_lock (img_req) ||__rbd_is_lock_owner (rbd_dev)); +down_read (&rbd_dev->header_rwsem); + mapping_size = rbd_dev->mapping.size;if (rbd_img_is_write (img_req)) {rbd_assert (!img_req->snapc); -down_read (&rbd_dev->header_rwsem); img_req->snapc =ceph_get_snap_context (rbd_dev->header.snapc); -up_read (&rbd_dev->header_rwsem); + } +up_read (&rbd_dev->header_rwsem); + +if (!test_bit (IMG_REQ_CHILD , &img_req->flags)) { +struct request *rq =blk_mq_rq_from_pdu (img_req); +u64 off = (u64 )blk_rq_pos (rq) <<SECTOR_SHIFT ; +u64 len =blk_rq_bytes (rq); + +if (unlikely (off + len > mapping_size)) { +rbd_warn (rbd_dev,"beyond EOD (%llu~%llu > %llu)" , + off, len, mapping_size); + img_req->pending.result = -EIO ; +return ; + } }for_each_obj_request (img_req, obj_req) {
@@ -4725,7 +4740,6 @@ static void rbd_queue_workfn(struct work_struct *work)struct request *rq =blk_mq_rq_from_pdu (img_request);u64 offset = (u64 )blk_rq_pos (rq) <<SECTOR_SHIFT ;u64 length =blk_rq_bytes (rq); -u64 mapping_size;int result;/* Ignore/skip any zero-length requests */
@@ -4738,17 +4752,9 @@ static void rbd_queue_workfn(struct work_struct *work)blk_mq_start_request (rq);down_read (&rbd_dev->header_rwsem); - mapping_size = rbd_dev->mapping.size;rbd_img_capture_header (img_request);up_read (&rbd_dev->header_rwsem); -if (offset + length > mapping_size) { -rbd_warn (rbd_dev,"beyond EOD (%llu~%llu > %llu)" , offset, - length, mapping_size); - result = -EIO ; - goto err_img_request; - } -dout ("%s rbd_dev %p img_req %p %s %llu~%llu\n" , __func__, rbd_dev, img_request,obj_op_name (op_type), offset, length);
Signature · 1 line
2.49.0
Quoted 7 lines · Ilya Dryomov
Similar to commit
870611e4877e("rbd: get snapshot context after exclusive lock is ensured to be held"), move the "beyond EOD" check into the image request state machine so that it's performed after exclusive lock is ensured to be held. This avoids various race conditions which can arise when the image is shrunk under I/O (in practice, mostly readahead). In one such scenario
Quoted 9 lines
rbd_assert(objno < rbd_dev->object_map_size);can be triggered if a close-to-EOD read gets queued right before the shrink is initiated and the EOD check is performed against an outdated
mapping_size. After the resize is done on the server side and exclusive lock is (re)acquired bringing along the new (now shrunk) object map, the read starts going through the state machine andrbd_obj_may_exist()gets invoked on an object that is out of bounds ofrbd_dev->object_maparray.
Motivation makes sense to me.
Thanks,
Slava.
Quoted 70 lines
Signed-off-by Ilya Dryomov <[email protected]>--- drivers/block/rbd.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-)drivers/block/rbd.c +17
-11@@ -3490,16 +3490,31 @@ static void rbd_img_object_requests(struct rbd_img_request *img_req) {struct rbd_device *rbd_dev = img_req->rbd_dev;struct rbd_obj_request *obj_req; +u64 mapping_size;rbd_assert (!img_req->pending.result && !img_req->pending.num_pending);rbd_assert (!need_exclusive_lock (img_req) ||__rbd_is_lock_owner (rbd_dev)); +down_read (&rbd_dev->header_rwsem); + mapping_size = rbd_dev->mapping.size;if (rbd_img_is_write (img_req)) {rbd_assert (!img_req->snapc); -down_read (&rbd_dev->header_rwsem); img_req->snapc =ceph_get_snap_context (rbd_dev->header.snapc); -up_read (&rbd_dev->header_rwsem); + } +up_read (&rbd_dev->header_rwsem); + +if (!test_bit (IMG_REQ_CHILD , &img_req->flags)) { +struct request *rq =blk_mq_rq_from_pdu (img_req); +u64 off = (u64 )blk_rq_pos (rq) <<SECTOR_SHIFT ; +u64 len =blk_rq_bytes (rq); + +if (unlikely (off + len > mapping_size)) { +rbd_warn (rbd_dev,"beyond EOD (%llu~%llu > %llu)" , + off, len, mapping_size); + img_req->pending.result = -EIO ; +return ; + } }for_each_obj_request (img_req, obj_req) {@@ -4725,7 +4740,6 @@ static void rbd_queue_workfn(struct work_struct *work)struct request *rq =blk_mq_rq_from_pdu (img_request);u64 offset = (u64 )blk_rq_pos (rq) <<SECTOR_SHIFT ;u64 length =blk_rq_bytes (rq); -u64 mapping_size;int result;/* Ignore/skip any zero-length requests */ @@ -4738,17 +4752,9 @@ static void rbd_queue_workfn(struct work_struct *work)blk_mq_start_request (rq);down_read (&rbd_dev->header_rwsem); - mapping_size = rbd_dev->mapping.size;rbd_img_capture_header (img_request);up_read (&rbd_dev->header_rwsem); -if (offset + length > mapping_size) { -rbd_warn (rbd_dev,"beyond EOD (%llu~%llu > %llu)" , offset, - length, mapping_size); - result = -EIO ; - goto err_img_request; - } -dout ("%s rbd_dev %p img_req %p %s %llu~%llu\n" , __func__, rbd_dev, img_request,obj_op_name (op_type), offset, length);
Dongsheng Yang
reviewerQuoted 48 lines · Ilya Dryomov
Similar to commit
870611e4877e("rbd: get snapshot context after exclusive lock is ensured to be held"), move the "beyond EOD" check into the image request state machine so that it's performed after exclusive lock is ensured to be held. This avoids various race conditions which can arise when the image is shrunk under I/O (in practice, mostly readahead). In one such scenariorbd_assert(objno < rbd_dev->object_map_size);can be triggered if a close-to-EOD read gets queued right before the shrink is initiated and the EOD check is performed against an outdated
mapping_size. After the resize is done on the server side and exclusive lock is (re)acquired bringing along the new (now shrunk) object map, the read starts going through the state machine andrbd_obj_may_exist()gets invoked on an object that is out of bounds ofrbd_dev->object_maparray.Signed-off-by Ilya Dryomov <[email protected]>--- drivers/block/rbd.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-)drivers/block/rbd.c +8
-2@@ -3490,16 +3490,31 @@ static void rbd_img_object_requests(struct rbd_img_request *img_req) {struct rbd_device *rbd_dev = img_req->rbd_dev;struct rbd_obj_request *obj_req; +u64 mapping_size;rbd_assert (!img_req->pending.result && !img_req->pending.num_pending);rbd_assert (!need_exclusive_lock (img_req) ||__rbd_is_lock_owner (rbd_dev)); +down_read (&rbd_dev->header_rwsem); + mapping_size = rbd_dev->mapping.size;if (rbd_img_is_write (img_req)) {rbd_assert (!img_req->snapc); -down_read (&rbd_dev->header_rwsem); img_req->snapc =ceph_get_snap_context (rbd_dev->header.snapc); -up_read (&rbd_dev->header_rwsem); + } +up_read (&rbd_dev->header_rwsem); + +if (!test_bit (IMG_REQ_CHILD , &img_req->flags)) { +struct request *rq =blk_mq_rq_from_pdu (img_req);
drivers/block/rbd.c static void rbd_img_object_requests(struct rbd_img_request *img_req) · line 3518
Quoted 10 lines
drivers/block/rbd.c +9
-0+u64 off = (u64 )blk_rq_pos (rq) <<SECTOR_SHIFT ; +u64 len =blk_rq_bytes (rq); + +if (unlikely (off + len > mapping_size)) { +rbd_warn (rbd_dev,"beyond EOD (%llu~%llu > %llu)" , + off, len, mapping_size); + img_req->pending.result = -EIO ; +return ; + }
Hi Ilya,
Fix looks good in logic. But one potential concern is that with this change all child read requests will also acquire |header_rwsem| for reading.
In principle this could be avoided, since child read requests do not need to fetch |snapc| and do not need to perform the beyond-EOD check.
Dongsheng
Quoted 29 lines
drivers/block/rbd.c +0
-0}for_each_obj_request(img_req, obj_req) {drivers/block/rbd.c +0
-9@@ -4725,7 +4740,6 @@ static void rbd_queue_workfn(struct work_struct *work)struct request *rq =blk_mq_rq_from_pdu (img_request);u64 offset = (u64 )blk_rq_pos (rq) <<SECTOR_SHIFT ;u64 length =blk_rq_bytes (rq); -u64 mapping_size;int result;/* Ignore/skip any zero-length requests */ @@ -4738,17 +4752,9 @@ static void rbd_queue_workfn(struct work_struct *work)blk_mq_start_request (rq);down_read (&rbd_dev->header_rwsem); - mapping_size = rbd_dev->mapping.size;rbd_img_capture_header (img_request);up_read (&rbd_dev->header_rwsem); -if (offset + length > mapping_size) { -rbd_warn (rbd_dev,"beyond EOD (%llu~%llu > %llu)" , offset, - length, mapping_size); - result = -EIO ; - goto err_img_request; - } -dout ("%s rbd_dev %p img_req %p %s %llu~%llu\n" , __func__, rbd_dev, img_request,obj_op_name (op_type), offset, length);
Ilya Dryomov
maintainerQuoted 66 lines · Dongsheng Yang
Quoted 48 lines · Ilya Dryomov
Similar to commit
870611e4877e("rbd: get snapshot context after exclusive lock is ensured to be held"), move the "beyond EOD" check into the image request state machine so that it's performed after exclusive lock is ensured to be held. This avoids various race conditions which can arise when the image is shrunk under I/O (in practice, mostly readahead). In one such scenariorbd_assert(objno < rbd_dev->object_map_size);can be triggered if a close-to-EOD read gets queued right before the shrink is initiated and the EOD check is performed against an outdated
mapping_size. After the resize is done on the server side and exclusive lock is (re)acquired bringing along the new (now shrunk) object map, the read starts going through the state machine andrbd_obj_may_exist()gets invoked on an object that is out of bounds ofrbd_dev->object_maparray.Signed-off-by Ilya Dryomov <[email protected]>--- drivers/block/rbd.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-)drivers/block/rbd.c +8
-2@@ -3490,16 +3490,31 @@ static void rbd_img_object_requests(struct rbd_img_request *img_req) {struct rbd_device *rbd_dev = img_req->rbd_dev;struct rbd_obj_request *obj_req; +u64 mapping_size;rbd_assert (!img_req->pending.result && !img_req->pending.num_pending);rbd_assert (!need_exclusive_lock (img_req) ||__rbd_is_lock_owner (rbd_dev)); +down_read (&rbd_dev->header_rwsem); + mapping_size = rbd_dev->mapping.size;if (rbd_img_is_write (img_req)) {rbd_assert (!img_req->snapc); -down_read (&rbd_dev->header_rwsem); img_req->snapc =ceph_get_snap_context (rbd_dev->header.snapc); -up_read (&rbd_dev->header_rwsem); + } +up_read (&rbd_dev->header_rwsem); + +if (!test_bit (IMG_REQ_CHILD , &img_req->flags)) { +struct request *rq =blk_mq_rq_from_pdu (img_req);drivers/block/rbd.c static void rbd_img_object_requests(struct rbd_img_request *img_req) · line 3518
Quoted 10 lines
drivers/block/rbd.c +9
-0+u64 off = (u64 )blk_rq_pos (rq) <<SECTOR_SHIFT ; +u64 len =blk_rq_bytes (rq); + +if (unlikely (off + len > mapping_size)) { +rbd_warn (rbd_dev,"beyond EOD (%llu~%llu > %llu)" , + off, len, mapping_size); + img_req->pending.result = -EIO ; +return ; + }Hi Ilya,
Quoted 6 lines
Fix looks good in logic. But one potential concern is that with this change all child read requests will also acquire |header_rwsem| for reading.
In principle this could be avoided, since child read requests do not need to fetch |snapc| and do not need to perform the beyond-EOD check.
Hi Dongsheng,
Good catch, I'll refactor appropriately and post v2 shortly.
Thanks,
Ilya