Skip to content

Commit ed19f19

Browse files
Sebastian Andrzej SiewiorIngo Molnar
authored andcommitted
zram: Allocate struct zcomp_strm as per-CPU memory
zcomp::stream is a per-CPU pointer, pointing to struct zcomp_strm which contains two pointers. Having struct zcomp_strm allocated directly as per-CPU memory would avoid one additional memory allocation and a pointer dereference. This also simplifies the addition of a local_lock to struct zcomp_strm. Allocate zcomp::stream directly as per-CPU memory. Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Acked-by: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 3e92fd7 commit ed19f19

File tree

2 files changed

+17
-26
lines changed

2 files changed

+17
-26
lines changed

drivers/block/zram/zcomp.c

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,16 @@ static void zcomp_strm_free(struct zcomp_strm *zstrm)
3737
if (!IS_ERR_OR_NULL(zstrm->tfm))
3838
crypto_free_comp(zstrm->tfm);
3939
free_pages((unsigned long)zstrm->buffer, 1);
40-
kfree(zstrm);
40+
zstrm->tfm = NULL;
41+
zstrm->buffer = NULL;
4142
}
4243

4344
/*
44-
* allocate new zcomp_strm structure with ->tfm initialized by
45-
* backend, return NULL on error
45+
* Initialize zcomp_strm structure with ->tfm initialized by backend, and
46+
* ->buffer. Return a negative value on error.
4647
*/
47-
static struct zcomp_strm *zcomp_strm_alloc(struct zcomp *comp)
48+
static int zcomp_strm_init(struct zcomp_strm *zstrm, struct zcomp *comp)
4849
{
49-
struct zcomp_strm *zstrm = kmalloc(sizeof(*zstrm), GFP_KERNEL);
50-
if (!zstrm)
51-
return NULL;
52-
5350
zstrm->tfm = crypto_alloc_comp(comp->name, 0, 0);
5451
/*
5552
* allocate 2 pages. 1 for compressed data, plus 1 extra for the
@@ -58,9 +55,9 @@ static struct zcomp_strm *zcomp_strm_alloc(struct zcomp *comp)
5855
zstrm->buffer = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
5956
if (IS_ERR_OR_NULL(zstrm->tfm) || !zstrm->buffer) {
6057
zcomp_strm_free(zstrm);
61-
zstrm = NULL;
58+
return -ENOMEM;
6259
}
63-
return zstrm;
60+
return 0;
6461
}
6562

6663
bool zcomp_available_algorithm(const char *comp)
@@ -113,7 +110,7 @@ ssize_t zcomp_available_show(const char *comp, char *buf)
113110

114111
struct zcomp_strm *zcomp_stream_get(struct zcomp *comp)
115112
{
116-
return *get_cpu_ptr(comp->stream);
113+
return get_cpu_ptr(comp->stream);
117114
}
118115

119116
void zcomp_stream_put(struct zcomp *comp)
@@ -159,36 +156,30 @@ int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
159156
{
160157
struct zcomp *comp = hlist_entry(node, struct zcomp, node);
161158
struct zcomp_strm *zstrm;
159+
int ret;
162160

163-
if (WARN_ON(*per_cpu_ptr(comp->stream, cpu)))
164-
return 0;
165-
166-
zstrm = zcomp_strm_alloc(comp);
167-
if (IS_ERR_OR_NULL(zstrm)) {
161+
zstrm = per_cpu_ptr(comp->stream, cpu);
162+
ret = zcomp_strm_init(zstrm, comp);
163+
if (ret)
168164
pr_err("Can't allocate a compression stream\n");
169-
return -ENOMEM;
170-
}
171-
*per_cpu_ptr(comp->stream, cpu) = zstrm;
172-
return 0;
165+
return ret;
173166
}
174167

175168
int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node)
176169
{
177170
struct zcomp *comp = hlist_entry(node, struct zcomp, node);
178171
struct zcomp_strm *zstrm;
179172

180-
zstrm = *per_cpu_ptr(comp->stream, cpu);
181-
if (!IS_ERR_OR_NULL(zstrm))
182-
zcomp_strm_free(zstrm);
183-
*per_cpu_ptr(comp->stream, cpu) = NULL;
173+
zstrm = per_cpu_ptr(comp->stream, cpu);
174+
zcomp_strm_free(zstrm);
184175
return 0;
185176
}
186177

187178
static int zcomp_init(struct zcomp *comp)
188179
{
189180
int ret;
190181

191-
comp->stream = alloc_percpu(struct zcomp_strm *);
182+
comp->stream = alloc_percpu(struct zcomp_strm);
192183
if (!comp->stream)
193184
return -ENOMEM;
194185

drivers/block/zram/zcomp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct zcomp_strm {
1414

1515
/* dynamic per-device compression frontend */
1616
struct zcomp {
17-
struct zcomp_strm * __percpu *stream;
17+
struct zcomp_strm __percpu *stream;
1818
const char *name;
1919
struct hlist_node node;
2020
};

0 commit comments

Comments
 (0)