Skip to content

Commit 614f038

Browse files
Linus WalleijUlf Hansson
authored andcommitted
mmc: block: move single ioctl() commands to block requests
This wraps single ioctl() commands into block requests using the custom block layer request types REQ_OP_DRV_IN and REQ_OP_DRV_OUT. By doing this we are loosening the grip on the big host lock, since two calls to mmc_get_card()/mmc_put_card() are removed. We are storing the ioctl() in/out argument as a pointer in the per-request struct mmc_blk_request container. Since we now let the block layer allocate this data, blk_get_request() will allocate it for us and we can immediately dereference it and use it to pass the argument into the block layer. We refactor the if/else/if/else ladder in mmc_blk_issue_rq() as part of the job, keeping some extra attention to the case when a NULL req is passed into this function and making that pipeline flush more explicit. Tested on the ux500 with the userspace: mmc extcsd read /dev/mmcblk3 resulting in a successful EXTCSD info dump back to the console. This commit fixes a starvation issue in the MMC/SD stack that can be easily provoked in the following way by issueing the following commands in sequence: > dd if=/dev/mmcblk3 of=/dev/null bs=1M & > mmc extcs read /dev/mmcblk3 Before this patch, the extcsd read command would hang (starve) while waiting for the dd command to finish since the block layer was holding the card/host lock. After this patch, the extcsd ioctl() command is nicely interpersed with the rest of the block commands and we can issue a bunch of ioctl()s from userspace while there is some busy block IO going on without any problems. Conversely userspace ioctl()s can no longer starve the block layer by holding the card/host lock. Signed-off-by: Linus Walleij <linus.walleij@linaro.org> Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org> Tested-by: Avri Altman <Avri.Altman@sandisk.com>
1 parent 829043c commit 614f038

2 files changed

Lines changed: 88 additions & 26 deletions

File tree

‎drivers/mmc/core/block.c‎

