File: kmemleak.c

package info (click to toggle)
intel-gpu-tools 2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 62,024 kB
  • sloc: xml: 769,439; ansic: 348,692; python: 8,307; yacc: 2,781; perl: 1,196; sh: 1,178; lex: 487; asm: 227; makefile: 27; lisp: 11
file content (277 lines) | stat: -rw-r--r-- 6,546 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
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
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2025 Intel Corporation
 */

#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>

#include "kmemleak.h"

/* We can change the path for unit testing, see runner_kmemleak_init() */
static char runner_kmemleak_file[256] = "/sys/kernel/debug/kmemleak";

#define MAX_WRITE_RETRIES 5

/**
 * runner_kmemleak_write: Writes the buffer to the file descriptor retrying when
 * possible.
 * @fd: The file descriptor to write to.
 * @buf: Pointer to the data to write.
 * @count: Total number of bytes to write.
 *
 * Writes the buffer to the file descriptor retrying when possible.
 */
static bool runner_kmemleak_write(int fd, const void *buf, size_t count)
{
	const char *ptr = buf;
	size_t remaining = count;
	ssize_t written;
	int retries = 0;

	while (remaining > 0) {
		written = write(fd, ptr, remaining);
		if (written > 0) {
			ptr += written;
			remaining -= written;
		} else if (written == -1) {
			if (errno == EINTR || errno == EAGAIN) {
				/* Retry for recoverable errors */
				if (++retries > MAX_WRITE_RETRIES) {
					fprintf(stderr, "%s: Exceeded retry limit\n", __func__);
					return false;
				}
				continue;
			} else {
				/* Log unrecoverable error */
				fprintf(stderr, "%s: unrecoverable write error\n", __func__);
				return false;
			}
		} else if (written == 0) {
			if (++retries > MAX_WRITE_RETRIES) {
				fprintf(stderr, "%s: Exceeded retry limit\n", __func__);
				return false;
			}
		}
	}
	return true;
}

/**
 * runner_kmemleak_cmd:
 * @cmd: command to send to kmemleak
 *
 * Send a command to kmemleak.
 *
 * Returns: true if sending the command was successful, false otherwise.
 */
static bool runner_kmemleak_cmd(const char *cmd)
{
	int fd;
	bool res;

	fd = open(runner_kmemleak_file, O_RDWR);
	if (fd < 0)
		return false;

	res = runner_kmemleak_write(fd, cmd, strlen(cmd));
	close(fd);

	return res;
}

/**
 * runner_kmemleak_clear:
 *
 * Trigger an immediate clear on kmemleak.
 *
 * Returns: true if sending the command to clear was successful, false
 * otherwise.
 */
static bool runner_kmemleak_clear(void)
{
	return runner_kmemleak_cmd("clear");
}

/**
 * runner_kmemleak_found_leaks:
 *
 * Check if kmemleak found any leaks by trying to read one byte from the
 * kmemleak file.
 *
 * Returns: true if kmemleak found any leaks, false otherwise.
 */
static bool runner_kmemleak_found_leaks(void)
{
	int fd;
	char buf[1];
	size_t rlen;

	fd = open(runner_kmemleak_file, O_RDONLY);
	if (fd < 0)
		return false;

	rlen = read(fd, buf, 1);

	if (rlen == 1)
		lseek(fd, 0, SEEK_SET);

	close(fd);

	return rlen == 1;
}

/**
 * runner_kmemleak_scan:
 *
 * Trigger an immediate scan on kmemleak.
 *
 * Returns: true if leaks are found. False on failure and when no leaks are
 * found.
 */
static bool runner_kmemleak_scan(void)
{
	if (!runner_kmemleak_cmd("scan"))
		return false;

	/* kmemleak documentation states that "the memory scanning is only
	 * performed when the /sys/kernel/debug/kmemleak file is read." Read
	 * a byte to trigger the scan now.
	 */
	return runner_kmemleak_found_leaks();
}

