File: ras-memory-failure-handler.c

package info (click to toggle)
rasdaemon 0.8.1-3~bpo12%2B1
  • links: PTS
  • area: main
  • in suites: bookworm-backports
  • size: 2,624 kB
  • sloc: ansic: 15,360; sh: 4,417; perl: 2,216; makefile: 123
file content (230 lines) | stat: -rw-r--r-- 6,178 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ras-logger.h"
#include "ras-memory-failure-handler.h"
#include "ras-record.h"
#include "ras-report.h"
#include "trigger.h"

/* Memory failure - various types of pages */
enum mf_action_page_type {
	MF_MSG_KERNEL,
	MF_MSG_KERNEL_HIGH_ORDER,
	MF_MSG_SLAB,
	MF_MSG_DIFFERENT_COMPOUND,
	MF_MSG_HUGE,
	MF_MSG_FREE_HUGE,
	MF_MSG_UNMAP_FAILED,
	MF_MSG_DIRTY_SWAPCACHE,
	MF_MSG_CLEAN_SWAPCACHE,
	MF_MSG_DIRTY_MLOCKED_LRU,
	MF_MSG_CLEAN_MLOCKED_LRU,
	MF_MSG_DIRTY_UNEVICTABLE_LRU,
	MF_MSG_CLEAN_UNEVICTABLE_LRU,
	MF_MSG_DIRTY_LRU,
	MF_MSG_CLEAN_LRU,
	MF_MSG_TRUNCATED_LRU,
	MF_MSG_BUDDY,
	MF_MSG_DAX,
	MF_MSG_UNSPLIT_THP,
	MF_MSG_UNKNOWN,
};

/* Action results for various types of pages */
enum mf_action_result {
	MF_IGNORED,     /* Error: cannot be handled */
	MF_FAILED,      /* Error: handling failed */
	MF_DELAYED,     /* Will be handled later */
	MF_RECOVERED,   /* Successfully recovered */
};

/* memory failure page types */
static const struct {
	int	type;
	const char	*page_type;
} mf_page_type[] = {
	{ MF_MSG_KERNEL, "reserved kernel page" },
	{ MF_MSG_KERNEL_HIGH_ORDER, "high-order kernel page"},
	{ MF_MSG_SLAB, "kernel slab page"},
	{ MF_MSG_DIFFERENT_COMPOUND, "different compound page after locking"},
	{ MF_MSG_HUGE, "huge page"},
	{ MF_MSG_FREE_HUGE, "free huge page"},
	{ MF_MSG_UNMAP_FAILED, "unmapping failed page"},
	{ MF_MSG_DIRTY_SWAPCACHE, "dirty swapcache page"},
	{ MF_MSG_CLEAN_SWAPCACHE, "clean swapcache page"},
	{ MF_MSG_DIRTY_MLOCKED_LRU, "dirty mlocked LRU page"},
	{ MF_MSG_CLEAN_MLOCKED_LRU, "clean mlocked LRU page"},
	{ MF_MSG_DIRTY_UNEVICTABLE_LRU, "dirty unevictable LRU page"},
	{ MF_MSG_CLEAN_UNEVICTABLE_LRU, "clean unevictable LRU page"},
	{ MF_MSG_DIRTY_LRU, "dirty LRU page"},
	{ MF_MSG_CLEAN_LRU, "clean LRU page"},
	{ MF_MSG_TRUNCATED_LRU, "already truncated LRU page"},
	{ MF_MSG_BUDDY, "free buddy page"},
	{ MF_MSG_DAX, "dax page"},
	{ MF_MSG_UNSPLIT_THP, "unsplit thp"},
	{ MF_MSG_UNKNOWN, "unknown page"},
};

/* memory failure action results */
static const struct {
	int result;
	const char *action_result;
} mf_action_result[] = {
	{ MF_IGNORED, "Ignored" },
	{ MF_FAILED, "Failed" },
	{ MF_DELAYED, "Delayed" },
	{ MF_RECOVERED, "Recovered" },
};

#define MAX_ENV 6
static const char *mf_trigger = NULL;

