gpio: of: Break out OF-only code
[linux/fpc-iii.git] / kernel / locking / lock_events.c
blobfa2c2f951c6b5f8571cd576c2012c57ddd46dbb2
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * Authors: Waiman Long <waiman.long@hpe.com>
17 * Collect locking event counts
19 #include <linux/debugfs.h>
20 #include <linux/sched.h>
21 #include <linux/sched/clock.h>
22 #include <linux/fs.h>
24 #include "lock_events.h"
26 #undef LOCK_EVENT
27 #define LOCK_EVENT(name) [LOCKEVENT_ ## name] = #name,
29 #define LOCK_EVENTS_DIR "lock_event_counts"
32 * When CONFIG_LOCK_EVENT_COUNTS is enabled, event counts of different
33 * types of locks will be reported under the <debugfs>/lock_event_counts/
34 * directory. See lock_events_list.h for the list of available locking
35 * events.
37 * Writing to the special ".reset_counts" file will reset all the above
38 * locking event counts. This is a very slow operation and so should not
39 * be done frequently.
41 * These event counts are implemented as per-cpu variables which are
42 * summed and computed whenever the corresponding debugfs files are read. This
43 * minimizes added overhead making the counts usable even in a production
44 * environment.
46 static const char * const lockevent_names[lockevent_num + 1] = {
48 #include "lock_events_list.h"
50 [LOCKEVENT_reset_cnts] = ".reset_counts",
54 * Per-cpu counts
56 DEFINE_PER_CPU(unsigned long, lockevents[lockevent_num]);
59 * The lockevent_read() function can be overridden.
61 ssize_t __weak lockevent_read(struct file *file, char __user *user_buf,
62 size_t count, loff_t *ppos)
64 char buf[64];
65 int cpu, id, len;
66 u64 sum = 0;
69 * Get the counter ID stored in file->f_inode->i_private
71 id = (long)file_inode(file)->i_private;
73 if (id >= lockevent_num)
74 return -EBADF;
76 for_each_possible_cpu(cpu)
77 sum += per_cpu(lockevents[id], cpu);
78 len = snprintf(buf, sizeof(buf) - 1, "%llu\n", sum);
80 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
84 * Function to handle write request
86 * When idx = reset_cnts, reset all the counts.
88 static ssize_t lockevent_write(struct file *file, const char __user *user_buf,
89 size_t count, loff_t *ppos)
91 int cpu;
94 * Get the counter ID stored in file->f_inode->i_private
96 if ((long)file_inode(file)->i_private != LOCKEVENT_reset_cnts)
97 return count;
99 for_each_possible_cpu(cpu) {
100 int i;
101 unsigned long *ptr = per_cpu_ptr(lockevents, cpu);
103 for (i = 0 ; i < lockevent_num; i++)
104 WRITE_ONCE(ptr[i], 0);
106 return count;
110 * Debugfs data structures
112 static const struct file_operations fops_lockevent = {
113 .read = lockevent_read,
114 .write = lockevent_write,
115 .llseek = default_llseek,
118 #ifdef CONFIG_PARAVIRT_SPINLOCKS
119 #include <asm/paravirt.h>
121 static bool __init skip_lockevent(const char *name)
123 static int pv_on __initdata = -1;
125 if (pv_on < 0)
126 pv_on = !pv_is_native_spin_unlock();
128 * Skip PV qspinlock events on bare metal.
130 if (!pv_on && !memcmp(name, "pv_", 3))
131 return true;
132 return false;
134 #else
135 static inline bool skip_lockevent(const char *name)
137 return false;
139 #endif
142 * Initialize debugfs for the locking event counts.
144 static int __init init_lockevent_counts(void)
146 struct dentry *d_counts = debugfs_create_dir(LOCK_EVENTS_DIR, NULL);
147 int i;
149 if (!d_counts)
150 goto out;
153 * Create the debugfs files
155 * As reading from and writing to the stat files can be slow, only
156 * root is allowed to do the read/write to limit impact to system
157 * performance.
159 for (i = 0; i < lockevent_num; i++) {
160 if (skip_lockevent(lockevent_names[i]))
161 continue;
162 if (!debugfs_create_file(lockevent_names[i], 0400, d_counts,
163 (void *)(long)i, &fops_lockevent))
164 goto fail_undo;
167 if (!debugfs_create_file(lockevent_names[LOCKEVENT_reset_cnts], 0200,
168 d_counts, (void *)(long)LOCKEVENT_reset_cnts,
169 &fops_lockevent))
170 goto fail_undo;
172 return 0;
173 fail_undo:
174 debugfs_remove_recursive(d_counts);
175 out:
176 pr_warn("Could not create '%s' debugfs entries\n", LOCK_EVENTS_DIR);
177 return -ENOMEM;
179 fs_initcall(init_lockevent_counts);