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

i2c-smbus: Convert kzalloc to devm_kzalloc

Converting kzalloc to devm_kzalloc simplifies the code and ensures that the
result, alert, is freed after the irq allocated by the subsequent
devm_request_irq. This in turn ensures that when an interrupt can be
triggered, the alert structure is still available.

The problem of a free after a devm_request_irq was found using the
following semantic match (http://coccinelle.lip6.fr/)

// <smpl>
@r exists@
expression e1,e2,x,a,b,c,d;
identifier free;
position p1,p2;
@@

devm_request_irq@p1(e1,e2,...,x)
... when any
when != e2 = a
when != x = b
if (...) {
... when != e2 = c
when != x = d
free@p2(...,x,...);
...
return ...;
}
// </smpl>

Signed-off-by: Julia Lawall <Julia.Lawall@lip6.fr>
Signed-off-by: Jean Delvare <khali@linux-fr.org>

authored by

Julia Lawall and committed by
Jean Delvare
71b57845 eee543e8

+4 -7
+4 -7
drivers/i2c/i2c-smbus.c
··· 142 142 struct i2c_adapter *adapter = ara->adapter; 143 143 int res; 144 144 145 - alert = kzalloc(sizeof(struct i2c_smbus_alert), GFP_KERNEL); 145 + alert = devm_kzalloc(&ara->dev, sizeof(struct i2c_smbus_alert), 146 + GFP_KERNEL); 146 147 if (!alert) 147 148 return -ENOMEM; 148 149 ··· 155 154 if (setup->irq > 0) { 156 155 res = devm_request_irq(&ara->dev, setup->irq, smbalert_irq, 157 156 0, "smbus_alert", alert); 158 - if (res) { 159 - kfree(alert); 157 + if (res) 160 158 return res; 161 - } 162 159 } 163 160 164 161 i2c_set_clientdata(ara, alert); ··· 166 167 return 0; 167 168 } 168 169 169 - /* IRQ resource is managed so it is freed automatically */ 170 + /* IRQ and memory resources are managed so they are freed automatically */ 170 171 static int smbalert_remove(struct i2c_client *ara) 171 172 { 172 173 struct i2c_smbus_alert *alert = i2c_get_clientdata(ara); 173 174 174 175 cancel_work_sync(&alert->alert); 175 - 176 - kfree(alert); 177 176 return 0; 178 177 } 179 178