File: daxctl-create.sh

package info (click to toggle)
ndctl 81-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,436 kB
  • sloc: ansic: 41,432; sh: 3,931; makefile: 28
file content (412 lines) | stat: -rwxr-xr-x 9,417 bytes parent folder | download | duplicates (2)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/bin/bash -Ex
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2020, Oracle Corporation.

rc=77
. $(dirname $0)/common

trap 'cleanup $LINENO' ERR

modprobe -r cxl_test
modprobe cxl_test
$CXL list

cleanup()
{
	printf "Error at line %d\n" "$1"
	[[ $testdev ]] && reset_dax
	exit $rc
}

find_testdev()
{
	local rc=77

	# find a victim region provided by cxl_test
	region_id="$("$DAXCTL" list -R | jq -r ".[] | select(.path | contains(\"cxl_acpi.0\")) | .id")"

	if [[ ! "$region_id" ]]; then
		printf "Unable to find a victim region\n"
		exit "$rc"
	fi

	# find a victim device
	testdev=$("$DAXCTL" list -D -r "$region_id" | jq -er '.[0].chardev | .//""')
	if [[ ! $testdev  ]]; then
		printf "Unable to find a victim device\n"
		exit "$rc"
	fi
	printf "Found victim dev: %s on region id 0\n" "$testdev"
}

setup_dev()
{
	local rc=1
	local nmaps=0
	test -n "$testdev"

	nmaps=$(daxctl_get_nr_mappings "$testdev")
	if [[ $nmaps == 0 ]]; then
		printf "Device is idle"
		exit "$rc"
	fi

	"$DAXCTL" reconfigure-device -m devdax -f "$testdev"
	"$DAXCTL" disable-device "$testdev"
	"$DAXCTL" reconfigure-device -s 0 "$testdev"
	available=$("$DAXCTL" list -r "$region_id" | jq -er '.[0].available_size | .//""')
}

reset_dev()
{
	test -n "$testdev"

	"$DAXCTL" disable-device "$testdev"
	"$DAXCTL" reconfigure-device -s "$available" "$testdev"
	"$DAXCTL" enable-device "$testdev"
}

reset_dax()
{
	test -n "$testdev"

	"$DAXCTL" disable-device -r "$region_id" all
	"$DAXCTL" destroy-device -r "$region_id" all
	"$DAXCTL" reconfigure-device -s "$available" "$testdev"
}

clear_dev()
{
	"$DAXCTL" disable-device "$testdev"
	"$DAXCTL" reconfigure-device -s 0 "$testdev"
}

test_pass()
{
	local rc=1

	# Available size
	_available_size=$("$DAXCTL" list -r "$region_id" | jq -er '.[0].available_size | .//""')
	if [[ ! $_available_size == "$available" ]]; then
		echo "Unexpected available size $_available_size != $available"
		exit "$rc"
	fi
}

fail_if_available()
{
	local rc=1

	_size=$("$DAXCTL" list -r "$region_id" | jq -er '.[0].available_size | .//""')
	if [[ $_size ]]; then
		echo "Unexpected available size $_size"
		exit "$rc"
	fi
}

daxctl_get_dev()
{
	"$DAXCTL" list -d "$1" | jq -er '.[].chardev'
}

daxctl_get_mode()
{
	"$DAXCTL" list -d "$1" | jq -er '.[].mode'
}

daxctl_get_size_by_mapping()
{
	local size=0
	local _start=0
	local _end=0

	_start=$(cat "$1"/start)
	_end=$(cat "$1"/end)
	((size=size + _end - _start + 1))
	echo $size
}

daxctl_get_nr_mappings()
{
	local i=0
	local _size=0
	local devsize=0
	local path=""

	path=$(readlink -f /sys/bus/dax/devices/"$1"/)
	until ! [ -d "$path/mapping$i" ]
	do
		_size=$(daxctl_get_size_by_mapping "$path/mapping$i")
		if [[ $_size == 0 ]]; then
			i=0
			break
		fi
		((devsize=devsize + _size))
		((i=i + 1))
	done

	# Return number of mappings if the sizes between size field
	# and the one computed by mappingNNN are the same
	_size=$("$DAXCTL" list -d "$1" | jq -er '.[0].size | .//""')
	if [[ ! $_size == "$devsize" ]]; then
		echo 0
	else
		echo $i
	fi
}

daxctl_test_multi()
{
	local daxdev

	size=$((available / 4))

	if [[ $2 ]]; then
		"$DAXCTL" disable-device "$testdev"
		"$DAXCTL" reconfigure-device -s $size "$testdev"
	fi

	daxdev_1=$("$DAXCTL" create-device -r "$region_id" -s $size | jq -er '.[].chardev')
	test -n "$daxdev_1"

	daxdev_2=$("$DAXCTL" create-device -r "$region_id" -s $size | jq -er '.[].chardev')
	test -n "$daxdev_2"

	if [[ ! $2 ]]; then
		daxdev_3=$("$DAXCTL" create-device -r "$region_id" -s $size | jq -er '.[].chardev')
		test -n "$daxdev_3"
	fi

	# Hole
	"$DAXCTL" disable-device  "$1"
	"$DAXCTL" destroy-device "$1"

	# Pick space in the created hole and at the end
	new_size=$((size * 2))
	daxdev_4=$("$DAXCTL" create-device -r "$region_id" -s "$new_size" | jq -er '.[].chardev')
	test -n "$daxdev_4"
	test "$(daxctl_get_nr_mappings "$daxdev_4")" -eq 2

	fail_if_available

	"$DAXCTL" disable-device -r "$region_id" all
	"$DAXCTL" destroy-device -r "$region_id" all
}

daxctl_test_multi_reconfig()
{
	local ncfgs=$1
	local dump=$2
	local daxdev

	size=$((available / ncfgs))

	test -n "$testdev"

	"$DAXCTL" disable-device "$testdev"
	"$DAXCTL" reconfigure-device -s $size "$testdev"
	"$DAXCTL" disable-device "$testdev"

	daxdev_1=$("$DAXCTL" create-device -r "$region_id" -s $size | jq -er '.[].chardev')
	"$DAXCTL" disable-device "$daxdev_1"

	start=$((size + size))
	max=$((size * ncfgs / 2))
	for i in $(seq $start $size $max)
	do
		"$DAXCTL" disable-device "$testdev"
		"$DAXCTL" reconfigure-device -s "$i" "$testdev"

		"$DAXCTL" disable-device "$daxdev_1"
		"$DAXCTL" reconfigure-device -s "$i" "$daxdev_1"
	done

	test "$(daxctl_get_nr_mappings "$testdev")" -eq $((ncfgs / 2))
	test "$(daxctl_get_nr_mappings "$daxdev_1")" -eq $((ncfgs / 2))

	if [[ $dump ]]; then
		"$DAXCTL" list -M -d "$daxdev_1" | jq -er '.[]' > "$dump"
	fi

	fail_if_available

	"$DAXCTL" disable-device "$daxdev_1" && "$DAXCTL" destroy-device "$daxdev_1"
}

daxctl_test_adjust()
{
	local rc=1
	local ncfgs=4
	local daxdev

	size=$((available / ncfgs))

	test -n "$testdev"

	start=$((size + size))
	for i in $(seq 1 1 $ncfgs)
	do
		daxdev=$("$DAXCTL" create-device -r "$region_id" -s "$size" | jq -er '.[].chardev')
		test "$(daxctl_get_nr_mappings "$daxdev")" -eq 1
	done

	daxdev=$(daxctl_get_dev "dax$region_id.1")
	"$DAXCTL" disable-device "$daxdev" && "$DAXCTL" destroy-device "$daxdev"
	daxdev=$(daxctl_get_dev "dax$region_id.4")
	"$DAXCTL" disable-device "$daxdev" && "$DAXCTL" destroy-device "$daxdev"

	daxdev=$(daxctl_get_dev "dax$region_id.2")
	"$DAXCTL" disable-device "$daxdev"
	"$DAXCTL" reconfigure-device -s $((size * 2)) "$daxdev"
	# Allocates space at the beginning: expect two mappings as
	# as don't adjust the mappingX region. This is because we
	# preserve the relative page_offset of existing allocations
	test "$(daxctl_get_nr_mappings "$daxdev")" -eq 2

	daxdev=$(daxctl_get_dev "dax$region_id.3")
	"$DAXCTL" disable-device "$daxdev"
	"$DAXCTL" reconfigure-device -s $((size * 2)) "$daxdev"
	# Adjusts space at the end, expect one mapping because we are
	# able to extend existing region range.
	test "$(daxctl_get_nr_mappings "$daxdev")" -eq 1

	fail_if_available

	daxdev=$(daxctl_get_dev "dax$region_id.3")
	"$DAXCTL" disable-device "$daxdev" && "$DAXCTL" destroy-device "$daxdev"
	daxdev=$(daxctl_get_dev "dax$region_id.2")
	"$DAXCTL" disable-device "$daxdev" && "$DAXCTL" destroy-device "$daxdev"
}

# Test 0:
# Sucessfully zero out the region device and allocate the whole space again.
daxctl_test0()
{
	clear_dev
	test_pass
}

# Test 1:
# Sucessfully creates and destroys a device with the whole available space
daxctl_test1()
{
	local daxdev

	daxdev=$("$DAXCTL" create-device -r "$region_id" | jq -er '.[].chardev')

	test -n "$daxdev"
	test "$(daxctl_get_nr_mappings "$daxdev")" -eq 1
	fail_if_available

	"$DAXCTL" disable-device "$daxdev" && "$DAXCTL" destroy-device "$daxdev"

	clear_dev
	test_pass
}

# Test 2: space at the middle and at the end
# Successfully pick space in the middle and space at the end, by
# having the region device reconfigured with some of the memory.
daxctl_test2()
{
	daxctl_test_multi "$region_id.1" 1
	clear_dev
	test_pass
}

# Test 3: space at the beginning and at the end
# Successfully pick space in the beginning and space at the end, by
# having the region device emptied (so region beginning starts with daxX.1).
daxctl_test3()
{
	daxctl_test_multi "$region_id.1"
	clear_dev
	test_pass
}

# Test 4: space at the end
# Successfully reconfigure two devices in increasingly bigger allocations.
# The difference is that it reuses an existing resource, and only needs to
# pick at the end of the region
daxctl_test4()
{
	daxctl_test_multi_reconfig 8 ""
	clear_dev
	test_pass
}

# Test 5: space adjust
# Successfully adjusts two resources to fill the whole region
# First adjusts towards the beginning of region, the second towards the end.
daxctl_test5()
{
	daxctl_test_adjust
	clear_dev
	test_pass
}

# Test 6: align
# Successfully creates a device with a align property
daxctl_test6()
{
	local daxdev
	local align
	local size

	# Available size
	size=$available

	# Use 2M by default or 1G if supported
	align=2097152
	if (( available >= 1073741824 )); then
		align=1073741824
		size=$align
	fi

	daxdev=$("$DAXCTL" create-device -r "$region_id" -s $size -a $align | jq -er '.[].chardev')

	test -n "$daxdev"

	"$DAXCTL" disable-device "$daxdev" && "$DAXCTL" destroy-device "$daxdev"

	clear_dev
	test_pass
}

# Test 7: input device
# Successfully creates a device with an input file from the multi-range
# device test, and checking that we have the same number of mappings/size.
daxctl_test7()
{
	daxctl_test_multi_reconfig 8 "input.json"

	# The parameter should parse the region_id from the chardev entry
	# therefore using the same region_id as test4
	daxdev_1=$("$DAXCTL" create-device --input input.json | jq -er '.[].chardev')

	# Validate if it's the same mappings as done by test4
	# It also validates the size computed from the mappings
	# A zero value means it failed, and four mappings is what's
	# created by daxctl_test4
	test "$(daxctl_get_nr_mappings "$daxdev_1")" -eq 4

	"$DAXCTL" disable-device "$daxdev_1" && "$DAXCTL" destroy-device "$daxdev_1"

	clear_dev
	test_pass
}

find_testdev
rc=1
setup_dev
daxctl_test0
daxctl_test1
daxctl_test2
daxctl_test3
daxctl_test4
daxctl_test5
daxctl_test6
daxctl_test7
reset_dev
modprobe -r cxl_test
exit 0