Skip to contentSkip to conversation

infiniband: Remove redundant NULL check before kfree

PATCH Maninder Singh 2 participants 2 messages Jul 8, 2015 InfiniBand

lore
Share & tools
lore
#0 lore raw

SGVsbG8sDQoNCj4+ICsJCQlmb3IgKGkgPSAwOyBpIDwgZGV2LT5jYXBzLm51bV9wb3J0czsgaSsr KQ0KPj4gKwkJCQlrZnJlZShkbVtpXSk7DQo+PiAJCQlnb3RvIG91dDsNCj4+IAkJfQ0KPj4gCX0N Cj4+IC0tDQo+PiAxLjcuOS41DQo+PiANCj4NCj5JZiB5b3UgYXJlIGdvaW5nIHRvIGNoYW5nZSB0 aGlzLCB5b3UgbWlnaHQgYXMgd2VsbCBtYWtlIGl0IDEwMCUgY29ycmVjdDoNCj4NCj5ply07DQo+ d2hpbGUgKGkgPj0gMCkNCj4Ja2ZyZWUoZG1baV0pOw0KPg0KPlRoZW4geW91IGRvbpJ0IGhhdmUg dG8gd29ycnkgYWJvdXQgd2hldGhlciBrZnJlZSB3b3JrcyBvbiBOVUxMLCBldmVyeSBpdGVtIHlv dSBmcmVlIHdpbGwgYmUgZ3VhcmFudGVlZCB0byBiZSBub24tTlVMTC4NClRoYW5rcyBmb3Igc3Vn Z2VzdGlvbiA6KQ0KU2VudCBuZXcgcGF0Y2ggd2l0aCBkZXNjcmliZWQgY2hhbmdlcywgSSB3YXMg dGhpbmtpbmcgb25lIG1vcmUgdGhpbmcuDQoNCkluIGJlbG93IGNvZGUgOi0NCiAgICAgICAgaWYg KCFpYmRldi0+c3Jpb3YuaXNfZ29pbmdfZG93bikNCiAgICAgICAgICAgIHF1ZXVlX3dvcmsoaWJk ZXYtPnNyaW92LmRlbXV4W2ldLnVkX3dxLCAmZG1baV0tPndvcmspOw0KICAgICAgICBzcGluX3Vu bG9ja19pcnFyZXN0b3JlKCZpYmRldi0+c3Jpb3YuZ29pbmdfZG93bl9sb2NrLCBmbGFncyk7DQog ICAgfQ0Kb3V0Og0KICAgIGtmcmVlKGRtKTsNCiAgICByZXR1cm47DQoNCmRtIGlzIGZyZWVkIGFm dGVyIHF1ZXVlX3dvcmssIGlzIGl0IGNvcnJlY3QgdG8gZnJlZSBkbSB3aGVuIG90aGVyIGRtW2ld IGFyZSBhbGxvY2F0ZWQgPyBpIGRpZCBub3QgZ2V0IGl0Lg0KDQpUaGFua3MNCk1hbmluZGVyDQot LS0tLS0tLS0tLS0=

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#1 lore raw
Quoted 30 lines · Maninder Singh

Hello,

Quoted 11 lines
Quoted 8 lines
(quoted diff) +2 -0
+			for (i = 0; i < dev->caps.num_ports; i++)
+				kfree(dm[i]);
			goto out;
		}
	}

--
1.7.9.5

If you are going to change this, you might as well make it 100% correct:

Quoted 5 lines

i—-;
while (i >= 0)

	kfree(dm[i]);

Then you don’t have to worry about whether kfree works on NULL, every item you free will be guaranteed to be non-NULL.

Thanks for suggestion :)
Sent new patch with described changes, I was thinking one more thing.

In below code :-

        if (!ibdev->sriov.is_going_down)
            queue_work(ibdev->sriov.demux[i].ud_wq, &dm[i]->work);
        spin_unlock_irqrestore(&ibdev->sriov.going_down_lock, flags);
    }
out:
    kfree(dm);
    return;
Quoted 1 line

dm is freed after queue_work, is it correct to free dm when other dm[i] are allocated ? i did not get it.

The dm is just there to give an easy way to refer to a variable number of work structs. The flow is supposed to be something like this:

alloc(dm)
for(i=0;i<num_qps;i++)

    dm[i] == alloc(work item);

for(i=0;i<num_qps;i++)
init dm[i] work item
queue dm[i] work item
free(dm)

In this scenario, all of the dm[i] items should be queued to delayed work. When that work completes, it should then free these structs. So, yes, the dm variable itself is just a temporary means of keeping all those work struct pointers together. However, your question caused me to look closely here, and I see that there is a bug. In particular, if we check the sriov.is_going_down and as a result *don't* queue a work item, then we end up leaking that work struct. In addition, I think there is room to optimize this routine considerably. I'll post a patch for that in a minute.

Signature · 2 lines
Doug Ledford <[email protected]>
              GPG KeyID: 0E572FDD