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

Commit 96390f62 authored by Nelson Escobar's avatar Nelson Escobar Committed by Doug Ledford
Browse files

IB/usnic: Handle 0 counts in resource allocation

parent dc92d146
Loading
Loading
Loading
Loading
+29 −25
Original line number Diff line number Diff line
@@ -237,7 +237,7 @@ usnic_vnic_get_resources(struct usnic_vnic *vnic, enum usnic_vnic_res_type type,
	struct usnic_vnic_res *res;
	int i;

	if (usnic_vnic_res_free_cnt(vnic, type) < cnt || cnt < 1 || !owner)
	if (usnic_vnic_res_free_cnt(vnic, type) < cnt || cnt < 0 || !owner)
		return ERR_PTR(-EINVAL);

	ret = kzalloc(sizeof(*ret), GFP_ATOMIC);
@@ -247,7 +247,8 @@ usnic_vnic_get_resources(struct usnic_vnic *vnic, enum usnic_vnic_res_type type,
		return ERR_PTR(-ENOMEM);
	}

	ret->res = kzalloc(sizeof(*(ret->res))*cnt, GFP_ATOMIC);
	if (cnt > 0) {
		ret->res = kcalloc(cnt, sizeof(*(ret->res)), GFP_ATOMIC);
		if (!ret->res) {
			usnic_err("Failed to allocate resources for %s. Out of memory\n",
					usnic_vnic_pci_name(vnic));
@@ -267,6 +268,7 @@ usnic_vnic_get_resources(struct usnic_vnic *vnic, enum usnic_vnic_res_type type,
		}

		spin_unlock(&vnic->res_lock);
	}
	ret->type = type;
	ret->vnic = vnic;
	WARN_ON(ret->cnt != cnt);
@@ -281,6 +283,7 @@ void usnic_vnic_put_resources(struct usnic_vnic_res_chunk *chunk)
	int i;
	struct usnic_vnic *vnic = chunk->vnic;

	if (chunk->cnt > 0) {
		spin_lock(&vnic->res_lock);
		while ((i = --chunk->cnt) >= 0) {
			res = chunk->res[i];
@@ -289,6 +292,7 @@ void usnic_vnic_put_resources(struct usnic_vnic_res_chunk *chunk)
			vnic->chunks[res->type].free_cnt++;
		}
		spin_unlock(&vnic->res_lock);
	}

	kfree(chunk->res);
	kfree(chunk);