opal-elog.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Error log support on PowerNV.
  4. *
  5. * Copyright 2013,2014 IBM Corp.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/of.h>
  11. #include <linux/slab.h>
  12. #include <linux/sysfs.h>
  13. #include <linux/fs.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/fcntl.h>
  16. #include <linux/kobject.h>
  17. #include <linux/uaccess.h>
  18. #include <asm/opal.h>
  19. struct elog_obj {
  20. struct kobject kobj;
  21. struct bin_attribute raw_attr;
  22. uint64_t id;
  23. uint64_t type;
  24. size_t size;
  25. char *buffer;
  26. };
  27. #define to_elog_obj(x) container_of(x, struct elog_obj, kobj)
  28. struct elog_attribute {
  29. struct attribute attr;
  30. ssize_t (*show)(struct elog_obj *elog, struct elog_attribute *attr,
  31. char *buf);
  32. ssize_t (*store)(struct elog_obj *elog, struct elog_attribute *attr,
  33. const char *buf, size_t count);
  34. };
  35. #define to_elog_attr(x) container_of(x, struct elog_attribute, attr)
  36. static ssize_t elog_id_show(struct elog_obj *elog_obj,
  37. struct elog_attribute *attr,
  38. char *buf)
  39. {
  40. return sprintf(buf, "0x%llx\n", elog_obj->id);
  41. }
  42. static const char *elog_type_to_string(uint64_t type)
  43. {
  44. switch (type) {
  45. case 0: return "PEL";
  46. default: return "unknown";
  47. }
  48. }
  49. static ssize_t elog_type_show(struct elog_obj *elog_obj,
  50. struct elog_attribute *attr,
  51. char *buf)
  52. {
  53. return sprintf(buf, "0x%llx %s\n",
  54. elog_obj->type,
  55. elog_type_to_string(elog_obj->type));
  56. }
  57. static ssize_t elog_ack_show(struct elog_obj *elog_obj,
  58. struct elog_attribute *attr,
  59. char *buf)
  60. {
  61. return sprintf(buf, "ack - acknowledge log message\n");
  62. }
  63. static ssize_t elog_ack_store(struct elog_obj *elog_obj,
  64. struct elog_attribute *attr,
  65. const char *buf,
  66. size_t count)
  67. {
  68. /*
  69. * Try to self remove this attribute. If we are successful,
  70. * delete the kobject itself.
  71. */
  72. if (sysfs_remove_file_self(&elog_obj->kobj, &attr->attr)) {
  73. opal_send_ack_elog(elog_obj->id);
  74. kobject_put(&elog_obj->kobj);
  75. }
  76. return count;
  77. }
  78. static struct elog_attribute id_attribute =
  79. __ATTR(id, 0444, elog_id_show, NULL);
  80. static struct elog_attribute type_attribute =
  81. __ATTR(type, 0444, elog_type_show, NULL);
  82. static struct elog_attribute ack_attribute =
  83. __ATTR(acknowledge, 0660, elog_ack_show, elog_ack_store);
  84. static struct kset *elog_kset;
  85. static ssize_t elog_attr_show(struct kobject *kobj,
  86. struct attribute *attr,
  87. char *buf)
  88. {
  89. struct elog_attribute *attribute;
  90. struct elog_obj *elog;
  91. attribute = to_elog_attr(attr);
  92. elog = to_elog_obj(kobj);
  93. if (!attribute->show)
  94. return -EIO;
  95. return attribute->show(elog, attribute, buf);
  96. }
  97. static ssize_t elog_attr_store(struct kobject *kobj,
  98. struct attribute *attr,
  99. const char *buf, size_t len)
  100. {
  101. struct elog_attribute *attribute;
  102. struct elog_obj *elog;
  103. attribute = to_elog_attr(attr);
  104. elog = to_elog_obj(kobj);
  105. if (!attribute->store)
  106. return -EIO;
  107. return attribute->store(elog, attribute, buf, len);
  108. }
  109. static const struct sysfs_ops elog_sysfs_ops = {
  110. .show = elog_attr_show,
  111. .store = elog_attr_store,
  112. };
  113. static void elog_release(struct kobject *kobj)
  114. {
  115. struct elog_obj *elog;
  116. elog = to_elog_obj(kobj);
  117. kfree(elog->buffer);
  118. kfree(elog);
  119. }
  120. static struct attribute *elog_default_attrs[] = {
  121. &id_attribute.attr,
  122. &type_attribute.attr,
  123. &ack_attribute.attr,
  124. NULL,
  125. };
  126. ATTRIBUTE_GROUPS(elog_default);
  127. static struct kobj_type elog_ktype = {
  128. .sysfs_ops = &elog_sysfs_ops,
  129. .release = &elog_release,
  130. .default_groups = elog_default_groups,
  131. };
  132. /* Maximum size of a single log on FSP is 16KB */
  133. #define OPAL_MAX_ERRLOG_SIZE 16384
  134. static ssize_t raw_attr_read(struct file *filep, struct kobject *kobj,
  135. struct bin_attribute *bin_attr,
  136. char *buffer, loff_t pos, size_t count)
  137. {
  138. int opal_rc;
  139. struct elog_obj *elog = to_elog_obj(kobj);
  140. /* We may have had an error reading before, so let's retry */
  141. if (!elog->buffer) {
  142. elog->buffer = kzalloc(elog->size, GFP_KERNEL);
  143. if (!elog->buffer)
  144. return -EIO;
  145. opal_rc = opal_read_elog(__pa(elog->buffer),
  146. elog->size, elog->id);
  147. if (opal_rc != OPAL_SUCCESS) {
  148. pr_err_ratelimited("ELOG: log read failed for log-id=%llx\n",
  149. elog->id);
  150. kfree(elog->buffer);
  151. elog->buffer = NULL;
  152. return -EIO;
  153. }
  154. }
  155. memcpy(buffer, elog->buffer + pos, count);
  156. return count;
  157. }
  158. static void create_elog_obj(uint64_t id, size_t size, uint64_t type)
  159. {
  160. struct elog_obj *elog;
  161. int rc;
  162. elog = kzalloc(sizeof(*elog), GFP_KERNEL);
  163. if (!elog)
  164. return;
  165. elog->kobj.kset = elog_kset;
  166. kobject_init(&elog->kobj, &elog_ktype);
  167. sysfs_bin_attr_init(&elog->raw_attr);
  168. elog->raw_attr.attr.name = "raw";
  169. elog->raw_attr.attr.mode = 0400;
  170. elog->raw_attr.size = size;
  171. elog->raw_attr.read = raw_attr_read;
  172. elog->id = id;
  173. elog->size = size;
  174. elog->type = type;
  175. elog->buffer = kzalloc(elog->size, GFP_KERNEL);
  176. if (elog->buffer) {
  177. rc = opal_read_elog(__pa(elog->buffer),
  178. elog->size, elog->id);
  179. if (rc != OPAL_SUCCESS) {
  180. pr_err("ELOG: log read failed for log-id=%llx\n",
  181. elog->id);
  182. kfree(elog->buffer);
  183. elog->buffer = NULL;
  184. }
  185. }
  186. rc = kobject_add(&elog->kobj, NULL, "0x%llx", id);
  187. if (rc) {
  188. kobject_put(&elog->kobj);
  189. return;
  190. }
  191. /*
  192. * As soon as the sysfs file for this elog is created/activated there is
  193. * a chance the opal_errd daemon (or any userspace) might read and
  194. * acknowledge the elog before kobject_uevent() is called. If that
  195. * happens then there is a potential race between
  196. * elog_ack_store->kobject_put() and kobject_uevent() which leads to a
  197. * use-after-free of a kernfs object resulting in a kernel crash.
  198. *
  199. * To avoid that, we need to take a reference on behalf of the bin file,
  200. * so that our reference remains valid while we call kobject_uevent().
  201. * We then drop our reference before exiting the function, leaving the
  202. * bin file to drop the last reference (if it hasn't already).
  203. */
  204. /* Take a reference for the bin file */
  205. kobject_get(&elog->kobj);
  206. rc = sysfs_create_bin_file(&elog->kobj, &elog->raw_attr);
  207. if (rc == 0) {
  208. kobject_uevent(&elog->kobj, KOBJ_ADD);
  209. } else {
  210. /* Drop the reference taken for the bin file */
  211. kobject_put(&elog->kobj);
  212. }
  213. /* Drop our reference */
  214. kobject_put(&elog->kobj);
  215. return;
  216. }
  217. static irqreturn_t elog_event(int irq, void *data)
  218. {
  219. __be64 size;
  220. __be64 id;
  221. __be64 type;
  222. uint64_t elog_size;
  223. uint64_t log_id;
  224. uint64_t elog_type;
  225. int rc;
  226. char name[2+16+1];
  227. struct kobject *kobj;
  228. rc = opal_get_elog_size(&id, &size, &type);
  229. if (rc != OPAL_SUCCESS) {
  230. pr_err("ELOG: OPAL log info read failed\n");
  231. return IRQ_HANDLED;
  232. }
  233. elog_size = be64_to_cpu(size);
  234. log_id = be64_to_cpu(id);
  235. elog_type = be64_to_cpu(type);
  236. WARN_ON(elog_size > OPAL_MAX_ERRLOG_SIZE);
  237. if (elog_size >= OPAL_MAX_ERRLOG_SIZE)
  238. elog_size = OPAL_MAX_ERRLOG_SIZE;
  239. sprintf(name, "0x%llx", log_id);
  240. /* we may get notified twice, let's handle
  241. * that gracefully and not create two conflicting
  242. * entries.
  243. */
  244. kobj = kset_find_obj(elog_kset, name);
  245. if (kobj) {
  246. /* Drop reference added by kset_find_obj() */
  247. kobject_put(kobj);
  248. return IRQ_HANDLED;
  249. }
  250. create_elog_obj(log_id, elog_size, elog_type);
  251. return IRQ_HANDLED;
  252. }
  253. int __init opal_elog_init(void)
  254. {
  255. int rc = 0, irq;
  256. /* ELOG not supported by firmware */
  257. if (!opal_check_token(OPAL_ELOG_READ))
  258. return -1;
  259. elog_kset = kset_create_and_add("elog", NULL, opal_kobj);
  260. if (!elog_kset) {
  261. pr_warn("%s: failed to create elog kset\n", __func__);
  262. return -1;
  263. }
  264. irq = opal_event_request(ilog2(OPAL_EVENT_ERROR_LOG_AVAIL));
  265. if (!irq) {
  266. pr_err("%s: Can't register OPAL event irq (%d)\n",
  267. __func__, irq);
  268. return irq;
  269. }
  270. rc = request_threaded_irq(irq, NULL, elog_event,
  271. IRQF_TRIGGER_HIGH | IRQF_ONESHOT, "opal-elog", NULL);
  272. if (rc) {
  273. pr_err("%s: Can't request OPAL event irq (%d)\n",
  274. __func__, rc);
  275. return rc;
  276. }
  277. /* We are now ready to pull error logs from opal. */
  278. if (opal_check_token(OPAL_ELOG_RESEND))
  279. opal_resend_pending_logs();
  280. return 0;
  281. }