Commit 12d1e2f3 authored by Christophe JAILLET's avatar Christophe JAILLET Committed by Jason Gunthorpe
Browse files

IB/mthca: Use bitmap_zalloc() when applicable

Use 'bitmap_zalloc()' to simplify code, improve the semantic and avoid
some open-coded arithmetic in allocator arguments.

Using the 'zalloc' version of the allocator also saves a now useless
'bitmap_zero()' call.

Also change the corresponding 'kfree()' into 'bitmap_free()' to keep
consistency.

Link: https://lore.kernel.org/r/ea9031e28f453bc179033740f66f0c19293fcf0b.1637785902.git.christophe.jaillet@wanadoo.fr


Signed-off-by: default avatarChristophe JAILLET <christophe.jaillet@wanadoo.fr>
Signed-off-by: default avatarJason Gunthorpe <jgg@nvidia.com>
parent 9c3631d1
Loading
Loading
Loading
Loading
+2 −4
Original line number Original line Diff line number Diff line
@@ -90,12 +90,10 @@ int mthca_alloc_init(struct mthca_alloc *alloc, u32 num, u32 mask,
	alloc->max  = num;
	alloc->max  = num;
	alloc->mask = mask;
	alloc->mask = mask;
	spin_lock_init(&alloc->lock);
	spin_lock_init(&alloc->lock);
	alloc->table = kmalloc_array(BITS_TO_LONGS(num), sizeof(long),
	alloc->table = bitmap_zalloc(num, GFP_KERNEL);
				     GFP_KERNEL);
	if (!alloc->table)
	if (!alloc->table)
		return -ENOMEM;
		return -ENOMEM;


	bitmap_zero(alloc->table, num);
	for (i = 0; i < reserved; ++i)
	for (i = 0; i < reserved; ++i)
		set_bit(i, alloc->table);
		set_bit(i, alloc->table);


@@ -104,7 +102,7 @@ int mthca_alloc_init(struct mthca_alloc *alloc, u32 num, u32 mask,


void mthca_alloc_cleanup(struct mthca_alloc *alloc)
void mthca_alloc_cleanup(struct mthca_alloc *alloc)
{
{
	kfree(alloc->table);
	bitmap_free(alloc->table);
}
}


/*
/*
+5 −7
Original line number Original line Diff line number Diff line
@@ -139,7 +139,7 @@ static void mthca_buddy_free(struct mthca_buddy *buddy, u32 seg, int order)


static int mthca_buddy_init(struct mthca_buddy *buddy, int max_order)
static int mthca_buddy_init(struct mthca_buddy *buddy, int max_order)
{
{
	int i, s;
	int i;


	buddy->max_order = max_order;
	buddy->max_order = max_order;
	spin_lock_init(&buddy->lock);
	spin_lock_init(&buddy->lock);
@@ -152,12 +152,10 @@ static int mthca_buddy_init(struct mthca_buddy *buddy, int max_order)
		goto err_out;
		goto err_out;


	for (i = 0; i <= buddy->max_order; ++i) {
	for (i = 0; i <= buddy->max_order; ++i) {
		s = BITS_TO_LONGS(1 << (buddy->max_order - i));
		buddy->bits[i] = bitmap_zalloc(1 << (buddy->max_order - i),
		buddy->bits[i] = kmalloc_array(s, sizeof(long), GFP_KERNEL);
					       GFP_KERNEL);
		if (!buddy->bits[i])
		if (!buddy->bits[i])
			goto err_out_free;
			goto err_out_free;
		bitmap_zero(buddy->bits[i],
			    1 << (buddy->max_order - i));
	}
	}


	set_bit(0, buddy->bits[buddy->max_order]);
	set_bit(0, buddy->bits[buddy->max_order]);
@@ -167,7 +165,7 @@ static int mthca_buddy_init(struct mthca_buddy *buddy, int max_order)


err_out_free:
err_out_free:
	for (i = 0; i <= buddy->max_order; ++i)
	for (i = 0; i <= buddy->max_order; ++i)
		kfree(buddy->bits[i]);
		bitmap_free(buddy->bits[i]);


err_out:
err_out:
	kfree(buddy->bits);
	kfree(buddy->bits);
@@ -181,7 +179,7 @@ static void mthca_buddy_cleanup(struct mthca_buddy *buddy)
	int i;
	int i;


	for (i = 0; i <= buddy->max_order; ++i)
	for (i = 0; i <= buddy->max_order; ++i)
		kfree(buddy->bits[i]);
		bitmap_free(buddy->bits[i]);


	kfree(buddy->bits);
	kfree(buddy->bits);
	kfree(buddy->num_free);
	kfree(buddy->num_free);