Skip to content

Commit 71b5784

Browse files
JuliaLawallJean Delvare
authored andcommitted
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>
1 parent eee543e commit 71b5784

1 file changed

Lines changed: 4 additions & 7 deletions

File tree

‎drivers/i2c/i2c-smbus.c‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ static int smbalert_probe(struct i2c_client *ara,
142142
struct i2c_adapter *adapter = ara->adapter;
143143
int res;
144144

145-
alert = kzalloc(sizeof(struct i2c_smbus_alert), GFP_KERNEL);
145+
alert = devm_kzalloc(&ara->dev, sizeof(struct i2c_smbus_alert),
146+
GFP_KERNEL);
146147
if (!alert)
147148
return -ENOMEM;
148149

@@ -154,10 +155,8 @@ static int smbalert_probe(struct i2c_client *ara,
154155
if (setup->irq > 0) {
155156
res = devm_request_irq(&ara->dev, setup->irq, smbalert_irq,
156157
0, "smbus_alert", alert);
157-
if (res) {
158-
kfree(alert);
158+
if (res)
159159
return res;
160-
}
161160
}
162161

163162
i2c_set_clientdata(ara, alert);
@@ -167,14 +166,12 @@ static int smbalert_probe(struct i2c_client *ara,
167166
return 0;
168167
}
169168

170-
/* IRQ resource is managed so it is freed automatically */
169+
/* IRQ and memory resources are managed so they are freed automatically */
171170
static int smbalert_remove(struct i2c_client *ara)
172171
{
173172
struct i2c_smbus_alert *alert = i2c_get_clientdata(ara);
174173

175174
cancel_work_sync(&alert->alert);
176-
177-
kfree(alert);
178175
return 0;
179176
}
180177

0 commit comments

Comments
 (0)