Skip to content

Commit e74a1c6

Browse files
sergey-senozhatskyaxboe
authored andcommitted
zram: pass buffer offset to zcomp_available_show()
In most cases zcomp_available_show() is the only emitting function that is called from sysfs read() handler, so it assumes that there is a whole PAGE_SIZE buffer to work with. There is an exception, however: recomp_algorithm_show(). In recomp_algorithm_show() we prepend the buffer with priority number before we pass it to zcomp_available_show(), so it cannot assume PAGE_SIZE anymore and must take recomp_algorithm_show() modifications into consideration. Therefore we need to pass buffer offset to zcomp_available_show(). Also convert it to use sysfs_emit_at(), to stay aligned with the rest of zram's sysfs read() handlers. On practice we are never even close to using the whole PAGE_SIZE buffer, so that's not a critical bug, but still. Signed-off-by: Sergey Senozhatsky <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 264a3fd commit e74a1c6

3 files changed

Lines changed: 13 additions & 13 deletions

File tree

drivers/block/zram/zcomp.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <linux/sched.h>
99
#include <linux/cpuhotplug.h>
1010
#include <linux/vmalloc.h>
11+
#include <linux/sysfs.h>
1112

1213
#include "zcomp.h"
1314

@@ -89,23 +90,21 @@ bool zcomp_available_algorithm(const char *comp)
8990
}
9091

9192
/* show available compressors */
92-
ssize_t zcomp_available_show(const char *comp, char *buf)
93+
ssize_t zcomp_available_show(const char *comp, char *buf, ssize_t at)
9394
{
94-
ssize_t sz = 0;
9595
int i;
9696

9797
for (i = 0; i < ARRAY_SIZE(backends) - 1; i++) {
9898
if (!strcmp(comp, backends[i]->name)) {
99-
sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
100-
"[%s] ", backends[i]->name);
99+
at += sysfs_emit_at(buf, at, "[%s] ",
100+
backends[i]->name);
101101
} else {
102-
sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
103-
"%s ", backends[i]->name);
102+
at += sysfs_emit_at(buf, at, "%s ", backends[i]->name);
104103
}
105104
}
106105

107-
sz += scnprintf(buf + sz, PAGE_SIZE - sz, "\n");
108-
return sz;
106+
at += sysfs_emit_at(buf, at, "\n");
107+
return at;
109108
}
110109

111110
struct zcomp_strm *zcomp_stream_get(struct zcomp *comp)

drivers/block/zram/zcomp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct zcomp {
7979

8080
int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node);
8181
int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node);
82-
ssize_t zcomp_available_show(const char *comp, char *buf);
82+
ssize_t zcomp_available_show(const char *comp, char *buf, ssize_t at);
8383
bool zcomp_available_algorithm(const char *comp);
8484

8585
struct zcomp *zcomp_create(const char *alg, struct zcomp_params *params);

drivers/block/zram/zram_drv.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,12 +1225,13 @@ static void comp_algorithm_set(struct zram *zram, u32 prio, const char *alg)
12251225
zram->comp_algs[prio] = alg;
12261226
}
12271227

1228-
static ssize_t __comp_algorithm_show(struct zram *zram, u32 prio, char *buf)
1228+
static ssize_t __comp_algorithm_show(struct zram *zram, u32 prio,
1229+
char *buf, ssize_t at)
12291230
{
12301231
ssize_t sz;
12311232

12321233
down_read(&zram->init_lock);
1233-
sz = zcomp_available_show(zram->comp_algs[prio], buf);
1234+
sz = zcomp_available_show(zram->comp_algs[prio], buf, at);
12341235
up_read(&zram->init_lock);
12351236

12361237
return sz;
@@ -1387,7 +1388,7 @@ static ssize_t comp_algorithm_show(struct device *dev,
13871388
{
13881389
struct zram *zram = dev_to_zram(dev);
13891390

1390-
return __comp_algorithm_show(zram, ZRAM_PRIMARY_COMP, buf);
1391+
return __comp_algorithm_show(zram, ZRAM_PRIMARY_COMP, buf, 0);
13911392
}
13921393

13931394
static ssize_t comp_algorithm_store(struct device *dev,
@@ -1416,7 +1417,7 @@ static ssize_t recomp_algorithm_show(struct device *dev,
14161417
continue;
14171418

14181419
sz += sysfs_emit_at(buf, sz, "#%d: ", prio);
1419-
sz += __comp_algorithm_show(zram, prio, buf + sz);
1420+
sz += __comp_algorithm_show(zram, prio, buf, sz);
14201421
}
14211422

14221423
return sz;

0 commit comments

Comments
 (0)