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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
|
// 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;
}
/*
* A simple test that tries to remove the first entry of the array of
* available memory regions. By "removing" a region we mean overwriting it
* with the next region in memblock.memory. To check this is the case, the
* test adds two memory blocks and verifies that the value of the latter
* was used to erase r1 region. It also checks if the region counter and
* total size were updated to expected values.
*/
static int memblock_remove_simple_check(void)
{
struct memblock_region *rgn;
rgn = &memblock.memory.regions[0];
struct region r1 = {
.base = SZ_2K,
.size = SZ_4K
};
struct region r2 = {
.base = SZ_128K,
.size = SZ_4M
};
reset_memblock();
memblock_add(r1.base, r1.size);
memblock_add(r2.base, r2.size);
memblock_remove(r1.base, r1.size);
assert(rgn->base == r2.base);
assert(rgn->size == r2.size);
assert(memblock.memory.cnt == 1);
assert(memblock.memory.total_size == r2.size);
return 0;
}
/*
* A test that tries to remove a region that was not registered as available
* memory (i.e. has no corresponding entry in memblock.memory). It verifies
* that array, regions counter and total size were not modified.
*/
static int memblock_remove_absent_check(void)
{
struct memblock_region *rgn;
rgn = &memblock.memory.regions[0];
struct region r1 = {
.base = SZ_512K,
.size = SZ_4M
};
struct region r2 = {
.base = SZ_64M,
.size = SZ_1G
};
reset_memblock();
memblock_add(r1.base, r1.size);
memblock_remove(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 test that tries to remove a region which overlaps with the beginning of
* the already existing entry r1 (that is r1.base < r2.base + r2.size). It
* checks if only the intersection of both regions is removed from the available
* memory pool. The test also checks if the regions counter and total size are
* updated to expected values.
*/
static int memblock_remove_overlap_top_check(void)
{
struct memblock_region *rgn;
phys_addr_t r1_end, r2_end, total_size;
rgn = &memblock.memory.regions[0];
struct region r1 = {
.base = SZ_32M,
.size = SZ_32M
};
struct region r2 = {
.base = SZ_16M,
.size = SZ_32M
};
r1_end = r1.base + r1.size;
r2_end = r2.base + r2.size;
total_size = r1_end - r2_end;
reset_memblock();
memblock_add(r1.base, r1.size);
memblock_remove(r2.base, r2.size);
assert(rgn->base == r1.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 remove a region which overlaps with the end of the
* first entry (that is r2.base < r1.base + r1.size). It checks if only the
* intersection of both regions is removed from the available memory pool.
* The test also checks if the regions counter and total size are updated to
* expected values.
*/
static int memblock_remove_overlap_bottom_check(void)
{
struct memblock_region *rgn;
phys_addr_t total_size;
rgn = &memblock.memory.regions[0];
struct region r1 = {
.base = SZ_2M,
.size = SZ_64M
};
struct region r2 = {
.base = SZ_32M,
.size = SZ_256M
};
total_size = r2.base - r1.base;
reset_memblock();
memblock_add(r1.base, r1.size);
memblock_remove(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 remove a region which is within the range of the
* already existing entry (that is
* (r1.base < r2.base) && (r2.base + r2.size < r1.base + r1.size)).
* It checks if the region is split into two - one that ends at r2.base and
* second that starts at r2.base + size, with appropriate sizes. The test
* also checks if the region counter and total size were updated to
* expected values.
*/
static int memblock_remove_within_check(void)
{
struct memblock_region *rgn1, *rgn2;
phys_addr_t r1_size, r2_size, total_size;
rgn1 = &memblock.memory.regions[0];
rgn2 = &memblock.memory.regions[1];
struct region r1 = {
.base = SZ_1M,
.size = SZ_32M
};
struct region r2 = {
.base = SZ_16M,
.size = SZ_1M
};
r1_size = r2.base - r1.base;
r2_size = (r1.base + r1.size) - (r2.base + r2.size);
total_size = r1_size + r2_size;
reset_memblock();
memblock_add(r1.base, r1.size);
memblock_remove(r2.base, r2.size);
assert(rgn1->base == r1.base);
assert(rgn1->size == r1_size);
assert(rgn2->base == r2.base + r2.size);
assert(rgn2->size == r2_size);
assert(memblock.memory.cnt == 2);
assert(memblock.memory.total_size == total_size);
return 0;
}
static int memblock_remove_checks(void)
{
memblock_remove_simple_check();
memblock_remove_absent_check();
memblock_remove_overlap_top_check();
memblock_remove_overlap_bottom_check();
memblock_remove_within_check();
return 0;
}
int memblock_basic_checks(void)
{
memblock_initialization_check();
memblock_add_checks();
memblock_reserve_checks();
memblock_remove_checks();
return 0;
}
|