Skip to content

Commit 9f92e93

Browse files
SpieringsAEjic23
authored andcommitted
iio: common: st_sensors: Fix use of uninitialize device structs
Throughout the various probe functions &indio_dev->dev is used before it is initialized. This caused a kernel panic in st_sensors_power_enable() when the call to devm_regulator_bulk_get_enable() fails and then calls dev_err_probe() with the uninitialized device. This seems to only cause a panic with dev_err_probe(), dev_err(), dev_warn() and dev_info() don't seem to cause a panic, but are fixed as well. The issue is reported and traced here: [1] Link: https://lore.kernel.org/all/AM7P189MB100986A83D2F28AF3FFAF976E39EA@AM7P189MB1009.EURP189.PROD.OUTLOOK.COM/ [1] Cc: stable@vger.kernel.org Signed-off-by: Maud Spierings <maudspierings@gocontroll.com> Reviewed-by: Andy Shevchenko <andy@kernel.org> Link: https://... [1] Link: https://patch.msgid.link/20250527-st_iio_fix-v4-1-12d89801c761@gocontroll.com Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 6ac609d commit 9f92e93

3 files changed

Lines changed: 31 additions & 35 deletions

File tree

drivers/iio/accel/st_accel_core.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,7 @@ static int apply_acpi_orientation(struct iio_dev *indio_dev)
13531353
union acpi_object *ont;
13541354
union acpi_object *elements;
13551355
acpi_status status;
1356+
struct device *parent = indio_dev->dev.parent;
13561357
int ret = -EINVAL;
13571358
unsigned int val;
13581359
int i, j;
@@ -1371,7 +1372,7 @@ static int apply_acpi_orientation(struct iio_dev *indio_dev)
13711372
};
13721373

13731374

1374-
adev = ACPI_COMPANION(indio_dev->dev.parent);
1375+
adev = ACPI_COMPANION(parent);
13751376
if (!adev)
13761377
return -ENXIO;
13771378

@@ -1380,8 +1381,7 @@ static int apply_acpi_orientation(struct iio_dev *indio_dev)
13801381
if (status == AE_NOT_FOUND) {
13811382
return -ENXIO;
13821383
} else if (ACPI_FAILURE(status)) {
1383-
dev_warn(&indio_dev->dev, "failed to execute _ONT: %d\n",
1384-
status);
1384+
dev_warn(parent, "failed to execute _ONT: %d\n", status);
13851385
return status;
13861386
}
13871387

@@ -1457,12 +1457,12 @@ static int apply_acpi_orientation(struct iio_dev *indio_dev)
14571457
}
14581458

14591459
ret = 0;
1460-
dev_info(&indio_dev->dev, "computed mount matrix from ACPI\n");
1460+
dev_info(parent, "computed mount matrix from ACPI\n");
14611461

14621462
out:
14631463
kfree(buffer.pointer);
14641464
if (ret)
1465-
dev_dbg(&indio_dev->dev,
1465+
dev_dbg(parent,
14661466
"failed to apply ACPI orientation data: %d\n", ret);
14671467

14681468
return ret;

drivers/iio/common/st_sensors/st_sensors_core.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static int st_sensors_set_fullscale(struct iio_dev *indio_dev, unsigned int fs)
154154
return err;
155155

156156
st_accel_set_fullscale_error:
157-
dev_err(&indio_dev->dev, "failed to set new fullscale.\n");
157+
dev_err(indio_dev->dev.parent, "failed to set new fullscale.\n");
158158
return err;
159159
}
160160

@@ -231,8 +231,7 @@ int st_sensors_power_enable(struct iio_dev *indio_dev)
231231
ARRAY_SIZE(regulator_names),
232232
regulator_names);
233233
if (err)
234-
return dev_err_probe(&indio_dev->dev, err,
235-
"unable to enable supplies\n");
234+
return dev_err_probe(parent, err, "unable to enable supplies\n");
236235

