~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/mm/damon/sysfs.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /mm/damon/sysfs.c (Version linux-6.12-rc7) and /mm/damon/sysfs.c (Version linux-6.10.14)


  1 // SPDX-License-Identifier: GPL-2.0                 1 // SPDX-License-Identifier: GPL-2.0
  2 /*                                                  2 /*
  3  * DAMON sysfs Interface                            3  * DAMON sysfs Interface
  4  *                                                  4  *
  5  * Copyright (c) 2022 SeongJae Park <sj@kernel      5  * Copyright (c) 2022 SeongJae Park <sj@kernel.org>
  6  */                                                 6  */
  7                                                     7 
  8 #include <linux/pid.h>                              8 #include <linux/pid.h>
  9 #include <linux/sched.h>                            9 #include <linux/sched.h>
 10 #include <linux/slab.h>                            10 #include <linux/slab.h>
 11                                                    11 
 12 #include "sysfs-common.h"                          12 #include "sysfs-common.h"
 13                                                    13 
 14 /*                                                 14 /*
 15  * init region directory                           15  * init region directory
 16  */                                                16  */
 17                                                    17 
 18 struct damon_sysfs_region {                        18 struct damon_sysfs_region {
 19         struct kobject kobj;                       19         struct kobject kobj;
 20         struct damon_addr_range ar;                20         struct damon_addr_range ar;
 21 };                                                 21 };
 22                                                    22 
 23 static struct damon_sysfs_region *damon_sysfs_     23 static struct damon_sysfs_region *damon_sysfs_region_alloc(void)
 24 {                                                  24 {
 25         return kzalloc(sizeof(struct damon_sys     25         return kzalloc(sizeof(struct damon_sysfs_region), GFP_KERNEL);
 26 }                                                  26 }
 27                                                    27 
 28 static ssize_t start_show(struct kobject *kobj     28 static ssize_t start_show(struct kobject *kobj, struct kobj_attribute *attr,
 29                 char *buf)                         29                 char *buf)
 30 {                                                  30 {
 31         struct damon_sysfs_region *region = co     31         struct damon_sysfs_region *region = container_of(kobj,
 32                         struct damon_sysfs_reg     32                         struct damon_sysfs_region, kobj);
 33                                                    33 
 34         return sysfs_emit(buf, "%lu\n", region     34         return sysfs_emit(buf, "%lu\n", region->ar.start);
 35 }                                                  35 }
 36                                                    36 
 37 static ssize_t start_store(struct kobject *kob     37 static ssize_t start_store(struct kobject *kobj, struct kobj_attribute *attr,
 38                 const char *buf, size_t count)     38                 const char *buf, size_t count)
 39 {                                                  39 {
 40         struct damon_sysfs_region *region = co     40         struct damon_sysfs_region *region = container_of(kobj,
 41                         struct damon_sysfs_reg     41                         struct damon_sysfs_region, kobj);
 42         int err = kstrtoul(buf, 0, &region->ar     42         int err = kstrtoul(buf, 0, &region->ar.start);
 43                                                    43 
 44         return err ? err : count;                  44         return err ? err : count;
 45 }                                                  45 }
 46                                                    46 
 47 static ssize_t end_show(struct kobject *kobj,      47 static ssize_t end_show(struct kobject *kobj, struct kobj_attribute *attr,
 48                 char *buf)                         48                 char *buf)
 49 {                                                  49 {
 50         struct damon_sysfs_region *region = co     50         struct damon_sysfs_region *region = container_of(kobj,
 51                         struct damon_sysfs_reg     51                         struct damon_sysfs_region, kobj);
 52                                                    52 
 53         return sysfs_emit(buf, "%lu\n", region     53         return sysfs_emit(buf, "%lu\n", region->ar.end);
 54 }                                                  54 }
 55                                                    55 
 56 static ssize_t end_store(struct kobject *kobj,     56 static ssize_t end_store(struct kobject *kobj, struct kobj_attribute *attr,
 57                 const char *buf, size_t count)     57                 const char *buf, size_t count)
 58 {                                                  58 {
 59         struct damon_sysfs_region *region = co     59         struct damon_sysfs_region *region = container_of(kobj,
 60                         struct damon_sysfs_reg     60                         struct damon_sysfs_region, kobj);
 61         int err = kstrtoul(buf, 0, &region->ar     61         int err = kstrtoul(buf, 0, &region->ar.end);
 62                                                    62 
 63         return err ? err : count;                  63         return err ? err : count;
 64 }                                                  64 }
 65                                                    65 
 66 static void damon_sysfs_region_release(struct      66 static void damon_sysfs_region_release(struct kobject *kobj)
 67 {                                                  67 {
 68         kfree(container_of(kobj, struct damon_     68         kfree(container_of(kobj, struct damon_sysfs_region, kobj));
 69 }                                                  69 }
 70                                                    70 
 71 static struct kobj_attribute damon_sysfs_regio     71 static struct kobj_attribute damon_sysfs_region_start_attr =
 72                 __ATTR_RW_MODE(start, 0600);       72                 __ATTR_RW_MODE(start, 0600);
 73                                                    73 
 74 static struct kobj_attribute damon_sysfs_regio     74 static struct kobj_attribute damon_sysfs_region_end_attr =
 75                 __ATTR_RW_MODE(end, 0600);         75                 __ATTR_RW_MODE(end, 0600);
 76                                                    76 
 77 static struct attribute *damon_sysfs_region_at     77 static struct attribute *damon_sysfs_region_attrs[] = {
 78         &damon_sysfs_region_start_attr.attr,       78         &damon_sysfs_region_start_attr.attr,
 79         &damon_sysfs_region_end_attr.attr,         79         &damon_sysfs_region_end_attr.attr,
 80         NULL,                                      80         NULL,
 81 };                                                 81 };
 82 ATTRIBUTE_GROUPS(damon_sysfs_region);              82 ATTRIBUTE_GROUPS(damon_sysfs_region);
 83                                                    83 
 84 static const struct kobj_type damon_sysfs_regi     84 static const struct kobj_type damon_sysfs_region_ktype = {
 85         .release = damon_sysfs_region_release,     85         .release = damon_sysfs_region_release,
 86         .sysfs_ops = &kobj_sysfs_ops,              86         .sysfs_ops = &kobj_sysfs_ops,
 87         .default_groups = damon_sysfs_region_g     87         .default_groups = damon_sysfs_region_groups,
 88 };                                                 88 };
 89                                                    89 
 90 /*                                                 90 /*
 91  * init_regions directory                          91  * init_regions directory
 92  */                                                92  */
 93                                                    93 
 94 struct damon_sysfs_regions {                       94 struct damon_sysfs_regions {
 95         struct kobject kobj;                       95         struct kobject kobj;
 96         struct damon_sysfs_region **regions_ar     96         struct damon_sysfs_region **regions_arr;
 97         int nr;                                    97         int nr;
 98 };                                                 98 };
 99                                                    99 
100 static struct damon_sysfs_regions *damon_sysfs    100 static struct damon_sysfs_regions *damon_sysfs_regions_alloc(void)
101 {                                                 101 {
102         return kzalloc(sizeof(struct damon_sys    102         return kzalloc(sizeof(struct damon_sysfs_regions), GFP_KERNEL);
103 }                                                 103 }
104                                                   104 
105 static void damon_sysfs_regions_rm_dirs(struct    105 static void damon_sysfs_regions_rm_dirs(struct damon_sysfs_regions *regions)
106 {                                                 106 {
107         struct damon_sysfs_region **regions_ar    107         struct damon_sysfs_region **regions_arr = regions->regions_arr;
108         int i;                                    108         int i;
109                                                   109 
110         for (i = 0; i < regions->nr; i++)         110         for (i = 0; i < regions->nr; i++)
111                 kobject_put(&regions_arr[i]->k    111                 kobject_put(&regions_arr[i]->kobj);
112         regions->nr = 0;                          112         regions->nr = 0;
113         kfree(regions_arr);                       113         kfree(regions_arr);
114         regions->regions_arr = NULL;              114         regions->regions_arr = NULL;
115 }                                                 115 }
116                                                   116 
117 static int damon_sysfs_regions_add_dirs(struct    117 static int damon_sysfs_regions_add_dirs(struct damon_sysfs_regions *regions,
118                 int nr_regions)                   118                 int nr_regions)
119 {                                                 119 {
120         struct damon_sysfs_region **regions_ar    120         struct damon_sysfs_region **regions_arr, *region;
121         int err, i;                               121         int err, i;
122                                                   122 
123         damon_sysfs_regions_rm_dirs(regions);     123         damon_sysfs_regions_rm_dirs(regions);
124         if (!nr_regions)                          124         if (!nr_regions)
125                 return 0;                         125                 return 0;
126                                                   126 
127         regions_arr = kmalloc_array(nr_regions    127         regions_arr = kmalloc_array(nr_regions, sizeof(*regions_arr),
128                         GFP_KERNEL | __GFP_NOW    128                         GFP_KERNEL | __GFP_NOWARN);
129         if (!regions_arr)                         129         if (!regions_arr)
130                 return -ENOMEM;                   130                 return -ENOMEM;
131         regions->regions_arr = regions_arr;       131         regions->regions_arr = regions_arr;
132                                                   132 
133         for (i = 0; i < nr_regions; i++) {        133         for (i = 0; i < nr_regions; i++) {
134                 region = damon_sysfs_region_al    134                 region = damon_sysfs_region_alloc();
135                 if (!region) {                    135                 if (!region) {
136                         damon_sysfs_regions_rm    136                         damon_sysfs_regions_rm_dirs(regions);
137                         return -ENOMEM;           137                         return -ENOMEM;
138                 }                                 138                 }
139                                                   139 
140                 err = kobject_init_and_add(&re    140                 err = kobject_init_and_add(&region->kobj,
141                                 &damon_sysfs_r    141                                 &damon_sysfs_region_ktype, &regions->kobj,
142                                 "%d", i);         142                                 "%d", i);
143                 if (err) {                        143                 if (err) {
144                         kobject_put(&region->k    144                         kobject_put(&region->kobj);
145                         damon_sysfs_regions_rm    145                         damon_sysfs_regions_rm_dirs(regions);
146                         return err;               146                         return err;
147                 }                                 147                 }
148                                                   148 
149                 regions_arr[i] = region;          149                 regions_arr[i] = region;
150                 regions->nr++;                    150                 regions->nr++;
151         }                                         151         }
152         return 0;                                 152         return 0;
153 }                                                 153 }
154                                                   154 
155 static ssize_t nr_regions_show(struct kobject     155 static ssize_t nr_regions_show(struct kobject *kobj,
156                 struct kobj_attribute *attr, c    156                 struct kobj_attribute *attr, char *buf)
157 {                                                 157 {
158         struct damon_sysfs_regions *regions =     158         struct damon_sysfs_regions *regions = container_of(kobj,
159                         struct damon_sysfs_reg    159                         struct damon_sysfs_regions, kobj);
160                                                   160 
161         return sysfs_emit(buf, "%d\n", regions    161         return sysfs_emit(buf, "%d\n", regions->nr);
162 }                                                 162 }
163                                                   163 
164 static ssize_t nr_regions_store(struct kobject    164 static ssize_t nr_regions_store(struct kobject *kobj,
165                 struct kobj_attribute *attr, c    165                 struct kobj_attribute *attr, const char *buf, size_t count)
166 {                                                 166 {
167         struct damon_sysfs_regions *regions;      167         struct damon_sysfs_regions *regions;
168         int nr, err = kstrtoint(buf, 0, &nr);     168         int nr, err = kstrtoint(buf, 0, &nr);
169                                                   169 
170         if (err)                                  170         if (err)
171                 return err;                       171                 return err;
172         if (nr < 0)                               172         if (nr < 0)
173                 return -EINVAL;                   173                 return -EINVAL;
174                                                   174 
175         regions = container_of(kobj, struct da    175         regions = container_of(kobj, struct damon_sysfs_regions, kobj);
176                                                   176 
177         if (!mutex_trylock(&damon_sysfs_lock))    177         if (!mutex_trylock(&damon_sysfs_lock))
178                 return -EBUSY;                    178                 return -EBUSY;
179         err = damon_sysfs_regions_add_dirs(reg    179         err = damon_sysfs_regions_add_dirs(regions, nr);
180         mutex_unlock(&damon_sysfs_lock);          180         mutex_unlock(&damon_sysfs_lock);
181         if (err)                                  181         if (err)
182                 return err;                       182                 return err;
183                                                   183 
184         return count;                             184         return count;
185 }                                                 185 }
186                                                   186 
187 static void damon_sysfs_regions_release(struct    187 static void damon_sysfs_regions_release(struct kobject *kobj)
188 {                                                 188 {
189         kfree(container_of(kobj, struct damon_    189         kfree(container_of(kobj, struct damon_sysfs_regions, kobj));
190 }                                                 190 }
191                                                   191 
192 static struct kobj_attribute damon_sysfs_regio    192 static struct kobj_attribute damon_sysfs_regions_nr_attr =
193                 __ATTR_RW_MODE(nr_regions, 060    193                 __ATTR_RW_MODE(nr_regions, 0600);
194                                                   194 
195 static struct attribute *damon_sysfs_regions_a    195 static struct attribute *damon_sysfs_regions_attrs[] = {
196         &damon_sysfs_regions_nr_attr.attr,        196         &damon_sysfs_regions_nr_attr.attr,
197         NULL,                                     197         NULL,
198 };                                                198 };
199 ATTRIBUTE_GROUPS(damon_sysfs_regions);            199 ATTRIBUTE_GROUPS(damon_sysfs_regions);
200                                                   200 
201 static const struct kobj_type damon_sysfs_regi    201 static const struct kobj_type damon_sysfs_regions_ktype = {
202         .release = damon_sysfs_regions_release    202         .release = damon_sysfs_regions_release,
203         .sysfs_ops = &kobj_sysfs_ops,             203         .sysfs_ops = &kobj_sysfs_ops,
204         .default_groups = damon_sysfs_regions_    204         .default_groups = damon_sysfs_regions_groups,
205 };                                                205 };
206                                                   206 
207 /*                                                207 /*
208  * target directory                               208  * target directory
209  */                                               209  */
210                                                   210 
211 struct damon_sysfs_target {                       211 struct damon_sysfs_target {
212         struct kobject kobj;                      212         struct kobject kobj;
213         struct damon_sysfs_regions *regions;      213         struct damon_sysfs_regions *regions;
214         int pid;                                  214         int pid;
215 };                                                215 };
216                                                   216 
217 static struct damon_sysfs_target *damon_sysfs_    217 static struct damon_sysfs_target *damon_sysfs_target_alloc(void)
218 {                                                 218 {
219         return kzalloc(sizeof(struct damon_sys    219         return kzalloc(sizeof(struct damon_sysfs_target), GFP_KERNEL);
220 }                                                 220 }
221                                                   221 
222 static int damon_sysfs_target_add_dirs(struct     222 static int damon_sysfs_target_add_dirs(struct damon_sysfs_target *target)
223 {                                                 223 {
224         struct damon_sysfs_regions *regions =     224         struct damon_sysfs_regions *regions = damon_sysfs_regions_alloc();
225         int err;                                  225         int err;
226                                                   226 
227         if (!regions)                             227         if (!regions)
228                 return -ENOMEM;                   228                 return -ENOMEM;
229                                                   229 
230         err = kobject_init_and_add(&regions->k    230         err = kobject_init_and_add(&regions->kobj, &damon_sysfs_regions_ktype,
231                         &target->kobj, "region    231                         &target->kobj, "regions");
232         if (err)                                  232         if (err)
233                 kobject_put(&regions->kobj);      233                 kobject_put(&regions->kobj);
234         else                                      234         else
235                 target->regions = regions;        235                 target->regions = regions;
236         return err;                               236         return err;
237 }                                                 237 }
238                                                   238 
239 static void damon_sysfs_target_rm_dirs(struct     239 static void damon_sysfs_target_rm_dirs(struct damon_sysfs_target *target)
240 {                                                 240 {
241         damon_sysfs_regions_rm_dirs(target->re    241         damon_sysfs_regions_rm_dirs(target->regions);
242         kobject_put(&target->regions->kobj);      242         kobject_put(&target->regions->kobj);
243 }                                                 243 }
244                                                   244 
245 static ssize_t pid_target_show(struct kobject     245 static ssize_t pid_target_show(struct kobject *kobj,
246                 struct kobj_attribute *attr, c    246                 struct kobj_attribute *attr, char *buf)
247 {                                                 247 {
248         struct damon_sysfs_target *target = co    248         struct damon_sysfs_target *target = container_of(kobj,
249                         struct damon_sysfs_tar    249                         struct damon_sysfs_target, kobj);
250                                                   250 
251         return sysfs_emit(buf, "%d\n", target-    251         return sysfs_emit(buf, "%d\n", target->pid);
252 }                                                 252 }
253                                                   253 
254 static ssize_t pid_target_store(struct kobject    254 static ssize_t pid_target_store(struct kobject *kobj,
255                 struct kobj_attribute *attr, c    255                 struct kobj_attribute *attr, const char *buf, size_t count)
256 {                                                 256 {
257         struct damon_sysfs_target *target = co    257         struct damon_sysfs_target *target = container_of(kobj,
258                         struct damon_sysfs_tar    258                         struct damon_sysfs_target, kobj);
259         int err = kstrtoint(buf, 0, &target->p    259         int err = kstrtoint(buf, 0, &target->pid);
260                                                   260 
261         if (err)                                  261         if (err)
262                 return -EINVAL;                   262                 return -EINVAL;
263         return count;                             263         return count;
264 }                                                 264 }
265                                                   265 
266 static void damon_sysfs_target_release(struct     266 static void damon_sysfs_target_release(struct kobject *kobj)
267 {                                                 267 {
268         kfree(container_of(kobj, struct damon_    268         kfree(container_of(kobj, struct damon_sysfs_target, kobj));
269 }                                                 269 }
270                                                   270 
271 static struct kobj_attribute damon_sysfs_targe    271 static struct kobj_attribute damon_sysfs_target_pid_attr =
272                 __ATTR_RW_MODE(pid_target, 060    272                 __ATTR_RW_MODE(pid_target, 0600);
273                                                   273 
274 static struct attribute *damon_sysfs_target_at    274 static struct attribute *damon_sysfs_target_attrs[] = {
275         &damon_sysfs_target_pid_attr.attr,        275         &damon_sysfs_target_pid_attr.attr,
276         NULL,                                     276         NULL,
277 };                                                277 };
278 ATTRIBUTE_GROUPS(damon_sysfs_target);             278 ATTRIBUTE_GROUPS(damon_sysfs_target);
279                                                   279 
280 static const struct kobj_type damon_sysfs_targ    280 static const struct kobj_type damon_sysfs_target_ktype = {
281         .release = damon_sysfs_target_release,    281         .release = damon_sysfs_target_release,
282         .sysfs_ops = &kobj_sysfs_ops,             282         .sysfs_ops = &kobj_sysfs_ops,
283         .default_groups = damon_sysfs_target_g    283         .default_groups = damon_sysfs_target_groups,
284 };                                                284 };
285                                                   285 
286 /*                                                286 /*
287  * targets directory                              287  * targets directory
288  */                                               288  */
289                                                   289 
290 struct damon_sysfs_targets {                      290 struct damon_sysfs_targets {
291         struct kobject kobj;                      291         struct kobject kobj;
292         struct damon_sysfs_target **targets_ar    292         struct damon_sysfs_target **targets_arr;
293         int nr;                                   293         int nr;
294 };                                                294 };
295                                                   295 
296 static struct damon_sysfs_targets *damon_sysfs    296 static struct damon_sysfs_targets *damon_sysfs_targets_alloc(void)
297 {                                                 297 {
298         return kzalloc(sizeof(struct damon_sys    298         return kzalloc(sizeof(struct damon_sysfs_targets), GFP_KERNEL);
299 }                                                 299 }
300                                                   300 
301 static void damon_sysfs_targets_rm_dirs(struct    301 static void damon_sysfs_targets_rm_dirs(struct damon_sysfs_targets *targets)
302 {                                                 302 {
303         struct damon_sysfs_target **targets_ar    303         struct damon_sysfs_target **targets_arr = targets->targets_arr;
304         int i;                                    304         int i;
305                                                   305 
306         for (i = 0; i < targets->nr; i++) {       306         for (i = 0; i < targets->nr; i++) {
307                 damon_sysfs_target_rm_dirs(tar    307                 damon_sysfs_target_rm_dirs(targets_arr[i]);
308                 kobject_put(&targets_arr[i]->k    308                 kobject_put(&targets_arr[i]->kobj);
309         }                                         309         }
310         targets->nr = 0;                          310         targets->nr = 0;
311         kfree(targets_arr);                       311         kfree(targets_arr);
312         targets->targets_arr = NULL;              312         targets->targets_arr = NULL;
313 }                                                 313 }
314                                                   314 
315 static int damon_sysfs_targets_add_dirs(struct    315 static int damon_sysfs_targets_add_dirs(struct damon_sysfs_targets *targets,
316                 int nr_targets)                   316                 int nr_targets)
317 {                                                 317 {
318         struct damon_sysfs_target **targets_ar    318         struct damon_sysfs_target **targets_arr, *target;
319         int err, i;                               319         int err, i;
320                                                   320 
321         damon_sysfs_targets_rm_dirs(targets);     321         damon_sysfs_targets_rm_dirs(targets);
322         if (!nr_targets)                          322         if (!nr_targets)
323                 return 0;                         323                 return 0;
324                                                   324 
325         targets_arr = kmalloc_array(nr_targets    325         targets_arr = kmalloc_array(nr_targets, sizeof(*targets_arr),
326                         GFP_KERNEL | __GFP_NOW    326                         GFP_KERNEL | __GFP_NOWARN);
327         if (!targets_arr)                         327         if (!targets_arr)
328                 return -ENOMEM;                   328                 return -ENOMEM;
329         targets->targets_arr = targets_arr;       329         targets->targets_arr = targets_arr;
330                                                   330 
331         for (i = 0; i < nr_targets; i++) {        331         for (i = 0; i < nr_targets; i++) {
332                 target = damon_sysfs_target_al    332                 target = damon_sysfs_target_alloc();
333                 if (!target) {                    333                 if (!target) {
334                         damon_sysfs_targets_rm    334                         damon_sysfs_targets_rm_dirs(targets);
335                         return -ENOMEM;           335                         return -ENOMEM;
336                 }                                 336                 }
337                                                   337 
338                 err = kobject_init_and_add(&ta    338                 err = kobject_init_and_add(&target->kobj,
339                                 &damon_sysfs_t    339                                 &damon_sysfs_target_ktype, &targets->kobj,
340                                 "%d", i);         340                                 "%d", i);
341                 if (err)                          341                 if (err)
342                         goto out;                 342                         goto out;
343                                                   343 
344                 err = damon_sysfs_target_add_d    344                 err = damon_sysfs_target_add_dirs(target);
345                 if (err)                          345                 if (err)
346                         goto out;                 346                         goto out;
347                                                   347 
348                 targets_arr[i] = target;          348                 targets_arr[i] = target;
349                 targets->nr++;                    349                 targets->nr++;
350         }                                         350         }
351         return 0;                                 351         return 0;
352                                                   352 
353 out:                                              353 out:
354         damon_sysfs_targets_rm_dirs(targets);     354         damon_sysfs_targets_rm_dirs(targets);
355         kobject_put(&target->kobj);               355         kobject_put(&target->kobj);
356         return err;                               356         return err;
357 }                                                 357 }
358                                                   358 
359 static ssize_t nr_targets_show(struct kobject     359 static ssize_t nr_targets_show(struct kobject *kobj,
360                 struct kobj_attribute *attr, c    360                 struct kobj_attribute *attr, char *buf)
361 {                                                 361 {
362         struct damon_sysfs_targets *targets =     362         struct damon_sysfs_targets *targets = container_of(kobj,
363                         struct damon_sysfs_tar    363                         struct damon_sysfs_targets, kobj);
364                                                   364 
365         return sysfs_emit(buf, "%d\n", targets    365         return sysfs_emit(buf, "%d\n", targets->nr);
366 }                                                 366 }
367                                                   367 
368 static ssize_t nr_targets_store(struct kobject    368 static ssize_t nr_targets_store(struct kobject *kobj,
369                 struct kobj_attribute *attr, c    369                 struct kobj_attribute *attr, const char *buf, size_t count)
370 {                                                 370 {
371         struct damon_sysfs_targets *targets;      371         struct damon_sysfs_targets *targets;
372         int nr, err = kstrtoint(buf, 0, &nr);     372         int nr, err = kstrtoint(buf, 0, &nr);
373                                                   373 
374         if (err)                                  374         if (err)
375                 return err;                       375                 return err;
376         if (nr < 0)                               376         if (nr < 0)
377                 return -EINVAL;                   377                 return -EINVAL;
378                                                   378 
379         targets = container_of(kobj, struct da    379         targets = container_of(kobj, struct damon_sysfs_targets, kobj);
380                                                   380 
381         if (!mutex_trylock(&damon_sysfs_lock))    381         if (!mutex_trylock(&damon_sysfs_lock))
382                 return -EBUSY;                    382                 return -EBUSY;
383         err = damon_sysfs_targets_add_dirs(tar    383         err = damon_sysfs_targets_add_dirs(targets, nr);
384         mutex_unlock(&damon_sysfs_lock);          384         mutex_unlock(&damon_sysfs_lock);
385         if (err)                                  385         if (err)
386                 return err;                       386                 return err;
387                                                   387 
388         return count;                             388         return count;
389 }                                                 389 }
390                                                   390 
391 static void damon_sysfs_targets_release(struct    391 static void damon_sysfs_targets_release(struct kobject *kobj)
392 {                                                 392 {
393         kfree(container_of(kobj, struct damon_    393         kfree(container_of(kobj, struct damon_sysfs_targets, kobj));
394 }                                                 394 }
395                                                   395 
396 static struct kobj_attribute damon_sysfs_targe    396 static struct kobj_attribute damon_sysfs_targets_nr_attr =
397                 __ATTR_RW_MODE(nr_targets, 060    397                 __ATTR_RW_MODE(nr_targets, 0600);
398                                                   398 
399 static struct attribute *damon_sysfs_targets_a    399 static struct attribute *damon_sysfs_targets_attrs[] = {
400         &damon_sysfs_targets_nr_attr.attr,        400         &damon_sysfs_targets_nr_attr.attr,
401         NULL,                                     401         NULL,
402 };                                                402 };
403 ATTRIBUTE_GROUPS(damon_sysfs_targets);            403 ATTRIBUTE_GROUPS(damon_sysfs_targets);
404                                                   404 
405 static const struct kobj_type damon_sysfs_targ    405 static const struct kobj_type damon_sysfs_targets_ktype = {
406         .release = damon_sysfs_targets_release    406         .release = damon_sysfs_targets_release,
407         .sysfs_ops = &kobj_sysfs_ops,             407         .sysfs_ops = &kobj_sysfs_ops,
408         .default_groups = damon_sysfs_targets_    408         .default_groups = damon_sysfs_targets_groups,
409 };                                                409 };
410                                                   410 
411 /*                                                411 /*
412  * intervals directory                            412  * intervals directory
413  */                                               413  */
414                                                   414 
415 struct damon_sysfs_intervals {                    415 struct damon_sysfs_intervals {
416         struct kobject kobj;                      416         struct kobject kobj;
417         unsigned long sample_us;                  417         unsigned long sample_us;
418         unsigned long aggr_us;                    418         unsigned long aggr_us;
419         unsigned long update_us;                  419         unsigned long update_us;
420 };                                                420 };
421                                                   421 
422 static struct damon_sysfs_intervals *damon_sys    422 static struct damon_sysfs_intervals *damon_sysfs_intervals_alloc(
423                 unsigned long sample_us, unsig    423                 unsigned long sample_us, unsigned long aggr_us,
424                 unsigned long update_us)          424                 unsigned long update_us)
425 {                                                 425 {
426         struct damon_sysfs_intervals *interval    426         struct damon_sysfs_intervals *intervals = kmalloc(sizeof(*intervals),
427                         GFP_KERNEL);              427                         GFP_KERNEL);
428                                                   428 
429         if (!intervals)                           429         if (!intervals)
430                 return NULL;                      430                 return NULL;
431                                                   431 
432         intervals->kobj = (struct kobject){};     432         intervals->kobj = (struct kobject){};
433         intervals->sample_us = sample_us;         433         intervals->sample_us = sample_us;
434         intervals->aggr_us = aggr_us;             434         intervals->aggr_us = aggr_us;
435         intervals->update_us = update_us;         435         intervals->update_us = update_us;
436         return intervals;                         436         return intervals;
437 }                                                 437 }
438                                                   438 
439 static ssize_t sample_us_show(struct kobject *    439 static ssize_t sample_us_show(struct kobject *kobj,
440                 struct kobj_attribute *attr, c    440                 struct kobj_attribute *attr, char *buf)
441 {                                                 441 {
442         struct damon_sysfs_intervals *interval    442         struct damon_sysfs_intervals *intervals = container_of(kobj,
443                         struct damon_sysfs_int    443                         struct damon_sysfs_intervals, kobj);
444                                                   444 
445         return sysfs_emit(buf, "%lu\n", interv    445         return sysfs_emit(buf, "%lu\n", intervals->sample_us);
446 }                                                 446 }
447                                                   447 
448 static ssize_t sample_us_store(struct kobject     448 static ssize_t sample_us_store(struct kobject *kobj,
449                 struct kobj_attribute *attr, c    449                 struct kobj_attribute *attr, const char *buf, size_t count)
450 {                                                 450 {
451         struct damon_sysfs_intervals *interval    451         struct damon_sysfs_intervals *intervals = container_of(kobj,
452                         struct damon_sysfs_int    452                         struct damon_sysfs_intervals, kobj);
453         unsigned long us;                         453         unsigned long us;
454         int err = kstrtoul(buf, 0, &us);          454         int err = kstrtoul(buf, 0, &us);
455                                                   455 
456         if (err)                                  456         if (err)
457                 return err;                       457                 return err;
458                                                   458 
459         intervals->sample_us = us;                459         intervals->sample_us = us;
460         return count;                             460         return count;
461 }                                                 461 }
462                                                   462 
463 static ssize_t aggr_us_show(struct kobject *ko    463 static ssize_t aggr_us_show(struct kobject *kobj, struct kobj_attribute *attr,
464                 char *buf)                        464                 char *buf)
465 {                                                 465 {
466         struct damon_sysfs_intervals *interval    466         struct damon_sysfs_intervals *intervals = container_of(kobj,
467                         struct damon_sysfs_int    467                         struct damon_sysfs_intervals, kobj);
468                                                   468 
469         return sysfs_emit(buf, "%lu\n", interv    469         return sysfs_emit(buf, "%lu\n", intervals->aggr_us);
470 }                                                 470 }
471                                                   471 
472 static ssize_t aggr_us_store(struct kobject *k    472 static ssize_t aggr_us_store(struct kobject *kobj, struct kobj_attribute *attr,
473                 const char *buf, size_t count)    473                 const char *buf, size_t count)
474 {                                                 474 {
475         struct damon_sysfs_intervals *interval    475         struct damon_sysfs_intervals *intervals = container_of(kobj,
476                         struct damon_sysfs_int    476                         struct damon_sysfs_intervals, kobj);
477         unsigned long us;                         477         unsigned long us;
478         int err = kstrtoul(buf, 0, &us);          478         int err = kstrtoul(buf, 0, &us);
479                                                   479 
480         if (err)                                  480         if (err)
481                 return err;                       481                 return err;
482                                                   482 
483         intervals->aggr_us = us;                  483         intervals->aggr_us = us;
484         return count;                             484         return count;
485 }                                                 485 }
486                                                   486 
487 static ssize_t update_us_show(struct kobject *    487 static ssize_t update_us_show(struct kobject *kobj,
488                 struct kobj_attribute *attr, c    488                 struct kobj_attribute *attr, char *buf)
489 {                                                 489 {
490         struct damon_sysfs_intervals *interval    490         struct damon_sysfs_intervals *intervals = container_of(kobj,
491                         struct damon_sysfs_int    491                         struct damon_sysfs_intervals, kobj);
492                                                   492 
493         return sysfs_emit(buf, "%lu\n", interv    493         return sysfs_emit(buf, "%lu\n", intervals->update_us);
494 }                                                 494 }
495                                                   495 
496 static ssize_t update_us_store(struct kobject     496 static ssize_t update_us_store(struct kobject *kobj,
497                 struct kobj_attribute *attr, c    497                 struct kobj_attribute *attr, const char *buf, size_t count)
498 {                                                 498 {
499         struct damon_sysfs_intervals *interval    499         struct damon_sysfs_intervals *intervals = container_of(kobj,
500                         struct damon_sysfs_int    500                         struct damon_sysfs_intervals, kobj);
501         unsigned long us;                         501         unsigned long us;
502         int err = kstrtoul(buf, 0, &us);          502         int err = kstrtoul(buf, 0, &us);
503                                                   503 
504         if (err)                                  504         if (err)
505                 return err;                       505                 return err;
506                                                   506 
507         intervals->update_us = us;                507         intervals->update_us = us;
508         return count;                             508         return count;
509 }                                                 509 }
510                                                   510 
511 static void damon_sysfs_intervals_release(stru    511 static void damon_sysfs_intervals_release(struct kobject *kobj)
512 {                                                 512 {
513         kfree(container_of(kobj, struct damon_    513         kfree(container_of(kobj, struct damon_sysfs_intervals, kobj));
514 }                                                 514 }
515                                                   515 
516 static struct kobj_attribute damon_sysfs_inter    516 static struct kobj_attribute damon_sysfs_intervals_sample_us_attr =
517                 __ATTR_RW_MODE(sample_us, 0600    517                 __ATTR_RW_MODE(sample_us, 0600);
518                                                   518 
519 static struct kobj_attribute damon_sysfs_inter    519 static struct kobj_attribute damon_sysfs_intervals_aggr_us_attr =
520                 __ATTR_RW_MODE(aggr_us, 0600);    520                 __ATTR_RW_MODE(aggr_us, 0600);
521                                                   521 
522 static struct kobj_attribute damon_sysfs_inter    522 static struct kobj_attribute damon_sysfs_intervals_update_us_attr =
523                 __ATTR_RW_MODE(update_us, 0600    523                 __ATTR_RW_MODE(update_us, 0600);
524                                                   524 
525 static struct attribute *damon_sysfs_intervals    525 static struct attribute *damon_sysfs_intervals_attrs[] = {
526         &damon_sysfs_intervals_sample_us_attr.    526         &damon_sysfs_intervals_sample_us_attr.attr,
527         &damon_sysfs_intervals_aggr_us_attr.at    527         &damon_sysfs_intervals_aggr_us_attr.attr,
528         &damon_sysfs_intervals_update_us_attr.    528         &damon_sysfs_intervals_update_us_attr.attr,
529         NULL,                                     529         NULL,
530 };                                                530 };
531 ATTRIBUTE_GROUPS(damon_sysfs_intervals);          531 ATTRIBUTE_GROUPS(damon_sysfs_intervals);
532                                                   532 
533 static const struct kobj_type damon_sysfs_inte    533 static const struct kobj_type damon_sysfs_intervals_ktype = {
534         .release = damon_sysfs_intervals_relea    534         .release = damon_sysfs_intervals_release,
535         .sysfs_ops = &kobj_sysfs_ops,             535         .sysfs_ops = &kobj_sysfs_ops,
536         .default_groups = damon_sysfs_interval    536         .default_groups = damon_sysfs_intervals_groups,
537 };                                                537 };
538                                                   538 
539 /*                                                539 /*
540  * monitoring_attrs directory                     540  * monitoring_attrs directory
541  */                                               541  */
542                                                   542 
543 struct damon_sysfs_attrs {                        543 struct damon_sysfs_attrs {
544         struct kobject kobj;                      544         struct kobject kobj;
545         struct damon_sysfs_intervals *interval    545         struct damon_sysfs_intervals *intervals;
546         struct damon_sysfs_ul_range *nr_region    546         struct damon_sysfs_ul_range *nr_regions_range;
547 };                                                547 };
548                                                   548 
549 static struct damon_sysfs_attrs *damon_sysfs_a    549 static struct damon_sysfs_attrs *damon_sysfs_attrs_alloc(void)
550 {                                                 550 {
551         struct damon_sysfs_attrs *attrs = kmal    551         struct damon_sysfs_attrs *attrs = kmalloc(sizeof(*attrs), GFP_KERNEL);
552                                                   552 
553         if (!attrs)                               553         if (!attrs)
554                 return NULL;                      554                 return NULL;
555         attrs->kobj = (struct kobject){};         555         attrs->kobj = (struct kobject){};
556         return attrs;                             556         return attrs;
557 }                                                 557 }
558                                                   558 
559 static int damon_sysfs_attrs_add_dirs(struct d    559 static int damon_sysfs_attrs_add_dirs(struct damon_sysfs_attrs *attrs)
560 {                                                 560 {
561         struct damon_sysfs_intervals *interval    561         struct damon_sysfs_intervals *intervals;
562         struct damon_sysfs_ul_range *nr_region    562         struct damon_sysfs_ul_range *nr_regions_range;
563         int err;                                  563         int err;
564                                                   564 
565         intervals = damon_sysfs_intervals_allo    565         intervals = damon_sysfs_intervals_alloc(5000, 100000, 60000000);
566         if (!intervals)                           566         if (!intervals)
567                 return -ENOMEM;                   567                 return -ENOMEM;
568                                                   568 
569         err = kobject_init_and_add(&intervals-    569         err = kobject_init_and_add(&intervals->kobj,
570                         &damon_sysfs_intervals    570                         &damon_sysfs_intervals_ktype, &attrs->kobj,
571                         "intervals");             571                         "intervals");
572         if (err)                                  572         if (err)
573                 goto put_intervals_out;           573                 goto put_intervals_out;
574         attrs->intervals = intervals;             574         attrs->intervals = intervals;
575                                                   575 
576         nr_regions_range = damon_sysfs_ul_rang    576         nr_regions_range = damon_sysfs_ul_range_alloc(10, 1000);
577         if (!nr_regions_range) {                  577         if (!nr_regions_range) {
578                 err = -ENOMEM;                    578                 err = -ENOMEM;
579                 goto put_intervals_out;           579                 goto put_intervals_out;
580         }                                         580         }
581                                                   581 
582         err = kobject_init_and_add(&nr_regions    582         err = kobject_init_and_add(&nr_regions_range->kobj,
583                         &damon_sysfs_ul_range_    583                         &damon_sysfs_ul_range_ktype, &attrs->kobj,
584                         "nr_regions");            584                         "nr_regions");
585         if (err)                                  585         if (err)
586                 goto put_nr_regions_intervals_    586                 goto put_nr_regions_intervals_out;
587         attrs->nr_regions_range = nr_regions_r    587         attrs->nr_regions_range = nr_regions_range;
588         return 0;                                 588         return 0;
589                                                   589 
590 put_nr_regions_intervals_out:                     590 put_nr_regions_intervals_out:
591         kobject_put(&nr_regions_range->kobj);     591         kobject_put(&nr_regions_range->kobj);
592         attrs->nr_regions_range = NULL;           592         attrs->nr_regions_range = NULL;
593 put_intervals_out:                                593 put_intervals_out:
594         kobject_put(&intervals->kobj);            594         kobject_put(&intervals->kobj);
595         attrs->intervals = NULL;                  595         attrs->intervals = NULL;
596         return err;                               596         return err;
597 }                                                 597 }
598                                                   598 
599 static void damon_sysfs_attrs_rm_dirs(struct d    599 static void damon_sysfs_attrs_rm_dirs(struct damon_sysfs_attrs *attrs)
600 {                                                 600 {
601         kobject_put(&attrs->nr_regions_range->    601         kobject_put(&attrs->nr_regions_range->kobj);
602         kobject_put(&attrs->intervals->kobj);     602         kobject_put(&attrs->intervals->kobj);
603 }                                                 603 }
604                                                   604 
605 static void damon_sysfs_attrs_release(struct k    605 static void damon_sysfs_attrs_release(struct kobject *kobj)
606 {                                                 606 {
607         kfree(container_of(kobj, struct damon_    607         kfree(container_of(kobj, struct damon_sysfs_attrs, kobj));
608 }                                                 608 }
609                                                   609 
610 static struct attribute *damon_sysfs_attrs_att    610 static struct attribute *damon_sysfs_attrs_attrs[] = {
611         NULL,                                     611         NULL,
612 };                                                612 };
613 ATTRIBUTE_GROUPS(damon_sysfs_attrs);              613 ATTRIBUTE_GROUPS(damon_sysfs_attrs);
614                                                   614 
615 static const struct kobj_type damon_sysfs_attr    615 static const struct kobj_type damon_sysfs_attrs_ktype = {
616         .release = damon_sysfs_attrs_release,     616         .release = damon_sysfs_attrs_release,
617         .sysfs_ops = &kobj_sysfs_ops,             617         .sysfs_ops = &kobj_sysfs_ops,
618         .default_groups = damon_sysfs_attrs_gr    618         .default_groups = damon_sysfs_attrs_groups,
619 };                                                619 };
620                                                   620 
621 /*                                                621 /*
622  * context directory                              622  * context directory
623  */                                               623  */
624                                                   624 
625 /* This should match with enum damon_ops_id */    625 /* This should match with enum damon_ops_id */
626 static const char * const damon_sysfs_ops_strs    626 static const char * const damon_sysfs_ops_strs[] = {
627         "vaddr",                                  627         "vaddr",
628         "fvaddr",                                 628         "fvaddr",
629         "paddr",                                  629         "paddr",
630 };                                                630 };
631                                                   631 
632 struct damon_sysfs_context {                      632 struct damon_sysfs_context {
633         struct kobject kobj;                      633         struct kobject kobj;
634         enum damon_ops_id ops_id;                 634         enum damon_ops_id ops_id;
635         struct damon_sysfs_attrs *attrs;          635         struct damon_sysfs_attrs *attrs;
636         struct damon_sysfs_targets *targets;      636         struct damon_sysfs_targets *targets;
637         struct damon_sysfs_schemes *schemes;      637         struct damon_sysfs_schemes *schemes;
638 };                                                638 };
639                                                   639 
640 static struct damon_sysfs_context *damon_sysfs    640 static struct damon_sysfs_context *damon_sysfs_context_alloc(
641                 enum damon_ops_id ops_id)         641                 enum damon_ops_id ops_id)
642 {                                                 642 {
643         struct damon_sysfs_context *context =     643         struct damon_sysfs_context *context = kmalloc(sizeof(*context),
644                                 GFP_KERNEL);      644                                 GFP_KERNEL);
645                                                   645 
646         if (!context)                             646         if (!context)
647                 return NULL;                      647                 return NULL;
648         context->kobj = (struct kobject){};       648         context->kobj = (struct kobject){};
649         context->ops_id = ops_id;                 649         context->ops_id = ops_id;
650         return context;                           650         return context;
651 }                                                 651 }
652                                                   652 
653 static int damon_sysfs_context_set_attrs(struc    653 static int damon_sysfs_context_set_attrs(struct damon_sysfs_context *context)
654 {                                                 654 {
655         struct damon_sysfs_attrs *attrs = damo    655         struct damon_sysfs_attrs *attrs = damon_sysfs_attrs_alloc();
656         int err;                                  656         int err;
657                                                   657 
658         if (!attrs)                               658         if (!attrs)
659                 return -ENOMEM;                   659                 return -ENOMEM;
660         err = kobject_init_and_add(&attrs->kob    660         err = kobject_init_and_add(&attrs->kobj, &damon_sysfs_attrs_ktype,
661                         &context->kobj, "monit    661                         &context->kobj, "monitoring_attrs");
662         if (err)                                  662         if (err)
663                 goto out;                         663                 goto out;
664         err = damon_sysfs_attrs_add_dirs(attrs    664         err = damon_sysfs_attrs_add_dirs(attrs);
665         if (err)                                  665         if (err)
666                 goto out;                         666                 goto out;
667         context->attrs = attrs;                   667         context->attrs = attrs;
668         return 0;                                 668         return 0;
669                                                   669 
670 out:                                              670 out:
671         kobject_put(&attrs->kobj);                671         kobject_put(&attrs->kobj);
672         return err;                               672         return err;
673 }                                                 673 }
674                                                   674 
675 static int damon_sysfs_context_set_targets(str    675 static int damon_sysfs_context_set_targets(struct damon_sysfs_context *context)
676 {                                                 676 {
677         struct damon_sysfs_targets *targets =     677         struct damon_sysfs_targets *targets = damon_sysfs_targets_alloc();
678         int err;                                  678         int err;
679                                                   679 
680         if (!targets)                             680         if (!targets)
681                 return -ENOMEM;                   681                 return -ENOMEM;
682         err = kobject_init_and_add(&targets->k    682         err = kobject_init_and_add(&targets->kobj, &damon_sysfs_targets_ktype,
683                         &context->kobj, "targe    683                         &context->kobj, "targets");
684         if (err) {                                684         if (err) {
685                 kobject_put(&targets->kobj);      685                 kobject_put(&targets->kobj);
686                 return err;                       686                 return err;
687         }                                         687         }
688         context->targets = targets;               688         context->targets = targets;
689         return 0;                                 689         return 0;
690 }                                                 690 }
691                                                   691 
692 static int damon_sysfs_context_set_schemes(str    692 static int damon_sysfs_context_set_schemes(struct damon_sysfs_context *context)
693 {                                                 693 {
694         struct damon_sysfs_schemes *schemes =     694         struct damon_sysfs_schemes *schemes = damon_sysfs_schemes_alloc();
695         int err;                                  695         int err;
696                                                   696 
697         if (!schemes)                             697         if (!schemes)
698                 return -ENOMEM;                   698                 return -ENOMEM;
699         err = kobject_init_and_add(&schemes->k    699         err = kobject_init_and_add(&schemes->kobj, &damon_sysfs_schemes_ktype,
700                         &context->kobj, "schem    700                         &context->kobj, "schemes");
701         if (err) {                                701         if (err) {
702                 kobject_put(&schemes->kobj);      702                 kobject_put(&schemes->kobj);
703                 return err;                       703                 return err;
704         }                                         704         }
705         context->schemes = schemes;               705         context->schemes = schemes;
706         return 0;                                 706         return 0;
707 }                                                 707 }
708                                                   708 
709 static int damon_sysfs_context_add_dirs(struct    709 static int damon_sysfs_context_add_dirs(struct damon_sysfs_context *context)
710 {                                                 710 {
711         int err;                                  711         int err;
712                                                   712 
713         err = damon_sysfs_context_set_attrs(co    713         err = damon_sysfs_context_set_attrs(context);
714         if (err)                                  714         if (err)
715                 return err;                       715                 return err;
716                                                   716 
717         err = damon_sysfs_context_set_targets(    717         err = damon_sysfs_context_set_targets(context);
718         if (err)                                  718         if (err)
719                 goto put_attrs_out;               719                 goto put_attrs_out;
720                                                   720 
721         err = damon_sysfs_context_set_schemes(    721         err = damon_sysfs_context_set_schemes(context);
722         if (err)                                  722         if (err)
723                 goto put_targets_attrs_out;       723                 goto put_targets_attrs_out;
724         return 0;                                 724         return 0;
725                                                   725 
726 put_targets_attrs_out:                            726 put_targets_attrs_out:
727         kobject_put(&context->targets->kobj);     727         kobject_put(&context->targets->kobj);
728         context->targets = NULL;                  728         context->targets = NULL;
729 put_attrs_out:                                    729 put_attrs_out:
730         kobject_put(&context->attrs->kobj);       730         kobject_put(&context->attrs->kobj);
731         context->attrs = NULL;                    731         context->attrs = NULL;
732         return err;                               732         return err;
733 }                                                 733 }
734                                                   734 
735 static void damon_sysfs_context_rm_dirs(struct    735 static void damon_sysfs_context_rm_dirs(struct damon_sysfs_context *context)
736 {                                                 736 {
737         damon_sysfs_attrs_rm_dirs(context->att    737         damon_sysfs_attrs_rm_dirs(context->attrs);
738         kobject_put(&context->attrs->kobj);       738         kobject_put(&context->attrs->kobj);
739         damon_sysfs_targets_rm_dirs(context->t    739         damon_sysfs_targets_rm_dirs(context->targets);
740         kobject_put(&context->targets->kobj);     740         kobject_put(&context->targets->kobj);
741         damon_sysfs_schemes_rm_dirs(context->s    741         damon_sysfs_schemes_rm_dirs(context->schemes);
742         kobject_put(&context->schemes->kobj);     742         kobject_put(&context->schemes->kobj);
743 }                                                 743 }
744                                                   744 
745 static ssize_t avail_operations_show(struct ko    745 static ssize_t avail_operations_show(struct kobject *kobj,
746                 struct kobj_attribute *attr, c    746                 struct kobj_attribute *attr, char *buf)
747 {                                                 747 {
748         enum damon_ops_id id;                     748         enum damon_ops_id id;
749         int len = 0;                              749         int len = 0;
750                                                   750 
751         for (id = 0; id < NR_DAMON_OPS; id++)     751         for (id = 0; id < NR_DAMON_OPS; id++) {
752                 if (!damon_is_registered_ops(i    752                 if (!damon_is_registered_ops(id))
753                         continue;                 753                         continue;
754                 len += sysfs_emit_at(buf, len,    754                 len += sysfs_emit_at(buf, len, "%s\n",
755                                 damon_sysfs_op    755                                 damon_sysfs_ops_strs[id]);
756         }                                         756         }
757         return len;                               757         return len;
758 }                                                 758 }
759                                                   759 
760 static ssize_t operations_show(struct kobject     760 static ssize_t operations_show(struct kobject *kobj,
761                 struct kobj_attribute *attr, c    761                 struct kobj_attribute *attr, char *buf)
762 {                                                 762 {
763         struct damon_sysfs_context *context =     763         struct damon_sysfs_context *context = container_of(kobj,
764                         struct damon_sysfs_con    764                         struct damon_sysfs_context, kobj);
765                                                   765 
766         return sysfs_emit(buf, "%s\n", damon_s    766         return sysfs_emit(buf, "%s\n", damon_sysfs_ops_strs[context->ops_id]);
767 }                                                 767 }
768                                                   768 
769 static ssize_t operations_store(struct kobject    769 static ssize_t operations_store(struct kobject *kobj,
770                 struct kobj_attribute *attr, c    770                 struct kobj_attribute *attr, const char *buf, size_t count)
771 {                                                 771 {
772         struct damon_sysfs_context *context =     772         struct damon_sysfs_context *context = container_of(kobj,
773                         struct damon_sysfs_con    773                         struct damon_sysfs_context, kobj);
774         enum damon_ops_id id;                     774         enum damon_ops_id id;
775                                                   775 
776         for (id = 0; id < NR_DAMON_OPS; id++)     776         for (id = 0; id < NR_DAMON_OPS; id++) {
777                 if (sysfs_streq(buf, damon_sys    777                 if (sysfs_streq(buf, damon_sysfs_ops_strs[id])) {
778                         context->ops_id = id;     778                         context->ops_id = id;
779                         return count;             779                         return count;
780                 }                                 780                 }
781         }                                         781         }
782         return -EINVAL;                           782         return -EINVAL;
783 }                                                 783 }
784                                                   784 
785 static void damon_sysfs_context_release(struct    785 static void damon_sysfs_context_release(struct kobject *kobj)
786 {                                                 786 {
787         kfree(container_of(kobj, struct damon_    787         kfree(container_of(kobj, struct damon_sysfs_context, kobj));
788 }                                                 788 }
789                                                   789 
790 static struct kobj_attribute damon_sysfs_conte    790 static struct kobj_attribute damon_sysfs_context_avail_operations_attr =
791                 __ATTR_RO_MODE(avail_operation    791                 __ATTR_RO_MODE(avail_operations, 0400);
792                                                   792 
793 static struct kobj_attribute damon_sysfs_conte    793 static struct kobj_attribute damon_sysfs_context_operations_attr =
794                 __ATTR_RW_MODE(operations, 060    794                 __ATTR_RW_MODE(operations, 0600);
795                                                   795 
796 static struct attribute *damon_sysfs_context_a    796 static struct attribute *damon_sysfs_context_attrs[] = {
797         &damon_sysfs_context_avail_operations_    797         &damon_sysfs_context_avail_operations_attr.attr,
798         &damon_sysfs_context_operations_attr.a    798         &damon_sysfs_context_operations_attr.attr,
799         NULL,                                     799         NULL,
800 };                                                800 };
801 ATTRIBUTE_GROUPS(damon_sysfs_context);            801 ATTRIBUTE_GROUPS(damon_sysfs_context);
802                                                   802 
803 static const struct kobj_type damon_sysfs_cont    803 static const struct kobj_type damon_sysfs_context_ktype = {
804         .release = damon_sysfs_context_release    804         .release = damon_sysfs_context_release,
805         .sysfs_ops = &kobj_sysfs_ops,             805         .sysfs_ops = &kobj_sysfs_ops,
806         .default_groups = damon_sysfs_context_    806         .default_groups = damon_sysfs_context_groups,
807 };                                                807 };
808                                                   808 
809 /*                                                809 /*
810  * contexts directory                             810  * contexts directory
811  */                                               811  */
812                                                   812 
813 struct damon_sysfs_contexts {                     813 struct damon_sysfs_contexts {
814         struct kobject kobj;                      814         struct kobject kobj;
815         struct damon_sysfs_context **contexts_    815         struct damon_sysfs_context **contexts_arr;
816         int nr;                                   816         int nr;
817 };                                                817 };
818                                                   818 
819 static struct damon_sysfs_contexts *damon_sysf    819 static struct damon_sysfs_contexts *damon_sysfs_contexts_alloc(void)
820 {                                                 820 {
821         return kzalloc(sizeof(struct damon_sys    821         return kzalloc(sizeof(struct damon_sysfs_contexts), GFP_KERNEL);
822 }                                                 822 }
823                                                   823 
824 static void damon_sysfs_contexts_rm_dirs(struc    824 static void damon_sysfs_contexts_rm_dirs(struct damon_sysfs_contexts *contexts)
825 {                                                 825 {
826         struct damon_sysfs_context **contexts_    826         struct damon_sysfs_context **contexts_arr = contexts->contexts_arr;
827         int i;                                    827         int i;
828                                                   828 
829         for (i = 0; i < contexts->nr; i++) {      829         for (i = 0; i < contexts->nr; i++) {
830                 damon_sysfs_context_rm_dirs(co    830                 damon_sysfs_context_rm_dirs(contexts_arr[i]);
831                 kobject_put(&contexts_arr[i]->    831                 kobject_put(&contexts_arr[i]->kobj);
832         }                                         832         }
833         contexts->nr = 0;                         833         contexts->nr = 0;
834         kfree(contexts_arr);                      834         kfree(contexts_arr);
835         contexts->contexts_arr = NULL;            835         contexts->contexts_arr = NULL;
836 }                                                 836 }
837                                                   837 
838 static int damon_sysfs_contexts_add_dirs(struc    838 static int damon_sysfs_contexts_add_dirs(struct damon_sysfs_contexts *contexts,
839                 int nr_contexts)                  839                 int nr_contexts)
840 {                                                 840 {
841         struct damon_sysfs_context **contexts_    841         struct damon_sysfs_context **contexts_arr, *context;
842         int err, i;                               842         int err, i;
843                                                   843 
844         damon_sysfs_contexts_rm_dirs(contexts)    844         damon_sysfs_contexts_rm_dirs(contexts);
845         if (!nr_contexts)                         845         if (!nr_contexts)
846                 return 0;                         846                 return 0;
847                                                   847 
848         contexts_arr = kmalloc_array(nr_contex    848         contexts_arr = kmalloc_array(nr_contexts, sizeof(*contexts_arr),
849                         GFP_KERNEL | __GFP_NOW    849                         GFP_KERNEL | __GFP_NOWARN);
850         if (!contexts_arr)                        850         if (!contexts_arr)
851                 return -ENOMEM;                   851                 return -ENOMEM;
852         contexts->contexts_arr = contexts_arr;    852         contexts->contexts_arr = contexts_arr;
853                                                   853 
854         for (i = 0; i < nr_contexts; i++) {       854         for (i = 0; i < nr_contexts; i++) {
855                 context = damon_sysfs_context_    855                 context = damon_sysfs_context_alloc(DAMON_OPS_VADDR);
856                 if (!context) {                   856                 if (!context) {
857                         damon_sysfs_contexts_r    857                         damon_sysfs_contexts_rm_dirs(contexts);
858                         return -ENOMEM;           858                         return -ENOMEM;
859                 }                                 859                 }
860                                                   860 
861                 err = kobject_init_and_add(&co    861                 err = kobject_init_and_add(&context->kobj,
862                                 &damon_sysfs_c    862                                 &damon_sysfs_context_ktype, &contexts->kobj,
863                                 "%d", i);         863                                 "%d", i);
864                 if (err)                          864                 if (err)
865                         goto out;                 865                         goto out;
866                                                   866 
867                 err = damon_sysfs_context_add_    867                 err = damon_sysfs_context_add_dirs(context);
868                 if (err)                          868                 if (err)
869                         goto out;                 869                         goto out;
870                                                   870 
871                 contexts_arr[i] = context;        871                 contexts_arr[i] = context;
872                 contexts->nr++;                   872                 contexts->nr++;
873         }                                         873         }
874         return 0;                                 874         return 0;
875                                                   875 
876 out:                                              876 out:
877         damon_sysfs_contexts_rm_dirs(contexts)    877         damon_sysfs_contexts_rm_dirs(contexts);
878         kobject_put(&context->kobj);              878         kobject_put(&context->kobj);
879         return err;                               879         return err;
880 }                                                 880 }
881                                                   881 
882 static ssize_t nr_contexts_show(struct kobject    882 static ssize_t nr_contexts_show(struct kobject *kobj,
883                 struct kobj_attribute *attr, c    883                 struct kobj_attribute *attr, char *buf)
884 {                                                 884 {
885         struct damon_sysfs_contexts *contexts     885         struct damon_sysfs_contexts *contexts = container_of(kobj,
886                         struct damon_sysfs_con    886                         struct damon_sysfs_contexts, kobj);
887                                                   887 
888         return sysfs_emit(buf, "%d\n", context    888         return sysfs_emit(buf, "%d\n", contexts->nr);
889 }                                                 889 }
890                                                   890 
891 static ssize_t nr_contexts_store(struct kobjec    891 static ssize_t nr_contexts_store(struct kobject *kobj,
892                 struct kobj_attribute *attr, c    892                 struct kobj_attribute *attr, const char *buf, size_t count)
893 {                                                 893 {
894         struct damon_sysfs_contexts *contexts;    894         struct damon_sysfs_contexts *contexts;
895         int nr, err;                              895         int nr, err;
896                                                   896 
897         err = kstrtoint(buf, 0, &nr);             897         err = kstrtoint(buf, 0, &nr);
898         if (err)                                  898         if (err)
899                 return err;                       899                 return err;
900         /* TODO: support multiple contexts per    900         /* TODO: support multiple contexts per kdamond */
901         if (nr < 0 || 1 < nr)                     901         if (nr < 0 || 1 < nr)
902                 return -EINVAL;                   902                 return -EINVAL;
903                                                   903 
904         contexts = container_of(kobj, struct d    904         contexts = container_of(kobj, struct damon_sysfs_contexts, kobj);
905         if (!mutex_trylock(&damon_sysfs_lock))    905         if (!mutex_trylock(&damon_sysfs_lock))
906                 return -EBUSY;                    906                 return -EBUSY;
907         err = damon_sysfs_contexts_add_dirs(co    907         err = damon_sysfs_contexts_add_dirs(contexts, nr);
908         mutex_unlock(&damon_sysfs_lock);          908         mutex_unlock(&damon_sysfs_lock);
909         if (err)                                  909         if (err)
910                 return err;                       910                 return err;
911                                                   911 
912         return count;                             912         return count;
913 }                                                 913 }
914                                                   914 
915 static void damon_sysfs_contexts_release(struc    915 static void damon_sysfs_contexts_release(struct kobject *kobj)
916 {                                                 916 {
917         kfree(container_of(kobj, struct damon_    917         kfree(container_of(kobj, struct damon_sysfs_contexts, kobj));
918 }                                                 918 }
919                                                   919 
920 static struct kobj_attribute damon_sysfs_conte    920 static struct kobj_attribute damon_sysfs_contexts_nr_attr
921                 = __ATTR_RW_MODE(nr_contexts,     921                 = __ATTR_RW_MODE(nr_contexts, 0600);
922                                                   922 
923 static struct attribute *damon_sysfs_contexts_    923 static struct attribute *damon_sysfs_contexts_attrs[] = {
924         &damon_sysfs_contexts_nr_attr.attr,       924         &damon_sysfs_contexts_nr_attr.attr,
925         NULL,                                     925         NULL,
926 };                                                926 };
927 ATTRIBUTE_GROUPS(damon_sysfs_contexts);           927 ATTRIBUTE_GROUPS(damon_sysfs_contexts);
928                                                   928 
929 static const struct kobj_type damon_sysfs_cont    929 static const struct kobj_type damon_sysfs_contexts_ktype = {
930         .release = damon_sysfs_contexts_releas    930         .release = damon_sysfs_contexts_release,
931         .sysfs_ops = &kobj_sysfs_ops,             931         .sysfs_ops = &kobj_sysfs_ops,
932         .default_groups = damon_sysfs_contexts    932         .default_groups = damon_sysfs_contexts_groups,
933 };                                                933 };
934                                                   934 
935 /*                                                935 /*
936  * kdamond directory                              936  * kdamond directory
937  */                                               937  */
938                                                   938 
939 struct damon_sysfs_kdamond {                      939 struct damon_sysfs_kdamond {
940         struct kobject kobj;                      940         struct kobject kobj;
941         struct damon_sysfs_contexts *contexts;    941         struct damon_sysfs_contexts *contexts;
942         struct damon_ctx *damon_ctx;              942         struct damon_ctx *damon_ctx;
943 };                                                943 };
944                                                   944 
945 static struct damon_sysfs_kdamond *damon_sysfs    945 static struct damon_sysfs_kdamond *damon_sysfs_kdamond_alloc(void)
946 {                                                 946 {
947         return kzalloc(sizeof(struct damon_sys    947         return kzalloc(sizeof(struct damon_sysfs_kdamond), GFP_KERNEL);
948 }                                                 948 }
949                                                   949 
950 static int damon_sysfs_kdamond_add_dirs(struct    950 static int damon_sysfs_kdamond_add_dirs(struct damon_sysfs_kdamond *kdamond)
951 {                                                 951 {
952         struct damon_sysfs_contexts *contexts;    952         struct damon_sysfs_contexts *contexts;
953         int err;                                  953         int err;
954                                                   954 
955         contexts = damon_sysfs_contexts_alloc(    955         contexts = damon_sysfs_contexts_alloc();
956         if (!contexts)                            956         if (!contexts)
957                 return -ENOMEM;                   957                 return -ENOMEM;
958                                                   958 
959         err = kobject_init_and_add(&contexts->    959         err = kobject_init_and_add(&contexts->kobj,
960                         &damon_sysfs_contexts_    960                         &damon_sysfs_contexts_ktype, &kdamond->kobj,
961                         "contexts");              961                         "contexts");
962         if (err) {                                962         if (err) {
963                 kobject_put(&contexts->kobj);     963                 kobject_put(&contexts->kobj);
964                 return err;                       964                 return err;
965         }                                         965         }
966         kdamond->contexts = contexts;             966         kdamond->contexts = contexts;
967                                                   967 
968         return err;                               968         return err;
969 }                                                 969 }
970                                                   970 
971 static void damon_sysfs_kdamond_rm_dirs(struct    971 static void damon_sysfs_kdamond_rm_dirs(struct damon_sysfs_kdamond *kdamond)
972 {                                                 972 {
973         damon_sysfs_contexts_rm_dirs(kdamond->    973         damon_sysfs_contexts_rm_dirs(kdamond->contexts);
974         kobject_put(&kdamond->contexts->kobj);    974         kobject_put(&kdamond->contexts->kobj);
975 }                                                 975 }
976                                                   976 
977 static bool damon_sysfs_ctx_running(struct dam    977 static bool damon_sysfs_ctx_running(struct damon_ctx *ctx)
978 {                                                 978 {
979         bool running;                             979         bool running;
980                                                   980 
981         mutex_lock(&ctx->kdamond_lock);           981         mutex_lock(&ctx->kdamond_lock);
982         running = ctx->kdamond != NULL;           982         running = ctx->kdamond != NULL;
983         mutex_unlock(&ctx->kdamond_lock);         983         mutex_unlock(&ctx->kdamond_lock);
984         return running;                           984         return running;
985 }                                                 985 }
986                                                   986 
987 /*                                                987 /*
988  * enum damon_sysfs_cmd - Commands for a speci    988  * enum damon_sysfs_cmd - Commands for a specific kdamond.
989  */                                               989  */
990 enum damon_sysfs_cmd {                            990 enum damon_sysfs_cmd {
991         /* @DAMON_SYSFS_CMD_ON: Turn the kdamo    991         /* @DAMON_SYSFS_CMD_ON: Turn the kdamond on. */
992         DAMON_SYSFS_CMD_ON,                       992         DAMON_SYSFS_CMD_ON,
993         /* @DAMON_SYSFS_CMD_OFF: Turn the kdam    993         /* @DAMON_SYSFS_CMD_OFF: Turn the kdamond off. */
994         DAMON_SYSFS_CMD_OFF,                      994         DAMON_SYSFS_CMD_OFF,
995         /* @DAMON_SYSFS_CMD_COMMIT: Update kda    995         /* @DAMON_SYSFS_CMD_COMMIT: Update kdamond inputs. */
996         DAMON_SYSFS_CMD_COMMIT,                   996         DAMON_SYSFS_CMD_COMMIT,
997         /*                                        997         /*
998          * @DAMON_SYSFS_CMD_COMMIT_SCHEMES_QUO    998          * @DAMON_SYSFS_CMD_COMMIT_SCHEMES_QUOTA_GOALS: Commit the quota goals
999          * to DAMON.                              999          * to DAMON.
1000          */                                      1000          */
1001         DAMON_SYSFS_CMD_COMMIT_SCHEMES_QUOTA_    1001         DAMON_SYSFS_CMD_COMMIT_SCHEMES_QUOTA_GOALS,
1002         /*                                       1002         /*
1003          * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_ST    1003          * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS: Update scheme stats sysfs
1004          * files.                                1004          * files.
1005          */                                      1005          */
1006         DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS,    1006         DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS,
1007         /*                                       1007         /*
1008          * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_TR    1008          * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_BYTES: Update
1009          * tried_regions/total_bytes sysfs fi    1009          * tried_regions/total_bytes sysfs files for each scheme.
1010          */                                      1010          */
1011         DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_    1011         DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_BYTES,
1012         /*                                       1012         /*
1013          * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_TR    1013          * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_REGIONS: Update schemes tried
1014          * regions                               1014          * regions
1015          */                                      1015          */
1016         DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_    1016         DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_REGIONS,
1017         /*                                       1017         /*
1018          * @DAMON_SYSFS_CMD_CLEAR_SCHEMES_TRI    1018          * @DAMON_SYSFS_CMD_CLEAR_SCHEMES_TRIED_REGIONS: Clear schemes tried
1019          * regions                               1019          * regions
1020          */                                      1020          */
1021         DAMON_SYSFS_CMD_CLEAR_SCHEMES_TRIED_R    1021         DAMON_SYSFS_CMD_CLEAR_SCHEMES_TRIED_REGIONS,
1022         /*                                       1022         /*
1023          * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_EF    1023          * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_EFFECTIVE_QUOTAS: Update the
1024          * effective size quota of the scheme    1024          * effective size quota of the scheme in bytes.
1025          */                                      1025          */
1026         DAMON_SYSFS_CMD_UPDATE_SCHEMES_EFFECT    1026         DAMON_SYSFS_CMD_UPDATE_SCHEMES_EFFECTIVE_QUOTAS,
1027         /*                                       1027         /*
1028          * @NR_DAMON_SYSFS_CMDS: Total number    1028          * @NR_DAMON_SYSFS_CMDS: Total number of DAMON sysfs commands.
1029          */                                      1029          */
1030         NR_DAMON_SYSFS_CMDS,                     1030         NR_DAMON_SYSFS_CMDS,
1031 };                                               1031 };
1032                                                  1032 
1033 /* Should match with enum damon_sysfs_cmd */     1033 /* Should match with enum damon_sysfs_cmd */
1034 static const char * const damon_sysfs_cmd_str    1034 static const char * const damon_sysfs_cmd_strs[] = {
1035         "on",                                    1035         "on",
1036         "off",                                   1036         "off",
1037         "commit",                                1037         "commit",
1038         "commit_schemes_quota_goals",            1038         "commit_schemes_quota_goals",
1039         "update_schemes_stats",                  1039         "update_schemes_stats",
1040         "update_schemes_tried_bytes",            1040         "update_schemes_tried_bytes",
1041         "update_schemes_tried_regions",          1041         "update_schemes_tried_regions",
1042         "clear_schemes_tried_regions",           1042         "clear_schemes_tried_regions",
1043         "update_schemes_effective_quotas",       1043         "update_schemes_effective_quotas",
1044 };                                               1044 };
1045                                                  1045 
1046 /*                                               1046 /*
1047  * struct damon_sysfs_cmd_request - A request    1047  * struct damon_sysfs_cmd_request - A request to the DAMON callback.
1048  * @cmd:        The command that needs to be     1048  * @cmd:        The command that needs to be handled by the callback.
1049  * @kdamond:    The kobject wrapper that asso    1049  * @kdamond:    The kobject wrapper that associated to the kdamond thread.
1050  *                                               1050  *
1051  * This structure represents a sysfs command     1051  * This structure represents a sysfs command request that need to access some
1052  * DAMON context-internal data.  Because DAMO    1052  * DAMON context-internal data.  Because DAMON context-internal data can be
1053  * safely accessed from DAMON callbacks witho    1053  * safely accessed from DAMON callbacks without additional synchronization, the
1054  * request will be handled by the DAMON callb    1054  * request will be handled by the DAMON callback.  None-``NULL`` @kdamond means
1055  * the request is valid.                         1055  * the request is valid.
1056  */                                              1056  */
1057 struct damon_sysfs_cmd_request {                 1057 struct damon_sysfs_cmd_request {
1058         enum damon_sysfs_cmd cmd;                1058         enum damon_sysfs_cmd cmd;
1059         struct damon_sysfs_kdamond *kdamond;     1059         struct damon_sysfs_kdamond *kdamond;
1060 };                                               1060 };
1061                                                  1061 
1062 /* Current DAMON callback request.  Protected    1062 /* Current DAMON callback request.  Protected by damon_sysfs_lock. */
1063 static struct damon_sysfs_cmd_request damon_s    1063 static struct damon_sysfs_cmd_request damon_sysfs_cmd_request;
1064                                                  1064 
1065 static ssize_t state_show(struct kobject *kob    1065 static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
1066                 char *buf)                       1066                 char *buf)
1067 {                                                1067 {
1068         struct damon_sysfs_kdamond *kdamond =    1068         struct damon_sysfs_kdamond *kdamond = container_of(kobj,
1069                         struct damon_sysfs_kd    1069                         struct damon_sysfs_kdamond, kobj);
1070         struct damon_ctx *ctx = kdamond->damo    1070         struct damon_ctx *ctx = kdamond->damon_ctx;
1071         bool running;                            1071         bool running;
1072                                                  1072 
1073         if (!ctx)                                1073         if (!ctx)
1074                 running = false;                 1074                 running = false;
1075         else                                     1075         else
1076                 running = damon_sysfs_ctx_run    1076                 running = damon_sysfs_ctx_running(ctx);
1077                                                  1077 
1078         return sysfs_emit(buf, "%s\n", runnin    1078         return sysfs_emit(buf, "%s\n", running ?
1079                         damon_sysfs_cmd_strs[    1079                         damon_sysfs_cmd_strs[DAMON_SYSFS_CMD_ON] :
1080                         damon_sysfs_cmd_strs[    1080                         damon_sysfs_cmd_strs[DAMON_SYSFS_CMD_OFF]);
1081 }                                                1081 }
1082                                                  1082 
1083 static int damon_sysfs_set_attrs(struct damon    1083 static int damon_sysfs_set_attrs(struct damon_ctx *ctx,
1084                 struct damon_sysfs_attrs *sys    1084                 struct damon_sysfs_attrs *sys_attrs)
1085 {                                                1085 {
1086         struct damon_sysfs_intervals *sys_int    1086         struct damon_sysfs_intervals *sys_intervals = sys_attrs->intervals;
1087         struct damon_sysfs_ul_range *sys_nr_r    1087         struct damon_sysfs_ul_range *sys_nr_regions =
1088                 sys_attrs->nr_regions_range;     1088                 sys_attrs->nr_regions_range;
1089         struct damon_attrs attrs = {             1089         struct damon_attrs attrs = {
1090                 .sample_interval = sys_interv    1090                 .sample_interval = sys_intervals->sample_us,
1091                 .aggr_interval = sys_interval    1091                 .aggr_interval = sys_intervals->aggr_us,
1092                 .ops_update_interval = sys_in    1092                 .ops_update_interval = sys_intervals->update_us,
1093                 .min_nr_regions = sys_nr_regi    1093                 .min_nr_regions = sys_nr_regions->min,
1094                 .max_nr_regions = sys_nr_regi    1094                 .max_nr_regions = sys_nr_regions->max,
1095         };                                       1095         };
1096         return damon_set_attrs(ctx, &attrs);     1096         return damon_set_attrs(ctx, &attrs);
1097 }                                                1097 }
1098                                                  1098 
1099 static void damon_sysfs_destroy_targets(struc    1099 static void damon_sysfs_destroy_targets(struct damon_ctx *ctx)
1100 {                                                1100 {
1101         struct damon_target *t, *next;           1101         struct damon_target *t, *next;
1102         bool has_pid = damon_target_has_pid(c    1102         bool has_pid = damon_target_has_pid(ctx);
1103                                                  1103 
1104         damon_for_each_target_safe(t, next, c    1104         damon_for_each_target_safe(t, next, ctx) {
1105                 if (has_pid)                     1105                 if (has_pid)
1106                         put_pid(t->pid);         1106                         put_pid(t->pid);
1107                 damon_destroy_target(t);         1107                 damon_destroy_target(t);
1108         }                                        1108         }
1109 }                                                1109 }
1110                                                  1110 
1111 static int damon_sysfs_set_regions(struct dam    1111 static int damon_sysfs_set_regions(struct damon_target *t,
1112                 struct damon_sysfs_regions *s    1112                 struct damon_sysfs_regions *sysfs_regions)
1113 {                                                1113 {
1114         struct damon_addr_range *ranges = kma    1114         struct damon_addr_range *ranges = kmalloc_array(sysfs_regions->nr,
1115                         sizeof(*ranges), GFP_    1115                         sizeof(*ranges), GFP_KERNEL | __GFP_NOWARN);
1116         int i, err = -EINVAL;                    1116         int i, err = -EINVAL;
1117                                                  1117 
1118         if (!ranges)                             1118         if (!ranges)
1119                 return -ENOMEM;                  1119                 return -ENOMEM;
1120         for (i = 0; i < sysfs_regions->nr; i+    1120         for (i = 0; i < sysfs_regions->nr; i++) {
1121                 struct damon_sysfs_region *sy    1121                 struct damon_sysfs_region *sys_region =
1122                         sysfs_regions->region    1122                         sysfs_regions->regions_arr[i];
1123                                                  1123 
1124                 if (sys_region->ar.start > sy    1124                 if (sys_region->ar.start > sys_region->ar.end)
1125                         goto out;                1125                         goto out;
1126                                                  1126 
1127                 ranges[i].start = sys_region-    1127                 ranges[i].start = sys_region->ar.start;
1128                 ranges[i].end = sys_region->a    1128                 ranges[i].end = sys_region->ar.end;
1129                 if (i == 0)                      1129                 if (i == 0)
1130                         continue;                1130                         continue;
1131                 if (ranges[i - 1].end > range    1131                 if (ranges[i - 1].end > ranges[i].start)
1132                         goto out;                1132                         goto out;
1133         }                                        1133         }
1134         err = damon_set_regions(t, ranges, sy    1134         err = damon_set_regions(t, ranges, sysfs_regions->nr);
1135 out:                                             1135 out:
1136         kfree(ranges);                           1136         kfree(ranges);
1137         return err;                              1137         return err;
1138                                                  1138 
1139 }                                                1139 }
1140                                                  1140 
1141 static int damon_sysfs_add_target(struct damo    1141 static int damon_sysfs_add_target(struct damon_sysfs_target *sys_target,
1142                 struct damon_ctx *ctx)           1142                 struct damon_ctx *ctx)
1143 {                                                1143 {
1144         struct damon_target *t = damon_new_ta    1144         struct damon_target *t = damon_new_target();
1145         int err = -EINVAL;                       1145         int err = -EINVAL;
1146                                                  1146 
1147         if (!t)                                  1147         if (!t)
1148                 return -ENOMEM;                  1148                 return -ENOMEM;
1149         damon_add_target(ctx, t);                1149         damon_add_target(ctx, t);
1150         if (damon_target_has_pid(ctx)) {         1150         if (damon_target_has_pid(ctx)) {
1151                 t->pid = find_get_pid(sys_tar    1151                 t->pid = find_get_pid(sys_target->pid);
1152                 if (!t->pid)                     1152                 if (!t->pid)
1153                         goto destroy_targets_    1153                         goto destroy_targets_out;
1154         }                                        1154         }
1155         err = damon_sysfs_set_regions(t, sys_    1155         err = damon_sysfs_set_regions(t, sys_target->regions);
1156         if (err)                                 1156         if (err)
1157                 goto destroy_targets_out;        1157                 goto destroy_targets_out;
1158         return 0;                                1158         return 0;
1159                                                  1159 
1160 destroy_targets_out:                             1160 destroy_targets_out:
1161         damon_sysfs_destroy_targets(ctx);        1161         damon_sysfs_destroy_targets(ctx);
1162         return err;                              1162         return err;
1163 }                                                1163 }
1164                                                  1164 
1165 static int damon_sysfs_add_targets(struct dam !! 1165 static int damon_sysfs_update_target_pid(struct damon_target *target, int pid)
                                                   >> 1166 {
                                                   >> 1167         struct pid *pid_new;
                                                   >> 1168 
                                                   >> 1169         pid_new = find_get_pid(pid);
                                                   >> 1170         if (!pid_new)
                                                   >> 1171                 return -EINVAL;
                                                   >> 1172 
                                                   >> 1173         if (pid_new == target->pid) {
                                                   >> 1174                 put_pid(pid_new);
                                                   >> 1175                 return 0;
                                                   >> 1176         }
                                                   >> 1177 
                                                   >> 1178         put_pid(target->pid);
                                                   >> 1179         target->pid = pid_new;
                                                   >> 1180         return 0;
                                                   >> 1181 }
                                                   >> 1182 
                                                   >> 1183 static int damon_sysfs_update_target(struct damon_target *target,
                                                   >> 1184                 struct damon_ctx *ctx,
                                                   >> 1185                 struct damon_sysfs_target *sys_target)
                                                   >> 1186 {
                                                   >> 1187         int err = 0;
                                                   >> 1188 
                                                   >> 1189         if (damon_target_has_pid(ctx)) {
                                                   >> 1190                 err = damon_sysfs_update_target_pid(target, sys_target->pid);
                                                   >> 1191                 if (err)
                                                   >> 1192                         return err;
                                                   >> 1193         }
                                                   >> 1194 
                                                   >> 1195         /*
                                                   >> 1196          * Do monitoring target region boundary update only if one or more
                                                   >> 1197          * regions are set by the user.  This is for keeping current monitoring
                                                   >> 1198          * target results and range easier, especially for dynamic monitoring
                                                   >> 1199          * target regions update ops like 'vaddr'.
                                                   >> 1200          */
                                                   >> 1201         if (sys_target->regions->nr)
                                                   >> 1202                 err = damon_sysfs_set_regions(target, sys_target->regions);
                                                   >> 1203         return err;
                                                   >> 1204 }
                                                   >> 1205 
                                                   >> 1206 static int damon_sysfs_set_targets(struct damon_ctx *ctx,
1166                 struct damon_sysfs_targets *s    1207                 struct damon_sysfs_targets *sysfs_targets)
1167 {                                                1208 {
1168         int i, err;                           !! 1209         struct damon_target *t, *next;
                                                   >> 1210         int i = 0, err;
1169                                                  1211 
1170         /* Multiple physical address space mo    1212         /* Multiple physical address space monitoring targets makes no sense */
1171         if (ctx->ops.id == DAMON_OPS_PADDR &&    1213         if (ctx->ops.id == DAMON_OPS_PADDR && sysfs_targets->nr > 1)
1172                 return -EINVAL;                  1214                 return -EINVAL;
1173                                                  1215 
1174         for (i = 0; i < sysfs_targets->nr; i+ !! 1216         damon_for_each_target_safe(t, next, ctx) {
                                                   >> 1217                 if (i < sysfs_targets->nr) {
                                                   >> 1218                         err = damon_sysfs_update_target(t, ctx,
                                                   >> 1219                                         sysfs_targets->targets_arr[i]);
                                                   >> 1220                         if (err)
                                                   >> 1221                                 return err;
                                                   >> 1222                 } else {
                                                   >> 1223                         if (damon_target_has_pid(ctx))
                                                   >> 1224                                 put_pid(t->pid);
                                                   >> 1225                         damon_destroy_target(t);
                                                   >> 1226                 }
                                                   >> 1227                 i++;
                                                   >> 1228         }
                                                   >> 1229 
                                                   >> 1230         for (; i < sysfs_targets->nr; i++) {
1175                 struct damon_sysfs_target *st    1231                 struct damon_sysfs_target *st = sysfs_targets->targets_arr[i];
1176                                                  1232 
1177                 err = damon_sysfs_add_target(    1233                 err = damon_sysfs_add_target(st, ctx);
1178                 if (err)                         1234                 if (err)
1179                         return err;              1235                         return err;
1180         }                                        1236         }
1181         return 0;                                1237         return 0;
1182 }                                                1238 }
1183                                                  1239 
1184 static bool damon_sysfs_schemes_regions_updat    1240 static bool damon_sysfs_schemes_regions_updating;
1185                                                  1241 
1186 static void damon_sysfs_before_terminate(stru    1242 static void damon_sysfs_before_terminate(struct damon_ctx *ctx)
1187 {                                                1243 {
1188         struct damon_target *t, *next;           1244         struct damon_target *t, *next;
1189         struct damon_sysfs_kdamond *kdamond;     1245         struct damon_sysfs_kdamond *kdamond;
1190         enum damon_sysfs_cmd cmd;                1246         enum damon_sysfs_cmd cmd;
1191                                                  1247 
1192         /* damon_sysfs_schemes_update_regions    1248         /* damon_sysfs_schemes_update_regions_stop() might not yet called */
1193         kdamond = damon_sysfs_cmd_request.kda    1249         kdamond = damon_sysfs_cmd_request.kdamond;
1194         cmd = damon_sysfs_cmd_request.cmd;       1250         cmd = damon_sysfs_cmd_request.cmd;
1195         if (kdamond && ctx == kdamond->damon_    1251         if (kdamond && ctx == kdamond->damon_ctx &&
1196                         (cmd == DAMON_SYSFS_C    1252                         (cmd == DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_REGIONS ||
1197                          cmd == DAMON_SYSFS_C    1253                          cmd == DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_BYTES) &&
1198                         damon_sysfs_schemes_r    1254                         damon_sysfs_schemes_regions_updating) {
1199                 damon_sysfs_schemes_update_re    1255                 damon_sysfs_schemes_update_regions_stop(ctx);
1200                 damon_sysfs_schemes_regions_u    1256                 damon_sysfs_schemes_regions_updating = false;
1201                 mutex_unlock(&damon_sysfs_loc    1257                 mutex_unlock(&damon_sysfs_lock);
1202         }                                        1258         }
1203                                                  1259 
1204         if (!damon_target_has_pid(ctx))          1260         if (!damon_target_has_pid(ctx))
1205                 return;                          1261                 return;
1206                                                  1262 
1207         mutex_lock(&ctx->kdamond_lock);          1263         mutex_lock(&ctx->kdamond_lock);
1208         damon_for_each_target_safe(t, next, c    1264         damon_for_each_target_safe(t, next, ctx) {
1209                 put_pid(t->pid);                 1265                 put_pid(t->pid);
1210                 damon_destroy_target(t);         1266                 damon_destroy_target(t);
1211         }                                        1267         }
1212         mutex_unlock(&ctx->kdamond_lock);        1268         mutex_unlock(&ctx->kdamond_lock);
1213 }                                                1269 }
1214                                                  1270 
1215 /*                                               1271 /*
1216  * damon_sysfs_upd_schemes_stats() - Update s    1272  * damon_sysfs_upd_schemes_stats() - Update schemes stats sysfs files.
1217  * @kdamond:    The kobject wrapper that asso    1273  * @kdamond:    The kobject wrapper that associated to the kdamond thread.
1218  *                                               1274  *
1219  * This function reads the schemes stats of s    1275  * This function reads the schemes stats of specific kdamond and update the
1220  * related values for sysfs files.  This func    1276  * related values for sysfs files.  This function should be called from DAMON
1221  * callbacks while holding ``damon_syfs_lock`    1277  * callbacks while holding ``damon_syfs_lock``, to safely access the DAMON
1222  * contexts-internal data and DAMON sysfs var    1278  * contexts-internal data and DAMON sysfs variables.
1223  */                                              1279  */
1224 static int damon_sysfs_upd_schemes_stats(stru    1280 static int damon_sysfs_upd_schemes_stats(struct damon_sysfs_kdamond *kdamond)
1225 {                                                1281 {
1226         struct damon_ctx *ctx = kdamond->damo    1282         struct damon_ctx *ctx = kdamond->damon_ctx;
1227                                                  1283 
1228         if (!ctx)                                1284         if (!ctx)
1229                 return -EINVAL;                  1285                 return -EINVAL;
1230         damon_sysfs_schemes_update_stats(        1286         damon_sysfs_schemes_update_stats(
1231                         kdamond->contexts->co    1287                         kdamond->contexts->contexts_arr[0]->schemes, ctx);
1232         return 0;                                1288         return 0;
1233 }                                                1289 }
1234                                                  1290 
1235 static int damon_sysfs_upd_schemes_regions_st    1291 static int damon_sysfs_upd_schemes_regions_start(
1236                 struct damon_sysfs_kdamond *k    1292                 struct damon_sysfs_kdamond *kdamond, bool total_bytes_only)
1237 {                                                1293 {
1238         struct damon_ctx *ctx = kdamond->damo    1294         struct damon_ctx *ctx = kdamond->damon_ctx;
1239                                                  1295 
1240         if (!ctx)                                1296         if (!ctx)
1241                 return -EINVAL;                  1297                 return -EINVAL;
1242         return damon_sysfs_schemes_update_reg    1298         return damon_sysfs_schemes_update_regions_start(
1243                         kdamond->contexts->co    1299                         kdamond->contexts->contexts_arr[0]->schemes, ctx,
1244                         total_bytes_only);       1300                         total_bytes_only);
1245 }                                                1301 }
1246                                                  1302 
1247 static int damon_sysfs_upd_schemes_regions_st    1303 static int damon_sysfs_upd_schemes_regions_stop(
1248                 struct damon_sysfs_kdamond *k    1304                 struct damon_sysfs_kdamond *kdamond)
1249 {                                                1305 {
1250         struct damon_ctx *ctx = kdamond->damo    1306         struct damon_ctx *ctx = kdamond->damon_ctx;
1251                                                  1307 
1252         if (!ctx)                                1308         if (!ctx)
1253                 return -EINVAL;                  1309                 return -EINVAL;
1254         return damon_sysfs_schemes_update_reg    1310         return damon_sysfs_schemes_update_regions_stop(ctx);
1255 }                                                1311 }
1256                                                  1312 
1257 static int damon_sysfs_clear_schemes_regions(    1313 static int damon_sysfs_clear_schemes_regions(
1258                 struct damon_sysfs_kdamond *k    1314                 struct damon_sysfs_kdamond *kdamond)
1259 {                                                1315 {
1260         struct damon_ctx *ctx = kdamond->damo    1316         struct damon_ctx *ctx = kdamond->damon_ctx;
1261                                                  1317 
1262         if (!ctx)                                1318         if (!ctx)
1263                 return -EINVAL;                  1319                 return -EINVAL;
1264         return damon_sysfs_schemes_clear_regi    1320         return damon_sysfs_schemes_clear_regions(
1265                         kdamond->contexts->co    1321                         kdamond->contexts->contexts_arr[0]->schemes, ctx);
1266 }                                                1322 }
1267                                                  1323 
1268 static inline bool damon_sysfs_kdamond_runnin    1324 static inline bool damon_sysfs_kdamond_running(
1269                 struct damon_sysfs_kdamond *k    1325                 struct damon_sysfs_kdamond *kdamond)
1270 {                                                1326 {
1271         return kdamond->damon_ctx &&             1327         return kdamond->damon_ctx &&
1272                 damon_sysfs_ctx_running(kdamo    1328                 damon_sysfs_ctx_running(kdamond->damon_ctx);
1273 }                                                1329 }
1274                                                  1330 
1275 static int damon_sysfs_apply_inputs(struct da    1331 static int damon_sysfs_apply_inputs(struct damon_ctx *ctx,
1276                 struct damon_sysfs_context *s    1332                 struct damon_sysfs_context *sys_ctx)
1277 {                                                1333 {
1278         int err;                                 1334         int err;
1279                                                  1335 
1280         err = damon_select_ops(ctx, sys_ctx->    1336         err = damon_select_ops(ctx, sys_ctx->ops_id);
1281         if (err)                                 1337         if (err)
1282                 return err;                      1338                 return err;
1283         err = damon_sysfs_set_attrs(ctx, sys_    1339         err = damon_sysfs_set_attrs(ctx, sys_ctx->attrs);
1284         if (err)                                 1340         if (err)
1285                 return err;                      1341                 return err;
1286         err = damon_sysfs_add_targets(ctx, sy !! 1342         err = damon_sysfs_set_targets(ctx, sys_ctx->targets);
1287         if (err)                                 1343         if (err)
1288                 return err;                      1344                 return err;
1289         return damon_sysfs_add_schemes(ctx, s !! 1345         return damon_sysfs_set_schemes(ctx, sys_ctx->schemes);
1290 }                                                1346 }
1291                                                  1347 
1292 static struct damon_ctx *damon_sysfs_build_ct << 
1293                 struct damon_sysfs_context *s << 
1294                                               << 
1295 /*                                               1348 /*
1296  * damon_sysfs_commit_input() - Commit user i    1349  * damon_sysfs_commit_input() - Commit user inputs to a running kdamond.
1297  * @kdamond:    The kobject wrapper for the a    1350  * @kdamond:    The kobject wrapper for the associated kdamond.
1298  *                                               1351  *
1299  * If the sysfs input is wrong, the kdamond w    1352  * If the sysfs input is wrong, the kdamond will be terminated.
1300  */                                              1353  */
1301 static int damon_sysfs_commit_input(struct da    1354 static int damon_sysfs_commit_input(struct damon_sysfs_kdamond *kdamond)
1302 {                                                1355 {
1303         struct damon_ctx *param_ctx;          << 
1304         int err;                              << 
1305                                               << 
1306         if (!damon_sysfs_kdamond_running(kdam    1356         if (!damon_sysfs_kdamond_running(kdamond))
1307                 return -EINVAL;                  1357                 return -EINVAL;
1308         /* TODO: Support multiple contexts pe    1358         /* TODO: Support multiple contexts per kdamond */
1309         if (kdamond->contexts->nr != 1)          1359         if (kdamond->contexts->nr != 1)
1310                 return -EINVAL;                  1360                 return -EINVAL;
1311                                                  1361 
1312         param_ctx = damon_sysfs_build_ctx(kda !! 1362         return damon_sysfs_apply_inputs(kdamond->damon_ctx,
1313         if (IS_ERR(param_ctx))                !! 1363                         kdamond->contexts->contexts_arr[0]);
1314                 return PTR_ERR(param_ctx);    << 
1315         err = damon_commit_ctx(kdamond->damon << 
1316         damon_sysfs_destroy_targets(param_ctx << 
1317         damon_destroy_ctx(param_ctx);         << 
1318         return err;                           << 
1319 }                                                1364 }
1320                                                  1365 
1321 static int damon_sysfs_commit_schemes_quota_g    1366 static int damon_sysfs_commit_schemes_quota_goals(
1322                 struct damon_sysfs_kdamond *s    1367                 struct damon_sysfs_kdamond *sysfs_kdamond)
1323 {                                                1368 {
1324         struct damon_ctx *ctx;                   1369         struct damon_ctx *ctx;
1325         struct damon_sysfs_context *sysfs_ctx    1370         struct damon_sysfs_context *sysfs_ctx;
1326                                                  1371 
1327         if (!damon_sysfs_kdamond_running(sysf    1372         if (!damon_sysfs_kdamond_running(sysfs_kdamond))
1328                 return -EINVAL;                  1373                 return -EINVAL;
1329         /* TODO: Support multiple contexts pe    1374         /* TODO: Support multiple contexts per kdamond */
1330         if (sysfs_kdamond->contexts->nr != 1)    1375         if (sysfs_kdamond->contexts->nr != 1)
1331                 return -EINVAL;                  1376                 return -EINVAL;
1332                                                  1377 
1333         ctx = sysfs_kdamond->damon_ctx;          1378         ctx = sysfs_kdamond->damon_ctx;
1334         sysfs_ctx = sysfs_kdamond->contexts->    1379         sysfs_ctx = sysfs_kdamond->contexts->contexts_arr[0];
1335         return damos_sysfs_set_quota_scores(s    1380         return damos_sysfs_set_quota_scores(sysfs_ctx->schemes, ctx);
1336 }                                                1381 }
1337                                                  1382 
1338 /*                                               1383 /*
1339  * damon_sysfs_upd_schemes_effective_quotas()    1384  * damon_sysfs_upd_schemes_effective_quotas() - Update schemes effective quotas
1340  * sysfs files.                                  1385  * sysfs files.
1341  * @kdamond:    The kobject wrapper that asso    1386  * @kdamond:    The kobject wrapper that associated to the kdamond thread.
1342  *                                               1387  *
1343  * This function reads the schemes' effective    1388  * This function reads the schemes' effective quotas of specific kdamond and
1344  * update the related values for sysfs files.    1389  * update the related values for sysfs files.  This function should be called
1345  * from DAMON callbacks while holding ``damon    1390  * from DAMON callbacks while holding ``damon_syfs_lock``, to safely access the
1346  * DAMON contexts-internal data and DAMON sys    1391  * DAMON contexts-internal data and DAMON sysfs variables.
1347  */                                              1392  */
1348 static int damon_sysfs_upd_schemes_effective_    1393 static int damon_sysfs_upd_schemes_effective_quotas(
1349                 struct damon_sysfs_kdamond *k    1394                 struct damon_sysfs_kdamond *kdamond)
1350 {                                                1395 {
1351         struct damon_ctx *ctx = kdamond->damo    1396         struct damon_ctx *ctx = kdamond->damon_ctx;
1352                                                  1397 
1353         if (!ctx)                                1398         if (!ctx)
1354                 return -EINVAL;                  1399                 return -EINVAL;
1355         damos_sysfs_update_effective_quotas(     1400         damos_sysfs_update_effective_quotas(
1356                         kdamond->contexts->co    1401                         kdamond->contexts->contexts_arr[0]->schemes, ctx);
1357         return 0;                                1402         return 0;
1358 }                                                1403 }
1359                                                  1404 
1360                                                  1405 
1361 /*                                               1406 /*
1362  * damon_sysfs_cmd_request_callback() - DAMON    1407  * damon_sysfs_cmd_request_callback() - DAMON callback for handling requests.
1363  * @c:          The DAMON context of the call    1408  * @c:          The DAMON context of the callback.
1364  * @active:     Whether @c is not deactivated    1409  * @active:     Whether @c is not deactivated due to watermarks.
1365  * @after_aggr: Whether this is called from a    1410  * @after_aggr: Whether this is called from after_aggregation() callback.
1366  *                                               1411  *
1367  * This function is periodically called back     1412  * This function is periodically called back from the kdamond thread for @c.
1368  * Then, it checks if there is a waiting DAMO    1413  * Then, it checks if there is a waiting DAMON sysfs request and handles it.
1369  */                                              1414  */
1370 static int damon_sysfs_cmd_request_callback(s    1415 static int damon_sysfs_cmd_request_callback(struct damon_ctx *c, bool active,
1371                 bool after_aggregation)          1416                 bool after_aggregation)
1372 {                                                1417 {
1373         struct damon_sysfs_kdamond *kdamond;     1418         struct damon_sysfs_kdamond *kdamond;
1374         bool total_bytes_only = false;           1419         bool total_bytes_only = false;
1375         int err = 0;                             1420         int err = 0;
1376                                                  1421 
1377         /* avoid deadlock due to concurrent s    1422         /* avoid deadlock due to concurrent state_store('off') */
1378         if (!damon_sysfs_schemes_regions_upda    1423         if (!damon_sysfs_schemes_regions_updating &&
1379                         !mutex_trylock(&damon    1424                         !mutex_trylock(&damon_sysfs_lock))
1380                 return 0;                        1425                 return 0;
1381         kdamond = damon_sysfs_cmd_request.kda    1426         kdamond = damon_sysfs_cmd_request.kdamond;
1382         if (!kdamond || kdamond->damon_ctx !=    1427         if (!kdamond || kdamond->damon_ctx != c)
1383                 goto out;                        1428                 goto out;
1384         switch (damon_sysfs_cmd_request.cmd)     1429         switch (damon_sysfs_cmd_request.cmd) {
1385         case DAMON_SYSFS_CMD_UPDATE_SCHEMES_S    1430         case DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS:
1386                 err = damon_sysfs_upd_schemes    1431                 err = damon_sysfs_upd_schemes_stats(kdamond);
1387                 break;                           1432                 break;
1388         case DAMON_SYSFS_CMD_COMMIT:             1433         case DAMON_SYSFS_CMD_COMMIT:
1389                 if (!after_aggregation)          1434                 if (!after_aggregation)
1390                         goto out;                1435                         goto out;
1391                 err = damon_sysfs_commit_inpu    1436                 err = damon_sysfs_commit_input(kdamond);
1392                 break;                           1437                 break;
1393         case DAMON_SYSFS_CMD_COMMIT_SCHEMES_Q    1438         case DAMON_SYSFS_CMD_COMMIT_SCHEMES_QUOTA_GOALS:
1394                 err = damon_sysfs_commit_sche    1439                 err = damon_sysfs_commit_schemes_quota_goals(kdamond);
1395                 break;                           1440                 break;
1396         case DAMON_SYSFS_CMD_UPDATE_SCHEMES_T    1441         case DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_BYTES:
1397                 total_bytes_only = true;         1442                 total_bytes_only = true;
1398                 fallthrough;                     1443                 fallthrough;
1399         case DAMON_SYSFS_CMD_UPDATE_SCHEMES_T    1444         case DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_REGIONS:
1400                 if (!damon_sysfs_schemes_regi    1445                 if (!damon_sysfs_schemes_regions_updating) {
1401                         err = damon_sysfs_upd    1446                         err = damon_sysfs_upd_schemes_regions_start(kdamond,
1402                                         total    1447                                         total_bytes_only);
1403                         if (!err) {              1448                         if (!err) {
1404                                 damon_sysfs_s    1449                                 damon_sysfs_schemes_regions_updating = true;
1405                                 goto keep_loc    1450                                 goto keep_lock_out;
1406                         }                        1451                         }
1407                 } else {                         1452                 } else {
1408                         damos_sysfs_mark_fini    1453                         damos_sysfs_mark_finished_regions_updates(c);
1409                         /*                       1454                         /*
1410                          * Continue regions u    1455                          * Continue regions updating if DAMON is till
1411                          * active and the upd    1456                          * active and the update for all schemes is not
1412                          * finished.             1457                          * finished.
1413                          */                      1458                          */
1414                         if (active && !damos_    1459                         if (active && !damos_sysfs_regions_upd_done())
1415                                 goto keep_loc    1460                                 goto keep_lock_out;
1416                         err = damon_sysfs_upd    1461                         err = damon_sysfs_upd_schemes_regions_stop(kdamond);
1417                         damon_sysfs_schemes_r    1462                         damon_sysfs_schemes_regions_updating = false;
1418                 }                                1463                 }
1419                 break;                           1464                 break;
1420         case DAMON_SYSFS_CMD_CLEAR_SCHEMES_TR    1465         case DAMON_SYSFS_CMD_CLEAR_SCHEMES_TRIED_REGIONS:
1421                 err = damon_sysfs_clear_schem    1466                 err = damon_sysfs_clear_schemes_regions(kdamond);
1422                 break;                           1467                 break;
1423         case DAMON_SYSFS_CMD_UPDATE_SCHEMES_E    1468         case DAMON_SYSFS_CMD_UPDATE_SCHEMES_EFFECTIVE_QUOTAS:
1424                 err = damon_sysfs_upd_schemes    1469                 err = damon_sysfs_upd_schemes_effective_quotas(kdamond);
1425                 break;                           1470                 break;
1426         default:                                 1471         default:
1427                 break;                           1472                 break;
1428         }                                        1473         }
1429         /* Mark the request as invalid now. *    1474         /* Mark the request as invalid now. */
1430         damon_sysfs_cmd_request.kdamond = NUL    1475         damon_sysfs_cmd_request.kdamond = NULL;
1431 out:                                             1476 out:
1432         if (!damon_sysfs_schemes_regions_upda    1477         if (!damon_sysfs_schemes_regions_updating)
1433                 mutex_unlock(&damon_sysfs_loc    1478                 mutex_unlock(&damon_sysfs_lock);
1434 keep_lock_out:                                   1479 keep_lock_out:
1435         return err;                              1480         return err;
1436 }                                                1481 }
1437                                                  1482 
1438 static int damon_sysfs_after_wmarks_check(str    1483 static int damon_sysfs_after_wmarks_check(struct damon_ctx *c)
1439 {                                                1484 {
1440         /*                                       1485         /*
1441          * after_wmarks_check() is called bac    1486          * after_wmarks_check() is called back while the context is deactivated
1442          * by watermarks.                        1487          * by watermarks.
1443          */                                      1488          */
1444         return damon_sysfs_cmd_request_callba    1489         return damon_sysfs_cmd_request_callback(c, false, false);
1445 }                                                1490 }
1446                                                  1491 
1447 static int damon_sysfs_after_sampling(struct     1492 static int damon_sysfs_after_sampling(struct damon_ctx *c)
1448 {                                                1493 {
1449         /*                                       1494         /*
1450          * after_sampling() is called back on    1495          * after_sampling() is called back only while the context is not
1451          * deactivated by watermarks.            1496          * deactivated by watermarks.
1452          */                                      1497          */
1453         return damon_sysfs_cmd_request_callba    1498         return damon_sysfs_cmd_request_callback(c, true, false);
1454 }                                                1499 }
1455                                                  1500 
1456 static int damon_sysfs_after_aggregation(stru    1501 static int damon_sysfs_after_aggregation(struct damon_ctx *c)
1457 {                                                1502 {
1458         /*                                       1503         /*
1459          * after_aggregation() is called back    1504          * after_aggregation() is called back only while the context is not
1460          * deactivated by watermarks.            1505          * deactivated by watermarks.
1461          */                                      1506          */
1462         return damon_sysfs_cmd_request_callba    1507         return damon_sysfs_cmd_request_callback(c, true, true);
1463 }                                                1508 }
1464                                                  1509 
1465 static struct damon_ctx *damon_sysfs_build_ct    1510 static struct damon_ctx *damon_sysfs_build_ctx(
1466                 struct damon_sysfs_context *s    1511                 struct damon_sysfs_context *sys_ctx)
1467 {                                                1512 {
1468         struct damon_ctx *ctx = damon_new_ctx    1513         struct damon_ctx *ctx = damon_new_ctx();
1469         int err;                                 1514         int err;
1470                                                  1515 
1471         if (!ctx)                                1516         if (!ctx)
1472                 return ERR_PTR(-ENOMEM);         1517                 return ERR_PTR(-ENOMEM);
1473                                                  1518 
1474         err = damon_sysfs_apply_inputs(ctx, s    1519         err = damon_sysfs_apply_inputs(ctx, sys_ctx);
1475         if (err) {                               1520         if (err) {
1476                 damon_destroy_ctx(ctx);          1521                 damon_destroy_ctx(ctx);
1477                 return ERR_PTR(err);             1522                 return ERR_PTR(err);
1478         }                                        1523         }
1479                                                  1524 
1480         ctx->callback.after_wmarks_check = da    1525         ctx->callback.after_wmarks_check = damon_sysfs_after_wmarks_check;
1481         ctx->callback.after_sampling = damon_    1526         ctx->callback.after_sampling = damon_sysfs_after_sampling;
1482         ctx->callback.after_aggregation = dam    1527         ctx->callback.after_aggregation = damon_sysfs_after_aggregation;
1483         ctx->callback.before_terminate = damo    1528         ctx->callback.before_terminate = damon_sysfs_before_terminate;
1484         return ctx;                              1529         return ctx;
1485 }                                                1530 }
1486                                                  1531 
1487 static int damon_sysfs_turn_damon_on(struct d    1532 static int damon_sysfs_turn_damon_on(struct damon_sysfs_kdamond *kdamond)
1488 {                                                1533 {
1489         struct damon_ctx *ctx;                   1534         struct damon_ctx *ctx;
1490         int err;                                 1535         int err;
1491                                                  1536 
1492         if (damon_sysfs_kdamond_running(kdamo    1537         if (damon_sysfs_kdamond_running(kdamond))
1493                 return -EBUSY;                   1538                 return -EBUSY;
1494         if (damon_sysfs_cmd_request.kdamond =    1539         if (damon_sysfs_cmd_request.kdamond == kdamond)
1495                 return -EBUSY;                   1540                 return -EBUSY;
1496         /* TODO: support multiple contexts pe    1541         /* TODO: support multiple contexts per kdamond */
1497         if (kdamond->contexts->nr != 1)          1542         if (kdamond->contexts->nr != 1)
1498                 return -EINVAL;                  1543                 return -EINVAL;
1499                                                  1544 
1500         if (kdamond->damon_ctx)                  1545         if (kdamond->damon_ctx)
1501                 damon_destroy_ctx(kdamond->da    1546                 damon_destroy_ctx(kdamond->damon_ctx);
1502         kdamond->damon_ctx = NULL;               1547         kdamond->damon_ctx = NULL;
1503                                                  1548 
1504         ctx = damon_sysfs_build_ctx(kdamond->    1549         ctx = damon_sysfs_build_ctx(kdamond->contexts->contexts_arr[0]);
1505         if (IS_ERR(ctx))                         1550         if (IS_ERR(ctx))
1506                 return PTR_ERR(ctx);             1551                 return PTR_ERR(ctx);
1507         err = damon_start(&ctx, 1, false);       1552         err = damon_start(&ctx, 1, false);
1508         if (err) {                               1553         if (err) {
1509                 damon_destroy_ctx(ctx);          1554                 damon_destroy_ctx(ctx);
1510                 return err;                      1555                 return err;
1511         }                                        1556         }
1512         kdamond->damon_ctx = ctx;                1557         kdamond->damon_ctx = ctx;
1513         return err;                              1558         return err;
1514 }                                                1559 }
1515                                                  1560 
1516 static int damon_sysfs_turn_damon_off(struct     1561 static int damon_sysfs_turn_damon_off(struct damon_sysfs_kdamond *kdamond)
1517 {                                                1562 {
1518         if (!kdamond->damon_ctx)                 1563         if (!kdamond->damon_ctx)
1519                 return -EINVAL;                  1564                 return -EINVAL;
1520         return damon_stop(&kdamond->damon_ctx    1565         return damon_stop(&kdamond->damon_ctx, 1);
1521         /*                                       1566         /*
1522          * To allow users show final monitori    1567          * To allow users show final monitoring results of already turned-off
1523          * DAMON, we free kdamond->damon_ctx     1568          * DAMON, we free kdamond->damon_ctx in next
1524          * damon_sysfs_turn_damon_on(), or kd    1569          * damon_sysfs_turn_damon_on(), or kdamonds_nr_store()
1525          */                                      1570          */
1526 }                                                1571 }
1527                                                  1572 
1528 /*                                               1573 /*
1529  * damon_sysfs_handle_cmd() - Handle a comman    1574  * damon_sysfs_handle_cmd() - Handle a command for a specific kdamond.
1530  * @cmd:        The command to handle.           1575  * @cmd:        The command to handle.
1531  * @kdamond:    The kobject wrapper for the a    1576  * @kdamond:    The kobject wrapper for the associated kdamond.
1532  *                                               1577  *
1533  * This function handles a DAMON sysfs comman    1578  * This function handles a DAMON sysfs command for a kdamond.  For commands
1534  * that need to access running DAMON context-    1579  * that need to access running DAMON context-internal data, it requests
1535  * handling of the command to the DAMON callb    1580  * handling of the command to the DAMON callback
1536  * (@damon_sysfs_cmd_request_callback()) and     1581  * (@damon_sysfs_cmd_request_callback()) and wait until it is properly handled,
1537  * or the context is completed.                  1582  * or the context is completed.
1538  *                                               1583  *
1539  * Return: 0 on success, negative error code     1584  * Return: 0 on success, negative error code otherwise.
1540  */                                              1585  */
1541 static int damon_sysfs_handle_cmd(enum damon_    1586 static int damon_sysfs_handle_cmd(enum damon_sysfs_cmd cmd,
1542                 struct damon_sysfs_kdamond *k    1587                 struct damon_sysfs_kdamond *kdamond)
1543 {                                                1588 {
1544         bool need_wait = true;                   1589         bool need_wait = true;
1545                                                  1590 
1546         /* Handle commands that doesn't acces    1591         /* Handle commands that doesn't access DAMON context-internal data */
1547         switch (cmd) {                           1592         switch (cmd) {
1548         case DAMON_SYSFS_CMD_ON:                 1593         case DAMON_SYSFS_CMD_ON:
1549                 return damon_sysfs_turn_damon    1594                 return damon_sysfs_turn_damon_on(kdamond);
1550         case DAMON_SYSFS_CMD_OFF:                1595         case DAMON_SYSFS_CMD_OFF:
1551                 return damon_sysfs_turn_damon    1596                 return damon_sysfs_turn_damon_off(kdamond);
1552         default:                                 1597         default:
1553                 break;                           1598                 break;
1554         }                                        1599         }
1555                                                  1600 
1556         /* Pass the command to DAMON callback    1601         /* Pass the command to DAMON callback for safe DAMON context access */
1557         if (damon_sysfs_cmd_request.kdamond)     1602         if (damon_sysfs_cmd_request.kdamond)
1558                 return -EBUSY;                   1603                 return -EBUSY;
1559         if (!damon_sysfs_kdamond_running(kdam    1604         if (!damon_sysfs_kdamond_running(kdamond))
1560                 return -EINVAL;                  1605                 return -EINVAL;
1561         damon_sysfs_cmd_request.cmd = cmd;       1606         damon_sysfs_cmd_request.cmd = cmd;
1562         damon_sysfs_cmd_request.kdamond = kda    1607         damon_sysfs_cmd_request.kdamond = kdamond;
1563                                                  1608 
1564         /*                                       1609         /*
1565          * wait until damon_sysfs_cmd_request    1610          * wait until damon_sysfs_cmd_request_callback() handles the request
1566          * from kdamond context                  1611          * from kdamond context
1567          */                                      1612          */
1568         mutex_unlock(&damon_sysfs_lock);         1613         mutex_unlock(&damon_sysfs_lock);
1569         while (need_wait) {                      1614         while (need_wait) {
1570                 schedule_timeout_idle(msecs_t    1615                 schedule_timeout_idle(msecs_to_jiffies(100));
1571                 if (!mutex_trylock(&damon_sys    1616                 if (!mutex_trylock(&damon_sysfs_lock))
1572                         continue;                1617                         continue;
1573                 if (!damon_sysfs_cmd_request.    1618                 if (!damon_sysfs_cmd_request.kdamond) {
1574                         /* damon_sysfs_cmd_re    1619                         /* damon_sysfs_cmd_request_callback() handled */
1575                         need_wait = false;       1620                         need_wait = false;
1576                 } else if (!damon_sysfs_kdamo    1621                 } else if (!damon_sysfs_kdamond_running(kdamond)) {
1577                         /* kdamond has alread    1622                         /* kdamond has already finished */
1578                         need_wait = false;       1623                         need_wait = false;
1579                         damon_sysfs_cmd_reque    1624                         damon_sysfs_cmd_request.kdamond = NULL;
1580                 }                                1625                 }
1581                 mutex_unlock(&damon_sysfs_loc    1626                 mutex_unlock(&damon_sysfs_lock);
1582         }                                        1627         }
1583         mutex_lock(&damon_sysfs_lock);           1628         mutex_lock(&damon_sysfs_lock);
1584         return 0;                                1629         return 0;
1585 }                                                1630 }
1586                                                  1631 
1587 static ssize_t state_store(struct kobject *ko    1632 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
1588                 const char *buf, size_t count    1633                 const char *buf, size_t count)
1589 {                                                1634 {
1590         struct damon_sysfs_kdamond *kdamond =    1635         struct damon_sysfs_kdamond *kdamond = container_of(kobj,
1591                         struct damon_sysfs_kd    1636                         struct damon_sysfs_kdamond, kobj);
1592         enum damon_sysfs_cmd cmd;                1637         enum damon_sysfs_cmd cmd;
1593         ssize_t ret = -EINVAL;                   1638         ssize_t ret = -EINVAL;
1594                                                  1639 
1595         if (!mutex_trylock(&damon_sysfs_lock)    1640         if (!mutex_trylock(&damon_sysfs_lock))
1596                 return -EBUSY;                   1641                 return -EBUSY;
1597         for (cmd = 0; cmd < NR_DAMON_SYSFS_CM    1642         for (cmd = 0; cmd < NR_DAMON_SYSFS_CMDS; cmd++) {
1598                 if (sysfs_streq(buf, damon_sy    1643                 if (sysfs_streq(buf, damon_sysfs_cmd_strs[cmd])) {
1599                         ret = damon_sysfs_han    1644                         ret = damon_sysfs_handle_cmd(cmd, kdamond);
1600                         break;                   1645                         break;
1601                 }                                1646                 }
1602         }                                        1647         }
1603         mutex_unlock(&damon_sysfs_lock);         1648         mutex_unlock(&damon_sysfs_lock);
1604         if (!ret)                                1649         if (!ret)
1605                 ret = count;                     1650                 ret = count;
1606         return ret;                              1651         return ret;
1607 }                                                1652 }
1608                                                  1653 
1609 static ssize_t pid_show(struct kobject *kobj,    1654 static ssize_t pid_show(struct kobject *kobj,
1610                 struct kobj_attribute *attr,     1655                 struct kobj_attribute *attr, char *buf)
1611 {                                                1656 {
1612         struct damon_sysfs_kdamond *kdamond =    1657         struct damon_sysfs_kdamond *kdamond = container_of(kobj,
1613                         struct damon_sysfs_kd    1658                         struct damon_sysfs_kdamond, kobj);
1614         struct damon_ctx *ctx;                   1659         struct damon_ctx *ctx;
1615         int pid = -1;                            1660         int pid = -1;
1616                                                  1661 
1617         if (!mutex_trylock(&damon_sysfs_lock)    1662         if (!mutex_trylock(&damon_sysfs_lock))
1618                 return -EBUSY;                   1663                 return -EBUSY;
1619         ctx = kdamond->damon_ctx;                1664         ctx = kdamond->damon_ctx;
1620         if (!ctx)                                1665         if (!ctx)
1621                 goto out;                        1666                 goto out;
1622                                                  1667 
1623         mutex_lock(&ctx->kdamond_lock);          1668         mutex_lock(&ctx->kdamond_lock);
1624         if (ctx->kdamond)                        1669         if (ctx->kdamond)
1625                 pid = ctx->kdamond->pid;         1670                 pid = ctx->kdamond->pid;
1626         mutex_unlock(&ctx->kdamond_lock);        1671         mutex_unlock(&ctx->kdamond_lock);
1627 out:                                             1672 out:
1628         mutex_unlock(&damon_sysfs_lock);         1673         mutex_unlock(&damon_sysfs_lock);
1629         return sysfs_emit(buf, "%d\n", pid);     1674         return sysfs_emit(buf, "%d\n", pid);
1630 }                                                1675 }
1631                                                  1676 
1632 static void damon_sysfs_kdamond_release(struc    1677 static void damon_sysfs_kdamond_release(struct kobject *kobj)
1633 {                                                1678 {
1634         struct damon_sysfs_kdamond *kdamond =    1679         struct damon_sysfs_kdamond *kdamond = container_of(kobj,
1635                         struct damon_sysfs_kd    1680                         struct damon_sysfs_kdamond, kobj);
1636                                                  1681 
1637         if (kdamond->damon_ctx)                  1682         if (kdamond->damon_ctx)
1638                 damon_destroy_ctx(kdamond->da    1683                 damon_destroy_ctx(kdamond->damon_ctx);
1639         kfree(kdamond);                          1684         kfree(kdamond);
1640 }                                                1685 }
1641                                                  1686 
1642 static struct kobj_attribute damon_sysfs_kdam    1687 static struct kobj_attribute damon_sysfs_kdamond_state_attr =
1643                 __ATTR_RW_MODE(state, 0600);     1688                 __ATTR_RW_MODE(state, 0600);
1644                                                  1689 
1645 static struct kobj_attribute damon_sysfs_kdam    1690 static struct kobj_attribute damon_sysfs_kdamond_pid_attr =
1646                 __ATTR_RO_MODE(pid, 0400);       1691                 __ATTR_RO_MODE(pid, 0400);
1647                                                  1692 
1648 static struct attribute *damon_sysfs_kdamond_    1693 static struct attribute *damon_sysfs_kdamond_attrs[] = {
1649         &damon_sysfs_kdamond_state_attr.attr,    1694         &damon_sysfs_kdamond_state_attr.attr,
1650         &damon_sysfs_kdamond_pid_attr.attr,      1695         &damon_sysfs_kdamond_pid_attr.attr,
1651         NULL,                                    1696         NULL,
1652 };                                               1697 };
1653 ATTRIBUTE_GROUPS(damon_sysfs_kdamond);           1698 ATTRIBUTE_GROUPS(damon_sysfs_kdamond);
1654                                                  1699 
1655 static const struct kobj_type damon_sysfs_kda    1700 static const struct kobj_type damon_sysfs_kdamond_ktype = {
1656         .release = damon_sysfs_kdamond_releas    1701         .release = damon_sysfs_kdamond_release,
1657         .sysfs_ops = &kobj_sysfs_ops,            1702         .sysfs_ops = &kobj_sysfs_ops,
1658         .default_groups = damon_sysfs_kdamond    1703         .default_groups = damon_sysfs_kdamond_groups,
1659 };                                               1704 };
1660                                                  1705 
1661 /*                                               1706 /*
1662  * kdamonds directory                            1707  * kdamonds directory
1663  */                                              1708  */
1664                                                  1709 
1665 struct damon_sysfs_kdamonds {                    1710 struct damon_sysfs_kdamonds {
1666         struct kobject kobj;                     1711         struct kobject kobj;
1667         struct damon_sysfs_kdamond **kdamonds    1712         struct damon_sysfs_kdamond **kdamonds_arr;
1668         int nr;                                  1713         int nr;
1669 };                                               1714 };
1670                                                  1715 
1671 static struct damon_sysfs_kdamonds *damon_sys    1716 static struct damon_sysfs_kdamonds *damon_sysfs_kdamonds_alloc(void)
1672 {                                                1717 {
1673         return kzalloc(sizeof(struct damon_sy    1718         return kzalloc(sizeof(struct damon_sysfs_kdamonds), GFP_KERNEL);
1674 }                                                1719 }
1675                                                  1720 
1676 static void damon_sysfs_kdamonds_rm_dirs(stru    1721 static void damon_sysfs_kdamonds_rm_dirs(struct damon_sysfs_kdamonds *kdamonds)
1677 {                                                1722 {
1678         struct damon_sysfs_kdamond **kdamonds    1723         struct damon_sysfs_kdamond **kdamonds_arr = kdamonds->kdamonds_arr;
1679         int i;                                   1724         int i;
1680                                                  1725 
1681         for (i = 0; i < kdamonds->nr; i++) {     1726         for (i = 0; i < kdamonds->nr; i++) {
1682                 damon_sysfs_kdamond_rm_dirs(k    1727                 damon_sysfs_kdamond_rm_dirs(kdamonds_arr[i]);
1683                 kobject_put(&kdamonds_arr[i]-    1728                 kobject_put(&kdamonds_arr[i]->kobj);
1684         }                                        1729         }
1685         kdamonds->nr = 0;                        1730         kdamonds->nr = 0;
1686         kfree(kdamonds_arr);                     1731         kfree(kdamonds_arr);
1687         kdamonds->kdamonds_arr = NULL;           1732         kdamonds->kdamonds_arr = NULL;
1688 }                                                1733 }
1689                                                  1734 
1690 static bool damon_sysfs_kdamonds_busy(struct     1735 static bool damon_sysfs_kdamonds_busy(struct damon_sysfs_kdamond **kdamonds,
1691                 int nr_kdamonds)                 1736                 int nr_kdamonds)
1692 {                                                1737 {
1693         int i;                                   1738         int i;
1694                                                  1739 
1695         for (i = 0; i < nr_kdamonds; i++) {      1740         for (i = 0; i < nr_kdamonds; i++) {
1696                 if (damon_sysfs_kdamond_runni    1741                 if (damon_sysfs_kdamond_running(kdamonds[i]) ||
1697                     damon_sysfs_cmd_request.k    1742                     damon_sysfs_cmd_request.kdamond == kdamonds[i])
1698                         return true;             1743                         return true;
1699         }                                        1744         }
1700                                                  1745 
1701         return false;                            1746         return false;
1702 }                                                1747 }
1703                                                  1748 
1704 static int damon_sysfs_kdamonds_add_dirs(stru    1749 static int damon_sysfs_kdamonds_add_dirs(struct damon_sysfs_kdamonds *kdamonds,
1705                 int nr_kdamonds)                 1750                 int nr_kdamonds)
1706 {                                                1751 {
1707         struct damon_sysfs_kdamond **kdamonds    1752         struct damon_sysfs_kdamond **kdamonds_arr, *kdamond;
1708         int err, i;                              1753         int err, i;
1709                                                  1754 
1710         if (damon_sysfs_kdamonds_busy(kdamond    1755         if (damon_sysfs_kdamonds_busy(kdamonds->kdamonds_arr, kdamonds->nr))
1711                 return -EBUSY;                   1756                 return -EBUSY;
1712                                                  1757 
1713         damon_sysfs_kdamonds_rm_dirs(kdamonds    1758         damon_sysfs_kdamonds_rm_dirs(kdamonds);
1714         if (!nr_kdamonds)                        1759         if (!nr_kdamonds)
1715                 return 0;                        1760                 return 0;
1716                                                  1761 
1717         kdamonds_arr = kmalloc_array(nr_kdamo    1762         kdamonds_arr = kmalloc_array(nr_kdamonds, sizeof(*kdamonds_arr),
1718                         GFP_KERNEL | __GFP_NO    1763                         GFP_KERNEL | __GFP_NOWARN);
1719         if (!kdamonds_arr)                       1764         if (!kdamonds_arr)
1720                 return -ENOMEM;                  1765                 return -ENOMEM;
1721         kdamonds->kdamonds_arr = kdamonds_arr    1766         kdamonds->kdamonds_arr = kdamonds_arr;
1722                                                  1767 
1723         for (i = 0; i < nr_kdamonds; i++) {      1768         for (i = 0; i < nr_kdamonds; i++) {
1724                 kdamond = damon_sysfs_kdamond    1769                 kdamond = damon_sysfs_kdamond_alloc();
1725                 if (!kdamond) {                  1770                 if (!kdamond) {
1726                         damon_sysfs_kdamonds_    1771                         damon_sysfs_kdamonds_rm_dirs(kdamonds);
1727                         return -ENOMEM;          1772                         return -ENOMEM;
1728                 }                                1773                 }
1729                                                  1774 
1730                 err = kobject_init_and_add(&k    1775                 err = kobject_init_and_add(&kdamond->kobj,
1731                                 &damon_sysfs_    1776                                 &damon_sysfs_kdamond_ktype, &kdamonds->kobj,
1732                                 "%d", i);        1777                                 "%d", i);
1733                 if (err)                         1778                 if (err)
1734                         goto out;                1779                         goto out;
1735                                                  1780 
1736                 err = damon_sysfs_kdamond_add    1781                 err = damon_sysfs_kdamond_add_dirs(kdamond);
1737                 if (err)                         1782                 if (err)
1738                         goto out;                1783                         goto out;
1739                                                  1784 
1740                 kdamonds_arr[i] = kdamond;       1785                 kdamonds_arr[i] = kdamond;
1741                 kdamonds->nr++;                  1786                 kdamonds->nr++;
1742         }                                        1787         }
1743         return 0;                                1788         return 0;
1744                                                  1789 
1745 out:                                             1790 out:
1746         damon_sysfs_kdamonds_rm_dirs(kdamonds    1791         damon_sysfs_kdamonds_rm_dirs(kdamonds);
1747         kobject_put(&kdamond->kobj);             1792         kobject_put(&kdamond->kobj);
1748         return err;                              1793         return err;
1749 }                                                1794 }
1750                                                  1795 
1751 static ssize_t nr_kdamonds_show(struct kobjec    1796 static ssize_t nr_kdamonds_show(struct kobject *kobj,
1752                 struct kobj_attribute *attr,     1797                 struct kobj_attribute *attr, char *buf)
1753 {                                                1798 {
1754         struct damon_sysfs_kdamonds *kdamonds    1799         struct damon_sysfs_kdamonds *kdamonds = container_of(kobj,
1755                         struct damon_sysfs_kd    1800                         struct damon_sysfs_kdamonds, kobj);
1756                                                  1801 
1757         return sysfs_emit(buf, "%d\n", kdamon    1802         return sysfs_emit(buf, "%d\n", kdamonds->nr);
1758 }                                                1803 }
1759                                                  1804 
1760 static ssize_t nr_kdamonds_store(struct kobje    1805 static ssize_t nr_kdamonds_store(struct kobject *kobj,
1761                 struct kobj_attribute *attr,     1806                 struct kobj_attribute *attr, const char *buf, size_t count)
1762 {                                                1807 {
1763         struct damon_sysfs_kdamonds *kdamonds    1808         struct damon_sysfs_kdamonds *kdamonds;
1764         int nr, err;                             1809         int nr, err;
1765                                                  1810 
1766         err = kstrtoint(buf, 0, &nr);            1811         err = kstrtoint(buf, 0, &nr);
1767         if (err)                                 1812         if (err)
1768                 return err;                      1813                 return err;
1769         if (nr < 0)                              1814         if (nr < 0)
1770                 return -EINVAL;                  1815                 return -EINVAL;
1771                                                  1816 
1772         kdamonds = container_of(kobj, struct     1817         kdamonds = container_of(kobj, struct damon_sysfs_kdamonds, kobj);
1773                                                  1818 
1774         if (!mutex_trylock(&damon_sysfs_lock)    1819         if (!mutex_trylock(&damon_sysfs_lock))
1775                 return -EBUSY;                   1820                 return -EBUSY;
1776         err = damon_sysfs_kdamonds_add_dirs(k    1821         err = damon_sysfs_kdamonds_add_dirs(kdamonds, nr);
1777         mutex_unlock(&damon_sysfs_lock);         1822         mutex_unlock(&damon_sysfs_lock);
1778         if (err)                                 1823         if (err)
1779                 return err;                      1824                 return err;
1780                                                  1825 
1781         return count;                            1826         return count;
1782 }                                                1827 }
1783                                                  1828 
1784 static void damon_sysfs_kdamonds_release(stru    1829 static void damon_sysfs_kdamonds_release(struct kobject *kobj)
1785 {                                                1830 {
1786         kfree(container_of(kobj, struct damon    1831         kfree(container_of(kobj, struct damon_sysfs_kdamonds, kobj));
1787 }                                                1832 }
1788                                                  1833 
1789 static struct kobj_attribute damon_sysfs_kdam    1834 static struct kobj_attribute damon_sysfs_kdamonds_nr_attr =
1790                 __ATTR_RW_MODE(nr_kdamonds, 0    1835                 __ATTR_RW_MODE(nr_kdamonds, 0600);
1791                                                  1836 
1792 static struct attribute *damon_sysfs_kdamonds    1837 static struct attribute *damon_sysfs_kdamonds_attrs[] = {
1793         &damon_sysfs_kdamonds_nr_attr.attr,      1838         &damon_sysfs_kdamonds_nr_attr.attr,
1794         NULL,                                    1839         NULL,
1795 };                                               1840 };
1796 ATTRIBUTE_GROUPS(damon_sysfs_kdamonds);          1841 ATTRIBUTE_GROUPS(damon_sysfs_kdamonds);
1797                                                  1842 
1798 static const struct kobj_type damon_sysfs_kda    1843 static const struct kobj_type damon_sysfs_kdamonds_ktype = {
1799         .release = damon_sysfs_kdamonds_relea    1844         .release = damon_sysfs_kdamonds_release,
1800         .sysfs_ops = &kobj_sysfs_ops,            1845         .sysfs_ops = &kobj_sysfs_ops,
1801         .default_groups = damon_sysfs_kdamond    1846         .default_groups = damon_sysfs_kdamonds_groups,
1802 };                                               1847 };
1803                                                  1848 
1804 /*                                               1849 /*
1805  * damon user interface directory                1850  * damon user interface directory
1806  */                                              1851  */
1807                                                  1852 
1808 struct damon_sysfs_ui_dir {                      1853 struct damon_sysfs_ui_dir {
1809         struct kobject kobj;                     1854         struct kobject kobj;
1810         struct damon_sysfs_kdamonds *kdamonds    1855         struct damon_sysfs_kdamonds *kdamonds;
1811 };                                               1856 };
1812                                                  1857 
1813 static struct damon_sysfs_ui_dir *damon_sysfs    1858 static struct damon_sysfs_ui_dir *damon_sysfs_ui_dir_alloc(void)
1814 {                                                1859 {
1815         return kzalloc(sizeof(struct damon_sy    1860         return kzalloc(sizeof(struct damon_sysfs_ui_dir), GFP_KERNEL);
1816 }                                                1861 }
1817                                                  1862 
1818 static int damon_sysfs_ui_dir_add_dirs(struct    1863 static int damon_sysfs_ui_dir_add_dirs(struct damon_sysfs_ui_dir *ui_dir)
1819 {                                                1864 {
1820         struct damon_sysfs_kdamonds *kdamonds    1865         struct damon_sysfs_kdamonds *kdamonds;
1821         int err;                                 1866         int err;
1822                                                  1867 
1823         kdamonds = damon_sysfs_kdamonds_alloc    1868         kdamonds = damon_sysfs_kdamonds_alloc();
1824         if (!kdamonds)                           1869         if (!kdamonds)
1825                 return -ENOMEM;                  1870                 return -ENOMEM;
1826                                                  1871 
1827         err = kobject_init_and_add(&kdamonds-    1872         err = kobject_init_and_add(&kdamonds->kobj,
1828                         &damon_sysfs_kdamonds    1873                         &damon_sysfs_kdamonds_ktype, &ui_dir->kobj,
1829                         "kdamonds");             1874                         "kdamonds");
1830         if (err) {                               1875         if (err) {
1831                 kobject_put(&kdamonds->kobj);    1876                 kobject_put(&kdamonds->kobj);
1832                 return err;                      1877                 return err;
1833         }                                        1878         }
1834         ui_dir->kdamonds = kdamonds;             1879         ui_dir->kdamonds = kdamonds;
1835         return err;                              1880         return err;
1836 }                                                1881 }
1837                                                  1882 
1838 static void damon_sysfs_ui_dir_release(struct    1883 static void damon_sysfs_ui_dir_release(struct kobject *kobj)
1839 {                                                1884 {
1840         kfree(container_of(kobj, struct damon    1885         kfree(container_of(kobj, struct damon_sysfs_ui_dir, kobj));
1841 }                                                1886 }
1842                                                  1887 
1843 static struct attribute *damon_sysfs_ui_dir_a    1888 static struct attribute *damon_sysfs_ui_dir_attrs[] = {
1844         NULL,                                    1889         NULL,
1845 };                                               1890 };
1846 ATTRIBUTE_GROUPS(damon_sysfs_ui_dir);            1891 ATTRIBUTE_GROUPS(damon_sysfs_ui_dir);
1847                                                  1892 
1848 static const struct kobj_type damon_sysfs_ui_    1893 static const struct kobj_type damon_sysfs_ui_dir_ktype = {
1849         .release = damon_sysfs_ui_dir_release    1894         .release = damon_sysfs_ui_dir_release,
1850         .sysfs_ops = &kobj_sysfs_ops,            1895         .sysfs_ops = &kobj_sysfs_ops,
1851         .default_groups = damon_sysfs_ui_dir_    1896         .default_groups = damon_sysfs_ui_dir_groups,
1852 };                                               1897 };
1853                                                  1898 
1854 static int __init damon_sysfs_init(void)         1899 static int __init damon_sysfs_init(void)
1855 {                                                1900 {
1856         struct kobject *damon_sysfs_root;        1901         struct kobject *damon_sysfs_root;
1857         struct damon_sysfs_ui_dir *admin;        1902         struct damon_sysfs_ui_dir *admin;
1858         int err;                                 1903         int err;
1859                                                  1904 
1860         damon_sysfs_root = kobject_create_and    1905         damon_sysfs_root = kobject_create_and_add("damon", mm_kobj);
1861         if (!damon_sysfs_root)                   1906         if (!damon_sysfs_root)
1862                 return -ENOMEM;                  1907                 return -ENOMEM;
1863                                                  1908 
1864         admin = damon_sysfs_ui_dir_alloc();      1909         admin = damon_sysfs_ui_dir_alloc();
1865         if (!admin) {                            1910         if (!admin) {
1866                 kobject_put(damon_sysfs_root)    1911                 kobject_put(damon_sysfs_root);
1867                 return -ENOMEM;                  1912                 return -ENOMEM;
1868         }                                        1913         }
1869         err = kobject_init_and_add(&admin->ko    1914         err = kobject_init_and_add(&admin->kobj, &damon_sysfs_ui_dir_ktype,
1870                         damon_sysfs_root, "ad    1915                         damon_sysfs_root, "admin");
1871         if (err)                                 1916         if (err)
1872                 goto out;                        1917                 goto out;
1873         err = damon_sysfs_ui_dir_add_dirs(adm    1918         err = damon_sysfs_ui_dir_add_dirs(admin);
1874         if (err)                                 1919         if (err)
1875                 goto out;                        1920                 goto out;
1876         return 0;                                1921         return 0;
1877                                                  1922 
1878 out:                                             1923 out:
1879         kobject_put(&admin->kobj);               1924         kobject_put(&admin->kobj);
1880         kobject_put(damon_sysfs_root);           1925         kobject_put(damon_sysfs_root);
1881         return err;                              1926         return err;
1882 }                                                1927 }
1883 subsys_initcall(damon_sysfs_init);               1928 subsys_initcall(damon_sysfs_init);
1884                                                  1929 
1885 #include "tests/sysfs-kunit.h"                !! 1930 #include "sysfs-test.h"
1886                                                  1931 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php