Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm: cma: support sysfs

Since CMA is getting used more widely, it's more important to keep
monitoring CMA statistics for system health since it's directly related to
user experience.

This patch introduces sysfs statistics for CMA, in order to provide some
basic monitoring of the CMA allocator.

* the number of CMA page successful allocations
* the number of CMA page allocation failures

These two values allow the user to calcuate the allocation
failure rate for each CMA area.

e.g.)
/sys/kernel/mm/cma/WIFI/alloc_pages_[success|fail]
/sys/kernel/mm/cma/SENSOR/alloc_pages_[success|fail]
/sys/kernel/mm/cma/BLUETOOTH/alloc_pages_[success|fail]

The cma_stat was intentionally allocated by dynamic allocation
to harmonize with kobject lifetime management.
https://lore.kernel.org/linux-mm/YCOAmXqt6dZkCQYs@kroah.com/

Link: https://lkml.kernel.org/r/20210324230759.2213957-1-minchan@kernel.org
Link: https://lore.kernel.org/linux-mm/20210316100433.17665-1-colin.king@canonical.com/
Signed-off-by: Minchan Kim <minchan@kernel.org>
Signed-off-by: Colin Ian King <colin.king@canonical.com>

Tested-by: Dmitry Osipenko <digetx@gmail.com>
Reviewed-by: Dmitry Osipenko <digetx@gmail.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: John Hubbard <jhubbard@nvidia.com>
Tested-by: Anders Roxell <anders.roxell@linaro.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: John Dias <joaodias@google.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Colin Ian King <colin.king@canonical.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Minchan Kim and committed by
Linus Torvalds
43ca106f 7bc1aec5

