xref: /linux/kernel/ksysfs.c (revision 6e7fd890f1d6ac83805409e9c346240de2705584)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * kernel/ksysfs.c - sysfs attributes in /sys/kernel, which
4  * 		     are not related to any other subsystem
5  *
6  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
7  */
8 
9 #include <asm/byteorder.h>
10 #include <linux/kobject.h>
11 #include <linux/string.h>
12 #include <linux/sysfs.h>
13 #include <linux/export.h>
14 #include <linux/init.h>
15 #include <linux/kexec.h>
16 #include <linux/profile.h>
17 #include <linux/stat.h>
18 #include <linux/sched.h>
19 #include <linux/capability.h>
20 #include <linux/compiler.h>
21 
22 #include <linux/rcupdate.h>	/* rcu_expedited and rcu_normal */
23 
24 #if defined(__LITTLE_ENDIAN)
25 #define CPU_BYTEORDER_STRING	"little"
26 #elif defined(__BIG_ENDIAN)
27 #define CPU_BYTEORDER_STRING	"big"
28 #else
29 #error Unknown byteorder
30 #endif
31 
32 #define KERNEL_ATTR_RO(_name) \
33 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
34 
35 #define KERNEL_ATTR_RW(_name) \
36 static struct kobj_attribute _name##_attr = __ATTR_RW(_name)
37 
38 /* current uevent sequence number */
39 static ssize_t uevent_seqnum_show(struct kobject *kobj,
40 				  struct kobj_attribute *attr, char *buf)
41 {
42 	return sysfs_emit(buf, "%llu\n", (u64)atomic64_read(&uevent_seqnum));
43 }
44 KERNEL_ATTR_RO(uevent_seqnum);
45 
46 /* cpu byteorder */
47 static ssize_t cpu_byteorder_show(struct kobject *kobj,
48 				  struct kobj_attribute *attr, char *buf)
49 {
50 	return sysfs_emit(buf, "%s\n", CPU_BYTEORDER_STRING);
51 }
52 KERNEL_ATTR_RO(cpu_byteorder);
53 
54 /* address bits */
55 static ssize_t address_bits_show(struct kobject *kobj,
56 				 struct kobj_attribute *attr, char *buf)
57 {
58 	return sysfs_emit(buf, "%zu\n", sizeof(void *) * 8 /* CHAR_BIT */);
59 }
60 KERNEL_ATTR_RO(address_bits);
61 
62 #ifdef CONFIG_UEVENT_HELPER
63 /* uevent helper program, used during early boot */
64 static ssize_t uevent_helper_show(struct kobject *kobj,
65 				  struct kobj_attribute *attr, char *buf)
66 {
67 	return sysfs_emit(buf, "%s\n", uevent_helper);
68 }
69 static ssize_t uevent_helper_store(struct kobject *kobj,
70 				   struct kobj_attribute *attr,
71 				   const char *buf, size_t count)
72 {
73 	if (count+1 > UEVENT_HELPER_PATH_LEN)
74 		return -ENOENT;
75 	memcpy(uevent_helper, buf, count);
76 	uevent_helper[count] = '\0';
77 	if (count && uevent_helper[count-1] == '\n')
78 		uevent_helper[count-1] = '\0';
79 	return count;
80 }
81 KERNEL_ATTR_RW(uevent_helper);
82 #endif
83 
84 #ifdef CONFIG_PROFILING
85 static ssize_t profiling_show(struct kobject *kobj,
86 				  struct kobj_attribute *attr, char *buf)
87 {
88 	return sysfs_emit(buf, "%d\n", prof_on);
89 }
90 static ssize_t profiling_store(struct kobject *kobj,
91 				   struct kobj_attribute *attr,
92 				   const char *buf, size_t count)
93 {
94 	int ret;
95 	static DEFINE_MUTEX(lock);
96 
97 	/*
98 	 * We need serialization, for profile_setup() initializes prof_on
99 	 * value and profile_init() must not reallocate prof_buffer after
100 	 * once allocated.
101 	 */
102 	guard(mutex)(&lock);
103 	if (prof_on)
104 		return -EEXIST;
105 	/*
106 	 * This eventually calls into get_option() which
107 	 * has a ton of callers and is not const.  It is
108 	 * easiest to cast it away here.
109 	 */
110 	profile_setup((char *)buf);
111 	ret = profile_init();
112 	if (ret)
113 		return ret;
114 	ret = create_proc_profile();
115 	if (ret)
116 		return ret;
117 	return count;
118 }
119 KERNEL_ATTR_RW(profiling);
120 #endif
121 
122 #ifdef CONFIG_KEXEC_CORE
123 static ssize_t kexec_loaded_show(struct kobject *kobj,
124 				 struct kobj_attribute *attr, char *buf)
125 {
126 	return sysfs_emit(buf, "%d\n", !!kexec_image);
127 }
128 KERNEL_ATTR_RO(kexec_loaded);
129 
130 #ifdef CONFIG_CRASH_DUMP
131 static ssize_t kexec_crash_loaded_show(struct kobject *kobj,
132 				       struct kobj_attribute *attr, char *buf)
133 {
134 	return sysfs_emit(buf, "%d\n", kexec_crash_loaded());
135 }
136 KERNEL_ATTR_RO(kexec_crash_loaded);
137 
138 static ssize_t kexec_crash_size_show(struct kobject *kobj,
139 				       struct kobj_attribute *attr, char *buf)
140 {
141 	ssize_t size = crash_get_memory_size();
142 
143 	if (size < 0)
144 		return size;
145 
146 	return sysfs_emit(buf, "%zd\n", size);
147 }
148 static ssize_t kexec_crash_size_store(struct kobject *kobj,
149 				   struct kobj_attribute *attr,
150 				   const char *buf, size_t count)
151 {
152 	unsigned long cnt;
153 	int ret;
154 
155 	if (kstrtoul(buf, 0, &cnt))
156 		return -EINVAL;
157 
158 	ret = crash_shrink_memory(cnt);
159 	return ret < 0 ? ret : count;
160 }
161 KERNEL_ATTR_RW(kexec_crash_size);
162 
163 #endif /* CONFIG_CRASH_DUMP*/
164 #endif /* CONFIG_KEXEC_CORE */
165 
166 #ifdef CONFIG_VMCORE_INFO
167 
168 static ssize_t vmcoreinfo_show(struct kobject *kobj,
169 			       struct kobj_attribute *attr, char *buf)
170 {
171 	phys_addr_t vmcore_base = paddr_vmcoreinfo_note();
172 	return sysfs_emit(buf, "%pa %x\n", &vmcore_base,
173 			  (unsigned int)VMCOREINFO_NOTE_SIZE);
174 }
175 KERNEL_ATTR_RO(vmcoreinfo);
176 
177 #ifdef CONFIG_CRASH_HOTPLUG
178 static ssize_t crash_elfcorehdr_size_show(struct kobject *kobj,
179 			       struct kobj_attribute *attr, char *buf)
180 {
181 	unsigned int sz = crash_get_elfcorehdr_size();
182 
183 	return sysfs_emit(buf, "%u\n", sz);
184 }
185 KERNEL_ATTR_RO(crash_elfcorehdr_size);
186 
187 #endif
188 
189 #endif /* CONFIG_VMCORE_INFO */
190 
191 /* whether file capabilities are enabled */
192 static ssize_t fscaps_show(struct kobject *kobj,
193 				  struct kobj_attribute *attr, char *buf)
194 {
195 	return sysfs_emit(buf, "%d\n", file_caps_enabled);
196 }
197 KERNEL_ATTR_RO(fscaps);
198 
199 #ifndef CONFIG_TINY_RCU
200 int rcu_expedited;
201 static ssize_t rcu_expedited_show(struct kobject *kobj,
202 				  struct kobj_attribute *attr, char *buf)
203 {
204 	return sysfs_emit(buf, "%d\n", READ_ONCE(rcu_expedited));
205 }
206 static ssize_t rcu_expedited_store(struct kobject *kobj,
207 				   struct kobj_attribute *attr,
208 				   const char *buf, size_t count)
209 {
210 	if (kstrtoint(buf, 0, &rcu_expedited))
211 		return -EINVAL;
212 
213 	return count;
214 }
215 KERNEL_ATTR_RW(rcu_expedited);
216 
217 int rcu_normal;
218 static ssize_t rcu_normal_show(struct kobject *kobj,
219 			       struct kobj_attribute *attr, char *buf)
220 {
221 	return sysfs_emit(buf, "%d\n", READ_ONCE(rcu_normal));
222 }
223 static ssize_t rcu_normal_store(struct kobject *kobj,
224 				struct kobj_attribute *attr,
225 				const char *buf, size_t count)
226 {
227 	if (kstrtoint(buf, 0, &rcu_normal))
228 		return -EINVAL;
229 
230 	return count;
231 }
232 KERNEL_ATTR_RW(rcu_normal);
233 #endif /* #ifndef CONFIG_TINY_RCU */
234 
235 /*
236  * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
237  */
238 extern const void __start_notes;
239 extern const void __stop_notes;
240 #define	notes_size (&__stop_notes - &__start_notes)
241 
242 static ssize_t notes_read(struct file *filp, struct kobject *kobj,
243 			  struct bin_attribute *bin_attr,
244 			  char *buf, loff_t off, size_t count)
245 {
246 	memcpy(buf, &__start_notes + off, count);
247 	return count;
248 }
249 
250 static struct bin_attribute notes_attr __ro_after_init  = {
251 	.attr = {
252 		.name = "notes",
253 		.mode = S_IRUGO,
254 	},
255 	.read = &notes_read,
256 };
257 
258 struct kobject *kernel_kobj;
259 EXPORT_SYMBOL_GPL(kernel_kobj);
260 
261 static struct attribute * kernel_attrs[] = {
262 	&fscaps_attr.attr,
263 	&uevent_seqnum_attr.attr,
264 	&cpu_byteorder_attr.attr,
265 	&address_bits_attr.attr,
266 #ifdef CONFIG_UEVENT_HELPER
267 	&uevent_helper_attr.attr,
268 #endif
269 #ifdef CONFIG_PROFILING
270 	&profiling_attr.attr,
271 #endif
272 #ifdef CONFIG_KEXEC_CORE
273 	&kexec_loaded_attr.attr,
274 #ifdef CONFIG_CRASH_DUMP
275 	&kexec_crash_loaded_attr.attr,
276 	&kexec_crash_size_attr.attr,
277 #endif
278 #endif
279 #ifdef CONFIG_VMCORE_INFO
280 	&vmcoreinfo_attr.attr,
281 #ifdef CONFIG_CRASH_HOTPLUG
282 	&crash_elfcorehdr_size_attr.attr,
283 #endif
284 #endif
285 #ifndef CONFIG_TINY_RCU
286 	&rcu_expedited_attr.attr,
287 	&rcu_normal_attr.attr,
288 #endif
289 	NULL
290 };
291 
292 static const struct attribute_group kernel_attr_group = {
293 	.attrs = kernel_attrs,
294 };
295 
296 static int __init ksysfs_init(void)
297 {
298 	int error;
299 
300 	kernel_kobj = kobject_create_and_add("kernel", NULL);
301 	if (!kernel_kobj) {
302 		error = -ENOMEM;
303 		goto exit;
304 	}
305 	error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
306 	if (error)
307 		goto kset_exit;
308 
309 	if (notes_size > 0) {
310 		notes_attr.size = notes_size;
311 		error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
312 		if (error)
313 			goto group_exit;
314 	}
315 
316 	return 0;
317 
318 group_exit:
319 	sysfs_remove_group(kernel_kobj, &kernel_attr_group);
320 kset_exit:
321 	kobject_put(kernel_kobj);
322 exit:
323 	return error;
324 }
325 
326 core_initcall(ksysfs_init);
327