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,
75 opal_send_ack_elog(elog_obj->id);
76 sysfs_remove_file_self(&elog_obj->kobj, &attr->attr);
77 kobject_put(&elog_obj->kobj);
81 static struct elog_attribute id_attribute =
82 __ATTR(id, 0444, elog_id_show, NULL);
83 static struct elog_attribute type_attribute =
84 __ATTR(type, 0444, elog_type_show, NULL);
85 static struct elog_attribute ack_attribute =
86 __ATTR(acknowledge, 0660, elog_ack_show, elog_ack_store);
88 static struct kset *elog_kset;
90 static ssize_t elog_attr_show(struct kobject *kobj,
91 struct attribute *attr,
94 struct elog_attribute *attribute;
95 struct elog_obj *elog;
97 attribute = to_elog_attr(attr);
98 elog = to_elog_obj(kobj);
100 if (!attribute->show)
103 return attribute->show(elog, attribute, buf);
106 static ssize_t elog_attr_store(struct kobject *kobj,
107 struct attribute *attr,
108 const char *buf, size_t len)
110 struct elog_attribute *attribute;
111 struct elog_obj *elog;
113 attribute = to_elog_attr(attr);
114 elog = to_elog_obj(kobj);
116 if (!attribute->store)
119 return attribute->store(elog, attribute, buf, len);
122 static const struct sysfs_ops elog_sysfs_ops = {
123 .show = elog_attr_show,
124 .store = elog_attr_store,
127 static void elog_release(struct kobject *kobj)
129 struct elog_obj *elog;
131 elog = to_elog_obj(kobj);
136 static struct attribute *elog_default_attrs[] = {
138 &type_attribute.attr,
143 static struct kobj_type elog_ktype = {
144 .sysfs_ops = &elog_sysfs_ops,
145 .release = &elog_release,
146 .default_attrs = elog_default_attrs,
149 /* Maximum size of a single log on FSP is 16KB */
150 #define OPAL_MAX_ERRLOG_SIZE 16384
152 static ssize_t raw_attr_read(struct file *filep, struct kobject *kobj,
153 struct bin_attribute *bin_attr,
154 char *buffer, loff_t pos, size_t count)
158 struct elog_obj *elog = to_elog_obj(kobj);
160 /* We may have had an error reading before, so let's retry */
162 elog->buffer = kzalloc(elog->size, GFP_KERNEL);
166 opal_rc = opal_read_elog(__pa(elog->buffer),
167 elog->size, elog->id);
168 if (opal_rc != OPAL_SUCCESS) {
169 pr_err("ELOG: log read failed for log-id=%llx\n",
177 memcpy(buffer, elog->buffer + pos, count);
182 static void create_elog_obj(uint64_t id, size_t size, uint64_t type)
184 struct elog_obj *elog;
187 elog = kzalloc(sizeof(*elog), GFP_KERNEL);
191 elog->kobj.kset = elog_kset;
193 kobject_init(&elog->kobj, &elog_ktype);
195 sysfs_bin_attr_init(&elog->raw_attr);
197 elog->raw_attr.attr.name = "raw";
198 elog->raw_attr.attr.mode = 0400;
199 elog->raw_attr.size = size;
200 elog->raw_attr.read = raw_attr_read;
206 elog->buffer = kzalloc(elog->size, GFP_KERNEL);
209 rc = opal_read_elog(__pa(elog->buffer),
210 elog->size, elog->id);
211 if (rc != OPAL_SUCCESS) {
212 pr_err("ELOG: log read failed for log-id=%llx\n",
219 rc = kobject_add(&elog->kobj, NULL, "0x%llx", id);
221 kobject_put(&elog->kobj);
226 * As soon as the sysfs file for this elog is created/activated there is
227 * a chance the opal_errd daemon (or any userspace) might read and
228 * acknowledge the elog before kobject_uevent() is called. If that
229 * happens then there is a potential race between
230 * elog_ack_store->kobject_put() and kobject_uevent() which leads to a
231 * use-after-free of a kernfs object resulting in a kernel crash.
233 * To avoid that, we need to take a reference on behalf of the bin file,
234 * so that our reference remains valid while we call kobject_uevent().
235 * We then drop our reference before exiting the function, leaving the
236 * bin file to drop the last reference (if it hasn't already).
239 /* Take a reference for the bin file */
240 kobject_get(&elog->kobj);
241 rc = sysfs_create_bin_file(&elog->kobj, &elog->raw_attr);
243 kobject_uevent(&elog->kobj, KOBJ_ADD);
245 /* Drop the reference taken for the bin file */
246 kobject_put(&elog->kobj);
249 /* Drop our reference */
250 kobject_put(&elog->kobj);
255 static irqreturn_t elog_event(int irq, void *data)
265 struct kobject *kobj;
267 rc = opal_get_elog_size(&id, &size, &type);
268 if (rc != OPAL_SUCCESS) {
269 pr_err("ELOG: OPAL log info read failed\n");
273 elog_size = be64_to_cpu(size);
274 log_id = be64_to_cpu(id);
275 elog_type = be64_to_cpu(type);
277 WARN_ON(elog_size > OPAL_MAX_ERRLOG_SIZE);
279 if (elog_size >= OPAL_MAX_ERRLOG_SIZE)
280 elog_size = OPAL_MAX_ERRLOG_SIZE;
282 sprintf(name, "0x%llx", log_id);
284 /* we may get notified twice, let's handle
285 * that gracefully and not create two conflicting
288 kobj = kset_find_obj(elog_kset, name);
290 /* Drop reference added by kset_find_obj() */
295 create_elog_obj(log_id, elog_size, elog_type);
300 int __init opal_elog_init(void)
304 /* ELOG not supported by firmware */
305 if (!opal_check_token(OPAL_ELOG_READ))
308 elog_kset = kset_create_and_add("elog", NULL, opal_kobj);
310 pr_warn("%s: failed to create elog kset\n", __func__);
314 irq = opal_event_request(ilog2(OPAL_EVENT_ERROR_LOG_AVAIL));
316 pr_err("%s: Can't register OPAL event irq (%d)\n",
321 rc = request_threaded_irq(irq, NULL, elog_event,
322 IRQF_TRIGGER_HIGH | IRQF_ONESHOT, "opal-elog", NULL);
324 pr_err("%s: Can't request OPAL event irq (%d)\n",
329 /* We are now ready to pull error logs from opal. */
330 if (opal_check_token(OPAL_ELOG_RESEND))
331 opal_resend_pending_logs();