cma_sysfs.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "cma.h"
  11. #define CMA_ATTR_RO(_name) \
  12. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  13. void cma_sysfs_account_success_pages(struct cma *cma, unsigned long nr_pages)
  14. {
  15. atomic64_add(nr_pages, &cma->nr_pages_succeeded);
  16. }
  17. void cma_sysfs_account_fail_pages(struct cma *cma, unsigned long nr_pages)
  18. {
  19. atomic64_add(nr_pages, &cma->nr_pages_failed);
  20. }
  21. void cma_sysfs_account_release_pages(struct cma *cma, unsigned long nr_pages)
  22. {
  23. atomic64_add(nr_pages, &cma->nr_pages_released);
  24. }
  25. static inline struct cma *cma_from_kobj(struct kobject *kobj)
  26. {
  27. return container_of(kobj, struct cma_kobject, kobj)->cma;
  28. }
  29. static ssize_t alloc_pages_success_show(struct kobject *kobj,
  30. struct kobj_attribute *attr, char *buf)
  31. {
  32. struct cma *cma = cma_from_kobj(kobj);
  33. return sysfs_emit(buf, "%llu\n",
  34. atomic64_read(&cma->nr_pages_succeeded));
  35. }
  36. CMA_ATTR_RO(alloc_pages_success);
  37. static ssize_t alloc_pages_fail_show(struct kobject *kobj,
  38. struct kobj_attribute *attr, char *buf)
  39. {
  40. struct cma *cma = cma_from_kobj(kobj);
  41. return sysfs_emit(buf, "%llu\n", atomic64_read(&cma->nr_pages_failed));
  42. }
  43. CMA_ATTR_RO(alloc_pages_fail);
  44. static ssize_t release_pages_success_show(struct kobject *kobj,
  45. struct kobj_attribute *attr, char *buf)
  46. {
  47. struct cma *cma = cma_from_kobj(kobj);
  48. return sysfs_emit(buf, "%llu\n", atomic64_read(&cma->nr_pages_released));
  49. }
  50. CMA_ATTR_RO(release_pages_success);
  51. static ssize_t total_pages_show(struct kobject *kobj,
  52. struct kobj_attribute *attr, char *buf)
  53. {
  54. struct cma *cma = cma_from_kobj(kobj);
  55. return sysfs_emit(buf, "%lu\n", cma->count);
  56. }
  57. CMA_ATTR_RO(total_pages);
  58. static ssize_t available_pages_show(struct kobject *kobj,
  59. struct kobj_attribute *attr, char *buf)
  60. {
  61. struct cma *cma = cma_from_kobj(kobj);
  62. return sysfs_emit(buf, "%lu\n", cma->available_count);
  63. }
  64. CMA_ATTR_RO(available_pages);
  65. static void cma_kobj_release(struct kobject *kobj)
  66. {
  67. struct cma *cma = cma_from_kobj(kobj);
  68. struct cma_kobject *cma_kobj = cma->cma_kobj;
  69. kfree(cma_kobj);
  70. cma->cma_kobj = NULL;
  71. }
  72. static struct attribute *cma_attrs[] = {
  73. &alloc_pages_success_attr.attr,
  74. &alloc_pages_fail_attr.attr,
  75. &release_pages_success_attr.attr,
  76. &total_pages_attr.attr,
  77. &available_pages_attr.attr,
  78. NULL,
  79. };
  80. ATTRIBUTE_GROUPS(cma);
  81. static const struct kobj_type cma_ktype = {
  82. .release = cma_kobj_release,
  83. .sysfs_ops = &kobj_sysfs_ops,
  84. .default_groups = cma_groups,
  85. };
  86. static int __init cma_sysfs_init(void)
  87. {
  88. struct kobject *cma_kobj_root;
  89. struct cma_kobject *cma_kobj;
  90. struct cma *cma;
  91. int i, err;
  92. cma_kobj_root = kobject_create_and_add("cma", mm_kobj);
  93. if (!cma_kobj_root)
  94. return -ENOMEM;
  95. for (i = 0; i < cma_area_count; i++) {
  96. cma_kobj = kzalloc_obj(*cma_kobj);
  97. if (!cma_kobj) {
  98. err = -ENOMEM;
  99. goto out;
  100. }
  101. cma = &cma_areas[i];
  102. cma->cma_kobj = cma_kobj;
  103. cma_kobj->cma = cma;
  104. err = kobject_init_and_add(&cma_kobj->kobj, &cma_ktype,
  105. cma_kobj_root, "%s", cma->name);
  106. if (err) {
  107. kobject_put(&cma_kobj->kobj);
  108. goto out;
  109. }
  110. }
  111. return 0;
  112. out:
  113. while (--i >= 0) {
  114. cma = &cma_areas[i];
  115. kobject_put(&cma->cma_kobj->kobj);
  116. }
  117. kobject_put(cma_kobj_root);
  118. return err;
  119. }
  120. subsys_initcall(cma_sysfs_init);