ksysfs.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. static ssize_t vmcoreinfo_show(struct kobject *kobj,
  115. struct kobj_attribute *attr, char *buf)
  116. {
  117. phys_addr_t vmcore_base = paddr_vmcoreinfo_note();
  118. return sprintf(buf, "%pa %x\n", &vmcore_base,
  119. (unsigned int)sizeof(vmcoreinfo_note));
  120. }
  121. KERNEL_ATTR_RO(vmcoreinfo);
  122. #endif /* CONFIG_KEXEC_CORE */
  123. /* whether file capabilities are enabled */
  124. static ssize_t fscaps_show(struct kobject *kobj,
  125. struct kobj_attribute *attr, char *buf)
  126. {
  127. return sprintf(buf, "%d\n", file_caps_enabled);
  128. }
  129. KERNEL_ATTR_RO(fscaps);
  130. #ifndef CONFIG_TINY_RCU
  131. int rcu_expedited;
  132. static ssize_t rcu_expedited_show(struct kobject *kobj,
  133. struct kobj_attribute *attr, char *buf)
  134. {
  135. return sprintf(buf, "%d\n", READ_ONCE(rcu_expedited));
  136. }
  137. static ssize_t rcu_expedited_store(struct kobject *kobj,
  138. struct kobj_attribute *attr,
  139. const char *buf, size_t count)
  140. {
  141. if (kstrtoint(buf, 0, &rcu_expedited))
  142. return -EINVAL;
  143. return count;
  144. }
  145. KERNEL_ATTR_RW(rcu_expedited);
  146. int rcu_normal;
  147. static ssize_t rcu_normal_show(struct kobject *kobj,
  148. struct kobj_attribute *attr, char *buf)
  149. {
  150. return sprintf(buf, "%d\n", READ_ONCE(rcu_normal));
  151. }
  152. static ssize_t rcu_normal_store(struct kobject *kobj,
  153. struct kobj_attribute *attr,
  154. const char *buf, size_t count)
  155. {
  156. if (kstrtoint(buf, 0, &rcu_normal))
  157. return -EINVAL;
  158. return count;
  159. }
  160. KERNEL_ATTR_RW(rcu_normal);
  161. #endif /* #ifndef CONFIG_TINY_RCU */
  162. /*
  163. * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
  164. */
  165. extern const void __start_notes __weak;
  166. extern const void __stop_notes __weak;
  167. #define notes_size (&__stop_notes - &__start_notes)
  168. static ssize_t notes_read(struct file *filp, struct kobject *kobj,
  169. struct bin_attribute *bin_attr,
  170. char *buf, loff_t off, size_t count)
  171. {
  172. memcpy(buf, &__start_notes + off, count);
  173. return count;
  174. }
  175. static struct bin_attribute notes_attr = {
  176. .attr = {
  177. .name = "notes",
  178. .mode = S_IRUGO,
  179. },
  180. .read = &notes_read,
  181. };
  182. struct kobject *kernel_kobj;
  183. EXPORT_SYMBOL_GPL(kernel_kobj);
  184. static struct attribute * kernel_attrs[] = {
  185. &fscaps_attr.attr,
  186. &uevent_seqnum_attr.attr,
  187. #ifdef CONFIG_UEVENT_HELPER
  188. &uevent_helper_attr.attr,
  189. #endif
  190. #ifdef CONFIG_PROFILING
  191. &profiling_attr.attr,
  192. #endif
  193. #ifdef CONFIG_KEXEC_CORE
  194. &kexec_loaded_attr.attr,
  195. &kexec_crash_loaded_attr.attr,
  196. &kexec_crash_size_attr.attr,
  197. &vmcoreinfo_attr.attr,
  198. #endif
  199. #ifndef CONFIG_TINY_RCU
  200. &rcu_expedited_attr.attr,
  201. &rcu_normal_attr.attr,
  202. #endif
  203. NULL
  204. };
  205. static struct attribute_group kernel_attr_group = {
  206. .attrs = kernel_attrs,
  207. };
  208. static int __init ksysfs_init(void)
  209. {
  210. int error;
  211. kernel_kobj = kobject_create_and_add("kernel", NULL);
  212. if (!kernel_kobj) {
  213. error = -ENOMEM;
  214. goto exit;
  215. }
  216. error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
  217. if (error)
  218. goto kset_exit;
  219. if (notes_size > 0) {
  220. notes_attr.size = notes_size;
  221. error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
  222. if (error)
  223. goto group_exit;
  224. }
  225. return 0;
  226. group_exit:
  227. sysfs_remove_group(kernel_kobj, &kernel_attr_group);
  228. kset_exit:
  229. kobject_put(kernel_kobj);
  230. exit:
  231. return error;
  232. }
  233. core_initcall(ksysfs_init);