Blame SOURCES/0069-rasdaemon-add-support-for-ARM-events.patch

b18917
From 5662e5376adcc45da43d7818c8ac1882883c18ac Mon Sep 17 00:00:00 2001
b18917
From: Tyler Baicar <tbaicar@codeaurora.org>
b18917
Date: Tue, 12 Sep 2017 14:58:25 -0600
b18917
Subject: [PATCH 1/2] rasdaemon: add support for ARM events
b18917
b18917
Add support to handle the ARM kernel trace events
b18917
which cover RAS ARM processor errors.
b18917
b18917
[V4]: fix arm_event_tab usage
b18917
b18917
Change-Id: Ife99c97042498d5fad4d9b8e873ecfba6a47947d
b18917
Signed-off-by: Tyler Baicar <tbaicar@codeaurora.org>
b18917
Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>
b18917
---
b18917
 Makefile.am       |  3 ++
b18917
 configure.ac      |  9 ++++++
b18917
 ras-arm-handler.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
b18917
 ras-arm-handler.h | 24 +++++++++++++++
b18917
 ras-events.c      | 15 ++++++++++
b18917
 ras-record.c      | 59 ++++++++++++++++++++++++++++++++++++
b18917
 ras-record.h      | 16 ++++++++++
b18917
 ras-report.c      | 75 ++++++++++++++++++++++++++++++++++++++++++++++
b18917
 ras-report.h      |  5 +++-
b18917
 9 files changed, 295 insertions(+), 1 deletion(-)
b18917
 create mode 100644 ras-arm-handler.c
b18917
 create mode 100644 ras-arm-handler.h
b18917
b18917
diff --git a/Makefile.am b/Makefile.am
b18917
index 2500772..4aa5543 100644
b18917
--- a/Makefile.am
b18917
+++ b/Makefile.am
b18917
@@ -27,6 +27,9 @@ endif
b18917
 if WITH_NON_STANDARD
b18917
    rasdaemon_SOURCES += ras-non-standard-handler.c
b18917
 endif
b18917
+if WITH_ARM
b18917
+   rasdaemon_SOURCES += ras-arm-handler.c
b18917
+endif
b18917
 if WITH_MCE
b18917
    rasdaemon_SOURCES += ras-mce-handler.c mce-intel.c mce-amd-k8.c \
b18917
 			mce-intel-p4-p6.c mce-intel-nehalem.c \
b18917
diff --git a/configure.ac b/configure.ac
b18917
index ecd4b2f..14fc2f2 100644
b18917
--- a/configure.ac
b18917
+++ b/configure.ac
b18917
@@ -53,6 +53,15 @@ AS_IF([test "x$enable_non_standard" = "xyes"], [
b18917
 ])
b18917
 AM_CONDITIONAL([WITH_NON_STANDARD], [test x$enable_non_standard = xyes])
b18917
 
b18917
+AC_ARG_ENABLE([arm],
b18917
+    AS_HELP_STRING([--enable-arm], [enable ARM events (currently experimental)]))
b18917
+
b18917
+AS_IF([test "x$enable_arm" = "xyes"], [
b18917
+  AC_DEFINE(HAVE_ARM,1,"have ARM events collect")
b18917
+  AC_SUBST([WITH_ARM])
b18917
+])
b18917
+AM_CONDITIONAL([WITH_ARM], [test x$enable_arm = xyes])
b18917
+
b18917
 AC_ARG_ENABLE([mce],
b18917
     AS_HELP_STRING([--enable-mce], [enable MCE events (currently experimental)]))
b18917
 
