summaryrefslogtreecommitdiff
path: root/tests/core_hotunplug.c
blob: 2f7031094e2369a1ae9137f9135837340f88d4c3 (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
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright © 2019 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "i915/gem.h"
#include "igt.h"
#include "igt_device_scan.h"
#include "igt_kmod.h"
#include "igt_sysfs.h"

IGT_TEST_DESCRIPTION("Examine behavior of a driver on device hot unplug");

struct hotunplug {
	struct {
		int drm;
		int sysfs_dev;
		int sysfs_bus;
		int sysfs_drv;
	} fd;
	const char *dev_bus_addr;
};

/* Helpers */

/**
 * Subtests must be able to close examined devices completely.  Don't
 * use drm_open_driver() since in case of an i915 device it opens it
 * twice and keeps a second file descriptor open for exit handler use.
 */
static int local_drm_open_driver(const char *when, const char *why)
{
	int fd_drm;

	igt_debug("%sopening device%s\n", when, why);

	fd_drm = __drm_open_driver(DRIVER_ANY);
	igt_assert_fd(fd_drm);

	return fd_drm;
}

static void prepare_for_unbind(struct hotunplug *priv, char *buf, int buflen)
{
	int len;

	igt_assert(buflen);

	priv->fd.sysfs_drv = openat(priv->fd.sysfs_dev, "device/driver",
				    O_DIRECTORY);
	igt_assert_fd(priv->fd.sysfs_drv);

	len = readlinkat(priv->fd.sysfs_dev, "device", buf, buflen - 1);
	buf[len] = '\0';
	priv->dev_bus_addr = strrchr(buf, '/');
	igt_assert(priv->dev_bus_addr++);

	/* sysfs_dev no longer needed */
	close(priv->fd.sysfs_dev);
}

static void prepare(struct hotunplug *priv, char *buf, int buflen)
{
	priv->fd.drm = local_drm_open_driver("", " for subtest");

	priv->fd.sysfs_dev = igt_sysfs_open(priv->fd.drm);
	igt_assert_fd(priv->fd.sysfs_dev);

	if (buf) {
		prepare_for_unbind(priv, buf, buflen);
	} else {
		/* prepare for bus rescan */
		priv->fd.sysfs_bus = openat(priv->fd.sysfs_dev,
					    "device/subsystem", O_DIRECTORY);
		igt_assert_fd(priv->fd.sysfs_bus);
	}
}

static const char *failure;

/* Unbind the driver from the device */
static void driver_unbind(int fd_sysfs_drv, const char *dev_bus_addr,
			  const char *prefix)
{
	igt_debug("%sunbinding the driver from the device\n", prefix);

	failure = "Driver unbind timeout!";
	igt_set_timeout(60, failure);
	igt_sysfs_set(fd_sysfs_drv, "unbind", dev_bus_addr);
	igt_reset_timeout();
	failure = NULL;

	/* don't close fd_sysfs_drv, it will be used for driver rebinding */
}

/* Re-bind the driver to the device */
static void driver_bind(int fd_sysfs_drv, const char *dev_bus_addr)
{
	igt_debug("rebinding the driver to the device\n");

	failure = "Driver re-bind timeout!";
	igt_set_timeout(60, failure);
	igt_sysfs_set(fd_sysfs_drv, "bind", dev_bus_addr);
	igt_reset_timeout();
	failure = NULL;

	close(fd_sysfs_drv);
}

/* Remove (virtually unplug) the device from its bus */
static void device_unplug(int fd_sysfs_dev, const char *prefix)
{
	igt_debug("%sunplugging the device\n", prefix);

	failure = "Device unplug timeout!";
	igt_set_timeout(60, failure);
	igt_sysfs_set(fd_sysfs_dev, "device/remove", "1");
	igt_reset_timeout();
	failure = NULL;

	close(fd_sysfs_dev);
}

/* Re-discover the device by rescanning its bus */
static void bus_rescan(int fd_sysfs_bus)
{
	igt_debug("rediscovering the device\n");

	failure = "Bus rescan timeout!";
	igt_set_timeout(60, failure);
	igt_sysfs_set(fd_sysfs_bus, "rescan", "1");
	igt_reset_timeout();
	failure = NULL;

	close(fd_sysfs_bus);
}

static void healthcheck(void)
{
	int fd_drm;

	/* device name may have changed, rebuild IGT device list */
	igt_devices_scan(true);

	failure = "Device reopen failure!";
	fd_drm = local_drm_open_driver("re", " for health check");
	failure = NULL;

	if (is_i915_device(fd_drm)) {
		failure = "GEM failure";
		igt_require_gem(fd_drm);
		failure = NULL;
	}

	close(fd_drm);
}

static void set_filter_from_device(int fd)
{
	const char *filter_type = "sys:";
	char filter[strlen(filter_type) + PATH_MAX + 1];
	char *dst = stpcpy(filter, filter_type);
	char path[PATH_MAX + 1];

	igt_assert(igt_sysfs_path(fd, path, PATH_MAX));
	strncat(path, "/device", PATH_MAX - strlen(path));
	igt_assert(realpath(path, dst));

	igt_device_filter_free_all();
	igt_assert_eq(igt_device_filter_add(filter), 1);
}

/* Subtests */

static void unbind_rebind(void)
{
	struct hotunplug priv;
	char buf[PATH_MAX];

	prepare(&priv, buf, sizeof(buf));

	igt_debug("closing the device\n");
	close(priv.fd.drm);

	driver_unbind(priv.fd.sysfs_drv, priv.dev_bus_addr, "");

	driver_bind(priv.fd.sysfs_drv, priv.dev_bus_addr);

	healthcheck();
}

static void unplug_rescan(void)
{
	struct hotunplug priv;

	prepare(&priv, NULL, 0);

	igt_debug("closing the device\n");
	close(priv.fd.drm);

	device_unplug(priv.fd.sysfs_dev, "");

	bus_rescan(priv.fd.sysfs_bus);

	healthcheck();
}

static void hotunbind_lateclose(void)
{
	struct hotunplug priv;
	char buf[PATH_MAX];

	prepare(&priv, buf, sizeof(buf));

	driver_unbind(priv.fd.sysfs_drv, priv.dev_bus_addr, "hot ");

	driver_bind(priv.fd.sysfs_drv, priv.dev_bus_addr);

	igt_debug("late closing the unbound device instance\n");
	close(priv.fd.drm);

	healthcheck();
}

static void hotunplug_lateclose(void)
{
	struct hotunplug priv;

	prepare(&priv, NULL, 0);

	device_unplug(priv.fd.sysfs_dev, "hot ");

	bus_rescan(priv.fd.sysfs_bus);

	igt_debug("late closing the removed device instance\n");
	close(priv.fd.drm);

	healthcheck();
}

/* Main */

igt_main
{
	igt_fixture {
		int fd_drm;

		fd_drm = __drm_open_driver(DRIVER_ANY);
		igt_skip_on_f(fd_drm < 0, "No known DRM device found\n");

		if (is_i915_device(fd_drm)) {
			gem_quiescent_gpu(fd_drm);
			igt_require_gem(fd_drm);
		}

		/* Make sure subtests always reopen the same device */
		set_filter_from_device(fd_drm);

		close(fd_drm);
	}

	igt_describe("Check if the driver can be cleanly unbound from a device believed to be closed");
	igt_subtest("unbind-rebind")
		unbind_rebind();

	igt_fixture
		igt_abort_on_f(failure, "%s\n", failure);

	igt_describe("Check if a device believed to be closed can be cleanly unplugged");
	igt_subtest("unplug-rescan")
		unplug_rescan();

	igt_fixture
		igt_abort_on_f(failure, "%s\n", failure);

	igt_describe("Check if the driver can be cleanly unbound from a still open device, then released");
	igt_subtest("hotunbind-lateclose")
		hotunbind_lateclose();

	igt_fixture
		igt_abort_on_f(failure, "%s\n", failure);

	igt_describe("Check if a still open device can be cleanly unplugged, then released");
	igt_subtest("hotunplug-lateclose")
		hotunplug_lateclose();

	igt_fixture
		igt_abort_on_f(failure, "%s\n", failure);
}