Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Error log support on PowerNV.
0004  *
0005  * Copyright 2013,2014 IBM Corp.
0006  */
0007 #include <linux/kernel.h>
0008 #include <linux/init.h>
0009 #include <linux/interrupt.h>
0010 #include <linux/of.h>
0011 #include <linux/slab.h>
0012 #include <linux/sysfs.h>
0013 #include <linux/fs.h>
0014 #include <linux/vmalloc.h>
0015 #include <linux/fcntl.h>
0016 #include <linux/kobject.h>
0017 #include <linux/uaccess.h>
0018 #include <asm/opal.h>
0019 
0020 struct elog_obj {
0021     struct kobject kobj;
0022     struct bin_attribute raw_attr;
0023     uint64_t id;
0024     uint64_t type;
0025     size_t size;
0026     char *buffer;
0027 };
0028 #define to_elog_obj(x) container_of(x, struct elog_obj, kobj)
0029 
0030 struct elog_attribute {
0031     struct attribute attr;
0032     ssize_t (*show)(struct elog_obj *elog, struct elog_attribute *attr,
0033             char *buf);
0034     ssize_t (*store)(struct elog_obj *elog, struct elog_attribute *attr,
0035              const char *buf, size_t count);
0036 };
0037 #define to_elog_attr(x) container_of(x, struct elog_attribute, attr)
0038 
0039 static ssize_t elog_id_show(struct elog_obj *elog_obj,
0040                 struct elog_attribute *attr,
0041                 char *buf)
0042 {
0043     return sprintf(buf, "0x%llx\n", elog_obj->id);
0044 }
0045 
0046 static const char *elog_type_to_string(uint64_t type)
0047 {
0048     switch (type) {
0049     case 0: return "PEL";
0050     default: return "unknown";
0051     }
0052 }
0053 
0054 static ssize_t elog_type_show(struct elog_obj *elog_obj,
0055                   struct elog_attribute *attr,
0056                   char *buf)
0057 {
0058     return sprintf(buf, "0x%llx %s\n",
0059                elog_obj->type,
0060                elog_type_to_string(elog_obj->type));
0061 }
0062 
0063 static ssize_t elog_ack_show(struct elog_obj *elog_obj,
0064                  struct elog_attribute *attr,
0065                  char *buf)
0066 {
0067     return sprintf(buf, "ack - acknowledge log message\n");
0068 }
0069 
0070 static ssize_t elog_ack_store(struct elog_obj *elog_obj,
0071                   struct elog_attribute *attr,
0072                   const char *buf,
0073                   size_t count)
0074 {
0075     /*
0076      * Try to self remove this attribute. If we are successful,
0077      * delete the kobject itself.
0078      */
0079     if (sysfs_remove_file_self(&elog_obj->kobj, &attr->attr)) {
0080         opal_send_ack_elog(elog_obj->id);
0081         kobject_put(&elog_obj->kobj);
0082     }
0083     return count;
0084 }
0085 
0086 static struct elog_attribute id_attribute =
0087     __ATTR(id, 0444, elog_id_show, NULL);
0088 static struct elog_attribute type_attribute =
0089     __ATTR(type, 0444, elog_type_show, NULL);
0090 static struct elog_attribute ack_attribute =
0091     __ATTR(acknowledge, 0660, elog_ack_show, elog_ack_store);
0092 
0093 static struct kset *elog_kset;
0094 
0095 static ssize_t elog_attr_show(struct kobject *kobj,
0096                   struct attribute *attr,
0097                   char *buf)
0098 {
0099     struct elog_attribute *attribute;
0100     struct elog_obj *elog;
0101 
0102     attribute = to_elog_attr(attr);
0103     elog = to_elog_obj(kobj);
0104 
0105     if (!attribute->show)
0106         return -EIO;
0107 
0108     return attribute->show(elog, attribute, buf);
0109 }
0110 
0111 static ssize_t elog_attr_store(struct kobject *kobj,
0112                    struct attribute *attr,
0113                    const char *buf, size_t len)
0114 {
0115     struct elog_attribute *attribute;
0116     struct elog_obj *elog;
0117 
0118     attribute = to_elog_attr(attr);
0119     elog = to_elog_obj(kobj);
0120 
0121     if (!attribute->store)
0122         return -EIO;
0123 
0124     return attribute->store(elog, attribute, buf, len);
0125 }
0126 
0127 static const struct sysfs_ops elog_sysfs_ops = {
0128     .show = elog_attr_show,
0129     .store = elog_attr_store,
0130 };
0131 
0132 static void elog_release(struct kobject *kobj)
0133 {
0134     struct elog_obj *elog;
0135 
0136     elog = to_elog_obj(kobj);
0137     kfree(elog->buffer);
0138     kfree(elog);
0139 }
0140 
0141 static struct attribute *elog_default_attrs[] = {
0142     &id_attribute.attr,
0143     &type_attribute.attr,
0144     &ack_attribute.attr,
0145     NULL,
0146 };
0147 ATTRIBUTE_GROUPS(elog_default);
0148 
0149 static struct kobj_type elog_ktype = {
0150     .sysfs_ops = &elog_sysfs_ops,
0151     .release = &elog_release,
0152     .default_groups = elog_default_groups,
0153 };
0154 
0155 /* Maximum size of a single log on FSP is 16KB */
0156 #define OPAL_MAX_ERRLOG_SIZE    16384
0157 
0158 static ssize_t raw_attr_read(struct file *filep, struct kobject *kobj,
0159                  struct bin_attribute *bin_attr,
0160                  char *buffer, loff_t pos, size_t count)
0161 {
0162     int opal_rc;
0163 
0164     struct elog_obj *elog = to_elog_obj(kobj);
0165 
0166     /* We may have had an error reading before, so let's retry */
0167     if (!elog->buffer) {
0168         elog->buffer = kzalloc(elog->size, GFP_KERNEL);
0169         if (!elog->buffer)
0170             return -EIO;
0171 
0172         opal_rc = opal_read_elog(__pa(elog->buffer),
0173                      elog->size, elog->id);
0174         if (opal_rc != OPAL_SUCCESS) {
0175             pr_err_ratelimited("ELOG: log read failed for log-id=%llx\n",
0176                        elog->id);
0177             kfree(elog->buffer);
0178             elog->buffer = NULL;
0179             return -EIO;
0180         }
0181     }
0182 
0183     memcpy(buffer, elog->buffer + pos, count);
0184 
0185     return count;
0186 }
0187 
0188 static void create_elog_obj(uint64_t id, size_t size, uint64_t type)
0189 {
0190     struct elog_obj *elog;
0191     int rc;
0192 
0193     elog = kzalloc(sizeof(*elog), GFP_KERNEL);
0194     if (!elog)
0195         return;
0196 
0197     elog->kobj.kset = elog_kset;
0198 
0199     kobject_init(&elog->kobj, &elog_ktype);
0200 
0201     sysfs_bin_attr_init(&elog->raw_attr);
0202 
0203     elog->raw_attr.attr.name = "raw";
0204     elog->raw_attr.attr.mode = 0400;
0205     elog->raw_attr.size = size;
0206     elog->raw_attr.read = raw_attr_read;
0207 
0208     elog->id = id;
0209     elog->size = size;
0210     elog->type = type;
0211 
0212     elog->buffer = kzalloc(elog->size, GFP_KERNEL);
0213 
0214     if (elog->buffer) {
0215         rc = opal_read_elog(__pa(elog->buffer),
0216                      elog->size, elog->id);
0217         if (rc != OPAL_SUCCESS) {
0218             pr_err("ELOG: log read failed for log-id=%llx\n",
0219                    elog->id);
0220             kfree(elog->buffer);
0221             elog->buffer = NULL;
0222         }
0223     }
0224 
0225     rc = kobject_add(&elog->kobj, NULL, "0x%llx", id);
0226     if (rc) {
0227         kobject_put(&elog->kobj);
0228         return;
0229     }
0230 
0231     /*
0232      * As soon as the sysfs file for this elog is created/activated there is
0233      * a chance the opal_errd daemon (or any userspace) might read and
0234      * acknowledge the elog before kobject_uevent() is called. If that
0235      * happens then there is a potential race between
0236      * elog_ack_store->kobject_put() and kobject_uevent() which leads to a
0237      * use-after-free of a kernfs object resulting in a kernel crash.
0238      *
0239      * To avoid that, we need to take a reference on behalf of the bin file,
0240      * so that our reference remains valid while we call kobject_uevent().
0241      * We then drop our reference before exiting the function, leaving the
0242      * bin file to drop the last reference (if it hasn't already).
0243      */
0244 
0245     /* Take a reference for the bin file */
0246     kobject_get(&elog->kobj);
0247     rc = sysfs_create_bin_file(&elog->kobj, &elog->raw_attr);
0248     if (rc == 0) {
0249         kobject_uevent(&elog->kobj, KOBJ_ADD);
0250     } else {
0251         /* Drop the reference taken for the bin file */
0252         kobject_put(&elog->kobj);
0253     }
0254 
0255     /* Drop our reference */
0256     kobject_put(&elog->kobj);
0257 
0258     return;
0259 }
0260 
0261 static irqreturn_t elog_event(int irq, void *data)
0262 {
0263     __be64 size;
0264     __be64 id;
0265     __be64 type;
0266     uint64_t elog_size;
0267     uint64_t log_id;
0268     uint64_t elog_type;
0269     int rc;
0270     char name[2+16+1];
0271     struct kobject *kobj;
0272 
0273     rc = opal_get_elog_size(&id, &size, &type);
0274     if (rc != OPAL_SUCCESS) {
0275         pr_err("ELOG: OPAL log info read failed\n");
0276         return IRQ_HANDLED;
0277     }
0278 
0279     elog_size = be64_to_cpu(size);
0280     log_id = be64_to_cpu(id);
0281     elog_type = be64_to_cpu(type);
0282 
0283     WARN_ON(elog_size > OPAL_MAX_ERRLOG_SIZE);
0284 
0285     if (elog_size >= OPAL_MAX_ERRLOG_SIZE)
0286         elog_size  =  OPAL_MAX_ERRLOG_SIZE;
0287 
0288     sprintf(name, "0x%llx", log_id);
0289 
0290     /* we may get notified twice, let's handle
0291      * that gracefully and not create two conflicting
0292      * entries.
0293      */
0294     kobj = kset_find_obj(elog_kset, name);
0295     if (kobj) {
0296         /* Drop reference added by kset_find_obj() */
0297         kobject_put(kobj);
0298         return IRQ_HANDLED;
0299     }
0300 
0301     create_elog_obj(log_id, elog_size, elog_type);
0302 
0303     return IRQ_HANDLED;
0304 }
0305 
0306 int __init opal_elog_init(void)
0307 {
0308     int rc = 0, irq;
0309 
0310     /* ELOG not supported by firmware */
0311     if (!opal_check_token(OPAL_ELOG_READ))
0312         return -1;
0313 
0314     elog_kset = kset_create_and_add("elog", NULL, opal_kobj);
0315     if (!elog_kset) {
0316         pr_warn("%s: failed to create elog kset\n", __func__);
0317         return -1;
0318     }
0319 
0320     irq = opal_event_request(ilog2(OPAL_EVENT_ERROR_LOG_AVAIL));
0321     if (!irq) {
0322         pr_err("%s: Can't register OPAL event irq (%d)\n",
0323                __func__, irq);
0324         return irq;
0325     }
0326 
0327     rc = request_threaded_irq(irq, NULL, elog_event,
0328             IRQF_TRIGGER_HIGH | IRQF_ONESHOT, "opal-elog", NULL);
0329     if (rc) {
0330         pr_err("%s: Can't request OPAL event irq (%d)\n",
0331                __func__, rc);
0332         return rc;
0333     }
0334 
0335     /* We are now ready to pull error logs from opal. */
0336     if (opal_check_token(OPAL_ELOG_RESEND))
0337         opal_resend_pending_logs();
0338 
0339     return 0;
0340 }