ksysfs.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  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. #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. #include <linux/rcupdate.h> /* rcu_expedited and rcu_normal */
  22. #define KERNEL_ATTR_RO(_name) \
  23. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  24. #define KERNEL_ATTR_RW(_name) \
  25. static struct kobj_attribute _name##_attr = \
  26. __ATTR(_name, 0644, _name##_show, _name##_store)
  27. /* current uevent sequence number */
  28. static ssize_t uevent_seqnum_show(struct kobject *kobj,
  29. struct kobj_attribute *attr, char *buf)
  30. {
  31. return sprintf(buf, "%llu\n", (unsigned long long)uevent_seqnum);
  32. }
  33. KERNEL_ATTR_RO(uevent_seqnum);
  34. #ifdef CONFIG_UEVENT_HELPER
  35. /* uevent helper program, used during early boot */
  36. static ssize_t uevent_helper_show(struct kobject *kobj,
  37. struct kobj_attribute *attr, char *buf)
  38. {
  39. return sprintf(buf, "%s\n", uevent_helper);
  40. }
  41. static ssize_t uevent_helper_store(struct kobject *kobj,
  42. struct kobj_attribute *attr,
  43. const char *buf, size_t count)
  44. {
  45. if (count+1 > UEVENT_HELPER_PATH_LEN)
  46. return -ENOENT;
  47. memcpy(uevent_helper, buf, count);
  48. uevent_helper[count] = '\0';
  49. if (count && uevent_helper[count-1] == '\n')
  50. uevent_helper[count-1] = '\0';
  51. return count;
  52. }
  53. KERNEL_ATTR_RW(uevent_helper);
  54. #endif
  55. #ifdef CONFIG_PROFILING
  56. static ssize_t profiling_show(struct kobject *kobj,
  57. struct kobj_attribute *attr, char *buf)
  58. {
  59. return sprintf(buf, "%d\n", prof_on);
  60. }
  61. static ssize_t profiling_store(struct kobject *kobj,
  62. struct kobj_attribute *attr,
  63. const char *buf, size_t count)
  64. {
  65. int ret;
  66. if (prof_on)
  67. return -EEXIST;
  68. /*
  69. * This eventually calls into get_option() which
  70. * has a ton of callers and is not const. It is
  71. * easiest to cast it away here.
  72. */
  73. profile_setup((char *)buf);
  74. ret = profile_init();
  75. if (ret)
  76. return ret;
  77. ret = create_proc_profile();
  78. if (ret)
  79. return ret;
  80. return count;
  81. }
  82. KERNEL_ATTR_RW(profiling);
  83. #endif
  84. #ifdef CONFIG_KEXEC_CORE
  85. static ssize_t kexec_loaded_show(struct kobject *kobj,
  86. struct kobj_attribute *attr, char *buf)
  87. {
  88. return sprintf(buf, "%d\n", !!kexec_image);
  89. }
  90. KERNEL_ATTR_RO(kexec_loaded);
  91. static ssize_t kexec_crash_loaded_show(struct kobject *kobj,
  92. struct kobj_attribute *attr, char *buf)
  93. {
  94. return sprintf(buf, "%d\n", kexec_crash_loaded());
  95. }
  96. KERNEL_ATTR_RO(kexec_crash_loaded);
  97. static ssize_t kexec_crash_size_show(struct kobject *kobj,
  98. struct kobj_attribute *attr, char *buf)
  99. {
  100. return sprintf(buf, "%zu\n", crash_get_memory_size());
  101. }
  102. static ssize_t kexec_crash_size_store(struct kobject *kobj,
  103. struct kobj_attribute *attr,
  104. const char *buf, size_t count)
  105. {
  106. unsigned long cnt;
  107. int ret;
  108. if (kstrtoul(buf, 0, &cnt))
  109. return -EINVAL;
  110. ret = crash_shrink_memory(cnt);
  111. return ret < 0 ? ret : count;
  112. }
  113. KERNEL_ATTR_RW(kexec_crash_size);
  114. #endif /* CONFIG_KEXEC_CORE */
  115. #ifdef CONFIG_CRASH_CORE
  116. static ssize_t vmcoreinfo_show(struct kobject *kobj,
  117. struct kobj_attribute *attr, char *buf)
  118. {
  119. phys_addr_t vmcore_base = paddr_vmcoreinfo_note();
  120. return sprintf(buf, "%pa %x\n", &vmcore_base,
  121. (unsigned int)VMCOREINFO_NOTE_SIZE);
  122. }
  123. KERNEL_ATTR_RO(vmcoreinfo);
  124. #endif /* CONFIG_CRASH_CORE */
  125. /* whether file capabilities are enabled */
  126. static ssize_t fscaps_show(struct kobject *kobj,
  127. struct kobj_attribute *attr, char *buf)
  128. {
  129. return sprintf(buf, "%d\n", file_caps_enabled);
  130. }
  131. KERNEL_ATTR_RO(fscaps);
  132. #ifndef CONFIG_TINY_RCU
  133. int rcu_expedited;
  134. static ssize_t rcu_expedited_show(struct kobject *kobj,
  135. struct kobj_attribute *attr, char *buf)
  136. {
  137. return sprintf(buf, "%d\n", READ_ONCE(rcu_expedited));
  138. }
  139. static ssize_t rcu_expedited_store(struct kobject *kobj,
  140. struct kobj_attribute *attr,
  141. const char *buf, size_t count)
  142. {
  143. if (kstrtoint(buf, 0, &rcu_expedited))
  144. return -EINVAL;
  145. return count;
  146. }
  147. KERNEL_ATTR_RW(rcu_expedited);
  148. int rcu_normal;
  149. static ssize_t rcu_normal_show(struct kobject *kobj,
  150. struct kobj_attribute *attr, char *buf)
  151. {
  152. return sprintf(buf, "%d\n", READ_ONCE(rcu_normal));
  153. }
  154. static ssize_t rcu_normal_store(struct kobject *kobj,
  155. struct kobj_attribute *attr,
  156. const char *buf, size_t count)
  157. {
  158. if (kstrtoint(buf, 0, &rcu_normal))
  159. return -EINVAL;
  160. return count;
  161. }
  162. KERNEL_ATTR_RW(rcu_normal);
  163. #endif /* #ifndef CONFIG_TINY_RCU */
  164. /*
  165. * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
  166. */
  167. extern const void __start_notes __weak;
  168. extern const void __stop_notes __weak;
  169. #define notes_size (&__stop_notes - &__start_notes)
  170. static ssize_t notes_read(struct file *filp, struct kobject *kobj,
  171. struct bin_attribute *bin_attr,
  172. char *buf, loff_t off, size_t count)
  173. {
  174. memcpy(buf, &__start_notes + off, count);
  175. return count;
  176. }
  177. static struct bin_attribute notes_attr __ro_after_init = {
  178. .attr = {
  179. .name = "notes",
  180. .mode = S_IRUGO,
  181. },
  182. .read = &notes_read,
  183. };
  184. struct kobject *kernel_kobj;
  185. EXPORT_SYMBOL_GPL(kernel_kobj);
  186. static struct attribute * kernel_attrs[] = {
  187. &fscaps_attr.attr,
  188. &uevent_seqnum_attr.attr,
  189. #ifdef CONFIG_UEVENT_HELPER
  190. &uevent_helper_attr.attr,
  191. #endif
  192. #ifdef CONFIG_PROFILING
  193. &profiling_attr.attr,
  194. #endif
  195. #ifdef CONFIG_KEXEC_CORE
  196. &kexec_loaded_attr.attr,
  197. &kexec_crash_loaded_attr.attr,
  198. &kexec_crash_size_attr.attr,
  199. #endif
  200. #ifdef CONFIG_CRASH_CORE
  201. &vmcoreinfo_attr.attr,
  202. #endif
  203. #ifndef CONFIG_TINY_RCU
  204. &rcu_expedited_attr.attr,
  205. &rcu_normal_attr.attr,
  206. #endif
  207. NULL
  208. };
  209. static const struct attribute_group kernel_attr_group = {
  210. .attrs = kernel_attrs,
  211. };
  212. static int __init ksysfs_init(void)
  213. {
  214. int error;
  215. kernel_kobj = kobject_create_and_add("kernel", NULL);
  216. if (!kernel_kobj) {
  217. error = -ENOMEM;
  218. goto exit;
  219. }
  220. error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
  221. if (error)
  222. goto kset_exit;
  223. if (notes_size > 0) {
  224. notes_attr.size = notes_size;
  225. error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
  226. if (error)
  227. goto group_exit;
  228. }
  229. return 0;
  230. group_exit:
  231. sysfs_remove_group(kernel_kobj, &kernel_attr_group);
  232. kset_exit:
  233. kobject_put(kernel_kobj);
  234. exit:
  235. return error;
  236. }
  237. core_initcall(ksysfs_init);