Skip to content

Commit fe8eb12

Browse files
sergey-senozhatskytorvalds
authored andcommitted
zram: add set_max_streams knob
This patch allows to change max_comp_streams on initialised zcomp. Introduce zcomp set_max_streams() knob, zcomp_strm_multi_set_max_streams() and zcomp_strm_single_set_max_streams() callbacks to change streams limit for zcomp_strm_multi and zcomp_strm_single, accordingly. set_max_streams for single steam zcomp does nothing. If user has lowered the limit, then zcomp_strm_multi_set_max_streams() attempts to immediately free extra streams (as much as it can, depending on idle streams availability). Note, this patch does not allow to change stream 'policy' from single to multi stream (or vice versa) on already initialised compression backend. Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com> Acked-by: Minchan Kim <minchan@kernel.org> Cc: Jerome Marchand <jmarchan@redhat.com> Cc: Nitin Gupta <ngupta@vflare.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent beca3ec commit fe8eb12

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

‎drivers/block/zram/zcomp.c‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,29 @@ static void zcomp_strm_multi_release(struct zcomp *comp, struct zcomp_strm *zstr
136136
zcomp_strm_free(comp, zstrm);
137137
}
138138

139+
/* change max_strm limit */
140+
static int zcomp_strm_multi_set_max_streams(struct zcomp *comp, int num_strm)
141+
{
142+
struct zcomp_strm_multi *zs = comp->stream;
143+
struct zcomp_strm *zstrm;
144+
145+
spin_lock(&zs->strm_lock);
146+
zs->max_strm = num_strm;
147+
/*
148+
* if user has lowered the limit and there are idle streams,
149+
* immediately free as much streams (and memory) as we can.
150+
*/
151+
while (zs->avail_strm > num_strm && !list_empty(&zs->idle_strm)) {
152+
zstrm = list_entry(zs->idle_strm.next,
153+
struct zcomp_strm, list);
154+
list_del(&zstrm->list);
155+
zcomp_strm_free(comp, zstrm);
156+
zs->avail_strm--;
157+
}
158+
spin_unlock(&zs->strm_lock);
159+
return 0;
160+
}
161+
139162
static void zcomp_strm_multi_destroy(struct zcomp *comp)
140163
{
141164
struct zcomp_strm_multi *zs = comp->stream;
@@ -158,6 +181,7 @@ static int zcomp_strm_multi_create(struct zcomp *comp, int max_strm)
158181
comp->destroy = zcomp_strm_multi_destroy;
159182
comp->strm_find = zcomp_strm_multi_find;
160183
comp->strm_release = zcomp_strm_multi_release;
184+
comp->set_max_streams = zcomp_strm_multi_set_max_streams;
161185
zs = kmalloc(sizeof(struct zcomp_strm_multi), GFP_KERNEL);
162186
if (!zs)
163187
return -ENOMEM;
@@ -192,6 +216,12 @@ static void zcomp_strm_single_release(struct zcomp *comp,
192216
mutex_unlock(&zs->strm_lock);
193217
}
194218

219+
static int zcomp_strm_single_set_max_streams(struct zcomp *comp, int num_strm)
220+
{
221+
/* zcomp_strm_single support only max_comp_streams == 1 */
222+
return -ENOTSUPP;
223+
}
224+
195225
static void zcomp_strm_single_destroy(struct zcomp *comp)
196226
{
197227
struct zcomp_strm_single *zs = comp->stream;
@@ -206,6 +236,7 @@ static int zcomp_strm_single_create(struct zcomp *comp)
206236
comp->destroy = zcomp_strm_single_destroy;
207237
comp->strm_find = zcomp_strm_single_find;
208238
comp->strm_release = zcomp_strm_single_release;
239+
comp->set_max_streams = zcomp_strm_single_set_max_streams;
209240
zs = kmalloc(sizeof(struct zcomp_strm_single), GFP_KERNEL);
210241
if (!zs)
211242
return -ENOMEM;
@@ -220,6 +251,11 @@ static int zcomp_strm_single_create(struct zcomp *comp)
220251
return 0;
221252
}
222253

254+
int zcomp_set_max_streams(struct zcomp *comp, int num_strm)
255+
{
256+
return comp->set_max_streams(comp, num_strm);
257+
}
258+
223259
struct zcomp_strm *zcomp_strm_find(struct zcomp *comp)
224260
{
225261
return comp->strm_find(comp);

‎drivers/block/zram/zcomp.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct zcomp {
4646

4747
struct zcomp_strm *(*strm_find)(struct zcomp *comp);
4848
void (*strm_release)(struct zcomp *comp, struct zcomp_strm *zstrm);
49+
int (*set_max_streams)(struct zcomp *comp, int num_strm);
4950
void (*destroy)(struct zcomp *comp);
5051
};
5152

@@ -60,4 +61,6 @@ int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm,
6061

6162
int zcomp_decompress(struct zcomp *comp, const unsigned char *src,
6263
size_t src_len, unsigned char *dst);
64+
65+
int zcomp_set_max_streams(struct zcomp *comp, int num_strm);
6366
#endif /* _ZCOMP_H_ */

‎drivers/block/zram/zram_drv.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,8 @@ static ssize_t max_comp_streams_store(struct device *dev,
133133
return -EINVAL;
134134
down_write(&zram->init_lock);
135135
if (init_done(zram)) {
136-
up_write(&zram->init_lock);
137-
pr_info("Can't set max_comp_streams for initialized device\n");
138-
return -EBUSY;
136+
if (zcomp_set_max_streams(zram->comp, num))
137+
pr_info("Cannot change max compression streams\n");
139138
}
140139
zram->max_comp_streams = num;
141140
up_write(&zram->init_lock);

0 commit comments

Comments
 (0)