Lines changed: 85 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,10 @@ static int mmc_blk_ioctl_cmd(struct block_device *bdev,
564564
{
565565
struct mmc_blk_ioc_data *idata;
566566
struct mmc_blk_data *md;
567+
struct mmc_queue *mq;
567568
struct mmc_card *card;
568569
int err = 0, ioc_err = 0;
570+
struct request *req;
569571

570572
/*
571573
* The caller must have CAP_SYS_RAWIO, and must be calling this on the
@@ -591,17 +593,18 @@ static int mmc_blk_ioctl_cmd(struct block_device *bdev,
591593
goto cmd_done;
592594
}
593595

594-
mmc_get_card(card);
595-
596-
ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
597-
598-
/* Always switch back to main area after RPMB access */
599-
if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
600-
mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
601-
602-
mmc_put_card(card);
603-
596+
/*
597+
* Dispatch the ioctl() into the block request queue.
598+
*/
599+
mq = &md->queue;
600+
req = blk_get_request(mq->queue,
601+
idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
602+
__GFP_RECLAIM);
603+
req_to_mmc_queue_req(req)->idata = idata;
604+
blk_execute_rq(mq->queue, NULL, req, 0);
605+
ioc_err = req_to_mmc_queue_req(req)->ioc_result;
604606
err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
607+
blk_put_request(req);
605608

606609
cmd_done:
607610
mmc_blk_put(md);
@@ -611,6 +614,31 @@ static int mmc_blk_ioctl_cmd(struct block_device *bdev,
611614
return ioc_err ? ioc_err : err;
612615
}
613616

617+
/*
618+
* The ioctl commands come back from the block layer after it queued it and
619+
* processed it with all other requests and then they get issued in this
620+
* function.
621+
*/
622+
static void mmc_blk_ioctl_cmd_issue(struct mmc_queue *mq, struct request *req)
623+
{
624+
struct mmc_queue_req *mq_rq;
625+
struct mmc_blk_ioc_data *idata;
626+
struct mmc_card *card = mq->card;
627+
struct mmc_blk_data *md = mq->blkdata;
628+
int ioc_err;
629+
630+
mq_rq = req_to_mmc_queue_req(req);
631+
idata = mq_rq->idata;
632+
ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
633+
mq_rq->ioc_result = ioc_err;
634+
635+
/* Always switch back to main area after RPMB access */
636+
if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
637+
mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
638+
639+
blk_end_request_all(req, ioc_err);
640+
}
641+
614642
static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
615643
struct mmc_ioc_multi_cmd __user *user)
616644
{
@@ -1854,23 +1882,54 @@ void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
18541882
goto out;
18551883
}
18561884

1857-
if (req && req_op(req) == REQ_OP_DISCARD) {
1858-
/* complete ongoing async transfer before issuing discard */
1859-
if (mq->qcnt)
1860-
mmc_blk_issue_rw_rq(mq, NULL);
1861-
mmc_blk_issue_discard_rq(mq, req);
1862-
} else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
1863-
/* complete ongoing async transfer before issuing secure erase*/
1864-
if (mq->qcnt)
1865-
mmc_blk_issue_rw_rq(mq, NULL);
1866-
mmc_blk_issue_secdiscard_rq(mq, req);
1867-
} else if (req && req_op(req) == REQ_OP_FLUSH) {
1868-
/* complete ongoing async transfer before issuing flush */
1869-
if (mq->qcnt)
1870-
mmc_blk_issue_rw_rq(mq, NULL);
1871-
mmc_blk_issue_flush(mq, req);
1885+
if (req) {
1886+
switch (req_op(req)) {
1887+
case REQ_OP_DRV_IN:
1888+
case REQ_OP_DRV_OUT:
1889+
/*
1890+
* Complete ongoing async transfer before issuing
1891+
* ioctl()s
1892+
*/
1893+
if (mq->qcnt)
1894+
mmc_blk_issue_rw_rq(mq, NULL);
1895+
mmc_blk_ioctl_cmd_issue(mq, req);
1896+
break;
1897+
case REQ_OP_DISCARD:
1898+
/*
1899+
* Complete ongoing async transfer before issuing
1900+
* discard.
1901+
*/
1902+
if (mq->qcnt)
1903+
mmc_blk_issue_rw_rq(mq, NULL);
1904+
mmc_blk_issue_discard_rq(mq, req);
1905+
break;
1906+
case REQ_OP_SECURE_ERASE:
1907+
/*
1908+
* Complete ongoing async transfer before issuing
1909+
* secure erase.
1910+
*/
1911+
if (mq->qcnt)
1912+
mmc_blk_issue_rw_rq(mq, NULL);
1913+
mmc_blk_issue_secdiscard_rq(mq, req);
1914+
break;
1915+
case REQ_OP_FLUSH:
1916+
/*
1917+
* Complete ongoing async transfer before issuing
1918+
* flush.
1919+
*/
1920+
if (mq->qcnt)
1921+
mmc_blk_issue_rw_rq(mq, NULL);
1922+
mmc_blk_issue_flush(mq, req);
1923+
break;
1924+
default:
1925+
/* Normal request, just issue it */
1926+
mmc_blk_issue_rw_rq(mq, req);
1927+
card->host->context_info.is_waiting_last_req = false;
1928+
break;
1929+
};
18721930
} else {
1873-
mmc_blk_issue_rw_rq(mq, req);
1931+
/* No request, flushing the pipeline with NULL */
1932+
mmc_blk_issue_rw_rq(mq, NULL);
18741933
card->host->context_info.is_waiting_last_req = false;
18751934
}
18761935

‎drivers/mmc/core/queue.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ static inline bool mmc_req_is_special(struct request *req)
2222

2323
struct task_struct;
2424
struct mmc_blk_data;
25+
struct mmc_blk_ioc_data;
2526

2627
struct mmc_blk_request {
2728
struct mmc_request mrq;
@@ -40,6 +41,8 @@ struct mmc_queue_req {
4041
struct scatterlist *bounce_sg;
4142
unsigned int bounce_sg_len;
4243
struct mmc_async_req areq;
44+
int ioc_result;
45+
struct mmc_blk_ioc_data *idata;
4346
};
4447

4548
struct mmc_queue {

0 commit comments

Comments
 (0)