ksysfs.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 <kay.sievers@vrfy.org>
  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 = \
  24. __ATTR(_name, 0644, _name##_show, _name##_store)
  25. /* current uevent sequence number */
  26. static ssize_t uevent_seqnum_show(struct kobject *kobj,
  27. struct kobj_attribute *attr, char *buf)
  28. {
  29. return sprintf(buf, "%llu\n", (unsigned long long)uevent_seqnum);
  30. }
  31. KERNEL_ATTR_RO(uevent_seqnum);
  32. #ifdef CONFIG_UEVENT_HELPER
  33. /* uevent helper program, used during early boot */
  34. static ssize_t uevent_helper_show(struct kobject *kobj,
  35. struct kobj_attribute *attr, char *buf)
  36. {
  37. return sprintf(buf, "%s\n", uevent_helper);
  38. }
  39. static ssize_t uevent_helper_store(struct kobject *kobj,
  40. struct kobj_attribute *attr,
  41. const char *buf, size_t count)
  42. {
  43. if (count+1 > UEVENT_HELPER_PATH_LEN)
  44. return -ENOENT;
  45. memcpy(uevent_helper, buf, count);
  46. uevent_helper[count] = '\0';
  47. if (count && uevent_helper[count-1] == '\n')
  48. uevent_helper[count-1] = '\0';
  49. return count;
  50. }
  51. KERNEL_ATTR_RW(uevent_helper);
  52. #endif
  53. #ifdef CONFIG_PROFILING
  54. static ssize_t profiling_show(struct kobject *kobj,
  55. struct kobj_attribute *attr, char *buf)
  56. {
  57. return sprintf(buf, "%d\n", prof_on);
  58. }
  59. static ssize_t profiling_store(struct kobject *kobj,
  60. struct kobj_attribute *attr,
  61. const char *buf, size_t count)
  62. {
  63. int ret;
  64. if (prof_on)
  65. return -EEXIST;
  66. /*
  67. * This eventually calls into get_option() which
  68. * has a ton of callers and is not const. It is
  69. * easiest to cast it away here.
  70. */
  71. profile_setup((char *)buf);
  72. ret = profile_init();
  73. if (ret)
  74. return ret;
  75. ret = create_proc_profile();
  76. if (ret)
  77. return ret;
  78. return count;
  79. }
  80. KERNEL_ATTR_RW(profiling);
  81. #endif
  82. #ifdef CONFIG_KEXEC_CORE
  83. static ssize_t kexec_loaded_show(struct kobject *kobj,
  84. struct kobj_attribute *attr, char *buf)
  85. {
  86. return sprintf(buf, "%d\n", !!kexec_image);
  87. }
  88. KERNEL_ATTR_RO(kexec_loaded);
  89. static ssize_t kexec_crash_loaded_show(struct kobject *kobj,
  90. struct kobj_attribute *attr, char *buf)
  91. {
  92. return sprintf(buf, "%d\n", kexec_crash_loaded());
  93. }
  94. KERNEL_ATTR_RO(kexec_crash_loaded);
  95. static ssize_t kexec_crash_size_show(struct kobject *kobj,
  96. struct kobj_attribute *attr, char *buf)
  97. {
  98. return sprintf(buf, "%zu\n", crash_get_memory_size());
  99. }
  100. static ssize_t kexec_crash_size_store(struct kobject *kobj,
  101. struct kobj_attribute *attr,
  102. const char *buf, size_t count)
  103. {
  104. unsigned long cnt;
  105. int ret;
  106. if (kstrtoul(buf, 0, &cnt))
  107. return -EINVAL;
  108. ret = crash_shrink_memory(cnt);
  109. return ret < 0 ? ret : count;
  110. }
  111. KERNEL_ATTR_RW(kexec_crash_size);
  112. #endif /* CONFIG_KEXEC_CORE */
  113. #ifdef CONFIG_CRASH_CORE
  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)VMCOREINFO_NOTE_SIZE);
  120. }
  121. KERNEL_ATTR_RO(vmcoreinfo);
  122. #endif /* CONFIG_CRASH_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 __ro_after_init = {
  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. #endif
  198. #ifdef CONFIG_CRASH_CORE
  199. &vmcoreinfo_attr.attr,
  200. #endif
  201. #ifndef CONFIG_TINY_RCU
  202. &rcu_expedited_attr.attr,
  203. &rcu_normal_attr.attr,
  204. #endif
  205. NULL
  206. };
  207. static const struct attribute_group kernel_attr_group = {
  208. .attrs = kernel_attrs,
  209. };
  210. static int __init ksysfs_init(void)
  211. {
  212. int error;
  213. kernel_kobj = kobject_create_and_add("kernel", NULL);
  214. if (!kernel_kobj) {
  215. error = -ENOMEM;
  216. goto exit;
  217. }
  218. error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
  219. if (error)
  220. goto kset_exit;
  221. if (notes_size > 0) {
  222. notes_attr.size = notes_size;
  223. error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
  224. if (error)
  225. goto group_exit;
  226. }
  227. return 0;
  228. group_exit:
  229. sysfs_remove_group(kernel_kobj, &kernel_attr_group);
  230. kset_exit:
  231. kobject_put(kernel_kobj);
  232. exit:
  233. return error;
  234. }
  235. core_initcall(ksysfs_init);