Skip to content

Commit 8ba2f84

Browse files
Chengming Zhouakpm00
authored andcommitted
mm/zswap: change per-cpu mutex and buffer to per-acomp_ctx
First of all, we need to rename acomp_ctx->dstmem field to buffer, since we are now using for purposes other than compression. Then we change per-cpu mutex and buffer to per-acomp_ctx, since them belong to the acomp_ctx and are necessary parts when used in the compress/decompress contexts. So we can remove the old per-cpu mutex and dstmem. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Chengming Zhou <[email protected]> Acked-by: Chris Li <[email protected]> (Google) Reviewed-by: Nhat Pham <[email protected]> Cc: Barry Song <[email protected]> Cc: Dan Streetman <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Seth Jennings <[email protected]> Cc: Vitaly Wool <[email protected]> Cc: Yosry Ahmed <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent e947ba0 commit 8ba2f84

File tree

2 files changed

+33
-72
lines changed

2 files changed

+33
-72
lines changed

include/linux/cpuhotplug.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ enum cpuhp_state {
124124
CPUHP_ARM_BL_PREPARE,
125125
CPUHP_TRACE_RB_PREPARE,
126126
CPUHP_MM_ZS_PREPARE,
127-
CPUHP_MM_ZSWP_MEM_PREPARE,
128127
CPUHP_MM_ZSWP_POOL_PREPARE,
129128
CPUHP_KVM_PPC_BOOK3S_PREPARE,
130129
CPUHP_ZCOMP_PREPARE,

mm/zswap.c

Lines changed: 33 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ struct crypto_acomp_ctx {
161161
struct crypto_acomp *acomp;
162162
struct acomp_req *req;
163163
struct crypto_wait wait;
164-
u8 *dstmem;
165-
struct mutex *mutex;
164+
u8 *buffer;
165+
struct mutex mutex;
166166
};
167167

168168
/*
@@ -688,72 +688,35 @@ static void zswap_alloc_shrinker(struct zswap_pool *pool)
688688
/*********************************
689689
* per-cpu code
690690
**********************************/
691-
static DEFINE_PER_CPU(u8 *, zswap_dstmem);
692-
/*
693-
* If users dynamically change the zpool type and compressor at runtime, i.e.
694-
* zswap is running, zswap can have more than one zpool on one cpu, but they
695-
* are sharing dtsmem. So we need this mutex to be per-cpu.
696-
*/
697-
static DEFINE_PER_CPU(struct mutex *, zswap_mutex);
698-
699-
static int zswap_dstmem_prepare(unsigned int cpu)
700-
{
701-
struct mutex *mutex;
702-
u8 *dst;
703-
704-
dst = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu));
705-
if (!dst)
706-
return -ENOMEM;
707-
708-
mutex = kmalloc_node(sizeof(*mutex), GFP_KERNEL, cpu_to_node(cpu));
709-
if (!mutex) {
710-
kfree(dst);
711-
return -ENOMEM;
712-
}
713-
714-
mutex_init(mutex);
715-
per_cpu(zswap_dstmem, cpu) = dst;
716-
per_cpu(zswap_mutex, cpu) = mutex;
717-
return 0;
718-
}
719-
720-
static int zswap_dstmem_dead(unsigned int cpu)
721-
{
722-
struct mutex *mutex;
723-
u8 *dst;
724-
725-
mutex = per_cpu(zswap_mutex, cpu);
726-
kfree(mutex);
727-
per_cpu(zswap_mutex, cpu) = NULL;
728-
729-
dst = per_cpu(zswap_dstmem, cpu);
730-
kfree(dst);
731-
per_cpu(zswap_dstmem, cpu) = NULL;
732-
733-
return 0;
734-
}
735-
736691
static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
737692
{
738693
struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);
739694
struct crypto_acomp_ctx *acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu);
740695
struct crypto_acomp *acomp;
741696
struct acomp_req *req;
697+
int ret;
698+
699+
mutex_init(&acomp_ctx->mutex);
700+
701+
acomp_ctx->buffer = kmalloc_node(PAGE_SIZE * 2, GFP_KERNEL, cpu_to_node(cpu));
702+
if (!acomp_ctx->buffer)
703+
return -ENOMEM;
742704

743705
acomp = crypto_alloc_acomp_node(pool->tfm_name, 0, 0, cpu_to_node(cpu));
744706
if (IS_ERR(acomp)) {
745707
pr_err("could not alloc crypto acomp %s : %ld\n",
746708
pool->tfm_name, PTR_ERR(acomp));
747-
return PTR_ERR(acomp);
709+
ret = PTR_ERR(acomp);
710+
goto acomp_fail;
748711
}
749712
acomp_ctx->acomp = acomp;
750713

751714
req = acomp_request_alloc(acomp_ctx->acomp);
752715
if (!req) {
753716
pr_err("could not alloc crypto acomp_request %s\n",
754717
pool->tfm_name);
755-
crypto_free_acomp(acomp_ctx->acomp);
756-
return -ENOMEM;
718+
ret = -ENOMEM;
719+
goto req_fail;
757720
}
758721
acomp_ctx->req = req;
759722

@@ -766,10 +729,13 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
766729
acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
767730
crypto_req_done, &acomp_ctx->wait);
768731

