summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fm/modules/common/cpumem-retire/cma_page.c
blob: a5cca2502dcaeec826205aa72ae830a020cecd5a (plain)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Page retirement can be an extended process due to the fact that a retirement
 * may not be possible when the original request is made.  The kernel will
 * repeatedly attempt to retire a given page, but will not let us know when the
 * page has been retired.  We therefore have to poll to see if the retirement
 * has been completed.  This poll is implemented with a bounded exponential
 * backoff to reduce the burden which we impose upon the system.
 *
 * To reduce the burden on fmd in the face of retirement storms, we schedule
 * all retries as a group.  In the simplest case, we attempt to retire a single
 * page.  When forced to retry, we initially schedule a retry at a configurable
 * interval t.  If the retry fails, we schedule another at 2 * t, and so on,
 * until t reaches the maximum interval (also configurable).  Future retries
 * for that page will occur with t equal to the maximum interval value.  We
 * will never give up on a retirement.
 *
 * With multiple retirements, the situation gets slightly more complicated.  As
 * indicated above, we schedule retries as a group.  We don't want to deny new
 * pages their short retry intervals, so we'll (re)set the retry interval to the
 * value appropriate for the newest page.
 */

#include <cma.h>

#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <strings.h>
#include <fm/fmd_api.h>
#include <fm/libtopo.h>
#include <fm/fmd_fmri.h>
#include <fm/fmd_agent.h>
#include <sys/fm/protocol.h>

static void
cma_page_free(fmd_hdl_t *hdl, cma_page_t *page)
{
	nvlist_free(page->pg_asru);
	nvlist_free(page->pg_rsrc);
	fmd_hdl_free(hdl, page, sizeof (cma_page_t));
}

/*
 * Retire the specified ASRU, referring to a memory page by PA or by DIMM
 * offset (i.e. the encoded coordinates internal bank, row, and column).
 * In the initial FMA implementation, fault.memory.page exported an ASRU
 * with an explicit physical address, which is valid at the initial time of
 * diagnosis but may not be later following DR, DIMM removal, or interleave
 * changes.  On SPARC, this issue was solved by exporting the DIMM offset
 * and pushing the entire FMRI to the platform memory controller through
 * /dev/fm so it can derive the current PA from the DIMM and offset.
 * On x86, we also encode DIMM and offset in hc-specific, which is then used
 * by the x64 memory controller driver.
 * At some point these three approaches need to be rationalized: all platforms
 * should use the same scheme, either with decoding in the kernel or decoding
 * in userland (i.e. with a libtopo method to compute and update the PA).
 */
/*ARGSUSED*/
int
cma_page_retire(fmd_hdl_t *hdl, nvlist_t *nvl, nvlist_t *asru,
    const char *uuid, boolean_t repair)
{
	cma_page_t *page;
	uint64_t pageaddr;
	const char *action = repair ? "unretire" : "retire";
	int rc;
	nvlist_t *rsrc = NULL, *asrucp = NULL, *hcsp;

	(void) nvlist_lookup_nvlist(nvl, FM_FAULT_RESOURCE, &rsrc);

	if (nvlist_dup(asru, &asrucp, 0) != 0) {
		fmd_hdl_debug(hdl, "page retire nvlist dup failed\n");
		return (CMA_RA_FAILURE);
	}

	/* It should already be expanded, but we'll do it again anyway */
	if (fmd_nvl_fmri_expand(hdl, asrucp) < 0) {
		fmd_hdl_debug(hdl, "failed to expand page asru\n");
		cma_stats.bad_flts.fmds_value.ui64++;
		nvlist_free(asrucp);
		return (CMA_RA_FAILURE);
	}

	if (!repair && !fmd_nvl_fmri_present(hdl, asrucp)) {
		fmd_hdl_debug(hdl, "page retire overtaken by events\n");
		cma_stats.page_nonent.fmds_value.ui64++;
		nvlist_free(asrucp);
		return (CMA_RA_SUCCESS);
	}

	/* Figure out physaddr from resource or asru */
	if (rsrc == NULL ||
	    nvlist_lookup_nvlist(rsrc, FM_FMRI_HC_SPECIFIC, &hcsp) != 0 ||
	    (nvlist_lookup_uint64(hcsp, "asru-" FM_FMRI_HC_SPECIFIC_PHYSADDR,
	    &pageaddr) != 0 && nvlist_lookup_uint64(hcsp,
	    FM_FMRI_HC_SPECIFIC_PHYSADDR, &pageaddr) != 0)) {
		if (nvlist_lookup_uint64(asrucp, FM_FMRI_MEM_PHYSADDR,
		    &pageaddr) != 0) {
			fmd_hdl_debug(hdl, "mem fault missing 'physaddr'\n");
			cma_stats.bad_flts.fmds_value.ui64++;
			nvlist_free(asrucp);
			return (CMA_RA_FAILURE);
		}
	}

	if (repair) {
		if (!cma.cma_page_dounretire) {
			fmd_hdl_debug(hdl, "suppressed unretire of page %llx\n",
			    (u_longlong_t)pageaddr);
			cma_stats.page_supp.fmds_value.ui64++;
			nvlist_free(asrucp);
			return (CMA_RA_SUCCESS);
		}
		/* If unretire via topo fails, we fall back to legacy way */
		if (rsrc == NULL || (rc = fmd_nvl_fmri_unretire(hdl, rsrc)) < 0)
			rc = cma_fmri_page_unretire(hdl, asrucp);
	} else {
		if (!cma.cma_page_doretire) {
			fmd_hdl_debug(hdl, "suppressed retire of page %llx\n",
			    (u_longlong_t)pageaddr);
			cma_stats.page_supp.fmds_value.ui64++;
			nvlist_free(asrucp);
			return (CMA_RA_FAILURE);
		}
		/* If retire via topo fails, we fall back to legacy way */
		if (rsrc == NULL || (rc = fmd_nvl_fmri_retire(hdl, rsrc)) < 0)
			rc = cma_fmri_page_retire(hdl, asrucp);
	}

	if (rc == FMD_AGENT_RETIRE_DONE) {
		fmd_hdl_debug(hdl, "%sd page 0x%llx\n",
		    action, (u_longlong_t)pageaddr);
		if (repair)
			cma_stats.page_repairs.fmds_value.ui64++;
		else
			cma_stats.page_flts.fmds_value.ui64++;
		nvlist_free(asrucp);
		return (CMA_RA_SUCCESS);
	} else if (repair || rc != FMD_AGENT_RETIRE_ASYNC) {
		fmd_hdl_debug(hdl, "%s of page 0x%llx failed, will not "
		    "retry: %s\n", action, (u_longlong_t)pageaddr,
		    strerror(errno));

		cma_stats.page_fails.fmds_value.ui64++;
		nvlist_free(asrucp);
		return (CMA_RA_FAILURE);
	}

	/*
	 * The page didn't immediately retire.  We'll need to periodically
	 * check to see if it has been retired.
	 */
	fmd_hdl_debug(hdl, "page didn't retire - sleeping\n");

	page = fmd_hdl_zalloc(hdl, sizeof (cma_page_t), FMD_SLEEP);
	page->pg_addr = pageaddr;
	if (rsrc != NULL)
		(void) nvlist_dup(rsrc, &page->pg_rsrc, 0);
	page->pg_asru = asrucp;
	if (uuid != NULL)
		page->pg_uuid = fmd_hdl_strdup(hdl, uuid, FMD_SLEEP);

	page->pg_next = cma.cma_pages;
	cma.cma_pages = page;

	if (cma.cma_page_timerid != 0)
		fmd_timer_remove(hdl, cma.cma_page_timerid);

	cma.cma_page_curdelay = cma.cma_page_mindelay;

	cma.cma_page_timerid =
	    fmd_timer_install(hdl, NULL, NULL, cma.cma_page_curdelay);

	/* Don't free asrucp here.  This FMRI will be needed for retry. */
	return (CMA_RA_FAILURE);
}

