ksysfs.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #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. #if defined(CONFIG_HOTPLUG)
  26. /* current uevent sequence number */
  27. static ssize_t uevent_seqnum_show(struct kobject *kobj,
  28. struct kobj_attribute *attr, char *buf)
  29. {
  30. return sprintf(buf, "%llu\n", (unsigned long long)uevent_seqnum);
  31. }
  32. KERNEL_ATTR_RO(uevent_seqnum);
  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
  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_image);
  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 (strict_strtoul(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. static ssize_t vmcoreinfo_show(struct kobject *kobj,
  113. struct kobj_attribute *attr, char *buf)
  114. {
  115. return sprintf(buf, "%lx %x\n",
  116. paddr_vmcoreinfo_note(),
  117. (unsigned int)vmcoreinfo_max_size);
  118. }
  119. KERNEL_ATTR_RO(vmcoreinfo);
  120. #endif /* CONFIG_KEXEC */
  121. /* whether file capabilities are enabled */
  122. static ssize_t fscaps_show(struct kobject *kobj,
  123. struct kobj_attribute *attr, char *buf)
  124. {
  125. return sprintf(buf, "%d\n", file_caps_enabled);
  126. }
  127. KERNEL_ATTR_RO(fscaps);
  128. int rcu_expedited;
  129. static ssize_t rcu_expedited_show(struct kobject *kobj,
  130. struct kobj_attribute *attr, char *buf)
  131. {
  132. return sprintf(buf, "%d\n", rcu_expedited);
  133. }
  134. static ssize_t rcu_expedited_store(struct kobject *kobj,
  135. struct kobj_attribute *attr,
  136. const char *buf, size_t count)
  137. {
  138. if (kstrtoint(buf, 0, &rcu_expedited))
  139. return -EINVAL;
  140. return count;
  141. }
  142. KERNEL_ATTR_RW(rcu_expedited);
  143. /*
  144. * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
  145. */
  146. extern const void __start_notes __attribute__((weak));
  147. extern const void __stop_notes __attribute__((weak));
  148. #define notes_size (&__stop_notes - &__start_notes)
  149. static ssize_t notes_read(struct file *filp, struct kobject *kobj,
  150. struct bin_attribute *bin_attr,
  151. char *buf, loff_t off, size_t count)
  152. {
  153. memcpy(buf, &__start_notes + off, count);
  154. return count;
  155. }
  156. static struct bin_attribute notes_attr = {
  157. .attr = {
  158. .name = "notes",
  159. .mode = S_IRUGO,
  160. },
  161. .read = &notes_read,
  162. };
  163. struct kobject *kernel_kobj;
  164. EXPORT_SYMBOL_GPL(kernel_kobj);
  165. static struct attribute * kernel_attrs[] = {
  166. &fscaps_attr.attr,
  167. #if defined(CONFIG_HOTPLUG)
  168. &uevent_seqnum_attr.attr,
  169. &uevent_helper_attr.attr,
  170. #endif
  171. #ifdef CONFIG_PROFILING
  172. &profiling_attr.attr,
  173. #endif
  174. #ifdef CONFIG_KEXEC
  175. &kexec_loaded_attr.attr,
  176. &kexec_crash_loaded_attr.attr,
  177. &kexec_crash_size_attr.attr,
  178. &vmcoreinfo_attr.attr,
  179. #endif
  180. &rcu_expedited_attr.attr,
  181. NULL
  182. };
  183. static struct attribute_group kernel_attr_group = {
  184. .attrs = kernel_attrs,
  185. };
  186. static int __init ksysfs_init(void)
  187. {
  188. int error;
  189. kernel_kobj = kobject_create_and_add("kernel", NULL);
  190. if (!kernel_kobj) {
  191. error = -ENOMEM;
  192. goto exit;
  193. }
  194. error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
  195. if (error)
  196. goto kset_exit;
  197. if (notes_size > 0) {
  198. notes_attr.size = notes_size;
  199. error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
  200. if (error)
  201. goto group_exit;
  202. }
  203. return 0;
  204. group_exit:
  205. sysfs_remove_group(kernel_kobj, &kernel_attr_group);
  206. kset_exit:
  207. kobject_put(kernel_kobj);
  208. exit:
  209. return error;
  210. }
  211. core_initcall(ksysfs_init);