1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Error log support on PowerNV.
5 * Copyright 2013,2014 IBM Corp.
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/interrupt.h>
11 #include <linux/slab.h>
12 #include <linux/sysfs.h>
14 #include <linux/vmalloc.h>
15 #include <linux/fcntl.h>
16 #include <linux/kobject.h>
17 #include <linux/uaccess.h>
22 struct bin_attribute raw_attr;
28 #define to_elog_obj(x) container_of(x, struct elog_obj, kobj)
30 struct elog_attribute {
31 struct attribute attr;
32 ssize_t (*show)(struct elog_obj *elog, struct elog_attribute *attr,
34 ssize_t (*store)(struct elog_obj *elog, struct elog_attribute *attr,
35 const char *buf, size_t count);
37 #define to_elog_attr(x) container_of(x, struct elog_attribute, attr)
39 static ssize_t elog_id_show(struct elog_obj *elog_obj,
40 struct elog_attribute *attr,
43 return sprintf(buf, "0x%llx\n", elog_obj->id);
46 static const char *elog_type_to_string(uint64_t type)
50 default: return "unknown";
54 static ssize_t elog_type_show(struct elog_obj *elog_obj,
55 struct elog_attribute *attr,
58 return sprintf(buf, "0x%llx %s\n",
60 elog_type_to_string(elog_obj->type));
63 static ssize_t elog_ack_show(struct elog_obj *elog_obj,
64 struct elog_attribute *attr,
67 return sprintf(buf, "ack - acknowledge log message\n");
70 static ssize_t elog_ack_store(struct elog_obj *elog_obj,
71 struct elog_attribute *attr,
76 * Try to self remove this attribute. If we are successful,
77 * delete the kobject itself.
79 if (sysfs_remove_file_self(&elog_obj->kobj, &attr->attr)) {
80 opal_send_ack_elog(elog_obj->id);
81 kobject_put(&elog_obj->kobj);
86 static struct elog_attribute id_attribute =
87 __ATTR(id, 0444, elog_id_show, NULL);
88 static struct elog_attribute type_attribute =
89 __ATTR(type, 0444, elog_type_show, NULL);
90 static struct elog_attribute ack_attribute =
91 __ATTR(acknowledge, 0660, elog_ack_show, elog_ack_store);
93 static struct kset *elog_kset;
95 static ssize_t elog_attr_show(struct kobject *kobj,
96 struct attribute *attr,
99 struct elog_attribute *attribute;
100 struct elog_obj *elog;
102 attribute = to_elog_attr(attr);
103 elog = to_elog_obj(kobj);
105 if (!attribute->show)
108 return attribute->show(elog, attribute, buf);
111 static ssize_t elog_attr_store(struct kobject *kobj,
112 struct attribute *attr,
113 const char *buf, size_t len)
115 struct elog_attribute *attribute;
116 struct elog_obj *elog;
118 attribute = to_elog_attr(attr);
119 elog = to_elog_obj(kobj);
121 if (!attribute->store)
124 return attribute->store(elog, attribute, buf, len);
127 static const struct sysfs_ops elog_sysfs_ops = {
128 .show = elog_attr_show,
129 .store = elog_attr_store,
132 static void elog_release(struct kobject *kobj)
134 struct elog_obj *elog;
136 elog = to_elog_obj(kobj);
141 static struct attribute *elog_default_attrs[] = {
143 &type_attribute.attr,
147 ATTRIBUTE_GROUPS(elog_default);
149 static struct kobj_type elog_ktype = {
150 .sysfs_ops = &elog_sysfs_ops,
151 .release = &elog_release,
152 .default_groups = elog_default_groups,
155 /* Maximum size of a single log on FSP is 16KB */
156 #define OPAL_MAX_ERRLOG_SIZE 16384
158 static ssize_t raw_attr_read(struct file *filep, struct kobject *kobj,
159 struct bin_attribute *bin_attr,
160 char *buffer, loff_t pos, size_t count)
164 struct elog_obj *elog = to_elog_obj(kobj);
166 /* We may have had an error reading before, so let's retry */
168 elog->buffer = kzalloc(elog->size, GFP_KERNEL);
172 opal_rc = opal_read_elog(__pa(elog->buffer),
173 elog->size, elog->id);
174 if (opal_rc != OPAL_SUCCESS) {
175 pr_err_ratelimited("ELOG: log read failed for log-id=%llx\n",
183 memcpy(buffer, elog->buffer + pos, count);
188 static void create_elog_obj(uint64_t id, size_t size, uint64_t type)
190 struct elog_obj *elog;
193 elog = kzalloc(sizeof(*elog), GFP_KERNEL);
197 elog->kobj.kset = elog_kset;
199 kobject_init(&elog->kobj, &elog_ktype);
201 sysfs_bin_attr_init(&elog->raw_attr);
203 elog->raw_attr.attr.name = "raw";
204 elog->raw_attr.attr.mode = 0400;
205 elog->raw_attr.size = size;
206 elog->raw_attr.read = raw_attr_read;
212 elog->buffer = kzalloc(elog->size, GFP_KERNEL);
215 rc = opal_read_elog(__pa(elog->buffer),
216 elog->size, elog->id);
217 if (rc != OPAL_SUCCESS) {
218 pr_err("ELOG: log read failed for log-id=%llx\n",
225 rc = kobject_add(&elog->kobj, NULL, "0x%llx", id);
227 kobject_put(&elog->kobj);
232 * As soon as the sysfs file for this elog is created/activated there is
233 * a chance the opal_errd daemon (or any userspace) might read and
234 * acknowledge the elog before kobject_uevent() is called. If that
235 * happens then there is a potential race between
236 * elog_ack_store->kobject_put() and kobject_uevent() which leads to a
237 * use-after-free of a kernfs object resulting in a kernel crash.
239 * To avoid that, we need to take a reference on behalf of the bin file,
240 * so that our reference remains valid while we call kobject_uevent().
241 * We then drop our reference before exiting the function, leaving the
242 * bin file to drop the last reference (if it hasn't already).
245 /* Take a reference for the bin file */
246 kobject_get(&elog->kobj);
247 rc = sysfs_create_bin_file(&elog->kobj, &elog->raw_attr);
249 kobject_uevent(&elog->kobj, KOBJ_ADD);
251 /* Drop the reference taken for the bin file */
252 kobject_put(&elog->kobj);
255 /* Drop our reference */
256 kobject_put(&elog->kobj);
261 static irqreturn_t elog_event(int irq, void *data)
271 struct kobject *kobj;
273 rc = opal_get_elog_size(&id, &size, &type);
274 if (rc != OPAL_SUCCESS) {
275 pr_err("ELOG: OPAL log info read failed\n");
279 elog_size = be64_to_cpu(size);
280 log_id = be64_to_cpu(id);
281 elog_type = be64_to_cpu(type);
283 WARN_ON(elog_size > OPAL_MAX_ERRLOG_SIZE);
285 if (elog_size >= OPAL_MAX_ERRLOG_SIZE)
286 elog_size = OPAL_MAX_ERRLOG_SIZE;
288 sprintf(name, "0x%llx", log_id);
290 /* we may get notified twice, let's handle
291 * that gracefully and not create two conflicting
294 kobj = kset_find_obj(elog_kset, name);
296 /* Drop reference added by kset_find_obj() */
301 create_elog_obj(log_id, elog_size, elog_type);
306 int __init opal_elog_init(void)
310 /* ELOG not supported by firmware */
311 if (!opal_check_token(OPAL_ELOG_READ))
314 elog_kset = kset_create_and_add("elog", NULL, opal_kobj);
316 pr_warn("%s: failed to create elog kset\n", __func__);
320 irq = opal_event_request(ilog2(OPAL_EVENT_ERROR_LOG_AVAIL));
322 pr_err("%s: Can't register OPAL event irq (%d)\n",
327 rc = request_threaded_irq(irq, NULL, elog_event,
328 IRQF_TRIGGER_HIGH | IRQF_ONESHOT, "opal-elog", NULL);
330 pr_err("%s: Can't request OPAL event irq (%d)\n",
335 /* We are now ready to pull error logs from opal. */
336 if (opal_check_token(OPAL_ELOG_RESEND))
337 opal_resend_pending_logs();