cma_sysfs.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * CMA SysFS Interface
  4. *
  5. * Copyright (c) 2021 Minchan Kim <minchan@kernel.org>
  6. */
  7. #include <linux/cma.h>
  8. #include <linux/kernel.h>
  9. #include <linux/slab.h>
  10. #include <linux/module.h>
  11. #include "cma.h"
  12. static bool experimental;
  13. #define CMA_ATTR_RO(_name) \
  14. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  15. void cma_sysfs_account_success_pages(struct cma *cma, unsigned long nr_pages)
  16. {
  17. atomic64_add(nr_pages, &cma->nr_pages_succeeded);
  18. }
  19. void cma_sysfs_account_fail_pages(struct cma *cma, unsigned long nr_pages)
  20. {
  21. atomic64_add(nr_pages, &cma->nr_pages_failed);
  22. }
  23. static inline struct cma *cma_from_kobj(struct kobject *kobj)
  24. {
  25. return container_of(kobj, struct cma_kobject, kobj)->cma;
  26. }
  27. static ssize_t alloc_pages_success_show(struct kobject *kobj,
  28. struct kobj_attribute *attr, char *buf)
  29. {
  30. struct cma *cma = cma_from_kobj(kobj);
  31. return sysfs_emit(buf, "%llu\n",
  32. atomic64_read(&cma->nr_pages_succeeded));
  33. }
  34. CMA_ATTR_RO(alloc_pages_success);
  35. static ssize_t alloc_pages_fail_show(struct kobject *kobj,
  36. struct kobj_attribute *attr, char *buf)
  37. {
  38. struct cma *cma = cma_from_kobj(kobj);
  39. return sysfs_emit(buf, "%llu\n", atomic64_read(&cma->nr_pages_failed));
  40. }
  41. CMA_ATTR_RO(alloc_pages_fail);
  42. static void cma_kobj_release(struct kobject *kobj)
  43. {
  44. struct cma *cma = cma_from_kobj(kobj);
  45. struct cma_kobject *cma_kobj = cma->cma_kobj;
  46. kfree(cma_kobj);
  47. cma->cma_kobj = NULL;
  48. }
  49. static struct attribute *cma_attrs[] = {
  50. &alloc_pages_success_attr.attr,
  51. &alloc_pages_fail_attr.attr,
  52. NULL,
  53. };
  54. ATTRIBUTE_GROUPS(cma);
  55. static struct kobj_type cma_ktype = {
  56. .release = cma_kobj_release,
  57. .sysfs_ops = &kobj_sysfs_ops,
  58. .default_groups = cma_groups,
  59. };
  60. static int __init cma_sysfs_init(void)
  61. {
  62. struct kobject *cma_kobj_root;
  63. struct cma_kobject *cma_kobj;
  64. struct cma *cma;
  65. int i, err;
  66. if (!experimental)
  67. return 0;
  68. cma_kobj_root = kobject_create_and_add("cma", mm_kobj);
  69. if (!cma_kobj_root)
  70. return -ENOMEM;
  71. for (i = 0; i < cma_area_count; i++) {
  72. cma_kobj = kzalloc(sizeof(*cma_kobj), GFP_KERNEL);
  73. if (!cma_kobj) {
  74. err = -ENOMEM;
  75. goto out;
  76. }
  77. cma = &cma_areas[i];
  78. cma->cma_kobj = cma_kobj;
  79. cma_kobj->cma = cma;
  80. err = kobject_init_and_add(&cma_kobj->kobj, &cma_ktype,
  81. cma_kobj_root, "%s", cma->name);
  82. if (err) {
  83. kobject_put(&cma_kobj->kobj);
  84. goto out;
  85. }
  86. }
  87. return 0;
  88. out:
  89. while (--i >= 0) {
  90. cma = &cma_areas[i];
  91. kobject_put(&cma->cma_kobj->kobj);
  92. }
  93. kobject_put(cma_kobj_root);
  94. return err;
  95. }
  96. subsys_initcall(cma_sysfs_init);
  97. module_param(experimental, bool, 0400);