ksysfs.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 <asm/byteorder.h>
  9. #include <linux/kobject.h>
  10. #include <linux/string.h>
  11. #include <linux/sysfs.h>
  12. #include <linux/export.h>
  13. #include <linux/init.h>
  14. #include <linux/kexec.h>
  15. #include <linux/profile.h>
  16. #include <linux/stat.h>
  17. #include <linux/sched.h>
  18. #include <linux/capability.h>
  19. #include <linux/compiler.h>
  20. #include <linux/rcupdate.h> /* rcu_expedited and rcu_normal */
  21. #if defined(__LITTLE_ENDIAN)
  22. #define CPU_BYTEORDER_STRING "little"
  23. #elif defined(__BIG_ENDIAN)
  24. #define CPU_BYTEORDER_STRING "big"
  25. #else
  26. #error Unknown byteorder
  27. #endif
  28. #define KERNEL_ATTR_RO(_name) \
  29. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  30. #define KERNEL_ATTR_RW(_name) \
  31. static struct kobj_attribute _name##_attr = __ATTR_RW(_name)
  32. /* current uevent sequence number */
  33. static ssize_t uevent_seqnum_show(struct kobject *kobj,
  34. struct kobj_attribute *attr, char *buf)
  35. {
  36. return sysfs_emit(buf, "%llu\n", (u64)atomic64_read(&uevent_seqnum));
  37. }
  38. KERNEL_ATTR_RO(uevent_seqnum);
  39. /* cpu byteorder */
  40. static ssize_t cpu_byteorder_show(struct kobject *kobj,
  41. struct kobj_attribute *attr, char *buf)
  42. {
  43. return sysfs_emit(buf, "%s\n", CPU_BYTEORDER_STRING);
  44. }
  45. KERNEL_ATTR_RO(cpu_byteorder);
  46. /* address bits */
  47. static ssize_t address_bits_show(struct kobject *kobj,
  48. struct kobj_attribute *attr, char *buf)
  49. {
  50. return sysfs_emit(buf, "%zu\n", sizeof(void *) * 8 /* CHAR_BIT */);
  51. }
  52. KERNEL_ATTR_RO(address_bits);
  53. #ifdef CONFIG_UEVENT_HELPER
  54. /* uevent helper program, used during early boot */
  55. static ssize_t uevent_helper_show(struct kobject *kobj,
  56. struct kobj_attribute *attr, char *buf)
  57. {
  58. return sysfs_emit(buf, "%s\n", uevent_helper);
  59. }
  60. static ssize_t uevent_helper_store(struct kobject *kobj,
  61. struct kobj_attribute *attr,
  62. const char *buf, size_t count)
  63. {
  64. if (count+1 > UEVENT_HELPER_PATH_LEN)
  65. return -ENOENT;
  66. memcpy(uevent_helper, buf, count);
  67. uevent_helper[count] = '\0';
  68. if (count && uevent_helper[count-1] == '\n')
  69. uevent_helper[count-1] = '\0';
  70. return count;
  71. }
  72. KERNEL_ATTR_RW(uevent_helper);
  73. #endif
  74. #ifdef CONFIG_PROFILING
  75. static ssize_t profiling_show(struct kobject *kobj,
  76. struct kobj_attribute *attr, char *buf)
  77. {
  78. return sysfs_emit(buf, "%d\n", prof_on);
  79. }
  80. static ssize_t profiling_store(struct kobject *kobj,
  81. struct kobj_attribute *attr,
  82. const char *buf, size_t count)
  83. {
  84. int ret;
  85. static DEFINE_MUTEX(lock);
  86. /*
  87. * We need serialization, for profile_setup() initializes prof_on
  88. * value and profile_init() must not reallocate prof_buffer after
  89. * once allocated.
  90. */
  91. guard(mutex)(&lock);
  92. if (prof_on)
  93. return -EEXIST;
  94. /*
  95. * This eventually calls into get_option() which
  96. * has a ton of callers and is not const. It is
  97. * easiest to cast it away here.
  98. */
  99. profile_setup((char *)buf);
  100. ret = profile_init();
  101. if (ret)
  102. return ret;
  103. ret = create_proc_profile();
  104. if (ret)
  105. return ret;
  106. return count;
  107. }
  108. KERNEL_ATTR_RW(profiling);
  109. #endif
  110. #ifdef CONFIG_KEXEC_CORE
  111. static ssize_t kexec_loaded_show(struct kobject *kobj,
  112. struct kobj_attribute *attr, char *buf)
  113. {
  114. return sysfs_emit(buf, "%d\n", !!kexec_image);
  115. }
  116. KERNEL_ATTR_RO(kexec_loaded);
  117. #ifdef CONFIG_CRASH_DUMP
  118. static ssize_t kexec_crash_loaded_show(struct kobject *kobj,
  119. struct kobj_attribute *attr, char *buf)
  120. {
  121. return sysfs_emit(buf, "%d\n", kexec_crash_loaded());
  122. }
  123. KERNEL_ATTR_RO(kexec_crash_loaded);
  124. static ssize_t kexec_crash_size_show(struct kobject *kobj,
  125. struct kobj_attribute *attr, char *buf)
  126. {
  127. ssize_t size = crash_get_memory_size();
  128. if (size < 0)
  129. return size;
  130. return sysfs_emit(buf, "%zd\n", size);
  131. }
  132. static ssize_t kexec_crash_size_store(struct kobject *kobj,
  133. struct kobj_attribute *attr,
  134. const char *buf, size_t count)
  135. {
  136. unsigned long cnt;
  137. int ret;
  138. if (kstrtoul(buf, 0, &cnt))
  139. return -EINVAL;
  140. ret = crash_shrink_memory(cnt);
  141. return ret < 0 ? ret : count;
  142. }
  143. KERNEL_ATTR_RW(kexec_crash_size);
  144. #endif /* CONFIG_CRASH_DUMP*/
  145. #endif /* CONFIG_KEXEC_CORE */
  146. #ifdef CONFIG_VMCORE_INFO
  147. static ssize_t vmcoreinfo_show(struct kobject *kobj,
  148. struct kobj_attribute *attr, char *buf)
  149. {
  150. phys_addr_t vmcore_base = paddr_vmcoreinfo_note();
  151. return sysfs_emit(buf, "%pa %x\n", &vmcore_base,
  152. (unsigned int)VMCOREINFO_NOTE_SIZE);
  153. }
  154. KERNEL_ATTR_RO(vmcoreinfo);
  155. #ifdef CONFIG_CRASH_HOTPLUG
  156. static ssize_t crash_elfcorehdr_size_show(struct kobject *kobj,
  157. struct kobj_attribute *attr, char *buf)
  158. {
  159. unsigned int sz = crash_get_elfcorehdr_size();
  160. return sysfs_emit(buf, "%u\n", sz);
  161. }
  162. KERNEL_ATTR_RO(crash_elfcorehdr_size);
  163. #endif
  164. #endif /* CONFIG_VMCORE_INFO */
  165. /* whether file capabilities are enabled */
  166. static ssize_t fscaps_show(struct kobject *kobj,
  167. struct kobj_attribute *attr, char *buf)
  168. {
  169. return sysfs_emit(buf, "%d\n", file_caps_enabled);
  170. }
  171. KERNEL_ATTR_RO(fscaps);
  172. #ifndef CONFIG_TINY_RCU
  173. int rcu_expedited;
  174. static ssize_t rcu_expedited_show(struct kobject *kobj,
  175. struct kobj_attribute *attr, char *buf)
  176. {
  177. return sysfs_emit(buf, "%d\n", READ_ONCE(rcu_expedited));
  178. }
  179. static ssize_t rcu_expedited_store(struct kobject *kobj,
  180. struct kobj_attribute *attr,
  181. const char *buf, size_t count)
  182. {
  183. if (kstrtoint(buf, 0, &rcu_expedited))
  184. return -EINVAL;
  185. return count;
  186. }
  187. KERNEL_ATTR_RW(rcu_expedited);
  188. int rcu_normal;
  189. static ssize_t rcu_normal_show(struct kobject *kobj,
  190. struct kobj_attribute *attr, char *buf)
  191. {
  192. return sysfs_emit(buf, "%d\n", READ_ONCE(rcu_normal));
  193. }
  194. static ssize_t rcu_normal_store(struct kobject *kobj,
  195. struct kobj_attribute *attr,
  196. const char *buf, size_t count)
  197. {
  198. if (kstrtoint(buf, 0, &rcu_normal))
  199. return -EINVAL;
  200. return count;
  201. }
  202. KERNEL_ATTR_RW(rcu_normal);
  203. #endif /* #ifndef CONFIG_TINY_RCU */
  204. /*
  205. * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
  206. */
  207. extern const void __start_notes;
  208. extern const void __stop_notes;
  209. #define notes_size (&__stop_notes - &__start_notes)
  210. static ssize_t notes_read(struct file *filp, struct kobject *kobj,
  211. struct bin_attribute *bin_attr,
  212. char *buf, loff_t off, size_t count)
  213. {
  214. memcpy(buf, &__start_notes + off, count);
  215. return count;
  216. }
  217. static struct bin_attribute notes_attr __ro_after_init = {
  218. .attr = {
  219. .name = "notes",
  220. .mode = S_IRUGO,
  221. },
  222. .read = &notes_read,
  223. };
  224. struct kobject *kernel_kobj;
  225. EXPORT_SYMBOL_GPL(kernel_kobj);
  226. static struct attribute * kernel_attrs[] = {
  227. &fscaps_attr.attr,
  228. &uevent_seqnum_attr.attr,
  229. &cpu_byteorder_attr.attr,
  230. &address_bits_attr.attr,
  231. #ifdef CONFIG_UEVENT_HELPER
  232. &uevent_helper_attr.attr,
  233. #endif
  234. #ifdef CONFIG_PROFILING
  235. &profiling_attr.attr,
  236. #endif
  237. #ifdef CONFIG_KEXEC_CORE
  238. &kexec_loaded_attr.attr,
  239. #ifdef CONFIG_CRASH_DUMP
  240. &kexec_crash_loaded_attr.attr,
  241. &kexec_crash_size_attr.attr,
  242. #endif
  243. #endif
  244. #ifdef CONFIG_VMCORE_INFO
  245. &vmcoreinfo_attr.attr,
  246. #ifdef CONFIG_CRASH_HOTPLUG
  247. &crash_elfcorehdr_size_attr.attr,
  248. #endif
  249. #endif
  250. #ifndef CONFIG_TINY_RCU
  251. &rcu_expedited_attr.attr,
  252. &rcu_normal_attr.attr,
  253. #endif
  254. NULL
  255. };
  256. static const struct attribute_group kernel_attr_group = {
  257. .attrs = kernel_attrs,
  258. };
  259. static int __init ksysfs_init(void)
  260. {
  261. int error;
  262. kernel_kobj = kobject_create_and_add("kernel", NULL);
  263. if (!kernel_kobj) {
  264. error = -ENOMEM;
  265. goto exit;
  266. }
  267. error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
  268. if (error)
  269. goto kset_exit;
  270. if (notes_size > 0) {
  271. notes_attr.size = notes_size;
  272. error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
  273. if (error)
  274. goto group_exit;
  275. }
  276. return 0;
  277. group_exit:
  278. sysfs_remove_group(kernel_kobj, &kernel_attr_group);
  279. kset_exit:
  280. kobject_put(kernel_kobj);
  281. exit:
  282. return error;
  283. }
  284. core_initcall(ksysfs_init);