mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Borislav Petkov <bp@amd64.org>
To: <acme@infradead.org>, <fweisbec@gmail.com>, <mingo@elte.hu>,
	<peterz@infradead.org>, <rostedt@goodmis.org>
Cc: <linux-kernel@vger.kernel.org>,
	Borislav Petkov <borislav.petkov@amd.com>
Subject: [RFC PATCH 9/9] tools, ras: Add a RAS daemon
Date: Fri,  6 Aug 2010 16:25:13 +0200	[thread overview]
Message-ID: <1281104713-23141-10-git-send-email-bp@amd64.org> (raw)
In-Reply-To: <1281104713-23141-1-git-send-email-bp@amd64.org>

From: Borislav Petkov <borislav.petkov@amd.com>

Signed-off-by: Borislav Petkov <borislav.petkov@amd.com>
---
 tools/Makefile     |    4 ++
 tools/ras/Makefile |   16 ++++++
 tools/ras/rasd.c   |  144 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 164 insertions(+), 0 deletions(-)
 create mode 100644 tools/ras/Makefile
 create mode 100644 tools/ras/rasd.c

diff --git a/tools/Makefile b/tools/Makefile
index 691f78b..360454c 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -37,6 +37,9 @@ export PERF_TOP_DIR
 perf: libparsevent lklib lkperflib .FORCE
 	$(QUIET_SUBDIR0)perf/ $(QUIET_SUBDIR1)
 
+ras: libparsevent lklib lkperflib .FORCE
+	$(QUIET_SUBDIR0)ras/ $(QUIET_SUBDIR1)
+
 libparsevent: .FORCE
 	$(QUIET_SUBDIR0)lib/trace/ $(QUIET_SUBDIR1)
 
@@ -51,5 +54,6 @@ clean:
 	$(QUIET_SUBDIR0)lib/lk/ $(QUIET_SUBDIR1) clean
 	$(QUIET_SUBDIR0)lib/perf/ $(QUIET_SUBDIR1) clean
 	$(QUIET_SUBDIR0)perf/ $(QUIET_SUBDIR1) clean
+	$(QUIET_SUBDIR0)ras/ $(QUIET_SUBDIR1) clean
 
 .PHONY: clean .FORCE
