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/damon.h> >> 9 #include <linux/kobject.h> 8 #include <linux/pid.h> 10 #include <linux/pid.h> 9 #include <linux/sched.h> 11 #include <linux/sched.h> 10 #include <linux/slab.h> 12 #include <linux/slab.h> 11 13 12 #include "sysfs-common.h" !! 14 static DEFINE_MUTEX(damon_sysfs_lock); >> 15 >> 16 /* >> 17 * unsigned long range directory >> 18 */ >> 19 >> 20 struct damon_sysfs_ul_range { >> 21 struct kobject kobj; >> 22 unsigned long min; >> 23 unsigned long max; >> 24 }; >> 25 >> 26 static struct damon_sysfs_ul_range *damon_sysfs_ul_range_alloc( >> 27 unsigned long min, >> 28 unsigned long max) >> 29 { >> 30 struct damon_sysfs_ul_range *range = kmalloc(sizeof(*range), >> 31 GFP_KERNEL); >> 32 >> 33 if (!range) >> 34 return NULL; >> 35 range->kobj = (struct kobject){}; >> 36 range->min = min; >> 37 range->max = max; >> 38 >> 39 return range; >> 40 } >> 41 >> 42 static ssize_t min_show(struct kobject *kobj, struct kobj_attribute *attr, >> 43 char *buf) >> 44 { >> 45 struct damon_sysfs_ul_range *range = container_of(kobj, >> 46 struct damon_sysfs_ul_range, kobj); >> 47 >> 48 return sysfs_emit(buf, "%lu\n", range->min); >> 49 } >> 50 >> 51 static ssize_t min_store(struct kobject *kobj, struct kobj_attribute *attr, >> 52 const char *buf, size_t count) >> 53 { >> 54 struct damon_sysfs_ul_range *range = container_of(kobj, >> 55 struct damon_sysfs_ul_range, kobj); >> 56 unsigned long min; >> 57 int err; >> 58 >> 59 err = kstrtoul(buf, 0, &min); >> 60 if (err) >> 61 return -EINVAL; >> 62 >> 63 range->min = min; >> 64 return count; >> 65 } >> 66 >> 67 static ssize_t max_show(struct kobject *kobj, struct kobj_attribute *attr, >> 68 char *buf) >> 69 { >> 70 struct damon_sysfs_ul_range *range = container_of(kobj, >> 71 struct damon_sysfs_ul_range, kobj); >> 72 >> 73 return sysfs_emit(buf, "%lu\n", range->max); >> 74 } >> 75 >> 76 static ssize_t max_store(struct kobject *kobj, struct kobj_attribute *attr, >> 77 const char *buf, size_t count) >> 78 { >> 79 struct damon_sysfs_ul_range *range = container_of(kobj, >> 80 struct damon_sysfs_ul_range, kobj); >> 81 unsigned long max; >> 82 int err; >> 83 >> 84 err = kstrtoul(buf, 0, &max); >> 85 if (err) >> 86 return -EINVAL; >> 87 >> 88 range->max = max; >> 89 return count; >> 90 } >> 91 >> 92 static void damon_sysfs_ul_range_release(struct kobject *kobj) >> 93 { >> 94 kfree(container_of(kobj, struct damon_sysfs_ul_range, kobj)); >> 95 } >> 96 >> 97 static struct kobj_attribute damon_sysfs_ul_range_min_attr = >> 98 __ATTR_RW_MODE(min, 0600); >> 99 >> 100 static struct kobj_attribute damon_sysfs_ul_range_max_attr = >> 101 __ATTR_RW_MODE(max, 0600); >> 102 >> 103 static struct attribute *damon_sysfs_ul_range_attrs[] = { >> 104 &damon_sysfs_ul_range_min_attr.attr, >> 105 &damon_sysfs_ul_range_max_attr.attr, >> 106 NULL, >> 107 }; >> 108 ATTRIBUTE_GROUPS(damon_sysfs_ul_range); >> 109 >> 110 static struct kobj_type damon_sysfs_ul_range_ktype = { >> 111 .release = damon_sysfs_ul_range_release, >> 112 .sysfs_ops = &kobj_sysfs_ops, >> 113 .default_groups = damon_sysfs_ul_range_groups, >> 114 }; >> 115 >> 116 /* >> 117 * schemes/stats directory >> 118 */ >> 119 >> 120 struct damon_sysfs_stats { >> 121 struct kobject kobj; >> 122 unsigned long nr_tried; >> 123 unsigned long sz_tried; >> 124 unsigned long nr_applied; >> 125 unsigned long sz_applied; >> 126 unsigned long qt_exceeds; >> 127 }; >> 128 >> 129 static struct damon_sysfs_stats *damon_sysfs_stats_alloc(void) >> 130 { >> 131 return kzalloc(sizeof(struct damon_sysfs_stats), GFP_KERNEL); >> 132 } >> 133 >> 134 static ssize_t nr_tried_show(struct kobject *kobj, struct kobj_attribute *attr, >> 135 char *buf) >> 136 { >> 137 struct damon_sysfs_stats *stats = container_of(kobj, >> 138 struct damon_sysfs_stats, kobj); >> 139 >> 140 return sysfs_emit(buf, "%lu\n", stats->nr_tried); >> 141 } >> 142 >> 143 static ssize_t sz_tried_show(struct kobject *kobj, struct kobj_attribute *attr, >> 144 char *buf) >> 145 { >> 146 struct damon_sysfs_stats *stats = container_of(kobj, >> 147 struct damon_sysfs_stats, kobj); >> 148 >> 149 return sysfs_emit(buf, "%lu\n", stats->sz_tried); >> 150 } >> 151 >> 152 static ssize_t nr_applied_show(struct kobject *kobj, >> 153 struct kobj_attribute *attr, char *buf) >> 154 { >> 155 struct damon_sysfs_stats *stats = container_of(kobj, >> 156 struct damon_sysfs_stats, kobj); >> 157 >> 158 return sysfs_emit(buf, "%lu\n", stats->nr_applied); >> 159 } >> 160 >> 161 static ssize_t sz_applied_show(struct kobject *kobj, >> 162 struct kobj_attribute *attr, char *buf) >> 163 { >> 164 struct damon_sysfs_stats *stats = container_of(kobj, >> 165 struct damon_sysfs_stats, kobj); >> 166 >> 167 return sysfs_emit(buf, "%lu\n", stats->sz_applied); >> 168 } >> 169 >> 170 static ssize_t qt_exceeds_show(struct kobject *kobj, >> 171 struct kobj_attribute *attr, char *buf) >> 172 { >> 173 struct damon_sysfs_stats *stats = container_of(kobj, >> 174 struct damon_sysfs_stats, kobj); >> 175 >> 176 return sysfs_emit(buf, "%lu\n", stats->qt_exceeds); >> 177 } >> 178 >> 179 static void damon_sysfs_stats_release(struct kobject *kobj) >> 180 { >> 181 kfree(container_of(kobj, struct damon_sysfs_stats, kobj)); >> 182 } >> 183 >> 184 static struct kobj_attribute damon_sysfs_stats_nr_tried_attr = >> 185 __ATTR_RO_MODE(nr_tried, 0400); >> 186 >> 187 static struct kobj_attribute damon_sysfs_stats_sz_tried_attr = >> 188 __ATTR_RO_MODE(sz_tried, 0400); >> 189 >> 190 static struct kobj_attribute damon_sysfs_stats_nr_applied_attr = >> 191 __ATTR_RO_MODE(nr_applied, 0400); >> 192 >> 193 static struct kobj_attribute damon_sysfs_stats_sz_applied_attr = >> 194 __ATTR_RO_MODE(sz_applied, 0400); >> 195 >> 196 static struct kobj_attribute damon_sysfs_stats_qt_exceeds_attr = >> 197 __ATTR_RO_MODE(qt_exceeds, 0400); >> 198 >> 199 static struct attribute *damon_sysfs_stats_attrs[] = { >> 200 &damon_sysfs_stats_nr_tried_attr.attr, >> 201 &damon_sysfs_stats_sz_tried_attr.attr, >> 202 &damon_sysfs_stats_nr_applied_attr.attr, >> 203 &damon_sysfs_stats_sz_applied_attr.attr, >> 204 &damon_sysfs_stats_qt_exceeds_attr.attr, >> 205 NULL, >> 206 }; >> 207 ATTRIBUTE_GROUPS(damon_sysfs_stats); >> 208 >> 209 static struct kobj_type damon_sysfs_stats_ktype = { >> 210 .release = damon_sysfs_stats_release, >> 211 .sysfs_ops = &kobj_sysfs_ops, >> 212 .default_groups = damon_sysfs_stats_groups, >> 213 }; >> 214 >> 215 /* >> 216 * watermarks directory >> 217 */ >> 218 >> 219 struct damon_sysfs_watermarks { >> 220 struct kobject kobj; >> 221 enum damos_wmark_metric metric; >> 222 unsigned long interval_us; >> 223 unsigned long high; >> 224 unsigned long mid; >> 225 unsigned long low; >> 226 }; >> 227 >> 228 static struct damon_sysfs_watermarks *damon_sysfs_watermarks_alloc( >> 229 enum damos_wmark_metric metric, unsigned long interval_us, >> 230 unsigned long high, unsigned long mid, unsigned long low) >> 231 { >> 232 struct damon_sysfs_watermarks *watermarks = kmalloc( >> 233 sizeof(*watermarks), GFP_KERNEL); >> 234 >> 235 if (!watermarks) >> 236 return NULL; >> 237 watermarks->kobj = (struct kobject){}; >> 238 watermarks->metric = metric; >> 239 watermarks->interval_us = interval_us; >> 240 watermarks->high = high; >> 241 watermarks->mid = mid; >> 242 watermarks->low = low; >> 243 return watermarks; >> 244 } >> 245 >> 246 /* Should match with enum damos_wmark_metric */ >> 247 static const char * const damon_sysfs_wmark_metric_strs[] = { >> 248 "none", >> 249 "free_mem_rate", >> 250 }; >> 251 >> 252 static ssize_t metric_show(struct kobject *kobj, struct kobj_attribute *attr, >> 253 char *buf) >> 254 { >> 255 struct damon_sysfs_watermarks *watermarks = container_of(kobj, >> 256 struct damon_sysfs_watermarks, kobj); >> 257 >> 258 return sysfs_emit(buf, "%s\n", >> 259 damon_sysfs_wmark_metric_strs[watermarks->metric]); >> 260 } >> 261 >> 262 static ssize_t metric_store(struct kobject *kobj, struct kobj_attribute *attr, >> 263 const char *buf, size_t count) >> 264 { >> 265 struct damon_sysfs_watermarks *watermarks = container_of(kobj, >> 266 struct damon_sysfs_watermarks, kobj); >> 267 enum damos_wmark_metric metric; >> 268 >> 269 for (metric = 0; metric < NR_DAMOS_WMARK_METRICS; metric++) { >> 270 if (sysfs_streq(buf, damon_sysfs_wmark_metric_strs[metric])) { >> 271 watermarks->metric = metric; >> 272 return count; >> 273 } >> 274 } >> 275 return -EINVAL; >> 276 } >> 277 >> 278 static ssize_t interval_us_show(struct kobject *kobj, >> 279 struct kobj_attribute *attr, char *buf) >> 280 { >> 281 struct damon_sysfs_watermarks *watermarks = container_of(kobj, >> 282 struct damon_sysfs_watermarks, kobj); >> 283 >> 284 return sysfs_emit(buf, "%lu\n", watermarks->interval_us); >> 285 } >> 286 >> 287 static ssize_t interval_us_store(struct kobject *kobj, >> 288 struct kobj_attribute *attr, const char *buf, size_t count) >> 289 { >> 290 struct damon_sysfs_watermarks *watermarks = container_of(kobj, >> 291 struct damon_sysfs_watermarks, kobj); >> 292 int err = kstrtoul(buf, 0, &watermarks->interval_us); >> 293 >> 294 if (err) >> 295 return -EINVAL; >> 296 return count; >> 297 } >> 298 >> 299 static ssize_t high_show(struct kobject *kobj, >> 300 struct kobj_attribute *attr, char *buf) >> 301 { >> 302 struct damon_sysfs_watermarks *watermarks = container_of(kobj, >> 303 struct damon_sysfs_watermarks, kobj); >> 304 >> 305 return sysfs_emit(buf, "%lu\n", watermarks->high); >> 306 } >> 307 >> 308 static ssize_t high_store(struct kobject *kobj, >> 309 struct kobj_attribute *attr, const char *buf, size_t count) >> 310 { >> 311 struct damon_sysfs_watermarks *watermarks = container_of(kobj, >> 312 struct damon_sysfs_watermarks, kobj); >> 313 int err = kstrtoul(buf, 0, &watermarks->high); >> 314 >> 315 if (err) >> 316 return -EINVAL; >> 317 return count; >> 318 } >> 319 >> 320 static ssize_t mid_show(struct kobject *kobj, >> 321 struct kobj_attribute *attr, char *buf) >> 322 { >> 323 struct damon_sysfs_watermarks *watermarks = container_of(kobj, >> 324 struct damon_sysfs_watermarks, kobj); >> 325 >> 326 return sysfs_emit(buf, "%lu\n", watermarks->mid); >> 327 } >> 328 >> 329 static ssize_t mid_store(struct kobject *kobj, >> 330 struct kobj_attribute *attr, const char *buf, size_t count) >> 331 { >> 332 struct damon_sysfs_watermarks *watermarks = container_of(kobj, >> 333 struct damon_sysfs_watermarks, kobj); >> 334 int err = kstrtoul(buf, 0, &watermarks->mid); >> 335 >> 336 if (err) >> 337 return -EINVAL; >> 338 return count; >> 339 } >> 340 >> 341 static ssize_t low_show(struct kobject *kobj, >> 342 struct kobj_attribute *attr, char *buf) >> 343 { >> 344 struct damon_sysfs_watermarks *watermarks = container_of(kobj, >> 345 struct damon_sysfs_watermarks, kobj); >> 346 >> 347 return sysfs_emit(buf, "%lu\n", watermarks->low); >> 348 } >> 349 >> 350 static ssize_t low_store(struct kobject *kobj, >> 351 struct kobj_attribute *attr, const char *buf, size_t count) >> 352 { >> 353 struct damon_sysfs_watermarks *watermarks = container_of(kobj, >> 354 struct damon_sysfs_watermarks, kobj); >> 355 int err = kstrtoul(buf, 0, &watermarks->low); >> 356 >> 357 if (err) >> 358 return -EINVAL; >> 359 return count; >> 360 } >> 361 >> 362 static void damon_sysfs_watermarks_release(struct kobject *kobj) >> 363 { >> 364 kfree(container_of(kobj, struct damon_sysfs_watermarks, kobj)); >> 365 } >> 366 >> 367 static struct kobj_attribute damon_sysfs_watermarks_metric_attr = >> 368 __ATTR_RW_MODE(metric, 0600); >> 369 >> 370 static struct kobj_attribute damon_sysfs_watermarks_interval_us_attr = >> 371 __ATTR_RW_MODE(interval_us, 0600); >> 372 >> 373 static struct kobj_attribute damon_sysfs_watermarks_high_attr = >> 374 __ATTR_RW_MODE(high, 0600); >> 375 >> 376 static struct kobj_attribute damon_sysfs_watermarks_mid_attr = >> 377 __ATTR_RW_MODE(mid, 0600); >> 378 >> 379 static struct kobj_attribute damon_sysfs_watermarks_low_attr = >> 380 __ATTR_RW_MODE(low, 0600); >> 381 >> 382 static struct attribute *damon_sysfs_watermarks_attrs[] = { >> 383 &damon_sysfs_watermarks_metric_attr.attr, >> 384 &damon_sysfs_watermarks_interval_us_attr.attr, >> 385 &damon_sysfs_watermarks_high_attr.attr, >> 386 &damon_sysfs_watermarks_mid_attr.attr, >> 387 &damon_sysfs_watermarks_low_attr.attr, >> 388 NULL, >> 389 }; >> 390 ATTRIBUTE_GROUPS(damon_sysfs_watermarks); >> 391 >> 392 static struct kobj_type damon_sysfs_watermarks_ktype = { >> 393 .release = damon_sysfs_watermarks_release, >> 394 .sysfs_ops = &kobj_sysfs_ops, >> 395 .default_groups = damon_sysfs_watermarks_groups, >> 396 }; >> 397 >> 398 /* >> 399 * scheme/weights directory >> 400 */ >> 401 >> 402 struct damon_sysfs_weights { >> 403 struct kobject kobj; >> 404 unsigned int sz; >> 405 unsigned int nr_accesses; >> 406 unsigned int age; >> 407 }; >> 408 >> 409 static struct damon_sysfs_weights *damon_sysfs_weights_alloc(unsigned int sz, >> 410 unsigned int nr_accesses, unsigned int age) >> 411 { >> 412 struct damon_sysfs_weights *weights = kmalloc(sizeof(*weights), >> 413 GFP_KERNEL); >> 414 >> 415 if (!weights) >> 416 return NULL; >> 417 weights->kobj = (struct kobject){}; >> 418 weights->sz = sz; >> 419 weights->nr_accesses = nr_accesses; >> 420 weights->age = age; >> 421 return weights; >> 422 } >> 423 >> 424 static ssize_t sz_permil_show(struct kobject *kobj, >> 425 struct kobj_attribute *attr, char *buf) >> 426 { >> 427 struct damon_sysfs_weights *weights = container_of(kobj, >> 428 struct damon_sysfs_weights, kobj); >> 429 >> 430 return sysfs_emit(buf, "%u\n", weights->sz); >> 431 } >> 432 >> 433 static ssize_t sz_permil_store(struct kobject *kobj, >> 434 struct kobj_attribute *attr, const char *buf, size_t count) >> 435 { >> 436 struct damon_sysfs_weights *weights = container_of(kobj, >> 437 struct damon_sysfs_weights, kobj); >> 438 int err = kstrtouint(buf, 0, &weights->sz); >> 439 >> 440 if (err) >> 441 return -EINVAL; >> 442 return count; >> 443 } >> 444 >> 445 static ssize_t nr_accesses_permil_show(struct kobject *kobj, >> 446 struct kobj_attribute *attr, char *buf) >> 447 { >> 448 struct damon_sysfs_weights *weights = container_of(kobj, >> 449 struct damon_sysfs_weights, kobj); >> 450 >> 451 return sysfs_emit(buf, "%u\n", weights->nr_accesses); >> 452 } >> 453 >> 454 static ssize_t nr_accesses_permil_store(struct kobject *kobj, >> 455 struct kobj_attribute *attr, const char *buf, size_t count) >> 456 { >> 457 struct damon_sysfs_weights *weights = container_of(kobj, >> 458 struct damon_sysfs_weights, kobj); >> 459 int err = kstrtouint(buf, 0, &weights->nr_accesses); >> 460 >> 461 if (err) >> 462 return -EINVAL; >> 463 return count; >> 464 } >> 465 >> 466 static ssize_t age_permil_show(struct kobject *kobj, >> 467 struct kobj_attribute *attr, char *buf) >> 468 { >> 469 struct damon_sysfs_weights *weights = container_of(kobj, >> 470 struct damon_sysfs_weights, kobj); >> 471 >> 472 return sysfs_emit(buf, "%u\n", weights->age); >> 473 } >> 474 >> 475 static ssize_t age_permil_store(struct kobject *kobj, >> 476 struct kobj_attribute *attr, const char *buf, size_t count) >> 477 { >> 478 struct damon_sysfs_weights *weights = container_of(kobj, >> 479 struct damon_sysfs_weights, kobj); >> 480 int err = kstrtouint(buf, 0, &weights->age); >> 481 >> 482 if (err) >> 483 return -EINVAL; >> 484 return count; >> 485 } >> 486 >> 487 static void damon_sysfs_weights_release(struct kobject *kobj) >> 488 { >> 489 kfree(container_of(kobj, struct damon_sysfs_weights, kobj)); >> 490 } >> 491 >> 492 static struct kobj_attribute damon_sysfs_weights_sz_attr = >> 493 __ATTR_RW_MODE(sz_permil, 0600); >> 494 >> 495 static struct kobj_attribute damon_sysfs_weights_nr_accesses_attr = >> 496 __ATTR_RW_MODE(nr_accesses_permil, 0600); >> 497 >> 498 static struct kobj_attribute damon_sysfs_weights_age_attr = >> 499 __ATTR_RW_MODE(age_permil, 0600); >> 500 >> 501 static struct attribute *damon_sysfs_weights_attrs[] = { >> 502 &damon_sysfs_weights_sz_attr.attr, >> 503 &damon_sysfs_weights_nr_accesses_attr.attr, >> 504 &damon_sysfs_weights_age_attr.attr, >> 505 NULL, >> 506 }; >> 507 ATTRIBUTE_GROUPS(damon_sysfs_weights); >> 508 >> 509 static struct kobj_type damon_sysfs_weights_ktype = { >> 510 .release = damon_sysfs_weights_release, >> 511 .sysfs_ops = &kobj_sysfs_ops, >> 512 .default_groups = damon_sysfs_weights_groups, >> 513 }; >> 514 >> 515 /* >> 516 * quotas directory >> 517 */ >> 518 >> 519 struct damon_sysfs_quotas { >> 520 struct kobject kobj; >> 521 struct damon_sysfs_weights *weights; >> 522 unsigned long ms; >> 523 unsigned long sz; >> 524 unsigned long reset_interval_ms; >> 525 }; >> 526 >> 527 static struct damon_sysfs_quotas *damon_sysfs_quotas_alloc(void) >> 528 { >> 529 return kzalloc(sizeof(struct damon_sysfs_quotas), GFP_KERNEL); >> 530 } >> 531 >> 532 static int damon_sysfs_quotas_add_dirs(struct damon_sysfs_quotas *quotas) >> 533 { >> 534 struct damon_sysfs_weights *weights; >> 535 int err; >> 536 >> 537 weights = damon_sysfs_weights_alloc(0, 0, 0); >> 538 if (!weights) >> 539 return -ENOMEM; >> 540 >> 541 err = kobject_init_and_add(&weights->kobj, &damon_sysfs_weights_ktype, >> 542 "as->kobj, "weights"); >> 543 if (err) >> 544 kobject_put(&weights->kobj); >> 545 else >> 546 quotas->weights = weights; >> 547 return err; >> 548 } >> 549 >> 550 static void damon_sysfs_quotas_rm_dirs(struct damon_sysfs_quotas *quotas) >> 551 { >> 552 kobject_put("as->weights->kobj); >> 553 } >> 554 >> 555 static ssize_t ms_show(struct kobject *kobj, struct kobj_attribute *attr, >> 556 char *buf) >> 557 { >> 558 struct damon_sysfs_quotas *quotas = container_of(kobj, >> 559 struct damon_sysfs_quotas, kobj); >> 560 >> 561 return sysfs_emit(buf, "%lu\n", quotas->ms); >> 562 } >> 563 >> 564 static ssize_t ms_store(struct kobject *kobj, struct kobj_attribute *attr, >> 565 const char *buf, size_t count) >> 566 { >> 567 struct damon_sysfs_quotas *quotas = container_of(kobj, >> 568 struct damon_sysfs_quotas, kobj); >> 569 int err = kstrtoul(buf, 0, "as->ms); >> 570 >> 571 if (err) >> 572 return -EINVAL; >> 573 return count; >> 574 } >> 575 >> 576 static ssize_t bytes_show(struct kobject *kobj, struct kobj_attribute *attr, >> 577 char *buf) >> 578 { >> 579 struct damon_sysfs_quotas *quotas = container_of(kobj, >> 580 struct damon_sysfs_quotas, kobj); >> 581 >> 582 return sysfs_emit(buf, "%lu\n", quotas->sz); >> 583 } >> 584 >> 585 static ssize_t bytes_store(struct kobject *kobj, >> 586 struct kobj_attribute *attr, const char *buf, size_t count) >> 587 { >> 588 struct damon_sysfs_quotas *quotas = container_of(kobj, >> 589 struct damon_sysfs_quotas, kobj); >> 590 int err = kstrtoul(buf, 0, "as->sz); >> 591 >> 592 if (err) >> 593 return -EINVAL; >> 594 return count; >> 595 } >> 596 >> 597 static ssize_t reset_interval_ms_show(struct kobject *kobj, >> 598 struct kobj_attribute *attr, char *buf) >> 599 { >> 600 struct damon_sysfs_quotas *quotas = container_of(kobj, >> 601 struct damon_sysfs_quotas, kobj); >> 602 >> 603 return sysfs_emit(buf, "%lu\n", quotas->reset_interval_ms); >> 604 } >> 605 >> 606 static ssize_t reset_interval_ms_store(struct kobject *kobj, >> 607 struct kobj_attribute *attr, const char *buf, size_t count) >> 608 { >> 609 struct damon_sysfs_quotas *quotas = container_of(kobj, >> 610 struct damon_sysfs_quotas, kobj); >> 611 int err = kstrtoul(buf, 0, "as->reset_interval_ms); >> 612 >> 613 if (err) >> 614 return -EINVAL; >> 615 return count; >> 616 } >> 617 >> 618 static void damon_sysfs_quotas_release(struct kobject *kobj) >> 619 { >> 620 kfree(container_of(kobj, struct damon_sysfs_quotas, kobj)); >> 621 } >> 622 >> 623 static struct kobj_attribute damon_sysfs_quotas_ms_attr = >> 624 __ATTR_RW_MODE(ms, 0600); >> 625 >> 626 static struct kobj_attribute damon_sysfs_quotas_sz_attr = >> 627 __ATTR_RW_MODE(bytes, 0600); >> 628 >> 629 static struct kobj_attribute damon_sysfs_quotas_reset_interval_ms_attr = >> 630 __ATTR_RW_MODE(reset_interval_ms, 0600); >> 631 >> 632 static struct attribute *damon_sysfs_quotas_attrs[] = { >> 633 &damon_sysfs_quotas_ms_attr.attr, >> 634 &damon_sysfs_quotas_sz_attr.attr, >> 635 &damon_sysfs_quotas_reset_interval_ms_attr.attr, >> 636 NULL, >> 637 }; >> 638 ATTRIBUTE_GROUPS(damon_sysfs_quotas); >> 639 >> 640 static struct kobj_type damon_sysfs_quotas_ktype = { >> 641 .release = damon_sysfs_quotas_release, >> 642 .sysfs_ops = &kobj_sysfs_ops, >> 643 .default_groups = damon_sysfs_quotas_groups, >> 644 }; >> 645 >> 646 /* >> 647 * access_pattern directory >> 648 */ >> 649 >> 650 struct damon_sysfs_access_pattern { >> 651 struct kobject kobj; >> 652 struct damon_sysfs_ul_range *sz; >> 653 struct damon_sysfs_ul_range *nr_accesses; >> 654 struct damon_sysfs_ul_range *age; >> 655 }; >> 656 >> 657 static >> 658 struct damon_sysfs_access_pattern *damon_sysfs_access_pattern_alloc(void) >> 659 { >> 660 struct damon_sysfs_access_pattern *access_pattern = >> 661 kmalloc(sizeof(*access_pattern), GFP_KERNEL); >> 662 >> 663 if (!access_pattern) >> 664 return NULL; >> 665 access_pattern->kobj = (struct kobject){}; >> 666 return access_pattern; >> 667 } >> 668 >> 669 static int damon_sysfs_access_pattern_add_range_dir( >> 670 struct damon_sysfs_access_pattern *access_pattern, >> 671 struct damon_sysfs_ul_range **range_dir_ptr, >> 672 char *name) >> 673 { >> 674 struct damon_sysfs_ul_range *range = damon_sysfs_ul_range_alloc(0, 0); >> 675 int err; >> 676 >> 677 if (!range) >> 678 return -ENOMEM; >> 679 err = kobject_init_and_add(&range->kobj, &damon_sysfs_ul_range_ktype, >> 680 &access_pattern->kobj, name); >> 681 if (err) >> 682 kobject_put(&range->kobj); >> 683 else >> 684 *range_dir_ptr = range; >> 685 return err; >> 686 } >> 687 >> 688 static int damon_sysfs_access_pattern_add_dirs( >> 689 struct damon_sysfs_access_pattern *access_pattern) >> 690 { >> 691 int err; >> 692 >> 693 err = damon_sysfs_access_pattern_add_range_dir(access_pattern, >> 694 &access_pattern->sz, "sz"); >> 695 if (err) >> 696 goto put_sz_out; >> 697 >> 698 err = damon_sysfs_access_pattern_add_range_dir(access_pattern, >> 699 &access_pattern->nr_accesses, "nr_accesses"); >> 700 if (err) >> 701 goto put_nr_accesses_sz_out; >> 702 >> 703 err = damon_sysfs_access_pattern_add_range_dir(access_pattern, >> 704 &access_pattern->age, "age"); >> 705 if (err) >> 706 goto put_age_nr_accesses_sz_out; >> 707 return 0; >> 708 >> 709 put_age_nr_accesses_sz_out: >> 710 kobject_put(&access_pattern->age->kobj); >> 711 access_pattern->age = NULL; >> 712 put_nr_accesses_sz_out: >> 713 kobject_put(&access_pattern->nr_accesses->kobj); >> 714 access_pattern->nr_accesses = NULL; >> 715 put_sz_out: >> 716 kobject_put(&access_pattern->sz->kobj); >> 717 access_pattern->sz = NULL; >> 718 return err; >> 719 } >> 720 >> 721 static void damon_sysfs_access_pattern_rm_dirs( >> 722 struct damon_sysfs_access_pattern *access_pattern) >> 723 { >> 724 kobject_put(&access_pattern->sz->kobj); >> 725 kobject_put(&access_pattern->nr_accesses->kobj); >> 726 kobject_put(&access_pattern->age->kobj); >> 727 } >> 728 >> 729 static void damon_sysfs_access_pattern_release(struct kobject *kobj) >> 730 { >> 731 kfree(container_of(kobj, struct damon_sysfs_access_pattern, kobj)); >> 732 } >> 733 >> 734 static struct attribute *damon_sysfs_access_pattern_attrs[] = { >> 735 NULL, >> 736 }; >> 737 ATTRIBUTE_GROUPS(damon_sysfs_access_pattern); >> 738 >> 739 static struct kobj_type damon_sysfs_access_pattern_ktype = { >> 740 .release = damon_sysfs_access_pattern_release, >> 741 .sysfs_ops = &kobj_sysfs_ops, >> 742 .default_groups = damon_sysfs_access_pattern_groups, >> 743 }; >> 744 >> 745 /* >> 746 * scheme directory >> 747 */ >> 748 >> 749 struct damon_sysfs_scheme { >> 750 struct kobject kobj; >> 751 enum damos_action action; >> 752 struct damon_sysfs_access_pattern *access_pattern; >> 753 struct damon_sysfs_quotas *quotas; >> 754 struct damon_sysfs_watermarks *watermarks; >> 755 struct damon_sysfs_stats *stats; >> 756 }; >> 757 >> 758 /* This should match with enum damos_action */ >> 759 static const char * const damon_sysfs_damos_action_strs[] = { >> 760 "willneed", >> 761 "cold", >> 762 "pageout", >> 763 "hugepage", >> 764 "nohugepage", >> 765 "lru_prio", >> 766 "lru_deprio", >> 767 "stat", >> 768 }; >> 769 >> 770 static struct damon_sysfs_scheme *damon_sysfs_scheme_alloc( >> 771 enum damos_action action) >> 772 { >> 773 struct damon_sysfs_scheme *scheme = kmalloc(sizeof(*scheme), >> 774 GFP_KERNEL); >> 775 >> 776 if (!scheme) >> 777 return NULL; >> 778 scheme->kobj = (struct kobject){}; >> 779 scheme->action = action; >> 780 return scheme; >> 781 } >> 782 >> 783 static int damon_sysfs_scheme_set_access_pattern( >> 784 struct damon_sysfs_scheme *scheme) >> 785 { >> 786 struct damon_sysfs_access_pattern *access_pattern; >> 787 int err; >> 788 >> 789 access_pattern = damon_sysfs_access_pattern_alloc(); >> 790 if (!access_pattern) >> 791 return -ENOMEM; >> 792 err = kobject_init_and_add(&access_pattern->kobj, >> 793 &damon_sysfs_access_pattern_ktype, &scheme->kobj, >> 794 "access_pattern"); >> 795 if (err) >> 796 goto out; >> 797 err = damon_sysfs_access_pattern_add_dirs(access_pattern); >> 798 if (err) >> 799 goto out; >> 800 scheme->access_pattern = access_pattern; >> 801 return 0; >> 802 >> 803 out: >> 804 kobject_put(&access_pattern->kobj); >> 805 return err; >> 806 } >> 807 >> 808 static int damon_sysfs_scheme_set_quotas(struct damon_sysfs_scheme *scheme) >> 809 { >> 810 struct damon_sysfs_quotas *quotas = damon_sysfs_quotas_alloc(); >> 811 int err; >> 812 >> 813 if (!quotas) >> 814 return -ENOMEM; >> 815 err = kobject_init_and_add("as->kobj, &damon_sysfs_quotas_ktype, >> 816 &scheme->kobj, "quotas"); >> 817 if (err) >> 818 goto out; >> 819 err = damon_sysfs_quotas_add_dirs(quotas); >> 820 if (err) >> 821 goto out; >> 822 scheme->quotas = quotas; >> 823 return 0; >> 824 >> 825 out: >> 826 kobject_put("as->kobj); >> 827 return err; >> 828 } >> 829 >> 830 static int damon_sysfs_scheme_set_watermarks(struct damon_sysfs_scheme *scheme) >> 831 { >> 832 struct damon_sysfs_watermarks *watermarks = >> 833 damon_sysfs_watermarks_alloc(DAMOS_WMARK_NONE, 0, 0, 0, 0); >> 834 int err; >> 835 >> 836 if (!watermarks) >> 837 return -ENOMEM; >> 838 err = kobject_init_and_add(&watermarks->kobj, >> 839 &damon_sysfs_watermarks_ktype, &scheme->kobj, >> 840 "watermarks"); >> 841 if (err) >> 842 kobject_put(&watermarks->kobj); >> 843 else >> 844 scheme->watermarks = watermarks; >> 845 return err; >> 846 } >> 847 >> 848 static int damon_sysfs_scheme_set_stats(struct damon_sysfs_scheme *scheme) >> 849 { >> 850 struct damon_sysfs_stats *stats = damon_sysfs_stats_alloc(); >> 851 int err; >> 852 >> 853 if (!stats) >> 854 return -ENOMEM; >> 855 err = kobject_init_and_add(&stats->kobj, &damon_sysfs_stats_ktype, >> 856 &scheme->kobj, "stats"); >> 857 if (err) >> 858 kobject_put(&stats->kobj); >> 859 else >> 860 scheme->stats = stats; >> 861 return err; >> 862 } >> 863 >> 864 static int damon_sysfs_scheme_add_dirs(struct damon_sysfs_scheme *scheme) >> 865 { >> 866 int err; >> 867 >> 868 err = damon_sysfs_scheme_set_access_pattern(scheme); >> 869 if (err) >> 870 return err; >> 871 err = damon_sysfs_scheme_set_quotas(scheme); >> 872 if (err) >> 873 goto put_access_pattern_out; >> 874 err = damon_sysfs_scheme_set_watermarks(scheme); >> 875 if (err) >> 876 goto put_quotas_access_pattern_out; >> 877 err = damon_sysfs_scheme_set_stats(scheme); >> 878 if (err) >> 879 goto put_watermarks_quotas_access_pattern_out; >> 880 return 0; >> 881 >> 882 put_watermarks_quotas_access_pattern_out: >> 883 kobject_put(&scheme->watermarks->kobj); >> 884 scheme->watermarks = NULL; >> 885 put_quotas_access_pattern_out: >> 886 kobject_put(&scheme->quotas->kobj); >> 887 scheme->quotas = NULL; >> 888 put_access_pattern_out: >> 889 kobject_put(&scheme->access_pattern->kobj); >> 890 scheme->access_pattern = NULL; >> 891 return err; >> 892 } >> 893 >> 894 static void damon_sysfs_scheme_rm_dirs(struct damon_sysfs_scheme *scheme) >> 895 { >> 896 damon_sysfs_access_pattern_rm_dirs(scheme->access_pattern); >> 897 kobject_put(&scheme->access_pattern->kobj); >> 898 damon_sysfs_quotas_rm_dirs(scheme->quotas); >> 899 kobject_put(&scheme->quotas->kobj); >> 900 kobject_put(&scheme->watermarks->kobj); >> 901 kobject_put(&scheme->stats->kobj); >> 902 } >> 903 >> 904 static ssize_t action_show(struct kobject *kobj, struct kobj_attribute *attr, >> 905 char *buf) >> 906 { >> 907 struct damon_sysfs_scheme *scheme = container_of(kobj, >> 908 struct damon_sysfs_scheme, kobj); >> 909 >> 910 return sysfs_emit(buf, "%s\n", >> 911 damon_sysfs_damos_action_strs[scheme->action]); >> 912 } >> 913 >> 914 static ssize_t action_store(struct kobject *kobj, struct kobj_attribute *attr, >> 915 const char *buf, size_t count) >> 916 { >> 917 struct damon_sysfs_scheme *scheme = container_of(kobj, >> 918 struct damon_sysfs_scheme, kobj); >> 919 enum damos_action action; >> 920 >> 921 for (action = 0; action < NR_DAMOS_ACTIONS; action++) { >> 922 if (sysfs_streq(buf, damon_sysfs_damos_action_strs[action])) { >> 923 scheme->action = action; >> 924 return count; >> 925 } >> 926 } >> 927 return -EINVAL; >> 928 } >> 929 >> 930 static void damon_sysfs_scheme_release(struct kobject *kobj) >> 931 { >> 932 kfree(container_of(kobj, struct damon_sysfs_scheme, kobj)); >> 933 } >> 934 >> 935 static struct kobj_attribute damon_sysfs_scheme_action_attr = >> 936 __ATTR_RW_MODE(action, 0600); >> 937 >> 938 static struct attribute *damon_sysfs_scheme_attrs[] = { >> 939 &damon_sysfs_scheme_action_attr.attr, >> 940 NULL, >> 941 }; >> 942 ATTRIBUTE_GROUPS(damon_sysfs_scheme); >> 943 >> 944 static struct kobj_type damon_sysfs_scheme_ktype = { >> 945 .release = damon_sysfs_scheme_release, >> 946 .sysfs_ops = &kobj_sysfs_ops, >> 947 .default_groups = damon_sysfs_scheme_groups, >> 948 }; >> 949 >> 950 /* >> 951 * schemes directory >> 952 */ >> 953 >> 954 struct damon_sysfs_schemes { >> 955 struct kobject kobj; >> 956 struct damon_sysfs_scheme **schemes_arr; >> 957 int nr; >> 958 }; >> 959 >> 960 static struct damon_sysfs_schemes *damon_sysfs_schemes_alloc(void) >> 961 { >> 962 return kzalloc(sizeof(struct damon_sysfs_schemes), GFP_KERNEL); >> 963 } >> 964 >> 965 static void damon_sysfs_schemes_rm_dirs(struct damon_sysfs_schemes *schemes) >> 966 { >> 967 struct damon_sysfs_scheme **schemes_arr = schemes->schemes_arr; >> 968 int i; >> 969 >> 970 for (i = 0; i < schemes->nr; i++) { >> 971 damon_sysfs_scheme_rm_dirs(schemes_arr[i]); >> 972 kobject_put(&schemes_arr[i]->kobj); >> 973 } >> 974 schemes->nr = 0; >> 975 kfree(schemes_arr); >> 976 schemes->schemes_arr = NULL; >> 977 } >> 978 >> 979 static int damon_sysfs_schemes_add_dirs(struct damon_sysfs_schemes *schemes, >> 980 int nr_schemes) >> 981 { >> 982 struct damon_sysfs_scheme **schemes_arr, *scheme; >> 983 int err, i; >> 984 >> 985 damon_sysfs_schemes_rm_dirs(schemes); >> 986 if (!nr_schemes) >> 987 return 0; >> 988 >> 989 schemes_arr = kmalloc_array(nr_schemes, sizeof(*schemes_arr), >> 990 GFP_KERNEL | __GFP_NOWARN); >> 991 if (!schemes_arr) >> 992 return -ENOMEM; >> 993 schemes->schemes_arr = schemes_arr; >> 994 >> 995 for (i = 0; i < nr_schemes; i++) { >> 996 scheme = damon_sysfs_scheme_alloc(DAMOS_STAT); >> 997 if (!scheme) { >> 998 damon_sysfs_schemes_rm_dirs(schemes); >> 999 return -ENOMEM; >> 1000 } >> 1001 >> 1002 err = kobject_init_and_add(&scheme->kobj, >> 1003 &damon_sysfs_scheme_ktype, &schemes->kobj, >> 1004 "%d", i); >> 1005 if (err) >> 1006 goto out; >> 1007 err = damon_sysfs_scheme_add_dirs(scheme); >> 1008 if (err) >> 1009 goto out; >> 1010 >> 1011 schemes_arr[i] = scheme; >> 1012 schemes->nr++; >> 1013 } >> 1014 return 0; >> 1015 >> 1016 out: >> 1017 damon_sysfs_schemes_rm_dirs(schemes); >> 1018 kobject_put(&scheme->kobj); >> 1019 return err; >> 1020 } >> 1021 >> 1022 static ssize_t nr_schemes_show(struct kobject *kobj, >> 1023 struct kobj_attribute *attr, char *buf) >> 1024 { >> 1025 struct damon_sysfs_schemes *schemes = container_of(kobj, >> 1026 struct damon_sysfs_schemes, kobj); >> 1027 >> 1028 return sysfs_emit(buf, "%d\n", schemes->nr); >> 1029 } >> 1030 >> 1031 static ssize_t nr_schemes_store(struct kobject *kobj, >> 1032 struct kobj_attribute *attr, const char *buf, size_t count) >> 1033 { >> 1034 struct damon_sysfs_schemes *schemes = container_of(kobj, >> 1035 struct damon_sysfs_schemes, kobj); >> 1036 int nr, err = kstrtoint(buf, 0, &nr); >> 1037 >> 1038 if (err) >> 1039 return err; >> 1040 if (nr < 0) >> 1041 return -EINVAL; >> 1042 >> 1043 if (!mutex_trylock(&damon_sysfs_lock)) >> 1044 return -EBUSY; >> 1045 err = damon_sysfs_schemes_add_dirs(schemes, nr); >> 1046 mutex_unlock(&damon_sysfs_lock); >> 1047 if (err) >> 1048 return err; >> 1049 return count; >> 1050 } >> 1051 >> 1052 static void damon_sysfs_schemes_release(struct kobject *kobj) >> 1053 { >> 1054 kfree(container_of(kobj, struct damon_sysfs_schemes, kobj)); >> 1055 } >> 1056 >> 1057 static struct kobj_attribute damon_sysfs_schemes_nr_attr = >> 1058 __ATTR_RW_MODE(nr_schemes, 0600); >> 1059 >> 1060 static struct attribute *damon_sysfs_schemes_attrs[] = { >> 1061 &damon_sysfs_schemes_nr_attr.attr, >> 1062 NULL, >> 1063 }; >> 1064 ATTRIBUTE_GROUPS(damon_sysfs_schemes); >> 1065 >> 1066 static struct kobj_type damon_sysfs_schemes_ktype = { >> 1067 .release = damon_sysfs_schemes_release, >> 1068 .sysfs_ops = &kobj_sysfs_ops, >> 1069 .default_groups = damon_sysfs_schemes_groups, >> 1070 }; 13 1071 14 /* 1072 /* 15 * init region directory 1073 * init region directory 16 */ 1074 */ 17 1075 18 struct damon_sysfs_region { 1076 struct damon_sysfs_region { 19 struct kobject kobj; 1077 struct kobject kobj; 20 struct damon_addr_range ar; !! 1078 unsigned long start; >> 1079 unsigned long end; 21 }; 1080 }; 22 1081 23 static struct damon_sysfs_region *damon_sysfs_ !! 1082 static struct damon_sysfs_region *damon_sysfs_region_alloc( >> 1083 unsigned long start, >> 1084 unsigned long end) 24 { 1085 { 25 return kzalloc(sizeof(struct damon_sys !! 1086 struct damon_sysfs_region *region = kmalloc(sizeof(*region), >> 1087 GFP_KERNEL); >> 1088 >> 1089 if (!region) >> 1090 return NULL; >> 1091 region->kobj = (struct kobject){}; >> 1092 region->start = start; >> 1093 region->end = end; >> 1094 return region; 26 } 1095 } 27 1096 28 static ssize_t start_show(struct kobject *kobj 1097 static ssize_t start_show(struct kobject *kobj, struct kobj_attribute *attr, 29 char *buf) 1098 char *buf) 30 { 1099 { 31 struct damon_sysfs_region *region = co 1100 struct damon_sysfs_region *region = container_of(kobj, 32 struct damon_sysfs_reg 1101 struct damon_sysfs_region, kobj); 33 1102 34 return sysfs_emit(buf, "%lu\n", region !! 1103 return sysfs_emit(buf, "%lu\n", region->start); 35 } 1104 } 36 1105 37 static ssize_t start_store(struct kobject *kob 1106 static ssize_t start_store(struct kobject *kobj, struct kobj_attribute *attr, 38 const char *buf, size_t count) 1107 const char *buf, size_t count) 39 { 1108 { 40 struct damon_sysfs_region *region = co 1109 struct damon_sysfs_region *region = container_of(kobj, 41 struct damon_sysfs_reg 1110 struct damon_sysfs_region, kobj); 42 int err = kstrtoul(buf, 0, ®ion->ar !! 1111 int err = kstrtoul(buf, 0, ®ion->start); 43 1112 44 return err ? err : count; !! 1113 if (err) >> 1114 return -EINVAL; >> 1115 return count; 45 } 1116 } 46 1117 47 static ssize_t end_show(struct kobject *kobj, 1118 static ssize_t end_show(struct kobject *kobj, struct kobj_attribute *attr, 48 char *buf) 1119 char *buf) 49 { 1120 { 50 struct damon_sysfs_region *region = co 1121 struct damon_sysfs_region *region = container_of(kobj, 51 struct damon_sysfs_reg 1122 struct damon_sysfs_region, kobj); 52 1123 53 return sysfs_emit(buf, "%lu\n", region !! 1124 return sysfs_emit(buf, "%lu\n", region->end); 54 } 1125 } 55 1126 56 static ssize_t end_store(struct kobject *kobj, 1127 static ssize_t end_store(struct kobject *kobj, struct kobj_attribute *attr, 57 const char *buf, size_t count) 1128 const char *buf, size_t count) 58 { 1129 { 59 struct damon_sysfs_region *region = co 1130 struct damon_sysfs_region *region = container_of(kobj, 60 struct damon_sysfs_reg 1131 struct damon_sysfs_region, kobj); 61 int err = kstrtoul(buf, 0, ®ion->ar !! 1132 int err = kstrtoul(buf, 0, ®ion->end); 62 1133 63 return err ? err : count; !! 1134 if (err) >> 1135 return -EINVAL; >> 1136 return count; 64 } 1137 } 65 1138 66 static void damon_sysfs_region_release(struct 1139 static void damon_sysfs_region_release(struct kobject *kobj) 67 { 1140 { 68 kfree(container_of(kobj, struct damon_ 1141 kfree(container_of(kobj, struct damon_sysfs_region, kobj)); 69 } 1142 } 70 1143 71 static struct kobj_attribute damon_sysfs_regio 1144 static struct kobj_attribute damon_sysfs_region_start_attr = 72 __ATTR_RW_MODE(start, 0600); 1145 __ATTR_RW_MODE(start, 0600); 73 1146 74 static struct kobj_attribute damon_sysfs_regio 1147 static struct kobj_attribute damon_sysfs_region_end_attr = 75 __ATTR_RW_MODE(end, 0600); 1148 __ATTR_RW_MODE(end, 0600); 76 1149 77 static struct attribute *damon_sysfs_region_at 1150 static struct attribute *damon_sysfs_region_attrs[] = { 78 &damon_sysfs_region_start_attr.attr, 1151 &damon_sysfs_region_start_attr.attr, 79 &damon_sysfs_region_end_attr.attr, 1152 &damon_sysfs_region_end_attr.attr, 80 NULL, 1153 NULL, 81 }; 1154 }; 82 ATTRIBUTE_GROUPS(damon_sysfs_region); 1155 ATTRIBUTE_GROUPS(damon_sysfs_region); 83 1156 84 static const struct kobj_type damon_sysfs_regi !! 1157 static struct kobj_type damon_sysfs_region_ktype = { 85 .release = damon_sysfs_region_release, 1158 .release = damon_sysfs_region_release, 86 .sysfs_ops = &kobj_sysfs_ops, 1159 .sysfs_ops = &kobj_sysfs_ops, 87 .default_groups = damon_sysfs_region_g 1160 .default_groups = damon_sysfs_region_groups, 88 }; 1161 }; 89 1162 90 /* 1163 /* 91 * init_regions directory 1164 * init_regions directory 92 */ 1165 */ 93 1166 94 struct damon_sysfs_regions { 1167 struct damon_sysfs_regions { 95 struct kobject kobj; 1168 struct kobject kobj; 96 struct damon_sysfs_region **regions_ar 1169 struct damon_sysfs_region **regions_arr; 97 int nr; 1170 int nr; 98 }; 1171 }; 99 1172 100 static struct damon_sysfs_regions *damon_sysfs 1173 static struct damon_sysfs_regions *damon_sysfs_regions_alloc(void) 101 { 1174 { 102 return kzalloc(sizeof(struct damon_sys 1175 return kzalloc(sizeof(struct damon_sysfs_regions), GFP_KERNEL); 103 } 1176 } 104 1177 105 static void damon_sysfs_regions_rm_dirs(struct 1178 static void damon_sysfs_regions_rm_dirs(struct damon_sysfs_regions *regions) 106 { 1179 { 107 struct damon_sysfs_region **regions_ar 1180 struct damon_sysfs_region **regions_arr = regions->regions_arr; 108 int i; 1181 int i; 109 1182 110 for (i = 0; i < regions->nr; i++) 1183 for (i = 0; i < regions->nr; i++) 111 kobject_put(®ions_arr[i]->k 1184 kobject_put(®ions_arr[i]->kobj); 112 regions->nr = 0; 1185 regions->nr = 0; 113 kfree(regions_arr); 1186 kfree(regions_arr); 114 regions->regions_arr = NULL; 1187 regions->regions_arr = NULL; 115 } 1188 } 116 1189 117 static int damon_sysfs_regions_add_dirs(struct 1190 static int damon_sysfs_regions_add_dirs(struct damon_sysfs_regions *regions, 118 int nr_regions) 1191 int nr_regions) 119 { 1192 { 120 struct damon_sysfs_region **regions_ar 1193 struct damon_sysfs_region **regions_arr, *region; 121 int err, i; 1194 int err, i; 122 1195 123 damon_sysfs_regions_rm_dirs(regions); 1196 damon_sysfs_regions_rm_dirs(regions); 124 if (!nr_regions) 1197 if (!nr_regions) 125 return 0; 1198 return 0; 126 1199 127 regions_arr = kmalloc_array(nr_regions 1200 regions_arr = kmalloc_array(nr_regions, sizeof(*regions_arr), 128 GFP_KERNEL | __GFP_NOW 1201 GFP_KERNEL | __GFP_NOWARN); 129 if (!regions_arr) 1202 if (!regions_arr) 130 return -ENOMEM; 1203 return -ENOMEM; 131 regions->regions_arr = regions_arr; 1204 regions->regions_arr = regions_arr; 132 1205 133 for (i = 0; i < nr_regions; i++) { 1206 for (i = 0; i < nr_regions; i++) { 134 region = damon_sysfs_region_al !! 1207 region = damon_sysfs_region_alloc(0, 0); 135 if (!region) { 1208 if (!region) { 136 damon_sysfs_regions_rm 1209 damon_sysfs_regions_rm_dirs(regions); 137 return -ENOMEM; 1210 return -ENOMEM; 138 } 1211 } 139 1212 140 err = kobject_init_and_add(&re 1213 err = kobject_init_and_add(®ion->kobj, 141 &damon_sysfs_r 1214 &damon_sysfs_region_ktype, ®ions->kobj, 142 "%d", i); 1215 "%d", i); 143 if (err) { 1216 if (err) { 144 kobject_put(®ion->k 1217 kobject_put(®ion->kobj); 145 damon_sysfs_regions_rm 1218 damon_sysfs_regions_rm_dirs(regions); 146 return err; 1219 return err; 147 } 1220 } 148 1221 149 regions_arr[i] = region; 1222 regions_arr[i] = region; 150 regions->nr++; 1223 regions->nr++; 151 } 1224 } 152 return 0; 1225 return 0; 153 } 1226 } 154 1227 155 static ssize_t nr_regions_show(struct kobject 1228 static ssize_t nr_regions_show(struct kobject *kobj, 156 struct kobj_attribute *attr, c 1229 struct kobj_attribute *attr, char *buf) 157 { 1230 { 158 struct damon_sysfs_regions *regions = 1231 struct damon_sysfs_regions *regions = container_of(kobj, 159 struct damon_sysfs_reg 1232 struct damon_sysfs_regions, kobj); 160 1233 161 return sysfs_emit(buf, "%d\n", regions 1234 return sysfs_emit(buf, "%d\n", regions->nr); 162 } 1235 } 163 1236 164 static ssize_t nr_regions_store(struct kobject 1237 static ssize_t nr_regions_store(struct kobject *kobj, 165 struct kobj_attribute *attr, c 1238 struct kobj_attribute *attr, const char *buf, size_t count) 166 { 1239 { 167 struct damon_sysfs_regions *regions; !! 1240 struct damon_sysfs_regions *regions = container_of(kobj, >> 1241 struct damon_sysfs_regions, kobj); 168 int nr, err = kstrtoint(buf, 0, &nr); 1242 int nr, err = kstrtoint(buf, 0, &nr); 169 1243 170 if (err) 1244 if (err) 171 return err; 1245 return err; 172 if (nr < 0) 1246 if (nr < 0) 173 return -EINVAL; 1247 return -EINVAL; 174 1248 175 regions = container_of(kobj, struct da << 176 << 177 if (!mutex_trylock(&damon_sysfs_lock)) 1249 if (!mutex_trylock(&damon_sysfs_lock)) 178 return -EBUSY; 1250 return -EBUSY; 179 err = damon_sysfs_regions_add_dirs(reg 1251 err = damon_sysfs_regions_add_dirs(regions, nr); 180 mutex_unlock(&damon_sysfs_lock); 1252 mutex_unlock(&damon_sysfs_lock); 181 if (err) 1253 if (err) 182 return err; 1254 return err; 183 1255 184 return count; 1256 return count; 185 } 1257 } 186 1258 187 static void damon_sysfs_regions_release(struct 1259 static void damon_sysfs_regions_release(struct kobject *kobj) 188 { 1260 { 189 kfree(container_of(kobj, struct damon_ 1261 kfree(container_of(kobj, struct damon_sysfs_regions, kobj)); 190 } 1262 } 191 1263 192 static struct kobj_attribute damon_sysfs_regio 1264 static struct kobj_attribute damon_sysfs_regions_nr_attr = 193 __ATTR_RW_MODE(nr_regions, 060 1265 __ATTR_RW_MODE(nr_regions, 0600); 194 1266 195 static struct attribute *damon_sysfs_regions_a 1267 static struct attribute *damon_sysfs_regions_attrs[] = { 196 &damon_sysfs_regions_nr_attr.attr, 1268 &damon_sysfs_regions_nr_attr.attr, 197 NULL, 1269 NULL, 198 }; 1270 }; 199 ATTRIBUTE_GROUPS(damon_sysfs_regions); 1271 ATTRIBUTE_GROUPS(damon_sysfs_regions); 200 1272 201 static const struct kobj_type damon_sysfs_regi !! 1273 static struct kobj_type damon_sysfs_regions_ktype = { 202 .release = damon_sysfs_regions_release 1274 .release = damon_sysfs_regions_release, 203 .sysfs_ops = &kobj_sysfs_ops, 1275 .sysfs_ops = &kobj_sysfs_ops, 204 .default_groups = damon_sysfs_regions_ 1276 .default_groups = damon_sysfs_regions_groups, 205 }; 1277 }; 206 1278 207 /* 1279 /* 208 * target directory 1280 * target directory 209 */ 1281 */ 210 1282 211 struct damon_sysfs_target { 1283 struct damon_sysfs_target { 212 struct kobject kobj; 1284 struct kobject kobj; 213 struct damon_sysfs_regions *regions; 1285 struct damon_sysfs_regions *regions; 214 int pid; 1286 int pid; 215 }; 1287 }; 216 1288 217 static struct damon_sysfs_target *damon_sysfs_ 1289 static struct damon_sysfs_target *damon_sysfs_target_alloc(void) 218 { 1290 { 219 return kzalloc(sizeof(struct damon_sys 1291 return kzalloc(sizeof(struct damon_sysfs_target), GFP_KERNEL); 220 } 1292 } 221 1293 222 static int damon_sysfs_target_add_dirs(struct 1294 static int damon_sysfs_target_add_dirs(struct damon_sysfs_target *target) 223 { 1295 { 224 struct damon_sysfs_regions *regions = 1296 struct damon_sysfs_regions *regions = damon_sysfs_regions_alloc(); 225 int err; 1297 int err; 226 1298 227 if (!regions) 1299 if (!regions) 228 return -ENOMEM; 1300 return -ENOMEM; 229 1301 230 err = kobject_init_and_add(®ions->k 1302 err = kobject_init_and_add(®ions->kobj, &damon_sysfs_regions_ktype, 231 &target->kobj, "region 1303 &target->kobj, "regions"); 232 if (err) 1304 if (err) 233 kobject_put(®ions->kobj); 1305 kobject_put(®ions->kobj); 234 else 1306 else 235 target->regions = regions; 1307 target->regions = regions; 236 return err; 1308 return err; 237 } 1309 } 238 1310 239 static void damon_sysfs_target_rm_dirs(struct 1311 static void damon_sysfs_target_rm_dirs(struct damon_sysfs_target *target) 240 { 1312 { 241 damon_sysfs_regions_rm_dirs(target->re 1313 damon_sysfs_regions_rm_dirs(target->regions); 242 kobject_put(&target->regions->kobj); 1314 kobject_put(&target->regions->kobj); 243 } 1315 } 244 1316 245 static ssize_t pid_target_show(struct kobject 1317 static ssize_t pid_target_show(struct kobject *kobj, 246 struct kobj_attribute *attr, c 1318 struct kobj_attribute *attr, char *buf) 247 { 1319 { 248 struct damon_sysfs_target *target = co 1320 struct damon_sysfs_target *target = container_of(kobj, 249 struct damon_sysfs_tar 1321 struct damon_sysfs_target, kobj); 250 1322 251 return sysfs_emit(buf, "%d\n", target- 1323 return sysfs_emit(buf, "%d\n", target->pid); 252 } 1324 } 253 1325 254 static ssize_t pid_target_store(struct kobject 1326 static ssize_t pid_target_store(struct kobject *kobj, 255 struct kobj_attribute *attr, c 1327 struct kobj_attribute *attr, const char *buf, size_t count) 256 { 1328 { 257 struct damon_sysfs_target *target = co 1329 struct damon_sysfs_target *target = container_of(kobj, 258 struct damon_sysfs_tar 1330 struct damon_sysfs_target, kobj); 259 int err = kstrtoint(buf, 0, &target->p 1331 int err = kstrtoint(buf, 0, &target->pid); 260 1332 261 if (err) 1333 if (err) 262 return -EINVAL; 1334 return -EINVAL; 263 return count; 1335 return count; 264 } 1336 } 265 1337 266 static void damon_sysfs_target_release(struct 1338 static void damon_sysfs_target_release(struct kobject *kobj) 267 { 1339 { 268 kfree(container_of(kobj, struct damon_ 1340 kfree(container_of(kobj, struct damon_sysfs_target, kobj)); 269 } 1341 } 270 1342 271 static struct kobj_attribute damon_sysfs_targe 1343 static struct kobj_attribute damon_sysfs_target_pid_attr = 272 __ATTR_RW_MODE(pid_target, 060 1344 __ATTR_RW_MODE(pid_target, 0600); 273 1345 274 static struct attribute *damon_sysfs_target_at 1346 static struct attribute *damon_sysfs_target_attrs[] = { 275 &damon_sysfs_target_pid_attr.attr, 1347 &damon_sysfs_target_pid_attr.attr, 276 NULL, 1348 NULL, 277 }; 1349 }; 278 ATTRIBUTE_GROUPS(damon_sysfs_target); 1350 ATTRIBUTE_GROUPS(damon_sysfs_target); 279 1351 280 static const struct kobj_type damon_sysfs_targ !! 1352 static struct kobj_type damon_sysfs_target_ktype = { 281 .release = damon_sysfs_target_release, 1353 .release = damon_sysfs_target_release, 282 .sysfs_ops = &kobj_sysfs_ops, 1354 .sysfs_ops = &kobj_sysfs_ops, 283 .default_groups = damon_sysfs_target_g 1355 .default_groups = damon_sysfs_target_groups, 284 }; 1356 }; 285 1357 286 /* 1358 /* 287 * targets directory 1359 * targets directory 288 */ 1360 */ 289 1361 290 struct damon_sysfs_targets { 1362 struct damon_sysfs_targets { 291 struct kobject kobj; 1363 struct kobject kobj; 292 struct damon_sysfs_target **targets_ar 1364 struct damon_sysfs_target **targets_arr; 293 int nr; 1365 int nr; 294 }; 1366 }; 295 1367 296 static struct damon_sysfs_targets *damon_sysfs 1368 static struct damon_sysfs_targets *damon_sysfs_targets_alloc(void) 297 { 1369 { 298 return kzalloc(sizeof(struct damon_sys 1370 return kzalloc(sizeof(struct damon_sysfs_targets), GFP_KERNEL); 299 } 1371 } 300 1372 301 static void damon_sysfs_targets_rm_dirs(struct 1373 static void damon_sysfs_targets_rm_dirs(struct damon_sysfs_targets *targets) 302 { 1374 { 303 struct damon_sysfs_target **targets_ar 1375 struct damon_sysfs_target **targets_arr = targets->targets_arr; 304 int i; 1376 int i; 305 1377 306 for (i = 0; i < targets->nr; i++) { 1378 for (i = 0; i < targets->nr; i++) { 307 damon_sysfs_target_rm_dirs(tar 1379 damon_sysfs_target_rm_dirs(targets_arr[i]); 308 kobject_put(&targets_arr[i]->k 1380 kobject_put(&targets_arr[i]->kobj); 309 } 1381 } 310 targets->nr = 0; 1382 targets->nr = 0; 311 kfree(targets_arr); 1383 kfree(targets_arr); 312 targets->targets_arr = NULL; 1384 targets->targets_arr = NULL; 313 } 1385 } 314 1386 315 static int damon_sysfs_targets_add_dirs(struct 1387 static int damon_sysfs_targets_add_dirs(struct damon_sysfs_targets *targets, 316 int nr_targets) 1388 int nr_targets) 317 { 1389 { 318 struct damon_sysfs_target **targets_ar 1390 struct damon_sysfs_target **targets_arr, *target; 319 int err, i; 1391 int err, i; 320 1392 321 damon_sysfs_targets_rm_dirs(targets); 1393 damon_sysfs_targets_rm_dirs(targets); 322 if (!nr_targets) 1394 if (!nr_targets) 323 return 0; 1395 return 0; 324 1396 325 targets_arr = kmalloc_array(nr_targets 1397 targets_arr = kmalloc_array(nr_targets, sizeof(*targets_arr), 326 GFP_KERNEL | __GFP_NOW 1398 GFP_KERNEL | __GFP_NOWARN); 327 if (!targets_arr) 1399 if (!targets_arr) 328 return -ENOMEM; 1400 return -ENOMEM; 329 targets->targets_arr = targets_arr; 1401 targets->targets_arr = targets_arr; 330 1402 331 for (i = 0; i < nr_targets; i++) { 1403 for (i = 0; i < nr_targets; i++) { 332 target = damon_sysfs_target_al 1404 target = damon_sysfs_target_alloc(); 333 if (!target) { 1405 if (!target) { 334 damon_sysfs_targets_rm 1406 damon_sysfs_targets_rm_dirs(targets); 335 return -ENOMEM; 1407 return -ENOMEM; 336 } 1408 } 337 1409 338 err = kobject_init_and_add(&ta 1410 err = kobject_init_and_add(&target->kobj, 339 &damon_sysfs_t 1411 &damon_sysfs_target_ktype, &targets->kobj, 340 "%d", i); 1412 "%d", i); 341 if (err) 1413 if (err) 342 goto out; 1414 goto out; 343 1415 344 err = damon_sysfs_target_add_d 1416 err = damon_sysfs_target_add_dirs(target); 345 if (err) 1417 if (err) 346 goto out; 1418 goto out; 347 1419 348 targets_arr[i] = target; 1420 targets_arr[i] = target; 349 targets->nr++; 1421 targets->nr++; 350 } 1422 } 351 return 0; 1423 return 0; 352 1424 353 out: 1425 out: 354 damon_sysfs_targets_rm_dirs(targets); 1426 damon_sysfs_targets_rm_dirs(targets); 355 kobject_put(&target->kobj); 1427 kobject_put(&target->kobj); 356 return err; 1428 return err; 357 } 1429 } 358 1430 359 static ssize_t nr_targets_show(struct kobject 1431 static ssize_t nr_targets_show(struct kobject *kobj, 360 struct kobj_attribute *attr, c 1432 struct kobj_attribute *attr, char *buf) 361 { 1433 { 362 struct damon_sysfs_targets *targets = 1434 struct damon_sysfs_targets *targets = container_of(kobj, 363 struct damon_sysfs_tar 1435 struct damon_sysfs_targets, kobj); 364 1436 365 return sysfs_emit(buf, "%d\n", targets 1437 return sysfs_emit(buf, "%d\n", targets->nr); 366 } 1438 } 367 1439 368 static ssize_t nr_targets_store(struct kobject 1440 static ssize_t nr_targets_store(struct kobject *kobj, 369 struct kobj_attribute *attr, c 1441 struct kobj_attribute *attr, const char *buf, size_t count) 370 { 1442 { 371 struct damon_sysfs_targets *targets; !! 1443 struct damon_sysfs_targets *targets = container_of(kobj, >> 1444 struct damon_sysfs_targets, kobj); 372 int nr, err = kstrtoint(buf, 0, &nr); 1445 int nr, err = kstrtoint(buf, 0, &nr); 373 1446 374 if (err) 1447 if (err) 375 return err; 1448 return err; 376 if (nr < 0) 1449 if (nr < 0) 377 return -EINVAL; 1450 return -EINVAL; 378 1451 379 targets = container_of(kobj, struct da << 380 << 381 if (!mutex_trylock(&damon_sysfs_lock)) 1452 if (!mutex_trylock(&damon_sysfs_lock)) 382 return -EBUSY; 1453 return -EBUSY; 383 err = damon_sysfs_targets_add_dirs(tar 1454 err = damon_sysfs_targets_add_dirs(targets, nr); 384 mutex_unlock(&damon_sysfs_lock); 1455 mutex_unlock(&damon_sysfs_lock); 385 if (err) 1456 if (err) 386 return err; 1457 return err; 387 1458 388 return count; 1459 return count; 389 } 1460 } 390 1461 391 static void damon_sysfs_targets_release(struct 1462 static void damon_sysfs_targets_release(struct kobject *kobj) 392 { 1463 { 393 kfree(container_of(kobj, struct damon_ 1464 kfree(container_of(kobj, struct damon_sysfs_targets, kobj)); 394 } 1465 } 395 1466 396 static struct kobj_attribute damon_sysfs_targe 1467 static struct kobj_attribute damon_sysfs_targets_nr_attr = 397 __ATTR_RW_MODE(nr_targets, 060 1468 __ATTR_RW_MODE(nr_targets, 0600); 398 1469 399 static struct attribute *damon_sysfs_targets_a 1470 static struct attribute *damon_sysfs_targets_attrs[] = { 400 &damon_sysfs_targets_nr_attr.attr, 1471 &damon_sysfs_targets_nr_attr.attr, 401 NULL, 1472 NULL, 402 }; 1473 }; 403 ATTRIBUTE_GROUPS(damon_sysfs_targets); 1474 ATTRIBUTE_GROUPS(damon_sysfs_targets); 404 1475 405 static const struct kobj_type damon_sysfs_targ !! 1476 static struct kobj_type damon_sysfs_targets_ktype = { 406 .release = damon_sysfs_targets_release 1477 .release = damon_sysfs_targets_release, 407 .sysfs_ops = &kobj_sysfs_ops, 1478 .sysfs_ops = &kobj_sysfs_ops, 408 .default_groups = damon_sysfs_targets_ 1479 .default_groups = damon_sysfs_targets_groups, 409 }; 1480 }; 410 1481 411 /* 1482 /* 412 * intervals directory 1483 * intervals directory 413 */ 1484 */ 414 1485 415 struct damon_sysfs_intervals { 1486 struct damon_sysfs_intervals { 416 struct kobject kobj; 1487 struct kobject kobj; 417 unsigned long sample_us; 1488 unsigned long sample_us; 418 unsigned long aggr_us; 1489 unsigned long aggr_us; 419 unsigned long update_us; 1490 unsigned long update_us; 420 }; 1491 }; 421 1492 422 static struct damon_sysfs_intervals *damon_sys 1493 static struct damon_sysfs_intervals *damon_sysfs_intervals_alloc( 423 unsigned long sample_us, unsig 1494 unsigned long sample_us, unsigned long aggr_us, 424 unsigned long update_us) 1495 unsigned long update_us) 425 { 1496 { 426 struct damon_sysfs_intervals *interval 1497 struct damon_sysfs_intervals *intervals = kmalloc(sizeof(*intervals), 427 GFP_KERNEL); 1498 GFP_KERNEL); 428 1499 429 if (!intervals) 1500 if (!intervals) 430 return NULL; 1501 return NULL; 431 1502 432 intervals->kobj = (struct kobject){}; 1503 intervals->kobj = (struct kobject){}; 433 intervals->sample_us = sample_us; 1504 intervals->sample_us = sample_us; 434 intervals->aggr_us = aggr_us; 1505 intervals->aggr_us = aggr_us; 435 intervals->update_us = update_us; 1506 intervals->update_us = update_us; 436 return intervals; 1507 return intervals; 437 } 1508 } 438 1509 439 static ssize_t sample_us_show(struct kobject * 1510 static ssize_t sample_us_show(struct kobject *kobj, 440 struct kobj_attribute *attr, c 1511 struct kobj_attribute *attr, char *buf) 441 { 1512 { 442 struct damon_sysfs_intervals *interval 1513 struct damon_sysfs_intervals *intervals = container_of(kobj, 443 struct damon_sysfs_int 1514 struct damon_sysfs_intervals, kobj); 444 1515 445 return sysfs_emit(buf, "%lu\n", interv 1516 return sysfs_emit(buf, "%lu\n", intervals->sample_us); 446 } 1517 } 447 1518 448 static ssize_t sample_us_store(struct kobject 1519 static ssize_t sample_us_store(struct kobject *kobj, 449 struct kobj_attribute *attr, c 1520 struct kobj_attribute *attr, const char *buf, size_t count) 450 { 1521 { 451 struct damon_sysfs_intervals *interval 1522 struct damon_sysfs_intervals *intervals = container_of(kobj, 452 struct damon_sysfs_int 1523 struct damon_sysfs_intervals, kobj); 453 unsigned long us; 1524 unsigned long us; 454 int err = kstrtoul(buf, 0, &us); 1525 int err = kstrtoul(buf, 0, &us); 455 1526 456 if (err) 1527 if (err) 457 return err; !! 1528 return -EINVAL; 458 1529 459 intervals->sample_us = us; 1530 intervals->sample_us = us; 460 return count; 1531 return count; 461 } 1532 } 462 1533 463 static ssize_t aggr_us_show(struct kobject *ko 1534 static ssize_t aggr_us_show(struct kobject *kobj, struct kobj_attribute *attr, 464 char *buf) 1535 char *buf) 465 { 1536 { 466 struct damon_sysfs_intervals *interval 1537 struct damon_sysfs_intervals *intervals = container_of(kobj, 467 struct damon_sysfs_int 1538 struct damon_sysfs_intervals, kobj); 468 1539 469 return sysfs_emit(buf, "%lu\n", interv 1540 return sysfs_emit(buf, "%lu\n", intervals->aggr_us); 470 } 1541 } 471 1542 472 static ssize_t aggr_us_store(struct kobject *k 1543 static ssize_t aggr_us_store(struct kobject *kobj, struct kobj_attribute *attr, 473 const char *buf, size_t count) 1544 const char *buf, size_t count) 474 { 1545 { 475 struct damon_sysfs_intervals *interval 1546 struct damon_sysfs_intervals *intervals = container_of(kobj, 476 struct damon_sysfs_int 1547 struct damon_sysfs_intervals, kobj); 477 unsigned long us; 1548 unsigned long us; 478 int err = kstrtoul(buf, 0, &us); 1549 int err = kstrtoul(buf, 0, &us); 479 1550 480 if (err) 1551 if (err) 481 return err; !! 1552 return -EINVAL; 482 1553 483 intervals->aggr_us = us; 1554 intervals->aggr_us = us; 484 return count; 1555 return count; 485 } 1556 } 486 1557 487 static ssize_t update_us_show(struct kobject * 1558 static ssize_t update_us_show(struct kobject *kobj, 488 struct kobj_attribute *attr, c 1559 struct kobj_attribute *attr, char *buf) 489 { 1560 { 490 struct damon_sysfs_intervals *interval 1561 struct damon_sysfs_intervals *intervals = container_of(kobj, 491 struct damon_sysfs_int 1562 struct damon_sysfs_intervals, kobj); 492 1563 493 return sysfs_emit(buf, "%lu\n", interv 1564 return sysfs_emit(buf, "%lu\n", intervals->update_us); 494 } 1565 } 495 1566 496 static ssize_t update_us_store(struct kobject 1567 static ssize_t update_us_store(struct kobject *kobj, 497 struct kobj_attribute *attr, c 1568 struct kobj_attribute *attr, const char *buf, size_t count) 498 { 1569 { 499 struct damon_sysfs_intervals *interval 1570 struct damon_sysfs_intervals *intervals = container_of(kobj, 500 struct damon_sysfs_int 1571 struct damon_sysfs_intervals, kobj); 501 unsigned long us; 1572 unsigned long us; 502 int err = kstrtoul(buf, 0, &us); 1573 int err = kstrtoul(buf, 0, &us); 503 1574 504 if (err) 1575 if (err) 505 return err; !! 1576 return -EINVAL; 506 1577 507 intervals->update_us = us; 1578 intervals->update_us = us; 508 return count; 1579 return count; 509 } 1580 } 510 1581 511 static void damon_sysfs_intervals_release(stru 1582 static void damon_sysfs_intervals_release(struct kobject *kobj) 512 { 1583 { 513 kfree(container_of(kobj, struct damon_ 1584 kfree(container_of(kobj, struct damon_sysfs_intervals, kobj)); 514 } 1585 } 515 1586 516 static struct kobj_attribute damon_sysfs_inter 1587 static struct kobj_attribute damon_sysfs_intervals_sample_us_attr = 517 __ATTR_RW_MODE(sample_us, 0600 1588 __ATTR_RW_MODE(sample_us, 0600); 518 1589 519 static struct kobj_attribute damon_sysfs_inter 1590 static struct kobj_attribute damon_sysfs_intervals_aggr_us_attr = 520 __ATTR_RW_MODE(aggr_us, 0600); 1591 __ATTR_RW_MODE(aggr_us, 0600); 521 1592 522 static struct kobj_attribute damon_sysfs_inter 1593 static struct kobj_attribute damon_sysfs_intervals_update_us_attr = 523 __ATTR_RW_MODE(update_us, 0600 1594 __ATTR_RW_MODE(update_us, 0600); 524 1595 525 static struct attribute *damon_sysfs_intervals 1596 static struct attribute *damon_sysfs_intervals_attrs[] = { 526 &damon_sysfs_intervals_sample_us_attr. 1597 &damon_sysfs_intervals_sample_us_attr.attr, 527 &damon_sysfs_intervals_aggr_us_attr.at 1598 &damon_sysfs_intervals_aggr_us_attr.attr, 528 &damon_sysfs_intervals_update_us_attr. 1599 &damon_sysfs_intervals_update_us_attr.attr, 529 NULL, 1600 NULL, 530 }; 1601 }; 531 ATTRIBUTE_GROUPS(damon_sysfs_intervals); 1602 ATTRIBUTE_GROUPS(damon_sysfs_intervals); 532 1603 533 static const struct kobj_type damon_sysfs_inte !! 1604 static struct kobj_type damon_sysfs_intervals_ktype = { 534 .release = damon_sysfs_intervals_relea 1605 .release = damon_sysfs_intervals_release, 535 .sysfs_ops = &kobj_sysfs_ops, 1606 .sysfs_ops = &kobj_sysfs_ops, 536 .default_groups = damon_sysfs_interval 1607 .default_groups = damon_sysfs_intervals_groups, 537 }; 1608 }; 538 1609 539 /* 1610 /* 540 * monitoring_attrs directory 1611 * monitoring_attrs directory 541 */ 1612 */ 542 1613 543 struct damon_sysfs_attrs { 1614 struct damon_sysfs_attrs { 544 struct kobject kobj; 1615 struct kobject kobj; 545 struct damon_sysfs_intervals *interval 1616 struct damon_sysfs_intervals *intervals; 546 struct damon_sysfs_ul_range *nr_region 1617 struct damon_sysfs_ul_range *nr_regions_range; 547 }; 1618 }; 548 1619 549 static struct damon_sysfs_attrs *damon_sysfs_a 1620 static struct damon_sysfs_attrs *damon_sysfs_attrs_alloc(void) 550 { 1621 { 551 struct damon_sysfs_attrs *attrs = kmal 1622 struct damon_sysfs_attrs *attrs = kmalloc(sizeof(*attrs), GFP_KERNEL); 552 1623 553 if (!attrs) 1624 if (!attrs) 554 return NULL; 1625 return NULL; 555 attrs->kobj = (struct kobject){}; 1626 attrs->kobj = (struct kobject){}; 556 return attrs; 1627 return attrs; 557 } 1628 } 558 1629 559 static int damon_sysfs_attrs_add_dirs(struct d 1630 static int damon_sysfs_attrs_add_dirs(struct damon_sysfs_attrs *attrs) 560 { 1631 { 561 struct damon_sysfs_intervals *interval 1632 struct damon_sysfs_intervals *intervals; 562 struct damon_sysfs_ul_range *nr_region 1633 struct damon_sysfs_ul_range *nr_regions_range; 563 int err; 1634 int err; 564 1635 565 intervals = damon_sysfs_intervals_allo 1636 intervals = damon_sysfs_intervals_alloc(5000, 100000, 60000000); 566 if (!intervals) 1637 if (!intervals) 567 return -ENOMEM; 1638 return -ENOMEM; 568 1639 569 err = kobject_init_and_add(&intervals- 1640 err = kobject_init_and_add(&intervals->kobj, 570 &damon_sysfs_intervals 1641 &damon_sysfs_intervals_ktype, &attrs->kobj, 571 "intervals"); 1642 "intervals"); 572 if (err) 1643 if (err) 573 goto put_intervals_out; 1644 goto put_intervals_out; 574 attrs->intervals = intervals; 1645 attrs->intervals = intervals; 575 1646 576 nr_regions_range = damon_sysfs_ul_rang 1647 nr_regions_range = damon_sysfs_ul_range_alloc(10, 1000); 577 if (!nr_regions_range) { 1648 if (!nr_regions_range) { 578 err = -ENOMEM; 1649 err = -ENOMEM; 579 goto put_intervals_out; 1650 goto put_intervals_out; 580 } 1651 } 581 1652 582 err = kobject_init_and_add(&nr_regions 1653 err = kobject_init_and_add(&nr_regions_range->kobj, 583 &damon_sysfs_ul_range_ 1654 &damon_sysfs_ul_range_ktype, &attrs->kobj, 584 "nr_regions"); 1655 "nr_regions"); 585 if (err) 1656 if (err) 586 goto put_nr_regions_intervals_ 1657 goto put_nr_regions_intervals_out; 587 attrs->nr_regions_range = nr_regions_r 1658 attrs->nr_regions_range = nr_regions_range; 588 return 0; 1659 return 0; 589 1660 590 put_nr_regions_intervals_out: 1661 put_nr_regions_intervals_out: 591 kobject_put(&nr_regions_range->kobj); 1662 kobject_put(&nr_regions_range->kobj); 592 attrs->nr_regions_range = NULL; 1663 attrs->nr_regions_range = NULL; 593 put_intervals_out: 1664 put_intervals_out: 594 kobject_put(&intervals->kobj); 1665 kobject_put(&intervals->kobj); 595 attrs->intervals = NULL; 1666 attrs->intervals = NULL; 596 return err; 1667 return err; 597 } 1668 } 598 1669 599 static void damon_sysfs_attrs_rm_dirs(struct d 1670 static void damon_sysfs_attrs_rm_dirs(struct damon_sysfs_attrs *attrs) 600 { 1671 { 601 kobject_put(&attrs->nr_regions_range-> 1672 kobject_put(&attrs->nr_regions_range->kobj); 602 kobject_put(&attrs->intervals->kobj); 1673 kobject_put(&attrs->intervals->kobj); 603 } 1674 } 604 1675 605 static void damon_sysfs_attrs_release(struct k 1676 static void damon_sysfs_attrs_release(struct kobject *kobj) 606 { 1677 { 607 kfree(container_of(kobj, struct damon_ 1678 kfree(container_of(kobj, struct damon_sysfs_attrs, kobj)); 608 } 1679 } 609 1680 610 static struct attribute *damon_sysfs_attrs_att 1681 static struct attribute *damon_sysfs_attrs_attrs[] = { 611 NULL, 1682 NULL, 612 }; 1683 }; 613 ATTRIBUTE_GROUPS(damon_sysfs_attrs); 1684 ATTRIBUTE_GROUPS(damon_sysfs_attrs); 614 1685 615 static const struct kobj_type damon_sysfs_attr !! 1686 static struct kobj_type damon_sysfs_attrs_ktype = { 616 .release = damon_sysfs_attrs_release, 1687 .release = damon_sysfs_attrs_release, 617 .sysfs_ops = &kobj_sysfs_ops, 1688 .sysfs_ops = &kobj_sysfs_ops, 618 .default_groups = damon_sysfs_attrs_gr 1689 .default_groups = damon_sysfs_attrs_groups, 619 }; 1690 }; 620 1691 621 /* 1692 /* 622 * context directory 1693 * context directory 623 */ 1694 */ 624 1695 625 /* This should match with enum damon_ops_id */ 1696 /* This should match with enum damon_ops_id */ 626 static const char * const damon_sysfs_ops_strs 1697 static const char * const damon_sysfs_ops_strs[] = { 627 "vaddr", 1698 "vaddr", 628 "fvaddr", 1699 "fvaddr", 629 "paddr", 1700 "paddr", 630 }; 1701 }; 631 1702 632 struct damon_sysfs_context { 1703 struct damon_sysfs_context { 633 struct kobject kobj; 1704 struct kobject kobj; 634 enum damon_ops_id ops_id; 1705 enum damon_ops_id ops_id; 635 struct damon_sysfs_attrs *attrs; 1706 struct damon_sysfs_attrs *attrs; 636 struct damon_sysfs_targets *targets; 1707 struct damon_sysfs_targets *targets; 637 struct damon_sysfs_schemes *schemes; 1708 struct damon_sysfs_schemes *schemes; 638 }; 1709 }; 639 1710 640 static struct damon_sysfs_context *damon_sysfs 1711 static struct damon_sysfs_context *damon_sysfs_context_alloc( 641 enum damon_ops_id ops_id) 1712 enum damon_ops_id ops_id) 642 { 1713 { 643 struct damon_sysfs_context *context = 1714 struct damon_sysfs_context *context = kmalloc(sizeof(*context), 644 GFP_KERNEL); 1715 GFP_KERNEL); 645 1716 646 if (!context) 1717 if (!context) 647 return NULL; 1718 return NULL; 648 context->kobj = (struct kobject){}; 1719 context->kobj = (struct kobject){}; 649 context->ops_id = ops_id; 1720 context->ops_id = ops_id; 650 return context; 1721 return context; 651 } 1722 } 652 1723 653 static int damon_sysfs_context_set_attrs(struc 1724 static int damon_sysfs_context_set_attrs(struct damon_sysfs_context *context) 654 { 1725 { 655 struct damon_sysfs_attrs *attrs = damo 1726 struct damon_sysfs_attrs *attrs = damon_sysfs_attrs_alloc(); 656 int err; 1727 int err; 657 1728 658 if (!attrs) 1729 if (!attrs) 659 return -ENOMEM; 1730 return -ENOMEM; 660 err = kobject_init_and_add(&attrs->kob 1731 err = kobject_init_and_add(&attrs->kobj, &damon_sysfs_attrs_ktype, 661 &context->kobj, "monit 1732 &context->kobj, "monitoring_attrs"); 662 if (err) 1733 if (err) 663 goto out; 1734 goto out; 664 err = damon_sysfs_attrs_add_dirs(attrs 1735 err = damon_sysfs_attrs_add_dirs(attrs); 665 if (err) 1736 if (err) 666 goto out; 1737 goto out; 667 context->attrs = attrs; 1738 context->attrs = attrs; 668 return 0; 1739 return 0; 669 1740 670 out: 1741 out: 671 kobject_put(&attrs->kobj); 1742 kobject_put(&attrs->kobj); 672 return err; 1743 return err; 673 } 1744 } 674 1745 675 static int damon_sysfs_context_set_targets(str 1746 static int damon_sysfs_context_set_targets(struct damon_sysfs_context *context) 676 { 1747 { 677 struct damon_sysfs_targets *targets = 1748 struct damon_sysfs_targets *targets = damon_sysfs_targets_alloc(); 678 int err; 1749 int err; 679 1750 680 if (!targets) 1751 if (!targets) 681 return -ENOMEM; 1752 return -ENOMEM; 682 err = kobject_init_and_add(&targets->k 1753 err = kobject_init_and_add(&targets->kobj, &damon_sysfs_targets_ktype, 683 &context->kobj, "targe 1754 &context->kobj, "targets"); 684 if (err) { 1755 if (err) { 685 kobject_put(&targets->kobj); 1756 kobject_put(&targets->kobj); 686 return err; 1757 return err; 687 } 1758 } 688 context->targets = targets; 1759 context->targets = targets; 689 return 0; 1760 return 0; 690 } 1761 } 691 1762 692 static int damon_sysfs_context_set_schemes(str 1763 static int damon_sysfs_context_set_schemes(struct damon_sysfs_context *context) 693 { 1764 { 694 struct damon_sysfs_schemes *schemes = 1765 struct damon_sysfs_schemes *schemes = damon_sysfs_schemes_alloc(); 695 int err; 1766 int err; 696 1767 697 if (!schemes) 1768 if (!schemes) 698 return -ENOMEM; 1769 return -ENOMEM; 699 err = kobject_init_and_add(&schemes->k 1770 err = kobject_init_and_add(&schemes->kobj, &damon_sysfs_schemes_ktype, 700 &context->kobj, "schem 1771 &context->kobj, "schemes"); 701 if (err) { 1772 if (err) { 702 kobject_put(&schemes->kobj); 1773 kobject_put(&schemes->kobj); 703 return err; 1774 return err; 704 } 1775 } 705 context->schemes = schemes; 1776 context->schemes = schemes; 706 return 0; 1777 return 0; 707 } 1778 } 708 1779 709 static int damon_sysfs_context_add_dirs(struct 1780 static int damon_sysfs_context_add_dirs(struct damon_sysfs_context *context) 710 { 1781 { 711 int err; 1782 int err; 712 1783 713 err = damon_sysfs_context_set_attrs(co 1784 err = damon_sysfs_context_set_attrs(context); 714 if (err) 1785 if (err) 715 return err; 1786 return err; 716 1787 717 err = damon_sysfs_context_set_targets( 1788 err = damon_sysfs_context_set_targets(context); 718 if (err) 1789 if (err) 719 goto put_attrs_out; 1790 goto put_attrs_out; 720 1791 721 err = damon_sysfs_context_set_schemes( 1792 err = damon_sysfs_context_set_schemes(context); 722 if (err) 1793 if (err) 723 goto put_targets_attrs_out; 1794 goto put_targets_attrs_out; 724 return 0; 1795 return 0; 725 1796 726 put_targets_attrs_out: 1797 put_targets_attrs_out: 727 kobject_put(&context->targets->kobj); 1798 kobject_put(&context->targets->kobj); 728 context->targets = NULL; 1799 context->targets = NULL; 729 put_attrs_out: 1800 put_attrs_out: 730 kobject_put(&context->attrs->kobj); 1801 kobject_put(&context->attrs->kobj); 731 context->attrs = NULL; 1802 context->attrs = NULL; 732 return err; 1803 return err; 733 } 1804 } 734 1805 735 static void damon_sysfs_context_rm_dirs(struct 1806 static void damon_sysfs_context_rm_dirs(struct damon_sysfs_context *context) 736 { 1807 { 737 damon_sysfs_attrs_rm_dirs(context->att 1808 damon_sysfs_attrs_rm_dirs(context->attrs); 738 kobject_put(&context->attrs->kobj); 1809 kobject_put(&context->attrs->kobj); 739 damon_sysfs_targets_rm_dirs(context->t 1810 damon_sysfs_targets_rm_dirs(context->targets); 740 kobject_put(&context->targets->kobj); 1811 kobject_put(&context->targets->kobj); 741 damon_sysfs_schemes_rm_dirs(context->s 1812 damon_sysfs_schemes_rm_dirs(context->schemes); 742 kobject_put(&context->schemes->kobj); 1813 kobject_put(&context->schemes->kobj); 743 } 1814 } 744 1815 745 static ssize_t avail_operations_show(struct ko 1816 static ssize_t avail_operations_show(struct kobject *kobj, 746 struct kobj_attribute *attr, c 1817 struct kobj_attribute *attr, char *buf) 747 { 1818 { 748 enum damon_ops_id id; 1819 enum damon_ops_id id; 749 int len = 0; 1820 int len = 0; 750 1821 751 for (id = 0; id < NR_DAMON_OPS; id++) 1822 for (id = 0; id < NR_DAMON_OPS; id++) { 752 if (!damon_is_registered_ops(i 1823 if (!damon_is_registered_ops(id)) 753 continue; 1824 continue; 754 len += sysfs_emit_at(buf, len, 1825 len += sysfs_emit_at(buf, len, "%s\n", 755 damon_sysfs_op 1826 damon_sysfs_ops_strs[id]); 756 } 1827 } 757 return len; 1828 return len; 758 } 1829 } 759 1830 760 static ssize_t operations_show(struct kobject 1831 static ssize_t operations_show(struct kobject *kobj, 761 struct kobj_attribute *attr, c 1832 struct kobj_attribute *attr, char *buf) 762 { 1833 { 763 struct damon_sysfs_context *context = 1834 struct damon_sysfs_context *context = container_of(kobj, 764 struct damon_sysfs_con 1835 struct damon_sysfs_context, kobj); 765 1836 766 return sysfs_emit(buf, "%s\n", damon_s 1837 return sysfs_emit(buf, "%s\n", damon_sysfs_ops_strs[context->ops_id]); 767 } 1838 } 768 1839 769 static ssize_t operations_store(struct kobject 1840 static ssize_t operations_store(struct kobject *kobj, 770 struct kobj_attribute *attr, c 1841 struct kobj_attribute *attr, const char *buf, size_t count) 771 { 1842 { 772 struct damon_sysfs_context *context = 1843 struct damon_sysfs_context *context = container_of(kobj, 773 struct damon_sysfs_con 1844 struct damon_sysfs_context, kobj); 774 enum damon_ops_id id; 1845 enum damon_ops_id id; 775 1846 776 for (id = 0; id < NR_DAMON_OPS; id++) 1847 for (id = 0; id < NR_DAMON_OPS; id++) { 777 if (sysfs_streq(buf, damon_sys 1848 if (sysfs_streq(buf, damon_sysfs_ops_strs[id])) { 778 context->ops_id = id; 1849 context->ops_id = id; 779 return count; 1850 return count; 780 } 1851 } 781 } 1852 } 782 return -EINVAL; 1853 return -EINVAL; 783 } 1854 } 784 1855 785 static void damon_sysfs_context_release(struct 1856 static void damon_sysfs_context_release(struct kobject *kobj) 786 { 1857 { 787 kfree(container_of(kobj, struct damon_ 1858 kfree(container_of(kobj, struct damon_sysfs_context, kobj)); 788 } 1859 } 789 1860 790 static struct kobj_attribute damon_sysfs_conte 1861 static struct kobj_attribute damon_sysfs_context_avail_operations_attr = 791 __ATTR_RO_MODE(avail_operation 1862 __ATTR_RO_MODE(avail_operations, 0400); 792 1863 793 static struct kobj_attribute damon_sysfs_conte 1864 static struct kobj_attribute damon_sysfs_context_operations_attr = 794 __ATTR_RW_MODE(operations, 060 1865 __ATTR_RW_MODE(operations, 0600); 795 1866 796 static struct attribute *damon_sysfs_context_a 1867 static struct attribute *damon_sysfs_context_attrs[] = { 797 &damon_sysfs_context_avail_operations_ 1868 &damon_sysfs_context_avail_operations_attr.attr, 798 &damon_sysfs_context_operations_attr.a 1869 &damon_sysfs_context_operations_attr.attr, 799 NULL, 1870 NULL, 800 }; 1871 }; 801 ATTRIBUTE_GROUPS(damon_sysfs_context); 1872 ATTRIBUTE_GROUPS(damon_sysfs_context); 802 1873 803 static const struct kobj_type damon_sysfs_cont !! 1874 static struct kobj_type damon_sysfs_context_ktype = { 804 .release = damon_sysfs_context_release 1875 .release = damon_sysfs_context_release, 805 .sysfs_ops = &kobj_sysfs_ops, 1876 .sysfs_ops = &kobj_sysfs_ops, 806 .default_groups = damon_sysfs_context_ 1877 .default_groups = damon_sysfs_context_groups, 807 }; 1878 }; 808 1879 809 /* 1880 /* 810 * contexts directory 1881 * contexts directory 811 */ 1882 */ 812 1883 813 struct damon_sysfs_contexts { 1884 struct damon_sysfs_contexts { 814 struct kobject kobj; 1885 struct kobject kobj; 815 struct damon_sysfs_context **contexts_ 1886 struct damon_sysfs_context **contexts_arr; 816 int nr; 1887 int nr; 817 }; 1888 }; 818 1889 819 static struct damon_sysfs_contexts *damon_sysf 1890 static struct damon_sysfs_contexts *damon_sysfs_contexts_alloc(void) 820 { 1891 { 821 return kzalloc(sizeof(struct damon_sys 1892 return kzalloc(sizeof(struct damon_sysfs_contexts), GFP_KERNEL); 822 } 1893 } 823 1894 824 static void damon_sysfs_contexts_rm_dirs(struc 1895 static void damon_sysfs_contexts_rm_dirs(struct damon_sysfs_contexts *contexts) 825 { 1896 { 826 struct damon_sysfs_context **contexts_ 1897 struct damon_sysfs_context **contexts_arr = contexts->contexts_arr; 827 int i; 1898 int i; 828 1899 829 for (i = 0; i < contexts->nr; i++) { 1900 for (i = 0; i < contexts->nr; i++) { 830 damon_sysfs_context_rm_dirs(co 1901 damon_sysfs_context_rm_dirs(contexts_arr[i]); 831 kobject_put(&contexts_arr[i]-> 1902 kobject_put(&contexts_arr[i]->kobj); 832 } 1903 } 833 contexts->nr = 0; 1904 contexts->nr = 0; 834 kfree(contexts_arr); 1905 kfree(contexts_arr); 835 contexts->contexts_arr = NULL; 1906 contexts->contexts_arr = NULL; 836 } 1907 } 837 1908 838 static int damon_sysfs_contexts_add_dirs(struc 1909 static int damon_sysfs_contexts_add_dirs(struct damon_sysfs_contexts *contexts, 839 int nr_contexts) 1910 int nr_contexts) 840 { 1911 { 841 struct damon_sysfs_context **contexts_ 1912 struct damon_sysfs_context **contexts_arr, *context; 842 int err, i; 1913 int err, i; 843 1914 844 damon_sysfs_contexts_rm_dirs(contexts) 1915 damon_sysfs_contexts_rm_dirs(contexts); 845 if (!nr_contexts) 1916 if (!nr_contexts) 846 return 0; 1917 return 0; 847 1918 848 contexts_arr = kmalloc_array(nr_contex 1919 contexts_arr = kmalloc_array(nr_contexts, sizeof(*contexts_arr), 849 GFP_KERNEL | __GFP_NOW 1920 GFP_KERNEL | __GFP_NOWARN); 850 if (!contexts_arr) 1921 if (!contexts_arr) 851 return -ENOMEM; 1922 return -ENOMEM; 852 contexts->contexts_arr = contexts_arr; 1923 contexts->contexts_arr = contexts_arr; 853 1924 854 for (i = 0; i < nr_contexts; i++) { 1925 for (i = 0; i < nr_contexts; i++) { 855 context = damon_sysfs_context_ 1926 context = damon_sysfs_context_alloc(DAMON_OPS_VADDR); 856 if (!context) { 1927 if (!context) { 857 damon_sysfs_contexts_r 1928 damon_sysfs_contexts_rm_dirs(contexts); 858 return -ENOMEM; 1929 return -ENOMEM; 859 } 1930 } 860 1931 861 err = kobject_init_and_add(&co 1932 err = kobject_init_and_add(&context->kobj, 862 &damon_sysfs_c 1933 &damon_sysfs_context_ktype, &contexts->kobj, 863 "%d", i); 1934 "%d", i); 864 if (err) 1935 if (err) 865 goto out; 1936 goto out; 866 1937 867 err = damon_sysfs_context_add_ 1938 err = damon_sysfs_context_add_dirs(context); 868 if (err) 1939 if (err) 869 goto out; 1940 goto out; 870 1941 871 contexts_arr[i] = context; 1942 contexts_arr[i] = context; 872 contexts->nr++; 1943 contexts->nr++; 873 } 1944 } 874 return 0; 1945 return 0; 875 1946 876 out: 1947 out: 877 damon_sysfs_contexts_rm_dirs(contexts) 1948 damon_sysfs_contexts_rm_dirs(contexts); 878 kobject_put(&context->kobj); 1949 kobject_put(&context->kobj); 879 return err; 1950 return err; 880 } 1951 } 881 1952 882 static ssize_t nr_contexts_show(struct kobject 1953 static ssize_t nr_contexts_show(struct kobject *kobj, 883 struct kobj_attribute *attr, c 1954 struct kobj_attribute *attr, char *buf) 884 { 1955 { 885 struct damon_sysfs_contexts *contexts 1956 struct damon_sysfs_contexts *contexts = container_of(kobj, 886 struct damon_sysfs_con 1957 struct damon_sysfs_contexts, kobj); 887 1958 888 return sysfs_emit(buf, "%d\n", context 1959 return sysfs_emit(buf, "%d\n", contexts->nr); 889 } 1960 } 890 1961 891 static ssize_t nr_contexts_store(struct kobjec 1962 static ssize_t nr_contexts_store(struct kobject *kobj, 892 struct kobj_attribute *attr, c 1963 struct kobj_attribute *attr, const char *buf, size_t count) 893 { 1964 { 894 struct damon_sysfs_contexts *contexts; !! 1965 struct damon_sysfs_contexts *contexts = container_of(kobj, >> 1966 struct damon_sysfs_contexts, kobj); 895 int nr, err; 1967 int nr, err; 896 1968 897 err = kstrtoint(buf, 0, &nr); 1969 err = kstrtoint(buf, 0, &nr); 898 if (err) 1970 if (err) 899 return err; 1971 return err; 900 /* TODO: support multiple contexts per 1972 /* TODO: support multiple contexts per kdamond */ 901 if (nr < 0 || 1 < nr) 1973 if (nr < 0 || 1 < nr) 902 return -EINVAL; 1974 return -EINVAL; 903 1975 904 contexts = container_of(kobj, struct d << 905 if (!mutex_trylock(&damon_sysfs_lock)) 1976 if (!mutex_trylock(&damon_sysfs_lock)) 906 return -EBUSY; 1977 return -EBUSY; 907 err = damon_sysfs_contexts_add_dirs(co 1978 err = damon_sysfs_contexts_add_dirs(contexts, nr); 908 mutex_unlock(&damon_sysfs_lock); 1979 mutex_unlock(&damon_sysfs_lock); 909 if (err) 1980 if (err) 910 return err; 1981 return err; 911 1982 912 return count; 1983 return count; 913 } 1984 } 914 1985 915 static void damon_sysfs_contexts_release(struc 1986 static void damon_sysfs_contexts_release(struct kobject *kobj) 916 { 1987 { 917 kfree(container_of(kobj, struct damon_ 1988 kfree(container_of(kobj, struct damon_sysfs_contexts, kobj)); 918 } 1989 } 919 1990 920 static struct kobj_attribute damon_sysfs_conte 1991 static struct kobj_attribute damon_sysfs_contexts_nr_attr 921 = __ATTR_RW_MODE(nr_contexts, 1992 = __ATTR_RW_MODE(nr_contexts, 0600); 922 1993 923 static struct attribute *damon_sysfs_contexts_ 1994 static struct attribute *damon_sysfs_contexts_attrs[] = { 924 &damon_sysfs_contexts_nr_attr.attr, 1995 &damon_sysfs_contexts_nr_attr.attr, 925 NULL, 1996 NULL, 926 }; 1997 }; 927 ATTRIBUTE_GROUPS(damon_sysfs_contexts); 1998 ATTRIBUTE_GROUPS(damon_sysfs_contexts); 928 1999 929 static const struct kobj_type damon_sysfs_cont !! 2000 static struct kobj_type damon_sysfs_contexts_ktype = { 930 .release = damon_sysfs_contexts_releas 2001 .release = damon_sysfs_contexts_release, 931 .sysfs_ops = &kobj_sysfs_ops, 2002 .sysfs_ops = &kobj_sysfs_ops, 932 .default_groups = damon_sysfs_contexts 2003 .default_groups = damon_sysfs_contexts_groups, 933 }; 2004 }; 934 2005 935 /* 2006 /* 936 * kdamond directory 2007 * kdamond directory 937 */ 2008 */ 938 2009 939 struct damon_sysfs_kdamond { 2010 struct damon_sysfs_kdamond { 940 struct kobject kobj; 2011 struct kobject kobj; 941 struct damon_sysfs_contexts *contexts; 2012 struct damon_sysfs_contexts *contexts; 942 struct damon_ctx *damon_ctx; 2013 struct damon_ctx *damon_ctx; 943 }; 2014 }; 944 2015 945 static struct damon_sysfs_kdamond *damon_sysfs 2016 static struct damon_sysfs_kdamond *damon_sysfs_kdamond_alloc(void) 946 { 2017 { 947 return kzalloc(sizeof(struct damon_sys 2018 return kzalloc(sizeof(struct damon_sysfs_kdamond), GFP_KERNEL); 948 } 2019 } 949 2020 950 static int damon_sysfs_kdamond_add_dirs(struct 2021 static int damon_sysfs_kdamond_add_dirs(struct damon_sysfs_kdamond *kdamond) 951 { 2022 { 952 struct damon_sysfs_contexts *contexts; 2023 struct damon_sysfs_contexts *contexts; 953 int err; 2024 int err; 954 2025 955 contexts = damon_sysfs_contexts_alloc( 2026 contexts = damon_sysfs_contexts_alloc(); 956 if (!contexts) 2027 if (!contexts) 957 return -ENOMEM; 2028 return -ENOMEM; 958 2029 959 err = kobject_init_and_add(&contexts-> 2030 err = kobject_init_and_add(&contexts->kobj, 960 &damon_sysfs_contexts_ 2031 &damon_sysfs_contexts_ktype, &kdamond->kobj, 961 "contexts"); 2032 "contexts"); 962 if (err) { 2033 if (err) { 963 kobject_put(&contexts->kobj); 2034 kobject_put(&contexts->kobj); 964 return err; 2035 return err; 965 } 2036 } 966 kdamond->contexts = contexts; 2037 kdamond->contexts = contexts; 967 2038 968 return err; 2039 return err; 969 } 2040 } 970 2041 971 static void damon_sysfs_kdamond_rm_dirs(struct 2042 static void damon_sysfs_kdamond_rm_dirs(struct damon_sysfs_kdamond *kdamond) 972 { 2043 { 973 damon_sysfs_contexts_rm_dirs(kdamond-> 2044 damon_sysfs_contexts_rm_dirs(kdamond->contexts); 974 kobject_put(&kdamond->contexts->kobj); 2045 kobject_put(&kdamond->contexts->kobj); 975 } 2046 } 976 2047 977 static bool damon_sysfs_ctx_running(struct dam 2048 static bool damon_sysfs_ctx_running(struct damon_ctx *ctx) 978 { 2049 { 979 bool running; 2050 bool running; 980 2051 981 mutex_lock(&ctx->kdamond_lock); 2052 mutex_lock(&ctx->kdamond_lock); 982 running = ctx->kdamond != NULL; 2053 running = ctx->kdamond != NULL; 983 mutex_unlock(&ctx->kdamond_lock); 2054 mutex_unlock(&ctx->kdamond_lock); 984 return running; 2055 return running; 985 } 2056 } 986 2057 987 /* 2058 /* 988 * enum damon_sysfs_cmd - Commands for a speci 2059 * enum damon_sysfs_cmd - Commands for a specific kdamond. 989 */ 2060 */ 990 enum damon_sysfs_cmd { 2061 enum damon_sysfs_cmd { 991 /* @DAMON_SYSFS_CMD_ON: Turn the kdamo 2062 /* @DAMON_SYSFS_CMD_ON: Turn the kdamond on. */ 992 DAMON_SYSFS_CMD_ON, 2063 DAMON_SYSFS_CMD_ON, 993 /* @DAMON_SYSFS_CMD_OFF: Turn the kdam 2064 /* @DAMON_SYSFS_CMD_OFF: Turn the kdamond off. */ 994 DAMON_SYSFS_CMD_OFF, 2065 DAMON_SYSFS_CMD_OFF, 995 /* @DAMON_SYSFS_CMD_COMMIT: Update kda 2066 /* @DAMON_SYSFS_CMD_COMMIT: Update kdamond inputs. */ 996 DAMON_SYSFS_CMD_COMMIT, 2067 DAMON_SYSFS_CMD_COMMIT, 997 /* 2068 /* 998 * @DAMON_SYSFS_CMD_COMMIT_SCHEMES_QUO << 999 * to DAMON. << 1000 */ << 1001 DAMON_SYSFS_CMD_COMMIT_SCHEMES_QUOTA_ << 1002 /* << 1003 * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_ST 2069 * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS: Update scheme stats sysfs 1004 * files. 2070 * files. 1005 */ 2071 */ 1006 DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS, 2072 DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS, 1007 /* 2073 /* 1008 * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_TR << 1009 * tried_regions/total_bytes sysfs fi << 1010 */ << 1011 DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_ << 1012 /* << 1013 * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_TR << 1014 * regions << 1015 */ << 1016 DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_ << 1017 /* << 1018 * @DAMON_SYSFS_CMD_CLEAR_SCHEMES_TRI << 1019 * regions << 1020 */ << 1021 DAMON_SYSFS_CMD_CLEAR_SCHEMES_TRIED_R << 1022 /* << 1023 * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_EF << 1024 * effective size quota of the scheme << 1025 */ << 1026 DAMON_SYSFS_CMD_UPDATE_SCHEMES_EFFECT << 1027 /* << 1028 * @NR_DAMON_SYSFS_CMDS: Total number 2074 * @NR_DAMON_SYSFS_CMDS: Total number of DAMON sysfs commands. 1029 */ 2075 */ 1030 NR_DAMON_SYSFS_CMDS, 2076 NR_DAMON_SYSFS_CMDS, 1031 }; 2077 }; 1032 2078 1033 /* Should match with enum damon_sysfs_cmd */ 2079 /* Should match with enum damon_sysfs_cmd */ 1034 static const char * const damon_sysfs_cmd_str 2080 static const char * const damon_sysfs_cmd_strs[] = { 1035 "on", 2081 "on", 1036 "off", 2082 "off", 1037 "commit", 2083 "commit", 1038 "commit_schemes_quota_goals", << 1039 "update_schemes_stats", 2084 "update_schemes_stats", 1040 "update_schemes_tried_bytes", << 1041 "update_schemes_tried_regions", << 1042 "clear_schemes_tried_regions", << 1043 "update_schemes_effective_quotas", << 1044 }; 2085 }; 1045 2086 1046 /* 2087 /* 1047 * struct damon_sysfs_cmd_request - A request 2088 * struct damon_sysfs_cmd_request - A request to the DAMON callback. 1048 * @cmd: The command that needs to be 2089 * @cmd: The command that needs to be handled by the callback. 1049 * @kdamond: The kobject wrapper that asso 2090 * @kdamond: The kobject wrapper that associated to the kdamond thread. 1050 * 2091 * 1051 * This structure represents a sysfs command 2092 * This structure represents a sysfs command request that need to access some 1052 * DAMON context-internal data. Because DAMO 2093 * DAMON context-internal data. Because DAMON context-internal data can be 1053 * safely accessed from DAMON callbacks witho 2094 * safely accessed from DAMON callbacks without additional synchronization, the 1054 * request will be handled by the DAMON callb 2095 * request will be handled by the DAMON callback. None-``NULL`` @kdamond means 1055 * the request is valid. 2096 * the request is valid. 1056 */ 2097 */ 1057 struct damon_sysfs_cmd_request { 2098 struct damon_sysfs_cmd_request { 1058 enum damon_sysfs_cmd cmd; 2099 enum damon_sysfs_cmd cmd; 1059 struct damon_sysfs_kdamond *kdamond; 2100 struct damon_sysfs_kdamond *kdamond; 1060 }; 2101 }; 1061 2102 1062 /* Current DAMON callback request. Protected 2103 /* Current DAMON callback request. Protected by damon_sysfs_lock. */ 1063 static struct damon_sysfs_cmd_request damon_s 2104 static struct damon_sysfs_cmd_request damon_sysfs_cmd_request; 1064 2105 1065 static ssize_t state_show(struct kobject *kob 2106 static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr, 1066 char *buf) 2107 char *buf) 1067 { 2108 { 1068 struct damon_sysfs_kdamond *kdamond = 2109 struct damon_sysfs_kdamond *kdamond = container_of(kobj, 1069 struct damon_sysfs_kd 2110 struct damon_sysfs_kdamond, kobj); 1070 struct damon_ctx *ctx = kdamond->damo 2111 struct damon_ctx *ctx = kdamond->damon_ctx; 1071 bool running; 2112 bool running; 1072 2113 1073 if (!ctx) 2114 if (!ctx) 1074 running = false; 2115 running = false; 1075 else 2116 else 1076 running = damon_sysfs_ctx_run 2117 running = damon_sysfs_ctx_running(ctx); 1077 2118 1078 return sysfs_emit(buf, "%s\n", runnin 2119 return sysfs_emit(buf, "%s\n", running ? 1079 damon_sysfs_cmd_strs[ 2120 damon_sysfs_cmd_strs[DAMON_SYSFS_CMD_ON] : 1080 damon_sysfs_cmd_strs[ 2121 damon_sysfs_cmd_strs[DAMON_SYSFS_CMD_OFF]); 1081 } 2122 } 1082 2123 1083 static int damon_sysfs_set_attrs(struct damon 2124 static int damon_sysfs_set_attrs(struct damon_ctx *ctx, 1084 struct damon_sysfs_attrs *sys 2125 struct damon_sysfs_attrs *sys_attrs) 1085 { 2126 { 1086 struct damon_sysfs_intervals *sys_int 2127 struct damon_sysfs_intervals *sys_intervals = sys_attrs->intervals; 1087 struct damon_sysfs_ul_range *sys_nr_r 2128 struct damon_sysfs_ul_range *sys_nr_regions = 1088 sys_attrs->nr_regions_range; 2129 sys_attrs->nr_regions_range; 1089 struct damon_attrs attrs = { !! 2130 1090 .sample_interval = sys_interv !! 2131 return damon_set_attrs(ctx, sys_intervals->sample_us, 1091 .aggr_interval = sys_interval !! 2132 sys_intervals->aggr_us, sys_intervals->update_us, 1092 .ops_update_interval = sys_in !! 2133 sys_nr_regions->min, sys_nr_regions->max); 1093 .min_nr_regions = sys_nr_regi << 1094 .max_nr_regions = sys_nr_regi << 1095 }; << 1096 return damon_set_attrs(ctx, &attrs); << 1097 } 2134 } 1098 2135 1099 static void damon_sysfs_destroy_targets(struc 2136 static void damon_sysfs_destroy_targets(struct damon_ctx *ctx) 1100 { 2137 { 1101 struct damon_target *t, *next; 2138 struct damon_target *t, *next; 1102 bool has_pid = damon_target_has_pid(c << 1103 2139 1104 damon_for_each_target_safe(t, next, c 2140 damon_for_each_target_safe(t, next, ctx) { 1105 if (has_pid) !! 2141 if (damon_target_has_pid(ctx)) 1106 put_pid(t->pid); 2142 put_pid(t->pid); 1107 damon_destroy_target(t); 2143 damon_destroy_target(t); 1108 } 2144 } 1109 } 2145 } 1110 2146 1111 static int damon_sysfs_set_regions(struct dam 2147 static int damon_sysfs_set_regions(struct damon_target *t, 1112 struct damon_sysfs_regions *s 2148 struct damon_sysfs_regions *sysfs_regions) 1113 { 2149 { 1114 struct damon_addr_range *ranges = kma 2150 struct damon_addr_range *ranges = kmalloc_array(sysfs_regions->nr, 1115 sizeof(*ranges), GFP_ 2151 sizeof(*ranges), GFP_KERNEL | __GFP_NOWARN); 1116 int i, err = -EINVAL; 2152 int i, err = -EINVAL; 1117 2153 1118 if (!ranges) 2154 if (!ranges) 1119 return -ENOMEM; 2155 return -ENOMEM; 1120 for (i = 0; i < sysfs_regions->nr; i+ 2156 for (i = 0; i < sysfs_regions->nr; i++) { 1121 struct damon_sysfs_region *sy 2157 struct damon_sysfs_region *sys_region = 1122 sysfs_regions->region 2158 sysfs_regions->regions_arr[i]; 1123 2159 1124 if (sys_region->ar.start > sy !! 2160 if (sys_region->start > sys_region->end) 1125 goto out; 2161 goto out; 1126 2162 1127 ranges[i].start = sys_region- !! 2163 ranges[i].start = sys_region->start; 1128 ranges[i].end = sys_region->a !! 2164 ranges[i].end = sys_region->end; 1129 if (i == 0) 2165 if (i == 0) 1130 continue; 2166 continue; 1131 if (ranges[i - 1].end > range 2167 if (ranges[i - 1].end > ranges[i].start) 1132 goto out; 2168 goto out; 1133 } 2169 } 1134 err = damon_set_regions(t, ranges, sy 2170 err = damon_set_regions(t, ranges, sysfs_regions->nr); 1135 out: 2171 out: 1136 kfree(ranges); 2172 kfree(ranges); 1137 return err; 2173 return err; 1138 2174 1139 } 2175 } 1140 2176 1141 static int damon_sysfs_add_target(struct damo 2177 static int damon_sysfs_add_target(struct damon_sysfs_target *sys_target, 1142 struct damon_ctx *ctx) 2178 struct damon_ctx *ctx) 1143 { 2179 { 1144 struct damon_target *t = damon_new_ta 2180 struct damon_target *t = damon_new_target(); 1145 int err = -EINVAL; 2181 int err = -EINVAL; 1146 2182 1147 if (!t) 2183 if (!t) 1148 return -ENOMEM; 2184 return -ENOMEM; 1149 damon_add_target(ctx, t); 2185 damon_add_target(ctx, t); 1150 if (damon_target_has_pid(ctx)) { 2186 if (damon_target_has_pid(ctx)) { 1151 t->pid = find_get_pid(sys_tar 2187 t->pid = find_get_pid(sys_target->pid); 1152 if (!t->pid) 2188 if (!t->pid) 1153 goto destroy_targets_ 2189 goto destroy_targets_out; 1154 } 2190 } 1155 err = damon_sysfs_set_regions(t, sys_ 2191 err = damon_sysfs_set_regions(t, sys_target->regions); 1156 if (err) 2192 if (err) 1157 goto destroy_targets_out; 2193 goto destroy_targets_out; 1158 return 0; 2194 return 0; 1159 2195 1160 destroy_targets_out: 2196 destroy_targets_out: 1161 damon_sysfs_destroy_targets(ctx); 2197 damon_sysfs_destroy_targets(ctx); 1162 return err; 2198 return err; 1163 } 2199 } 1164 2200 1165 static int damon_sysfs_add_targets(struct dam !! 2201 /* >> 2202 * Search a target in a context that corresponds to the sysfs target input. >> 2203 * >> 2204 * Return: pointer to the target if found, NULL if not found, or negative >> 2205 * error code if the search failed. >> 2206 */ >> 2207 static struct damon_target *damon_sysfs_existing_target( >> 2208 struct damon_sysfs_target *sys_target, struct damon_ctx *ctx) >> 2209 { >> 2210 struct pid *pid; >> 2211 struct damon_target *t; >> 2212 >> 2213 if (!damon_target_has_pid(ctx)) { >> 2214 /* Up to only one target for paddr could exist */ >> 2215 damon_for_each_target(t, ctx) >> 2216 return t; >> 2217 return NULL; >> 2218 } >> 2219 >> 2220 /* ops.id should be DAMON_OPS_VADDR or DAMON_OPS_FVADDR */ >> 2221 pid = find_get_pid(sys_target->pid); >> 2222 if (!pid) >> 2223 return ERR_PTR(-EINVAL); >> 2224 damon_for_each_target(t, ctx) { >> 2225 if (t->pid == pid) { >> 2226 put_pid(pid); >> 2227 return t; >> 2228 } >> 2229 } >> 2230 put_pid(pid); >> 2231 return NULL; >> 2232 } >> 2233 >> 2234 static int damon_sysfs_set_targets(struct damon_ctx *ctx, 1166 struct damon_sysfs_targets *s 2235 struct damon_sysfs_targets *sysfs_targets) 1167 { 2236 { 1168 int i, err; 2237 int i, err; 1169 2238 1170 /* Multiple physical address space mo 2239 /* Multiple physical address space monitoring targets makes no sense */ 1171 if (ctx->ops.id == DAMON_OPS_PADDR && 2240 if (ctx->ops.id == DAMON_OPS_PADDR && sysfs_targets->nr > 1) 1172 return -EINVAL; 2241 return -EINVAL; 1173 2242 1174 for (i = 0; i < sysfs_targets->nr; i+ 2243 for (i = 0; i < sysfs_targets->nr; i++) { 1175 struct damon_sysfs_target *st 2244 struct damon_sysfs_target *st = sysfs_targets->targets_arr[i]; >> 2245 struct damon_target *t = damon_sysfs_existing_target(st, ctx); 1176 2246 1177 err = damon_sysfs_add_target( !! 2247 if (IS_ERR(t)) >> 2248 return PTR_ERR(t); >> 2249 if (!t) >> 2250 err = damon_sysfs_add_target(st, ctx); >> 2251 else >> 2252 err = damon_sysfs_set_regions(t, st->regions); 1178 if (err) 2253 if (err) 1179 return err; 2254 return err; 1180 } 2255 } 1181 return 0; 2256 return 0; 1182 } 2257 } 1183 2258 1184 static bool damon_sysfs_schemes_regions_updat !! 2259 static struct damos *damon_sysfs_mk_scheme( >> 2260 struct damon_sysfs_scheme *sysfs_scheme) >> 2261 { >> 2262 struct damon_sysfs_access_pattern *access_pattern = >> 2263 sysfs_scheme->access_pattern; >> 2264 struct damon_sysfs_quotas *sysfs_quotas = sysfs_scheme->quotas; >> 2265 struct damon_sysfs_weights *sysfs_weights = sysfs_quotas->weights; >> 2266 struct damon_sysfs_watermarks *sysfs_wmarks = sysfs_scheme->watermarks; >> 2267 >> 2268 struct damos_access_pattern pattern = { >> 2269 .min_sz_region = access_pattern->sz->min, >> 2270 .max_sz_region = access_pattern->sz->max, >> 2271 .min_nr_accesses = access_pattern->nr_accesses->min, >> 2272 .max_nr_accesses = access_pattern->nr_accesses->max, >> 2273 .min_age_region = access_pattern->age->min, >> 2274 .max_age_region = access_pattern->age->max, >> 2275 }; >> 2276 struct damos_quota quota = { >> 2277 .ms = sysfs_quotas->ms, >> 2278 .sz = sysfs_quotas->sz, >> 2279 .reset_interval = sysfs_quotas->reset_interval_ms, >> 2280 .weight_sz = sysfs_weights->sz, >> 2281 .weight_nr_accesses = sysfs_weights->nr_accesses, >> 2282 .weight_age = sysfs_weights->age, >> 2283 }; >> 2284 struct damos_watermarks wmarks = { >> 2285 .metric = sysfs_wmarks->metric, >> 2286 .interval = sysfs_wmarks->interval_us, >> 2287 .high = sysfs_wmarks->high, >> 2288 .mid = sysfs_wmarks->mid, >> 2289 .low = sysfs_wmarks->low, >> 2290 }; >> 2291 >> 2292 return damon_new_scheme(&pattern, sysfs_scheme->action, "a, >> 2293 &wmarks); >> 2294 } >> 2295 >> 2296 static void damon_sysfs_update_scheme(struct damos *scheme, >> 2297 struct damon_sysfs_scheme *sysfs_scheme) >> 2298 { >> 2299 struct damon_sysfs_access_pattern *access_pattern = >> 2300 sysfs_scheme->access_pattern; >> 2301 struct damon_sysfs_quotas *sysfs_quotas = sysfs_scheme->quotas; >> 2302 struct damon_sysfs_weights *sysfs_weights = sysfs_quotas->weights; >> 2303 struct damon_sysfs_watermarks *sysfs_wmarks = sysfs_scheme->watermarks; >> 2304 >> 2305 scheme->pattern.min_sz_region = access_pattern->sz->min; >> 2306 scheme->pattern.max_sz_region = access_pattern->sz->max; >> 2307 scheme->pattern.min_nr_accesses = access_pattern->nr_accesses->min; >> 2308 scheme->pattern.max_nr_accesses = access_pattern->nr_accesses->max; >> 2309 scheme->pattern.min_age_region = access_pattern->age->min; >> 2310 scheme->pattern.max_age_region = access_pattern->age->max; >> 2311 >> 2312 scheme->action = sysfs_scheme->action; >> 2313 >> 2314 scheme->quota.ms = sysfs_quotas->ms; >> 2315 scheme->quota.sz = sysfs_quotas->sz; >> 2316 scheme->quota.reset_interval = sysfs_quotas->reset_interval_ms; >> 2317 scheme->quota.weight_sz = sysfs_weights->sz; >> 2318 scheme->quota.weight_nr_accesses = sysfs_weights->nr_accesses; >> 2319 scheme->quota.weight_age = sysfs_weights->age; >> 2320 >> 2321 scheme->wmarks.metric = sysfs_wmarks->metric; >> 2322 scheme->wmarks.interval = sysfs_wmarks->interval_us; >> 2323 scheme->wmarks.high = sysfs_wmarks->high; >> 2324 scheme->wmarks.mid = sysfs_wmarks->mid; >> 2325 scheme->wmarks.low = sysfs_wmarks->low; >> 2326 } >> 2327 >> 2328 static int damon_sysfs_set_schemes(struct damon_ctx *ctx, >> 2329 struct damon_sysfs_schemes *sysfs_schemes) >> 2330 { >> 2331 struct damos *scheme, *next; >> 2332 int i = 0; >> 2333 >> 2334 damon_for_each_scheme_safe(scheme, next, ctx) { >> 2335 if (i < sysfs_schemes->nr) >> 2336 damon_sysfs_update_scheme(scheme, >> 2337 sysfs_schemes->schemes_arr[i]); >> 2338 else >> 2339 damon_destroy_scheme(scheme); >> 2340 i++; >> 2341 } >> 2342 >> 2343 for (; i < sysfs_schemes->nr; i++) { >> 2344 struct damos *scheme, *next; >> 2345 >> 2346 scheme = damon_sysfs_mk_scheme(sysfs_schemes->schemes_arr[i]); >> 2347 if (!scheme) { >> 2348 damon_for_each_scheme_safe(scheme, next, ctx) >> 2349 damon_destroy_scheme(scheme); >> 2350 return -ENOMEM; >> 2351 } >> 2352 damon_add_scheme(ctx, scheme); >> 2353 } >> 2354 return 0; >> 2355 } 1185 2356 1186 static void damon_sysfs_before_terminate(stru 2357 static void damon_sysfs_before_terminate(struct damon_ctx *ctx) 1187 { 2358 { 1188 struct damon_target *t, *next; 2359 struct damon_target *t, *next; 1189 struct damon_sysfs_kdamond *kdamond; << 1190 enum damon_sysfs_cmd cmd; << 1191 << 1192 /* damon_sysfs_schemes_update_regions << 1193 kdamond = damon_sysfs_cmd_request.kda << 1194 cmd = damon_sysfs_cmd_request.cmd; << 1195 if (kdamond && ctx == kdamond->damon_ << 1196 (cmd == DAMON_SYSFS_C << 1197 cmd == DAMON_SYSFS_C << 1198 damon_sysfs_schemes_r << 1199 damon_sysfs_schemes_update_re << 1200 damon_sysfs_schemes_regions_u << 1201 mutex_unlock(&damon_sysfs_loc << 1202 } << 1203 2360 1204 if (!damon_target_has_pid(ctx)) !! 2361 if (ctx->ops.id != DAMON_OPS_VADDR && ctx->ops.id != DAMON_OPS_FVADDR) 1205 return; 2362 return; 1206 2363 1207 mutex_lock(&ctx->kdamond_lock); 2364 mutex_lock(&ctx->kdamond_lock); 1208 damon_for_each_target_safe(t, next, c 2365 damon_for_each_target_safe(t, next, ctx) { 1209 put_pid(t->pid); 2366 put_pid(t->pid); 1210 damon_destroy_target(t); 2367 damon_destroy_target(t); 1211 } 2368 } 1212 mutex_unlock(&ctx->kdamond_lock); 2369 mutex_unlock(&ctx->kdamond_lock); 1213 } 2370 } 1214 2371 1215 /* 2372 /* 1216 * damon_sysfs_upd_schemes_stats() - Update s 2373 * damon_sysfs_upd_schemes_stats() - Update schemes stats sysfs files. 1217 * @kdamond: The kobject wrapper that asso 2374 * @kdamond: The kobject wrapper that associated to the kdamond thread. 1218 * 2375 * 1219 * This function reads the schemes stats of s 2376 * This function reads the schemes stats of specific kdamond and update the 1220 * related values for sysfs files. This func 2377 * related values for sysfs files. This function should be called from DAMON 1221 * callbacks while holding ``damon_syfs_lock` 2378 * callbacks while holding ``damon_syfs_lock``, to safely access the DAMON 1222 * contexts-internal data and DAMON sysfs var 2379 * contexts-internal data and DAMON sysfs variables. 1223 */ 2380 */ 1224 static int damon_sysfs_upd_schemes_stats(stru 2381 static int damon_sysfs_upd_schemes_stats(struct damon_sysfs_kdamond *kdamond) 1225 { 2382 { 1226 struct damon_ctx *ctx = kdamond->damo 2383 struct damon_ctx *ctx = kdamond->damon_ctx; >> 2384 struct damon_sysfs_schemes *sysfs_schemes; >> 2385 struct damos *scheme; >> 2386 int schemes_idx = 0; 1227 2387 1228 if (!ctx) 2388 if (!ctx) 1229 return -EINVAL; 2389 return -EINVAL; 1230 damon_sysfs_schemes_update_stats( !! 2390 sysfs_schemes = kdamond->contexts->contexts_arr[0]->schemes; 1231 kdamond->contexts->co !! 2391 damon_for_each_scheme(scheme, ctx) { 1232 return 0; !! 2392 struct damon_sysfs_stats *sysfs_stats; 1233 } << 1234 << 1235 static int damon_sysfs_upd_schemes_regions_st << 1236 struct damon_sysfs_kdamond *k << 1237 { << 1238 struct damon_ctx *ctx = kdamond->damo << 1239 << 1240 if (!ctx) << 1241 return -EINVAL; << 1242 return damon_sysfs_schemes_update_reg << 1243 kdamond->contexts->co << 1244 total_bytes_only); << 1245 } << 1246 << 1247 static int damon_sysfs_upd_schemes_regions_st << 1248 struct damon_sysfs_kdamond *k << 1249 { << 1250 struct damon_ctx *ctx = kdamond->damo << 1251 << 1252 if (!ctx) << 1253 return -EINVAL; << 1254 return damon_sysfs_schemes_update_reg << 1255 } << 1256 2393 1257 static int damon_sysfs_clear_schemes_regions( !! 2394 /* user could have removed the scheme sysfs dir */ 1258 struct damon_sysfs_kdamond *k !! 2395 if (schemes_idx >= sysfs_schemes->nr) 1259 { !! 2396 break; 1260 struct damon_ctx *ctx = kdamond->damo << 1261 2397 1262 if (!ctx) !! 2398 sysfs_stats = sysfs_schemes->schemes_arr[schemes_idx++]->stats; 1263 return -EINVAL; !! 2399 sysfs_stats->nr_tried = scheme->stat.nr_tried; 1264 return damon_sysfs_schemes_clear_regi !! 2400 sysfs_stats->sz_tried = scheme->stat.sz_tried; 1265 kdamond->contexts->co !! 2401 sysfs_stats->nr_applied = scheme->stat.nr_applied; >> 2402 sysfs_stats->sz_applied = scheme->stat.sz_applied; >> 2403 sysfs_stats->qt_exceeds = scheme->stat.qt_exceeds; >> 2404 } >> 2405 return 0; 1266 } 2406 } 1267 2407 1268 static inline bool damon_sysfs_kdamond_runnin 2408 static inline bool damon_sysfs_kdamond_running( 1269 struct damon_sysfs_kdamond *k 2409 struct damon_sysfs_kdamond *kdamond) 1270 { 2410 { 1271 return kdamond->damon_ctx && 2411 return kdamond->damon_ctx && 1272 damon_sysfs_ctx_running(kdamo 2412 damon_sysfs_ctx_running(kdamond->damon_ctx); 1273 } 2413 } 1274 2414 1275 static int damon_sysfs_apply_inputs(struct da 2415 static int damon_sysfs_apply_inputs(struct damon_ctx *ctx, 1276 struct damon_sysfs_context *s 2416 struct damon_sysfs_context *sys_ctx) 1277 { 2417 { 1278 int err; 2418 int err; 1279 2419 1280 err = damon_select_ops(ctx, sys_ctx-> 2420 err = damon_select_ops(ctx, sys_ctx->ops_id); 1281 if (err) 2421 if (err) 1282 return err; 2422 return err; 1283 err = damon_sysfs_set_attrs(ctx, sys_ 2423 err = damon_sysfs_set_attrs(ctx, sys_ctx->attrs); 1284 if (err) 2424 if (err) 1285 return err; 2425 return err; 1286 err = damon_sysfs_add_targets(ctx, sy !! 2426 err = damon_sysfs_set_targets(ctx, sys_ctx->targets); 1287 if (err) 2427 if (err) 1288 return err; 2428 return err; 1289 return damon_sysfs_add_schemes(ctx, s !! 2429 return damon_sysfs_set_schemes(ctx, sys_ctx->schemes); 1290 } 2430 } 1291 2431 1292 static struct damon_ctx *damon_sysfs_build_ct << 1293 struct damon_sysfs_context *s << 1294 << 1295 /* 2432 /* 1296 * damon_sysfs_commit_input() - Commit user i 2433 * damon_sysfs_commit_input() - Commit user inputs to a running kdamond. 1297 * @kdamond: The kobject wrapper for the a 2434 * @kdamond: The kobject wrapper for the associated kdamond. 1298 * 2435 * 1299 * If the sysfs input is wrong, the kdamond w 2436 * If the sysfs input is wrong, the kdamond will be terminated. 1300 */ 2437 */ 1301 static int damon_sysfs_commit_input(struct da 2438 static int damon_sysfs_commit_input(struct damon_sysfs_kdamond *kdamond) 1302 { 2439 { 1303 struct damon_ctx *param_ctx; << 1304 int err; << 1305 << 1306 if (!damon_sysfs_kdamond_running(kdam 2440 if (!damon_sysfs_kdamond_running(kdamond)) 1307 return -EINVAL; 2441 return -EINVAL; 1308 /* TODO: Support multiple contexts pe 2442 /* TODO: Support multiple contexts per kdamond */ 1309 if (kdamond->contexts->nr != 1) 2443 if (kdamond->contexts->nr != 1) 1310 return -EINVAL; 2444 return -EINVAL; 1311 2445 1312 param_ctx = damon_sysfs_build_ctx(kda !! 2446 return damon_sysfs_apply_inputs(kdamond->damon_ctx, 1313 if (IS_ERR(param_ctx)) !! 2447 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 } << 1320 << 1321 static int damon_sysfs_commit_schemes_quota_g << 1322 struct damon_sysfs_kdamond *s << 1323 { << 1324 struct damon_ctx *ctx; << 1325 struct damon_sysfs_context *sysfs_ctx << 1326 << 1327 if (!damon_sysfs_kdamond_running(sysf << 1328 return -EINVAL; << 1329 /* TODO: Support multiple contexts pe << 1330 if (sysfs_kdamond->contexts->nr != 1) << 1331 return -EINVAL; << 1332 << 1333 ctx = sysfs_kdamond->damon_ctx; << 1334 sysfs_ctx = sysfs_kdamond->contexts-> << 1335 return damos_sysfs_set_quota_scores(s << 1336 } << 1337 << 1338 /* << 1339 * damon_sysfs_upd_schemes_effective_quotas() << 1340 * sysfs files. << 1341 * @kdamond: The kobject wrapper that asso << 1342 * << 1343 * This function reads the schemes' effective << 1344 * update the related values for sysfs files. << 1345 * from DAMON callbacks while holding ``damon << 1346 * DAMON contexts-internal data and DAMON sys << 1347 */ << 1348 static int damon_sysfs_upd_schemes_effective_ << 1349 struct damon_sysfs_kdamond *k << 1350 { << 1351 struct damon_ctx *ctx = kdamond->damo << 1352 << 1353 if (!ctx) << 1354 return -EINVAL; << 1355 damos_sysfs_update_effective_quotas( << 1356 kdamond->contexts->co << 1357 return 0; << 1358 } 2448 } 1359 2449 1360 << 1361 /* 2450 /* 1362 * damon_sysfs_cmd_request_callback() - DAMON 2451 * damon_sysfs_cmd_request_callback() - DAMON callback for handling requests. 1363 * @c: The DAMON context of the call !! 2452 * @c: The DAMON context of the callback. 1364 * @active: Whether @c is not deactivated << 1365 * @after_aggr: Whether this is called from a << 1366 * 2453 * 1367 * This function is periodically called back 2454 * This function is periodically called back from the kdamond thread for @c. 1368 * Then, it checks if there is a waiting DAMO 2455 * Then, it checks if there is a waiting DAMON sysfs request and handles it. 1369 */ 2456 */ 1370 static int damon_sysfs_cmd_request_callback(s !! 2457 static int damon_sysfs_cmd_request_callback(struct damon_ctx *c) 1371 bool after_aggregation) << 1372 { 2458 { 1373 struct damon_sysfs_kdamond *kdamond; 2459 struct damon_sysfs_kdamond *kdamond; 1374 bool total_bytes_only = false; << 1375 int err = 0; 2460 int err = 0; 1376 2461 1377 /* avoid deadlock due to concurrent s 2462 /* avoid deadlock due to concurrent state_store('off') */ 1378 if (!damon_sysfs_schemes_regions_upda !! 2463 if (!mutex_trylock(&damon_sysfs_lock)) 1379 !mutex_trylock(&damon << 1380 return 0; 2464 return 0; 1381 kdamond = damon_sysfs_cmd_request.kda 2465 kdamond = damon_sysfs_cmd_request.kdamond; 1382 if (!kdamond || kdamond->damon_ctx != 2466 if (!kdamond || kdamond->damon_ctx != c) 1383 goto out; 2467 goto out; 1384 switch (damon_sysfs_cmd_request.cmd) 2468 switch (damon_sysfs_cmd_request.cmd) { 1385 case DAMON_SYSFS_CMD_UPDATE_SCHEMES_S 2469 case DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS: 1386 err = damon_sysfs_upd_schemes 2470 err = damon_sysfs_upd_schemes_stats(kdamond); 1387 break; 2471 break; 1388 case DAMON_SYSFS_CMD_COMMIT: 2472 case DAMON_SYSFS_CMD_COMMIT: 1389 if (!after_aggregation) << 1390 goto out; << 1391 err = damon_sysfs_commit_inpu 2473 err = damon_sysfs_commit_input(kdamond); 1392 break; 2474 break; 1393 case DAMON_SYSFS_CMD_COMMIT_SCHEMES_Q << 1394 err = damon_sysfs_commit_sche << 1395 break; << 1396 case DAMON_SYSFS_CMD_UPDATE_SCHEMES_T << 1397 total_bytes_only = true; << 1398 fallthrough; << 1399 case DAMON_SYSFS_CMD_UPDATE_SCHEMES_T << 1400 if (!damon_sysfs_schemes_regi << 1401 err = damon_sysfs_upd << 1402 total << 1403 if (!err) { << 1404 damon_sysfs_s << 1405 goto keep_loc << 1406 } << 1407 } else { << 1408 damos_sysfs_mark_fini << 1409 /* << 1410 * Continue regions u << 1411 * active and the upd << 1412 * finished. << 1413 */ << 1414 if (active && !damos_ << 1415 goto keep_loc << 1416 err = damon_sysfs_upd << 1417 damon_sysfs_schemes_r << 1418 } << 1419 break; << 1420 case DAMON_SYSFS_CMD_CLEAR_SCHEMES_TR << 1421 err = damon_sysfs_clear_schem << 1422 break; << 1423 case DAMON_SYSFS_CMD_UPDATE_SCHEMES_E << 1424 err = damon_sysfs_upd_schemes << 1425 break; << 1426 default: 2475 default: 1427 break; 2476 break; 1428 } 2477 } 1429 /* Mark the request as invalid now. * 2478 /* Mark the request as invalid now. */ 1430 damon_sysfs_cmd_request.kdamond = NUL 2479 damon_sysfs_cmd_request.kdamond = NULL; 1431 out: 2480 out: 1432 if (!damon_sysfs_schemes_regions_upda !! 2481 mutex_unlock(&damon_sysfs_lock); 1433 mutex_unlock(&damon_sysfs_loc << 1434 keep_lock_out: << 1435 return err; 2482 return err; 1436 } 2483 } 1437 2484 1438 static int damon_sysfs_after_wmarks_check(str << 1439 { << 1440 /* << 1441 * after_wmarks_check() is called bac << 1442 * by watermarks. << 1443 */ << 1444 return damon_sysfs_cmd_request_callba << 1445 } << 1446 << 1447 static int damon_sysfs_after_sampling(struct << 1448 { << 1449 /* << 1450 * after_sampling() is called back on << 1451 * deactivated by watermarks. << 1452 */ << 1453 return damon_sysfs_cmd_request_callba << 1454 } << 1455 << 1456 static int damon_sysfs_after_aggregation(stru << 1457 { << 1458 /* << 1459 * after_aggregation() is called back << 1460 * deactivated by watermarks. << 1461 */ << 1462 return damon_sysfs_cmd_request_callba << 1463 } << 1464 << 1465 static struct damon_ctx *damon_sysfs_build_ct 2485 static struct damon_ctx *damon_sysfs_build_ctx( 1466 struct damon_sysfs_context *s 2486 struct damon_sysfs_context *sys_ctx) 1467 { 2487 { 1468 struct damon_ctx *ctx = damon_new_ctx 2488 struct damon_ctx *ctx = damon_new_ctx(); 1469 int err; 2489 int err; 1470 2490 1471 if (!ctx) 2491 if (!ctx) 1472 return ERR_PTR(-ENOMEM); 2492 return ERR_PTR(-ENOMEM); 1473 2493 1474 err = damon_sysfs_apply_inputs(ctx, s 2494 err = damon_sysfs_apply_inputs(ctx, sys_ctx); 1475 if (err) { 2495 if (err) { 1476 damon_destroy_ctx(ctx); 2496 damon_destroy_ctx(ctx); 1477 return ERR_PTR(err); 2497 return ERR_PTR(err); 1478 } 2498 } 1479 2499 1480 ctx->callback.after_wmarks_check = da !! 2500 ctx->callback.after_wmarks_check = damon_sysfs_cmd_request_callback; 1481 ctx->callback.after_sampling = damon_ !! 2501 ctx->callback.after_aggregation = damon_sysfs_cmd_request_callback; 1482 ctx->callback.after_aggregation = dam << 1483 ctx->callback.before_terminate = damo 2502 ctx->callback.before_terminate = damon_sysfs_before_terminate; 1484 return ctx; 2503 return ctx; 1485 } 2504 } 1486 2505 1487 static int damon_sysfs_turn_damon_on(struct d 2506 static int damon_sysfs_turn_damon_on(struct damon_sysfs_kdamond *kdamond) 1488 { 2507 { 1489 struct damon_ctx *ctx; 2508 struct damon_ctx *ctx; 1490 int err; 2509 int err; 1491 2510 1492 if (damon_sysfs_kdamond_running(kdamo !! 2511 if (kdamond->damon_ctx && >> 2512 damon_sysfs_ctx_running(kdamond->damon_ctx)) 1493 return -EBUSY; 2513 return -EBUSY; 1494 if (damon_sysfs_cmd_request.kdamond = 2514 if (damon_sysfs_cmd_request.kdamond == kdamond) 1495 return -EBUSY; 2515 return -EBUSY; 1496 /* TODO: support multiple contexts pe 2516 /* TODO: support multiple contexts per kdamond */ 1497 if (kdamond->contexts->nr != 1) 2517 if (kdamond->contexts->nr != 1) 1498 return -EINVAL; 2518 return -EINVAL; 1499 2519 1500 if (kdamond->damon_ctx) 2520 if (kdamond->damon_ctx) 1501 damon_destroy_ctx(kdamond->da 2521 damon_destroy_ctx(kdamond->damon_ctx); 1502 kdamond->damon_ctx = NULL; 2522 kdamond->damon_ctx = NULL; 1503 2523 1504 ctx = damon_sysfs_build_ctx(kdamond-> 2524 ctx = damon_sysfs_build_ctx(kdamond->contexts->contexts_arr[0]); 1505 if (IS_ERR(ctx)) 2525 if (IS_ERR(ctx)) 1506 return PTR_ERR(ctx); 2526 return PTR_ERR(ctx); 1507 err = damon_start(&ctx, 1, false); 2527 err = damon_start(&ctx, 1, false); 1508 if (err) { 2528 if (err) { 1509 damon_destroy_ctx(ctx); 2529 damon_destroy_ctx(ctx); 1510 return err; 2530 return err; 1511 } 2531 } 1512 kdamond->damon_ctx = ctx; 2532 kdamond->damon_ctx = ctx; 1513 return err; 2533 return err; 1514 } 2534 } 1515 2535 1516 static int damon_sysfs_turn_damon_off(struct 2536 static int damon_sysfs_turn_damon_off(struct damon_sysfs_kdamond *kdamond) 1517 { 2537 { 1518 if (!kdamond->damon_ctx) 2538 if (!kdamond->damon_ctx) 1519 return -EINVAL; 2539 return -EINVAL; 1520 return damon_stop(&kdamond->damon_ctx 2540 return damon_stop(&kdamond->damon_ctx, 1); 1521 /* 2541 /* 1522 * To allow users show final monitori 2542 * To allow users show final monitoring results of already turned-off 1523 * DAMON, we free kdamond->damon_ctx 2543 * DAMON, we free kdamond->damon_ctx in next 1524 * damon_sysfs_turn_damon_on(), or kd 2544 * damon_sysfs_turn_damon_on(), or kdamonds_nr_store() 1525 */ 2545 */ 1526 } 2546 } 1527 2547 1528 /* 2548 /* 1529 * damon_sysfs_handle_cmd() - Handle a comman 2549 * damon_sysfs_handle_cmd() - Handle a command for a specific kdamond. 1530 * @cmd: The command to handle. 2550 * @cmd: The command to handle. 1531 * @kdamond: The kobject wrapper for the a 2551 * @kdamond: The kobject wrapper for the associated kdamond. 1532 * 2552 * 1533 * This function handles a DAMON sysfs comman 2553 * This function handles a DAMON sysfs command for a kdamond. For commands 1534 * that need to access running DAMON context- 2554 * that need to access running DAMON context-internal data, it requests 1535 * handling of the command to the DAMON callb 2555 * handling of the command to the DAMON callback 1536 * (@damon_sysfs_cmd_request_callback()) and 2556 * (@damon_sysfs_cmd_request_callback()) and wait until it is properly handled, 1537 * or the context is completed. 2557 * or the context is completed. 1538 * 2558 * 1539 * Return: 0 on success, negative error code 2559 * Return: 0 on success, negative error code otherwise. 1540 */ 2560 */ 1541 static int damon_sysfs_handle_cmd(enum damon_ 2561 static int damon_sysfs_handle_cmd(enum damon_sysfs_cmd cmd, 1542 struct damon_sysfs_kdamond *k 2562 struct damon_sysfs_kdamond *kdamond) 1543 { 2563 { 1544 bool need_wait = true; 2564 bool need_wait = true; 1545 2565 1546 /* Handle commands that doesn't acces 2566 /* Handle commands that doesn't access DAMON context-internal data */ 1547 switch (cmd) { 2567 switch (cmd) { 1548 case DAMON_SYSFS_CMD_ON: 2568 case DAMON_SYSFS_CMD_ON: 1549 return damon_sysfs_turn_damon 2569 return damon_sysfs_turn_damon_on(kdamond); 1550 case DAMON_SYSFS_CMD_OFF: 2570 case DAMON_SYSFS_CMD_OFF: 1551 return damon_sysfs_turn_damon 2571 return damon_sysfs_turn_damon_off(kdamond); 1552 default: 2572 default: 1553 break; 2573 break; 1554 } 2574 } 1555 2575 1556 /* Pass the command to DAMON callback 2576 /* Pass the command to DAMON callback for safe DAMON context access */ 1557 if (damon_sysfs_cmd_request.kdamond) 2577 if (damon_sysfs_cmd_request.kdamond) 1558 return -EBUSY; 2578 return -EBUSY; 1559 if (!damon_sysfs_kdamond_running(kdam 2579 if (!damon_sysfs_kdamond_running(kdamond)) 1560 return -EINVAL; 2580 return -EINVAL; 1561 damon_sysfs_cmd_request.cmd = cmd; 2581 damon_sysfs_cmd_request.cmd = cmd; 1562 damon_sysfs_cmd_request.kdamond = kda 2582 damon_sysfs_cmd_request.kdamond = kdamond; 1563 2583 1564 /* 2584 /* 1565 * wait until damon_sysfs_cmd_request 2585 * wait until damon_sysfs_cmd_request_callback() handles the request 1566 * from kdamond context 2586 * from kdamond context 1567 */ 2587 */ 1568 mutex_unlock(&damon_sysfs_lock); 2588 mutex_unlock(&damon_sysfs_lock); 1569 while (need_wait) { 2589 while (need_wait) { 1570 schedule_timeout_idle(msecs_t 2590 schedule_timeout_idle(msecs_to_jiffies(100)); 1571 if (!mutex_trylock(&damon_sys 2591 if (!mutex_trylock(&damon_sysfs_lock)) 1572 continue; 2592 continue; 1573 if (!damon_sysfs_cmd_request. 2593 if (!damon_sysfs_cmd_request.kdamond) { 1574 /* damon_sysfs_cmd_re 2594 /* damon_sysfs_cmd_request_callback() handled */ 1575 need_wait = false; 2595 need_wait = false; 1576 } else if (!damon_sysfs_kdamo 2596 } else if (!damon_sysfs_kdamond_running(kdamond)) { 1577 /* kdamond has alread 2597 /* kdamond has already finished */ 1578 need_wait = false; 2598 need_wait = false; 1579 damon_sysfs_cmd_reque 2599 damon_sysfs_cmd_request.kdamond = NULL; 1580 } 2600 } 1581 mutex_unlock(&damon_sysfs_loc 2601 mutex_unlock(&damon_sysfs_lock); 1582 } 2602 } 1583 mutex_lock(&damon_sysfs_lock); 2603 mutex_lock(&damon_sysfs_lock); 1584 return 0; 2604 return 0; 1585 } 2605 } 1586 2606 1587 static ssize_t state_store(struct kobject *ko 2607 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr, 1588 const char *buf, size_t count 2608 const char *buf, size_t count) 1589 { 2609 { 1590 struct damon_sysfs_kdamond *kdamond = 2610 struct damon_sysfs_kdamond *kdamond = container_of(kobj, 1591 struct damon_sysfs_kd 2611 struct damon_sysfs_kdamond, kobj); 1592 enum damon_sysfs_cmd cmd; 2612 enum damon_sysfs_cmd cmd; 1593 ssize_t ret = -EINVAL; 2613 ssize_t ret = -EINVAL; 1594 2614 1595 if (!mutex_trylock(&damon_sysfs_lock) 2615 if (!mutex_trylock(&damon_sysfs_lock)) 1596 return -EBUSY; 2616 return -EBUSY; 1597 for (cmd = 0; cmd < NR_DAMON_SYSFS_CM 2617 for (cmd = 0; cmd < NR_DAMON_SYSFS_CMDS; cmd++) { 1598 if (sysfs_streq(buf, damon_sy 2618 if (sysfs_streq(buf, damon_sysfs_cmd_strs[cmd])) { 1599 ret = damon_sysfs_han 2619 ret = damon_sysfs_handle_cmd(cmd, kdamond); 1600 break; 2620 break; 1601 } 2621 } 1602 } 2622 } 1603 mutex_unlock(&damon_sysfs_lock); 2623 mutex_unlock(&damon_sysfs_lock); 1604 if (!ret) 2624 if (!ret) 1605 ret = count; 2625 ret = count; 1606 return ret; 2626 return ret; 1607 } 2627 } 1608 2628 1609 static ssize_t pid_show(struct kobject *kobj, 2629 static ssize_t pid_show(struct kobject *kobj, 1610 struct kobj_attribute *attr, 2630 struct kobj_attribute *attr, char *buf) 1611 { 2631 { 1612 struct damon_sysfs_kdamond *kdamond = 2632 struct damon_sysfs_kdamond *kdamond = container_of(kobj, 1613 struct damon_sysfs_kd 2633 struct damon_sysfs_kdamond, kobj); 1614 struct damon_ctx *ctx; 2634 struct damon_ctx *ctx; 1615 int pid = -1; !! 2635 int pid; 1616 2636 1617 if (!mutex_trylock(&damon_sysfs_lock) 2637 if (!mutex_trylock(&damon_sysfs_lock)) 1618 return -EBUSY; 2638 return -EBUSY; 1619 ctx = kdamond->damon_ctx; 2639 ctx = kdamond->damon_ctx; 1620 if (!ctx) !! 2640 if (!ctx) { >> 2641 pid = -1; 1621 goto out; 2642 goto out; 1622 !! 2643 } 1623 mutex_lock(&ctx->kdamond_lock); 2644 mutex_lock(&ctx->kdamond_lock); 1624 if (ctx->kdamond) !! 2645 if (!ctx->kdamond) >> 2646 pid = -1; >> 2647 else 1625 pid = ctx->kdamond->pid; 2648 pid = ctx->kdamond->pid; 1626 mutex_unlock(&ctx->kdamond_lock); 2649 mutex_unlock(&ctx->kdamond_lock); 1627 out: 2650 out: 1628 mutex_unlock(&damon_sysfs_lock); 2651 mutex_unlock(&damon_sysfs_lock); 1629 return sysfs_emit(buf, "%d\n", pid); 2652 return sysfs_emit(buf, "%d\n", pid); 1630 } 2653 } 1631 2654 1632 static void damon_sysfs_kdamond_release(struc 2655 static void damon_sysfs_kdamond_release(struct kobject *kobj) 1633 { 2656 { 1634 struct damon_sysfs_kdamond *kdamond = 2657 struct damon_sysfs_kdamond *kdamond = container_of(kobj, 1635 struct damon_sysfs_kd 2658 struct damon_sysfs_kdamond, kobj); 1636 2659 1637 if (kdamond->damon_ctx) 2660 if (kdamond->damon_ctx) 1638 damon_destroy_ctx(kdamond->da 2661 damon_destroy_ctx(kdamond->damon_ctx); 1639 kfree(kdamond); 2662 kfree(kdamond); 1640 } 2663 } 1641 2664 1642 static struct kobj_attribute damon_sysfs_kdam 2665 static struct kobj_attribute damon_sysfs_kdamond_state_attr = 1643 __ATTR_RW_MODE(state, 0600); 2666 __ATTR_RW_MODE(state, 0600); 1644 2667 1645 static struct kobj_attribute damon_sysfs_kdam 2668 static struct kobj_attribute damon_sysfs_kdamond_pid_attr = 1646 __ATTR_RO_MODE(pid, 0400); 2669 __ATTR_RO_MODE(pid, 0400); 1647 2670 1648 static struct attribute *damon_sysfs_kdamond_ 2671 static struct attribute *damon_sysfs_kdamond_attrs[] = { 1649 &damon_sysfs_kdamond_state_attr.attr, 2672 &damon_sysfs_kdamond_state_attr.attr, 1650 &damon_sysfs_kdamond_pid_attr.attr, 2673 &damon_sysfs_kdamond_pid_attr.attr, 1651 NULL, 2674 NULL, 1652 }; 2675 }; 1653 ATTRIBUTE_GROUPS(damon_sysfs_kdamond); 2676 ATTRIBUTE_GROUPS(damon_sysfs_kdamond); 1654 2677 1655 static const struct kobj_type damon_sysfs_kda !! 2678 static struct kobj_type damon_sysfs_kdamond_ktype = { 1656 .release = damon_sysfs_kdamond_releas 2679 .release = damon_sysfs_kdamond_release, 1657 .sysfs_ops = &kobj_sysfs_ops, 2680 .sysfs_ops = &kobj_sysfs_ops, 1658 .default_groups = damon_sysfs_kdamond 2681 .default_groups = damon_sysfs_kdamond_groups, 1659 }; 2682 }; 1660 2683 1661 /* 2684 /* 1662 * kdamonds directory 2685 * kdamonds directory 1663 */ 2686 */ 1664 2687 1665 struct damon_sysfs_kdamonds { 2688 struct damon_sysfs_kdamonds { 1666 struct kobject kobj; 2689 struct kobject kobj; 1667 struct damon_sysfs_kdamond **kdamonds 2690 struct damon_sysfs_kdamond **kdamonds_arr; 1668 int nr; 2691 int nr; 1669 }; 2692 }; 1670 2693 1671 static struct damon_sysfs_kdamonds *damon_sys 2694 static struct damon_sysfs_kdamonds *damon_sysfs_kdamonds_alloc(void) 1672 { 2695 { 1673 return kzalloc(sizeof(struct damon_sy 2696 return kzalloc(sizeof(struct damon_sysfs_kdamonds), GFP_KERNEL); 1674 } 2697 } 1675 2698 1676 static void damon_sysfs_kdamonds_rm_dirs(stru 2699 static void damon_sysfs_kdamonds_rm_dirs(struct damon_sysfs_kdamonds *kdamonds) 1677 { 2700 { 1678 struct damon_sysfs_kdamond **kdamonds 2701 struct damon_sysfs_kdamond **kdamonds_arr = kdamonds->kdamonds_arr; 1679 int i; 2702 int i; 1680 2703 1681 for (i = 0; i < kdamonds->nr; i++) { 2704 for (i = 0; i < kdamonds->nr; i++) { 1682 damon_sysfs_kdamond_rm_dirs(k 2705 damon_sysfs_kdamond_rm_dirs(kdamonds_arr[i]); 1683 kobject_put(&kdamonds_arr[i]- 2706 kobject_put(&kdamonds_arr[i]->kobj); 1684 } 2707 } 1685 kdamonds->nr = 0; 2708 kdamonds->nr = 0; 1686 kfree(kdamonds_arr); 2709 kfree(kdamonds_arr); 1687 kdamonds->kdamonds_arr = NULL; 2710 kdamonds->kdamonds_arr = NULL; 1688 } 2711 } 1689 2712 1690 static bool damon_sysfs_kdamonds_busy(struct !! 2713 static int damon_sysfs_nr_running_ctxs(struct damon_sysfs_kdamond **kdamonds, 1691 int nr_kdamonds) 2714 int nr_kdamonds) 1692 { 2715 { >> 2716 int nr_running_ctxs = 0; 1693 int i; 2717 int i; 1694 2718 1695 for (i = 0; i < nr_kdamonds; i++) { 2719 for (i = 0; i < nr_kdamonds; i++) { 1696 if (damon_sysfs_kdamond_runni !! 2720 struct damon_ctx *ctx = kdamonds[i]->damon_ctx; 1697 damon_sysfs_cmd_request.k << 1698 return true; << 1699 } << 1700 2721 1701 return false; !! 2722 if (!ctx) >> 2723 continue; >> 2724 mutex_lock(&ctx->kdamond_lock); >> 2725 if (ctx->kdamond) >> 2726 nr_running_ctxs++; >> 2727 mutex_unlock(&ctx->kdamond_lock); >> 2728 } >> 2729 return nr_running_ctxs; 1702 } 2730 } 1703 2731 1704 static int damon_sysfs_kdamonds_add_dirs(stru 2732 static int damon_sysfs_kdamonds_add_dirs(struct damon_sysfs_kdamonds *kdamonds, 1705 int nr_kdamonds) 2733 int nr_kdamonds) 1706 { 2734 { 1707 struct damon_sysfs_kdamond **kdamonds 2735 struct damon_sysfs_kdamond **kdamonds_arr, *kdamond; 1708 int err, i; 2736 int err, i; 1709 2737 1710 if (damon_sysfs_kdamonds_busy(kdamond !! 2738 if (damon_sysfs_nr_running_ctxs(kdamonds->kdamonds_arr, kdamonds->nr)) 1711 return -EBUSY; 2739 return -EBUSY; 1712 2740 >> 2741 for (i = 0; i < kdamonds->nr; i++) { >> 2742 if (damon_sysfs_cmd_request.kdamond == >> 2743 kdamonds->kdamonds_arr[i]) >> 2744 return -EBUSY; >> 2745 } >> 2746 1713 damon_sysfs_kdamonds_rm_dirs(kdamonds 2747 damon_sysfs_kdamonds_rm_dirs(kdamonds); 1714 if (!nr_kdamonds) 2748 if (!nr_kdamonds) 1715 return 0; 2749 return 0; 1716 2750 1717 kdamonds_arr = kmalloc_array(nr_kdamo 2751 kdamonds_arr = kmalloc_array(nr_kdamonds, sizeof(*kdamonds_arr), 1718 GFP_KERNEL | __GFP_NO 2752 GFP_KERNEL | __GFP_NOWARN); 1719 if (!kdamonds_arr) 2753 if (!kdamonds_arr) 1720 return -ENOMEM; 2754 return -ENOMEM; 1721 kdamonds->kdamonds_arr = kdamonds_arr 2755 kdamonds->kdamonds_arr = kdamonds_arr; 1722 2756 1723 for (i = 0; i < nr_kdamonds; i++) { 2757 for (i = 0; i < nr_kdamonds; i++) { 1724 kdamond = damon_sysfs_kdamond 2758 kdamond = damon_sysfs_kdamond_alloc(); 1725 if (!kdamond) { 2759 if (!kdamond) { 1726 damon_sysfs_kdamonds_ 2760 damon_sysfs_kdamonds_rm_dirs(kdamonds); 1727 return -ENOMEM; 2761 return -ENOMEM; 1728 } 2762 } 1729 2763 1730 err = kobject_init_and_add(&k 2764 err = kobject_init_and_add(&kdamond->kobj, 1731 &damon_sysfs_ 2765 &damon_sysfs_kdamond_ktype, &kdamonds->kobj, 1732 "%d", i); 2766 "%d", i); 1733 if (err) 2767 if (err) 1734 goto out; 2768 goto out; 1735 2769 1736 err = damon_sysfs_kdamond_add 2770 err = damon_sysfs_kdamond_add_dirs(kdamond); 1737 if (err) 2771 if (err) 1738 goto out; 2772 goto out; 1739 2773 1740 kdamonds_arr[i] = kdamond; 2774 kdamonds_arr[i] = kdamond; 1741 kdamonds->nr++; 2775 kdamonds->nr++; 1742 } 2776 } 1743 return 0; 2777 return 0; 1744 2778 1745 out: 2779 out: 1746 damon_sysfs_kdamonds_rm_dirs(kdamonds 2780 damon_sysfs_kdamonds_rm_dirs(kdamonds); 1747 kobject_put(&kdamond->kobj); 2781 kobject_put(&kdamond->kobj); 1748 return err; 2782 return err; 1749 } 2783 } 1750 2784 1751 static ssize_t nr_kdamonds_show(struct kobjec 2785 static ssize_t nr_kdamonds_show(struct kobject *kobj, 1752 struct kobj_attribute *attr, 2786 struct kobj_attribute *attr, char *buf) 1753 { 2787 { 1754 struct damon_sysfs_kdamonds *kdamonds 2788 struct damon_sysfs_kdamonds *kdamonds = container_of(kobj, 1755 struct damon_sysfs_kd 2789 struct damon_sysfs_kdamonds, kobj); 1756 2790 1757 return sysfs_emit(buf, "%d\n", kdamon 2791 return sysfs_emit(buf, "%d\n", kdamonds->nr); 1758 } 2792 } 1759 2793 1760 static ssize_t nr_kdamonds_store(struct kobje 2794 static ssize_t nr_kdamonds_store(struct kobject *kobj, 1761 struct kobj_attribute *attr, 2795 struct kobj_attribute *attr, const char *buf, size_t count) 1762 { 2796 { 1763 struct damon_sysfs_kdamonds *kdamonds !! 2797 struct damon_sysfs_kdamonds *kdamonds = container_of(kobj, >> 2798 struct damon_sysfs_kdamonds, kobj); 1764 int nr, err; 2799 int nr, err; 1765 2800 1766 err = kstrtoint(buf, 0, &nr); 2801 err = kstrtoint(buf, 0, &nr); 1767 if (err) 2802 if (err) 1768 return err; 2803 return err; 1769 if (nr < 0) 2804 if (nr < 0) 1770 return -EINVAL; 2805 return -EINVAL; 1771 2806 1772 kdamonds = container_of(kobj, struct << 1773 << 1774 if (!mutex_trylock(&damon_sysfs_lock) 2807 if (!mutex_trylock(&damon_sysfs_lock)) 1775 return -EBUSY; 2808 return -EBUSY; 1776 err = damon_sysfs_kdamonds_add_dirs(k 2809 err = damon_sysfs_kdamonds_add_dirs(kdamonds, nr); 1777 mutex_unlock(&damon_sysfs_lock); 2810 mutex_unlock(&damon_sysfs_lock); 1778 if (err) 2811 if (err) 1779 return err; 2812 return err; 1780 2813 1781 return count; 2814 return count; 1782 } 2815 } 1783 2816 1784 static void damon_sysfs_kdamonds_release(stru 2817 static void damon_sysfs_kdamonds_release(struct kobject *kobj) 1785 { 2818 { 1786 kfree(container_of(kobj, struct damon 2819 kfree(container_of(kobj, struct damon_sysfs_kdamonds, kobj)); 1787 } 2820 } 1788 2821 1789 static struct kobj_attribute damon_sysfs_kdam 2822 static struct kobj_attribute damon_sysfs_kdamonds_nr_attr = 1790 __ATTR_RW_MODE(nr_kdamonds, 0 2823 __ATTR_RW_MODE(nr_kdamonds, 0600); 1791 2824 1792 static struct attribute *damon_sysfs_kdamonds 2825 static struct attribute *damon_sysfs_kdamonds_attrs[] = { 1793 &damon_sysfs_kdamonds_nr_attr.attr, 2826 &damon_sysfs_kdamonds_nr_attr.attr, 1794 NULL, 2827 NULL, 1795 }; 2828 }; 1796 ATTRIBUTE_GROUPS(damon_sysfs_kdamonds); 2829 ATTRIBUTE_GROUPS(damon_sysfs_kdamonds); 1797 2830 1798 static const struct kobj_type damon_sysfs_kda !! 2831 static struct kobj_type damon_sysfs_kdamonds_ktype = { 1799 .release = damon_sysfs_kdamonds_relea 2832 .release = damon_sysfs_kdamonds_release, 1800 .sysfs_ops = &kobj_sysfs_ops, 2833 .sysfs_ops = &kobj_sysfs_ops, 1801 .default_groups = damon_sysfs_kdamond 2834 .default_groups = damon_sysfs_kdamonds_groups, 1802 }; 2835 }; 1803 2836 1804 /* 2837 /* 1805 * damon user interface directory 2838 * damon user interface directory 1806 */ 2839 */ 1807 2840 1808 struct damon_sysfs_ui_dir { 2841 struct damon_sysfs_ui_dir { 1809 struct kobject kobj; 2842 struct kobject kobj; 1810 struct damon_sysfs_kdamonds *kdamonds 2843 struct damon_sysfs_kdamonds *kdamonds; 1811 }; 2844 }; 1812 2845 1813 static struct damon_sysfs_ui_dir *damon_sysfs 2846 static struct damon_sysfs_ui_dir *damon_sysfs_ui_dir_alloc(void) 1814 { 2847 { 1815 return kzalloc(sizeof(struct damon_sy 2848 return kzalloc(sizeof(struct damon_sysfs_ui_dir), GFP_KERNEL); 1816 } 2849 } 1817 2850 1818 static int damon_sysfs_ui_dir_add_dirs(struct 2851 static int damon_sysfs_ui_dir_add_dirs(struct damon_sysfs_ui_dir *ui_dir) 1819 { 2852 { 1820 struct damon_sysfs_kdamonds *kdamonds 2853 struct damon_sysfs_kdamonds *kdamonds; 1821 int err; 2854 int err; 1822 2855 1823 kdamonds = damon_sysfs_kdamonds_alloc 2856 kdamonds = damon_sysfs_kdamonds_alloc(); 1824 if (!kdamonds) 2857 if (!kdamonds) 1825 return -ENOMEM; 2858 return -ENOMEM; 1826 2859 1827 err = kobject_init_and_add(&kdamonds- 2860 err = kobject_init_and_add(&kdamonds->kobj, 1828 &damon_sysfs_kdamonds 2861 &damon_sysfs_kdamonds_ktype, &ui_dir->kobj, 1829 "kdamonds"); 2862 "kdamonds"); 1830 if (err) { 2863 if (err) { 1831 kobject_put(&kdamonds->kobj); 2864 kobject_put(&kdamonds->kobj); 1832 return err; 2865 return err; 1833 } 2866 } 1834 ui_dir->kdamonds = kdamonds; 2867 ui_dir->kdamonds = kdamonds; 1835 return err; 2868 return err; 1836 } 2869 } 1837 2870 1838 static void damon_sysfs_ui_dir_release(struct 2871 static void damon_sysfs_ui_dir_release(struct kobject *kobj) 1839 { 2872 { 1840 kfree(container_of(kobj, struct damon 2873 kfree(container_of(kobj, struct damon_sysfs_ui_dir, kobj)); 1841 } 2874 } 1842 2875 1843 static struct attribute *damon_sysfs_ui_dir_a 2876 static struct attribute *damon_sysfs_ui_dir_attrs[] = { 1844 NULL, 2877 NULL, 1845 }; 2878 }; 1846 ATTRIBUTE_GROUPS(damon_sysfs_ui_dir); 2879 ATTRIBUTE_GROUPS(damon_sysfs_ui_dir); 1847 2880 1848 static const struct kobj_type damon_sysfs_ui_ !! 2881 static struct kobj_type damon_sysfs_ui_dir_ktype = { 1849 .release = damon_sysfs_ui_dir_release 2882 .release = damon_sysfs_ui_dir_release, 1850 .sysfs_ops = &kobj_sysfs_ops, 2883 .sysfs_ops = &kobj_sysfs_ops, 1851 .default_groups = damon_sysfs_ui_dir_ 2884 .default_groups = damon_sysfs_ui_dir_groups, 1852 }; 2885 }; 1853 2886 1854 static int __init damon_sysfs_init(void) 2887 static int __init damon_sysfs_init(void) 1855 { 2888 { 1856 struct kobject *damon_sysfs_root; 2889 struct kobject *damon_sysfs_root; 1857 struct damon_sysfs_ui_dir *admin; 2890 struct damon_sysfs_ui_dir *admin; 1858 int err; 2891 int err; 1859 2892 1860 damon_sysfs_root = kobject_create_and 2893 damon_sysfs_root = kobject_create_and_add("damon", mm_kobj); 1861 if (!damon_sysfs_root) 2894 if (!damon_sysfs_root) 1862 return -ENOMEM; 2895 return -ENOMEM; 1863 2896 1864 admin = damon_sysfs_ui_dir_alloc(); 2897 admin = damon_sysfs_ui_dir_alloc(); 1865 if (!admin) { 2898 if (!admin) { 1866 kobject_put(damon_sysfs_root) 2899 kobject_put(damon_sysfs_root); 1867 return -ENOMEM; 2900 return -ENOMEM; 1868 } 2901 } 1869 err = kobject_init_and_add(&admin->ko 2902 err = kobject_init_and_add(&admin->kobj, &damon_sysfs_ui_dir_ktype, 1870 damon_sysfs_root, "ad 2903 damon_sysfs_root, "admin"); 1871 if (err) 2904 if (err) 1872 goto out; 2905 goto out; 1873 err = damon_sysfs_ui_dir_add_dirs(adm 2906 err = damon_sysfs_ui_dir_add_dirs(admin); 1874 if (err) 2907 if (err) 1875 goto out; 2908 goto out; 1876 return 0; 2909 return 0; 1877 2910 1878 out: 2911 out: 1879 kobject_put(&admin->kobj); 2912 kobject_put(&admin->kobj); 1880 kobject_put(damon_sysfs_root); 2913 kobject_put(damon_sysfs_root); 1881 return err; 2914 return err; 1882 } 2915 } 1883 subsys_initcall(damon_sysfs_init); 2916 subsys_initcall(damon_sysfs_init); 1884 << 1885 #include "tests/sysfs-kunit.h" << 1886 2917
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.