769-
acomp_ctx->mutex = per_cpu(zswap_mutex, cpu);
770-
acomp_ctx->dstmem = per_cpu(zswap_dstmem, cpu);
771-
772732
return 0;
733+
734+
req_fail:
735+
crypto_free_acomp(acomp_ctx->acomp);
736+
acomp_fail:
737+
kfree(acomp_ctx->buffer);
738+
return ret;
773739
}
774740

775741
static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)
@@ -782,6 +748,7 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)
782748
acomp_request_free(acomp_ctx->req);
783749
if (!IS_ERR_OR_NULL(acomp_ctx->acomp))
784750
crypto_free_acomp(acomp_ctx->acomp);
751+
kfree(acomp_ctx->buffer);
785752
}
786753

787754
return 0;
@@ -1391,12 +1358,12 @@ static void __zswap_load(struct zswap_entry *entry, struct page *page)
13911358
u8 *src;
13921359

13931360
acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
1394-
mutex_lock(acomp_ctx->mutex);
1361+
mutex_lock(&acomp_ctx->mutex);
13951362

13961363
src = zpool_map_handle(zpool, entry->handle, ZPOOL_MM_RO);
13971364
if (!zpool_can_sleep_mapped(zpool)) {
1398-
memcpy(acomp_ctx->dstmem, src, entry->length);
1399-
src = acomp_ctx->dstmem;
1365+
memcpy(acomp_ctx->buffer, src, entry->length);
1366+
src = acomp_ctx->buffer;
14001367
zpool_unmap_handle(zpool, entry->handle);
14011368
}
14021369

@@ -1406,7 +1373,7 @@ static void __zswap_load(struct zswap_entry *entry, struct page *page)
14061373
acomp_request_set_params(acomp_ctx->req, &input, &output, entry->length, PAGE_SIZE);
14071374
BUG_ON(crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait));
14081375
BUG_ON(acomp_ctx->req->dlen != PAGE_SIZE);
1409-
mutex_unlock(acomp_ctx->mutex);
1376+
mutex_unlock(&acomp_ctx->mutex);
14101377

14111378
if (zpool_can_sleep_mapped(zpool))
14121379
zpool_unmap_handle(zpool, entry->handle);
@@ -1622,13 +1589,17 @@ bool zswap_store(struct folio *folio)
16221589
/* compress */
16231590
acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
16241591

1625-
mutex_lock(acomp_ctx->mutex);
1592+
mutex_lock(&acomp_ctx->mutex);
16261593

1627-
dst = acomp_ctx->dstmem;
1594+
dst = acomp_ctx->buffer;
16281595
sg_init_table(&input, 1);
16291596
sg_set_page(&input, page, PAGE_SIZE, 0);
16301597

1631-
/* zswap_dstmem is of size (PAGE_SIZE * 2). Reflect same in sg_list */
1598+
/*
1599+
* We need PAGE_SIZE * 2 here since there maybe over-compression case,
1600+
* and hardware-accelerators may won't check the dst buffer size, so
1601+
* giving the dst buffer with enough length to avoid buffer overflow.
1602+
*/
16321603
sg_init_one(&output, dst, PAGE_SIZE * 2);
16331604
acomp_request_set_params(acomp_ctx->req, &input, &output, PAGE_SIZE, dlen);
16341605
/*
@@ -1668,7 +1639,7 @@ bool zswap_store(struct folio *folio)
16681639
buf = zpool_map_handle(zpool, handle, ZPOOL_MM_WO);
16691640
memcpy(buf, dst, dlen);
16701641
zpool_unmap_handle(zpool, handle);
1671-
mutex_unlock(acomp_ctx->mutex);
1642+
mutex_unlock(&acomp_ctx->mutex);
16721643

16731644
/* populate entry */
16741645
entry->swpentry = swp_entry(type, offset);
@@ -1711,7 +1682,7 @@ bool zswap_store(struct folio *folio)
17111682
return true;
17121683

17131684
put_dstmem:
1714-
mutex_unlock(acomp_ctx->mutex);
1685+
mutex_unlock(&acomp_ctx->mutex);
17151686
put_pool:
17161687
zswap_pool_put(entry->pool);
17171688
freepage:
@@ -1886,13 +1857,6 @@ static int zswap_setup(void)
18861857
goto cache_fail;
18871858
}
18881859

1889-
ret = cpuhp_setup_state(CPUHP_MM_ZSWP_MEM_PREPARE, "mm/zswap:prepare",
1890-
zswap_dstmem_prepare, zswap_dstmem_dead);
1891-
if (ret) {
1892-
pr_err("dstmem alloc failed\n");
1893-
goto dstmem_fail;
1894-
}
1895-
18961860
ret = cpuhp_setup_state_multi(CPUHP_MM_ZSWP_POOL_PREPARE,
18971861
"mm/zswap_pool:prepare",
18981862
zswap_cpu_comp_prepare,
@@ -1924,8 +1888,6 @@ static int zswap_setup(void)
19241888
if (pool)
19251889
zswap_pool_destroy(pool);
19261890
hp_fail:
1927-
cpuhp_remove_state(CPUHP_MM_ZSWP_MEM_PREPARE);
1928-
dstmem_fail:
19291891
kmem_cache_destroy(zswap_entry_cache);
19301892
cache_fail:
19311893
/* if built-in, we aren't unloaded on failure; don't allow use */

0 commit comments

Comments
 (0)