/**
 * runner_kmemleak_append_to:
 * @last_test: last test name to append to the file
 * @resdirfd: file descriptor of the results directory
 * @kmemleak_each: if true we scan after each test
 * @sync: sync the kmemleak file often
 *
 * Append the kmemleak file to the result file adding a header indicating if
 * the leaks are for all tests or for a single one.
 *
 * Returns: true if appending to the file was successful, false otherwise.
 */
static bool runner_kmemleak_append_to(const char *last_test, int resdirfd,
				      bool kmemleak_each, bool sync)
{
	const char *before = "kmemleaks found before running any test\n\n";
	const char *once = "kmemleaks found after running all tests\n";
	int kmemleakfd, resfilefd;
	char buf[16384];
	size_t rlen;

	kmemleakfd = open(runner_kmemleak_file, O_RDONLY);
	if (kmemleakfd < 0)
		return false;

	/* Seek back to first byte */
	if (lseek(kmemleakfd, 0, SEEK_SET) == (off_t)-1) {
		close(kmemleakfd);
		return false;
	}

	/* Open text file to append */
	resfilefd = openat(resdirfd, KMEMLEAK_RESFILENAME,
			   O_RDWR | O_CREAT | O_APPEND, 0666);
	if (resfilefd < 0) {
		close(kmemleakfd);
		return false;
	}

	/* This is the header added before the content of the kmemleak file */
	if (kmemleak_each) {
		if (!last_test) {
			runner_kmemleak_write(resfilefd, before, strlen(before));
		} else {
			/* Write \n\n last_test \n to buf */
			snprintf(buf, sizeof(buf),
				 "\n\nkmemleaks found after running %s:\n",
				 last_test);

			runner_kmemleak_write(resfilefd, buf, strlen(buf));
			memset(buf, 0, sizeof(buf));
		}
	} else {
		runner_kmemleak_write(resfilefd, once, strlen(once));
	}

	if (sync)
		fsync(resfilefd);

	while ((rlen = read(kmemleakfd, buf, sizeof(buf))) > 0) {
		if (!runner_kmemleak_write(resfilefd, buf, rlen)) {
			close(resfilefd);
			close(kmemleakfd);
			return false;
		}
		if (sync)
			fsync(resfilefd);
	}

	close(resfilefd);
	close(kmemleakfd);

	return true;
}

/**
 * runner_kmemleak_init:
 * @unit_test_kmemleak_file: path to kmemleak file for unit testing
 *
 * Check if kmemleak is enabled in the kernel, if debugfs is mounted and
 * if kmemleak file is present and readable.
 *
 * Returns: true if kmemleak is enabled, false otherwise.
 */
bool runner_kmemleak_init(const char *unit_test_kmemleak_file)
{
	int fd;

	if (unit_test_kmemleak_file)
		snprintf(runner_kmemleak_file,
			 sizeof(runner_kmemleak_file),
			 "%s",
			 unit_test_kmemleak_file);

	fd = open(runner_kmemleak_file, O_RDONLY);
	if (fd < 0)
		return false;

	close(fd);

	return true;
}

/**
 * runner_kmemleak:
 * @last_test: last test name to append to the file
 * @resdirfd: file descriptor of the results directory
 * @kmemleak_each: Are we scanning once or scanning after each test?
 * @sync: sync the kmemleak file often
 *
 * This is the main function that should be called when integrating runner_kmemleak
 * into igt_runner or elsewhere. There are two flows:
 *  - run once: runs only once after all tests are completed
 *  - run for each test: runs after every test
 *
 * Returns: true on success, false otherwise.
 */
bool runner_kmemleak(const char *last_test, int resdirfd, bool kmemleak_each,
		     bool sync)
{
	/* Scan to collect results */
	if (runner_kmemleak_scan())
		if (!runner_kmemleak_append_to(last_test, resdirfd,
					       kmemleak_each, sync))
			return false;

	if (kmemleak_each)
		runner_kmemleak_clear();

	return true;
}