.. | .. |
---|
151 | 151 | module_param_named(max_ring_page_order, xen_blkif_max_ring_order, int, 0444); |
---|
152 | 152 | MODULE_PARM_DESC(max_ring_page_order, "Maximum order of pages to be used for the shared ring"); |
---|
153 | 153 | |
---|
| 154 | +static bool __read_mostly xen_blkif_trusted = true; |
---|
| 155 | +module_param_named(trusted, xen_blkif_trusted, bool, 0644); |
---|
| 156 | +MODULE_PARM_DESC(trusted, "Is the backend trusted"); |
---|
| 157 | + |
---|
154 | 158 | #define BLK_RING_SIZE(info) \ |
---|
155 | 159 | __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * (info)->nr_ring_pages) |
---|
156 | | - |
---|
157 | | -#define BLK_MAX_RING_SIZE \ |
---|
158 | | - __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * XENBUS_MAX_RING_GRANTS) |
---|
159 | 160 | |
---|
160 | 161 | /* |
---|
161 | 162 | * ring-ref%u i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19 |
---|
.. | .. |
---|
180 | 181 | unsigned int evtchn, irq; |
---|
181 | 182 | struct work_struct work; |
---|
182 | 183 | struct gnttab_free_callback callback; |
---|
183 | | - struct blk_shadow shadow[BLK_MAX_RING_SIZE]; |
---|
184 | 184 | struct list_head indirect_pages; |
---|
185 | 185 | struct list_head grants; |
---|
186 | 186 | unsigned int persistent_gnts_c; |
---|
187 | 187 | unsigned long shadow_free; |
---|
188 | 188 | struct blkfront_info *dev_info; |
---|
| 189 | + struct blk_shadow shadow[]; |
---|
189 | 190 | }; |
---|
190 | 191 | |
---|
191 | 192 | /* |
---|
.. | .. |
---|
210 | 211 | unsigned int feature_fua:1; |
---|
211 | 212 | unsigned int feature_discard:1; |
---|
212 | 213 | unsigned int feature_secdiscard:1; |
---|
| 214 | + /* Connect-time cached feature_persistent parameter */ |
---|
| 215 | + unsigned int feature_persistent_parm:1; |
---|
| 216 | + /* Persistent grants feature negotiation result */ |
---|
213 | 217 | unsigned int feature_persistent:1; |
---|
| 218 | + unsigned int bounce:1; |
---|
214 | 219 | unsigned int discard_granularity; |
---|
215 | 220 | unsigned int discard_alignment; |
---|
216 | 221 | /* Number of 4KB segments handled */ |
---|
.. | .. |
---|
219 | 224 | struct blk_mq_tag_set tag_set; |
---|
220 | 225 | struct blkfront_ring_info *rinfo; |
---|
221 | 226 | unsigned int nr_rings; |
---|
| 227 | + unsigned int rinfo_size; |
---|
222 | 228 | /* Save uncomplete reqs and bios for migration. */ |
---|
223 | 229 | struct list_head requests; |
---|
224 | 230 | struct bio_list bio_list; |
---|
.. | .. |
---|
265 | 271 | static void blkfront_gather_backend_features(struct blkfront_info *info); |
---|
266 | 272 | static int negotiate_mq(struct blkfront_info *info); |
---|
267 | 273 | |
---|
| 274 | +#define for_each_rinfo(info, ptr, idx) \ |
---|
| 275 | + for ((ptr) = (info)->rinfo, (idx) = 0; \ |
---|
| 276 | + (idx) < (info)->nr_rings; \ |
---|
| 277 | + (idx)++, (ptr) = (void *)(ptr) + (info)->rinfo_size) |
---|
| 278 | + |
---|
| 279 | +static inline struct blkfront_ring_info * |
---|
| 280 | +get_rinfo(const struct blkfront_info *info, unsigned int i) |
---|
| 281 | +{ |
---|
| 282 | + BUG_ON(i >= info->nr_rings); |
---|
| 283 | + return (void *)info->rinfo + i * info->rinfo_size; |
---|
| 284 | +} |
---|
| 285 | + |
---|
268 | 286 | static int get_id_from_freelist(struct blkfront_ring_info *rinfo) |
---|
269 | 287 | { |
---|
270 | 288 | unsigned long free = rinfo->shadow_free; |
---|
.. | .. |
---|
300 | 318 | if (!gnt_list_entry) |
---|
301 | 319 | goto out_of_memory; |
---|
302 | 320 | |
---|
303 | | - if (info->feature_persistent) { |
---|
304 | | - granted_page = alloc_page(GFP_NOIO); |
---|
| 321 | + if (info->bounce) { |
---|
| 322 | + granted_page = alloc_page(GFP_NOIO | __GFP_ZERO); |
---|
305 | 323 | if (!granted_page) { |
---|
306 | 324 | kfree(gnt_list_entry); |
---|
307 | 325 | goto out_of_memory; |
---|
.. | .. |
---|
320 | 338 | list_for_each_entry_safe(gnt_list_entry, n, |
---|
321 | 339 | &rinfo->grants, node) { |
---|
322 | 340 | list_del(&gnt_list_entry->node); |
---|
323 | | - if (info->feature_persistent) |
---|
| 341 | + if (info->bounce) |
---|
324 | 342 | __free_page(gnt_list_entry->page); |
---|
325 | 343 | kfree(gnt_list_entry); |
---|
326 | 344 | i--; |
---|
.. | .. |
---|
366 | 384 | /* Assign a gref to this page */ |
---|
367 | 385 | gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head); |
---|
368 | 386 | BUG_ON(gnt_list_entry->gref == -ENOSPC); |
---|
369 | | - if (info->feature_persistent) |
---|
| 387 | + if (info->bounce) |
---|
370 | 388 | grant_foreign_access(gnt_list_entry, info); |
---|
371 | 389 | else { |
---|
372 | 390 | /* Grant access to the GFN passed by the caller */ |
---|
.. | .. |
---|
390 | 408 | /* Assign a gref to this page */ |
---|
391 | 409 | gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head); |
---|
392 | 410 | BUG_ON(gnt_list_entry->gref == -ENOSPC); |
---|
393 | | - if (!info->feature_persistent) { |
---|
| 411 | + if (!info->bounce) { |
---|
394 | 412 | struct page *indirect_page; |
---|
395 | 413 | |
---|
396 | 414 | /* Fetch a pre-allocated page to use for indirect grefs */ |
---|
.. | .. |
---|
705 | 723 | .grant_idx = 0, |
---|
706 | 724 | .segments = NULL, |
---|
707 | 725 | .rinfo = rinfo, |
---|
708 | | - .need_copy = rq_data_dir(req) && info->feature_persistent, |
---|
| 726 | + .need_copy = rq_data_dir(req) && info->bounce, |
---|
709 | 727 | }; |
---|
710 | 728 | |
---|
711 | 729 | /* |
---|
.. | .. |
---|
774 | 792 | ring_req->u.rw.handle = info->handle; |
---|
775 | 793 | ring_req->operation = rq_data_dir(req) ? |
---|
776 | 794 | BLKIF_OP_WRITE : BLKIF_OP_READ; |
---|
777 | | - if (req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA) { |
---|
| 795 | + if (req_op(req) == REQ_OP_FLUSH || |
---|
| 796 | + (req_op(req) == REQ_OP_WRITE && (req->cmd_flags & REQ_FUA))) { |
---|
778 | 797 | /* |
---|
779 | 798 | * Ideally we can do an unordered flush-to-disk. |
---|
780 | 799 | * In case the backend onlysupports barriers, use that. |
---|
.. | .. |
---|
898 | 917 | struct blkfront_info *info = hctx->queue->queuedata; |
---|
899 | 918 | struct blkfront_ring_info *rinfo = NULL; |
---|
900 | 919 | |
---|
901 | | - BUG_ON(info->nr_rings <= qid); |
---|
902 | | - rinfo = &info->rinfo[qid]; |
---|
| 920 | + rinfo = get_rinfo(info, qid); |
---|
903 | 921 | blk_mq_start_request(qd->rq); |
---|
904 | 922 | spin_lock_irqsave(&rinfo->ring_lock, flags); |
---|
905 | 923 | if (RING_FULL(&rinfo->ring)) |
---|
.. | .. |
---|
990 | 1008 | } else |
---|
991 | 1009 | info->tag_set.queue_depth = BLK_RING_SIZE(info); |
---|
992 | 1010 | info->tag_set.numa_node = NUMA_NO_NODE; |
---|
993 | | - info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; |
---|
| 1011 | + info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; |
---|
994 | 1012 | info->tag_set.cmd_size = sizeof(struct blkif_req); |
---|
995 | 1013 | info->tag_set.driver_data = info; |
---|
996 | 1014 | |
---|
.. | .. |
---|
1026 | 1044 | { |
---|
1027 | 1045 | blk_queue_write_cache(info->rq, info->feature_flush ? true : false, |
---|
1028 | 1046 | info->feature_fua ? true : false); |
---|
1029 | | - pr_info("blkfront: %s: %s %s %s %s %s\n", |
---|
| 1047 | + pr_info("blkfront: %s: %s %s %s %s %s %s %s\n", |
---|
1030 | 1048 | info->gd->disk_name, flush_info(info), |
---|
1031 | 1049 | "persistent grants:", info->feature_persistent ? |
---|
1032 | 1050 | "enabled;" : "disabled;", "indirect descriptors:", |
---|
1033 | | - info->max_indirect_segments ? "enabled;" : "disabled;"); |
---|
| 1051 | + info->max_indirect_segments ? "enabled;" : "disabled;", |
---|
| 1052 | + "bounce buffer:", info->bounce ? "enabled" : "disabled;"); |
---|
1034 | 1053 | } |
---|
1035 | 1054 | |
---|
1036 | 1055 | static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset) |
---|
.. | .. |
---|
1197 | 1216 | static void xlvbd_release_gendisk(struct blkfront_info *info) |
---|
1198 | 1217 | { |
---|
1199 | 1218 | unsigned int minor, nr_minors, i; |
---|
| 1219 | + struct blkfront_ring_info *rinfo; |
---|
1200 | 1220 | |
---|
1201 | 1221 | if (info->rq == NULL) |
---|
1202 | 1222 | return; |
---|
.. | .. |
---|
1204 | 1224 | /* No more blkif_request(). */ |
---|
1205 | 1225 | blk_mq_stop_hw_queues(info->rq); |
---|
1206 | 1226 | |
---|
1207 | | - for (i = 0; i < info->nr_rings; i++) { |
---|
1208 | | - struct blkfront_ring_info *rinfo = &info->rinfo[i]; |
---|
1209 | | - |
---|
| 1227 | + for_each_rinfo(info, rinfo, i) { |
---|
1210 | 1228 | /* No more gnttab callback work. */ |
---|
1211 | 1229 | gnttab_cancel_free_callback(&rinfo->callback); |
---|
1212 | 1230 | |
---|
.. | .. |
---|
1265 | 1283 | if (!list_empty(&rinfo->indirect_pages)) { |
---|
1266 | 1284 | struct page *indirect_page, *n; |
---|
1267 | 1285 | |
---|
1268 | | - BUG_ON(info->feature_persistent); |
---|
| 1286 | + BUG_ON(info->bounce); |
---|
1269 | 1287 | list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) { |
---|
1270 | 1288 | list_del(&indirect_page->lru); |
---|
1271 | 1289 | __free_page(indirect_page); |
---|
.. | .. |
---|
1282 | 1300 | 0, 0UL); |
---|
1283 | 1301 | rinfo->persistent_gnts_c--; |
---|
1284 | 1302 | } |
---|
1285 | | - if (info->feature_persistent) |
---|
| 1303 | + if (info->bounce) |
---|
1286 | 1304 | __free_page(persistent_gnt->page); |
---|
1287 | 1305 | kfree(persistent_gnt); |
---|
1288 | 1306 | } |
---|
.. | .. |
---|
1303 | 1321 | for (j = 0; j < segs; j++) { |
---|
1304 | 1322 | persistent_gnt = rinfo->shadow[i].grants_used[j]; |
---|
1305 | 1323 | gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL); |
---|
1306 | | - if (info->feature_persistent) |
---|
| 1324 | + if (info->bounce) |
---|
1307 | 1325 | __free_page(persistent_gnt->page); |
---|
1308 | 1326 | kfree(persistent_gnt); |
---|
1309 | 1327 | } |
---|
.. | .. |
---|
1344 | 1362 | rinfo->ring_ref[i] = GRANT_INVALID_REF; |
---|
1345 | 1363 | } |
---|
1346 | 1364 | } |
---|
1347 | | - free_pages((unsigned long)rinfo->ring.sring, get_order(info->nr_ring_pages * XEN_PAGE_SIZE)); |
---|
| 1365 | + free_pages_exact(rinfo->ring.sring, |
---|
| 1366 | + info->nr_ring_pages * XEN_PAGE_SIZE); |
---|
1348 | 1367 | rinfo->ring.sring = NULL; |
---|
1349 | 1368 | |
---|
1350 | 1369 | if (rinfo->irq) |
---|
.. | .. |
---|
1355 | 1374 | static void blkif_free(struct blkfront_info *info, int suspend) |
---|
1356 | 1375 | { |
---|
1357 | 1376 | unsigned int i; |
---|
| 1377 | + struct blkfront_ring_info *rinfo; |
---|
1358 | 1378 | |
---|
1359 | 1379 | /* Prevent new requests being issued until we fix things up. */ |
---|
1360 | 1380 | info->connected = suspend ? |
---|
.. | .. |
---|
1363 | 1383 | if (info->rq) |
---|
1364 | 1384 | blk_mq_stop_hw_queues(info->rq); |
---|
1365 | 1385 | |
---|
1366 | | - for (i = 0; i < info->nr_rings; i++) |
---|
1367 | | - blkif_free_ring(&info->rinfo[i]); |
---|
| 1386 | + for_each_rinfo(info, rinfo, i) |
---|
| 1387 | + blkif_free_ring(rinfo); |
---|
1368 | 1388 | |
---|
1369 | 1389 | kvfree(info->rinfo); |
---|
1370 | 1390 | info->rinfo = NULL; |
---|
.. | .. |
---|
1406 | 1426 | case BLKIF_RSP_EOPNOTSUPP: |
---|
1407 | 1427 | return REQ_EOPNOTSUPP; |
---|
1408 | 1428 | case BLKIF_RSP_ERROR: |
---|
1409 | | - /* Fallthrough. */ |
---|
1410 | 1429 | default: |
---|
1411 | 1430 | return REQ_ERROR; |
---|
1412 | 1431 | } |
---|
.. | .. |
---|
1428 | 1447 | return BLKIF_RSP_OKAY; |
---|
1429 | 1448 | } |
---|
1430 | 1449 | |
---|
1431 | | -static bool blkif_completion(unsigned long *id, |
---|
1432 | | - struct blkfront_ring_info *rinfo, |
---|
1433 | | - struct blkif_response *bret) |
---|
| 1450 | +/* |
---|
| 1451 | + * Return values: |
---|
| 1452 | + * 1 response processed. |
---|
| 1453 | + * 0 missing further responses. |
---|
| 1454 | + * -1 error while processing. |
---|
| 1455 | + */ |
---|
| 1456 | +static int blkif_completion(unsigned long *id, |
---|
| 1457 | + struct blkfront_ring_info *rinfo, |
---|
| 1458 | + struct blkif_response *bret) |
---|
1434 | 1459 | { |
---|
1435 | 1460 | int i = 0; |
---|
1436 | 1461 | struct scatterlist *sg; |
---|
.. | .. |
---|
1453 | 1478 | |
---|
1454 | 1479 | /* Wait the second response if not yet here. */ |
---|
1455 | 1480 | if (s2->status < REQ_DONE) |
---|
1456 | | - return false; |
---|
| 1481 | + return 0; |
---|
1457 | 1482 | |
---|
1458 | 1483 | bret->status = blkif_get_final_status(s->status, |
---|
1459 | 1484 | s2->status); |
---|
.. | .. |
---|
1486 | 1511 | data.s = s; |
---|
1487 | 1512 | num_sg = s->num_sg; |
---|
1488 | 1513 | |
---|
1489 | | - if (bret->operation == BLKIF_OP_READ && info->feature_persistent) { |
---|
| 1514 | + if (bret->operation == BLKIF_OP_READ && info->bounce) { |
---|
1490 | 1515 | for_each_sg(s->sg, sg, num_sg, i) { |
---|
1491 | 1516 | BUG_ON(sg->offset + sg->length > PAGE_SIZE); |
---|
1492 | 1517 | |
---|
.. | .. |
---|
1504 | 1529 | } |
---|
1505 | 1530 | /* Add the persistent grant into the list of free grants */ |
---|
1506 | 1531 | for (i = 0; i < num_grant; i++) { |
---|
1507 | | - if (gnttab_query_foreign_access(s->grants_used[i]->gref)) { |
---|
| 1532 | + if (!gnttab_try_end_foreign_access(s->grants_used[i]->gref)) { |
---|
1508 | 1533 | /* |
---|
1509 | 1534 | * If the grant is still mapped by the backend (the |
---|
1510 | 1535 | * backend has chosen to make this grant persistent) |
---|
1511 | 1536 | * we add it at the head of the list, so it will be |
---|
1512 | 1537 | * reused first. |
---|
1513 | 1538 | */ |
---|
1514 | | - if (!info->feature_persistent) |
---|
1515 | | - pr_alert_ratelimited("backed has not unmapped grant: %u\n", |
---|
1516 | | - s->grants_used[i]->gref); |
---|
| 1539 | + if (!info->feature_persistent) { |
---|
| 1540 | + pr_alert("backed has not unmapped grant: %u\n", |
---|
| 1541 | + s->grants_used[i]->gref); |
---|
| 1542 | + return -1; |
---|
| 1543 | + } |
---|
1517 | 1544 | list_add(&s->grants_used[i]->node, &rinfo->grants); |
---|
1518 | 1545 | rinfo->persistent_gnts_c++; |
---|
1519 | 1546 | } else { |
---|
1520 | 1547 | /* |
---|
1521 | | - * If the grant is not mapped by the backend we end the |
---|
1522 | | - * foreign access and add it to the tail of the list, |
---|
1523 | | - * so it will not be picked again unless we run out of |
---|
1524 | | - * persistent grants. |
---|
| 1548 | + * If the grant is not mapped by the backend we add it |
---|
| 1549 | + * to the tail of the list, so it will not be picked |
---|
| 1550 | + * again unless we run out of persistent grants. |
---|
1525 | 1551 | */ |
---|
1526 | | - gnttab_end_foreign_access(s->grants_used[i]->gref, 0, 0UL); |
---|
1527 | 1552 | s->grants_used[i]->gref = GRANT_INVALID_REF; |
---|
1528 | 1553 | list_add_tail(&s->grants_used[i]->node, &rinfo->grants); |
---|
1529 | 1554 | } |
---|
1530 | 1555 | } |
---|
1531 | 1556 | if (s->req.operation == BLKIF_OP_INDIRECT) { |
---|
1532 | 1557 | for (i = 0; i < INDIRECT_GREFS(num_grant); i++) { |
---|
1533 | | - if (gnttab_query_foreign_access(s->indirect_grants[i]->gref)) { |
---|
1534 | | - if (!info->feature_persistent) |
---|
1535 | | - pr_alert_ratelimited("backed has not unmapped grant: %u\n", |
---|
1536 | | - s->indirect_grants[i]->gref); |
---|
| 1558 | + if (!gnttab_try_end_foreign_access(s->indirect_grants[i]->gref)) { |
---|
| 1559 | + if (!info->feature_persistent) { |
---|
| 1560 | + pr_alert("backed has not unmapped grant: %u\n", |
---|
| 1561 | + s->indirect_grants[i]->gref); |
---|
| 1562 | + return -1; |
---|
| 1563 | + } |
---|
1537 | 1564 | list_add(&s->indirect_grants[i]->node, &rinfo->grants); |
---|
1538 | 1565 | rinfo->persistent_gnts_c++; |
---|
1539 | 1566 | } else { |
---|
1540 | 1567 | struct page *indirect_page; |
---|
1541 | 1568 | |
---|
1542 | | - gnttab_end_foreign_access(s->indirect_grants[i]->gref, 0, 0UL); |
---|
1543 | 1569 | /* |
---|
1544 | 1570 | * Add the used indirect page back to the list of |
---|
1545 | 1571 | * available pages for indirect grefs. |
---|
1546 | 1572 | */ |
---|
1547 | | - if (!info->feature_persistent) { |
---|
| 1573 | + if (!info->bounce) { |
---|
1548 | 1574 | indirect_page = s->indirect_grants[i]->page; |
---|
1549 | 1575 | list_add(&indirect_page->lru, &rinfo->indirect_pages); |
---|
1550 | 1576 | } |
---|
.. | .. |
---|
1554 | 1580 | } |
---|
1555 | 1581 | } |
---|
1556 | 1582 | |
---|
1557 | | - return true; |
---|
| 1583 | + return 1; |
---|
1558 | 1584 | } |
---|
1559 | 1585 | |
---|
1560 | 1586 | static irqreturn_t blkif_interrupt(int irq, void *dev_id) |
---|
.. | .. |
---|
1620 | 1646 | } |
---|
1621 | 1647 | |
---|
1622 | 1648 | if (bret.operation != BLKIF_OP_DISCARD) { |
---|
| 1649 | + int ret; |
---|
| 1650 | + |
---|
1623 | 1651 | /* |
---|
1624 | 1652 | * We may need to wait for an extra response if the |
---|
1625 | 1653 | * I/O request is split in 2 |
---|
1626 | 1654 | */ |
---|
1627 | | - if (!blkif_completion(&id, rinfo, &bret)) |
---|
| 1655 | + ret = blkif_completion(&id, rinfo, &bret); |
---|
| 1656 | + if (!ret) |
---|
1628 | 1657 | continue; |
---|
| 1658 | + if (unlikely(ret < 0)) |
---|
| 1659 | + goto err; |
---|
1629 | 1660 | } |
---|
1630 | 1661 | |
---|
1631 | 1662 | if (add_id_to_freelist(rinfo, id)) { |
---|
.. | .. |
---|
1673 | 1704 | info->feature_flush = 0; |
---|
1674 | 1705 | xlvbd_flush(info); |
---|
1675 | 1706 | } |
---|
1676 | | - /* fall through */ |
---|
| 1707 | + fallthrough; |
---|
1677 | 1708 | case BLKIF_OP_READ: |
---|
1678 | 1709 | case BLKIF_OP_WRITE: |
---|
1679 | 1710 | if (unlikely(bret.status != BLKIF_RSP_OKAY)) |
---|
.. | .. |
---|
1686 | 1717 | BUG(); |
---|
1687 | 1718 | } |
---|
1688 | 1719 | |
---|
1689 | | - blk_mq_complete_request(req); |
---|
| 1720 | + if (likely(!blk_should_fake_timeout(req->q))) |
---|
| 1721 | + blk_mq_complete_request(req); |
---|
1690 | 1722 | } |
---|
1691 | 1723 | |
---|
1692 | 1724 | rinfo->ring.rsp_cons = i; |
---|
.. | .. |
---|
1731 | 1763 | for (i = 0; i < info->nr_ring_pages; i++) |
---|
1732 | 1764 | rinfo->ring_ref[i] = GRANT_INVALID_REF; |
---|
1733 | 1765 | |
---|
1734 | | - sring = (struct blkif_sring *)__get_free_pages(GFP_NOIO | __GFP_HIGH, |
---|
1735 | | - get_order(ring_size)); |
---|
| 1766 | + sring = alloc_pages_exact(ring_size, GFP_NOIO | __GFP_ZERO); |
---|
1736 | 1767 | if (!sring) { |
---|
1737 | 1768 | xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring"); |
---|
1738 | 1769 | return -ENOMEM; |
---|
.. | .. |
---|
1742 | 1773 | |
---|
1743 | 1774 | err = xenbus_grant_ring(dev, rinfo->ring.sring, info->nr_ring_pages, gref); |
---|
1744 | 1775 | if (err < 0) { |
---|
1745 | | - free_pages((unsigned long)sring, get_order(ring_size)); |
---|
| 1776 | + free_pages_exact(sring, ring_size); |
---|
1746 | 1777 | rinfo->ring.sring = NULL; |
---|
1747 | 1778 | goto fail; |
---|
1748 | 1779 | } |
---|
.. | .. |
---|
1822 | 1853 | kfree(info); |
---|
1823 | 1854 | } |
---|
1824 | 1855 | |
---|
| 1856 | +/* Enable the persistent grants feature. */ |
---|
| 1857 | +static bool feature_persistent = true; |
---|
| 1858 | +module_param(feature_persistent, bool, 0644); |
---|
| 1859 | +MODULE_PARM_DESC(feature_persistent, |
---|
| 1860 | + "Enables the persistent grants feature"); |
---|
| 1861 | + |
---|
1825 | 1862 | /* Common code used when first setting up, and when resuming. */ |
---|
1826 | 1863 | static int talk_to_blkback(struct xenbus_device *dev, |
---|
1827 | 1864 | struct blkfront_info *info) |
---|
.. | .. |
---|
1831 | 1868 | int err; |
---|
1832 | 1869 | unsigned int i, max_page_order; |
---|
1833 | 1870 | unsigned int ring_page_order; |
---|
| 1871 | + struct blkfront_ring_info *rinfo; |
---|
1834 | 1872 | |
---|
1835 | 1873 | if (!info) |
---|
1836 | 1874 | return -ENODEV; |
---|
| 1875 | + |
---|
| 1876 | + /* Check if backend is trusted. */ |
---|
| 1877 | + info->bounce = !xen_blkif_trusted || |
---|
| 1878 | + !xenbus_read_unsigned(dev->nodename, "trusted", 1); |
---|
1837 | 1879 | |
---|
1838 | 1880 | max_page_order = xenbus_read_unsigned(info->xbdev->otherend, |
---|
1839 | 1881 | "max-ring-page-order", 0); |
---|
.. | .. |
---|
1844 | 1886 | if (err) |
---|
1845 | 1887 | goto destroy_blkring; |
---|
1846 | 1888 | |
---|
1847 | | - for (i = 0; i < info->nr_rings; i++) { |
---|
1848 | | - struct blkfront_ring_info *rinfo = &info->rinfo[i]; |
---|
1849 | | - |
---|
| 1889 | + for_each_rinfo(info, rinfo, i) { |
---|
1850 | 1890 | /* Create shared ring, alloc event channel. */ |
---|
1851 | 1891 | err = setup_blkring(dev, rinfo); |
---|
1852 | 1892 | if (err) |
---|
.. | .. |
---|
1871 | 1911 | |
---|
1872 | 1912 | /* We already got the number of queues/rings in _probe */ |
---|
1873 | 1913 | if (info->nr_rings == 1) { |
---|
1874 | | - err = write_per_ring_nodes(xbt, &info->rinfo[0], dev->nodename); |
---|
| 1914 | + err = write_per_ring_nodes(xbt, info->rinfo, dev->nodename); |
---|
1875 | 1915 | if (err) |
---|
1876 | 1916 | goto destroy_blkring; |
---|
1877 | 1917 | } else { |
---|
.. | .. |
---|
1893 | 1933 | goto abort_transaction; |
---|
1894 | 1934 | } |
---|
1895 | 1935 | |
---|
1896 | | - for (i = 0; i < info->nr_rings; i++) { |
---|
| 1936 | + for_each_rinfo(info, rinfo, i) { |
---|
1897 | 1937 | memset(path, 0, pathsize); |
---|
1898 | 1938 | snprintf(path, pathsize, "%s/queue-%u", dev->nodename, i); |
---|
1899 | | - err = write_per_ring_nodes(xbt, &info->rinfo[i], path); |
---|
| 1939 | + err = write_per_ring_nodes(xbt, rinfo, path); |
---|
1900 | 1940 | if (err) { |
---|
1901 | 1941 | kfree(path); |
---|
1902 | 1942 | goto destroy_blkring; |
---|
.. | .. |
---|
1910 | 1950 | message = "writing protocol"; |
---|
1911 | 1951 | goto abort_transaction; |
---|
1912 | 1952 | } |
---|
1913 | | - err = xenbus_printf(xbt, dev->nodename, |
---|
1914 | | - "feature-persistent", "%u", 1); |
---|
| 1953 | + info->feature_persistent_parm = feature_persistent; |
---|
| 1954 | + err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", |
---|
| 1955 | + info->feature_persistent_parm); |
---|
1915 | 1956 | if (err) |
---|
1916 | 1957 | dev_warn(&dev->dev, |
---|
1917 | 1958 | "writing persistent grants feature to xenbus"); |
---|
.. | .. |
---|
1924 | 1965 | goto destroy_blkring; |
---|
1925 | 1966 | } |
---|
1926 | 1967 | |
---|
1927 | | - for (i = 0; i < info->nr_rings; i++) { |
---|
| 1968 | + for_each_rinfo(info, rinfo, i) { |
---|
1928 | 1969 | unsigned int j; |
---|
1929 | | - struct blkfront_ring_info *rinfo = &info->rinfo[i]; |
---|
1930 | 1970 | |
---|
1931 | 1971 | for (j = 0; j < BLK_RING_SIZE(info); j++) |
---|
1932 | 1972 | rinfo->shadow[j].req.u.rw.id = j + 1; |
---|
.. | .. |
---|
1956 | 1996 | { |
---|
1957 | 1997 | unsigned int backend_max_queues; |
---|
1958 | 1998 | unsigned int i; |
---|
| 1999 | + struct blkfront_ring_info *rinfo; |
---|
1959 | 2000 | |
---|
1960 | 2001 | BUG_ON(info->nr_rings); |
---|
1961 | 2002 | |
---|
.. | .. |
---|
1967 | 2008 | if (!info->nr_rings) |
---|
1968 | 2009 | info->nr_rings = 1; |
---|
1969 | 2010 | |
---|
1970 | | - info->rinfo = kvcalloc(info->nr_rings, |
---|
1971 | | - sizeof(struct blkfront_ring_info), |
---|
1972 | | - GFP_KERNEL); |
---|
| 2011 | + info->rinfo_size = struct_size(info->rinfo, shadow, |
---|
| 2012 | + BLK_RING_SIZE(info)); |
---|
| 2013 | + info->rinfo = kvcalloc(info->nr_rings, info->rinfo_size, GFP_KERNEL); |
---|
1973 | 2014 | if (!info->rinfo) { |
---|
1974 | 2015 | xenbus_dev_fatal(info->xbdev, -ENOMEM, "allocating ring_info structure"); |
---|
1975 | 2016 | info->nr_rings = 0; |
---|
1976 | 2017 | return -ENOMEM; |
---|
1977 | 2018 | } |
---|
1978 | 2019 | |
---|
1979 | | - for (i = 0; i < info->nr_rings; i++) { |
---|
1980 | | - struct blkfront_ring_info *rinfo; |
---|
1981 | | - |
---|
1982 | | - rinfo = &info->rinfo[i]; |
---|
| 2020 | + for_each_rinfo(info, rinfo, i) { |
---|
1983 | 2021 | INIT_LIST_HEAD(&rinfo->indirect_pages); |
---|
1984 | 2022 | INIT_LIST_HEAD(&rinfo->grants); |
---|
1985 | 2023 | rinfo->dev_info = info; |
---|
.. | .. |
---|
1988 | 2026 | } |
---|
1989 | 2027 | return 0; |
---|
1990 | 2028 | } |
---|
| 2029 | + |
---|
1991 | 2030 | /** |
---|
1992 | 2031 | * Entry point to this code when a new device is created. Allocate the basic |
---|
1993 | 2032 | * structures and the ring buffer for communication with the backend, and |
---|
.. | .. |
---|
2072 | 2111 | int rc; |
---|
2073 | 2112 | struct bio *bio; |
---|
2074 | 2113 | unsigned int segs; |
---|
| 2114 | + struct blkfront_ring_info *rinfo; |
---|
2075 | 2115 | |
---|
2076 | 2116 | blkfront_gather_backend_features(info); |
---|
2077 | 2117 | /* Reset limits changed by blk_mq_update_nr_hw_queues(). */ |
---|
.. | .. |
---|
2079 | 2119 | segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST; |
---|
2080 | 2120 | blk_queue_max_segments(info->rq, segs / GRANTS_PER_PSEG); |
---|
2081 | 2121 | |
---|
2082 | | - for (r_index = 0; r_index < info->nr_rings; r_index++) { |
---|
2083 | | - struct blkfront_ring_info *rinfo = &info->rinfo[r_index]; |
---|
2084 | | - |
---|
| 2122 | + for_each_rinfo(info, rinfo, r_index) { |
---|
2085 | 2123 | rc = blkfront_setup_indirect(rinfo); |
---|
2086 | 2124 | if (rc) |
---|
2087 | 2125 | return rc; |
---|
.. | .. |
---|
2091 | 2129 | /* Now safe for us to use the shared ring */ |
---|
2092 | 2130 | info->connected = BLKIF_STATE_CONNECTED; |
---|
2093 | 2131 | |
---|
2094 | | - for (r_index = 0; r_index < info->nr_rings; r_index++) { |
---|
2095 | | - struct blkfront_ring_info *rinfo; |
---|
2096 | | - |
---|
2097 | | - rinfo = &info->rinfo[r_index]; |
---|
| 2132 | + for_each_rinfo(info, rinfo, r_index) { |
---|
2098 | 2133 | /* Kick any other new requests queued since we resumed */ |
---|
2099 | 2134 | kick_pending_request_queues(rinfo); |
---|
2100 | 2135 | } |
---|
.. | .. |
---|
2127 | 2162 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
---|
2128 | 2163 | int err = 0; |
---|
2129 | 2164 | unsigned int i, j; |
---|
| 2165 | + struct blkfront_ring_info *rinfo; |
---|
2130 | 2166 | |
---|
2131 | 2167 | dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename); |
---|
2132 | 2168 | |
---|
2133 | 2169 | bio_list_init(&info->bio_list); |
---|
2134 | 2170 | INIT_LIST_HEAD(&info->requests); |
---|
2135 | | - for (i = 0; i < info->nr_rings; i++) { |
---|
2136 | | - struct blkfront_ring_info *rinfo = &info->rinfo[i]; |
---|
| 2171 | + for_each_rinfo(info, rinfo, i) { |
---|
2137 | 2172 | struct bio_list merge_bio; |
---|
2138 | 2173 | struct blk_shadow *shadow = rinfo->shadow; |
---|
2139 | 2174 | |
---|
.. | .. |
---|
2261 | 2296 | if (err) |
---|
2262 | 2297 | goto out_of_memory; |
---|
2263 | 2298 | |
---|
2264 | | - if (!info->feature_persistent && info->max_indirect_segments) { |
---|
| 2299 | + if (!info->bounce && info->max_indirect_segments) { |
---|
2265 | 2300 | /* |
---|
2266 | | - * We are using indirect descriptors but not persistent |
---|
2267 | | - * grants, we need to allocate a set of pages that can be |
---|
| 2301 | + * We are using indirect descriptors but don't have a bounce |
---|
| 2302 | + * buffer, we need to allocate a set of pages that can be |
---|
2268 | 2303 | * used for mapping indirect grefs |
---|
2269 | 2304 | */ |
---|
2270 | 2305 | int num = INDIRECT_GREFS(grants) * BLK_RING_SIZE(info); |
---|
2271 | 2306 | |
---|
2272 | 2307 | BUG_ON(!list_empty(&rinfo->indirect_pages)); |
---|
2273 | 2308 | for (i = 0; i < num; i++) { |
---|
2274 | | - struct page *indirect_page = alloc_page(GFP_KERNEL); |
---|
| 2309 | + struct page *indirect_page = alloc_page(GFP_KERNEL | |
---|
| 2310 | + __GFP_ZERO); |
---|
2275 | 2311 | if (!indirect_page) |
---|
2276 | 2312 | goto out_of_memory; |
---|
2277 | 2313 | list_add(&indirect_page->lru, &rinfo->indirect_pages); |
---|
.. | .. |
---|
2360 | 2396 | if (xenbus_read_unsigned(info->xbdev->otherend, "feature-discard", 0)) |
---|
2361 | 2397 | blkfront_setup_discard(info); |
---|
2362 | 2398 | |
---|
2363 | | - info->feature_persistent = |
---|
2364 | | - !!xenbus_read_unsigned(info->xbdev->otherend, |
---|
2365 | | - "feature-persistent", 0); |
---|
| 2399 | + if (info->feature_persistent_parm) |
---|
| 2400 | + info->feature_persistent = |
---|
| 2401 | + !!xenbus_read_unsigned(info->xbdev->otherend, |
---|
| 2402 | + "feature-persistent", 0); |
---|
| 2403 | + if (info->feature_persistent) |
---|
| 2404 | + info->bounce = true; |
---|
2366 | 2405 | |
---|
2367 | 2406 | indirect_segments = xenbus_read_unsigned(info->xbdev->otherend, |
---|
2368 | 2407 | "feature-max-indirect-segments", 0); |
---|
.. | .. |
---|
2389 | 2428 | unsigned long sector_size; |
---|
2390 | 2429 | unsigned int physical_sector_size; |
---|
2391 | 2430 | unsigned int binfo; |
---|
2392 | | - char *envp[] = { "RESIZE=1", NULL }; |
---|
2393 | 2431 | int err, i; |
---|
| 2432 | + struct blkfront_ring_info *rinfo; |
---|
2394 | 2433 | |
---|
2395 | 2434 | switch (info->connected) { |
---|
2396 | 2435 | case BLKIF_STATE_CONNECTED: |
---|
.. | .. |
---|
2404 | 2443 | return; |
---|
2405 | 2444 | printk(KERN_INFO "Setting capacity to %Lu\n", |
---|
2406 | 2445 | sectors); |
---|
2407 | | - set_capacity(info->gd, sectors); |
---|
2408 | | - revalidate_disk(info->gd); |
---|
2409 | | - kobject_uevent_env(&disk_to_dev(info->gd)->kobj, |
---|
2410 | | - KOBJ_CHANGE, envp); |
---|
| 2446 | + set_capacity_revalidate_and_notify(info->gd, sectors, true); |
---|
2411 | 2447 | |
---|
2412 | 2448 | return; |
---|
2413 | 2449 | case BLKIF_STATE_SUSPENDED: |
---|
.. | .. |
---|
2448 | 2484 | "physical-sector-size", |
---|
2449 | 2485 | sector_size); |
---|
2450 | 2486 | blkfront_gather_backend_features(info); |
---|
2451 | | - for (i = 0; i < info->nr_rings; i++) { |
---|
2452 | | - err = blkfront_setup_indirect(&info->rinfo[i]); |
---|
| 2487 | + for_each_rinfo(info, rinfo, i) { |
---|
| 2488 | + err = blkfront_setup_indirect(rinfo); |
---|
2453 | 2489 | if (err) { |
---|
2454 | 2490 | xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s", |
---|
2455 | 2491 | info->xbdev->otherend); |
---|
.. | .. |
---|
2470 | 2506 | |
---|
2471 | 2507 | /* Kick pending requests. */ |
---|
2472 | 2508 | info->connected = BLKIF_STATE_CONNECTED; |
---|
2473 | | - for (i = 0; i < info->nr_rings; i++) |
---|
2474 | | - kick_pending_request_queues(&info->rinfo[i]); |
---|
| 2509 | + for_each_rinfo(info, rinfo, i) |
---|
| 2510 | + kick_pending_request_queues(rinfo); |
---|
2475 | 2511 | |
---|
2476 | | - device_add_disk(&info->xbdev->dev, info->gd); |
---|
| 2512 | + device_add_disk(&info->xbdev->dev, info->gd, NULL); |
---|
2477 | 2513 | |
---|
2478 | 2514 | info->is_ready = 1; |
---|
2479 | 2515 | return; |
---|
.. | .. |
---|
2530 | 2566 | case XenbusStateClosed: |
---|
2531 | 2567 | if (dev->state == XenbusStateClosed) |
---|
2532 | 2568 | break; |
---|
2533 | | - /* fall through */ |
---|
| 2569 | + fallthrough; |
---|
2534 | 2570 | case XenbusStateClosing: |
---|
2535 | 2571 | if (info) |
---|
2536 | 2572 | blkfront_closing(info); |
---|
.. | .. |
---|
2684 | 2720 | .release = blkif_release, |
---|
2685 | 2721 | .getgeo = blkif_getgeo, |
---|
2686 | 2722 | .ioctl = blkif_ioctl, |
---|
| 2723 | + .compat_ioctl = blkdev_compat_ptr_ioctl, |
---|
2687 | 2724 | }; |
---|
2688 | 2725 | |
---|
2689 | 2726 | |
---|
.. | .. |
---|
2705 | 2742 | { |
---|
2706 | 2743 | unsigned int i; |
---|
2707 | 2744 | unsigned long flags; |
---|
| 2745 | + struct blkfront_ring_info *rinfo; |
---|
2708 | 2746 | |
---|
2709 | | - for (i = 0; i < info->nr_rings; i++) { |
---|
2710 | | - struct blkfront_ring_info *rinfo = &info->rinfo[i]; |
---|
| 2747 | + for_each_rinfo(info, rinfo, i) { |
---|
2711 | 2748 | struct grant *gnt_list_entry, *tmp; |
---|
2712 | 2749 | |
---|
2713 | 2750 | spin_lock_irqsave(&rinfo->ring_lock, flags); |
---|
.. | .. |
---|
2720 | 2757 | list_for_each_entry_safe(gnt_list_entry, tmp, &rinfo->grants, |
---|
2721 | 2758 | node) { |
---|
2722 | 2759 | if (gnt_list_entry->gref == GRANT_INVALID_REF || |
---|
2723 | | - gnttab_query_foreign_access(gnt_list_entry->gref)) |
---|
| 2760 | + !gnttab_try_end_foreign_access(gnt_list_entry->gref)) |
---|
2724 | 2761 | continue; |
---|
2725 | 2762 | |
---|
2726 | 2763 | list_del(&gnt_list_entry->node); |
---|
2727 | | - gnttab_end_foreign_access(gnt_list_entry->gref, 0, 0UL); |
---|
2728 | 2764 | rinfo->persistent_gnts_c--; |
---|
2729 | 2765 | gnt_list_entry->gref = GRANT_INVALID_REF; |
---|
2730 | 2766 | list_add_tail(&gnt_list_entry->node, &rinfo->grants); |
---|
.. | .. |
---|
2739 | 2775 | struct blkfront_info *info; |
---|
2740 | 2776 | bool need_schedule_work = false; |
---|
2741 | 2777 | |
---|
| 2778 | + /* |
---|
| 2779 | + * Note that when using bounce buffers but not persistent grants |
---|
| 2780 | + * there's no need to run blkfront_delay_work because grants are |
---|
| 2781 | + * revoked in blkif_completion or else an error is reported and the |
---|
| 2782 | + * connection is closed. |
---|
| 2783 | + */ |
---|
| 2784 | + |
---|
2742 | 2785 | mutex_lock(&blkfront_mutex); |
---|
2743 | 2786 | |
---|
2744 | 2787 | list_for_each_entry(info, &info_list, info_list) { |
---|