blob: 3b053c04dd86fd926720c7843ba8a918cbc4e63d [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>
14#include <linux/module.h>
15#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>
Dhaval Giani5cb350b2007-10-15 17:00:14 +020018#include <linux/sched.h>
Ludwig Nussel088ab0b2011-02-28 15:57:17 +010019#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#define KERNEL_ATTR_RO(_name) \
Kay Sievers386f2752007-11-02 13:47:53 +010022static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define KERNEL_ATTR_RW(_name) \
Kay Sievers386f2752007-11-02 13:47:53 +010025static struct kobj_attribute _name##_attr = \
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 __ATTR(_name, 0644, _name##_show, _name##_store)
27
Ming Leicd3772e2008-11-16 18:22:09 +080028#if defined(CONFIG_HOTPLUG)
Kay Sievers0f76e5a2005-11-11 04:58:04 +010029/* current uevent sequence number */
Kay Sievers386f2752007-11-02 13:47:53 +010030static ssize_t uevent_seqnum_show(struct kobject *kobj,
31 struct kobj_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Kay Sievers386f2752007-11-02 13:47:53 +010033 return sprintf(buf, "%llu\n", (unsigned long long)uevent_seqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034}
Kay Sievers0f76e5a2005-11-11 04:58:04 +010035KERNEL_ATTR_RO(uevent_seqnum);
36
Thadeu Lima de Souza Cascardoaf665852010-01-17 19:14:26 -020037/* uevent helper program, used during early boot */
Kay Sievers386f2752007-11-02 13:47:53 +010038static ssize_t uevent_helper_show(struct kobject *kobj,
39 struct kobj_attribute *attr, char *buf)
Kay Sievers0f76e5a2005-11-11 04:58:04 +010040{
Kay Sievers386f2752007-11-02 13:47:53 +010041 return sprintf(buf, "%s\n", uevent_helper);
Kay Sievers0f76e5a2005-11-11 04:58:04 +010042}
Kay Sievers386f2752007-11-02 13:47:53 +010043static ssize_t uevent_helper_store(struct kobject *kobj,
44 struct kobj_attribute *attr,
45 const char *buf, size_t count)
Kay Sievers0f76e5a2005-11-11 04:58:04 +010046{
Kay Sievers312c0042005-11-16 09:00:00 +010047 if (count+1 > UEVENT_HELPER_PATH_LEN)
Kay Sievers0f76e5a2005-11-11 04:58:04 +010048 return -ENOENT;
Kay Sievers386f2752007-11-02 13:47:53 +010049 memcpy(uevent_helper, buf, count);
Kay Sievers312c0042005-11-16 09:00:00 +010050 uevent_helper[count] = '\0';
51 if (count && uevent_helper[count-1] == '\n')
52 uevent_helper[count-1] = '\0';
Kay Sievers0f76e5a2005-11-11 04:58:04 +010053 return count;
54}
55KERNEL_ATTR_RW(uevent_helper);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#endif
57
Dave Hansen22b8ce92008-10-15 22:01:46 -070058#ifdef CONFIG_PROFILING
59static ssize_t profiling_show(struct kobject *kobj,
60 struct kobj_attribute *attr, char *buf)
61{
62 return sprintf(buf, "%d\n", prof_on);
63}
64static ssize_t profiling_store(struct kobject *kobj,
65 struct kobj_attribute *attr,
66 const char *buf, size_t count)
67{
68 int ret;
69
70 if (prof_on)
71 return -EEXIST;
72 /*
73 * This eventually calls into get_option() which
74 * has a ton of callers and is not const. It is
75 * easiest to cast it away here.
76 */
77 profile_setup((char *)buf);
78 ret = profile_init();
79 if (ret)
80 return ret;
81 ret = create_proc_profile();
82 if (ret)
83 return ret;
84 return count;
85}
86KERNEL_ATTR_RW(profiling);
87#endif
88
Jeff Moyerc330dda2006-06-23 02:05:07 -070089#ifdef CONFIG_KEXEC
Kay Sievers386f2752007-11-02 13:47:53 +010090static ssize_t kexec_loaded_show(struct kobject *kobj,
91 struct kobj_attribute *attr, char *buf)
Jeff Moyerc330dda2006-06-23 02:05:07 -070092{
Kay Sievers386f2752007-11-02 13:47:53 +010093 return sprintf(buf, "%d\n", !!kexec_image);
Jeff Moyerc330dda2006-06-23 02:05:07 -070094}
95KERNEL_ATTR_RO(kexec_loaded);
96
Kay Sievers386f2752007-11-02 13:47:53 +010097static ssize_t kexec_crash_loaded_show(struct kobject *kobj,
98 struct kobj_attribute *attr, char *buf)
Jeff Moyerc330dda2006-06-23 02:05:07 -070099{
Kay Sievers386f2752007-11-02 13:47:53 +0100100 return sprintf(buf, "%d\n", !!kexec_crash_image);
Jeff Moyerc330dda2006-06-23 02:05:07 -0700101}
102KERNEL_ATTR_RO(kexec_crash_loaded);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700103
Amerigo Wang06a7f712009-12-15 16:47:46 -0800104static ssize_t kexec_crash_size_show(struct kobject *kobj,
105 struct kobj_attribute *attr, char *buf)
106{
107 return sprintf(buf, "%zu\n", crash_get_memory_size());
108}
109static ssize_t kexec_crash_size_store(struct kobject *kobj,
110 struct kobj_attribute *attr,
111 const char *buf, size_t count)
112{
113 unsigned long cnt;
114 int ret;
115
116 if (strict_strtoul(buf, 0, &cnt))
117 return -EINVAL;
118
119 ret = crash_shrink_memory(cnt);
120 return ret < 0 ? ret : count;
121}
122KERNEL_ATTR_RW(kexec_crash_size);
123
Kay Sievers386f2752007-11-02 13:47:53 +0100124static ssize_t vmcoreinfo_show(struct kobject *kobj,
125 struct kobj_attribute *attr, char *buf)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700126{
Kay Sievers386f2752007-11-02 13:47:53 +0100127 return sprintf(buf, "%lx %x\n",
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700128 paddr_vmcoreinfo_note(),
Ken'ichi Ohmichid7682812007-10-16 23:27:28 -0700129 (unsigned int)vmcoreinfo_max_size);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700130}
131KERNEL_ATTR_RO(vmcoreinfo);
132
Jeff Moyerc330dda2006-06-23 02:05:07 -0700133#endif /* CONFIG_KEXEC */
134
Ludwig Nussel088ab0b2011-02-28 15:57:17 +0100135/* whether file capabilities are enabled */
136static ssize_t fscaps_show(struct kobject *kobj,
137 struct kobj_attribute *attr, char *buf)
138{
139 return sprintf(buf, "%d\n", file_caps_enabled);
140}
141KERNEL_ATTR_RO(fscaps);
142
Roland McGrathda1a6792007-07-19 01:48:39 -0700143/*
144 * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
145 */
David Howells0b1937a2007-07-20 17:02:04 +0100146extern const void __start_notes __attribute__((weak));
147extern const void __stop_notes __attribute__((weak));
Roland McGrathda1a6792007-07-19 01:48:39 -0700148#define notes_size (&__stop_notes - &__start_notes)
149
Chris Wright2c3c8be2010-05-12 18:28:57 -0700150static ssize_t notes_read(struct file *filp, struct kobject *kobj,
151 struct bin_attribute *bin_attr,
Roland McGrathda1a6792007-07-19 01:48:39 -0700152 char *buf, loff_t off, size_t count)
153{
154 memcpy(buf, &__start_notes + off, count);
155 return count;
156}
157
158static struct bin_attribute notes_attr = {
159 .attr = {
160 .name = "notes",
161 .mode = S_IRUGO,
162 },
163 .read = &notes_read,
164};
165
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -0800166struct kobject *kernel_kobj;
167EXPORT_SYMBOL_GPL(kernel_kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169static struct attribute * kernel_attrs[] = {
Ludwig Nussel088ab0b2011-02-28 15:57:17 +0100170 &fscaps_attr.attr,
Ming Leicd3772e2008-11-16 18:22:09 +0800171#if defined(CONFIG_HOTPLUG)
Kay Sievers0f76e5a2005-11-11 04:58:04 +0100172 &uevent_seqnum_attr.attr,
173 &uevent_helper_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#endif
Dave Hansen22b8ce92008-10-15 22:01:46 -0700175#ifdef CONFIG_PROFILING
176 &profiling_attr.attr,
177#endif
Jeff Moyerc330dda2006-06-23 02:05:07 -0700178#ifdef CONFIG_KEXEC
179 &kexec_loaded_attr.attr,
180 &kexec_crash_loaded_attr.attr,
Amerigo Wang06a7f712009-12-15 16:47:46 -0800181 &kexec_crash_size_attr.attr,
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700182 &vmcoreinfo_attr.attr,
Jeff Moyerc330dda2006-06-23 02:05:07 -0700183#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 NULL
185};
186
187static struct attribute_group kernel_attr_group = {
188 .attrs = kernel_attrs,
189};
190
191static int __init ksysfs_init(void)
192{
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100193 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -0800195 kernel_kobj = kobject_create_and_add("kernel", NULL);
196 if (!kernel_kobj) {
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100197 error = -ENOMEM;
198 goto exit;
199 }
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -0800200 error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100201 if (error)
202 goto kset_exit;
203
204 if (notes_size > 0) {
Roland McGrathda1a6792007-07-19 01:48:39 -0700205 notes_attr.size = notes_size;
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -0800206 error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100207 if (error)
208 goto group_exit;
Roland McGrathda1a6792007-07-19 01:48:39 -0700209 }
210
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100211 return 0;
212
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100213group_exit:
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -0800214 sysfs_remove_group(kernel_kobj, &kernel_attr_group);
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100215kset_exit:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -0800216 kobject_put(kernel_kobj);
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100217exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return error;
219}
220
221core_initcall(ksysfs_init);