Forum | Documentation | Website | Blog

Skip to content
Snippets Groups Projects
Commit 144760f8 authored by SeongJae Park's avatar SeongJae Park Committed by Linus Torvalds
Browse files

mm/damon/dbgfs/init_regions: use target index instead of target id

Patch series "Remove the type-unclear target id concept".

DAMON asks each monitoring target ('struct damon_target') to have one
'unsigned long' integer called 'id', which should be unique among the
targets of same monitoring context.  Meaning of it is, however, totally up
to the monitoring primitives that registered to the monitoring context.
For example, the virtual address spaces monitoring primitives treats the
id as a 'struct pid' pointer.

This makes the code flexible but ugly, not well-documented, and
type-unsafe[1].  Also, identification of each target can be done via its
index.  For the reason, this patchset removes the concept and uses clear
type definition.

[1] https://lore.kernel.org/linux-mm/20211013154535.4aaeaaf9d0182922e405dd1e@linux-foundation.org/

This patch (of 4):

Target id is a 'unsigned long' data, which can be interpreted differently
by each monitoring primitives.  For example, it means 'struct pid *' for
the virtual address spaces monitoring, while it means nothing but an
integer to be displayed to debugfs interface users for the physical
address space monitoring.  It's flexible but makes code ugly and
type-unsafe[1].

To be prepared for eventual removal of the concept, this commit removes a
use case of the concept in 'init_regions' debugfs file handling.  In
detail, this commit replaces use of the id with the index of each target
in the context's targets list.

[1] https://lore.kernel.org/linux-mm/20211013154535.4aaeaaf9d0182922e405dd1e@linux-foundation.org/

Link: https://lkml.kernel.org/r/20211230100723.2238-1-sj@kernel.org
Link: https://lkml.kernel.org/r/20211230100723.2238-2-sj@kernel.org


Signed-off-by: default avatarSeongJae Park <sj@kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent d0977efa
No related merge requests found
...@@ -113,19 +113,19 @@ static void damon_dbgfs_test_set_init_regions(struct kunit *test) ...@@ -113,19 +113,19 @@ static void damon_dbgfs_test_set_init_regions(struct kunit *test)
{ {
struct damon_ctx *ctx = damon_new_ctx(); struct damon_ctx *ctx = damon_new_ctx();
unsigned long ids[] = {1, 2, 3}; unsigned long ids[] = {1, 2, 3};
/* Each line represents one region in ``<target id> <start> <end>`` */ /* Each line represents one region in ``<target idx> <start> <end>`` */
char * const valid_inputs[] = {"2 10 20\n 2 20 30\n2 35 45", char * const valid_inputs[] = {"1 10 20\n 1 20 30\n1 35 45",
"2 10 20\n", "1 10 20\n",
"2 10 20\n1 39 59\n1 70 134\n 2 20 25\n", "1 10 20\n0 39 59\n0 70 134\n 1 20 25\n",
""}; ""};
/* Reading the file again will show sorted, clean output */ /* Reading the file again will show sorted, clean output */
char * const valid_expects[] = {"2 10 20\n2 20 30\n2 35 45\n", char * const valid_expects[] = {"1 10 20\n1 20 30\n1 35 45\n",
"2 10 20\n", "1 10 20\n",
"1 39 59\n1 70 134\n2 10 20\n2 20 25\n", "0 39 59\n0 70 134\n1 10 20\n1 20 25\n",
""}; ""};
char * const invalid_inputs[] = {"4 10 20\n", /* target not exists */ char * const invalid_inputs[] = {"3 10 20\n", /* target not exists */
"2 10 20\n 2 14 26\n", /* regions overlap */ "1 10 20\n 1 14 26\n", /* regions overlap */
"1 10 20\n2 30 40\n 1 5 8"}; /* not sorted by address */ "0 10 20\n1 30 40\n 0 5 8"}; /* not sorted by address */
char *input, *expect; char *input, *expect;
int i, rc; int i, rc;
char buf[256]; char buf[256];
......
...@@ -440,18 +440,20 @@ static ssize_t sprint_init_regions(struct damon_ctx *c, char *buf, ssize_t len) ...@@ -440,18 +440,20 @@ static ssize_t sprint_init_regions(struct damon_ctx *c, char *buf, ssize_t len)
{ {
struct damon_target *t; struct damon_target *t;
struct damon_region *r; struct damon_region *r;
int target_idx = 0;
int written = 0; int written = 0;
int rc; int rc;
damon_for_each_target(t, c) { damon_for_each_target(t, c) {
damon_for_each_region(r, t) { damon_for_each_region(r, t) {
rc = scnprintf(&buf[written], len - written, rc = scnprintf(&buf[written], len - written,
"%lu %lu %lu\n", "%d %lu %lu\n",
t->id, r->ar.start, r->ar.end); target_idx, r->ar.start, r->ar.end);
if (!rc) if (!rc)
return -ENOMEM; return -ENOMEM;
written += rc; written += rc;
} }
target_idx++;
} }
return written; return written;
} }
...@@ -485,22 +487,19 @@ static ssize_t dbgfs_init_regions_read(struct file *file, char __user *buf, ...@@ -485,22 +487,19 @@ static ssize_t dbgfs_init_regions_read(struct file *file, char __user *buf,
return len; return len;
} }
static int add_init_region(struct damon_ctx *c, static int add_init_region(struct damon_ctx *c, int target_idx,
unsigned long target_id, struct damon_addr_range *ar) struct damon_addr_range *ar)
{ {
struct damon_target *t; struct damon_target *t;
struct damon_region *r, *prev; struct damon_region *r, *prev;
unsigned long id; unsigned long idx = 0;
int rc = -EINVAL; int rc = -EINVAL;
if (ar->start >= ar->end) if (ar->start >= ar->end)
return -EINVAL; return -EINVAL;
damon_for_each_target(t, c) { damon_for_each_target(t, c) {
id = t->id; if (idx++ == target_idx) {
if (targetid_is_pid(c))
id = (unsigned long)pid_vnr((struct pid *)id);
if (id == target_id) {
r = damon_new_region(ar->start, ar->end); r = damon_new_region(ar->start, ar->end);
if (!r) if (!r)
return -ENOMEM; return -ENOMEM;
...@@ -523,7 +522,7 @@ static int set_init_regions(struct damon_ctx *c, const char *str, ssize_t len) ...@@ -523,7 +522,7 @@ static int set_init_regions(struct damon_ctx *c, const char *str, ssize_t len)
struct damon_target *t; struct damon_target *t;
struct damon_region *r, *next; struct damon_region *r, *next;
int pos = 0, parsed, ret; int pos = 0, parsed, ret;
unsigned long target_id; int target_idx;
struct damon_addr_range ar; struct damon_addr_range ar;
int err; int err;
...@@ -533,11 +532,11 @@ static int set_init_regions(struct damon_ctx *c, const char *str, ssize_t len) ...@@ -533,11 +532,11 @@ static int set_init_regions(struct damon_ctx *c, const char *str, ssize_t len)
} }
while (pos < len) { while (pos < len) {
ret = sscanf(&str[pos], "%lu %lu %lu%n", ret = sscanf(&str[pos], "%d %lu %lu%n",
&target_id, &ar.start, &ar.end, &parsed); &target_idx, &ar.start, &ar.end, &parsed);
if (ret != 3) if (ret != 3)
break; break;
err = add_init_region(c, target_id, &ar); err = add_init_region(c, target_idx, &ar);
if (err) if (err)
goto fail; goto fail;
pos += parsed; pos += parsed;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment