[PATCH v2 7/7] zram: Use local lock to protect per-CPU data

From: Sebastian Andrzej Siewior
Date: Sun May 24 2020 - 17:58:05 EST


From: Mike Galbraith <umgwanakikbuti@xxxxxxxxx>

The zcomp driver uses per-CPU compression. The per-CPU data pointer is
acquired with get_cpu_ptr() which implicitly disables preemption.
It allocates memory inside the preempt disabled region which conflicts
with the PREEMPT_RT semantics.

Replace the implicit preemption control with an explicit local lock.
This allows RT kernels to substitute it with a real per CPU lock, which
serializes the access but keeps the code section preemptible. On non RT
kernels this maps to preempt_disable() as before, i.e. no functional
change.

[bigeasy: Use local_lock(), description, drop reordering]

Cc: Minchan Kim <minchan@xxxxxxxxxx>
Cc: Nitin Gupta <ngupta@xxxxxxxxxx>
Cc: Sergey Senozhatsky <sergey.senozhatsky.work@xxxxxxxxx>
Signed-off-by: Mike Galbraith <umgwanakikbuti@xxxxxxxxx>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@xxxxxxxxxxxxx>
---
drivers/block/zram/zcomp.c | 17 ++++++++++-------
drivers/block/zram/zcomp.h | 2 ++
2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c
index ae6dc137a1ed8..fa3485309735e 100644
--- a/drivers/block/zram/zcomp.c
+++ b/drivers/block/zram/zcomp.c
@@ -42,11 +42,11 @@ static void zcomp_strm_free(struct zcomp_strm *zstrm)
}

/*
- * allocate new zcomp_strm structure with ->tfm initialized by
- * backend, return NULL on error
+ * Initialize zcomp_strm structure with ->tfm initialized by
+ * backend, and ->buffer. Return a negative value on error.
*/
-static int zcomp_strm_alloc(struct zcomp_strm *zstrm,
- struct zcomp *comp)
+static int zcomp_strm_init(struct zcomp_strm *zstrm,
+ struct zcomp *comp)
{
zstrm->tfm = crypto_alloc_comp(comp->name, 0, 0);
/*
@@ -111,12 +111,13 @@ ssize_t zcomp_available_show(const char *comp, char *buf)

struct zcomp_strm *zcomp_stream_get(struct zcomp *comp)
{
- return get_cpu_ptr(comp->stream);
+ local_lock(&comp->stream->lock);
+ return this_cpu_ptr(comp->stream);
}

void zcomp_stream_put(struct zcomp *comp)
{
- put_cpu_ptr(comp->stream);
+ local_unlock(&comp->stream->lock);
}

int zcomp_compress(struct zcomp_strm *zstrm,
@@ -160,7 +161,9 @@ int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
int ret;

zstrm = per_cpu_ptr(comp->stream, cpu);
- ret = zcomp_strm_alloc(zstrm, comp);
+ local_lock_init(&zstrm->lock);
+
+ ret = zcomp_strm_init(zstrm, comp);
if (ret) {
pr_err("Can't allocate a compression stream\n");
return -ENOMEM;
diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h
index 72c2ee4d843ed..45c4c1858e5a9 100644
--- a/drivers/block/zram/zcomp.h
+++ b/drivers/block/zram/zcomp.h
@@ -5,11 +5,13 @@

#ifndef _ZCOMP_H_
#define _ZCOMP_H_
+#include <linux/locallock.h>

struct zcomp_strm {
/* compression/decompression buffer */
void *buffer;
struct crypto_comp *tfm;
+ struct local_lock lock;
};

/* dynamic per-device compression frontend */
--
2.27.0.rc0