static int
page_retry(fmd_hdl_t *hdl, cma_page_t *page)
{
	int rc;

	if (page->pg_asru != NULL &&
	    !fmd_nvl_fmri_present(hdl, page->pg_asru)) {
		fmd_hdl_debug(hdl, "page retire overtaken by events");
		cma_stats.page_nonent.fmds_value.ui64++;

		if (page->pg_uuid != NULL)
			fmd_case_uuclose(hdl, page->pg_uuid);
		return (1); /* no longer a page to retire */
	}

	if (page->pg_rsrc == NULL ||
	    (rc = fmd_nvl_fmri_service_state(hdl, page->pg_rsrc)) < 0)
		rc = cma_fmri_page_service_state(hdl, page->pg_asru);

	if (rc == FMD_SERVICE_STATE_UNUSABLE) {
		fmd_hdl_debug(hdl, "retired page 0x%llx on retry %u\n",
		    page->pg_addr, page->pg_nretries);
		cma_stats.page_flts.fmds_value.ui64++;

		if (page->pg_uuid != NULL)
			fmd_case_uuclose(hdl, page->pg_uuid);
		return (1); /* page retired */
	}

	if (rc == FMD_SERVICE_STATE_ISOLATE_PENDING) {
		fmd_hdl_debug(hdl, "scheduling another retry for 0x%llx\n",
		    page->pg_addr);
		return (0); /* schedule another retry */
	} else {
		fmd_hdl_debug(hdl, "failed to retry page 0x%llx "
		    "retirement: %s\n", page->pg_addr,
		    strerror(errno));

		cma_stats.page_fails.fmds_value.ui64++;
		return (1); /* give up */
	}
}

void
cma_page_retry(fmd_hdl_t *hdl)
{
	cma_page_t **pagep;

	cma.cma_page_timerid = 0;

	fmd_hdl_debug(hdl, "page_retry: timer fired\n");

	pagep = &cma.cma_pages;
	while (*pagep != NULL) {
		cma_page_t *page = *pagep;

		if (page_retry(hdl, page)) {
			/*
			 * Successful retry or we're giving up - remove from
			 * the list
			 */
			*pagep = page->pg_next;

			if (page->pg_uuid != NULL)
				fmd_hdl_strfree(hdl, page->pg_uuid);

			cma_page_free(hdl, page);
		} else {
			page->pg_nretries++;
			pagep = &page->pg_next;
		}
	}

	if (cma.cma_pages == NULL)
		return; /* no more retirements */

	/*
	 * We still have retirements that haven't completed.  Back the delay
	 * off, and schedule a retry.
	 */
	cma.cma_page_curdelay = MIN(cma.cma_page_curdelay * 2,
	    cma.cma_page_maxdelay);

	fmd_hdl_debug(hdl, "scheduled page retirement retry for %llu secs\n",
	    (u_longlong_t)(cma.cma_page_curdelay / NANOSEC));

	cma.cma_page_timerid =
	    fmd_timer_install(hdl, NULL, NULL, cma.cma_page_curdelay);
}

void
cma_page_fini(fmd_hdl_t *hdl)
{
	cma_page_t *page;

	while ((page = cma.cma_pages) != NULL) {
		cma.cma_pages = page->pg_next;
		if (page->pg_uuid != NULL)
			fmd_hdl_strfree(hdl, page->pg_uuid);
		cma_page_free(hdl, page);
	}
}