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

Configure Feed

Select the types of activity you want to include in your feed.

zram: protect recomp_algorithm_show() with ->init_lock

sysfs handlers should be called under ->init_lock and are not supposed to
unlock it until return, otherwise e.g. a concurrent reset() can occur.
There is one handler that breaks that rule: recomp_algorithm_show().

Move ->init_lock handling outside of __comp_algorithm_show() (also drop it
and call zcomp_available_show() directly) so that the entire
recomp_algorithm_show() loop is protected by the lock, as opposed to
protecting individual iterations.

The patch does not need to go to -stable, as it does not fix any
runtime errors (at least I can't think of any). It makes
recomp_algorithm_show() "atomic" w.r.t. zram reset() (just like the
rest of zram sysfs show() handlers), that's a pretty minor change.

Link: https://lkml.kernel.org/r/20250805101946.1774112-1-senozhatsky@chromium.org
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Reported-by: Seyediman Seyedarab <imandevel@gmail.com>
Suggested-by: Seyediman Seyedarab <imandevel@gmail.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Minchan Kim <minchan@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Sergey Senozhatsky and committed by
Andrew Morton
7cbce1ea 79e1c242

+8 -15
+8 -15
drivers/block/zram/zram_drv.c
··· 1225 1225 zram->comp_algs[prio] = alg; 1226 1226 } 1227 1227 1228 - static ssize_t __comp_algorithm_show(struct zram *zram, u32 prio, 1229 - char *buf, ssize_t at) 1230 - { 1231 - ssize_t sz; 1232 - 1233 - down_read(&zram->init_lock); 1234 - sz = zcomp_available_show(zram->comp_algs[prio], buf, at); 1235 - up_read(&zram->init_lock); 1236 - 1237 - return sz; 1238 - } 1239 - 1240 1228 static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf) 1241 1229 { 1242 1230 char *compressor; ··· 1375 1387 char *buf) 1376 1388 { 1377 1389 struct zram *zram = dev_to_zram(dev); 1390 + ssize_t sz; 1378 1391 1379 - return __comp_algorithm_show(zram, ZRAM_PRIMARY_COMP, buf, 0); 1392 + down_read(&zram->init_lock); 1393 + sz = zcomp_available_show(zram->comp_algs[ZRAM_PRIMARY_COMP], buf, 0); 1394 + up_read(&zram->init_lock); 1395 + return sz; 1380 1396 } 1381 1397 1382 1398 static ssize_t comp_algorithm_store(struct device *dev, ··· 1404 1412 ssize_t sz = 0; 1405 1413 u32 prio; 1406 1414 1415 + down_read(&zram->init_lock); 1407 1416 for (prio = ZRAM_SECONDARY_COMP; prio < ZRAM_MAX_COMPS; prio++) { 1408 1417 if (!zram->comp_algs[prio]) 1409 1418 continue; 1410 1419 1411 1420 sz += sysfs_emit_at(buf, sz, "#%d: ", prio); 1412 - sz += __comp_algorithm_show(zram, prio, buf, sz); 1421 + sz += zcomp_available_show(zram->comp_algs[prio], buf, sz); 1413 1422 } 1414 - 1423 + up_read(&zram->init_lock); 1415 1424 return sz; 1416 1425 } 1417 1426