blob: e660964086e2e46d1066e143d29c21887f712d5d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/ksysfs.c - sysfs attributes in /sys/kernel, which
3 * are not related to any other subsystem
4 *
5 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6 *
7 * This file is release under the GPLv2
8 *
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kobject.h>
12#include <linux/string.h>
13#include <linux/sysfs.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
Jeff Moyerc330dda2006-06-23 02:05:07 -070016#include <linux/kexec.h>
Dave Hansen22b8ce92008-10-15 22:01:46 -070017#include <linux/profile.h>
Paul Gortmaker15964252011-07-28 14:22:29 -040018#include <linux/stat.h>
Dhaval Giani5cb350b2007-10-15 17:00:14 +020019#include <linux/sched.h>
Ludwig Nussel088ab0b2011-02-28 15:57:17 +010020#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Paul Gortmaker7a754742014-02-11 16:10:12 -050022#include <linux/rcupdate.h> /* rcu_expedited */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define KERNEL_ATTR_RO(_name) \
Kay Sievers386f2752007-11-02 13:47:53 +010025static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#define KERNEL_ATTR_RW(_name) \
Kay Sievers386f2752007-11-02 13:47:53 +010028static struct kobj_attribute _name##_attr = \
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 __ATTR(_name, 0644, _name##_show, _name##_store)
30
Kay Sievers0f76e5a2005-11-11 04:58:04 +010031/* current uevent sequence number */
Kay Sievers386f2752007-11-02 13:47:53 +010032static ssize_t uevent_seqnum_show(struct kobject *kobj,
33 struct kobj_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Kay Sievers386f2752007-11-02 13:47:53 +010035 return sprintf(buf, "%llu\n", (unsigned long long)uevent_seqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
Kay Sievers0f76e5a2005-11-11 04:58:04 +010037KERNEL_ATTR_RO(uevent_seqnum);
38
Thadeu Lima de Souza Cascardoaf665852010-01-17 19:14:26 -020039/* uevent helper program, used during early boot */
Kay Sievers386f2752007-11-02 13:47:53 +010040static ssize_t uevent_helper_show(struct kobject *kobj,
41 struct kobj_attribute *attr, char *buf)
Kay Sievers0f76e5a2005-11-11 04:58:04 +010042{
Kay Sievers386f2752007-11-02 13:47:53 +010043 return sprintf(buf, "%s\n", uevent_helper);
Kay Sievers0f76e5a2005-11-11 04:58:04 +010044}
Kay Sievers386f2752007-11-02 13:47:53 +010045static ssize_t uevent_helper_store(struct kobject *kobj,
46 struct kobj_attribute *attr,
47 const char *buf, size_t count)
Kay Sievers0f76e5a2005-11-11 04:58:04 +010048{
Kay Sievers312c0042005-11-16 09:00:00 +010049 if (count+1 > UEVENT_HELPER_PATH_LEN)
Kay Sievers0f76e5a2005-11-11 04:58:04 +010050 return -ENOENT;
Kay Sievers386f2752007-11-02 13:47:53 +010051 memcpy(uevent_helper, buf, count);
Kay Sievers312c0042005-11-16 09:00:00 +010052 uevent_helper[count] = '\0';
53 if (count && uevent_helper[count-1] == '\n')
54 uevent_helper[count-1] = '\0';
Kay Sievers0f76e5a2005-11-11 04:58:04 +010055 return count;
56}
57KERNEL_ATTR_RW(uevent_helper);
Bill Pembertone3a1a5e2012-11-19 13:19:30 -050058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Dave Hansen22b8ce92008-10-15 22:01:46 -070060#ifdef CONFIG_PROFILING
61static ssize_t profiling_show(struct kobject *kobj,
62 struct kobj_attribute *attr, char *buf)
63{
64 return sprintf(buf, "%d\n", prof_on);
65}
66static ssize_t profiling_store(struct kobject *kobj,
67 struct kobj_attribute *attr,
68 const char *buf, size_t count)
69{
70 int ret;
71
72 if (prof_on)
73 return -EEXIST;
74 /*
75 * This eventually calls into get_option() which
76 * has a ton of callers and is not const. It is
77 * easiest to cast it away here.
78 */
79 profile_setup((char *)buf);
80 ret = profile_init();
81 if (ret)
82 return ret;
83 ret = create_proc_profile();
84 if (ret)
85 return ret;
86 return count;
87}
88KERNEL_ATTR_RW(profiling);
89#endif
90
Jeff Moyerc330dda2006-06-23 02:05:07 -070091#ifdef CONFIG_KEXEC
Kay Sievers386f2752007-11-02 13:47:53 +010092static ssize_t kexec_loaded_show(struct kobject *kobj,
93 struct kobj_attribute *attr, char *buf)
Jeff Moyerc330dda2006-06-23 02:05:07 -070094{
Kay Sievers386f2752007-11-02 13:47:53 +010095 return sprintf(buf, "%d\n", !!kexec_image);
Jeff Moyerc330dda2006-06-23 02:05:07 -070096}
97KERNEL_ATTR_RO(kexec_loaded);
98
Kay Sievers386f2752007-11-02 13:47:53 +010099static ssize_t kexec_crash_loaded_show(struct kobject *kobj,
100 struct kobj_attribute *attr, char *buf)
Jeff Moyerc330dda2006-06-23 02:05:07 -0700101{
Kay Sievers386f2752007-11-02 13:47:53 +0100102 return sprintf(buf, "%d\n", !!kexec_crash_image);
Jeff Moyerc330dda2006-06-23 02:05:07 -0700103}
104KERNEL_ATTR_RO(kexec_crash_loaded);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700105
Amerigo Wang06a7f712009-12-15 16:47:46 -0800106static ssize_t kexec_crash_size_show(struct kobject *kobj,
107 struct kobj_attribute *attr, char *buf)
108{
109 return sprintf(buf, "%zu\n", crash_get_memory_size());
110}
111static ssize_t kexec_crash_size_store(struct kobject *kobj,
112 struct kobj_attribute *attr,
113 const char *buf, size_t count)
114{
115 unsigned long cnt;
116 int ret;
117
Jingoo Han6072ddc2013-09-12 15:14:07 -0700118 if (kstrtoul(buf, 0, &cnt))
Amerigo Wang06a7f712009-12-15 16:47:46 -0800119 return -EINVAL;
120
121 ret = crash_shrink_memory(cnt);
122 return ret < 0 ? ret : count;
123}
124KERNEL_ATTR_RW(kexec_crash_size);
125
Kay Sievers386f2752007-11-02 13:47:53 +0100126static ssize_t vmcoreinfo_show(struct kobject *kobj,
127 struct kobj_attribute *attr, char *buf)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700128{
Kay Sievers386f2752007-11-02 13:47:53 +0100129 return sprintf(buf, "%lx %x\n",
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700130 paddr_vmcoreinfo_note(),
Vivek Goyal77019962014-01-23 15:56:00 -0800131 (unsigned int)sizeof(vmcoreinfo_note));
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700132}
133KERNEL_ATTR_RO(vmcoreinfo);
134
Jeff Moyerc330dda2006-06-23 02:05:07 -0700135#endif /* CONFIG_KEXEC */
136
Ludwig Nussel088ab0b2011-02-28 15:57:17 +0100137/* whether file capabilities are enabled */
138static ssize_t fscaps_show(struct kobject *kobj,
139 struct kobj_attribute *attr, char *buf)
140{
141 return sprintf(buf, "%d\n", file_caps_enabled);
142}
143KERNEL_ATTR_RO(fscaps);
144
Antti P Miettinen3705b882012-10-05 09:59:15 +0300145int rcu_expedited;
146static ssize_t rcu_expedited_show(struct kobject *kobj,
147 struct kobj_attribute *attr, char *buf)
148{
149 return sprintf(buf, "%d\n", rcu_expedited);
150}
151static ssize_t rcu_expedited_store(struct kobject *kobj,
152 struct kobj_attribute *attr,
153 const char *buf, size_t count)
154{
155 if (kstrtoint(buf, 0, &rcu_expedited))
156 return -EINVAL;
157
158 return count;
159}
160KERNEL_ATTR_RW(rcu_expedited);
161
Roland McGrathda1a6792007-07-19 01:48:39 -0700162/*
163 * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
164 */
David Howells0b1937a2007-07-20 17:02:04 +0100165extern const void __start_notes __attribute__((weak));
166extern const void __stop_notes __attribute__((weak));
Roland McGrathda1a6792007-07-19 01:48:39 -0700167#define notes_size (&__stop_notes - &__start_notes)
168
Chris Wright2c3c8be2010-05-12 18:28:57 -0700169static ssize_t notes_read(struct file *filp, struct kobject *kobj,
170 struct bin_attribute *bin_attr,
Roland McGrathda1a6792007-07-19 01:48:39 -0700171 char *buf, loff_t off, size_t count)
172{
173 memcpy(buf, &__start_notes + off, count);
174 return count;
175}
176
177static struct bin_attribute notes_attr = {
178 .attr = {
179 .name = "notes",
180 .mode = S_IRUGO,
181 },
182 .read = &notes_read,
183};
184
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -0800185struct kobject *kernel_kobj;
186EXPORT_SYMBOL_GPL(kernel_kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188static struct attribute * kernel_attrs[] = {
Ludwig Nussel088ab0b2011-02-28 15:57:17 +0100189 &fscaps_attr.attr,
Kay Sievers0f76e5a2005-11-11 04:58:04 +0100190 &uevent_seqnum_attr.attr,
191 &uevent_helper_attr.attr,
Dave Hansen22b8ce92008-10-15 22:01:46 -0700192#ifdef CONFIG_PROFILING
193 &profiling_attr.attr,
194#endif
Jeff Moyerc330dda2006-06-23 02:05:07 -0700195#ifdef CONFIG_KEXEC
196 &kexec_loaded_attr.attr,
197 &kexec_crash_loaded_attr.attr,
Amerigo Wang06a7f712009-12-15 16:47:46 -0800198 &kexec_crash_size_attr.attr,
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700199 &vmcoreinfo_attr.attr,
Jeff Moyerc330dda2006-06-23 02:05:07 -0700200#endif
Antti P Miettinen3705b882012-10-05 09:59:15 +0300201 &rcu_expedited_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 NULL
203};
204
205static struct attribute_group kernel_attr_group = {
206 .attrs = kernel_attrs,
207};
208
209static int __init ksysfs_init(void)
210{
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100211 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -0800213 kernel_kobj = kobject_create_and_add("kernel", NULL);
214 if (!kernel_kobj) {
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100215 error = -ENOMEM;
216 goto exit;
217 }
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -0800218 error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100219 if (error)
220 goto kset_exit;
221
222 if (notes_size > 0) {
Roland McGrathda1a6792007-07-19 01:48:39 -0700223 notes_attr.size = notes_size;
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -0800224 error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100225 if (error)
226 goto group_exit;
Roland McGrathda1a6792007-07-19 01:48:39 -0700227 }
228
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100229 return 0;
230
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100231group_exit:
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -0800232 sysfs_remove_group(kernel_kobj, &kernel_attr_group);
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100233kset_exit:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -0800234 kobject_put(kernel_kobj);
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100235exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return error;
237}
238
239core_initcall(ksysfs_init);