Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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