237236
return 0;
238237
}
@@ -241,13 +240,14 @@ EXPORT_SYMBOL_NS(st_sensors_power_enable, "IIO_ST_SENSORS");
241240
static int st_sensors_set_drdy_int_pin(struct iio_dev *indio_dev,
242241
struct st_sensors_platform_data *pdata)
243242
{
243+
struct device *parent = indio_dev->dev.parent;
244244
struct st_sensor_data *sdata = iio_priv(indio_dev);
245245

246246
/* Sensor does not support interrupts */
247247
if (!sdata->sensor_settings->drdy_irq.int1.addr &&
248248
!sdata->sensor_settings->drdy_irq.int2.addr) {
249249
if (pdata->drdy_int_pin)
250-
dev_info(&indio_dev->dev,
250+
dev_info(parent,
251251
"DRDY on pin INT%d specified, but sensor does not support interrupts\n",
252252
pdata->drdy_int_pin);
253253
return 0;
@@ -256,29 +256,27 @@ static int st_sensors_set_drdy_int_pin(struct iio_dev *indio_dev,
256256
switch (pdata->drdy_int_pin) {
257257
case 1:
258258
if (!sdata->sensor_settings->drdy_irq.int1.mask) {
259-
dev_err(&indio_dev->dev,
260-
"DRDY on INT1 not available.\n");
259+
dev_err(parent, "DRDY on INT1 not available.\n");
261260
return -EINVAL;
262261
}
263262
sdata->drdy_int_pin = 1;
264263
break;
265264
case 2:
266265
if (!sdata->sensor_settings->drdy_irq.int2.mask) {
267-
dev_err(&indio_dev->dev,
268-
"DRDY on INT2 not available.\n");
266+
dev_err(parent, "DRDY on INT2 not available.\n");
269267
return -EINVAL;
270268
}
271269
sdata->drdy_int_pin = 2;
272270
break;
273271
default:
274-
dev_err(&indio_dev->dev, "DRDY on pdata not valid.\n");
272+
dev_err(parent, "DRDY on pdata not valid.\n");
275273
return -EINVAL;
276274
}
277275

278276
if (pdata->open_drain) {
279277
if (!sdata->sensor_settings->drdy_irq.int1.addr_od &&
280278
!sdata->sensor_settings->drdy_irq.int2.addr_od)
281-
dev_err(&indio_dev->dev,
279+
dev_err(parent,
282280
"open drain requested but unsupported.\n");
283281
else
284282
sdata->int_pin_open_drain = true;
@@ -336,14 +334,15 @@ EXPORT_SYMBOL_NS(st_sensors_dev_name_probe, "IIO_ST_SENSORS");
336334
int st_sensors_init_sensor(struct iio_dev *indio_dev,
337335
struct st_sensors_platform_data *pdata)
338336
{
337+
struct device *parent = indio_dev->dev.parent;
339338
struct st_sensor_data *sdata = iio_priv(indio_dev);
340339
struct st_sensors_platform_data *of_pdata;
341340
int err = 0;
342341

343342
mutex_init(&sdata->odr_lock);
344343

345344
/* If OF/DT pdata exists, it will take precedence of anything else */
346-
of_pdata = st_sensors_dev_probe(indio_dev->dev.parent, pdata);
345+
of_pdata = st_sensors_dev_probe(parent, pdata);
347346
if (IS_ERR(of_pdata))
348347
return PTR_ERR(of_pdata);
349348
if (of_pdata)
@@ -370,7 +369,7 @@ int st_sensors_init_sensor(struct iio_dev *indio_dev,
370369
if (err < 0)
371370
return err;
372371
} else
373-
dev_info(&indio_dev->dev, "Full-scale not possible\n");
372+
dev_info(parent, "Full-scale not possible\n");
374373

375374
err = st_sensors_set_odr(indio_dev, sdata->odr);
376375
if (err < 0)
@@ -405,7 +404,7 @@ int st_sensors_init_sensor(struct iio_dev *indio_dev,
405404
mask = sdata->sensor_settings->drdy_irq.int2.mask_od;
406405
}
407406

408-
dev_info(&indio_dev->dev,
407+
dev_info(parent,
409408
"set interrupt line to open drain mode on pin %d\n",
410409
sdata->drdy_int_pin);
411410
err = st_sensors_write_data_with_mask(indio_dev, addr,
@@ -593,21 +592,20 @@ EXPORT_SYMBOL_NS(st_sensors_get_settings_index, "IIO_ST_SENSORS");
593592
int st_sensors_verify_id(struct iio_dev *indio_dev)
594593
{
595594
struct st_sensor_data *sdata = iio_priv(indio_dev);
595+
struct device *parent = indio_dev->dev.parent;
596596
int wai, err;
597597

598598
if (sdata->sensor_settings->wai_addr) {
599599
err = regmap_read(sdata->regmap,
600600
sdata->sensor_settings->wai_addr, &wai);
601601
if (err < 0) {
602-
dev_err(&indio_dev->dev,
603-
"failed to read Who-Am-I register.\n");
604-
return err;
602+
return dev_err_probe(parent, err,
603+
"failed to read Who-Am-I register.\n");
605604
}
606605

607606
if (sdata->sensor_settings->wai != wai) {
608-
dev_warn(&indio_dev->dev,
609-
"%s: WhoAmI mismatch (0x%x).\n",
610-
indio_dev->name, wai);
607+
dev_warn(parent, "%s: WhoAmI mismatch (0x%x).\n",
608+
indio_dev->name, wai);
611609
}
612610
}
613611

drivers/iio/common/st_sensors/st_sensors_trigger.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
127127
sdata->trig = devm_iio_trigger_alloc(parent, "%s-trigger",
128128
indio_dev->name);
129129
if (sdata->trig == NULL) {
130-
dev_err(&indio_dev->dev, "failed to allocate iio trigger.\n");
130+
dev_err(parent, "failed to allocate iio trigger.\n");
131131
return -ENOMEM;
132132
}
133133

@@ -143,7 +143,7 @@ int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
143143
case IRQF_TRIGGER_FALLING:
144144
case IRQF_TRIGGER_LOW:
145145
if (!sdata->sensor_settings->drdy_irq.addr_ihl) {
146-
dev_err(&indio_dev->dev,
146+
dev_err(parent,
147147
"falling/low specified for IRQ but hardware supports only rising/high: will request rising/high\n");
148148
if (irq_trig == IRQF_TRIGGER_FALLING)
149149
irq_trig = IRQF_TRIGGER_RISING;
@@ -156,21 +156,19 @@ int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
156156
sdata->sensor_settings->drdy_irq.mask_ihl, 1);
157157
if (err < 0)
158158
return err;
159-
dev_info(&indio_dev->dev,
159+
dev_info(parent,
160160
"interrupts on the falling edge or active low level\n");
161161
}
162162
break;
163163
case IRQF_TRIGGER_RISING:
164-
dev_info(&indio_dev->dev,
165-
"interrupts on the rising edge\n");
164+
dev_info(parent, "interrupts on the rising edge\n");
166165
break;
167166
case IRQF_TRIGGER_HIGH:
168-
dev_info(&indio_dev->dev,
169-
"interrupts active high level\n");
167+
dev_info(parent, "interrupts active high level\n");
170168
break;
171169
default:
172170
/* This is the most preferred mode, if possible */
173-
dev_err(&indio_dev->dev,
171+
dev_err(parent,
174172
"unsupported IRQ trigger specified (%lx), enforce rising edge\n", irq_trig);
175173
irq_trig = IRQF_TRIGGER_RISING;
176174
}
@@ -179,7 +177,7 @@ int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
179177
if (irq_trig == IRQF_TRIGGER_FALLING ||
180178
irq_trig == IRQF_TRIGGER_RISING) {
181179
if (!sdata->sensor_settings->drdy_irq.stat_drdy.addr) {
182-
dev_err(&indio_dev->dev,
180+
dev_err(parent,
183181
"edge IRQ not supported w/o stat register.\n");
184182
return -EOPNOTSUPP;
185183
}
@@ -214,13 +212,13 @@ int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
214212
sdata->trig->name,
215213
sdata->trig);
216214
if (err) {
217-
dev_err(&indio_dev->dev, "failed to request trigger IRQ.\n");
215+
dev_err(parent, "failed to request trigger IRQ.\n");
218216
return err;
219217
}
220218

221219
err = devm_iio_trigger_register(parent, sdata->trig);
222220
if (err < 0) {
223-
dev_err(&indio_dev->dev, "failed to register iio trigger.\n");
221+
dev_err(parent, "failed to register iio trigger.\n");
224222
return err;
225223
}
226224
indio_dev->trig = iio_trigger_get(sdata->trig);

0 commit comments

Comments
 (0)