diff --git a/tools/ras/Makefile b/tools/ras/Makefile
new file mode 100644
index 0000000..3aebe5a
--- /dev/null
+++ b/tools/ras/Makefile
@@ -0,0 +1,16 @@
+include ../scripts/Makefile.lib
+
+CFLAGS = -ggdb3 -Wall -Wextra -std=gnu99 -Werror $(CFLAGS_OPTIMIZE) -D_FORTIFY_SOURCE=2 $(EXTRA_WARNINGS) $(EXTRA_CFLAGS)
+ALL_CFLAGS = $(CFLAGS) $(BASIC_CFLAGS) -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
+ALL_LDFLAGS = $(LDFLAGS)
+
+RASLIBS=$(LIB_OUTPUT)libparsevent.a $(LIB_OUTPUT)lklib.a $(LIB_OUTPUT)lkperflib.a
+
+rasd: rasd.o $(RASLIBS)
+	$(QUIET_CC)$(CC) $(ALL_CFLAGS) -o $@ $^ $(RASLIBS)
+
+%.o: %.c
+	$(QUIET_CC)$(CC) $(ALL_CFLAGS) -c $<
+
+clean:
+	rm -rf *.o rasd
diff --git a/tools/ras/rasd.c b/tools/ras/rasd.c
new file mode 100644
index 0000000..5bf76cb
--- /dev/null
+++ b/tools/ras/rasd.c
@@ -0,0 +1,144 @@
+/*
+ * Linux RAS daemon.
+ *
+ * Initial code reused from Linux Daemon Writing HOWTO
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include <lk/util.h>
+#include <lk/cpumap.h>
+#include <lk/debugfs.h>
+#include <perf/mmap.h>
+#include <linux/compiler.h>
+
+#define MMAP_PAGES	128
+#define MAX_NR_CPUS	256
+
+#define PFX "rasd: "
+
+static int fds[MAX_NR_CPUS];
+static struct mmap_data mmaps[MAX_NR_CPUS];
+
+static int nr_cpus;
+static unsigned int page_size;
+static volatile int done = 0;
+
+static void sig_handler(int sig __used)
+{
+	done = 1;
+}
+
+static void write_output(void *buf, size_t size)
+{
+	while (size) {
+		int ret = write(STDOUT_FILENO, buf, size);
+
+		if (ret < 0)
+			die("failed to write");
+
+		size -= ret;
+		buf += ret;
+	}
+}
+
+static int ras_init(void)
+{
+	int cpu;
+
+	fprintf(stderr, PFX "Starting daemon.");
+
+	page_size = sysconf(_SC_PAGE_SIZE);
+
+	if (get_debugfs_mntpt()) {
+		error(PFX "Cannot mount debugfs, exiting...\n");
+		return 1;
+	}
+
+	nr_cpus = read_cpu_map(NULL);
+
+	for (cpu = 0; cpu < nr_cpus; cpu++) {
+		char path[MAXPATHLEN];
+
+		snprintf(path, MAXPATHLEN, "%s/mce/mce_record%d", debugfs_mntpt, cpu);
+		fds[cpu] = open(path, O_RDONLY, O_NONBLOCK);
+		if (fds[cpu] < 0) {
+			error("Error opening perf event on cpu %d\n", cpu);
+			return 1;
+		}
+	}
+
+	for (cpu = 0; cpu < nr_cpus; cpu++) {
+		mmaps[cpu].prev = 0;
+		mmaps[cpu].mask = MMAP_PAGES*page_size - 1;
+		mmaps[cpu].base = mmap(NULL, (MMAP_PAGES + 1) * page_size,
+				       PROT_READ, MAP_SHARED, fds[cpu], 0);
+
+		if (mmaps[cpu].base == MAP_FAILED) {
+			error("failed to mmap with %d (%s)\n", errno, strerror(errno));
+			return 1;
+		}
+	}
+
+	return 0;
+}
+
+int main(void)
+{
+	pid_t pid, sid;
+
+	pid = fork();
+	if (pid < 0) {
+		perror(PFX "Error forking daemon thread");
+		exit(EXIT_FAILURE);
+	}
+
+	/* parent can disappear now */
+	if (pid > 0)
+		exit(EXIT_SUCCESS);
+
+	umask(0);
+
+	/* TODO: open system logs */
+
+	sid = setsid();
+	if (sid < 0) {
+		perror(PFX "Error creating session");
+		exit(EXIT_FAILURE);
+	}
+
+	if (chdir("/") < 0) {
+		perror(PFX "Error chdir to /");
+		exit(EXIT_FAILURE);
+	}
+
+	close(STDIN_FILENO);
+/* 	close(STDOUT_FILENO); */
+/*	close(STDERR_FILENO); */
+
+	if (ras_init())
+		exit(EXIT_FAILURE);
+
+	signal(SIGCHLD, sig_handler);
+	signal(SIGINT, sig_handler);
+
+	while(1) {
+
+		if (mmap_read_all(mmaps, nr_cpus, write_output))
+			fprintf(stderr, "Read some mmapped data");
+
+//		if (done)
+//			return 0;
+
+		sleep(30);
+	}
+
+}
-- 
1.7.1


      parent reply	other threads:[~2010-08-06 14:26 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-06 14:25 [RFC PATCH 00/9] RAS daemon prototype, v2 Borislav Petkov
2010-08-06 14:25 ` [RFC PATCH 1/9] x86, mce: Notify about corrected events too Borislav Petkov
2010-08-06 14:25 ` [RFC PATCH 2/9] amd64_edac: Remove polling mechanism Borislav Petkov
2010-08-06 14:25 ` [RFC PATCH 3/9] perf_events: Export an assortment of functions Borislav Petkov
2010-08-06 14:25 ` [RFC PATCH 4/9] mce: Add persistent events Borislav Petkov
2010-08-06 14:57   ` Peter Zijlstra
2010-09-01  0:13     ` Steven Rostedt
2010-09-01  6:14       ` Ingo Molnar
2010-09-01 14:54         ` Steven Rostedt
2010-08-06 14:25 ` [RFC PATCH 5/9] perf: Move trace stuff into tools/lib/trace Borislav Petkov
2010-09-01  0:15   ` Steven Rostedt
2010-09-01  8:10     ` Borislav Petkov
2010-09-01 14:55       ` Steven Rostedt
2010-08-06 14:25 ` [RFC PATCH 6/9] perf: Export debugfs utilities Borislav Petkov
2010-08-06 14:25 ` [RFC PATCH 7/9] perf: Export cpumap Borislav Petkov
2010-08-06 14:39   ` Arnaldo Carvalho de Melo
2010-08-06 14:49     ` Borislav Petkov
2010-08-06 15:14       ` Arnaldo Carvalho de Melo
2010-08-06 16:04         ` Borislav Petkov
2010-08-06 21:48           ` Arnaldo Carvalho de Melo
2010-08-06 14:25 ` [RFC PATCH 8/9] perf: Carve out mmap helpers for general use Borislav Petkov
2010-08-06 14:25 ` Borislav Petkov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1281104713-23141-10-git-send-email-bp@amd64.org \
    --to=bp@amd64.org \
    --cc=acme@infradead.org \
    --cc=borislav.petkov@amd.com \
    --cc=fweisbec@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®