xref: /illumos-gate/usr/src/cmd/fm/modules/common/cpumem-retire/cma_page.c (revision 1a220b56b93ff1dc80855691548503117af4cc10)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Page retirement can be an extended process due to the fact that a retirement
31  * may not be possible when the original request is made.  The kernel will
32  * repeatedly attempt to retire a given page, but will not let us know when the
33  * page has been retired.  We therefore have to poll to see if the retirement
34  * has been completed.  This poll is implemented with a bounded exponential
35  * backoff to reduce the burden which we impose upon the system.
36  *
37  * To reduce the burden on fmd in the face of retirement storms, we schedule
38  * all retries as a group.  In the simplest case, we attempt to retire a single
39  * page.  When forced to retry, we initially schedule a retry at a configurable
40  * interval t.  If the retry fails, we schedule another at 2 * t, and so on,
41  * until t reaches the maximum interval (also configurable).  Future retries
42  * for that page will occur with t equal to the maximum interval value.  We
43  * will never give up on a retirement.
44  *
45  * With multiple retirements, the situation gets slightly more complicated.  As
46  * indicated above, we schedule retries as a group.  We don't want to deny new
47  * pages their short retry intervals, so we'll (re)set the retry interval to the
48  * value appropriate for the newest page.
49  */
50 
51 #include <cma.h>
52 
53 #include <time.h>
54 #include <fcntl.h>
55 #include <errno.h>
56 #include <unistd.h>
57 #include <strings.h>
58 #include <fm/fmd_api.h>
59 #include <fm/libtopo.h>
60 #include <sys/fm/protocol.h>
61 #include <sys/mem.h>
62 
63 static int
64 cma_page_cmd(fmd_hdl_t *hdl, int cmd, nvlist_t *nvl)
65 {
66 	mem_page_t mpage;
67 	char *fmribuf;
68 	size_t fmrisz;
69 	int fd, rc, err;
70 
71 	if ((fd = open("/dev/mem", O_RDONLY)) < 0)
72 		return (-1); /* errno is set for us */
73 
74 	if ((errno = nvlist_size(nvl, &fmrisz, NV_ENCODE_NATIVE)) != 0 ||
75 	    fmrisz > MEM_FMRI_MAX_BUFSIZE ||
76 	    (fmribuf = fmd_hdl_alloc(hdl, fmrisz, FMD_SLEEP)) == NULL) {
77 		(void) close(fd);
78 		return (-1); /* errno is set for us */
79 	}
80 
81 	if ((errno = nvlist_pack(nvl, &fmribuf, &fmrisz,
82 	    NV_ENCODE_NATIVE, 0)) != 0) {
83 		fmd_hdl_free(hdl, fmribuf, fmrisz);
84 		(void) close(fd);
85 		return (-1); /* errno is set for us */
86 	}
87 
88 	mpage.m_fmri = fmribuf;
89 	mpage.m_fmrisz = fmrisz;
90 
91 	if ((rc = ioctl(fd, cmd, &mpage)) < 0)
92 		err = errno;
93 
94 	fmd_hdl_free(hdl, fmribuf, fmrisz);
95 
96 	(void) close(fd);
97 
98 	if (rc < 0) {
99 		errno = err;
100 		return (-1);
101 	}
102 
103 	return (0);
104 }
105 
106 static void
107 cma_page_free(fmd_hdl_t *hdl, cma_page_t *page)
108 {
109 	if (page->pg_fmri != NULL)
110 		nvlist_free(page->pg_fmri);
111 	fmd_hdl_free(hdl, page, sizeof (cma_page_t));
112 }
113 
114 /*
115  * Retire the specified ASRU, referring to a memory page by PA or by DIMM
116  * offset (i.e. the encoded coordinates internal bank, row, and column).
117  * In the initial FMA implementation, fault.memory.page exported an ASRU
118  * with an explicit physical address, which is valid at the initial time of
119  * diagnosis but may not be later following DR, DIMM removal, or interleave
120  * changes.  On SPARC, this issue was solved by exporting the DIMM offset
121  * and pushing the entire FMRI to the platform memory controller through
122  * /dev/mem so it can derive the current PA from the DIMM and offset.
123  * On x64, we also use DIMM and offset, but the mem:/// unum string is an
124  * encoded hc:/// FMRI that is then used by the x64 memory controller driver.
125  * At some point these three approaches need to be rationalized: all platforms
126  * should use the same scheme, either with decoding in the kernel or decoding
127  * in userland (i.e. with a libtopo method to compute and update the PA).
128  */
129 /*ARGSUSED*/
130 int
131 cma_page_retire(fmd_hdl_t *hdl, nvlist_t *nvl, nvlist_t *asru, const char *uuid)
132 {
133 	cma_page_t *page;
134 	uint64_t pageaddr;
135 	char *unumstr;
136 	nvlist_t *asrucp = NULL;
137 
138 	/* It should already be expanded, but we'll do it again anyway */
139 	if (fmd_nvl_fmri_expand(hdl, asru) < 0) {
140 		fmd_hdl_debug(hdl, "failed to expand page asru\n");
141 		cma_stats.bad_flts.fmds_value.ui64++;
142 		return (CMA_RA_FAILURE);
143 	}
144 
145 	if (nvlist_lookup_uint64(asru, FM_FMRI_MEM_PHYSADDR, &pageaddr) != 0) {
146 		fmd_hdl_debug(hdl, "mem fault missing '%s'\n",
147 		    FM_FMRI_MEM_PHYSADDR);
148 		cma_stats.bad_flts.fmds_value.ui64++;
149 		return (CMA_RA_FAILURE);
150 	}
151 
152 	if (!cma.cma_page_doretire) {
153 		fmd_hdl_debug(hdl, "suppressed retire of page %llx\n",
154 		    (u_longlong_t)pageaddr);
155 		cma_stats.page_supp.fmds_value.ui64++;
156 		return (CMA_RA_FAILURE);
157 	}
158 
159 	if (!fmd_nvl_fmri_present(hdl, asru)) {
160 		fmd_hdl_debug(hdl, "page retire overtaken by events\n");
161 		cma_stats.page_nonent.fmds_value.ui64++;
162 		return (CMA_RA_SUCCESS);
163 	}
164 
165 	/*
166 	 * If the unum is an hc fmri string expand it to an fmri and include
167 	 * that in a modified asru nvlist.
168 	 */
169 	if (nvlist_lookup_string(asru, FM_FMRI_MEM_UNUM, &unumstr) == 0 &&
170 	    strncmp(unumstr, "hc:/", 4) == 0) {
171 		int err;
172 		nvlist_t *unumfmri;
173 		struct topo_hdl *thp = fmd_hdl_topology(hdl, TOPO_VERSION);
174 
175 		if (topo_fmri_str2nvl(thp, unumstr, &unumfmri, &err) != 0) {
176 			fmd_hdl_debug(hdl, "page retire str2nvl failed: %s\n",
177 			    topo_strerror(err));
178 			return (CMA_RA_FAILURE);
179 		}
180 
181 		if (nvlist_dup(asru, &asrucp, 0) != 0) {
182 			fmd_hdl_debug(hdl, "page retire nvlist dup failed\n");
183 			nvlist_free(unumfmri);
184 			return (CMA_RA_FAILURE);
185 		}
186 
187 		if (nvlist_add_nvlist(asrucp, FM_FMRI_MEM_UNUM "-fmri",
188 		    unumfmri) != 0) {
189 			fmd_hdl_debug(hdl, "page retire failed to add "
190 			    "unumfmri to modified asru");
191 			nvlist_free(unumfmri);
192 			nvlist_free(asrucp);
193 			return (CMA_RA_FAILURE);
194 		}
195 		nvlist_free(unumfmri);
196 	}
197 
198 	if (cma_page_cmd(hdl, MEM_PAGE_FMRI_RETIRE,
199 	    asrucp ? asrucp : asru) == 0) {
200 		fmd_hdl_debug(hdl, "retired page 0x%llx\n",
201 		    (u_longlong_t)pageaddr);
202 		cma_stats.page_flts.fmds_value.ui64++;
203 		if (asrucp)
204 			nvlist_free(asrucp);
205 		return (CMA_RA_SUCCESS);
206 	} else if (errno != EAGAIN) {
207 		fmd_hdl_debug(hdl, "retire of page 0x%llx failed, will not "
208 		    "retry: %s\n", (u_longlong_t)pageaddr, strerror(errno));
209 		if (asrucp)
210 			nvlist_free(asrucp);
211 		if (uuid != NULL && cma.cma_page_maxretries != 0)
212 			return (CMA_RA_SUCCESS);
213 		return (CMA_RA_FAILURE);
214 	}
215 
216 	/*
217 	 * The page didn't immediately retire.  We'll need to periodically
218 	 * check to see if it has been retired.
219 	 */
220 	fmd_hdl_debug(hdl, "page didn't retire - sleeping\n");
221 
222 	page = fmd_hdl_zalloc(hdl, sizeof (cma_page_t), FMD_SLEEP);
223 	page->pg_addr = pageaddr;
224 	if (asrucp) {
225 		page->pg_fmri = asrucp;
226 	} else {
227 		(void) nvlist_dup(asru, &page->pg_fmri, 0);
228 	}
229 	if (uuid != NULL)
230 		page->pg_uuid = fmd_hdl_strdup(hdl, uuid, FMD_SLEEP);
231 
232 	page->pg_next = cma.cma_pages;
233 	cma.cma_pages = page;
234 
235 	if (cma.cma_page_timerid != 0)
236 		fmd_timer_remove(hdl, cma.cma_page_timerid);
237 
238 	cma.cma_page_curdelay = cma.cma_page_mindelay;
239 
240 	cma.cma_page_timerid =
241 	    fmd_timer_install(hdl, NULL, NULL, cma.cma_page_curdelay);
242 
243 	return (CMA_RA_FAILURE);
244 }
245 
246 static int
247 page_retry(fmd_hdl_t *hdl, cma_page_t *page)
248 {
249 	if (page->pg_fmri != NULL && !fmd_nvl_fmri_present(hdl,
250 	    page->pg_fmri)) {
251 		fmd_hdl_debug(hdl, "page retire overtaken by events");
252 		cma_stats.page_nonent.fmds_value.ui64++;
253 
254 		if (page->pg_uuid != NULL)
255 			fmd_case_uuclose(hdl, page->pg_uuid);
256 		return (1); /* no longer a page to retire */
257 	}
258 
259 	if (cma_page_cmd(hdl, MEM_PAGE_FMRI_ISRETIRED, page->pg_fmri) == 0) {
260 		fmd_hdl_debug(hdl, "retired page 0x%llx on retry %u\n",
261 		    page->pg_addr, page->pg_nretries);
262 		cma_stats.page_flts.fmds_value.ui64++;
263 
264 		if (page->pg_uuid != NULL)
265 			fmd_case_uuclose(hdl, page->pg_uuid);
266 		return (1); /* page retired */
267 	}
268 
269 	if (errno == EAGAIN) {
270 		fmd_hdl_debug(hdl, "scheduling another retry for 0x%llx\n",
271 		    page->pg_addr);
272 		return (0); /* schedule another retry */
273 	} else {
274 		if (errno == EIO) {
275 			fmd_hdl_debug(hdl, "failed to retry page 0x%llx "
276 			    "retirement: page isn't scheduled for retirement\n",
277 			    page->pg_addr);
278 		} else {
279 			fmd_hdl_debug(hdl, "failed to retry page 0x%llx "
280 			    "retirement: %s\n", page->pg_addr,
281 			    strerror(errno));
282 		}
283 
284 		if (page->pg_uuid != NULL && cma.cma_page_maxretries != 0)
285 			fmd_case_uuclose(hdl, page->pg_uuid);
286 
287 		cma_stats.page_fails.fmds_value.ui64++;
288 		return (1); /* give up */
289 	}
290 }
291 
292 void
293 cma_page_retry(fmd_hdl_t *hdl)
294 {
295 	cma_page_t **pagep;
296 
297 	cma.cma_page_timerid = 0;
298 
299 	fmd_hdl_debug(hdl, "page_retry: timer fired\n");
300 
301 	pagep = &cma.cma_pages;
302 	while (*pagep != NULL) {
303 		cma_page_t *page = *pagep;
304 
305 		if (page_retry(hdl, page)) {
306 			/*
307 			 * Successful retry or we're giving up - remove from
308 			 * the list
309 			 */
310 			*pagep = page->pg_next;
311 
312 			if (page->pg_uuid != NULL)
313 				fmd_hdl_strfree(hdl, page->pg_uuid);
314 
315 			cma_page_free(hdl, page);
316 		} else if (cma.cma_page_maxretries == 0 ||
317 		    page->pg_nretries < cma.cma_page_maxretries) {
318 			page->pg_nretries++;
319 			pagep = &page->pg_next;
320 		} else {
321 			/*
322 			 * Tunable maxretries was set and we reached
323 			 * the max, so just close the case.
324 			 */
325 			fmd_hdl_debug(hdl,
326 			    "giving up page retire 0x%llx on retry %u\n",
327 			    page->pg_addr, page->pg_nretries);
328 			cma_stats.page_retmax.fmds_value.ui64++;
329 
330 			if (page->pg_uuid != NULL) {
331 				fmd_case_uuclose(hdl, page->pg_uuid);
332 				fmd_hdl_strfree(hdl, page->pg_uuid);
333 			}
334 
335 			*pagep = page->pg_next;
336 
337 			cma_page_free(hdl, page);
338 		}
339 	}
340 
341 	if (cma.cma_pages == NULL)
342 		return; /* no more retirements */
343 
344 	/*
345 	 * We still have retirements that haven't completed.  Back the delay
346 	 * off, and schedule a retry.
347 	 */
348 	cma.cma_page_curdelay = MIN(cma.cma_page_curdelay * 2,
349 	    cma.cma_page_maxdelay);
350 
351 	fmd_hdl_debug(hdl, "scheduled page retirement retry for %llu secs\n",
352 	    (u_longlong_t)(cma.cma_page_curdelay / NANOSEC));
353 
354 	cma.cma_page_timerid =
355 	    fmd_timer_install(hdl, NULL, NULL, cma.cma_page_curdelay);
356 }
357 
358 void
359 cma_page_fini(fmd_hdl_t *hdl)
360 {
361 	cma_page_t *page;
362 
363 	while ((page = cma.cma_pages) != NULL) {
364 		cma.cma_pages = page->pg_next;
365 		cma_page_free(hdl, page);
366 	}
367 }
368