Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

rds: stop using dmapool

RDMA ULPs should only perform DMA through the ib_dma_* API instead of
using the hidden dma_device directly. In addition using the dma coherent
API family that dmapool is a part of can be very ineffcient on plaforms
that are not DMA coherent. Switch to use slab allocations and the
ib_dma_* APIs instead.

Link: https://lore.kernel.org/r/20201106181941.1878556-6-hch@lst.de
Signed-off-by: Christoph Hellwig <hch@lst.de>
Acked-by: Santosh Shilimkar <santosh.shilimkar@oracle.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>

authored by

Christoph Hellwig and committed by
Jason Gunthorpe
42f2611c bf3b7b7b

+101 -69
-10
net/rds/ib.c
··· 30 30 * SOFTWARE. 31 31 * 32 32 */ 33 - #include <linux/dmapool.h> 34 33 #include <linux/kernel.h> 35 34 #include <linux/in.h> 36 35 #include <linux/if.h> ··· 107 108 rds_ib_destroy_mr_pool(rds_ibdev->mr_1m_pool); 108 109 if (rds_ibdev->pd) 109 110 ib_dealloc_pd(rds_ibdev->pd); 110 - dma_pool_destroy(rds_ibdev->rid_hdrs_pool); 111 111 112 112 list_for_each_entry_safe(i_ipaddr, i_next, &rds_ibdev->ipaddr_list, list) { 113 113 list_del(&i_ipaddr->list); ··· 187 189 if (IS_ERR(rds_ibdev->pd)) { 188 190 ret = PTR_ERR(rds_ibdev->pd); 189 191 rds_ibdev->pd = NULL; 190 - goto put_dev; 191 - } 192 - rds_ibdev->rid_hdrs_pool = dma_pool_create(device->name, 193 - device->dma_device, 194 - sizeof(struct rds_header), 195 - L1_CACHE_BYTES, 0); 196 - if (!rds_ibdev->rid_hdrs_pool) { 197 - ret = -ENOMEM; 198 192 goto put_dev; 199 193 } 200 194
-6
net/rds/ib.h
··· 246 246 struct list_head conn_list; 247 247 struct ib_device *dev; 248 248 struct ib_pd *pd; 249 - struct dma_pool *rid_hdrs_pool; /* RDS headers DMA pool */ 250 249 u8 odp_capable:1; 251 250 252 251 unsigned int max_mrs; ··· 379 380 int rds_ib_cm_initiate_connect(struct rdma_cm_id *cm_id, bool isv6); 380 381 void rds_ib_cm_connect_complete(struct rds_connection *conn, 381 382 struct rdma_cm_event *event); 382 - struct rds_header **rds_dma_hdrs_alloc(struct ib_device *ibdev, 383 - struct dma_pool *pool, 384 - dma_addr_t **dma_addrs, u32 num_hdrs); 385 - void rds_dma_hdrs_free(struct dma_pool *pool, struct rds_header **hdrs, 386 - dma_addr_t *dma_addrs, u32 num_hdrs); 387 383 388 384 #define rds_ib_conn_error(conn, fmt...) \ 389 385 __rds_ib_conn_error(conn, KERN_WARNING "RDS/IB: " fmt)
+78 -50
net/rds/ib_cm.c
··· 30 30 * SOFTWARE. 31 31 * 32 32 */ 33 - #include <linux/dmapool.h> 34 33 #include <linux/kernel.h> 35 34 #include <linux/in.h> 36 35 #include <linux/slab.h> ··· 440 441 rds_ibdev->vector_load[index]--; 441 442 } 442 443 444 + static void rds_dma_hdr_free(struct ib_device *dev, struct rds_header *hdr, 445 + dma_addr_t dma_addr, enum dma_data_direction dir) 446 + { 447 + ib_dma_unmap_single(dev, dma_addr, sizeof(*hdr), dir); 448 + kfree(hdr); 449 + } 450 + 451 + static struct rds_header *rds_dma_hdr_alloc(struct ib_device *dev, 452 + dma_addr_t *dma_addr, enum dma_data_direction dir) 453 + { 454 + struct rds_header *hdr; 455 + 456 + hdr = kzalloc_node(sizeof(*hdr), GFP_KERNEL, ibdev_to_node(dev)); 457 + if (!hdr) 458 + return NULL; 459 + 460 + *dma_addr = ib_dma_map_single(dev, hdr, sizeof(*hdr), 461 + DMA_BIDIRECTIONAL); 462 + if (ib_dma_mapping_error(dev, *dma_addr)) { 463 + kfree(hdr); 464 + return NULL; 465 + } 466 + 467 + return hdr; 468 + } 469 + 470 + /* Free the DMA memory used to store struct rds_header. 471 + * 472 + * @dev: the RDS IB device 473 + * @hdrs: pointer to the array storing DMA memory pointers 474 + * @dma_addrs: pointer to the array storing DMA addresses 475 + * @num_hdars: number of headers to free. 476 + */ 477 + static void rds_dma_hdrs_free(struct rds_ib_device *dev, 478 + struct rds_header **hdrs, dma_addr_t *dma_addrs, u32 num_hdrs, 479 + enum dma_data_direction dir) 480 + { 481 + u32 i; 482 + 483 + for (i = 0; i < num_hdrs; i++) 484 + rds_dma_hdr_free(dev->dev, hdrs[i], dma_addrs[i], dir); 485 + kvfree(hdrs); 486 + kvfree(dma_addrs); 487 + } 488 + 489 + 443 490 /* Allocate DMA coherent memory to be used to store struct rds_header for 444 491 * sending/receiving packets. The pointers to the DMA memory and the 445 492 * associated DMA addresses are stored in two arrays. 446 493 * 447 - * @ibdev: the IB device 448 - * @pool: the DMA memory pool 494 + * @dev: the RDS IB device 449 495 * @dma_addrs: pointer to the array for storing DMA addresses 450 496 * @num_hdrs: number of headers to allocate 451 497 * 452 498 * It returns the pointer to the array storing the DMA memory pointers. On 453 499 * error, NULL pointer is returned. 454 500 */ 455 - struct rds_header **rds_dma_hdrs_alloc(struct ib_device *ibdev, 456 - struct dma_pool *pool, 457 - dma_addr_t **dma_addrs, u32 num_hdrs) 501 + static struct rds_header **rds_dma_hdrs_alloc(struct rds_ib_device *dev, 502 + dma_addr_t **dma_addrs, u32 num_hdrs, 503 + enum dma_data_direction dir) 458 504 { 459 505 struct rds_header **hdrs; 460 506 dma_addr_t *hdr_daddrs; 461 507 u32 i; 462 508 463 509 hdrs = kvmalloc_node(sizeof(*hdrs) * num_hdrs, GFP_KERNEL, 464 - ibdev_to_node(ibdev)); 510 + ibdev_to_node(dev->dev)); 465 511 if (!hdrs) 466 512 return NULL; 467 513 468 514 hdr_daddrs = kvmalloc_node(sizeof(*hdr_daddrs) * num_hdrs, GFP_KERNEL, 469 - ibdev_to_node(ibdev)); 515 + ibdev_to_node(dev->dev)); 470 516 if (!hdr_daddrs) { 471 517 kvfree(hdrs); 472 518 return NULL; 473 519 } 474 520 475 521 for (i = 0; i < num_hdrs; i++) { 476 - hdrs[i] = dma_pool_zalloc(pool, GFP_KERNEL, &hdr_daddrs[i]); 522 + hdrs[i] = rds_dma_hdr_alloc(dev->dev, &hdr_daddrs[i], dir); 477 523 if (!hdrs[i]) { 478 - rds_dma_hdrs_free(pool, hdrs, hdr_daddrs, i); 524 + rds_dma_hdrs_free(dev, hdrs, hdr_daddrs, i, dir); 479 525 return NULL; 480 526 } 481 527 } 482 528 483 529 *dma_addrs = hdr_daddrs; 484 530 return hdrs; 485 - } 486 - 487 - /* Free the DMA memory used to store struct rds_header. 488 - * 489 - * @pool: the DMA memory pool 490 - * @hdrs: pointer to the array storing DMA memory pointers 491 - * @dma_addrs: pointer to the array storing DMA addresses 492 - * @num_hdars: number of headers to free. 493 - */ 494 - void rds_dma_hdrs_free(struct dma_pool *pool, struct rds_header **hdrs, 495 - dma_addr_t *dma_addrs, u32 num_hdrs) 496 - { 497 - u32 i; 498 - 499 - for (i = 0; i < num_hdrs; i++) 500 - dma_pool_free(pool, hdrs[i], dma_addrs[i]); 501 - kvfree(hdrs); 502 - kvfree(dma_addrs); 503 531 } 504 532 505 533 /* ··· 542 516 struct rds_ib_device *rds_ibdev; 543 517 unsigned long max_wrs; 544 518 int ret, fr_queue_space; 545 - struct dma_pool *pool; 546 519 547 520 /* 548 521 * It's normal to see a null device if an incoming connection races ··· 637 612 goto recv_cq_out; 638 613 } 639 614 640 - pool = rds_ibdev->rid_hdrs_pool; 641 - ic->i_send_hdrs = rds_dma_hdrs_alloc(dev, pool, &ic->i_send_hdrs_dma, 642 - ic->i_send_ring.w_nr); 615 + ic->i_send_hdrs = rds_dma_hdrs_alloc(rds_ibdev, &ic->i_send_hdrs_dma, 616 + ic->i_send_ring.w_nr, 617 + DMA_TO_DEVICE); 643 618 if (!ic->i_send_hdrs) { 644 619 ret = -ENOMEM; 645 620 rdsdebug("DMA send hdrs alloc failed\n"); 646 621 goto qp_out; 647 622 } 648 623 649 - ic->i_recv_hdrs = rds_dma_hdrs_alloc(dev, pool, &ic->i_recv_hdrs_dma, 650 - ic->i_recv_ring.w_nr); 624 + ic->i_recv_hdrs = rds_dma_hdrs_alloc(rds_ibdev, &ic->i_recv_hdrs_dma, 625 + ic->i_recv_ring.w_nr, 626 + DMA_FROM_DEVICE); 651 627 if (!ic->i_recv_hdrs) { 652 628 ret = -ENOMEM; 653 629 rdsdebug("DMA recv hdrs alloc failed\n"); 654 630 goto send_hdrs_dma_out; 655 631 } 656 632 657 - ic->i_ack = dma_pool_zalloc(pool, GFP_KERNEL, 658 - &ic->i_ack_dma); 633 + ic->i_ack = rds_dma_hdr_alloc(rds_ibdev->dev, &ic->i_ack_dma, 634 + DMA_TO_DEVICE); 659 635 if (!ic->i_ack) { 660 636 ret = -ENOMEM; 661 637 rdsdebug("DMA ack header alloc failed\n"); ··· 692 666 vfree(ic->i_sends); 693 667 694 668 ack_dma_out: 695 - dma_pool_free(pool, ic->i_ack, ic->i_ack_dma); 669 + rds_dma_hdr_free(rds_ibdev->dev, ic->i_ack, ic->i_ack_dma, 670 + DMA_TO_DEVICE); 696 671 ic->i_ack = NULL; 697 672 698 673 recv_hdrs_dma_out: 699 - rds_dma_hdrs_free(pool, ic->i_recv_hdrs, ic->i_recv_hdrs_dma, 700 - ic->i_recv_ring.w_nr); 674 + rds_dma_hdrs_free(rds_ibdev, ic->i_recv_hdrs, ic->i_recv_hdrs_dma, 675 + ic->i_recv_ring.w_nr, DMA_FROM_DEVICE); 701 676 ic->i_recv_hdrs = NULL; 702 677 ic->i_recv_hdrs_dma = NULL; 703 678 704 679 send_hdrs_dma_out: 705 - rds_dma_hdrs_free(pool, ic->i_send_hdrs, ic->i_send_hdrs_dma, 706 - ic->i_send_ring.w_nr); 680 + rds_dma_hdrs_free(rds_ibdev, ic->i_send_hdrs, ic->i_send_hdrs_dma, 681 + ic->i_send_ring.w_nr, DMA_TO_DEVICE); 707 682 ic->i_send_hdrs = NULL; 708 683 ic->i_send_hdrs_dma = NULL; 709 684 ··· 1137 1110 } 1138 1111 1139 1112 if (ic->rds_ibdev) { 1140 - struct dma_pool *pool; 1141 - 1142 - pool = ic->rds_ibdev->rid_hdrs_pool; 1143 - 1144 1113 /* then free the resources that ib callbacks use */ 1145 1114 if (ic->i_send_hdrs) { 1146 - rds_dma_hdrs_free(pool, ic->i_send_hdrs, 1115 + rds_dma_hdrs_free(ic->rds_ibdev, 1116 + ic->i_send_hdrs, 1147 1117 ic->i_send_hdrs_dma, 1148 - ic->i_send_ring.w_nr); 1118 + ic->i_send_ring.w_nr, 1119 + DMA_TO_DEVICE); 1149 1120 ic->i_send_hdrs = NULL; 1150 1121 ic->i_send_hdrs_dma = NULL; 1151 1122 } 1152 1123 1153 1124 if (ic->i_recv_hdrs) { 1154 - rds_dma_hdrs_free(pool, ic->i_recv_hdrs, 1125 + rds_dma_hdrs_free(ic->rds_ibdev, 1126 + ic->i_recv_hdrs, 1155 1127 ic->i_recv_hdrs_dma, 1156 - ic->i_recv_ring.w_nr); 1128 + ic->i_recv_ring.w_nr, 1129 + DMA_FROM_DEVICE); 1157 1130 ic->i_recv_hdrs = NULL; 1158 1131 ic->i_recv_hdrs_dma = NULL; 1159 1132 } 1160 1133 1161 1134 if (ic->i_ack) { 1162 - dma_pool_free(pool, ic->i_ack, ic->i_ack_dma); 1135 + rds_dma_hdr_free(ic->rds_ibdev->dev, ic->i_ack, 1136 + ic->i_ack_dma, DMA_TO_DEVICE); 1163 1137 ic->i_ack = NULL; 1164 1138 } 1165 1139 } else {
+15 -3
net/rds/ib_recv.c
··· 662 662 seq = rds_ib_get_ack(ic); 663 663 664 664 rdsdebug("send_ack: ic %p ack %llu\n", ic, (unsigned long long) seq); 665 + 666 + ib_dma_sync_single_for_cpu(ic->rds_ibdev->dev, ic->i_ack_dma, 667 + sizeof(*hdr), DMA_TO_DEVICE); 665 668 rds_message_populate_header(hdr, 0, 0, 0); 666 669 hdr->h_ack = cpu_to_be64(seq); 667 670 hdr->h_credit = adv_credits; 668 671 rds_message_make_checksum(hdr); 672 + ib_dma_sync_single_for_device(ic->rds_ibdev->dev, ic->i_ack_dma, 673 + sizeof(*hdr), DMA_TO_DEVICE); 674 + 669 675 ic->i_ack_queued = jiffies; 670 676 671 677 ret = ib_post_send(ic->i_cm_id->qp, &ic->i_ack_wr, NULL); ··· 851 845 struct rds_ib_connection *ic = conn->c_transport_data; 852 846 struct rds_ib_incoming *ibinc = ic->i_ibinc; 853 847 struct rds_header *ihdr, *hdr; 848 + dma_addr_t dma_addr = ic->i_recv_hdrs_dma[recv - ic->i_recvs]; 854 849 855 850 /* XXX shut down the connection if port 0,0 are seen? */ 856 851 ··· 870 863 871 864 ihdr = ic->i_recv_hdrs[recv - ic->i_recvs]; 872 865 866 + ib_dma_sync_single_for_cpu(ic->rds_ibdev->dev, dma_addr, 867 + sizeof(*ihdr), DMA_FROM_DEVICE); 873 868 /* Validate the checksum. */ 874 869 if (!rds_message_verify_checksum(ihdr)) { 875 870 rds_ib_conn_error(conn, "incoming message " ··· 879 870 "forcing a reconnect\n", 880 871 &conn->c_faddr); 881 872 rds_stats_inc(s_recv_drop_bad_checksum); 882 - return; 873 + goto done; 883 874 } 884 875 885 876 /* Process the ACK sequence which comes with every packet */ ··· 908 899 */ 909 900 rds_ib_frag_free(ic, recv->r_frag); 910 901 recv->r_frag = NULL; 911 - return; 902 + goto done; 912 903 } 913 904 914 905 /* ··· 942 933 hdr->h_dport != ihdr->h_dport) { 943 934 rds_ib_conn_error(conn, 944 935 "fragment header mismatch; forcing reconnect\n"); 945 - return; 936 + goto done; 946 937 } 947 938 } 948 939 ··· 974 965 975 966 rds_inc_put(&ibinc->ii_inc); 976 967 } 968 + done: 969 + ib_dma_sync_single_for_device(ic->rds_ibdev->dev, dma_addr, 970 + sizeof(*ihdr), DMA_FROM_DEVICE); 977 971 } 978 972 979 973 void rds_ib_recv_cqe_handler(struct rds_ib_connection *ic,
+8
net/rds/ib_send.c
··· 638 638 send->s_sge[0].length = sizeof(struct rds_header); 639 639 send->s_sge[0].lkey = ic->i_pd->local_dma_lkey; 640 640 641 + ib_dma_sync_single_for_cpu(ic->rds_ibdev->dev, 642 + ic->i_send_hdrs_dma[pos], 643 + sizeof(struct rds_header), 644 + DMA_TO_DEVICE); 641 645 memcpy(ic->i_send_hdrs[pos], &rm->m_inc.i_hdr, 642 646 sizeof(struct rds_header)); 643 647 ··· 692 688 adv_credits = 0; 693 689 rds_ib_stats_inc(s_ib_tx_credit_updates); 694 690 } 691 + ib_dma_sync_single_for_device(ic->rds_ibdev->dev, 692 + ic->i_send_hdrs_dma[pos], 693 + sizeof(struct rds_header), 694 + DMA_TO_DEVICE); 695 695 696 696 if (prev) 697 697 prev->s_wr.next = &send->s_wr;