ksysfs.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 file *filp, struct kobject *kobj,
  126. struct bin_attribute *bin_attr,
  127. char *buf, loff_t off, size_t count)
  128. {
  129. memcpy(buf, &__start_notes + off, count);
  130. return count;
  131. }
  132. static struct bin_attribute notes_attr = {
  133. .attr = {
  134. .name = "notes",
  135. .mode = S_IRUGO,
  136. },
  137. .read = &notes_read,
  138. };
  139. struct kobject *kernel_kobj;
  140. EXPORT_SYMBOL_GPL(kernel_kobj);
  141. static struct attribute * kernel_attrs[] = {
  142. #if defined(CONFIG_HOTPLUG)
  143. &uevent_seqnum_attr.attr,
  144. &uevent_helper_attr.attr,
  145. #endif
  146. #ifdef CONFIG_PROFILING
  147. &profiling_attr.attr,
  148. #endif
  149. #ifdef CONFIG_KEXEC
  150. &kexec_loaded_attr.attr,
  151. &kexec_crash_loaded_attr.attr,
  152. &kexec_crash_size_attr.attr,
  153. &vmcoreinfo_attr.attr,
  154. #endif
  155. NULL
  156. };
  157. static struct attribute_group kernel_attr_group = {
  158. .attrs = kernel_attrs,
  159. };
  160. static int __init ksysfs_init(void)
  161. {
  162. int error;
  163. kernel_kobj = kobject_create_and_add("kernel", NULL);
  164. if (!kernel_kobj) {
  165. error = -ENOMEM;
  166. goto exit;
  167. }
  168. error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
  169. if (error)
  170. goto kset_exit;
  171. if (notes_size > 0) {
  172. notes_attr.size = notes_size;
  173. error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
  174. if (error)
  175. goto group_exit;
  176. }
  177. return 0;
  178. group_exit:
  179. sysfs_remove_group(kernel_kobj, &kernel_attr_group);
  180. kset_exit:
  181. kobject_put(kernel_kobj);
  182. exit:
  183. return error;
  184. }
  185. core_initcall(ksysfs_init);