Blame view

kernel/ksysfs.c 6.2 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
  /*
   * kernel/ksysfs.c - sysfs attributes in /sys/kernel, which
   * 		     are not related to any other subsystem
   *
   * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
   * 
   * This file is release under the GPLv2
   *
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
11
12
  #include <linux/kobject.h>
  #include <linux/string.h>
  #include <linux/sysfs.h>
9984de1a5   Paul Gortmaker   kernel: Map most ...
13
  #include <linux/export.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
  #include <linux/init.h>
c330dda90   Jeff Moyer   [PATCH] Add a sys...
15
  #include <linux/kexec.h>
22b8ce947   Dave Hansen   profiling: dynami...
16
  #include <linux/profile.h>
1596425fd   Paul Gortmaker   kernel: ksysfs.c ...
17
  #include <linux/stat.h>
5cb350baf   Dhaval Giani   sched: group sche...
18
  #include <linux/sched.h>
088ab0b4d   Ludwig Nussel   kernel/ksysfs.c: ...
19
  #include <linux/capability.h>
52f5684c8   Gideon Israel Dsouza   kernel: use macro...
20
  #include <linux/compiler.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21

5a9be7c62   Paul E. McKenney   rcu: Add rcu_norm...
22
  #include <linux/rcupdate.h>	/* rcu_expedited and rcu_normal */
7a7547431   Paul Gortmaker   rcu: Fix sparse w...
23

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
  #define KERNEL_ATTR_RO(_name) \
386f275f5   Kay Sievers   Driver Core: swit...
25
  static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
  
  #define KERNEL_ATTR_RW(_name) \
