Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 459cc69f authored by Leon Romanovsky's avatar Leon Romanovsky Committed by Jason Gunthorpe
Browse files

RDMA: Provide safe ib_alloc_device() function



All callers to ib_alloc_device() provide a larger size than struct
ib_device and rely on the fact that struct ib_device is embedded in their
driver specific structure as the first member.

Provide a safer variant of ib_alloc_device() that checks and enforces this
approach to make sure the drivers are using it right.

Signed-off-by: default avatarLeon Romanovsky <leonro@mellanox.com>
Signed-off-by: default avatarJason Gunthorpe <jgg@mellanox.com>
parent e5c1bb47
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -268,7 +268,7 @@ static struct class ib_class = {
};

/**
 * ib_alloc_device - allocate an IB device struct
 * _ib_alloc_device - allocate an IB device struct
 * @size:size of structure to allocate
 *
 * Low-level drivers should use ib_alloc_device() to allocate &struct
@@ -277,7 +277,7 @@ static struct class ib_class = {
 * ib_dealloc_device() must be used to free structures allocated with
 * ib_alloc_device().
 */
struct ib_device *ib_alloc_device(size_t size)
struct ib_device *_ib_alloc_device(size_t size)
{
	struct ib_device *device;

@@ -303,7 +303,7 @@ struct ib_device *ib_alloc_device(size_t size)

	return device;
}
EXPORT_SYMBOL(ib_alloc_device);
EXPORT_SYMBOL(_ib_alloc_device);

/**
 * ib_dealloc_device - free an IB device struct
+1 −1
Original line number Diff line number Diff line
@@ -688,7 +688,7 @@ static struct bnxt_re_dev *bnxt_re_dev_add(struct net_device *netdev,
	struct bnxt_re_dev *rdev;

	/* Allocate bnxt_re_dev instance here */
	rdev = (struct bnxt_re_dev *)ib_alloc_device(sizeof(*rdev));
	rdev = ib_alloc_device(bnxt_re_dev, ibdev);
	if (!rdev) {
		dev_err(NULL, "%s: bnxt_re_dev allocation failure!",
			ROCE_DRV_MODULE_NAME);
+1 −1
Original line number Diff line number Diff line
@@ -146,7 +146,7 @@ static void open_rnic_dev(struct t3cdev *tdev)

	pr_debug("%s t3cdev %p\n", __func__,  tdev);
	pr_info_once("Chelsio T3 RDMA Driver - version %s\n", DRV_VERSION);
	rnicp = (struct iwch_dev *)ib_alloc_device(sizeof(*rnicp));
	rnicp = ib_alloc_device(iwch_dev, ibdev);
	if (!rnicp) {
		pr_err("Cannot allocate ib device\n");
		return;
+1 −1
Original line number Diff line number Diff line
@@ -966,7 +966,7 @@ static struct c4iw_dev *c4iw_alloc(const struct cxgb4_lld_info *infop)
		pr_info("%s: On-Chip Queues not supported on this device\n",
			pci_name(infop->pdev));

	devp = (struct c4iw_dev *)ib_alloc_device(sizeof(*devp));
	devp = ib_alloc_device(c4iw_dev, ibdev);
	if (!devp) {
		pr_err("Cannot allocate ib device\n");
		return ERR_PTR(-ENOMEM);
+1 −1
Original line number Diff line number Diff line
@@ -5002,7 +5002,7 @@ static int hns_roce_probe(struct platform_device *pdev)
	struct hns_roce_dev *hr_dev;
	struct device *dev = &pdev->dev;

	hr_dev = (struct hns_roce_dev *)ib_alloc_device(sizeof(*hr_dev));
	hr_dev = ib_alloc_device(hns_roce_dev, ib_dev);
	if (!hr_dev)
		return -ENOMEM;

Loading