Skip to content

Commit e114e2e

Browse files
Xueqin Luorafaeljw
authored andcommitted
PM: hibernate: dynamically allocate crc->unc_len/unc for configurable threads
Convert crc->unc_len and crc->unc from fixed-size arrays to dynamically allocated arrays, sized according to the actual number of threads selected at runtime. This removes the fixed limit imposed by CMP_THREADS. Signed-off-by: Xueqin Luo <luoxueqin@kylinos.cn> Link: https://patch.msgid.link/b5db63bb95729482d2649b12d3a11cb7547b7fcc.1761046167.git.luoxueqin@kylinos.cn Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent d3db87f commit e114e2e

1 file changed

Lines changed: 44 additions & 14 deletions

File tree

kernel/power/swap.c

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -585,10 +585,48 @@ struct crc_data {
585585
wait_queue_head_t go; /* start crc update */
586586
wait_queue_head_t done; /* crc update done */
587587
u32 *crc32; /* points to handle's crc32 */
588-
size_t *unc_len[CMP_THREADS]; /* uncompressed lengths */
589-
unsigned char *unc[CMP_THREADS]; /* uncompressed data */
588+
size_t **unc_len; /* uncompressed lengths */
589+
unsigned char **unc; /* uncompressed data */
590590
};
591591

592+
static struct crc_data *alloc_crc_data(int nr_threads)
593+
{
594+
struct crc_data *crc;
595+
596+
crc = kzalloc(sizeof(*crc), GFP_KERNEL);
597+
if (!crc)
598+
return NULL;
599+
600+
crc->unc = kcalloc(nr_threads, sizeof(*crc->unc), GFP_KERNEL);
601+
if (!crc->unc)
602+
goto err_free_crc;
603+
604+
crc->unc_len = kcalloc(nr_threads, sizeof(*crc->unc_len), GFP_KERNEL);
605+
if (!crc->unc_len)
606+
goto err_free_unc;
607+
608+
return crc;
609+
610+
err_free_unc:
611+
kfree(crc->unc);
612+
err_free_crc:
613+
kfree(crc);
614+
return NULL;
615+
}
616+
617+
static void free_crc_data(struct crc_data *crc)
618+
{
619+
if (!crc)
620+
return;
621+
622+
if (crc->thr)
623+
kthread_stop(crc->thr);
624+
625+
kfree(crc->unc_len);
626+
kfree(crc->unc);
627+
kfree(crc);
628+
}
629+
592630
/*
593631
* CRC32 update function that runs in its own thread.
594632
*/
@@ -719,7 +757,7 @@ static int save_compressed_image(struct swap_map_handle *handle,
719757
goto out_clean;
720758
}
721759

722-
crc = kzalloc(sizeof(*crc), GFP_KERNEL);
760+
crc = alloc_crc_data(nr_threads);
723761
if (!crc) {
724762
pr_err("Failed to allocate crc\n");
725763
ret = -ENOMEM;
@@ -885,11 +923,7 @@ static int save_compressed_image(struct swap_map_handle *handle,
885923

886924
out_clean:
887925
hib_finish_batch(&hb);
888-
if (crc) {
889-
if (crc->thr)
890-
kthread_stop(crc->thr);
891-
kfree(crc);
892-
}
926+
free_crc_data(crc);
893927
if (data) {
894928
for (thr = 0; thr < nr_threads; thr++) {
895929
if (data[thr].thr)
@@ -1239,7 +1273,7 @@ static int load_compressed_image(struct swap_map_handle *handle,
12391273
goto out_clean;
12401274
}
12411275

1242-
crc = kzalloc(sizeof(*crc), GFP_KERNEL);
1276+
crc = alloc_crc_data(nr_threads);
12431277
if (!crc) {
12441278
pr_err("Failed to allocate crc\n");
12451279
ret = -ENOMEM;
@@ -1506,11 +1540,7 @@ static int load_compressed_image(struct swap_map_handle *handle,
15061540
hib_finish_batch(&hb);
15071541
for (i = 0; i < ring_size; i++)
15081542
free_page((unsigned long)page[i]);
1509-
if (crc) {
1510-
if (crc->thr)
1511-
kthread_stop(crc->thr);
1512-
kfree(crc);
1513-
}
1543+
free_crc_data(crc);
15141544
if (data) {
15151545
for (thr = 0; thr < nr_threads; thr++) {
15161546
if (data[thr].thr)

0 commit comments

Comments
 (0)