Skip to content

Commit 7ae2c17

Browse files
sjp38akpm00
authored andcommitted
mm/damon/modules: deduplicate init steps for DAMON context setup
DAMON_RECLAIM and DAMON_LRU_SORT has duplicated code for DAMON context and target initializations. Deduplicate the part by implementing a function for the initialization in 'modules-common.c' and using it. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: SeongJae Park <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent c8e7b4d commit 7ae2c17

File tree

5 files changed

+53
-30
lines changed

5 files changed

+53
-30
lines changed

mm/damon/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ obj-$(CONFIG_DAMON_VADDR) += ops-common.o vaddr.o
55
obj-$(CONFIG_DAMON_PADDR) += ops-common.o paddr.o
66
obj-$(CONFIG_DAMON_SYSFS) += sysfs-common.o sysfs-schemes.o sysfs.o
77
obj-$(CONFIG_DAMON_DBGFS) += dbgfs.o
8-
obj-$(CONFIG_DAMON_RECLAIM) += reclaim.o
9-
obj-$(CONFIG_DAMON_LRU_SORT) += lru_sort.o
8+
obj-$(CONFIG_DAMON_RECLAIM) += modules-common.o reclaim.o
9+
obj-$(CONFIG_DAMON_LRU_SORT) += modules-common.o lru_sort.o

mm/damon/lru_sort.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -314,25 +314,14 @@ static int damon_lru_sort_after_wmarks_check(struct damon_ctx *c)
314314

315315
static int __init damon_lru_sort_init(void)
316316
{
317-
ctx = damon_new_ctx();
318-
if (!ctx)
319-
return -ENOMEM;
317+
int err = damon_modules_new_paddr_ctx_target(&ctx, &target);
320318

321-
if (damon_select_ops(ctx, DAMON_OPS_PADDR)) {
322-
damon_destroy_ctx(ctx);
323-
return -EINVAL;
324-
}
319+
if (err)
320+
return err;
325321

326322
ctx->callback.after_wmarks_check = damon_lru_sort_after_wmarks_check;
327323
ctx->callback.after_aggregation = damon_lru_sort_after_aggregation;
328324

329-
target = damon_new_target();
330-
if (!target) {
331-
damon_destroy_ctx(ctx);
332-
return -ENOMEM;
333-
}
334-
damon_add_target(ctx, target);
335-
336325
schedule_delayed_work(&damon_lru_sort_timer, 0);
337326

338327
damon_lru_sort_initialized = true;

mm/damon/modules-common.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Common Primitives for DAMON Modules
4+
*
5+
* Author: SeongJae Park <[email protected]>
6+
*/
7+
8+
#include <linux/damon.h>
9+
10+
#include "modules-common.h"
11+
12+
/*
13+
* Allocate, set, and return a DAMON context for the physical address space.
14+
* @ctxp: Pointer to save the point to the newly created context
15+
* @targetp: Pointer to save the point to the newly created target
16+
*/
17+
int damon_modules_new_paddr_ctx_target(struct damon_ctx **ctxp,
18+
struct damon_target **targetp)
19+
{
20+
struct damon_ctx *ctx;
21+
struct damon_target *target;
22+
23+
ctx = damon_new_ctx();
24+
if (!ctx)
25+
return -ENOMEM;
26+
27+
if (damon_select_ops(ctx, DAMON_OPS_PADDR)) {
28+
damon_destroy_ctx(ctx);
29+
return -EINVAL;
30+
}
31+
32+
target = damon_new_target();
33+
if (!target) {
34+
damon_destroy_ctx(ctx);
35+
return -ENOMEM;
36+
}
37+
damon_add_target(ctx, target);
38+
39+
*ctxp = ctx;
40+
*targetp = target;
41+
return 0;
42+
}

mm/damon/modules-common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@
4444
0400); \
4545
module_param_named(nr_##qt_exceed_name, stat.qt_exceeds, ulong, \
4646
0400);
47+
48+
int damon_modules_new_paddr_ctx_target(struct damon_ctx **ctxp,
49+
struct damon_target **targetp);

mm/damon/reclaim.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -256,25 +256,14 @@ static int damon_reclaim_after_wmarks_check(struct damon_ctx *c)
256256

257257
static int __init damon_reclaim_init(void)
258258
{
259-
ctx = damon_new_ctx();
260-
if (!ctx)
261-
return -ENOMEM;
259+
int err = damon_modules_new_paddr_ctx_target(&ctx, &target);
262260

263-
if (damon_select_ops(ctx, DAMON_OPS_PADDR)) {
264-
damon_destroy_ctx(ctx);
265-
return -EINVAL;
266-
}
261+
if (err)
262+
return err;
267263

268264
ctx->callback.after_wmarks_check = damon_reclaim_after_wmarks_check;
269265
ctx->callback.after_aggregation = damon_reclaim_after_aggregation;
270266

271-
target = damon_new_target();
272-
if (!target) {
273-
damon_destroy_ctx(ctx);
274-
return -ENOMEM;
275-
}
276-
damon_add_target(ctx, target);
277-
278267
schedule_delayed_work(&damon_reclaim_timer, 0);
279268

280269
damon_reclaim_initialized = true;

0 commit comments

Comments
 (0)