void mem_fail_event_trigger_setup(void)
{
	const char *trigger;

	trigger = getenv("MEM_FAIL_TRIGGER");
	if (trigger && strcmp(trigger, "")) {
		mf_trigger = trigger_check(trigger);

		if (!mf_trigger) {
			log(ALL, LOG_ERR,
			    "Cannot access memory_fail_event trigger `%s`\n",
			    trigger);
		} else {
			log(ALL, LOG_INFO,
			    "Setup memory_fail_event trigger `%s`\n",
			    trigger);
		}
	}
}

static void run_mf_trigger(struct ras_mf_event *ev)
{
	char *env[MAX_ENV];
	int ei = 0;
	int i;

	if (!mf_trigger)
		return;

	if (asprintf(&env[ei++], "PATH=%s", getenv("PATH") ?: "/sbin:/usr/sbin:/bin:/usr/bin") < 0)
		goto free;
	if (asprintf(&env[ei++], "TIMESTAMP=%s", ev->timestamp) < 0)
		goto free;
	if (asprintf(&env[ei++], "PFN=%s", ev->pfn) < 0)
		goto free;
	if (asprintf(&env[ei++], "PAGE_TYPE=%s", ev->page_type) < 0)
		goto free;
	if (asprintf(&env[ei++], "ACTION_RESULT=%s", ev->action_result) < 0)
		goto free;

	env[ei] = NULL;
	assert(ei < MAX_ENV);

	run_trigger(mf_trigger, NULL, env, "memory_fail_event");

free:
	for (i = 0; i < ei; i++)
		free(env[i]);
}

static const char *get_page_type(int page_type)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(mf_page_type); i++)
		if (mf_page_type[i].type == page_type)
			return mf_page_type[i].page_type;

	return "unknown page";
}

static const char *get_action_result(int result)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(mf_action_result); i++)
		if (mf_action_result[i].result == result)
			return mf_action_result[i].action_result;

	return "unknown";
}

int ras_memory_failure_event_handler(struct trace_seq *s,
				     struct tep_record *record,
				     struct tep_event *event, void *context)
{
	unsigned long long val;
	struct ras_events *ras = context;
	time_t now;
	struct tm *tm;
	struct ras_mf_event ev;

	/*
	 * Newer kernels (3.10-rc1 or upper) provide an uptime clock.
	 * On previous kernels, the way to properly generate an event would
	 * be to inject a fake one, measure its timestamp and diff it against
	 * gettimeofday. We won't do it here. Instead, let's use uptime,
	 * falling-back to the event report's time, if "uptime" clock is
	 * not available (legacy kernels).
	 */

	if (ras->use_uptime)
		now = record->ts / user_hz + ras->uptime_diff;
	else
		now = time(NULL);

	tm = localtime(&now);
	if (tm)
		strftime(ev.timestamp, sizeof(ev.timestamp),
			 "%Y-%m-%d %H:%M:%S %z", tm);
	else
		strncpy(ev.timestamp, "1970-01-01 00:00:00 +0000", sizeof(ev.timestamp));
	trace_seq_printf(s, "%s ", ev.timestamp);

	if (tep_get_field_val(s,  event, "pfn", record, &val, 1) < 0)
		return -1;
	sprintf(ev.pfn, "0x%llx", val);
	trace_seq_printf(s, "pfn=0x%llx ", val);

	if (tep_get_field_val(s, event, "type", record, &val, 1) < 0)
		return -1;
	ev.page_type = get_page_type(val);
	trace_seq_printf(s, "page_type=%s ", ev.page_type);

	if (tep_get_field_val(s, event, "result", record, &val, 1) < 0)
		return -1;
	ev.action_result = get_action_result(val);
	trace_seq_printf(s, "action_result=%s ", ev.action_result);

	/* Store data into the SQLite DB */
#ifdef HAVE_SQLITE3
	ras_store_mf_event(ras, &ev);
#endif

#ifdef HAVE_ABRT_REPORT
	/* Report event to ABRT */
	ras_report_mf_event(ras, &ev);
#endif
	run_mf_trigger(&ev);

	return 0;
}