Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
.\"	$NetBSD: uvm_hotplug.9,v 1.6 2020/01/17 12:34:55 skrll Exp $
.\"
.\" Copyright (c) 2016 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Cherry G Mathew and Santhosh N Raju.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd January 17, 2020
.Dt UVM_HOTPLUG 9
.Os
.Sh NAME
.Nm uvm_physseg_init ,
.Nm uvm_physseg_valid_p ,
.Nm uvm_physseg_get_start ,
.Nm uvm_physseg_get_end ,
.Nm uvm_physseg_get_avail_start ,
.Nm uvm_physseg_get_avail_end ,
.Nm uvm_physseg_get_pg ,
.Nm uvm_physseg_get_pmseg ,
.Nm uvm_physseg_get_free_list ,
.Nm uvm_physseg_get_start_hint ,
.Nm uvm_physseg_set_start_hint ,
.Nm uvm_physseg_get_next ,
.Nm uvm_physseg_get_prev ,
.Nm uvm_physseg_get_first ,
.Nm uvm_physseg_get_last ,
.Nm uvm_physseg_get_highest_frame ,
.Nm uvm_physseg_find ,
.Nm uvm_page_physload ,
.Nm uvm_page_physunload ,
.Nm uvm_page_physunload_force ,
.Nm uvm_physseg_plug ,
.Nm uvm_physseg_unplug ,
.Nm uvm_physseg_set_avail_start ,
.Nm uvm_physseg_set_avail_end
.Nd memory hotplug manager
.Sh SYNOPSIS
.In uvm/uvm_physseg.h
.Ft void
.Fn uvm_physseg_init "void"
.Ft uvm_physseg_t
.Fn uvm_page_physload "paddr_t start" "paddr_t end" "paddr_t avail_start" \
"paddr_t avail_end" "int free_list"
.Ft bool
.Fn uvm_page_physunload "uvm_physseg_t upm" "int free_list" \
"paddr_t *paddrp"
.Ft bool
.Fn uvm_page_physunload_force "uvm_physseg_t upm" "int free_list" \
"paddr_t *paddrp"
.Ft bool
.Fn uvm_physseg_plug "paddr_t pfn" "size_t npages" "uvm_physseg_t *upmp"
.Ft bool
.Fn uvm_physseg_unplug "paddr_t pfn" "size_t npages"
.Sh DESCRIPTION
These utility routines provide the ability to tell
.Xr uvm 9
about system memory segments.
When the kernel is compiled with
.Cd 'options UVM_HOTPLUG' ,
memory segments are handled in a dynamic data structure
.Pq Xr rbtree 3
compared to a static array when not.
This enables kernel code to add
or remove information about memory segments at any point after boot -
thus "hotplug".
.Pp
.Fn uvm_page_physload ,
.Fn uvm_page_physunload ,
and
.Fn uvm_page_physunload_force
are legacy interfaces which may be removed in the future.
They must
never be used after
.Xr uvm_init 9 .
.Pp
.Sy WARNING :
This is an experimental feature and should not be used in production
environments.
Furthermore, attempting to "hotplug" without
.Cd 'options UVM_HOTPLUG'
after boot will almost certainly end in a
.Xr panic 9 .
.Sh USAGE
.Ss INITIALIZING HOTPLUG
The function
.Fn uvm_physseg_init
initializes the hotplug subsystem.
This is expected to happen exactly
once, at boot time, and from MD code.
.Ss PLUGGING IN MEMORY
.Fn uvm_page_physload
registers
.Xr uvm 9
with a memory segment span, and on a specified
.Fa free_list .
It must be called at system boot time as part of setting up memory
management.
The arguments describe the start and end of the physical addresses of the
segment, and the available start and end addresses of pages not already in use.
If a system has memory banks of different speeds the slower memory should be
given a higher
.Fa free_list
value.
.Bl -tag -offset indent -width "avail_start"
.It Fa start
Starting page frame number of the physical memory segments.
.It Fa end
Ending page frame number of the physical memory segments.
.It Fa avail_start
Available starting page frame number of the physical memory segments.
.It Fa avail_end
Available ending page frame number of the physical memory segments.
.It Fa free_list
The free list types are defined in the Machine Dependent code.
.El
.Pp
This function returns a valid
.Dv uvm_physseg_t
handle when a successful plug occurs, else it will return
.Dv UVM_PHYSSEG_TYPE_INVALID
when the plug fails.
.Pp
.Fn uvm_physseg_plug
registers
.Xr uvm 9
with a memory segment span.
It can also be called to initiate a hotplug and register a newly
"hotplugged" physical memory range into the VM.
Unlike
.Fn uvm_page_physload
this function can, if
.Cd 'options UVM_HOTPLUG'
is enabled at compile time, be used after
.Xr uvm_init 9 .
The arguments describe the start page frame, the number of pages to
plug starting from the start page frame and an optional return variable, which
points to a valid
.Fa uvm_physseg_t
handle when a successful plug occurs.
.Bl -tag -offset indent -width "npages"
.It Fa pfn
Starting page frame number of the physical memory segment.
.It Fa npages
Total number of pages from the starting page frame number to plug in.
.It Fa upmp
If upmp is not
.Dv NULL ,
then on a successful plug, a valid pointer to the uvm_physseg_t handle
for the segment which was plugged is returned.
.El
.Pp
This function returns
.Fa true
when a successful plug occurs,
.Fa false
otherwise.
.Ss UNPLUGGING MEMORY
The functions
.Fn uvm_page_physunload ,
.Fn uvm_page_physunload_force ,
and
.Fn uvm_physseg_unplug
make
.Xr uvm 9
forget about previously registered memory segments or portions of
such.
.Pp
.Fn uvm_page_physunload
unloads pages from a segment (from the front or from the back)
depending on its availability.
When the last page is removed, the
segment handle is invalidated and supporting metadata is freed.
.Pp
Note: This function can only be used during boot time.
Pages, once unloaded, are unregistered from uvm and are therefore
assumed to be managed by the code which called
.Fn uvm_page_physunload 9
(usually boot time MD code, for boottime memory "allocation").
.Pp
The arguments are:
.Bl -tag -offset indent -width "free_list"
.It Fa upm
The handle identifying segment from which we are trying to unload memory.
.It Fa free_list
The free list types are defined in the Machine Dependent code.
.It Fa paddrp
The pointer to the physical address that was unloaded.
.El
.Pp
If the unload was successful,
.Fa true
is returned,
.Fa false
otherwise.
.Pp
.Fn uvm_page_physunload_force
unconditionally unloads pages from a segment.
When the last page is removed, the segment handle
is invalidated and supporting metadata is freed.
.Pp
Note: This function can only be used during boot time.
Pages, once unloaded, are unregistered from uvm and are therefore
assumed to be managed by the code which called
.Fn uvm_page_physunload_force 9
(usually boot time MD code, for boottime memory "allocation").
.Pp
The arguments are:
.Bl -tag -offset indent -width "free_list"
.It Fa upm
The handle identifying segment from which we are trying to unload memory.
.It Fa free_list
The free list types are defined in the Machine Dependent code.
.It Fa paddrp
The pointer to the physical address that was unloaded.
.El
.Pp
If the unload was successful
.Fa true
is returned,
.Fa false
otherwise.
.Pp
.Fn uvm_physseg_unplug
can be called to unplug an existing physical memory segment.
Unlike
.Fn uvm_page_physunload
and
.Fn uvm_page_physunload_force ,
it can be called after
.Xr uvm_init 9 ,
if
.Cd 'options UVM_HOTPLUG'
is enabled at compile time.
.Fn uvm_hotplug 9
makes no effort to manage the state of the underlying physical
memory.
It is up to the caller to ensure that it is not in use,
either by
.Xr uvm 9 ,
or by any other sub-system.
Further, any hardware
quiescing that may be required is the responsibility of MD code.
The arguments
describe the start page frame and the number of pages to unplug.
The arguments are:
.Bl -tag -offset indent -width "npages"
.It Fa pfn
Starting page frame number of the physical memory segment.
.It Fa npages
Total number of pages from the starting page frame number to unplug.
.El
.Pp
Returns
.Fa true
or
.Fa false
depending on success or failure respectively.
.Sh UTILITY FUNCTIONS
.Bl -ohang
.It Ft bool
.Fn uvm_physseg_valid_p "uvm_physseg_t upm"
.It Ft paddr_t
.Fn uvm_physseg_get_start "uvm_physseg_t upm"
.It Ft paddr_t
.Fn uvm_physseg_get_end "uvm_physseg_t upm"
.It Ft paddr_t
.Fn uvm_physseg_get_avail_start "uvm_physseg_t upm"
.It Ft paddr_t
.Fn uvm_physseg_get_avail_end "uvm_physseg_t upm"
.It Ft struct vm_page *
.Fn uvm_physseg_get_pg "uvm_physseg_t upm" "paddr_t index"
.It Ft struct pmap_physseg *
.Fn uvm_physseg_get_pmesg "uvm_physseg_t upm"
.It Ft int
.Fn uvm_physseg_get_free_list "uvm_physseg_t upm"
.It Ft u_int
.Fn uvm_physseg_get_start_hint "uvm_physseg_t upm"
.It Ft bool
.Fn uvm_physseg_set_start_hint "uvm_physseg_t upm" "u_int start_hint"
.It Ft uvm_physseg_t
.Fn uvm_physseg_get_next "uvm_physseg_t upm"
.It Ft uvm_physseg_t
.Fn uvm_physseg_get_prev "uvm_physseg_t upm"
.It Ft uvm_physseg_t
.Fn uvm_physseg_get_first "void"
.It Ft uvm_physseg_t
.Fn uvm_physseg_get_last "void"
.It Ft paddr_t
.Fn uvm_physseg_get_highest_frame "void"
.It Ft paddr_t
.Fn uvm_physseg_find "paddr pframe" "psize_t *offsetp"
.It Ft void
.Fn uvm_physseg_set_avail_start "uvm_physseg_t upm" "paddr_t avail_start"
.It Ft void
.Fn uvm_physseg_set_avail_end "uvm_physseg_t upm" "paddr_t avail_end"
.El
.Pp
.Fn uvm_physseg_valid_p
validates a handle that is passed in, returns
.Fa true
if the given handle is valid,
.Fa false
otherwise.
.Pp
.Fn uvm_physseg_get_start
if a valid
.Fa uvm_physseg_t
handle is passed in, it returns the starting physical address of
the segment.
The returned value is of type
.Ft paddr_t .
In case the handle is invalid the returned value will match
.Ft ( paddr_t )
\-1.
.Pp
.Fn uvm_physseg_get_end
if a valid
.Fa uvm_physseg_t
handle is passed in, it returns the ending physical address of the
segment.
The returned value is of type
.Ft paddr_t .
In case the handle is invalid the returned value will match
.Ft ( paddr_t )
\-1.
.Pp
.Fn uvm_physseg_get_avail_start
if a valid
.Fa uvm_physseg_t
handle is passed in, it returns the available starting physical
address of the segment.
The returned value is of type
.Ft paddr_t .
In case the handle is invalid the returned value will match
.Ft ( paddr_t )
\-1.
.Pp
.Fn uvm_physseg_get_avail_end
if a valid
.Fa uvm_physseg_t
handle is passed in, it returns the available ending physical
address of the segment.
The returned value is of type
.Ft paddr_t .
In case the handle is invalid the returned value will match
.Ft ( paddr_t )
\-1.
.Pp
.Fn uvm_physseg_get_pg
if a valid
.Fa uvm_physseg_t
handle along with an index value is passed in, it returns the
.Fa struct vm_page *
object contained in that location.
.Pp
.Fn uvm_physseg_get_pmseg
if a valid
.Fa uvm_physseg_t
handle is passed in, it returns the
.Fa struct pmap_physseg *
object contained in the handle.
.Pp
.Fn uvm_physseg_get_free_list
if a valid
.Fa uvm_physseg_t
handle is passed in, it returns the
.Fa free_list
type for which the current segment is associated with.
The returned value is of
type
.Fa int .
.Pp
.Fn uvm_physseg_get_start_hint
if a valid
.Fa uvm_physseg_t
handle is passed in, it returns the
.Fa start_hint
type for the current segment.
The returned value is of type
.Fa u_int .
.Pp
.Fn uvm_physseg_set_start_hint
if a valid handle along with the
.Fa start_hint
is passed in, the value is set in the segment.
And a
.Fa true
is returned to indicate a successful value setting.
In case the handle is invalid a
.Fa false
is returned.
.Pp
.Fn uvm_physseg_get_next
if a valid handle is passed in, it returns the next valid
.Fa uvm_physseg_t
handle in the sequence.
However if the handle passed is the last segment in the
sequence the function returns
.Fa UVM_PHYSSEG_TYPE_INVALID_OVERFLOW .
Passing an invalid handle is not fatal, and returns
.Fa UVM_PHYSSEG_TYPE_INVALID .
.
.Pp
.Fn uvm_physseg_get_prev
if a valid handle is passed in, it returns the previous validh
.Fa uvm_physseg_t
handle in the sequence.
However if the handle passed is the first segment in
the sequence the function returns
.Fa UVM_PHYSSEG_TYPE_INVALID_EMPTY .
Passing an invalid handle is not fatal, and returns
.Fa UVM_PHYSSEG_TYPE_INVALID .
.
.Pp
.Fn uvm_physseg_get_first
returns the first valid
.Fa uvm_physseg_t
handle in the sequence.
However if there are no valid handles in the sequence
yet, the function returns
.Fa UVM_PHYSSEG_TYPE_INVALID_EMPTY .
.Pp
.Fn uvm_physseg_get_last
returns the last valid
.Fa uvm_physseg_t
handle in the sequence.
However if there are no valid handles in the sequence
yet, the function returns
.Fa UVM_PHYSSEG_TYPE_INVALID_EMPTY .
.Pp
.Fn uvm_physseg_get_highest_frame
returns the frame number of the highest registered physical page frame
which is of type
.Ft paddr_t .
XXX: Searching on empty sequences are not yet processed in the function.
.Pp
.Fn uvm_physseg_find
searches for a given segment containing the page frame
.Ft ( paddr_t )
passed in.
If a segment that falls between starting and ending addresses is
found, the corresponding
.Fa uvm_physseg_t
handle is returned else a
.Fa UVM_PHYSSEG_TYPE_INVALID
is returned.
The second parameter, if not set to
.Dv NULL ,
the offset value of the page frame passed in with respect to the
starting address is set to the appropriate
.Fa psize_t
value if the search was successful in finding the segment.
.Pp
.Fn uvm_physseg_set_avail_start
if a valid
.Fa uvm_physseg_t
handle is passed in along with the available starting physical address of the
segment of type
.Ft paddr_t ,
the value is set in the segment.
.Pp
.Fn uvm_physseg_set_avail_end
if a valid
.Fa uvm_physseg_t
handle is passed in along with the available ending physical address of the
segment of type
.Ft paddr_t ,
the value is set in the segment.
.Sh NOTES
.Fn uvm_physseg_plug
and
.Fn uvm_physseg_unplug
must never be used after
.Xr uvm_init 9
in a kernel build where
.Cd 'options UVM_HOTPLUG'
is not enabled.
.Sh DIAGNOSTICS
Tests for
.Nm
are in
.Pa tests/sys/uvm .
.Pp
Unit / functional tests are in
.Pa tests/sys/uvm/t_uvm_physseg.c .
These tests focus on the expected working of the
.Nm
API and its utility functions.
.Pp
Load tests can be found in
.Pa tests/sys/uvm/t_uvm_physseg_load.c .
These tests focus on stressing the
.Nm
implementation in order to make performance comparisons between kernel
builds with and without
.Cd 'options UVM_HOTPLUG'
.
.\" .Sh RETURN VALUES
.\" .Sh EXAMPLES
.Sh CODE REFERENCES
The uvm hotplug feature is implemented in the file
.Pa sys/uvm/uvm_physseg.c .
The uvm hotplug API is exported via
.Pa sys/uvm/uvm_physseg.h .
.Sh SEE ALSO
.Xr extent 9 ,
.Xr free 9 ,
.Xr malloc 9 ,
.Xr memoryallocators 9 ,
.Xr uvm 9
.Sh HISTORY
This API emerged out of the need to insert new pages at runtime in the
Xen
.Xr x86/balloon 4
driver.
.Sh AUTHORS
.An -nosplit
.An Cherry G. Mathew
.Aq Mt cherry@NetBSD.org
designed and integrated the API.
.Pp
.An Santhosh N. Raju
.Aq Mt santhosh.raju@gmail.com
implemented the dynamic segment handling code and all tests for this API.
.Pp
.An Nick Hudson
.Aq Mt skrll@NetBSD.org
contributed bugfixes and testing on a wide range of hardware ports.