Skip to content

Commit f7134ee

Browse files
dfriesgregkh
authored andcommitted
w1_therm reference count family data
A temperature conversion can take 750 ms and when possible the w1_therm slave driver drops the bus_mutex to allow other bus operations, but that includes operations such as a periodic slave search, which can remove this slave when it is no longer detected. If that happens the sl->family_data will be freed and set to NULL causing w1_slave_show to crash when it wakes up. Signed-off-by: David Fries <David@Fries.net> Reported-By: Thorsten Bschorr <thorsten@bschorr.de> Tested-by: Thorsten Bschorr <thorsten@bschorr.de> Acked-by: Evgeniy Polyakov <zbr@ioremap.net> Cc: stable <stable@vger.kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent c309835 commit f7134ee

1 file changed

Lines changed: 47 additions & 15 deletions

File tree

‎drivers/w1/slaves/w1_therm.c‎

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,32 @@ MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00));
5959
static int w1_strong_pullup = 1;
6060
module_param_named(strong_pullup, w1_strong_pullup, int, 0);
6161

62+
struct w1_therm_family_data {
63+
uint8_t rom[9];
64+
atomic_t refcnt;
65+
};
66+
67+
/* return the address of the refcnt in the family data */
68+
#define THERM_REFCNT(family_data) \
69+
(&((struct w1_therm_family_data*)family_data)->refcnt)
70+
6271
static int w1_therm_add_slave(struct w1_slave *sl)
6372
{
64-
sl->family_data = kzalloc(9, GFP_KERNEL);
73+
sl->family_data = kzalloc(sizeof(struct w1_therm_family_data),
74+
GFP_KERNEL);
6575
if (!sl->family_data)
6676
return -ENOMEM;
77+
atomic_set(THERM_REFCNT(sl->family_data), 1);
6778
return 0;
6879
}
6980

7081
static void w1_therm_remove_slave(struct w1_slave *sl)
7182
{
83+
int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data));
84+
while(refcnt) {
85+
msleep(1000);
86+
refcnt = atomic_read(THERM_REFCNT(sl->family_data));
87+
}
7288
kfree(sl->family_data);
7389
sl->family_data = NULL;
7490
}
@@ -194,13 +210,22 @@ static ssize_t w1_slave_show(struct device *device,
194210
struct w1_slave *sl = dev_to_w1_slave(device);
195211
struct w1_master *dev = sl->master;
196212
u8 rom[9], crc, verdict, external_power;
197-
int i, max_trying = 10;
213+
int i, ret, max_trying = 10;
198214
ssize_t c = PAGE_SIZE;
215+
u8 *family_data = sl->family_data;
216+
217+
ret = mutex_lock_interruptible(&dev->bus_mutex);
218+
if (ret != 0)
219+
goto post_unlock;
199220

200-
i = mutex_lock_interruptible(&dev->bus_mutex);
201-
if (i != 0)
202-
return i;
221+
if(!sl->family_data)
222+
{
223+
ret = -ENODEV;
224+
goto pre_unlock;
225+
}
203226

227+
/* prevent the slave from going away in sleep */
228+
atomic_inc(THERM_REFCNT(family_data));
204229
memset(rom, 0, sizeof(rom));
205230

206231
while (max_trying--) {
@@ -230,17 +255,19 @@ static ssize_t w1_slave_show(struct device *device,
230255
mutex_unlock(&dev->bus_mutex);
231256

232257
sleep_rem = msleep_interruptible(tm);
233-
if (sleep_rem != 0)
234-
return -EINTR;
258+
if (sleep_rem != 0) {
259+
ret = -EINTR;
260+
goto post_unlock;
261+
}
235262

236-
i = mutex_lock_interruptible(&dev->bus_mutex);
237-
if (i != 0)
238-
return i;
263+
ret = mutex_lock_interruptible(&dev->bus_mutex);
264+
if (ret != 0)
265+
goto post_unlock;
239266
} else if (!w1_strong_pullup) {
240267
sleep_rem = msleep_interruptible(tm);
241268
if (sleep_rem != 0) {
242-
mutex_unlock(&dev->bus_mutex);
243-
return -EINTR;
269+
ret = -EINTR;
270+
goto pre_unlock;
244271
}
245272
}
246273

@@ -269,19 +296,24 @@ static ssize_t w1_slave_show(struct device *device,
269296
c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
270297
crc, (verdict) ? "YES" : "NO");
271298
if (verdict)
272-
memcpy(sl->family_data, rom, sizeof(rom));
299+
memcpy(family_data, rom, sizeof(rom));
273300
else
274301
dev_warn(device, "Read failed CRC check\n");
275302

276303
for (i = 0; i < 9; ++i)
277304
c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
278-
((u8 *)sl->family_data)[i]);
305+
((u8 *)family_data)[i]);
279306

280307
c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
281308
w1_convert_temp(rom, sl->family->fid));
309+
ret = PAGE_SIZE - c;
310+
311+
pre_unlock:
282312
mutex_unlock(&dev->bus_mutex);
283313

284-
return PAGE_SIZE - c;
314+
post_unlock:
315+
atomic_dec(THERM_REFCNT(family_data));
316+
return ret;
285317
}
286318

287319
static int __init w1_therm_init(void)

0 commit comments

Comments
 (0)