ksysfs.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * kernel/ksysfs.c - sysfs attributes in /sys/kernel, which
  4. * are not related to any other subsystem
  5. *
  6. * Copyright (C) 2004 Kay Sievers <[email protected]>
  7. */
  8. #include <linux/kobject.h>
  9. #include <linux/string.h>
  10. #include <linux/sysfs.h>
  11. #include <linux/export.h>
  12. #include <linux/init.h>
  13. #include <linux/kexec.h>
  14. #include <linux/profile.h>
  15. #include <linux/stat.h>
  16. #include <linux/sched.h>
  17. #include <linux/capability.h>
  18. #include <linux/compiler.h>
  19. #include <linux/rcupdate.h> /* rcu_expedited and rcu_normal */
  20. #define KERNEL_ATTR_RO(_name) \
  21. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  22. #define KERNEL_ATTR_RW(_name) \
  23. static struct kobj_attribute _name##_attr = __ATTR_RW(_name)
  24. /* current uevent sequence number */
  25. static ssize_t uevent_seqnum_show(struct kobject *kobj,
  26. struct kobj_attribute *attr, char *buf)
  27. {
  28. return sprintf(buf, "%llu\n", (unsigned long long)uevent_seqnum);
  29. }
  30. KERNEL_ATTR_RO(uevent_seqnum);
  31. #ifdef CONFIG_UEVENT_HELPER
  32. /* uevent helper program, used during early boot */
  33. static ssize_t uevent_helper_show(struct kobject *kobj,
  34. struct kobj_attribute *attr, char *buf)
  35. {
  36. return sprintf(buf, "%s\n", uevent_helper);
  37. }
  38. static ssize_t uevent_helper_store(struct kobject *kobj,
  39. struct kobj_attribute *attr,
  40. const char *buf, size_t count)
  41. {
  42. if (count+1 > UEVENT_HELPER_PATH_LEN)
  43. return -ENOENT;
  44. memcpy(uevent_helper, buf, count);
  45. uevent_helper[count] = '\0';
  46. if (count && uevent_helper[count-1] == '\n')
  47. uevent_helper[count-1] = '\0';
  48. return count;
  49. }
  50. KERNEL_ATTR_RW(uevent_helper);
  51. #endif
  52. #ifdef CONFIG_PROFILING
  53. static ssize_t profiling_show(struct kobject *kobj,
  54. struct kobj_attribute *attr, char *buf)
  55. {
  56. return sprintf(buf, "%d\n", prof_on);
  57. }
  58. static ssize_t profiling_store(struct kobject *kobj,
  59. struct kobj_attribute *attr,
  60. const char *buf, size_t count)
  61. {
  62. int ret;
  63. if (prof_on)
  64. return -EEXIST;
  65. /*
  66. * This eventually calls into get_option() which
  67. * has a ton of callers and is not const. It is
  68. * easiest to cast it away here.
  69. */
  70. profile_setup((char *)buf);
  71. ret = profile_init();
  72. if (ret)
  73. return ret;
  74. ret = create_proc_profile();
  75. if (ret)
  76. return ret;
  77. return count;
  78. }
  79. KERNEL_ATTR_RW(profiling);
  80. #endif
  81. #ifdef CONFIG_KEXEC_CORE
  82. static ssize_t kexec_loaded_show(struct kobject *kobj,
  83. struct kobj_attribute *attr, char *buf)
  84. {
  85. return sprintf(buf, "%d\n", !!kexec_image);
  86. }
  87. KERNEL_ATTR_RO(kexec_loaded);
  88. static ssize_t kexec_crash_loaded_show(struct kobject *kobj,
  89. struct kobj_attribute *attr, char *buf)
  90. {
  91. return sprintf(buf, "%d\n", kexec_crash_loaded());
  92. }
  93. KERNEL_ATTR_RO(kexec_crash_loaded);
  94. static ssize_t kexec_crash_size_show(struct kobject *kobj,
  95. struct kobj_attribute *attr, char *buf)
  96. {
  97. ssize_t size = crash_get_memory_size();
  98. if (size < 0)
  99. return size;
  100. return sprintf(buf, "%zd\n", 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);