b18917
diff --git a/ras-arm-handler.c b/ras-arm-handler.c
b18917
new file mode 100644
b18917
index 0000000..a76470d
b18917
--- /dev/null
b18917
+++ b/ras-arm-handler.c
b18917
@@ -0,0 +1,90 @@
b18917
+/*
b18917
+ * Copyright (c) 2016, The Linux Foundation. All rights reserved.
b18917
+ *
b18917
+ * This program is free software; you can redistribute it and/or modify
b18917
+ * it under the terms of the GNU General Public License version 2 and
b18917
+ * only version 2 as published by the Free Software Foundation.
b18917
+
b18917
+ * This program is distributed in the hope that it will be useful,
b18917
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
b18917
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b18917
+ * GNU General Public License for more details.
b18917
+ */
b18917
+
b18917
+#include <stdio.h>
b18917
+#include <stdlib.h>
b18917
+#include <string.h>
b18917
+#include <unistd.h>
b18917
+#include "libtrace/kbuffer.h"
b18917
+#include "ras-arm-handler.h"
b18917
+#include "ras-record.h"
b18917
+#include "ras-logger.h"
b18917
+#include "ras-report.h"
b18917
+
b18917
+int ras_arm_event_handler(struct trace_seq *s,
b18917
+			 struct pevent_record *record,
b18917
+			 struct event_format *event, void *context)
b18917
+{
b18917
+	unsigned long long val;
b18917
+	struct ras_events *ras = context;
b18917
+	time_t now;
b18917
+	struct tm *tm;
b18917
+	struct ras_arm_event ev;
b18917
+
b18917
+	/*
b18917
+	 * Newer kernels (3.10-rc1 or upper) provide an uptime clock.
b18917
+	 * On previous kernels, the way to properly generate an event would
b18917
+	 * be to inject a fake one, measure its timestamp and diff it against
b18917
+	 * gettimeofday. We won't do it here. Instead, let's use uptime,
b18917
+	 * falling-back to the event report's time, if "uptime" clock is
b18917
+	 * not available (legacy kernels).
b18917
+	 */
b18917
+
b18917
+	if (ras->use_uptime)
b18917
+		now = record->ts/user_hz + ras->uptime_diff;
b18917
+	else
b18917
+		now = time(NULL);
b18917
+
b18917
+	tm = localtime(&now;;
b18917
+	if (tm)
b18917
+		strftime(ev.timestamp, sizeof(ev.timestamp),
b18917
+			 "%Y-%m-%d %H:%M:%S %z", tm);
b18917
+	trace_seq_printf(s, "%s\n", ev.timestamp);
b18917
+
b18917
+	if (pevent_get_field_val(s, event, "affinity", record, &val, 1) < 0)
b18917
+		return -1;
b18917
+	ev.affinity = val;
b18917
+	trace_seq_printf(s, " affinity: %d", ev.affinity);
b18917
+
b18917
+	if (pevent_get_field_val(s, event, "mpidr", record, &val, 1) < 0)
b18917
+		return -1;
b18917
+	ev.mpidr = val;
b18917
+	trace_seq_printf(s, "\n MPIDR: 0x%llx", (unsigned long long)ev.mpidr);
b18917
+
b18917
+	if (pevent_get_field_val(s, event, "midr", record, &val, 1) < 0)
b18917
+		return -1;
b18917
+	ev.midr = val;
b18917
+	trace_seq_printf(s, "\n MIDR: 0x%llx", (unsigned long long)ev.midr);
b18917
+
b18917
+	if (pevent_get_field_val(s, event, "running_state", record, &val, 1) < 0)
b18917
+		return -1;
b18917
+	ev.running_state = val;
b18917
+	trace_seq_printf(s, "\n running_state: %d", ev.running_state);
b18917
+
b18917
+	if (pevent_get_field_val(s, event, "psci_state", record, &val, 1) < 0)
b18917
+		return -1;
b18917
+	ev.psci_state = val;
b18917
+	trace_seq_printf(s, "\n psci_state: %d", ev.psci_state);
b18917
+
b18917
+	/* Insert data into the SGBD */
b18917
+#ifdef HAVE_SQLITE3
b18917
+	ras_store_arm_record(ras, &ev;;
b18917
+#endif
b18917
+
b18917
+#ifdef HAVE_ABRT_REPORT
b18917
+	/* Report event to ABRT */
b18917
+	ras_report_arm_event(ras, &ev;;
b18917
+#endif
b18917
+
b18917
+	return 0;
b18917
+}
b18917
diff --git a/ras-arm-handler.h b/ras-arm-handler.h
b18917
new file mode 100644
b18917
index 0000000..eae10ec
b18917
--- /dev/null
b18917
+++ b/ras-arm-handler.h
b18917
@@ -0,0 +1,24 @@
b18917
+/*
b18917
+ * Copyright (c) 2016, The Linux Foundation. All rights reserved.
b18917
+ *
b18917
+ * This program is free software; you can redistribute it and/or modify
b18917
+ * it under the terms of the GNU General Public License version 2 and
b18917
+ * only version 2 as published by the Free Software Foundation.
b18917
+
b18917
+ * This program is distributed in the hope that it will be useful,
b18917
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
b18917
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b18917
+ * GNU General Public License for more details.
b18917
+ */
b18917
+
b18917
+#ifndef __RAS_ARM_HANDLER_H
b18917
+#define __RAS_ARM_HANDLER_H
b18917
+
b18917
+#include "ras-events.h"
b18917
+#include "libtrace/event-parse.h"
b18917
+
b18917
+int ras_arm_event_handler(struct trace_seq *s,
b18917
+			 struct pevent_record *record,
b18917
+			 struct event_format *event, void *context);
b18917
+
b18917
+#endif
b18917
diff --git a/ras-events.c b/ras-events.c
b18917
index 96aa6f1..812d712 100644
b18917
--- a/ras-events.c
b18917
+++ b/ras-events.c
b18917
@@ -30,6 +30,7 @@
b18917
 #include "ras-mc-handler.h"
b18917
 #include "ras-aer-handler.h"
b18917
 #include "ras-non-standard-handler.h"
b18917
+#include "ras-arm-handler.h"
b18917
 #include "ras-mce-handler.h"
b18917
 #include "ras-extlog-handler.h"
b18917
 #include "ras-record.h"
b18917
@@ -213,6 +214,10 @@ int toggle_ras_mc_event(int enable)
b18917
 	rc |= __toggle_ras_mc_event(ras, "ras", "non_standard_event", enable);
b18917
 #endif
b18917
 
b18917
+#ifdef HAVE_ARM
b18917
+	rc |= __toggle_ras_mc_event(ras, "ras", "arm_event", enable);
b18917
+#endif
b18917
+
b18917
 free_ras:
b18917
 	free(ras);
b18917
 	return rc;
b18917
@@ -691,6 +696,16 @@ int handle_ras_events(int record_events)
b18917
                     "ras", "non_standard_event");
b18917
 #endif
b18917
 
b18917
+#ifdef HAVE_ARM
b18917
+        rc = add_event_handler(ras, pevent, page_size, "ras", "arm_event",
b18917
+                               ras_arm_event_handler);
b18917
+        if (!rc)
b18917
+                num_events++;
b18917
+        else
b18917
+                log(ALL, LOG_ERR, "Can't get traces from %s:%s\n",
b18917
+                    "ras", "arm_event");
b18917
+#endif
b18917
+
b18917
 	cpus = get_num_cpus(ras);
b18917
 
b18917
 #ifdef HAVE_MCE
b18917
diff --git a/ras-record.c b/ras-record.c
b18917
index 357ab61..c3644cb 100644
b18917
--- a/ras-record.c
b18917
+++ b/ras-record.c
b18917
@@ -209,6 +209,58 @@ int ras_store_non_standard_record(struct ras_events *ras, struct ras_non_standar
b18917
 }
b18917
 #endif
b18917
 
b18917
+/*
b18917
+ * Table and functions to handle ras:arm
b18917
+ */
b18917
+
b18917
+#ifdef HAVE_ARM
b18917
+static const struct db_fields arm_event_fields[] = {
b18917
+		{ .name="id",			.type="INTEGER PRIMARY KEY" },
b18917
+		{ .name="timestamp",		.type="TEXT" },
b18917
+		{ .name="error_count",		.type="INTEGER" },
b18917
+		{ .name="affinity",		.type="INTEGER" },
b18917
+		{ .name="mpidr",		.type="INTEGER" },
b18917
+		{ .name="running_state",	.type="INTEGER" },
b18917
+		{ .name="psci_state",		.type="INTEGER" },
b18917
+};
b18917
+
b18917
+static const struct db_table_descriptor arm_event_tab = {
b18917
+	.name = "arm_event",
b18917
+	.fields = arm_event_fields,
b18917
+	.num_fields = ARRAY_SIZE(arm_event_fields),
b18917
+};
b18917
+
b18917
+int ras_store_arm_record(struct ras_events *ras, struct ras_arm_event *ev)
b18917
+{
b18917
+	int rc;
b18917
+	struct sqlite3_priv *priv = ras->db_priv;
b18917
+
b18917
+	if (!priv || !priv->stmt_arm_record)
b18917
+		return 0;
b18917
+	log(TERM, LOG_INFO, "arm_event store: %p\n", priv->stmt_arm_record);
b18917
+
b18917
+	sqlite3_bind_text (priv->stmt_arm_record,  1,  ev->timestamp, -1, NULL);
b18917
+	sqlite3_bind_int  (priv->stmt_arm_record,  2,  ev->error_count);
b18917
+	sqlite3_bind_int  (priv->stmt_arm_record,  3,  ev->affinity);
b18917
+	sqlite3_bind_int  (priv->stmt_arm_record,  4,  ev->mpidr);
b18917
+	sqlite3_bind_int  (priv->stmt_arm_record,  5,  ev->running_state);
b18917
+	sqlite3_bind_int  (priv->stmt_arm_record,  6,  ev->psci_state);
b18917
+
b18917
+	rc = sqlite3_step(priv->stmt_arm_record);
b18917
+	if (rc != SQLITE_OK && rc != SQLITE_DONE)
b18917
+		log(TERM, LOG_ERR,
b18917
+		    "Failed to do arm_event step on sqlite: error = %d\n", rc);
b18917
+	rc = sqlite3_reset(priv->stmt_arm_record);
b18917
+	if (rc != SQLITE_OK && rc != SQLITE_DONE)
b18917
+		log(TERM, LOG_ERR,
b18917
+		    "Failed reset arm_event on sqlite: error = %d\n",
b18917
+		    rc);
b18917
+	log(TERM, LOG_INFO, "register inserted at db\n");
b18917
+
b18917
+	return rc;
b18917
+}
b18917
+#endif
b18917
+
b18917
 #ifdef HAVE_EXTLOG
b18917
 static const struct db_fields extlog_event_fields[] = {
b18917
 		{ .name="id",			.type="INTEGER PRIMARY KEY" },
b18917
@@ -509,6 +561,13 @@ int ras_mc_event_opendb(unsigned cpu, struct ras_events *ras)
b18917
 					&non_standard_event_tab);
b18917
 #endif
b18917
 
b18917
+#ifdef HAVE_ARM
b18917
+	rc = ras_mc_create_table(priv, &arm_event_tab);
b18917
+	if (rc == SQLITE_OK)
b18917
+		rc = ras_mc_prepare_stmt(priv, &priv->stmt_arm_record,
b18917
+					&arm_event_tab);
b18917
+#endif
b18917
+
b18917
 		ras->db_priv = priv;
b18917
 	return 0;
b18917
 }
b18917
diff --git a/ras-record.h b/ras-record.h
b18917
index 473ae40..12c2218 100644
b18917
--- a/ras-record.h
b18917
+++ b/ras-record.h
b18917
@@ -65,10 +65,21 @@ struct ras_non_standard_event {
b18917
 	uint32_t length;
b18917
 };
b18917
 
b18917
+struct ras_arm_event {
b18917
+	char timestamp[64];
b18917
+	int32_t error_count;
b18917
+	int8_t affinity;
b18917
+	int64_t mpidr;
b18917
+	int64_t midr;
b18917
+	int32_t running_state;
b18917
+	int32_t psci_state;
b18917
+};
b18917
+
b18917
 struct ras_mc_event;
b18917
 struct ras_aer_event;
b18917
 struct ras_extlog_event;
b18917
 struct ras_non_standard_event;
b18917
+struct ras_arm_event;
b18917
 struct mce_event;
b18917
 
b18917
 #ifdef HAVE_SQLITE3
b18917
@@ -90,6 +101,9 @@ struct sqlite3_priv {
b18917
 #ifdef HAVE_NON_STANDARD
b18917
 	sqlite3_stmt	*stmt_non_standard_record;
b18917
 #endif
b18917
+#ifdef HAVE_ARM
b18917
+	sqlite3_stmt	*stmt_arm_record;
b18917
+#endif
b18917
 };
b18917
 
b18917
 int ras_mc_event_opendb(unsigned cpu, struct ras_events *ras);
b18917
@@ -98,6 +112,7 @@ int ras_store_aer_event(struct ras_events *ras, struct ras_aer_event *ev);
b18917
 int ras_store_mce_record(struct ras_events *ras, struct mce_event *ev);
b18917
 int ras_store_extlog_mem_record(struct ras_events *ras, struct ras_extlog_event *ev);
b18917
 int ras_store_non_standard_record(struct ras_events *ras, struct ras_non_standard_event *ev);
b18917
+int ras_store_arm_record(struct ras_events *ras, struct ras_arm_event *ev);
b18917
 
b18917
 #else
b18917
 static inline int ras_mc_event_opendb(unsigned cpu, struct ras_events *ras) { return 0; };
b18917
@@ -106,6 +121,7 @@ static inline int ras_store_aer_event(struct ras_events *ras, struct ras_aer_eve
b18917
 static inline int ras_store_mce_record(struct ras_events *ras, struct mce_event *ev) { return 0; };
b18917
 static inline int ras_store_extlog_mem_record(struct ras_events *ras, struct ras_extlog_event *ev) { return 0; };
b18917
 static inline int ras_store_non_standard_record(struct ras_events *ras, struct ras_non_standard_event *ev) { return 0; };
b18917
+static inline int ras_store_arm_record(struct ras_events *ras, struct ras_arm_event *ev) { return 0; };
b18917
 
b18917
 #endif
b18917
 
b18917
diff --git a/ras-report.c b/ras-report.c
b18917
index 1eb9f79..d4beee0 100644
b18917
--- a/ras-report.c
b18917
+++ b/ras-report.c
b18917
@@ -228,6 +228,33 @@ static int set_non_standard_event_backtrace(char *buf, struct ras_non_standard_e
b18917
 	return 0;
b18917
 }
b18917
 
b18917
+static int set_arm_event_backtrace(char *buf, struct ras_arm_event *ev){
b18917
+	char bt_buf[MAX_BACKTRACE_SIZE];
b18917
+
b18917
+	if(!buf || !ev)
b18917
+		return -1;
b18917
+
b18917
+	sprintf(bt_buf, "BACKTRACE="    \
b18917
+						"timestamp=%s\n"	\
b18917
+						"error_count=%d\n"	\
b18917
+						"affinity=%d\n"	\
b18917
+						"mpidr=0x%lx\n"	\
b18917
+						"midr=0x%lx\n"	\
b18917
+						"running_state=%d\n"	\
b18917
+						"psci_state=%d\n",	\
b18917
+						ev->timestamp,	\
b18917
+						ev->error_count,	\
b18917
+						ev->affinity,	\
b18917
+						ev->mpidr,	\
b18917
+						ev->midr,	\
b18917
+						ev->running_state,	\
b18917
+						ev->psci_state);
b18917
+
b18917
+	strcat(buf, bt_buf);
b18917
+
b18917
+	return 0;
b18917
+}
b18917
+
b18917
 static int commit_report_backtrace(int sockfd, int type, void *ev){
b18917
 	char buf[MAX_BACKTRACE_SIZE];
b18917
 	char *pbuf = buf;
b18917
@@ -253,6 +280,9 @@ static int commit_report_backtrace(int sockfd, int type, void *ev){
b18917
 	case NON_STANDARD_EVENT:
b18917
 		rc = set_non_standard_event_backtrace(buf, (struct ras_non_standard_event *)ev);
b18917
 		break;
b18917
+	case ARM_EVENT:
b18917
+		rc = set_arm_event_backtrace(buf, (struct ras_arm_event *)ev);
b18917
+		break;
b18917
 	default:
b18917
 		return -1;
b18917
 	}
b18917
@@ -425,6 +455,51 @@ non_standard_fail:
b18917
 	return rc;
b18917
 }
b18917
 
b18917
+int ras_report_arm_event(struct ras_events *ras, struct ras_arm_event *ev){
b18917
+	char buf[MAX_MESSAGE_SIZE];
b18917
+	int sockfd = 0;
b18917
+	int rc = -1;
b18917
+
b18917
+	memset(buf, 0, sizeof(buf));
b18917
+
b18917
+	sockfd = setup_report_socket();
b18917
+	if(sockfd < 0){
b18917
+		return rc;
b18917
+	}
b18917
+
b18917
+	rc = commit_report_basic(sockfd);
b18917
+	if(rc < 0){
b18917
+		goto arm_fail;
b18917
+	}
b18917
+
b18917
+	rc = commit_report_backtrace(sockfd, ARM_EVENT, ev);
b18917
+	if(rc < 0){
b18917
+		goto arm_fail;
b18917
+	}
b18917
+
b18917
+	sprintf(buf, "ANALYZER=%s", "rasdaemon-arm");
b18917
+	rc = write(sockfd, buf, strlen(buf) + 1);
b18917
+	if(rc < strlen(buf) + 1){
b18917
+		goto arm_fail;
b18917
+	}
b18917
+
b18917
+	sprintf(buf, "REASON=%s", "ARM CPU report problem");
b18917
+	rc = write(sockfd, buf, strlen(buf) + 1);
b18917
+	if(rc < strlen(buf) + 1){
b18917
+		goto arm_fail;
b18917
+	}
b18917
+
b18917
+	rc = 0;
b18917
+
b18917
+arm_fail:
b18917
+
b18917
+	if(sockfd > 0){
b18917
+		close(sockfd);
b18917
+	}
b18917
+
b18917
+	return rc;
b18917
+}
b18917
+
b18917
 int ras_report_mce_event(struct ras_events *ras, struct mce_event *ev){
b18917
 	char buf[MAX_MESSAGE_SIZE];
b18917
 	int sockfd = 0;
b18917
diff --git a/ras-report.h b/ras-report.h
b18917
index c2fcf42..6c466f5 100644
b18917
--- a/ras-report.h
b18917
+++ b/ras-report.h
b18917
@@ -33,7 +33,8 @@ enum {
b18917
 	MC_EVENT,
b18917
 	MCE_EVENT,
b18917
 	AER_EVENT,
b18917
-	NON_STANDARD_EVENT
b18917
+	NON_STANDARD_EVENT,
b18917
+	ARM_EVENT
b18917
 };
b18917
 
b18917
 #ifdef HAVE_ABRT_REPORT
b18917
@@ -42,6 +43,7 @@ int ras_report_mc_event(struct ras_events *ras, struct ras_mc_event *ev);
b18917
 int ras_report_aer_event(struct ras_events *ras, struct ras_aer_event *ev);
b18917
 int ras_report_mce_event(struct ras_events *ras, struct mce_event *ev);
b18917
 int ras_report_non_standard_event(struct ras_events *ras, struct ras_non_standard_event *ev);
b18917
+int ras_report_arm_event(struct ras_events *ras, struct ras_arm_event *ev);
b18917
 
b18917
 #else
b18917
 
b18917
@@ -49,6 +51,7 @@ static inline int ras_report_mc_event(struct ras_events *ras, struct ras_mc_even
b18917
 static inline int ras_report_aer_event(struct ras_events *ras, struct ras_aer_event *ev) { return 0; };
b18917
 static inline int ras_report_mce_event(struct ras_events *ras, struct mce_event *ev) { return 0; };
b18917
 static inline int ras_report_non_standard_event(struct ras_events *ras, struct ras_non_standard_event *ev) { return 0; };
b18917
+static inline int ras_report_arm_event(struct ras_events *ras, struct ras_arm_event *ev) { return 0; };
b18917
 
b18917
 #endif
b18917
 
b18917
-- 
b18917
1.8.3.1
b18917