summaryrefslogtreecommitdiffstats
path: root/tools/testing/memblock/tests/basic_api.c
blob: 70f62aae9df9b6df65b841e868105ad6830a7410 (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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// SPDX-License-Identifier: GPL-2.0-or-later
#include <string.h>
#include <linux/memblock.h>
#include <linux/sizes.h>
#include "basic_api.h"

#define EXPECTED_MEMBLOCK_REGIONS			128

static int memblock_initialization_check(void)
{
	reset_memblock();

	assert(memblock.memory.regions);
	assert(memblock.memory.cnt == 1);
	assert(memblock.memory.max == EXPECTED_MEMBLOCK_REGIONS);
	assert(strcmp(memblock.memory.name, "memory") == 0);

	assert(memblock.reserved.regions);
	assert(memblock.reserved.cnt == 1);
	assert(memblock.memory.max == EXPECTED_MEMBLOCK_REGIONS);
	assert(strcmp(memblock.reserved.name, "reserved") == 0);

	assert(!memblock.bottom_up);
	assert(memblock.current_limit == MEMBLOCK_ALLOC_ANYWHERE);

	return 0;
}

/*
 * A simple test that adds a memory block of a specified base address
 * and size to the collection of available memory regions (memblock.memory).
 * It checks if a new entry was created and if region counter and total memory
 * were correctly updated.
 */
static int memblock_add_simple_check(void)
{
	struct memblock_region *rgn;

	rgn = &memblock.memory.regions[0];

	struct region r = {
		.base = SZ_1G,
		.size = SZ_4M
	};

	reset_memblock();
	memblock_add(r.base, r.size);

	assert(rgn->base == r.base);
	assert(rgn->size == r.size);

	assert(memblock.memory.cnt == 1);
	assert(memblock.memory.total_size == r.size);

	return 0;
}

/*
 * A test that tries to add two memory blocks that don't overlap with one
 * another. It checks if two correctly initialized entries were added to the
 * collection of available memory regions (memblock.memory) and if this
 * change was reflected in memblock.memory's total size and region counter.
 */
static int memblock_add_disjoint_check(void)
{
	struct memblock_region *rgn1, *rgn2;

	rgn1 = &memblock.memory.regions[0];
	rgn2 = &memblock.memory.regions[1];

	struct region r1 = {
		.base = SZ_1G,
		.size = SZ_8K
	};
	struct region r2 = {
		.base = SZ_1G + SZ_16K,
		.size = SZ_8K
	};

	reset_memblock();
	memblock_add(r1.base, r1.size);
	memblock_add(r2.base, r2.size);

	assert(rgn1->base == r1.base);
	assert(rgn1->size == r1.size);

	assert(rgn2->base == r2.base);
	assert(rgn2->size == r2.size);

	assert(memblock.memory.cnt == 2);
	assert(memblock.memory.total_size == r1.size + r2.size);

	return 0;
}

/*
 * A test that tries to add two memory blocks, where the second one overlaps
 * with the beginning of the first entry (that is r1.base < r2.base + r2.size).
 * After this, it checks if two entries are merged into one region that starts
 * at r2.base and has size of two regions minus their intersection. It also
 * verifies the reported total size of the available memory and region counter.
 */
static int memblock_add_overlap_top_check(void)
{
	struct memblock_region *rgn;
	phys_addr_t total_size;

	rgn = &memblock.memory.regions[0];

	struct region r1 = {
		.base = SZ_512M,
		.size = SZ_1G
	};
	struct region r2 = {
		.base = SZ_256M,
		.size = SZ_512M
	};

	total_size = (r1.base - r2.base) + r1.size;

	reset_memblock();
	memblock_add(r1.base, r1.size);
	memblock_add(r2.base, r2.size);

	assert(rgn->base == r2.base);
	assert(rgn->size == total_size);

	assert(memblock.memory.cnt == 1);
	assert(memblock.memory.total_size == total_size);

	return 0;
}

/*
 * A test that tries to add two memory blocks, where the second one overlaps
 * with the end of the first entry (that is r2.base < r1.base + r1.size).
 * After this, it checks if two entries are merged into one region that starts
 * at r1.base and has size of two regions minus their intersection. It verifies
 * that memblock can still see only one entry and has a correct total size of
 * the available memory.
 */
static int memblock_add_overlap_bottom_check(void)
{
	struct memblock_region *rgn;
	phys_addr_t total_size;

	rgn = &memblock.memory.regions[0];

	struct region r1 = {
		.base = SZ_128M,
		.size = SZ_512M
	};
	struct region r2 = {
		.base = SZ_256M,
		.size = SZ_1G
	};

	total_size = (r2.base - r1.base) + r2.size;

	reset_memblock();
	memblock_add(r1.base, r1.size);
	memblock_add(r2.base, r2.size);

	assert(rgn->base == r1.base);
	assert(rgn->size == total_size);

	assert(memblock.memory.cnt == 1);
	assert(memblock.memory.total_size == total_size);

	return 0;
}

/*
 * A test that tries to add two memory blocks, where the second one is
 * within the range of the first entry (that is r1.base < r2.base &&
 * r2.base + r2.size < r1.base + r1.size). It checks if two entries are merged
 * into one region that stays the same. The counter and total size of available
 * memory are expected to not be updated.
 */
static int memblock_add_within_check(void)
{
	struct memblock_region *rgn;

	rgn = &memblock.memory.regions[0];

	struct region r1 = {
		.base = SZ_8M,
		.size = SZ_32M
	};
	struct region r2 = {
		.base = SZ_16M,
		.size = SZ_1M
	};

	reset_memblock();
	memblock_add(r1.base, r1.size);
	memblock_add(r2.base, r2.size);

	assert(rgn->base == r1.base);
	assert(rgn->size == r1.size);

	assert(memblock.memory.cnt == 1);
	assert(memblock.memory.total_size == r1.size);

	return 0;
}

/*
 * A simple test that tries to add the same memory block twice. The counter
 * and total size of available memory are expected to not be updated.
 */
static int memblock_add_twice_check(void)
{
	struct region r = {
		.base = SZ_16K,
		.size = SZ_2M
	};

	reset_memblock();

	memblock_add(r.base, r.size);
	memblock_add(r.base, r.size);

	assert(memblock.memory.cnt == 1);
	assert(memblock.memory.total_size == r.size);

	return 0;
}

static int memblock_add_checks(void)
{
	memblock_add_simple_check();
	memblock_add_disjoint_check();
	memblock_add_overlap_top_check();
	memblock_add_overlap_bottom_check();
	memblock_add_within_check();
	memblock_add_twice_check();

	return 0;
}

 /*
  * A simple test that marks a memory block of a specified base address
  * and size as reserved and to the collection of reserved memory regions
  * (memblock.reserved). It checks if a new entry was created and if region
  * counter and total memory size were correctly updated.
  */
static int memblock_reserve_simple_check(void)
{
	struct memblock_region *rgn;

	rgn =  &memblock.reserved.regions[0];

	struct region r = {
		.base = SZ_2G,
		.size = SZ_128M
	};

	reset_memblock();
	memblock_reserve(r.base, r.size);

	assert(rgn->base == r.base);
	assert(rgn->size == r.size);

	return 0;
}

/*
 * A test that tries to mark two memory blocks that don't overlap as reserved
 * and checks if two entries were correctly added to the collection of reserved
 * memory regions (memblock.reserved) and if this change was reflected in
 * memblock.reserved's total size and region counter.
 */
static int memblock_reserve_disjoint_check(void)
{
	struct memblock_region *rgn1, *rgn2;

	rgn1 = &memblock.reserved.regions[0];
	rgn2 = &memblock.reserved.regions[1];

	struct region r1 = {
		.base = SZ_256M,
		.size = SZ_16M
	};
	struct region r2 = {
		.base = SZ_512M,
		.size = SZ_512M
	};

	reset_memblock();
	memblock_reserve(r1.base, r1.size);
	memblock_reserve(r2.base, r2.size);

	assert(rgn1->base == r1.base);
	assert(rgn1->size == r1.size);

	assert(rgn2->base == r2.base);
	assert(rgn2->size == r2.size);

	assert(memblock.reserved.cnt == 2);
	assert(memblock.reserved.total_size == r1.size + r2.size);

	return 0;
}

/*
 * A test that tries to mark two memory blocks as reserved, where the
 * second one overlaps with the beginning of the first (that is
 * r1.base < r2.base + r2.size).
 * It checks if two entries are merged into one region that starts at r2.base
 * and has size of two regions minus their intersection. The test also verifies
 * that memblock can still see only one entry and has a correct total size of
 * the reserved memory.
 */
static int memblock_reserve_overlap_top_check(void)
{
	struct memblock_region *rgn;
	phys_addr_t total_size;

	rgn = &memblock.reserved.regions[0];

	struct region r1 = {
		.base = SZ_1G,
		.size = SZ_1G
	};
	struct region r2 = {
		.base = SZ_128M,
		.size = SZ_1G
	};

	total_size = (r1.base - r2.base) + r1.size;

	reset_memblock();
	memblock_reserve(r1.base, r1.size);
	memblock_reserve(r2.base, r2.size);

	assert(rgn->base == r2.base);
	assert(rgn->size == total_size);

	assert(memblock.reserved.cnt == 1);
	assert(memblock.reserved.total_size == total_size);

	return 0;
}

/*
 * A test that tries to mark two memory blocks as reserved, where the
 * second one overlaps with the end of the first entry (that is
 * r2.base < r1.base + r1.size).
 * It checks if two entries are merged into one region that starts at r1.base
 * and has size of two regions minus their intersection. It verifies that
 * memblock can still see only one entry and has a correct total size of the
 * reserved memory.
 */
static int memblock_reserve_overlap_bottom_check(void)
{
	struct memblock_region *rgn;
	phys_addr_t total_size;

	rgn = &memblock.reserved.regions[0];

	struct region r1 = {
		.base = SZ_2K,
		.size = SZ_128K
	};
	struct region r2 = {
		.base = SZ_128K,
		.size = SZ_128K
	};

	total_size = (r2.base - r1.base) + r2.size;

	reset_memblock();
	memblock_reserve(r1.base, r1.size);
	memblock_reserve(r2.base, r2.size);

	assert(rgn->base == r1.base);
	assert(rgn->size == total_size);

	assert(memblock.reserved.cnt == 1);
	assert(memblock.reserved.total_size == total_size);

	return 0;
}

/*
 * A test that tries to mark two memory blocks as reserved, where the second
 * one is within the range of the first entry (that is
 * (r1.base < r2.base) && (r2.base + r2.size < r1.base + r1.size)).
 * It checks if two entries are merged into one region that stays the
 * same. The counter and total size of available memory are expected to not be
 * updated.
 */
static int memblock_reserve_within_check(void)
{
	struct memblock_region *rgn;

	rgn = &memblock.reserved.regions[0];

	struct region r1 = {
		.base = SZ_1M,
		.size = SZ_8M
	};
	struct region r2 = {
		.base = SZ_2M,
		.size = SZ_64K
	};

	reset_memblock();
	memblock_reserve(r1.base, r1.size);
	memblock_reserve(r2.base, r2.size);

	assert(rgn->base == r1.base);
	assert(rgn->size == r1.size);

	assert(memblock.reserved.cnt == 1);
	assert(memblock.reserved.total_size == r1.size);

	return 0;
}

/*
 * A simple test that tries to reserve the same memory block twice.
 * The region counter and total size of reserved memory are expected to not
 * be updated.
 */
static int memblock_reserve_twice_check(void)
{
	struct region r = {
		.base = SZ_16K,
		.size = SZ_2M
	};

	reset_memblock();

	memblock_reserve(r.base, r.size);
	memblock_reserve(r.base, r.size);

	assert(memblock.reserved.cnt == 1);
	assert(memblock.reserved.total_size == r.size);

	return 0;
}

static int memblock_reserve_checks(void)
{
	memblock_reserve_simple_check();
	memblock_reserve_disjoint_check();
	memblock_reserve_overlap_top_check();
	memblock_reserve_overlap_bottom_check();
	memblock_reserve_within_check();
	memblock_reserve_twice_check();

	return 0;
}

int memblock_basic_checks(void)
{
	memblock_initialization_check();
	memblock_add_checks();
	memblock_reserve_checks();

	return 0;
}