cma_sysfs.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * CMA SysFS Interface
  4. *
  5. * Copyright (c) 2021 Minchan Kim <[email protected]>
  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. static inline struct cma *cma_from_kobj(struct kobject *kobj)
  22. {
  23. return container_of(kobj, struct cma_kobject, kobj)->cma;
  24. }
  25. static ssize_t alloc_pages_success_show(struct kobject *kobj,
  26. struct kobj_attribute *attr, char *buf)
  27. {
  28. struct cma *cma = cma_from_kobj(kobj);
  29. return sysfs_emit(buf, "%llu\n",
  30. atomic64_read(&cma->nr_pages_succeeded));
  31. }
  32. CMA_ATTR_RO(alloc_pages_success);
  33. static ssize_t alloc_pages_fail_show(struct kobject *kobj,
  34. struct kobj_attribute *attr, char *buf)
  35. {
  36. struct cma *cma = cma_from_kobj(kobj);
  37. return sysfs_emit(buf, "%llu\n", atomic64_read(&cma->nr_pages_failed));
  38. }
  39. CMA_ATTR_RO(alloc_pages_fail);
  40. static void cma_kobj_release(struct kobject *kobj)
  41. {
  42. struct cma *cma = cma_from_kobj(kobj);
  43. struct cma_kobject *cma_kobj = cma->cma_kobj;
  44. kfree(cma_kobj);
  45. cma->cma_kobj = NULL;
  46. }
  47. static struct attribute *cma_attrs[] = {
  48. &alloc_pages_success_attr.attr,
  49. &alloc_pages_fail_attr.attr,
  50. NULL,
  51. };
  52. ATTRIBUTE_GROUPS(cma);
  53. static struct kobj_type cma_ktype = {
  54. .release = cma_kobj_release,
  55. .sysfs_ops = &kobj_sysfs_ops,
  56. .default_groups = cma_groups,
  57. };
  58. static int __init cma_sysfs_init(void)
  59. {
  60. struct kobject *cma_kobj_root;
  61. struct cma_kobject *cma_kobj;
  62. struct cma *cma;
  63. int i, err;
  64. cma_kobj_root = kobject_create_and_add("cma", mm_kobj);
  65. if (!cma_kobj_root)
  66. return -ENOMEM;
  67. for (i = 0; i < cma_area_count; i++) {
  68. cma_kobj = kzalloc(sizeof(*cma_kobj), GFP_KERNEL);
  69. if (!cma_kobj) {
  70. err = -ENOMEM;
  71. goto out;
  72. }
  73. cma = &cma_areas[i];
  74. cma->cma_kobj = cma_kobj;
  75. cma_kobj->cma = cma;
  76. err = kobject_init_and_add(&cma_kobj->kobj, &cma_ktype,
  77. cma_kobj_root, "%s", cma->name);
  78. if (err) {
  79. kobject_put(&cma_kobj->kobj);
  80. goto out;
  81. }
  82. }
  83. return 0;
  84. out:
  85. while (--i >= 0) {
  86. cma = &cma_areas[i];
  87. kobject_put(&cma->cma_kobj->kobj);
  88. }
  89. kobject_put(cma_kobj_root);
  90. return err;
  91. }
  92. subsys_initcall(cma_sysfs_init);