ksysfs.c 5.5 KB

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