ksysfs.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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/module.h>
  14. #include <linux/init.h>
  15. #include <linux/kexec.h>
  16. #include <linux/profile.h>
  17. #include <linux/sched.h>
  18. #define KERNEL_ATTR_RO(_name) \
  19. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  20. #define KERNEL_ATTR_RW(_name) \
  21. static struct kobj_attribute _name##_attr = \
  22. __ATTR(_name, 0644, _name##_show, _name##_store)
  23. #if defined(CONFIG_HOTPLUG)
  24. /* current uevent sequence number */
  25. static ssize_t uevent_seqnum_show(struct kobject *kobj,
  26. struct kobj_attribute *attr, char *buf)
  27. {
  28. return sprintf(buf, "%llu\n", (unsigned long long)uevent_seqnum);
  29. }
  30. KERNEL_ATTR_RO(uevent_seqnum);
  31. /* uevent helper program, used during early boot */
  32. static ssize_t uevent_helper_show(struct kobject *kobj,
  33. struct kobj_attribute *attr, char *buf)
  34. {
  35. return sprintf(buf, "%s\n", uevent_helper);
  36. }
  37. static ssize_t uevent_helper_store(struct kobject *kobj,
  38. struct kobj_attribute *attr,
  39. const char *buf, size_t count)
  40. {
  41. if (count+1 > UEVENT_HELPER_PATH_LEN)
  42. return -ENOENT;
  43. memcpy(uevent_helper, buf, count);
  44. uevent_helper[count] = '\0';
  45. if (count && uevent_helper[count-1] == '\n')
  46. uevent_helper[count-1] = '\0';
  47. return count;
  48. }
  49. KERNEL_ATTR_RW(uevent_helper);
  50. #endif
  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. /*
  120. * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
  121. */
  122. extern const void __start_notes __attribute__((weak));
  123. extern const void __stop_notes __attribute__((weak));
  124. #define notes_size (&__stop_notes - &__start_notes)
  125. static ssize_t notes_read(struct kobject *kobj, struct bin_attribute *bin_attr,
  126. char *buf, loff_t off, size_t count)
  127. {
  128. memcpy(buf, &__start_notes + off, count);
  129. return count;
  130. }
  131. static struct bin_attribute notes_attr = {
  132. .attr = {
  133. .name = "notes",
  134. .mode = S_IRUGO,
  135. },
  136. .read = &notes_read,
  137. };
  138. struct kobject *kernel_kobj;
  139. EXPORT_SYMBOL_GPL(kernel_kobj);
  140. static struct attribute * kernel_attrs[] = {
  141. #if defined(CONFIG_HOTPLUG)
  142. &uevent_seqnum_attr.attr,
  143. &uevent_helper_attr.attr,
  144. #endif
  145. #ifdef CONFIG_PROFILING
  146. &profiling_attr.attr,
  147. #endif
  148. #ifdef CONFIG_KEXEC
  149. &kexec_loaded_attr.attr,
  150. &kexec_crash_loaded_attr.attr,
  151. &kexec_crash_size_attr.attr,
  152. &vmcoreinfo_attr.attr,
  153. #endif
  154. NULL
  155. };
  156. static struct attribute_group kernel_attr_group = {
  157. .attrs = kernel_attrs,
  158. };
  159. static int __init ksysfs_init(void)
  160. {
  161. int error;
  162. kernel_kobj = kobject_create_and_add("kernel", NULL);
  163. if (!kernel_kobj) {
  164. error = -ENOMEM;
  165. goto exit;
  166. }
  167. error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
  168. if (error)
  169. goto kset_exit;
  170. if (notes_size > 0) {
  171. notes_attr.size = notes_size;
  172. error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
  173. if (error)
  174. goto group_exit;
  175. }
  176. return 0;
  177. group_exit:
  178. sysfs_remove_group(kernel_kobj, &kernel_attr_group);
  179. kset_exit:
  180. kobject_put(kernel_kobj);
  181. exit:
  182. return error;
  183. }
  184. core_initcall(ksysfs_init);