Skip to content

Commit a52cc68

Browse files
committed
rbd: acquire header_rwsem just once in rbd_queue_workfn()
Currently header_rwsem is acquired twice: once in rbd_dev_parent_get() when the image request is being created and then in rbd_queue_workfn() to capture mapping_size and snapc. Introduce rbd_img_capture_header() and move image request allocation so that header_rwsem can be acquired just once. Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
1 parent 78b42a8 commit a52cc68

1 file changed

Lines changed: 31 additions & 28 deletions

File tree

‎drivers/block/rbd.c‎

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,10 +1601,8 @@ static bool rbd_dev_parent_get(struct rbd_device *rbd_dev)
16011601
if (!rbd_dev->parent_spec)
16021602
return false;
16031603

1604-
down_read(&rbd_dev->header_rwsem);
16051604
if (rbd_dev->parent_overlap)
16061605
counter = atomic_inc_return_safe(&rbd_dev->parent_ref);
1607-
up_read(&rbd_dev->header_rwsem);
16081606

16091607
if (counter < 0)
16101608
rbd_warn(rbd_dev, "parent reference overflow");
@@ -1619,8 +1617,7 @@ static bool rbd_dev_parent_get(struct rbd_device *rbd_dev)
16191617
*/
16201618
static struct rbd_img_request *rbd_img_request_create(
16211619
struct rbd_device *rbd_dev,
1622-
enum obj_operation_type op_type,
1623-
struct ceph_snap_context *snapc)
1620+
enum obj_operation_type op_type)
16241621
{
16251622
struct rbd_img_request *img_request;
16261623

@@ -1630,13 +1627,6 @@ static struct rbd_img_request *rbd_img_request_create(
16301627

16311628
img_request->rbd_dev = rbd_dev;
16321629
img_request->op_type = op_type;
1633-
if (!rbd_img_is_write(img_request))
1634-
img_request->snap_id = rbd_dev->spec->snap_id;
1635-
else
1636-
img_request->snapc = snapc;
1637-
1638-
if (rbd_dev_parent_get(rbd_dev))
1639-
img_request_layered_set(img_request);
16401630

16411631
INIT_LIST_HEAD(&img_request->lock_item);
16421632
INIT_LIST_HEAD(&img_request->object_extents);
@@ -1645,6 +1635,21 @@ static struct rbd_img_request *rbd_img_request_create(
16451635
return img_request;
16461636
}
16471637

1638+
static void rbd_img_capture_header(struct rbd_img_request *img_req)
1639+
{
1640+
struct rbd_device *rbd_dev = img_req->rbd_dev;
1641+
1642+
lockdep_assert_held(&rbd_dev->header_rwsem);
1643+
1644+
if (rbd_img_is_write(img_req))
1645+
img_req->snapc = ceph_get_snap_context(rbd_dev->header.snapc);
1646+
else
1647+
img_req->snap_id = rbd_dev->spec->snap_id;
1648+
1649+
if (rbd_dev_parent_get(rbd_dev))
1650+
img_request_layered_set(img_req);
1651+
}
1652+
16481653
static void rbd_img_request_destroy(struct rbd_img_request *img_request)
16491654
{
16501655
struct rbd_obj_request *obj_request;
@@ -2825,17 +2830,21 @@ static int rbd_obj_read_object(struct rbd_obj_request *obj_req)
28252830
static int rbd_obj_read_from_parent(struct rbd_obj_request *obj_req)
28262831
{
28272832
struct rbd_img_request *img_req = obj_req->img_request;
2833+
struct rbd_device *parent = img_req->rbd_dev->parent;
28282834
struct rbd_img_request *child_img_req;
28292835
int ret;
28302836

2831-
child_img_req = rbd_img_request_create(img_req->rbd_dev->parent,
2832-
OBJ_OP_READ, NULL);
2837+
child_img_req = rbd_img_request_create(parent, OBJ_OP_READ);
28332838
if (!child_img_req)
28342839
return -ENOMEM;
28352840

28362841
__set_bit(IMG_REQ_CHILD, &child_img_req->flags);
28372842
child_img_req->obj_request = obj_req;
28382843

2844+
down_read(&parent->header_rwsem);
2845+
rbd_img_capture_header(child_img_req);
2846+
up_read(&parent->header_rwsem);
2847+
28392848
dout("%s child_img_req %p for obj_req %p\n", __func__, child_img_req,
28402849
obj_req);
28412850

@@ -4686,7 +4695,6 @@ static void rbd_queue_workfn(struct work_struct *work)
46864695
struct request *rq = blk_mq_rq_from_pdu(work);
46874696
struct rbd_device *rbd_dev = rq->q->queuedata;
46884697
struct rbd_img_request *img_request;
4689-
struct ceph_snap_context *snapc = NULL;
46904698
u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT;
46914699
u64 length = blk_rq_bytes(rq);
46924700
enum obj_operation_type op_type;
@@ -4739,28 +4747,24 @@ static void rbd_queue_workfn(struct work_struct *work)
47394747

47404748
blk_mq_start_request(rq);
47414749

4750+
img_request = rbd_img_request_create(rbd_dev, op_type);
4751+
if (!img_request) {
4752+
result = -ENOMEM;
4753+
goto err_rq;
4754+
}
4755+
img_request->rq = rq;
4756+
47424757
down_read(&rbd_dev->header_rwsem);
47434758
mapping_size = rbd_dev->mapping.size;
4744-
if (op_type != OBJ_OP_READ) {
4745-
snapc = rbd_dev->header.snapc;
4746-
ceph_get_snap_context(snapc);
4747-
}
4759+
rbd_img_capture_header(img_request);
47484760
up_read(&rbd_dev->header_rwsem);
47494761

47504762
if (offset + length > mapping_size) {
47514763
rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset,
47524764
length, mapping_size);
47534765
result = -EIO;
4754-
goto err_rq;
4755-
}
4756-
4757-
img_request = rbd_img_request_create(rbd_dev, op_type, snapc);
4758-
if (!img_request) {
4759-
result = -ENOMEM;
4760-
goto err_rq;
4766+
goto err_img_request;
47614767
}
4762-
img_request->rq = rq;
4763-
snapc = NULL; /* img_request consumes a ref */
47644768

47654769
dout("%s rbd_dev %p img_req %p %s %llu~%llu\n", __func__, rbd_dev,
47664770
img_request, obj_op_name(op_type), offset, length);
@@ -4782,7 +4786,6 @@ static void rbd_queue_workfn(struct work_struct *work)
47824786
if (result)
47834787
rbd_warn(rbd_dev, "%s %llx at %llx result %d",
47844788
obj_op_name(op_type), length, offset, result);
4785-
ceph_put_snap_context(snapc);
47864789
err:
47874790
blk_mq_end_request(rq, errno_to_blk_status(result));
47884791
}

0 commit comments

Comments
 (0)