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
,
148 static struct kobj_type elog_ktype
= {
149 .sysfs_ops
= &elog_sysfs_ops
,
150 .release
= &elog_release
,
151 .default_attrs
= elog_default_attrs
,
154 /* Maximum size of a single log on FSP is 16KB */
155 #define OPAL_MAX_ERRLOG_SIZE 16384
157 static ssize_t
raw_attr_read(struct file
*filep
, struct kobject
*kobj
,
158 struct bin_attribute
*bin_attr
,
159 char *buffer
, loff_t pos
, size_t count
)
163 struct elog_obj
*elog
= to_elog_obj(kobj
);
165 /* We may have had an error reading before, so let's retry */
167 elog
->buffer
= kzalloc(elog
->size
, GFP_KERNEL
);
171 opal_rc
= opal_read_elog(__pa(elog
->buffer
),
172 elog
->size
, elog
->id
);
173 if (opal_rc
!= OPAL_SUCCESS
) {
174 pr_err_ratelimited("ELOG: log read failed for log-id=%llx\n",
182 memcpy(buffer
, elog
->buffer
+ pos
, count
);
187 static void create_elog_obj(uint64_t id
, size_t size
, uint64_t type
)
189 struct elog_obj
*elog
;
192 elog
= kzalloc(sizeof(*elog
), GFP_KERNEL
);
196 elog
->kobj
.kset
= elog_kset
;
198 kobject_init(&elog
->kobj
, &elog_ktype
);
200 sysfs_bin_attr_init(&elog
->raw_attr
);
202 elog
->raw_attr
.attr
.name
= "raw";
203 elog
->raw_attr
.attr
.mode
= 0400;
204 elog
->raw_attr
.size
= size
;
205 elog
->raw_attr
.read
= raw_attr_read
;
211 elog
->buffer
= kzalloc(elog
->size
, GFP_KERNEL
);
214 rc
= opal_read_elog(__pa(elog
->buffer
),
215 elog
->size
, elog
->id
);
216 if (rc
!= OPAL_SUCCESS
) {
217 pr_err("ELOG: log read failed for log-id=%llx\n",
224 rc
= kobject_add(&elog
->kobj
, NULL
, "0x%llx", id
);
226 kobject_put(&elog
->kobj
);
231 * As soon as the sysfs file for this elog is created/activated there is
232 * a chance the opal_errd daemon (or any userspace) might read and
233 * acknowledge the elog before kobject_uevent() is called. If that
234 * happens then there is a potential race between
235 * elog_ack_store->kobject_put() and kobject_uevent() which leads to a
236 * use-after-free of a kernfs object resulting in a kernel crash.
238 * To avoid that, we need to take a reference on behalf of the bin file,
239 * so that our reference remains valid while we call kobject_uevent().
240 * We then drop our reference before exiting the function, leaving the
241 * bin file to drop the last reference (if it hasn't already).
244 /* Take a reference for the bin file */
245 kobject_get(&elog
->kobj
);
246 rc
= sysfs_create_bin_file(&elog
->kobj
, &elog
->raw_attr
);
248 kobject_uevent(&elog
->kobj
, KOBJ_ADD
);
250 /* Drop the reference taken for the bin file */
251 kobject_put(&elog
->kobj
);
254 /* Drop our reference */
255 kobject_put(&elog
->kobj
);
260 static irqreturn_t
elog_event(int irq
, void *data
)
270 struct kobject
*kobj
;
272 rc
= opal_get_elog_size(&id
, &size
, &type
);
273 if (rc
!= OPAL_SUCCESS
) {
274 pr_err("ELOG: OPAL log info read failed\n");
278 elog_size
= be64_to_cpu(size
);
279 log_id
= be64_to_cpu(id
);
280 elog_type
= be64_to_cpu(type
);
282 WARN_ON(elog_size
> OPAL_MAX_ERRLOG_SIZE
);
284 if (elog_size
>= OPAL_MAX_ERRLOG_SIZE
)
285 elog_size
= OPAL_MAX_ERRLOG_SIZE
;
287 sprintf(name
, "0x%llx", log_id
);
289 /* we may get notified twice, let's handle
290 * that gracefully and not create two conflicting
293 kobj
= kset_find_obj(elog_kset
, name
);
295 /* Drop reference added by kset_find_obj() */
300 create_elog_obj(log_id
, elog_size
, elog_type
);
305 int __init
opal_elog_init(void)
309 /* ELOG not supported by firmware */
310 if (!opal_check_token(OPAL_ELOG_READ
))
313 elog_kset
= kset_create_and_add("elog", NULL
, opal_kobj
);
315 pr_warn("%s: failed to create elog kset\n", __func__
);
319 irq
= opal_event_request(ilog2(OPAL_EVENT_ERROR_LOG_AVAIL
));
321 pr_err("%s: Can't register OPAL event irq (%d)\n",
326 rc
= request_threaded_irq(irq
, NULL
, elog_event
,
327 IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
, "opal-elog", NULL
);
329 pr_err("%s: Can't request OPAL event irq (%d)\n",
334 /* We are now ready to pull error logs from opal. */
335 if (opal_check_token(OPAL_ELOG_RESEND
))
336 opal_resend_pending_logs();