+174 -2
+25
Documentation/ABI/testing/sysfs-kernel-mm-cma
··· 1 + What: /sys/kernel/mm/cma/ 2 + Date: Feb 2021 3 + Contact: Minchan Kim <minchan@kernel.org> 4 + Description: 5 + /sys/kernel/mm/cma/ contains a subdirectory for each CMA 6 + heap name (also sometimes called CMA areas). 7 + 8 + Each CMA heap subdirectory (that is, each 9 + /sys/kernel/mm/cma/<cma-heap-name> directory) contains the 10 + following items: 11 + 12 + alloc_pages_success 13 + alloc_pages_fail 14 + 15 + What: /sys/kernel/mm/cma/<cma-heap-name>/alloc_pages_success 16 + Date: Feb 2021 17 + Contact: Minchan Kim <minchan@kernel.org> 18 + Description: 19 + the number of pages CMA API succeeded to allocate 20 + 21 + What: /sys/kernel/mm/cma/<cma-heap-name>/alloc_pages_fail 22 + Date: Feb 2021 23 + Contact: Minchan Kim <minchan@kernel.org> 24 + Description: 25 + the number of pages CMA API failed to allocate
+7
mm/Kconfig
··· 518 518 help 519 519 Turns on the DebugFS interface for CMA. 520 520 521 + config CMA_SYSFS 522 + bool "CMA information through sysfs interface" 523 + depends on CMA && SYSFS 524 + help 525 + This option exposes some sysfs attributes to get information 526 + from CMA. 527 + 521 528 config CMA_AREAS 522 529 int "Maximum count of the CMA areas" 523 530 depends on CMA
+1
mm/Makefile
··· 109 109 obj-$(CONFIG_MEMORY_BALLOON) += balloon_compaction.o 110 110 obj-$(CONFIG_PAGE_EXTENSION) += page_ext.o 111 111 obj-$(CONFIG_CMA_DEBUGFS) += cma_debug.o 112 + obj-$(CONFIG_CMA_SYSFS) += cma_sysfs.o 112 113 obj-$(CONFIG_USERFAULTFD) += userfaultfd.o 113 114 obj-$(CONFIG_IDLE_PAGE_TRACKING) += page_idle.o 114 115 obj-$(CONFIG_DEBUG_PAGE_REF) += debug_page_ref.o
+6 -2
mm/cma.c
··· 511 511 512 512 pr_debug("%s(): returned %p\n", __func__, page); 513 513 out: 514 - if (page) 514 + if (page) { 515 515 count_vm_event(CMA_ALLOC_SUCCESS); 516 - else 516 + cma_sysfs_account_success_pages(cma, count); 517 + } else { 517 518 count_vm_event(CMA_ALLOC_FAIL); 519 + if (cma) 520 + cma_sysfs_account_fail_pages(cma, count); 521 + } 518 522 519 523 return page; 520 524 }
+23
mm/cma.h
··· 3 3 #define __MM_CMA_H__ 4 4 5 5 #include <linux/debugfs.h> 6 + #include <linux/kobject.h> 7 + 8 + struct cma_kobject { 9 + struct kobject kobj; 10 + struct cma *cma; 11 + }; 6 12 7 13 struct cma { 8 14 unsigned long base_pfn; ··· 22 16 struct debugfs_u32_array dfs_bitmap; 23 17 #endif 24 18 char name[CMA_MAX_NAME]; 19 + #ifdef CONFIG_CMA_SYSFS 20 + /* the number of CMA page successful allocations */ 21 + atomic64_t nr_pages_succeeded; 22 + /* the number of CMA page allocation failures */ 23 + atomic64_t nr_pages_failed; 24 + /* kobject requires dynamic object */ 25 + struct cma_kobject *cma_kobj; 26 + #endif 25 27 }; 26 28 27 29 extern struct cma cma_areas[MAX_CMA_AREAS]; ··· 40 26 return cma->count >> cma->order_per_bit; 41 27 } 42 28 29 + #ifdef CONFIG_CMA_SYSFS 30 + void cma_sysfs_account_success_pages(struct cma *cma, unsigned long nr_pages); 31 + void cma_sysfs_account_fail_pages(struct cma *cma, unsigned long nr_pages); 32 + #else 33 + static inline void cma_sysfs_account_success_pages(struct cma *cma, 34 + unsigned long nr_pages) {}; 35 + static inline void cma_sysfs_account_fail_pages(struct cma *cma, 36 + unsigned long nr_pages) {}; 37 + #endif 43 38 #endif
+112
mm/cma_sysfs.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * CMA SysFS Interface 4 + * 5 + * Copyright (c) 2021 Minchan Kim <minchan@kernel.org> 6 + */ 7 + 8 + #include <linux/cma.h> 9 + #include <linux/kernel.h> 10 + #include <linux/slab.h> 11 + 12 + #include "cma.h" 13 + 14 + #define CMA_ATTR_RO(_name) \ 15 + static struct kobj_attribute _name##_attr = __ATTR_RO(_name) 16 + 17 + void cma_sysfs_account_success_pages(struct cma *cma, unsigned long nr_pages) 18 + { 19 + atomic64_add(nr_pages, &cma->nr_pages_succeeded); 20 + } 21 + 22 + void cma_sysfs_account_fail_pages(struct cma *cma, unsigned long nr_pages) 23 + { 24 + atomic64_add(nr_pages, &cma->nr_pages_failed); 25 + } 26 + 27 + static inline struct cma *cma_from_kobj(struct kobject *kobj) 28 + { 29 + return container_of(kobj, struct cma_kobject, kobj)->cma; 30 + } 31 + 32 + static ssize_t alloc_pages_success_show(struct kobject *kobj, 33 + struct kobj_attribute *attr, char *buf) 34 + { 35 + struct cma *cma = cma_from_kobj(kobj); 36 + 37 + return sysfs_emit(buf, "%llu\n", 38 + atomic64_read(&cma->nr_pages_succeeded)); 39 + } 40 + CMA_ATTR_RO(alloc_pages_success); 41 + 42 + static ssize_t alloc_pages_fail_show(struct kobject *kobj, 43 + struct kobj_attribute *attr, char *buf) 44 + { 45 + struct cma *cma = cma_from_kobj(kobj); 46 + 47 + return sysfs_emit(buf, "%llu\n", atomic64_read(&cma->nr_pages_failed)); 48 + } 49 + CMA_ATTR_RO(alloc_pages_fail); 50 + 51 + static void cma_kobj_release(struct kobject *kobj) 52 + { 53 + struct cma *cma = cma_from_kobj(kobj); 54 + struct cma_kobject *cma_kobj = cma->cma_kobj; 55 + 56 + kfree(cma_kobj); 57 + cma->cma_kobj = NULL; 58 + } 59 + 60 + static struct attribute *cma_attrs[] = { 61 + &alloc_pages_success_attr.attr, 62 + &alloc_pages_fail_attr.attr, 63 + NULL, 64 + }; 65 + ATTRIBUTE_GROUPS(cma); 66 + 67 + static struct kobj_type cma_ktype = { 68 + .release = cma_kobj_release, 69 + .sysfs_ops = &kobj_sysfs_ops, 70 + .default_groups = cma_groups, 71 + }; 72 + 73 + static int __init cma_sysfs_init(void) 74 + { 75 + struct kobject *cma_kobj_root; 76 + struct cma_kobject *cma_kobj; 77 + struct cma *cma; 78 + int i, err; 79 + 80 + cma_kobj_root = kobject_create_and_add("cma", mm_kobj); 81 + if (!cma_kobj_root) 82 + return -ENOMEM; 83 + 84 + for (i = 0; i < cma_area_count; i++) { 85 + cma_kobj = kzalloc(sizeof(*cma_kobj), GFP_KERNEL); 86 + if (!cma_kobj) { 87 + err = -ENOMEM; 88 + goto out; 89 + } 90 + 91 + cma = &cma_areas[i]; 92 + cma->cma_kobj = cma_kobj; 93 + cma_kobj->cma = cma; 94 + err = kobject_init_and_add(&cma_kobj->kobj, &cma_ktype, 95 + cma_kobj_root, "%s", cma->name); 96 + if (err) { 97 + kobject_put(&cma_kobj->kobj); 98 + goto out; 99 + } 100 + } 101 + 102 + return 0; 103 + out: 104 + while (--i >= 0) { 105 + cma = &cma_areas[i]; 106 + kobject_put(&cma->cma_kobj->kobj); 107 + } 108 + kobject_put(cma_kobj_root); 109 + 110 + return err; 111 + } 112 + subsys_initcall(cma_sysfs_init);