ksysfs.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 */
  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_image);
  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. return sprintf(buf, "%lx %x\n",
  118. paddr_vmcoreinfo_note(),
  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. int rcu_expedited;
  131. static ssize_t rcu_expedited_show(struct kobject *kobj,
  132. struct kobj_attribute *attr, char *buf)
  133. {
  134. return sprintf(buf, "%d\n", rcu_expedited);
  135. }
  136. static ssize_t rcu_expedited_store(struct kobject *kobj,
  137. struct kobj_attribute *attr,
  138. const char *buf, size_t count)
  139. {
  140. if (kstrtoint(buf, 0, &rcu_expedited))
  141. return -EINVAL;
  142. return count;
  143. }
  144. KERNEL_ATTR_RW(rcu_expedited);
  145. /*
  146. * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
  147. */
  148. extern const void __start_notes __weak;
  149. extern const void __stop_notes __weak;
  150. #define notes_size (&__stop_notes - &__start_notes)
  151. static ssize_t notes_read(struct file *filp, struct kobject *kobj,
  152. struct bin_attribute *bin_attr,
  153. char *buf, loff_t off, size_t count)
  154. {
  155. memcpy(buf, &__start_notes + off, count);
  156. return count;
  157. }
  158. static struct bin_attribute notes_attr = {
  159. .attr = {
  160. .name = "notes",
  161. .mode = S_IRUGO,
  162. },
  163. .read = &notes_read,
  164. };
  165. struct kobject *kernel_kobj;
  166. EXPORT_SYMBOL_GPL(kernel_kobj);
  167. static struct attribute * kernel_attrs[] = {
  168. &fscaps_attr.attr,
  169. &uevent_seqnum_attr.attr,
  170. #ifdef CONFIG_UEVENT_HELPER
  171. &uevent_helper_attr.attr,
  172. #endif
  173. #ifdef CONFIG_PROFILING
  174. &profiling_attr.attr,
  175. #endif
  176. #ifdef CONFIG_KEXEC_CORE
  177. &kexec_loaded_attr.attr,
  178. &kexec_crash_loaded_attr.attr,
  179. &kexec_crash_size_attr.attr,
  180. &vmcoreinfo_attr.attr,
  181. #endif
  182. &rcu_expedited_attr.attr,
  183. NULL
  184. };
  185. static struct attribute_group kernel_attr_group = {
  186. .attrs = kernel_attrs,
  187. };
  188. static int __init ksysfs_init(void)
  189. {
  190. int error;
  191. kernel_kobj = kobject_create_and_add("kernel", NULL);
  192. if (!kernel_kobj) {
  193. error = -ENOMEM;
  194. goto exit;
  195. }
  196. error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
  197. if (error)
  198. goto kset_exit;
  199. if (notes_size > 0) {
  200. notes_attr.size = notes_size;
  201. error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
  202. if (error)
  203. goto group_exit;
  204. }
  205. return 0;
  206. group_exit:
  207. sysfs_remove_group(kernel_kobj, &kernel_attr_group);
  208. kset_exit:
  209. kobject_put(kernel_kobj);
  210. exit:
  211. return error;
  212. }
  213. core_initcall(ksysfs_init);