386f275f5   Kay Sievers   Driver Core: swit...
28
  static struct kobj_attribute _name##_attr = \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
  	__ATTR(_name, 0644, _name##_show, _name##_store)
0f76e5acf   Kay Sievers   [PATCH] add ueven...
30
  /* current uevent sequence number */
386f275f5   Kay Sievers   Driver Core: swit...
31
32
  static ssize_t uevent_seqnum_show(struct kobject *kobj,
  				  struct kobj_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
  {
386f275f5   Kay Sievers   Driver Core: swit...
34
35
  	return sprintf(buf, "%llu
  ", (unsigned long long)uevent_seqnum);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
  }
0f76e5acf   Kay Sievers   [PATCH] add ueven...
37
  KERNEL_ATTR_RO(uevent_seqnum);
86d56134f   Michael Marineau   kobject: Make sup...
38
  #ifdef CONFIG_UEVENT_HELPER
af6658527   Thadeu Lima de Souza Cascardo   fix comment typo ...
39
  /* uevent helper program, used during early boot */
386f275f5   Kay Sievers   Driver Core: swit...
40
41
  static ssize_t uevent_helper_show(struct kobject *kobj,
  				  struct kobj_attribute *attr, char *buf)
0f76e5acf   Kay Sievers   [PATCH] add ueven...
42
  {
386f275f5   Kay Sievers   Driver Core: swit...
43
44
  	return sprintf(buf, "%s
  ", uevent_helper);
0f76e5acf   Kay Sievers   [PATCH] add ueven...
45
  }
386f275f5   Kay Sievers   Driver Core: swit...
46
47
48
  static ssize_t uevent_helper_store(struct kobject *kobj,
  				   struct kobj_attribute *attr,
  				   const char *buf, size_t count)
0f76e5acf   Kay Sievers   [PATCH] add ueven...
49
  {
312c004d3   Kay Sievers   [PATCH] driver co...
50
  	if (count+1 > UEVENT_HELPER_PATH_LEN)
0f76e5acf   Kay Sievers   [PATCH] add ueven...
51
  		return -ENOENT;
386f275f5   Kay Sievers   Driver Core: swit...
52
  	memcpy(uevent_helper, buf, count);
312c004d3   Kay Sievers   [PATCH] driver co...
53
54
55
56
  	uevent_helper[count] = '\0';
  	if (count && uevent_helper[count-1] == '
  ')
  		uevent_helper[count-1] = '\0';
0f76e5acf   Kay Sievers   [PATCH] add ueven...
57
58
59
  	return count;
  }
  KERNEL_ATTR_RW(uevent_helper);
86d56134f   Michael Marineau   kobject: Make sup...
60
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61

22b8ce947   Dave Hansen   profiling: dynami...
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  #ifdef CONFIG_PROFILING
  static ssize_t profiling_show(struct kobject *kobj,
  				  struct kobj_attribute *attr, char *buf)
  {
  	return sprintf(buf, "%d
  ", prof_on);
  }
  static ssize_t profiling_store(struct kobject *kobj,
  				   struct kobj_attribute *attr,
  				   const char *buf, size_t count)
  {
  	int ret;
  
  	if (prof_on)
  		return -EEXIST;
  	/*
  	 * This eventually calls into get_option() which
  	 * has a ton of callers and is not const.  It is
  	 * easiest to cast it away here.
  	 */
  	profile_setup((char *)buf);
  	ret = profile_init();
  	if (ret)
  		return ret;
  	ret = create_proc_profile();
  	if (ret)
  		return ret;
  	return count;
  }
  KERNEL_ATTR_RW(profiling);
  #endif
2965faa5e   Dave Young   kexec: split kexe...
93
  #ifdef CONFIG_KEXEC_CORE
386f275f5   Kay Sievers   Driver Core: swit...
94
95
  static ssize_t kexec_loaded_show(struct kobject *kobj,
  				 struct kobj_attribute *attr, char *buf)
c330dda90   Jeff Moyer   [PATCH] Add a sys...
96
  {
386f275f5   Kay Sievers   Driver Core: swit...
97
98
  	return sprintf(buf, "%d
  ", !!kexec_image);
c330dda90   Jeff Moyer   [PATCH] Add a sys...
99
100
  }
  KERNEL_ATTR_RO(kexec_loaded);
386f275f5   Kay Sievers   Driver Core: swit...
101
102
  static ssize_t kexec_crash_loaded_show(struct kobject *kobj,
  				       struct kobj_attribute *attr, char *buf)
c330dda90   Jeff Moyer   [PATCH] Add a sys...
103
  {
21db79e8b   Petr Tesarik   kexec: add a kexe...
104
105
  	return sprintf(buf, "%d
  ", kexec_crash_loaded());
c330dda90   Jeff Moyer   [PATCH] Add a sys...
106
107
  }
  KERNEL_ATTR_RO(kexec_crash_loaded);
fd59d231f   Ken'ichi Ohmichi   Add vmcoreinfo
108

06a7f7112   Amerigo Wang   kexec: premit red...
109
110
111
112
113
114
115
116
117
118
119
120
  static ssize_t kexec_crash_size_show(struct kobject *kobj,
  				       struct kobj_attribute *attr, char *buf)
  {
  	return sprintf(buf, "%zu
  ", crash_get_memory_size());
  }
  static ssize_t kexec_crash_size_store(struct kobject *kobj,
  				   struct kobj_attribute *attr,
  				   const char *buf, size_t count)
  {
  	unsigned long cnt;
  	int ret;
6072ddc85   Jingoo Han   kernel: replace s...
121
  	if (kstrtoul(buf, 0, &cnt))
06a7f7112   Amerigo Wang   kexec: premit red...
122
123
124
125
126
127
  		return -EINVAL;
  
  	ret = crash_shrink_memory(cnt);
  	return ret < 0 ? ret : count;
  }
  KERNEL_ATTR_RW(kexec_crash_size);
386f275f5   Kay Sievers   Driver Core: swit...
128
129
  static ssize_t vmcoreinfo_show(struct kobject *kobj,
  			       struct kobj_attribute *attr, char *buf)
fd59d231f   Ken'ichi Ohmichi   Add vmcoreinfo
130
  {
dae28018f   Russell King   kdump: arrange fo...
131
132
133
  	phys_addr_t vmcore_base = paddr_vmcoreinfo_note();
  	return sprintf(buf, "%pa %x
  ", &vmcore_base,
77019967f   Vivek Goyal   kdump: fix export...
134
  		       (unsigned int)sizeof(vmcoreinfo_note));
fd59d231f   Ken'ichi Ohmichi   Add vmcoreinfo
135
136
  }
  KERNEL_ATTR_RO(vmcoreinfo);
2965faa5e   Dave Young   kexec: split kexe...
137
  #endif /* CONFIG_KEXEC_CORE */
c330dda90   Jeff Moyer   [PATCH] Add a sys...
138

088ab0b4d   Ludwig Nussel   kernel/ksysfs.c: ...
139
140
141
142
143
144
145
146
  /* whether file capabilities are enabled */
  static ssize_t fscaps_show(struct kobject *kobj,
  				  struct kobj_attribute *attr, char *buf)
  {
  	return sprintf(buf, "%d
  ", file_caps_enabled);
  }
  KERNEL_ATTR_RO(fscaps);
79cfea027   Paul E. McKenney   rcu: Remove TINY_...
147
  #ifndef CONFIG_TINY_RCU
3705b88db   Antti P Miettinen   rcu: Add a module...
148
149
150
151
  int rcu_expedited;
  static ssize_t rcu_expedited_show(struct kobject *kobj,
  				  struct kobj_attribute *attr, char *buf)
  {
5a9be7c62   Paul E. McKenney   rcu: Add rcu_norm...
152
153
  	return sprintf(buf, "%d
  ", READ_ONCE(rcu_expedited));
3705b88db   Antti P Miettinen   rcu: Add a module...
154
155
156
157
158
159
160
161
162
163
164
  }
  static ssize_t rcu_expedited_store(struct kobject *kobj,
  				   struct kobj_attribute *attr,
  				   const char *buf, size_t count)
  {
  	if (kstrtoint(buf, 0, &rcu_expedited))
  		return -EINVAL;
  
  	return count;
  }
  KERNEL_ATTR_RW(rcu_expedited);
5a9be7c62   Paul E. McKenney   rcu: Add rcu_norm...
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  int rcu_normal;
  static ssize_t rcu_normal_show(struct kobject *kobj,
  			       struct kobj_attribute *attr, char *buf)
  {
  	return sprintf(buf, "%d
  ", READ_ONCE(rcu_normal));
  }
  static ssize_t rcu_normal_store(struct kobject *kobj,
  				struct kobj_attribute *attr,
  				const char *buf, size_t count)
  {
  	if (kstrtoint(buf, 0, &rcu_normal))
  		return -EINVAL;
  
  	return count;
  }
  KERNEL_ATTR_RW(rcu_normal);
79cfea027   Paul E. McKenney   rcu: Remove TINY_...
182
  #endif /* #ifndef CONFIG_TINY_RCU */
5a9be7c62   Paul E. McKenney   rcu: Add rcu_norm...
183

da1a679cd   Roland McGrath   Add /sys/kernel/n...
184
185
186
  /*
   * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
   */
52f5684c8   Gideon Israel Dsouza   kernel: use macro...
187
188
  extern const void __start_notes __weak;
  extern const void __stop_notes __weak;
da1a679cd   Roland McGrath   Add /sys/kernel/n...
189
  #define	notes_size (&__stop_notes - &__start_notes)
2c3c8bea6   Chris Wright   sysfs: add struct...
190
191
  static ssize_t notes_read(struct file *filp, struct kobject *kobj,
  			  struct bin_attribute *bin_attr,
da1a679cd   Roland McGrath   Add /sys/kernel/n...
192
193
194
195
196
197
198
199
200
201
202
203
204
  			  char *buf, loff_t off, size_t count)
  {
  	memcpy(buf, &__start_notes + off, count);
  	return count;
  }
  
  static struct bin_attribute notes_attr = {
  	.attr = {
  		.name = "notes",
  		.mode = S_IRUGO,
  	},
  	.read = &notes_read,
  };
0ff21e466   Greg Kroah-Hartman   kobject: convert ...
205
206
  struct kobject *kernel_kobj;
  EXPORT_SYMBOL_GPL(kernel_kobj);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
207
208
  
  static struct attribute * kernel_attrs[] = {
088ab0b4d   Ludwig Nussel   kernel/ksysfs.c: ...
209
  	&fscaps_attr.attr,
0f76e5acf   Kay Sievers   [PATCH] add ueven...
210
  	&uevent_seqnum_attr.attr,
86d56134f   Michael Marineau   kobject: Make sup...
211
  #ifdef CONFIG_UEVENT_HELPER
0f76e5acf   Kay Sievers   [PATCH] add ueven...
212
  	&uevent_helper_attr.attr,
86d56134f   Michael Marineau   kobject: Make sup...
213
  #endif
22b8ce947   Dave Hansen   profiling: dynami...
214
215
216
  #ifdef CONFIG_PROFILING
  	&profiling_attr.attr,
  #endif
2965faa5e   Dave Young   kexec: split kexe...
217
  #ifdef CONFIG_KEXEC_CORE
c330dda90   Jeff Moyer   [PATCH] Add a sys...
218
219
  	&kexec_loaded_attr.attr,
  	&kexec_crash_loaded_attr.attr,
06a7f7112   Amerigo Wang   kexec: premit red...
220
  	&kexec_crash_size_attr.attr,
fd59d231f   Ken'ichi Ohmichi   Add vmcoreinfo
221
  	&vmcoreinfo_attr.attr,
c330dda90   Jeff Moyer   [PATCH] Add a sys...
222
  #endif
79cfea027   Paul E. McKenney   rcu: Remove TINY_...
223
  #ifndef CONFIG_TINY_RCU
3705b88db   Antti P Miettinen   rcu: Add a module...
224
  	&rcu_expedited_attr.attr,
5a9be7c62   Paul E. McKenney   rcu: Add rcu_norm...
225
  	&rcu_normal_attr.attr,
79cfea027   Paul E. McKenney   rcu: Remove TINY_...
226
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
228
229
230
231
232
233
234
235
  	NULL
  };
  
  static struct attribute_group kernel_attr_group = {
  	.attrs = kernel_attrs,
  };
  
  static int __init ksysfs_init(void)
  {
bd35b93d8   Greg Kroah-Hartman   kset: convert ker...
236
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
237

0ff21e466   Greg Kroah-Hartman   kobject: convert ...
238
239
  	kernel_kobj = kobject_create_and_add("kernel", NULL);
  	if (!kernel_kobj) {
bd35b93d8   Greg Kroah-Hartman   kset: convert ker...
240
241
242
  		error = -ENOMEM;
  		goto exit;
  	}
0ff21e466   Greg Kroah-Hartman   kobject: convert ...
243
  	error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
bd35b93d8   Greg Kroah-Hartman   kset: convert ker...
244
245
246
247
  	if (error)
  		goto kset_exit;
  
  	if (notes_size > 0) {
da1a679cd   Roland McGrath   Add /sys/kernel/n...
248
  		notes_attr.size = notes_size;
0ff21e466   Greg Kroah-Hartman   kobject: convert ...
249
  		error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
bd35b93d8   Greg Kroah-Hartman   kset: convert ker...
250
251
  		if (error)
  			goto group_exit;
da1a679cd   Roland McGrath   Add /sys/kernel/n...
252
  	}
bd35b93d8   Greg Kroah-Hartman   kset: convert ker...
253
  	return 0;
bd35b93d8   Greg Kroah-Hartman   kset: convert ker...
254
  group_exit:
0ff21e466   Greg Kroah-Hartman   kobject: convert ...
255
  	sysfs_remove_group(kernel_kobj, &kernel_attr_group);
bd35b93d8   Greg Kroah-Hartman   kset: convert ker...
256
  kset_exit:
78a2d906b   Greg Kroah-Hartman   Kobject: convert ...
257
  	kobject_put(kernel_kobj);
bd35b93d8   Greg Kroah-Hartman   kset: convert ker...
258
  exit:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
259
260
261
262
  	return error;
  }
  
  core_initcall(ksysfs_init);