ksysfs.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #define KERNEL_ATTR_RO(_name) \
  22. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  23. #define KERNEL_ATTR_RW(_name) \
  24. static struct kobj_attribute _name##_attr = \
  25. __ATTR(_name, 0644, _name##_show, _name##_store)
  26. #if defined(CONFIG_HOTPLUG)
  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. /* uevent helper program, used during early boot */
  35. static ssize_t uevent_helper_show(struct kobject *kobj,
  36. struct kobj_attribute *attr, char *buf)
  37. {
  38. return sprintf(buf, "%s\n", uevent_helper);
  39. }
  40. static ssize_t uevent_helper_store(struct kobject *kobj,
  41. struct kobj_attribute *attr,
  42. const char *buf, size_t count)
  43. {
  44. if (count+1 > UEVENT_HELPER_PATH_LEN)
  45. return -ENOENT;
  46. memcpy(uevent_helper, buf, count);
  47. uevent_helper[count] = '\0';
  48. if (count && uevent_helper[count-1] == '\n')
  49. uevent_helper[count-1] = '\0';
  50. return count;
  51. }
  52. KERNEL_ATTR_RW(uevent_helper);
  53. #endif
  54. #ifdef CONFIG_PROFILING
  55. static ssize_t profiling_show(struct kobject *kobj,
  56. struct kobj_attribute *attr, char *buf)
  57. {
  58. return sprintf(buf, "%d\n", prof_on);
  59. }
  60. static ssize_t profiling_store(struct kobject *kobj,
  61. struct kobj_attribute *attr,
  62. const char *buf, size_t count)
  63. {
  64. int ret;
  65. if (prof_on)
  66. return -EEXIST;
  67. /*
  68. * This eventually calls into get_option() which
  69. * has a ton of callers and is not const. It is
  70. * easiest to cast it away here.
  71. */
  72. profile_setup((char *)buf);
  73. ret = profile_init();
  74. if (ret)
  75. return ret;
  76. ret = create_proc_profile();
  77. if (ret)
  78. return ret;
  79. return count;
  80. }
  81. KERNEL_ATTR_RW(profiling);
  82. #endif
  83. #ifdef CONFIG_KEXEC
  84. static ssize_t kexec_loaded_show(struct kobject *kobj,
  85. struct kobj_attribute *attr, char *buf)
  86. {
  87. return sprintf(buf, "%d\n", !!kexec_image);
  88. }
  89. KERNEL_ATTR_RO(kexec_loaded);
  90. static ssize_t kexec_crash_loaded_show(struct kobject *kobj,
  91. struct kobj_attribute *attr, char *buf)
  92. {
  93. return sprintf(buf, "%d\n", !!kexec_crash_image);
  94. }
  95. KERNEL_ATTR_RO(kexec_crash_loaded);
  96. static ssize_t kexec_crash_size_show(struct kobject *kobj,
  97. struct kobj_attribute *attr, char *buf)
  98. {
  99. return sprintf(buf, "%zu\n", crash_get_memory_size());
  100. }
  101. static ssize_t kexec_crash_size_store(struct kobject *kobj,
  102. struct kobj_attribute *attr,
  103. const char *buf, size_t count)
  104. {
  105. unsigned long cnt;
  106. int ret;
  107. if (strict_strtoul(buf, 0, &cnt))
  108. return -EINVAL;
  109. ret = crash_shrink_memory(cnt);
  110. return ret < 0 ? ret : count;
  111. }
  112. KERNEL_ATTR_RW(kexec_crash_size);
  113. static ssize_t vmcoreinfo_show(struct kobject *kobj,
  114. struct kobj_attribute *attr, char *buf)
  115. {
  116. return sprintf(buf, "%lx %x\n",
  117. paddr_vmcoreinfo_note(),
  118. (unsigned int)vmcoreinfo_max_size);
  119. }
  120. KERNEL_ATTR_RO(vmcoreinfo);
  121. #endif /* CONFIG_KEXEC */
  122. /* whether file capabilities are enabled */
  123. static ssize_t fscaps_show(struct kobject *kobj,
  124. struct kobj_attribute *attr, char *buf)
  125. {
  126. return sprintf(buf, "%d\n", file_caps_enabled);
  127. }
  128. KERNEL_ATTR_RO(fscaps);
  129. int rcu_expedited;
  130. static ssize_t rcu_expedited_show(struct kobject *kobj,
  131. struct kobj_attribute *attr, char *buf)
  132. {
  133. return sprintf(buf, "%d\n", rcu_expedited);
  134. }
  135. static ssize_t rcu_expedited_store(struct kobject *kobj,
  136. struct kobj_attribute *attr,
  137. const char *buf, size_t count)
  138. {
  139. if (kstrtoint(buf, 0, &rcu_expedited))
  140. return -EINVAL;
  141. return count;
  142. }
  143. KERNEL_ATTR_RW(rcu_expedited);
  144. /*
  145. * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
  146. */
  147. extern const void __start_notes __weak;
  148. extern const void __stop_notes __weak;
  149. #define notes_size (&__stop_notes - &__start_notes)
  150. static ssize_t notes_read(struct file *filp, struct kobject *kobj,
  151. struct bin_attribute *bin_attr,
  152. char *buf, loff_t off, size_t count)
  153. {
  154. memcpy(buf, &__start_notes + off, count);
  155. return count;
  156. }
  157. static struct bin_attribute notes_attr = {
  158. .attr = {
  159. .name = "notes",
  160. .mode = S_IRUGO,
  161. },
  162. .read = &notes_read,
  163. };
  164. struct kobject *kernel_kobj;
  165. EXPORT_SYMBOL_GPL(kernel_kobj);
  166. static struct attribute * kernel_attrs[] = {
  167. &fscaps_attr.attr,
  168. #if defined(CONFIG_HOTPLUG)
  169. &uevent_seqnum_attr.attr,
  170. &uevent_helper_attr.attr,
  171. #endif
  172. #ifdef CONFIG_PROFILING
  173. &profiling_attr.attr,
  174. #endif
  175. #ifdef CONFIG_KEXEC
  176. &kexec_loaded_attr.attr,
  177. &kexec_crash_loaded_attr.attr,
  178. &kexec_crash_size_attr.attr,
  179. &vmcoreinfo_attr.attr,
  180. #endif
  181. &rcu_expedited_attr.attr,
  182. NULL
  183. };
  184. static struct attribute_group kernel_attr_group = {
  185. .attrs = kernel_attrs,
  186. };
  187. static int __init ksysfs_init(void)
  188. {
  189. int error;
  190. kernel_kobj = kobject_create_and_add("kernel", NULL);
  191. if (!kernel_kobj) {
  192. error = -ENOMEM;
  193. goto exit;
  194. }
  195. error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
  196. if (error)
  197. goto kset_exit;
  198. if (notes_size > 0) {
  199. notes_attr.size = notes_size;
  200. error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
  201. if (error)
  202. goto group_exit;
  203. }
  204. return 0;
  205. group_exit:
  206. sysfs_remove_group(kernel_kobj, &kernel_attr_group);
  207. kset_exit:
  208. kobject_put(kernel_kobj);
  209. exit:
  210. return error;
  211. }
  212. core_initcall(ksysfs_init);