| .. | .. |
|---|
| 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 | /* |
|---|
| .. | .. |
|---|
| 898 | 916 | struct blkfront_info *info = hctx->queue->queuedata; |
|---|
| 899 | 917 | struct blkfront_ring_info *rinfo = NULL; |
|---|
| 900 | 918 | |
|---|
| 901 | | - BUG_ON(info->nr_rings <= qid); |
|---|
| 902 | | - rinfo = &info->rinfo[qid]; |
|---|
| 919 | + rinfo = get_rinfo(info, qid); |
|---|
| 903 | 920 | blk_mq_start_request(qd->rq); |
|---|
| 904 | 921 | spin_lock_irqsave(&rinfo->ring_lock, flags); |
|---|
| 905 | 922 | if (RING_FULL(&rinfo->ring)) |
|---|
| .. | .. |
|---|
| 990 | 1007 | } else |
|---|
| 991 | 1008 | info->tag_set.queue_depth = BLK_RING_SIZE(info); |
|---|
| 992 | 1009 | info->tag_set.numa_node = NUMA_NO_NODE; |
|---|
| 993 | | - info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; |
|---|
| 1010 | + info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; |
|---|
| 994 | 1011 | info->tag_set.cmd_size = sizeof(struct blkif_req); |
|---|
| 995 | 1012 | info->tag_set.driver_data = info; |
|---|
| 996 | 1013 | |
|---|
| .. | .. |
|---|
| 1026 | 1043 | { |
|---|
| 1027 | 1044 | blk_queue_write_cache(info->rq, info->feature_flush ? true : false, |
|---|
| 1028 | 1045 | info->feature_fua ? true : false); |
|---|
| 1029 | | - pr_info("blkfront: %s: %s %s %s %s %s\n", |
|---|
| 1046 | + pr_info("blkfront: %s: %s %s %s %s %s %s %s\n", |
|---|
| 1030 | 1047 | info->gd->disk_name, flush_info(info), |
|---|
| 1031 | 1048 | "persistent grants:", info->feature_persistent ? |
|---|
| 1032 | 1049 | "enabled;" : "disabled;", "indirect descriptors:", |
|---|
| 1033 | | - info->max_indirect_segments ? "enabled;" : "disabled;"); |
|---|
| 1050 | + info->max_indirect_segments ? "enabled;" : "disabled;", |
|---|
| 1051 | + "bounce buffer:", info->bounce ? "enabled" : "disabled;"); |
|---|
| 1034 | 1052 | } |
|---|
| 1035 | 1053 | |
|---|
| 1036 | 1054 | static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset) |
|---|
| .. | .. |
|---|
| 1197 | 1215 | static void xlvbd_release_gendisk(struct blkfront_info *info) |
|---|
| 1198 | 1216 | { |
|---|
| 1199 | 1217 | unsigned int minor, nr_minors, i; |
|---|
| 1218 | + struct blkfront_ring_info *rinfo; |
|---|
| 1200 | 1219 | |
|---|
| 1201 | 1220 | if (info->rq == NULL) |
|---|
| 1202 | 1221 | return; |
|---|
| .. | .. |
|---|
| 1204 | 1223 | /* No more blkif_request(). */ |
|---|
| 1205 | 1224 | blk_mq_stop_hw_queues(info->rq); |
|---|
| 1206 | 1225 | |
|---|
| 1207 | | - for (i = 0; i < info->nr_rings; i++) { |
|---|
| 1208 | | - struct blkfront_ring_info *rinfo = &info->rinfo[i]; |
|---|
| 1209 | | - |
|---|
| 1226 | + for_each_rinfo(info, rinfo, i) { |
|---|
| 1210 | 1227 | /* No more gnttab callback work. */ |
|---|
| 1211 | 1228 | gnttab_cancel_free_callback(&rinfo->callback); |
|---|
| 1212 | 1229 | |
|---|
| .. | .. |
|---|
| 1265 | 1282 | if (!list_empty(&rinfo->indirect_pages)) { |
|---|
| 1266 | 1283 | struct page *indirect_page, *n; |
|---|
| 1267 | 1284 | |
|---|
| 1268 | | - BUG_ON(info->feature_persistent); |
|---|
| 1285 | + BUG_ON(info->bounce); |
|---|
| 1269 | 1286 | list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) { |
|---|
| 1270 | 1287 | list_del(&indirect_page->lru); |
|---|
| 1271 | 1288 | __free_page(indirect_page); |
|---|
| .. | .. |
|---|
| 1282 | 1299 | 0, 0UL); |
|---|
| 1283 | 1300 | rinfo->persistent_gnts_c--; |
|---|
| 1284 | 1301 | } |
|---|
| 1285 | | - if (info->feature_persistent) |
|---|
| 1302 | + if (info->bounce) |
|---|
| 1286 | 1303 | __free_page(persistent_gnt->page); |
|---|
| 1287 | 1304 | kfree(persistent_gnt); |
|---|
| 1288 | 1305 | } |
|---|
| .. | .. |
|---|
| 1303 | 1320 | for (j = 0; j < segs; j++) { |
|---|
| 1304 | 1321 | persistent_gnt = rinfo->shadow[i].grants_used[j]; |
|---|
| 1305 | 1322 | gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL); |
|---|
| 1306 | | - if (info->feature_persistent) |
|---|
| 1323 | + if (info->bounce) |
|---|
| 1307 | 1324 | __free_page(persistent_gnt->page); |
|---|
| 1308 | 1325 | kfree(persistent_gnt); |
|---|
| 1309 | 1326 | } |
|---|
| .. | .. |
|---|
| 1344 | 1361 | rinfo->ring_ref[i] = GRANT_INVALID_REF; |
|---|
| 1345 | 1362 | } |
|---|
| 1346 | 1363 | } |
|---|
| 1347 | | - free_pages((unsigned long)rinfo->ring.sring, get_order(info->nr_ring_pages * XEN_PAGE_SIZE)); |
|---|
| 1364 | + free_pages_exact(rinfo->ring.sring, |
|---|
| 1365 | + info->nr_ring_pages * XEN_PAGE_SIZE); |
|---|
| 1348 | 1366 | rinfo->ring.sring = NULL; |
|---|
| 1349 | 1367 | |
|---|
| 1350 | 1368 | if (rinfo->irq) |
|---|
| .. | .. |
|---|
| 1355 | 1373 | static void blkif_free(struct blkfront_info *info, int suspend) |
|---|
| 1356 | 1374 | { |
|---|
| 1357 | 1375 | unsigned int i; |
|---|
| 1376 | + struct blkfront_ring_info *rinfo; |
|---|
| 1358 | 1377 | |
|---|
| 1359 | 1378 | /* Prevent new requests being issued until we fix things up. */ |
|---|
| 1360 | 1379 | info->connected = suspend ? |
|---|
| .. | .. |
|---|
| 1363 | 1382 | if (info->rq) |
|---|
| 1364 | 1383 | blk_mq_stop_hw_queues(info->rq); |
|---|
| 1365 | 1384 | |
|---|
| 1366 | | - for (i = 0; i < info->nr_rings; i++) |
|---|
| 1367 | | - blkif_free_ring(&info->rinfo[i]); |
|---|
| 1385 | + for_each_rinfo(info, rinfo, i) |
|---|
| 1386 | + blkif_free_ring(rinfo); |
|---|
| 1368 | 1387 | |
|---|
| 1369 | 1388 | kvfree(info->rinfo); |
|---|
| 1370 | 1389 | info->rinfo = NULL; |
|---|
| .. | .. |
|---|
| 1406 | 1425 | case BLKIF_RSP_EOPNOTSUPP: |
|---|
| 1407 | 1426 | return REQ_EOPNOTSUPP; |
|---|
| 1408 | 1427 | case BLKIF_RSP_ERROR: |
|---|
| 1409 | | - /* Fallthrough. */ |
|---|
| 1410 | 1428 | default: |
|---|
| 1411 | 1429 | return REQ_ERROR; |
|---|
| 1412 | 1430 | } |
|---|
| .. | .. |
|---|
| 1428 | 1446 | return BLKIF_RSP_OKAY; |
|---|
| 1429 | 1447 | } |
|---|
| 1430 | 1448 | |
|---|
| 1431 | | -static bool blkif_completion(unsigned long *id, |
|---|
| 1432 | | - struct blkfront_ring_info *rinfo, |
|---|
| 1433 | | - struct blkif_response *bret) |
|---|
| 1449 | +/* |
|---|
| 1450 | + * Return values: |
|---|
| 1451 | + * 1 response processed. |
|---|
| 1452 | + * 0 missing further responses. |
|---|
| 1453 | + * -1 error while processing. |
|---|
| 1454 | + */ |
|---|
| 1455 | +static int blkif_completion(unsigned long *id, |
|---|
| 1456 | + struct blkfront_ring_info *rinfo, |
|---|
| 1457 | + struct blkif_response *bret) |
|---|
| 1434 | 1458 | { |
|---|
| 1435 | 1459 | int i = 0; |
|---|
| 1436 | 1460 | struct scatterlist *sg; |
|---|
| .. | .. |
|---|
| 1453 | 1477 | |
|---|
| 1454 | 1478 | /* Wait the second response if not yet here. */ |
|---|
| 1455 | 1479 | if (s2->status < REQ_DONE) |
|---|
| 1456 | | - return false; |
|---|
| 1480 | + return 0; |
|---|
| 1457 | 1481 | |
|---|
| 1458 | 1482 | bret->status = blkif_get_final_status(s->status, |
|---|
| 1459 | 1483 | s2->status); |
|---|
| .. | .. |
|---|
| 1486 | 1510 | data.s = s; |
|---|
| 1487 | 1511 | num_sg = s->num_sg; |
|---|
| 1488 | 1512 | |
|---|
| 1489 | | - if (bret->operation == BLKIF_OP_READ && info->feature_persistent) { |
|---|
| 1513 | + if (bret->operation == BLKIF_OP_READ && info->bounce) { |
|---|
| 1490 | 1514 | for_each_sg(s->sg, sg, num_sg, i) { |
|---|
| 1491 | 1515 | BUG_ON(sg->offset + sg->length > PAGE_SIZE); |
|---|
| 1492 | 1516 | |
|---|
| .. | .. |
|---|
| 1504 | 1528 | } |
|---|
| 1505 | 1529 | /* Add the persistent grant into the list of free grants */ |
|---|
| 1506 | 1530 | for (i = 0; i < num_grant; i++) { |
|---|
| 1507 | | - if (gnttab_query_foreign_access(s->grants_used[i]->gref)) { |
|---|
| 1531 | + if (!gnttab_try_end_foreign_access(s->grants_used[i]->gref)) { |
|---|
| 1508 | 1532 | /* |
|---|
| 1509 | 1533 | * If the grant is still mapped by the backend (the |
|---|
| 1510 | 1534 | * backend has chosen to make this grant persistent) |
|---|
| 1511 | 1535 | * we add it at the head of the list, so it will be |
|---|
| 1512 | 1536 | * reused first. |
|---|
| 1513 | 1537 | */ |
|---|
| 1514 | | - if (!info->feature_persistent) |
|---|
| 1515 | | - pr_alert_ratelimited("backed has not unmapped grant: %u\n", |
|---|
| 1516 | | - s->grants_used[i]->gref); |
|---|
| 1538 | + if (!info->feature_persistent) { |
|---|
| 1539 | + pr_alert("backed has not unmapped grant: %u\n", |
|---|
| 1540 | + s->grants_used[i]->gref); |
|---|
| 1541 | + return -1; |
|---|
| 1542 | + } |
|---|
| 1517 | 1543 | list_add(&s->grants_used[i]->node, &rinfo->grants); |
|---|
| 1518 | 1544 | rinfo->persistent_gnts_c++; |
|---|
| 1519 | 1545 | } else { |
|---|
| 1520 | 1546 | /* |
|---|
| 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. |
|---|
| 1547 | + * If the grant is not mapped by the backend we add it |
|---|
| 1548 | + * to the tail of the list, so it will not be picked |
|---|
| 1549 | + * again unless we run out of persistent grants. |
|---|
| 1525 | 1550 | */ |
|---|
| 1526 | | - gnttab_end_foreign_access(s->grants_used[i]->gref, 0, 0UL); |
|---|
| 1527 | 1551 | s->grants_used[i]->gref = GRANT_INVALID_REF; |
|---|
| 1528 | 1552 | list_add_tail(&s->grants_used[i]->node, &rinfo->grants); |
|---|
| 1529 | 1553 | } |
|---|
| 1530 | 1554 | } |
|---|
| 1531 | 1555 | if (s->req.operation == BLKIF_OP_INDIRECT) { |
|---|
| 1532 | 1556 | 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); |
|---|
| 1557 | + if (!gnttab_try_end_foreign_access(s->indirect_grants[i]->gref)) { |
|---|
| 1558 | + if (!info->feature_persistent) { |
|---|
| 1559 | + pr_alert("backed has not unmapped grant: %u\n", |
|---|
| 1560 | + s->indirect_grants[i]->gref); |
|---|
| 1561 | + return -1; |
|---|
| 1562 | + } |
|---|
| 1537 | 1563 | list_add(&s->indirect_grants[i]->node, &rinfo->grants); |
|---|
| 1538 | 1564 | rinfo->persistent_gnts_c++; |
|---|
| 1539 | 1565 | } else { |
|---|
| 1540 | 1566 | struct page *indirect_page; |
|---|
| 1541 | 1567 | |
|---|
| 1542 | | - gnttab_end_foreign_access(s->indirect_grants[i]->gref, 0, 0UL); |
|---|
| 1543 | 1568 | /* |
|---|
| 1544 | 1569 | * Add the used indirect page back to the list of |
|---|
| 1545 | 1570 | * available pages for indirect grefs. |
|---|
| 1546 | 1571 | */ |
|---|
| 1547 | | - if (!info->feature_persistent) { |
|---|
| 1572 | + if (!info->bounce) { |
|---|
| 1548 | 1573 | indirect_page = s->indirect_grants[i]->page; |
|---|
| 1549 | 1574 | list_add(&indirect_page->lru, &rinfo->indirect_pages); |
|---|
| 1550 | 1575 | } |
|---|
| .. | .. |
|---|
| 1554 | 1579 | } |
|---|
| 1555 | 1580 | } |
|---|
| 1556 | 1581 | |
|---|
| 1557 | | - return true; |
|---|
| 1582 | + return 1; |
|---|
| 1558 | 1583 | } |
|---|
| 1559 | 1584 | |
|---|
| 1560 | 1585 | static irqreturn_t blkif_interrupt(int irq, void *dev_id) |
|---|
| .. | .. |
|---|
| 1620 | 1645 | } |
|---|
| 1621 | 1646 | |
|---|
| 1622 | 1647 | if (bret.operation != BLKIF_OP_DISCARD) { |
|---|
| 1648 | + int ret; |
|---|
| 1649 | + |
|---|
| 1623 | 1650 | /* |
|---|
| 1624 | 1651 | * We may need to wait for an extra response if the |
|---|
| 1625 | 1652 | * I/O request is split in 2 |
|---|
| 1626 | 1653 | */ |
|---|
| 1627 | | - if (!blkif_completion(&id, rinfo, &bret)) |
|---|
| 1654 | + ret = blkif_completion(&id, rinfo, &bret); |
|---|
| 1655 | + if (!ret) |
|---|
| 1628 | 1656 | continue; |
|---|
| 1657 | + if (unlikely(ret < 0)) |
|---|
| 1658 | + goto err; |
|---|
| 1629 | 1659 | } |
|---|
| 1630 | 1660 | |
|---|
| 1631 | 1661 | if (add_id_to_freelist(rinfo, id)) { |
|---|
| .. | .. |
|---|
| 1673 | 1703 | info->feature_flush = 0; |
|---|
| 1674 | 1704 | xlvbd_flush(info); |
|---|
| 1675 | 1705 | } |
|---|
| 1676 | | - /* fall through */ |
|---|
| 1706 | + fallthrough; |
|---|
| 1677 | 1707 | case BLKIF_OP_READ: |
|---|
| 1678 | 1708 | case BLKIF_OP_WRITE: |
|---|
| 1679 | 1709 | if (unlikely(bret.status != BLKIF_RSP_OKAY)) |
|---|
| .. | .. |
|---|
| 1686 | 1716 | BUG(); |
|---|
| 1687 | 1717 | } |
|---|
| 1688 | 1718 | |
|---|
| 1689 | | - blk_mq_complete_request(req); |
|---|
| 1719 | + if (likely(!blk_should_fake_timeout(req->q))) |
|---|
| 1720 | + blk_mq_complete_request(req); |
|---|
| 1690 | 1721 | } |
|---|
| 1691 | 1722 | |
|---|
| 1692 | 1723 | rinfo->ring.rsp_cons = i; |
|---|
| .. | .. |
|---|
| 1731 | 1762 | for (i = 0; i < info->nr_ring_pages; i++) |
|---|
| 1732 | 1763 | rinfo->ring_ref[i] = GRANT_INVALID_REF; |
|---|
| 1733 | 1764 | |
|---|
| 1734 | | - sring = (struct blkif_sring *)__get_free_pages(GFP_NOIO | __GFP_HIGH, |
|---|
| 1735 | | - get_order(ring_size)); |
|---|
| 1765 | + sring = alloc_pages_exact(ring_size, GFP_NOIO | __GFP_ZERO); |
|---|
| 1736 | 1766 | if (!sring) { |
|---|
| 1737 | 1767 | xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring"); |
|---|
| 1738 | 1768 | return -ENOMEM; |
|---|
| .. | .. |
|---|
| 1742 | 1772 | |
|---|
| 1743 | 1773 | err = xenbus_grant_ring(dev, rinfo->ring.sring, info->nr_ring_pages, gref); |
|---|
| 1744 | 1774 | if (err < 0) { |
|---|
| 1745 | | - free_pages((unsigned long)sring, get_order(ring_size)); |
|---|
| 1775 | + free_pages_exact(sring, ring_size); |
|---|
| 1746 | 1776 | rinfo->ring.sring = NULL; |
|---|
| 1747 | 1777 | goto fail; |
|---|
| 1748 | 1778 | } |
|---|
| .. | .. |
|---|
| 1822 | 1852 | kfree(info); |
|---|
| 1823 | 1853 | } |
|---|
| 1824 | 1854 | |
|---|
| 1855 | +/* Enable the persistent grants feature. */ |
|---|
| 1856 | +static bool feature_persistent = true; |
|---|
| 1857 | +module_param(feature_persistent, bool, 0644); |
|---|
| 1858 | +MODULE_PARM_DESC(feature_persistent, |
|---|
| 1859 | + "Enables the persistent grants feature"); |
|---|
| 1860 | + |
|---|
| 1825 | 1861 | /* Common code used when first setting up, and when resuming. */ |
|---|
| 1826 | 1862 | static int talk_to_blkback(struct xenbus_device *dev, |
|---|
| 1827 | 1863 | struct blkfront_info *info) |
|---|
| .. | .. |
|---|
| 1831 | 1867 | int err; |
|---|
| 1832 | 1868 | unsigned int i, max_page_order; |
|---|
| 1833 | 1869 | unsigned int ring_page_order; |
|---|
| 1870 | + struct blkfront_ring_info *rinfo; |
|---|
| 1834 | 1871 | |
|---|
| 1835 | 1872 | if (!info) |
|---|
| 1836 | 1873 | return -ENODEV; |
|---|
| 1874 | + |
|---|
| 1875 | + /* Check if backend is trusted. */ |
|---|
| 1876 | + info->bounce = !xen_blkif_trusted || |
|---|
| 1877 | + !xenbus_read_unsigned(dev->nodename, "trusted", 1); |
|---|
| 1837 | 1878 | |
|---|
| 1838 | 1879 | max_page_order = xenbus_read_unsigned(info->xbdev->otherend, |
|---|
| 1839 | 1880 | "max-ring-page-order", 0); |
|---|
| .. | .. |
|---|
| 1844 | 1885 | if (err) |
|---|
| 1845 | 1886 | goto destroy_blkring; |
|---|
| 1846 | 1887 | |
|---|
| 1847 | | - for (i = 0; i < info->nr_rings; i++) { |
|---|
| 1848 | | - struct blkfront_ring_info *rinfo = &info->rinfo[i]; |
|---|
| 1849 | | - |
|---|
| 1888 | + for_each_rinfo(info, rinfo, i) { |
|---|
| 1850 | 1889 | /* Create shared ring, alloc event channel. */ |
|---|
| 1851 | 1890 | err = setup_blkring(dev, rinfo); |
|---|
| 1852 | 1891 | if (err) |
|---|
| .. | .. |
|---|
| 1871 | 1910 | |
|---|
| 1872 | 1911 | /* We already got the number of queues/rings in _probe */ |
|---|
| 1873 | 1912 | if (info->nr_rings == 1) { |
|---|
| 1874 | | - err = write_per_ring_nodes(xbt, &info->rinfo[0], dev->nodename); |
|---|
| 1913 | + err = write_per_ring_nodes(xbt, info->rinfo, dev->nodename); |
|---|
| 1875 | 1914 | if (err) |
|---|
| 1876 | 1915 | goto destroy_blkring; |
|---|
| 1877 | 1916 | } else { |
|---|
| .. | .. |
|---|
| 1893 | 1932 | goto abort_transaction; |
|---|
| 1894 | 1933 | } |
|---|
| 1895 | 1934 | |
|---|
| 1896 | | - for (i = 0; i < info->nr_rings; i++) { |
|---|
| 1935 | + for_each_rinfo(info, rinfo, i) { |
|---|
| 1897 | 1936 | memset(path, 0, pathsize); |
|---|
| 1898 | 1937 | snprintf(path, pathsize, "%s/queue-%u", dev->nodename, i); |
|---|
| 1899 | | - err = write_per_ring_nodes(xbt, &info->rinfo[i], path); |
|---|
| 1938 | + err = write_per_ring_nodes(xbt, rinfo, path); |
|---|
| 1900 | 1939 | if (err) { |
|---|
| 1901 | 1940 | kfree(path); |
|---|
| 1902 | 1941 | goto destroy_blkring; |
|---|
| .. | .. |
|---|
| 1910 | 1949 | message = "writing protocol"; |
|---|
| 1911 | 1950 | goto abort_transaction; |
|---|
| 1912 | 1951 | } |
|---|
| 1913 | | - err = xenbus_printf(xbt, dev->nodename, |
|---|
| 1914 | | - "feature-persistent", "%u", 1); |
|---|
| 1952 | + info->feature_persistent_parm = feature_persistent; |
|---|
| 1953 | + err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", |
|---|
| 1954 | + info->feature_persistent_parm); |
|---|
| 1915 | 1955 | if (err) |
|---|
| 1916 | 1956 | dev_warn(&dev->dev, |
|---|
| 1917 | 1957 | "writing persistent grants feature to xenbus"); |
|---|
| .. | .. |
|---|
| 1924 | 1964 | goto destroy_blkring; |
|---|
| 1925 | 1965 | } |
|---|
| 1926 | 1966 | |
|---|
| 1927 | | - for (i = 0; i < info->nr_rings; i++) { |
|---|
| 1967 | + for_each_rinfo(info, rinfo, i) { |
|---|
| 1928 | 1968 | unsigned int j; |
|---|
| 1929 | | - struct blkfront_ring_info *rinfo = &info->rinfo[i]; |
|---|
| 1930 | 1969 | |
|---|
| 1931 | 1970 | for (j = 0; j < BLK_RING_SIZE(info); j++) |
|---|
| 1932 | 1971 | rinfo->shadow[j].req.u.rw.id = j + 1; |
|---|
| .. | .. |
|---|
| 1956 | 1995 | { |
|---|
| 1957 | 1996 | unsigned int backend_max_queues; |
|---|
| 1958 | 1997 | unsigned int i; |
|---|
| 1998 | + struct blkfront_ring_info *rinfo; |
|---|
| 1959 | 1999 | |
|---|
| 1960 | 2000 | BUG_ON(info->nr_rings); |
|---|
| 1961 | 2001 | |
|---|
| .. | .. |
|---|
| 1967 | 2007 | if (!info->nr_rings) |
|---|
| 1968 | 2008 | info->nr_rings = 1; |
|---|
| 1969 | 2009 | |
|---|
| 1970 | | - info->rinfo = kvcalloc(info->nr_rings, |
|---|
| 1971 | | - sizeof(struct blkfront_ring_info), |
|---|
| 1972 | | - GFP_KERNEL); |
|---|
| 2010 | + info->rinfo_size = struct_size(info->rinfo, shadow, |
|---|
| 2011 | + BLK_RING_SIZE(info)); |
|---|
| 2012 | + info->rinfo = kvcalloc(info->nr_rings, info->rinfo_size, GFP_KERNEL); |
|---|
| 1973 | 2013 | if (!info->rinfo) { |
|---|
| 1974 | 2014 | xenbus_dev_fatal(info->xbdev, -ENOMEM, "allocating ring_info structure"); |
|---|
| 1975 | 2015 | info->nr_rings = 0; |
|---|
| 1976 | 2016 | return -ENOMEM; |
|---|
| 1977 | 2017 | } |
|---|
| 1978 | 2018 | |
|---|
| 1979 | | - for (i = 0; i < info->nr_rings; i++) { |
|---|
| 1980 | | - struct blkfront_ring_info *rinfo; |
|---|
| 1981 | | - |
|---|
| 1982 | | - rinfo = &info->rinfo[i]; |
|---|
| 2019 | + for_each_rinfo(info, rinfo, i) { |
|---|
| 1983 | 2020 | INIT_LIST_HEAD(&rinfo->indirect_pages); |
|---|
| 1984 | 2021 | INIT_LIST_HEAD(&rinfo->grants); |
|---|
| 1985 | 2022 | rinfo->dev_info = info; |
|---|
| .. | .. |
|---|
| 1988 | 2025 | } |
|---|
| 1989 | 2026 | return 0; |
|---|
| 1990 | 2027 | } |
|---|
| 2028 | + |
|---|
| 1991 | 2029 | /** |
|---|
| 1992 | 2030 | * Entry point to this code when a new device is created. Allocate the basic |
|---|
| 1993 | 2031 | * structures and the ring buffer for communication with the backend, and |
|---|
| .. | .. |
|---|
| 2072 | 2110 | int rc; |
|---|
| 2073 | 2111 | struct bio *bio; |
|---|
| 2074 | 2112 | unsigned int segs; |
|---|
| 2113 | + struct blkfront_ring_info *rinfo; |
|---|
| 2075 | 2114 | |
|---|
| 2076 | 2115 | blkfront_gather_backend_features(info); |
|---|
| 2077 | 2116 | /* Reset limits changed by blk_mq_update_nr_hw_queues(). */ |
|---|
| .. | .. |
|---|
| 2079 | 2118 | segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST; |
|---|
| 2080 | 2119 | blk_queue_max_segments(info->rq, segs / GRANTS_PER_PSEG); |
|---|
| 2081 | 2120 | |
|---|
| 2082 | | - for (r_index = 0; r_index < info->nr_rings; r_index++) { |
|---|
| 2083 | | - struct blkfront_ring_info *rinfo = &info->rinfo[r_index]; |
|---|
| 2084 | | - |
|---|
| 2121 | + for_each_rinfo(info, rinfo, r_index) { |
|---|
| 2085 | 2122 | rc = blkfront_setup_indirect(rinfo); |
|---|
| 2086 | 2123 | if (rc) |
|---|
| 2087 | 2124 | return rc; |
|---|
| .. | .. |
|---|
| 2091 | 2128 | /* Now safe for us to use the shared ring */ |
|---|
| 2092 | 2129 | info->connected = BLKIF_STATE_CONNECTED; |
|---|
| 2093 | 2130 | |
|---|
| 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]; |
|---|
| 2131 | + for_each_rinfo(info, rinfo, r_index) { |
|---|
| 2098 | 2132 | /* Kick any other new requests queued since we resumed */ |
|---|
| 2099 | 2133 | kick_pending_request_queues(rinfo); |
|---|
| 2100 | 2134 | } |
|---|
| .. | .. |
|---|
| 2127 | 2161 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
|---|
| 2128 | 2162 | int err = 0; |
|---|
| 2129 | 2163 | unsigned int i, j; |
|---|
| 2164 | + struct blkfront_ring_info *rinfo; |
|---|
| 2130 | 2165 | |
|---|
| 2131 | 2166 | dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename); |
|---|
| 2132 | 2167 | |
|---|
| 2133 | 2168 | bio_list_init(&info->bio_list); |
|---|
| 2134 | 2169 | INIT_LIST_HEAD(&info->requests); |
|---|
| 2135 | | - for (i = 0; i < info->nr_rings; i++) { |
|---|
| 2136 | | - struct blkfront_ring_info *rinfo = &info->rinfo[i]; |
|---|
| 2170 | + for_each_rinfo(info, rinfo, i) { |
|---|
| 2137 | 2171 | struct bio_list merge_bio; |
|---|
| 2138 | 2172 | struct blk_shadow *shadow = rinfo->shadow; |
|---|
| 2139 | 2173 | |
|---|
| .. | .. |
|---|
| 2261 | 2295 | if (err) |
|---|
| 2262 | 2296 | goto out_of_memory; |
|---|
| 2263 | 2297 | |
|---|
| 2264 | | - if (!info->feature_persistent && info->max_indirect_segments) { |
|---|
| 2298 | + if (!info->bounce && info->max_indirect_segments) { |
|---|
| 2265 | 2299 | /* |
|---|
| 2266 | | - * We are using indirect descriptors but not persistent |
|---|
| 2267 | | - * grants, we need to allocate a set of pages that can be |
|---|
| 2300 | + * We are using indirect descriptors but don't have a bounce |
|---|
| 2301 | + * buffer, we need to allocate a set of pages that can be |
|---|
| 2268 | 2302 | * used for mapping indirect grefs |
|---|
| 2269 | 2303 | */ |
|---|
| 2270 | 2304 | int num = INDIRECT_GREFS(grants) * BLK_RING_SIZE(info); |
|---|
| 2271 | 2305 | |
|---|
| 2272 | 2306 | BUG_ON(!list_empty(&rinfo->indirect_pages)); |
|---|
| 2273 | 2307 | for (i = 0; i < num; i++) { |
|---|
| 2274 | | - struct page *indirect_page = alloc_page(GFP_KERNEL); |
|---|
| 2308 | + struct page *indirect_page = alloc_page(GFP_KERNEL | |
|---|
| 2309 | + __GFP_ZERO); |
|---|
| 2275 | 2310 | if (!indirect_page) |
|---|
| 2276 | 2311 | goto out_of_memory; |
|---|
| 2277 | 2312 | list_add(&indirect_page->lru, &rinfo->indirect_pages); |
|---|
| .. | .. |
|---|
| 2360 | 2395 | if (xenbus_read_unsigned(info->xbdev->otherend, "feature-discard", 0)) |
|---|
| 2361 | 2396 | blkfront_setup_discard(info); |
|---|
| 2362 | 2397 | |
|---|
| 2363 | | - info->feature_persistent = |
|---|
| 2364 | | - !!xenbus_read_unsigned(info->xbdev->otherend, |
|---|
| 2365 | | - "feature-persistent", 0); |
|---|
| 2398 | + if (info->feature_persistent_parm) |
|---|
| 2399 | + info->feature_persistent = |
|---|
| 2400 | + !!xenbus_read_unsigned(info->xbdev->otherend, |
|---|
| 2401 | + "feature-persistent", 0); |
|---|
| 2402 | + if (info->feature_persistent) |
|---|
| 2403 | + info->bounce = true; |
|---|
| 2366 | 2404 | |
|---|
| 2367 | 2405 | indirect_segments = xenbus_read_unsigned(info->xbdev->otherend, |
|---|
| 2368 | 2406 | "feature-max-indirect-segments", 0); |
|---|
| .. | .. |
|---|
| 2389 | 2427 | unsigned long sector_size; |
|---|
| 2390 | 2428 | unsigned int physical_sector_size; |
|---|
| 2391 | 2429 | unsigned int binfo; |
|---|
| 2392 | | - char *envp[] = { "RESIZE=1", NULL }; |
|---|
| 2393 | 2430 | int err, i; |
|---|
| 2431 | + struct blkfront_ring_info *rinfo; |
|---|
| 2394 | 2432 | |
|---|
| 2395 | 2433 | switch (info->connected) { |
|---|
| 2396 | 2434 | case BLKIF_STATE_CONNECTED: |
|---|
| .. | .. |
|---|
| 2404 | 2442 | return; |
|---|
| 2405 | 2443 | printk(KERN_INFO "Setting capacity to %Lu\n", |
|---|
| 2406 | 2444 | 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); |
|---|
| 2445 | + set_capacity_revalidate_and_notify(info->gd, sectors, true); |
|---|
| 2411 | 2446 | |
|---|
| 2412 | 2447 | return; |
|---|
| 2413 | 2448 | case BLKIF_STATE_SUSPENDED: |
|---|
| .. | .. |
|---|
| 2448 | 2483 | "physical-sector-size", |
|---|
| 2449 | 2484 | sector_size); |
|---|
| 2450 | 2485 | blkfront_gather_backend_features(info); |
|---|
| 2451 | | - for (i = 0; i < info->nr_rings; i++) { |
|---|
| 2452 | | - err = blkfront_setup_indirect(&info->rinfo[i]); |
|---|
| 2486 | + for_each_rinfo(info, rinfo, i) { |
|---|
| 2487 | + err = blkfront_setup_indirect(rinfo); |
|---|
| 2453 | 2488 | if (err) { |
|---|
| 2454 | 2489 | xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s", |
|---|
| 2455 | 2490 | info->xbdev->otherend); |
|---|
| .. | .. |
|---|
| 2470 | 2505 | |
|---|
| 2471 | 2506 | /* Kick pending requests. */ |
|---|
| 2472 | 2507 | info->connected = BLKIF_STATE_CONNECTED; |
|---|
| 2473 | | - for (i = 0; i < info->nr_rings; i++) |
|---|
| 2474 | | - kick_pending_request_queues(&info->rinfo[i]); |
|---|
| 2508 | + for_each_rinfo(info, rinfo, i) |
|---|
| 2509 | + kick_pending_request_queues(rinfo); |
|---|
| 2475 | 2510 | |
|---|
| 2476 | | - device_add_disk(&info->xbdev->dev, info->gd); |
|---|
| 2511 | + device_add_disk(&info->xbdev->dev, info->gd, NULL); |
|---|
| 2477 | 2512 | |
|---|
| 2478 | 2513 | info->is_ready = 1; |
|---|
| 2479 | 2514 | return; |
|---|
| .. | .. |
|---|
| 2530 | 2565 | case XenbusStateClosed: |
|---|
| 2531 | 2566 | if (dev->state == XenbusStateClosed) |
|---|
| 2532 | 2567 | break; |
|---|
| 2533 | | - /* fall through */ |
|---|
| 2568 | + fallthrough; |
|---|
| 2534 | 2569 | case XenbusStateClosing: |
|---|
| 2535 | 2570 | if (info) |
|---|
| 2536 | 2571 | blkfront_closing(info); |
|---|
| .. | .. |
|---|
| 2684 | 2719 | .release = blkif_release, |
|---|
| 2685 | 2720 | .getgeo = blkif_getgeo, |
|---|
| 2686 | 2721 | .ioctl = blkif_ioctl, |
|---|
| 2722 | + .compat_ioctl = blkdev_compat_ptr_ioctl, |
|---|
| 2687 | 2723 | }; |
|---|
| 2688 | 2724 | |
|---|
| 2689 | 2725 | |
|---|
| .. | .. |
|---|
| 2705 | 2741 | { |
|---|
| 2706 | 2742 | unsigned int i; |
|---|
| 2707 | 2743 | unsigned long flags; |
|---|
| 2744 | + struct blkfront_ring_info *rinfo; |
|---|
| 2708 | 2745 | |
|---|
| 2709 | | - for (i = 0; i < info->nr_rings; i++) { |
|---|
| 2710 | | - struct blkfront_ring_info *rinfo = &info->rinfo[i]; |
|---|
| 2746 | + for_each_rinfo(info, rinfo, i) { |
|---|
| 2711 | 2747 | struct grant *gnt_list_entry, *tmp; |
|---|
| 2712 | 2748 | |
|---|
| 2713 | 2749 | spin_lock_irqsave(&rinfo->ring_lock, flags); |
|---|
| .. | .. |
|---|
| 2720 | 2756 | list_for_each_entry_safe(gnt_list_entry, tmp, &rinfo->grants, |
|---|
| 2721 | 2757 | node) { |
|---|
| 2722 | 2758 | if (gnt_list_entry->gref == GRANT_INVALID_REF || |
|---|
| 2723 | | - gnttab_query_foreign_access(gnt_list_entry->gref)) |
|---|
| 2759 | + !gnttab_try_end_foreign_access(gnt_list_entry->gref)) |
|---|
| 2724 | 2760 | continue; |
|---|
| 2725 | 2761 | |
|---|
| 2726 | 2762 | list_del(&gnt_list_entry->node); |
|---|
| 2727 | | - gnttab_end_foreign_access(gnt_list_entry->gref, 0, 0UL); |
|---|
| 2728 | 2763 | rinfo->persistent_gnts_c--; |
|---|
| 2729 | 2764 | gnt_list_entry->gref = GRANT_INVALID_REF; |
|---|
| 2730 | 2765 | list_add_tail(&gnt_list_entry->node, &rinfo->grants); |
|---|
| .. | .. |
|---|
| 2739 | 2774 | struct blkfront_info *info; |
|---|
| 2740 | 2775 | bool need_schedule_work = false; |
|---|
| 2741 | 2776 | |
|---|
| 2777 | + /* |
|---|
| 2778 | + * Note that when using bounce buffers but not persistent grants |
|---|
| 2779 | + * there's no need to run blkfront_delay_work because grants are |
|---|
| 2780 | + * revoked in blkif_completion or else an error is reported and the |
|---|
| 2781 | + * connection is closed. |
|---|
| 2782 | + */ |
|---|
| 2783 | + |
|---|
| 2742 | 2784 | mutex_lock(&blkfront_mutex); |
|---|
| 2743 | 2785 | |
|---|
| 2744 | 2786 | list_for_each_entry(info, &info_list, info_list) { |
|---|