2020-02-23 18:46:38 +03:00
|
|
|
/* $NetBSD: uvm_km.c,v 1.155 2020/02/23 15:46:43 ad Exp $ */
|
1998-02-05 09:25:08 +03:00
|
|
|
|
2001-05-25 08:06:11 +04:00
|
|
|
/*
|
1998-02-05 09:25:08 +03:00
|
|
|
* Copyright (c) 1997 Charles D. Cranor and Washington University.
|
2001-05-25 08:06:11 +04:00
|
|
|
* Copyright (c) 1991, 1993, The Regents of the University of California.
|
1998-02-05 09:25:08 +03:00
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* The Mach Operating System project at Carnegie-Mellon University.
|
|
|
|
*
|
|
|
|
* 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.
|
2011-02-02 18:25:27 +03:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1998-02-05 09:25:08 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
|
|
|
|
*
|
|
|
|
* @(#)vm_kern.c 8.3 (Berkeley) 1/12/94
|
1998-02-07 14:07:38 +03:00
|
|
|
* from: Id: uvm_km.c,v 1.1.2.14 1998/02/06 05:19:27 chs Exp
|
1998-02-05 09:25:08 +03:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright (c) 1987, 1990 Carnegie-Mellon University.
|
|
|
|
* All rights reserved.
|
2001-05-25 08:06:11 +04:00
|
|
|
*
|
1998-02-05 09:25:08 +03:00
|
|
|
* Permission to use, copy, modify and distribute this software and
|
|
|
|
* its documentation is hereby granted, provided that both the copyright
|
|
|
|
* notice and this permission notice appear in all copies of the
|
|
|
|
* software, derivative works or modified versions, and any portions
|
|
|
|
* thereof, and that both notices appear in supporting documentation.
|
2001-05-25 08:06:11 +04:00
|
|
|
*
|
|
|
|
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
|
|
|
|
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
|
1998-02-05 09:25:08 +03:00
|
|
|
* FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
|
2001-05-25 08:06:11 +04:00
|
|
|
*
|
1998-02-05 09:25:08 +03:00
|
|
|
* Carnegie Mellon requests users of this software to return to
|
|
|
|
*
|
|
|
|
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
|
|
|
|
* School of Computer Science
|
|
|
|
* Carnegie Mellon University
|
|
|
|
* Pittsburgh PA 15213-3890
|
|
|
|
*
|
|
|
|
* any improvements or extensions that they make and grant Carnegie the
|
|
|
|
* rights to redistribute these changes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* uvm_km.c: handle kernel memory allocation and management
|
|
|
|
*/
|
|
|
|
|
1998-02-24 18:58:09 +03:00
|
|
|
/*
|
|
|
|
* overview of kernel memory management:
|
|
|
|
*
|
|
|
|
* the kernel virtual address space is mapped by "kernel_map." kernel_map
|
2003-05-11 01:10:23 +04:00
|
|
|
* starts at VM_MIN_KERNEL_ADDRESS and goes to VM_MAX_KERNEL_ADDRESS.
|
|
|
|
* note that VM_MIN_KERNEL_ADDRESS is equal to vm_map_min(kernel_map).
|
1998-02-24 18:58:09 +03:00
|
|
|
*
|
2001-05-25 08:06:11 +04:00
|
|
|
* the kernel_map has several "submaps." submaps can only appear in
|
1998-02-24 18:58:09 +03:00
|
|
|
* the kernel_map (user processes can't use them). submaps "take over"
|
|
|
|
* the management of a sub-range of the kernel's address space. submaps
|
|
|
|
* are typically allocated at boot time and are never released. kernel
|
2001-05-25 08:06:11 +04:00
|
|
|
* virtual address space that is mapped by a submap is locked by the
|
1998-02-24 18:58:09 +03:00
|
|
|
* submap's lock -- not the kernel_map's lock.
|
|
|
|
*
|
|
|
|
* thus, the useful feature of submaps is that they allow us to break
|
|
|
|
* up the locking and protection of the kernel address space into smaller
|
|
|
|
* chunks.
|
|
|
|
*
|
2012-06-02 12:42:37 +04:00
|
|
|
* the vm system has several standard kernel submaps/arenas, including:
|
|
|
|
* kmem_arena => used for kmem/pool (memoryallocators(9))
|
1998-02-24 18:58:09 +03:00
|
|
|
* pager_map => used to map "buf" structures into kernel space
|
|
|
|
* exec_map => used during exec to handle exec args
|
|
|
|
* etc...
|
|
|
|
*
|
2012-06-03 21:12:49 +04:00
|
|
|
* The kmem_arena is a "special submap", as it lives in a fixed map entry
|
|
|
|
* within the kernel_map and is controlled by vmem(9).
|
2012-06-02 12:42:37 +04:00
|
|
|
*
|
1998-02-24 18:58:09 +03:00
|
|
|
* the kernel allocates its private memory out of special uvm_objects whose
|
|
|
|
* reference count is set to UVM_OBJ_KERN (thus indicating that the objects
|
|
|
|
* are "special" and never die). all kernel objects should be thought of
|
2001-05-25 08:06:11 +04:00
|
|
|
* as large, fixed-sized, sparsely populated uvm_objects. each kernel
|
2003-05-11 01:10:23 +04:00
|
|
|
* object is equal to the size of kernel virtual address space (i.e. the
|
|
|
|
* value "VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS").
|
1998-02-24 18:58:09 +03:00
|
|
|
*
|
2008-08-04 17:37:33 +04:00
|
|
|
* note that just because a kernel object spans the entire kernel virtual
|
1998-02-24 18:58:09 +03:00
|
|
|
* address space doesn't mean that it has to be mapped into the entire space.
|
2001-05-25 08:06:11 +04:00
|
|
|
* large chunks of a kernel object's space go unused either because
|
|
|
|
* that area of kernel VM is unmapped, or there is some other type of
|
1998-02-24 18:58:09 +03:00
|
|
|
* object mapped into that range (e.g. a vnode). for submap's kernel
|
|
|
|
* objects, the only part of the object that can ever be populated is the
|
|
|
|
* offsets that are managed by the submap.
|
|
|
|
*
|
|
|
|
* note that the "offset" in a kernel object is always the kernel virtual
|
2003-05-11 01:10:23 +04:00
|
|
|
* address minus the VM_MIN_KERNEL_ADDRESS (aka vm_map_min(kernel_map)).
|
1998-02-24 18:58:09 +03:00
|
|
|
* example:
|
2003-05-11 01:10:23 +04:00
|
|
|
* suppose VM_MIN_KERNEL_ADDRESS is 0xf8000000 and the kernel does a
|
1998-02-24 18:58:09 +03:00
|
|
|
* uvm_km_alloc(kernel_map, PAGE_SIZE) [allocate 1 wired down page in the
|
|
|
|
* kernel map]. if uvm_km_alloc returns virtual address 0xf8235000,
|
|
|
|
* then that means that the page at offset 0x235000 in kernel_object is
|
2001-05-25 08:06:11 +04:00
|
|
|
* mapped at 0xf8235000.
|
1998-02-24 18:58:09 +03:00
|
|
|
*
|
|
|
|
* kernel object have one other special property: when the kernel virtual
|
|
|
|
* memory mapping them is unmapped, the backing memory in the object is
|
|
|
|
* freed right away. this is done with the uvm_km_pgremove() function.
|
|
|
|
* this has to be done because there is no backing store for kernel pages
|
|
|
|
* and no need to save them after they are no longer referenced.
|
2012-06-02 12:42:37 +04:00
|
|
|
*
|
2012-06-03 21:12:49 +04:00
|
|
|
* Generic arenas:
|
2012-06-02 12:42:37 +04:00
|
|
|
*
|
2012-06-03 21:12:49 +04:00
|
|
|
* kmem_arena:
|
|
|
|
* Main arena controlling the kernel KVA used by other arenas.
|
2012-06-02 12:42:37 +04:00
|
|
|
*
|
2012-06-03 21:12:49 +04:00
|
|
|
* kmem_va_arena:
|
|
|
|
* Implements quantum caching in order to speedup allocations and
|
|
|
|
* reduce fragmentation. The pool(9), unless created with a custom
|
|
|
|
* meta-data allocator, and kmem(9) subsystems use this arena.
|
2012-06-02 12:42:37 +04:00
|
|
|
*
|
2012-06-03 21:12:49 +04:00
|
|
|
* Arenas for meta-data allocations are used by vmem(9) and pool(9).
|
|
|
|
* These arenas cannot use quantum cache. However, kmem_va_meta_arena
|
|
|
|
* compensates this by importing larger chunks from kmem_arena.
|
2012-06-02 12:42:37 +04:00
|
|
|
*
|
2012-06-03 21:12:49 +04:00
|
|
|
* kmem_va_meta_arena:
|
|
|
|
* Space for meta-data.
|
|
|
|
*
|
|
|
|
* kmem_meta_arena:
|
|
|
|
* Imports from kmem_va_meta_arena. Allocations from this arena are
|
|
|
|
* backed with the pages.
|
|
|
|
*
|
|
|
|
* Arena stacking:
|
|
|
|
*
|
|
|
|
* kmem_arena
|
|
|
|
* kmem_va_arena
|
|
|
|
* kmem_va_meta_arena
|
|
|
|
* kmem_meta_arena
|
1998-02-24 18:58:09 +03:00
|
|
|
*/
|
|
|
|
|
2001-11-10 10:36:59 +03:00
|
|
|
#include <sys/cdefs.h>
|
2020-02-23 18:46:38 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: uvm_km.c,v 1.155 2020/02/23 15:46:43 ad Exp $");
|
2001-11-10 10:36:59 +03:00
|
|
|
|
|
|
|
#include "opt_uvmhist.h"
|
|
|
|
|
2012-02-02 22:59:44 +04:00
|
|
|
#include "opt_kmempages.h"
|
|
|
|
|
|
|
|
#ifndef NKMEMPAGES
|
|
|
|
#define NKMEMPAGES 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Defaults for lower and upper-bounds for the kmem_arena page count.
|
|
|
|
* Can be overridden by kernel config options.
|
|
|
|
*/
|
|
|
|
#ifndef NKMEMPAGES_MIN
|
|
|
|
#define NKMEMPAGES_MIN NKMEMPAGES_MIN_DEFAULT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef NKMEMPAGES_MAX
|
|
|
|
#define NKMEMPAGES_MAX NKMEMPAGES_MAX_DEFAULT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1998-02-05 09:25:08 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2019-12-02 02:14:47 +03:00
|
|
|
#include <sys/atomic.h>
|
1998-02-05 09:25:08 +03:00
|
|
|
#include <sys/proc.h>
|
2005-01-02 00:08:02 +03:00
|
|
|
#include <sys/pool.h>
|
2012-01-27 23:48:38 +04:00
|
|
|
#include <sys/vmem.h>
|
2013-01-30 01:29:40 +04:00
|
|
|
#include <sys/vmem_impl.h>
|
2012-01-27 23:48:38 +04:00
|
|
|
#include <sys/kmem.h>
|
Add support for Kernel Memory Sanitizer (kMSan). It detects uninitialized
memory used by the kernel at run time, and just like kASan and kCSan, it
is an excellent feature. It has already detected 38 uninitialized variables
in the kernel during my testing, which I have since discreetly fixed.
We use two shadows:
- "shad", to track uninitialized memory with a bit granularity (1:1).
Each bit set to 1 in the shad corresponds to one uninitialized bit of
real kernel memory.
- "orig", to track the origin of the memory with a 4-byte granularity
(1:1). Each uint32_t cell in the orig indicates the origin of the
associated uint32_t of real kernel memory.
The memory consumption of these shadows is consequent, so at least 4GB of
RAM is recommended to run kMSan.
The compiler inserts calls to specific __msan_* functions on each memory
access, to manage both the shad and the orig and detect uninitialized
memory accesses that change the execution flow (like an "if" on an
uninitialized variable).
We mark as uninit several types of memory buffers (stack, pools, kmem,
malloc, uvm_km), and check each buffer passed to copyout, copyoutstr,
bwrite, if_transmit_lock and DMA operations, to detect uninitialized memory
that leaves the system. This allows us to detect kernel info leaks in a way
that is more efficient and also more user-friendly than KLEAK.
Contrary to kASan, kMSan requires comprehensive coverage, ie we cannot
tolerate having one non-instrumented function, because this could cause
false positives. kMSan cannot instrument ASM functions, so I converted
most of them to __asm__ inlines, which kMSan is able to instrument. Those
that remain receive special treatment.
Contrary to kASan again, kMSan uses a TLS, so we must context-switch this
TLS during interrupts. We use different contexts depending on the interrupt
level.
The orig tracks precisely the origin of a buffer. We use a special encoding
for the orig values, and pack together in each uint32_t cell of the orig:
- a code designating the type of memory (Stack, Pool, etc), and
- a compressed pointer, which points either (1) to a string containing
the name of the variable associated with the cell, or (2) to an area
in the kernel .text section which we resolve to a symbol name + offset.
This encoding allows us not to consume extra memory for associating
information with each cell, and produces a precise output, that can tell
for example the name of an uninitialized variable on the stack, the
function in which it was pushed on the stack, and the function where we
accessed this uninitialized variable.
kMSan is available with LLVM, but not with GCC.
The code is organized in a way that is similar to kASan and kCSan, so it
means that other architectures than amd64 can be supported.
2019-11-14 19:23:52 +03:00
|
|
|
#include <sys/msan.h>
|
1998-02-05 09:25:08 +03:00
|
|
|
|
|
|
|
#include <uvm/uvm.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* global data structures
|
|
|
|
*/
|
|
|
|
|
2001-06-02 22:09:08 +04:00
|
|
|
struct vm_map *kernel_map = NULL;
|
1998-02-05 09:25:08 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* local data structues
|
|
|
|
*/
|
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
static struct vm_map kernel_map_store;
|
|
|
|
static struct vm_map_entry kernel_image_mapent_store;
|
|
|
|
static struct vm_map_entry kernel_kmem_mapent_store;
|
1998-02-05 09:25:08 +03:00
|
|
|
|
2012-02-02 22:59:44 +04:00
|
|
|
int nkmempages = 0;
|
2012-01-27 23:48:38 +04:00
|
|
|
vaddr_t kmembase;
|
|
|
|
vsize_t kmemsize;
|
2005-01-02 00:08:02 +03:00
|
|
|
|
2013-01-30 01:29:40 +04:00
|
|
|
static struct vmem kmem_arena_store;
|
2012-09-07 10:45:04 +04:00
|
|
|
vmem_t *kmem_arena = NULL;
|
2013-01-30 01:29:40 +04:00
|
|
|
static struct vmem kmem_va_arena_store;
|
2012-01-27 23:48:38 +04:00
|
|
|
vmem_t *kmem_va_arena;
|
2005-01-02 00:08:02 +03:00
|
|
|
|
2012-02-02 22:59:44 +04:00
|
|
|
/*
|
|
|
|
* kmeminit_nkmempages: calculate the size of kmem_arena.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
kmeminit_nkmempages(void)
|
|
|
|
{
|
|
|
|
int npages;
|
|
|
|
|
|
|
|
if (nkmempages != 0) {
|
|
|
|
/*
|
|
|
|
* It's already been set (by us being here before)
|
|
|
|
* bail out now;
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Add support for Kernel Memory Sanitizer (kMSan). It detects uninitialized
memory used by the kernel at run time, and just like kASan and kCSan, it
is an excellent feature. It has already detected 38 uninitialized variables
in the kernel during my testing, which I have since discreetly fixed.
We use two shadows:
- "shad", to track uninitialized memory with a bit granularity (1:1).
Each bit set to 1 in the shad corresponds to one uninitialized bit of
real kernel memory.
- "orig", to track the origin of the memory with a 4-byte granularity
(1:1). Each uint32_t cell in the orig indicates the origin of the
associated uint32_t of real kernel memory.
The memory consumption of these shadows is consequent, so at least 4GB of
RAM is recommended to run kMSan.
The compiler inserts calls to specific __msan_* functions on each memory
access, to manage both the shad and the orig and detect uninitialized
memory accesses that change the execution flow (like an "if" on an
uninitialized variable).
We mark as uninit several types of memory buffers (stack, pools, kmem,
malloc, uvm_km), and check each buffer passed to copyout, copyoutstr,
bwrite, if_transmit_lock and DMA operations, to detect uninitialized memory
that leaves the system. This allows us to detect kernel info leaks in a way
that is more efficient and also more user-friendly than KLEAK.
Contrary to kASan, kMSan requires comprehensive coverage, ie we cannot
tolerate having one non-instrumented function, because this could cause
false positives. kMSan cannot instrument ASM functions, so I converted
most of them to __asm__ inlines, which kMSan is able to instrument. Those
that remain receive special treatment.
Contrary to kASan again, kMSan uses a TLS, so we must context-switch this
TLS during interrupts. We use different contexts depending on the interrupt
level.
The orig tracks precisely the origin of a buffer. We use a special encoding
for the orig values, and pack together in each uint32_t cell of the orig:
- a code designating the type of memory (Stack, Pool, etc), and
- a compressed pointer, which points either (1) to a string containing
the name of the variable associated with the cell, or (2) to an area
in the kernel .text section which we resolve to a symbol name + offset.
This encoding allows us not to consume extra memory for associating
information with each cell, and produces a precise output, that can tell
for example the name of an uninitialized variable on the stack, the
function in which it was pushed on the stack, and the function where we
accessed this uninitialized variable.
kMSan is available with LLVM, but not with GCC.
The code is organized in a way that is similar to kASan and kCSan, so it
means that other architectures than amd64 can be supported.
2019-11-14 19:23:52 +03:00
|
|
|
#if defined(KMSAN)
|
|
|
|
npages = (physmem / 8);
|
|
|
|
#elif defined(PMAP_MAP_POOLPAGE)
|
2012-02-04 21:56:16 +04:00
|
|
|
npages = (physmem / 4);
|
|
|
|
#else
|
|
|
|
npages = (physmem / 3) * 2;
|
|
|
|
#endif /* defined(PMAP_MAP_POOLPAGE) */
|
2012-02-02 22:59:44 +04:00
|
|
|
|
2012-02-04 21:56:16 +04:00
|
|
|
#ifndef NKMEMPAGES_MAX_UNLIMITED
|
2012-02-02 22:59:44 +04:00
|
|
|
if (npages > NKMEMPAGES_MAX)
|
|
|
|
npages = NKMEMPAGES_MAX;
|
2012-02-04 21:56:16 +04:00
|
|
|
#endif
|
2012-02-02 22:59:44 +04:00
|
|
|
|
|
|
|
if (npages < NKMEMPAGES_MIN)
|
|
|
|
npages = NKMEMPAGES_MIN;
|
|
|
|
|
|
|
|
nkmempages = npages;
|
|
|
|
}
|
|
|
|
|
2005-01-02 00:08:02 +03:00
|
|
|
/*
|
2012-01-27 23:48:38 +04:00
|
|
|
* uvm_km_bootstrap: init kernel maps and objects to reflect reality (i.e.
|
1998-02-05 09:25:08 +03:00
|
|
|
* KVM already allocated for text, data, bss, and static data structures).
|
|
|
|
*
|
2003-05-11 01:10:23 +04:00
|
|
|
* => KVM is defined by VM_MIN_KERNEL_ADDRESS/VM_MAX_KERNEL_ADDRESS.
|
2005-05-30 01:06:33 +04:00
|
|
|
* we assume that [vmin -> start] has already been allocated and that
|
2003-05-11 01:10:23 +04:00
|
|
|
* "end" is the end.
|
1998-02-05 09:25:08 +03:00
|
|
|
*/
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
void
|
2012-01-27 23:48:38 +04:00
|
|
|
uvm_km_bootstrap(vaddr_t start, vaddr_t end)
|
1998-02-05 09:25:08 +03:00
|
|
|
{
|
2012-02-04 21:56:16 +04:00
|
|
|
bool kmem_arena_small;
|
2003-05-11 01:10:23 +04:00
|
|
|
vaddr_t base = VM_MIN_KERNEL_ADDRESS;
|
2012-02-03 23:25:07 +04:00
|
|
|
struct uvm_map_args args;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
|
Update the kernhist(9) kernel history code to address issues identified
in PR kern/52639, as well as some general cleaning-up...
(As proposed on tech-kern@ with additional changes and enhancements.)
Details of changes:
* All history arguments are now stored as uintmax_t values[1], both in
the kernel and in the structures used for exporting the history data
to userland via sysctl(9). This avoids problems on some architectures
where passing a 64-bit (or larger) value to printf(3) can cause it to
process the value as multiple arguments. (This can be particularly
problematic when printf()'s format string is not a literal, since in
that case the compiler cannot know how large each argument should be.)
* Update the data structures used for exporting kernel history data to
include a version number as well as the length of history arguments.
* All [2] existing users of kernhist(9) have had their format strings
updated. Each format specifier now includes an explicit length
modifier 'j' to refer to numeric values of the size of uintmax_t.
* All [2] existing users of kernhist(9) have had their format strings
updated to replace uses of "%p" with "%#jx", and the pointer
arguments are now cast to (uintptr_t) before being subsequently cast
to (uintmax_t). This is needed to avoid compiler warnings about
casting "pointer to integer of a different size."
* All [2] existing users of kernhist(9) have had instances of "%s" or
"%c" format strings replaced with numeric formats; several instances
of mis-match between format string and argument list have been fixed.
* vmstat(1) has been modified to handle the new size of arguments in the
history data as exported by sysctl(9).
* vmstat(1) now provides a warning message if the history requested with
the -u option does not exist (previously, this condition was silently
ignored, with only a single blank line being printed).
* vmstat(1) now checks the version and argument length included in the
data exported via sysctl(9) and exits if they do not match the values
with which vmstat was built.
* The kernhist(9) man-page has been updated to note the additional
requirements imposed on the format strings, along with several other
minor changes and enhancements.
[1] It would have been possible to use an explicit length (for example,
uint64_t) for the history arguments. But that would require another
"rototill" of all the users in the future when we add support for an
architecture that supports a larger size. Also, the printf(3) format
specifiers for explicitly-sized values, such as "%"PRIu64, are much
more verbose (and less aesthetically appealing, IMHO) than simply
using "%ju".
[2] I've tried very hard to find "all [the] existing users of kernhist(9)"
but it is possible that I've missed some of them. I would be glad to
update any stragglers that anyone identifies.
2017-10-28 03:37:11 +03:00
|
|
|
UVMHIST_LOG(maphist, "start=%#jx end=%#jx", start, end, 0,0);
|
1998-02-05 09:25:08 +03:00
|
|
|
|
2012-02-02 22:59:44 +04:00
|
|
|
kmeminit_nkmempages();
|
2012-02-04 21:56:16 +04:00
|
|
|
kmemsize = (vsize_t)nkmempages * PAGE_SIZE;
|
|
|
|
kmem_arena_small = kmemsize < 64 * 1024 * 1024;
|
2012-01-27 23:48:38 +04:00
|
|
|
|
Update the kernhist(9) kernel history code to address issues identified
in PR kern/52639, as well as some general cleaning-up...
(As proposed on tech-kern@ with additional changes and enhancements.)
Details of changes:
* All history arguments are now stored as uintmax_t values[1], both in
the kernel and in the structures used for exporting the history data
to userland via sysctl(9). This avoids problems on some architectures
where passing a 64-bit (or larger) value to printf(3) can cause it to
process the value as multiple arguments. (This can be particularly
problematic when printf()'s format string is not a literal, since in
that case the compiler cannot know how large each argument should be.)
* Update the data structures used for exporting kernel history data to
include a version number as well as the length of history arguments.
* All [2] existing users of kernhist(9) have had their format strings
updated. Each format specifier now includes an explicit length
modifier 'j' to refer to numeric values of the size of uintmax_t.
* All [2] existing users of kernhist(9) have had their format strings
updated to replace uses of "%p" with "%#jx", and the pointer
arguments are now cast to (uintptr_t) before being subsequently cast
to (uintmax_t). This is needed to avoid compiler warnings about
casting "pointer to integer of a different size."
* All [2] existing users of kernhist(9) have had instances of "%s" or
"%c" format strings replaced with numeric formats; several instances
of mis-match between format string and argument list have been fixed.
* vmstat(1) has been modified to handle the new size of arguments in the
history data as exported by sysctl(9).
* vmstat(1) now provides a warning message if the history requested with
the -u option does not exist (previously, this condition was silently
ignored, with only a single blank line being printed).
* vmstat(1) now checks the version and argument length included in the
data exported via sysctl(9) and exits if they do not match the values
with which vmstat was built.
* The kernhist(9) man-page has been updated to note the additional
requirements imposed on the format strings, along with several other
minor changes and enhancements.
[1] It would have been possible to use an explicit length (for example,
uint64_t) for the history arguments. But that would require another
"rototill" of all the users in the future when we add support for an
architecture that supports a larger size. Also, the printf(3) format
specifiers for explicitly-sized values, such as "%"PRIu64, are much
more verbose (and less aesthetically appealing, IMHO) than simply
using "%ju".
[2] I've tried very hard to find "all [the] existing users of kernhist(9)"
but it is possible that I've missed some of them. I would be glad to
update any stragglers that anyone identifies.
2017-10-28 03:37:11 +03:00
|
|
|
UVMHIST_LOG(maphist, "kmemsize=%#jx", kmemsize, 0,0,0);
|
2012-02-03 23:25:07 +04:00
|
|
|
|
1999-06-05 03:38:41 +04:00
|
|
|
/*
|
|
|
|
* next, init kernel memory objects.
|
1998-03-09 03:58:55 +03:00
|
|
|
*/
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/* kernel_object: for pageable anonymous kernel memory */
|
2007-07-21 23:21:53 +04:00
|
|
|
uvm_kernel_object = uao_create(VM_MAX_KERNEL_ADDRESS -
|
2012-01-27 23:48:38 +04:00
|
|
|
VM_MIN_KERNEL_ADDRESS, UAO_FLAG_KERNOBJ);
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
2002-03-07 23:15:32 +03:00
|
|
|
* init the map and reserve any space that might already
|
|
|
|
* have been allocated kernel space before installing.
|
1998-03-09 03:58:55 +03:00
|
|
|
*/
|
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
uvm_map_setup(&kernel_map_store, base, end, VM_MAP_PAGEABLE);
|
|
|
|
kernel_map_store.pmap = pmap_kernel();
|
2005-01-02 00:00:06 +03:00
|
|
|
if (start != base) {
|
2012-01-27 23:48:38 +04:00
|
|
|
error = uvm_map_prepare(&kernel_map_store,
|
2005-01-02 00:02:12 +03:00
|
|
|
base, start - base,
|
2005-01-02 00:00:06 +03:00
|
|
|
NULL, UVM_UNKNOWN_OFFSET, 0,
|
2003-05-11 01:10:23 +04:00
|
|
|
UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
|
2005-01-02 00:00:06 +03:00
|
|
|
UVM_ADV_RANDOM, UVM_FLAG_FIXED), &args);
|
|
|
|
if (!error) {
|
2012-01-27 23:48:38 +04:00
|
|
|
kernel_image_mapent_store.flags =
|
|
|
|
UVM_MAP_KERNEL | UVM_MAP_STATIC | UVM_MAP_NOMERGE;
|
|
|
|
error = uvm_map_enter(&kernel_map_store, &args,
|
|
|
|
&kernel_image_mapent_store);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
panic(
|
|
|
|
"uvm_km_bootstrap: could not reserve space for kernel");
|
|
|
|
|
|
|
|
kmembase = args.uma_start + args.uma_size;
|
2012-01-31 04:30:52 +04:00
|
|
|
} else {
|
|
|
|
kmembase = base;
|
2005-01-02 00:00:06 +03:00
|
|
|
}
|
2001-05-25 08:06:11 +04:00
|
|
|
|
2012-02-03 23:25:07 +04:00
|
|
|
error = uvm_map_prepare(&kernel_map_store,
|
|
|
|
kmembase, kmemsize,
|
|
|
|
NULL, UVM_UNKNOWN_OFFSET, 0,
|
|
|
|
UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
|
|
|
|
UVM_ADV_RANDOM, UVM_FLAG_FIXED), &args);
|
|
|
|
if (!error) {
|
|
|
|
kernel_kmem_mapent_store.flags =
|
|
|
|
UVM_MAP_KERNEL | UVM_MAP_STATIC | UVM_MAP_NOMERGE;
|
|
|
|
error = uvm_map_enter(&kernel_map_store, &args,
|
|
|
|
&kernel_kmem_mapent_store);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
panic("uvm_km_bootstrap: could not reserve kernel kmem");
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
|
|
|
* install!
|
|
|
|
*/
|
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
kernel_map = &kernel_map_store;
|
|
|
|
|
|
|
|
pool_subsystem_init();
|
|
|
|
|
2013-01-30 01:29:40 +04:00
|
|
|
kmem_arena = vmem_init(&kmem_arena_store, "kmem",
|
|
|
|
kmembase, kmemsize, PAGE_SIZE, NULL, NULL, NULL,
|
2012-01-27 23:48:38 +04:00
|
|
|
0, VM_NOSLEEP | VM_BOOTSTRAP, IPL_VM);
|
2012-09-07 10:45:04 +04:00
|
|
|
#ifdef PMAP_GROWKERNEL
|
|
|
|
/*
|
|
|
|
* kmem_arena VA allocations happen independently of uvm_map.
|
|
|
|
* grow kernel to accommodate the kmem_arena.
|
|
|
|
*/
|
|
|
|
if (uvm_maxkaddr < kmembase + kmemsize) {
|
|
|
|
uvm_maxkaddr = pmap_growkernel(kmembase + kmemsize);
|
|
|
|
KASSERTMSG(uvm_maxkaddr >= kmembase + kmemsize,
|
|
|
|
"%#"PRIxVADDR" %#"PRIxVADDR" %#"PRIxVSIZE,
|
|
|
|
uvm_maxkaddr, kmembase, kmemsize);
|
|
|
|
}
|
|
|
|
#endif
|
2012-01-27 23:48:38 +04:00
|
|
|
|
2013-01-30 01:29:40 +04:00
|
|
|
vmem_subsystem_init(kmem_arena);
|
2012-01-27 23:48:38 +04:00
|
|
|
|
Update the kernhist(9) kernel history code to address issues identified
in PR kern/52639, as well as some general cleaning-up...
(As proposed on tech-kern@ with additional changes and enhancements.)
Details of changes:
* All history arguments are now stored as uintmax_t values[1], both in
the kernel and in the structures used for exporting the history data
to userland via sysctl(9). This avoids problems on some architectures
where passing a 64-bit (or larger) value to printf(3) can cause it to
process the value as multiple arguments. (This can be particularly
problematic when printf()'s format string is not a literal, since in
that case the compiler cannot know how large each argument should be.)
* Update the data structures used for exporting kernel history data to
include a version number as well as the length of history arguments.
* All [2] existing users of kernhist(9) have had their format strings
updated. Each format specifier now includes an explicit length
modifier 'j' to refer to numeric values of the size of uintmax_t.
* All [2] existing users of kernhist(9) have had their format strings
updated to replace uses of "%p" with "%#jx", and the pointer
arguments are now cast to (uintptr_t) before being subsequently cast
to (uintmax_t). This is needed to avoid compiler warnings about
casting "pointer to integer of a different size."
* All [2] existing users of kernhist(9) have had instances of "%s" or
"%c" format strings replaced with numeric formats; several instances
of mis-match between format string and argument list have been fixed.
* vmstat(1) has been modified to handle the new size of arguments in the
history data as exported by sysctl(9).
* vmstat(1) now provides a warning message if the history requested with
the -u option does not exist (previously, this condition was silently
ignored, with only a single blank line being printed).
* vmstat(1) now checks the version and argument length included in the
data exported via sysctl(9) and exits if they do not match the values
with which vmstat was built.
* The kernhist(9) man-page has been updated to note the additional
requirements imposed on the format strings, along with several other
minor changes and enhancements.
[1] It would have been possible to use an explicit length (for example,
uint64_t) for the history arguments. But that would require another
"rototill" of all the users in the future when we add support for an
architecture that supports a larger size. Also, the printf(3) format
specifiers for explicitly-sized values, such as "%"PRIu64, are much
more verbose (and less aesthetically appealing, IMHO) than simply
using "%ju".
[2] I've tried very hard to find "all [the] existing users of kernhist(9)"
but it is possible that I've missed some of them. I would be glad to
update any stragglers that anyone identifies.
2017-10-28 03:37:11 +03:00
|
|
|
UVMHIST_LOG(maphist, "kmem vmem created (base=%#jx, size=%#jx",
|
|
|
|
kmembase, kmemsize, 0,0);
|
2012-02-03 23:25:07 +04:00
|
|
|
|
2013-01-30 01:29:40 +04:00
|
|
|
kmem_va_arena = vmem_init(&kmem_va_arena_store, "kva",
|
|
|
|
0, 0, PAGE_SIZE, vmem_alloc, vmem_free, kmem_arena,
|
|
|
|
(kmem_arena_small ? 4 : VMEM_QCACHE_IDX_MAX) * PAGE_SIZE,
|
|
|
|
VM_NOSLEEP, IPL_VM);
|
2012-02-03 23:25:07 +04:00
|
|
|
|
|
|
|
UVMHIST_LOG(maphist, "<- done", 0,0,0,0);
|
2012-01-27 23:48:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* uvm_km_init: init the kernel maps virtual memory caches
|
|
|
|
* and start the pool/kmem allocator.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
uvm_km_init(void)
|
|
|
|
{
|
|
|
|
kmem_init();
|
1998-02-05 09:25:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* uvm_km_suballoc: allocate a submap in the kernel map. once a submap
|
|
|
|
* is allocated all references to that area of VM must go through it. this
|
|
|
|
* allows the locking of VAs in kernel_map to be broken up into regions.
|
|
|
|
*
|
2005-05-30 01:06:33 +04:00
|
|
|
* => if `fixed' is true, *vmin specifies where the region described
|
2012-01-27 23:48:38 +04:00
|
|
|
* pager_map => used to map "buf" structures into kernel space
|
1998-02-08 09:15:53 +03:00
|
|
|
* by the submap must start
|
1998-02-05 09:25:08 +03:00
|
|
|
* => if submap is non NULL we use that as the submap, otherwise we
|
|
|
|
* alloc a new map
|
|
|
|
*/
|
2005-04-01 15:59:21 +04:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
struct vm_map *
|
2005-06-27 06:19:48 +04:00
|
|
|
uvm_km_suballoc(struct vm_map *map, vaddr_t *vmin /* IN/OUT */,
|
2007-02-22 01:59:35 +03:00
|
|
|
vaddr_t *vmax /* OUT */, vsize_t size, int flags, bool fixed,
|
2012-01-27 23:48:38 +04:00
|
|
|
struct vm_map *submap)
|
1998-03-09 03:58:55 +03:00
|
|
|
{
|
|
|
|
int mapflags = UVM_FLAG_NOMERGE | (fixed ? UVM_FLAG_FIXED : 0);
|
2012-02-03 23:25:07 +04:00
|
|
|
UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
|
1998-02-05 09:25:08 +03:00
|
|
|
|
2005-01-02 00:02:12 +03:00
|
|
|
KASSERT(vm_map_pmap(map) == pmap_kernel());
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
size = round_page(size); /* round up to pagesize */
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
|
|
|
* first allocate a blank spot in the parent map
|
|
|
|
*/
|
1998-02-05 09:25:08 +03:00
|
|
|
|
2005-05-30 01:06:33 +04:00
|
|
|
if (uvm_map(map, vmin, size, NULL, UVM_UNKNOWN_OFFSET, 0,
|
1998-03-09 03:58:55 +03:00
|
|
|
UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
|
2001-03-15 09:10:32 +03:00
|
|
|
UVM_ADV_RANDOM, mapflags)) != 0) {
|
2012-02-03 23:25:07 +04:00
|
|
|
panic("%s: unable to allocate space in parent map", __func__);
|
1998-03-09 03:58:55 +03:00
|
|
|
}
|
1998-02-08 09:15:53 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
2005-05-30 01:06:33 +04:00
|
|
|
* set VM bounds (vmin is filled in by uvm_map)
|
1998-03-09 03:58:55 +03:00
|
|
|
*/
|
1998-02-05 09:25:08 +03:00
|
|
|
|
2005-05-30 01:06:33 +04:00
|
|
|
*vmax = *vmin + size;
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
|
|
|
* add references to pmap and create or init the submap
|
|
|
|
*/
|
|
|
|
|
|
|
|
pmap_reference(vm_map_pmap(map));
|
|
|
|
if (submap == NULL) {
|
2012-01-27 23:48:38 +04:00
|
|
|
submap = kmem_alloc(sizeof(*submap), KM_SLEEP);
|
1998-03-09 03:58:55 +03:00
|
|
|
}
|
2012-01-27 23:48:38 +04:00
|
|
|
uvm_map_setup(submap, *vmin, *vmax, flags);
|
|
|
|
submap->pmap = vm_map_pmap(map);
|
1998-03-09 03:58:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* now let uvm_map_submap plug in it...
|
|
|
|
*/
|
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
if (uvm_map_submap(map, *vmin, *vmax, submap) != 0)
|
1998-03-09 03:58:55 +03:00
|
|
|
panic("uvm_km_suballoc: submap allocation failed");
|
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
return(submap);
|
1998-02-05 09:25:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-07-05 18:03:06 +04:00
|
|
|
* uvm_km_pgremove: remove pages from a kernel uvm_object and KVA.
|
1998-02-05 09:25:08 +03:00
|
|
|
*/
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
void
|
2005-06-27 06:19:48 +04:00
|
|
|
uvm_km_pgremove(vaddr_t startva, vaddr_t endva)
|
1998-02-05 09:25:08 +03:00
|
|
|
{
|
2007-07-21 23:21:53 +04:00
|
|
|
struct uvm_object * const uobj = uvm_kernel_object;
|
2005-04-01 15:59:21 +04:00
|
|
|
const voff_t start = startva - vm_map_min(kernel_map);
|
|
|
|
const voff_t end = endva - vm_map_min(kernel_map);
|
2001-11-06 11:07:49 +03:00
|
|
|
struct vm_page *pg;
|
a whole bunch of changes to improve performance and robustness under load:
- remove special treatment of pager_map mappings in pmaps. this is
required now, since I've removed the globals that expose the address range.
pager_map now uses pmap_kenter_pa() instead of pmap_enter(), so there's
no longer any need to special-case it.
- eliminate struct uvm_vnode by moving its fields into struct vnode.
- rewrite the pageout path. the pager is now responsible for handling the
high-level requests instead of only getting control after a bunch of work
has already been done on its behalf. this will allow us to UBCify LFS,
which needs tighter control over its pages than other filesystems do.
writing a page to disk no longer requires making it read-only, which
allows us to write wired pages without causing all kinds of havoc.
- use a new PG_PAGEOUT flag to indicate that a page should be freed
on behalf of the pagedaemon when it's unlocked. this flag is very similar
to PG_RELEASED, but unlike PG_RELEASED, PG_PAGEOUT can be cleared if the
pageout fails due to eg. an indirect-block buffer being locked.
this allows us to remove the "version" field from struct vm_page,
and together with shrinking "loan_count" from 32 bits to 16,
struct vm_page is now 4 bytes smaller.
- no longer use PG_RELEASED for swap-backed pages. if the page is busy
because it's being paged out, we can't release the swap slot to be
reallocated until that write is complete, but unlike with vnodes we
don't keep a count of in-progress writes so there's no good way to
know when the write is done. instead, when we need to free a busy
swap-backed page, just sleep until we can get it busy ourselves.
- implement a fast-path for extending writes which allows us to avoid
zeroing new pages. this substantially reduces cpu usage.
- encapsulate the data used by the genfs code in a struct genfs_node,
which must be the first element of the filesystem-specific vnode data
for filesystems which use genfs_{get,put}pages().
- eliminate many of the UVM pagerops, since they aren't needed anymore
now that the pager "put" operation is a higher-level operation.
- enhance the genfs code to allow NFS to use the genfs_{get,put}pages
instead of a modified copy.
- clean up struct vnode by removing all the fields that used to be used by
the vfs_cluster.c code (which we don't use anymore with UBC).
- remove kmem_object and mb_object since they were useless.
instead of allocating pages to these objects, we now just allocate
pages with no object. such pages are mapped in the kernel until they
are freed, so we can use the mapping to find the page to free it.
this allows us to remove splvm() protection in several places.
The sum of all these changes improves write throughput on my
decstation 5000/200 to within 1% of the rate of NetBSD 1.5
and reduces the elapsed time for "make release" of a NetBSD 1.5
source tree on my 128MB pc to 10% less than a 1.5 kernel took.
2001-09-16 00:36:31 +04:00
|
|
|
voff_t curoff, nextoff;
|
2001-11-06 11:07:49 +03:00
|
|
|
int swpgonlydelta = 0;
|
2012-02-03 23:25:07 +04:00
|
|
|
UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
|
1998-02-05 09:25:08 +03:00
|
|
|
|
2005-04-01 15:59:21 +04:00
|
|
|
KASSERT(VM_MIN_KERNEL_ADDRESS <= startva);
|
|
|
|
KASSERT(startva < endva);
|
2006-04-06 01:56:24 +04:00
|
|
|
KASSERT(endva <= VM_MAX_KERNEL_ADDRESS);
|
2005-04-01 15:59:21 +04:00
|
|
|
|
2020-02-23 18:46:38 +03:00
|
|
|
rw_enter(uobj->vmobjlock, RW_WRITER);
|
2011-07-05 18:03:06 +04:00
|
|
|
pmap_remove(pmap_kernel(), startva, endva);
|
a whole bunch of changes to improve performance and robustness under load:
- remove special treatment of pager_map mappings in pmaps. this is
required now, since I've removed the globals that expose the address range.
pager_map now uses pmap_kenter_pa() instead of pmap_enter(), so there's
no longer any need to special-case it.
- eliminate struct uvm_vnode by moving its fields into struct vnode.
- rewrite the pageout path. the pager is now responsible for handling the
high-level requests instead of only getting control after a bunch of work
has already been done on its behalf. this will allow us to UBCify LFS,
which needs tighter control over its pages than other filesystems do.
writing a page to disk no longer requires making it read-only, which
allows us to write wired pages without causing all kinds of havoc.
- use a new PG_PAGEOUT flag to indicate that a page should be freed
on behalf of the pagedaemon when it's unlocked. this flag is very similar
to PG_RELEASED, but unlike PG_RELEASED, PG_PAGEOUT can be cleared if the
pageout fails due to eg. an indirect-block buffer being locked.
this allows us to remove the "version" field from struct vm_page,
and together with shrinking "loan_count" from 32 bits to 16,
struct vm_page is now 4 bytes smaller.
- no longer use PG_RELEASED for swap-backed pages. if the page is busy
because it's being paged out, we can't release the swap slot to be
reallocated until that write is complete, but unlike with vnodes we
don't keep a count of in-progress writes so there's no good way to
know when the write is done. instead, when we need to free a busy
swap-backed page, just sleep until we can get it busy ourselves.
- implement a fast-path for extending writes which allows us to avoid
zeroing new pages. this substantially reduces cpu usage.
- encapsulate the data used by the genfs code in a struct genfs_node,
which must be the first element of the filesystem-specific vnode data
for filesystems which use genfs_{get,put}pages().
- eliminate many of the UVM pagerops, since they aren't needed anymore
now that the pager "put" operation is a higher-level operation.
- enhance the genfs code to allow NFS to use the genfs_{get,put}pages
instead of a modified copy.
- clean up struct vnode by removing all the fields that used to be used by
the vfs_cluster.c code (which we don't use anymore with UBC).
- remove kmem_object and mb_object since they were useless.
instead of allocating pages to these objects, we now just allocate
pages with no object. such pages are mapped in the kernel until they
are freed, so we can use the mapping to find the page to free it.
this allows us to remove splvm() protection in several places.
The sum of all these changes improves write throughput on my
decstation 5000/200 to within 1% of the rate of NetBSD 1.5
and reduces the elapsed time for "make release" of a NetBSD 1.5
source tree on my 128MB pc to 10% less than a 1.5 kernel took.
2001-09-16 00:36:31 +04:00
|
|
|
for (curoff = start; curoff < end; curoff = nextoff) {
|
|
|
|
nextoff = curoff + PAGE_SIZE;
|
|
|
|
pg = uvm_pagelookup(uobj, curoff);
|
2001-11-06 11:07:49 +03:00
|
|
|
if (pg != NULL && pg->flags & PG_BUSY) {
|
a whole bunch of changes to improve performance and robustness under load:
- remove special treatment of pager_map mappings in pmaps. this is
required now, since I've removed the globals that expose the address range.
pager_map now uses pmap_kenter_pa() instead of pmap_enter(), so there's
no longer any need to special-case it.
- eliminate struct uvm_vnode by moving its fields into struct vnode.
- rewrite the pageout path. the pager is now responsible for handling the
high-level requests instead of only getting control after a bunch of work
has already been done on its behalf. this will allow us to UBCify LFS,
which needs tighter control over its pages than other filesystems do.
writing a page to disk no longer requires making it read-only, which
allows us to write wired pages without causing all kinds of havoc.
- use a new PG_PAGEOUT flag to indicate that a page should be freed
on behalf of the pagedaemon when it's unlocked. this flag is very similar
to PG_RELEASED, but unlike PG_RELEASED, PG_PAGEOUT can be cleared if the
pageout fails due to eg. an indirect-block buffer being locked.
this allows us to remove the "version" field from struct vm_page,
and together with shrinking "loan_count" from 32 bits to 16,
struct vm_page is now 4 bytes smaller.
- no longer use PG_RELEASED for swap-backed pages. if the page is busy
because it's being paged out, we can't release the swap slot to be
reallocated until that write is complete, but unlike with vnodes we
don't keep a count of in-progress writes so there's no good way to
know when the write is done. instead, when we need to free a busy
swap-backed page, just sleep until we can get it busy ourselves.
- implement a fast-path for extending writes which allows us to avoid
zeroing new pages. this substantially reduces cpu usage.
- encapsulate the data used by the genfs code in a struct genfs_node,
which must be the first element of the filesystem-specific vnode data
for filesystems which use genfs_{get,put}pages().
- eliminate many of the UVM pagerops, since they aren't needed anymore
now that the pager "put" operation is a higher-level operation.
- enhance the genfs code to allow NFS to use the genfs_{get,put}pages
instead of a modified copy.
- clean up struct vnode by removing all the fields that used to be used by
the vfs_cluster.c code (which we don't use anymore with UBC).
- remove kmem_object and mb_object since they were useless.
instead of allocating pages to these objects, we now just allocate
pages with no object. such pages are mapped in the kernel until they
are freed, so we can use the mapping to find the page to free it.
this allows us to remove splvm() protection in several places.
The sum of all these changes improves write throughput on my
decstation 5000/200 to within 1% of the rate of NetBSD 1.5
and reduces the elapsed time for "make release" of a NetBSD 1.5
source tree on my 128MB pc to 10% less than a 1.5 kernel took.
2001-09-16 00:36:31 +04:00
|
|
|
pg->flags |= PG_WANTED;
|
2020-02-23 18:46:38 +03:00
|
|
|
UVM_UNLOCK_AND_WAIT_RW(pg, uobj->vmobjlock, 0,
|
a whole bunch of changes to improve performance and robustness under load:
- remove special treatment of pager_map mappings in pmaps. this is
required now, since I've removed the globals that expose the address range.
pager_map now uses pmap_kenter_pa() instead of pmap_enter(), so there's
no longer any need to special-case it.
- eliminate struct uvm_vnode by moving its fields into struct vnode.
- rewrite the pageout path. the pager is now responsible for handling the
high-level requests instead of only getting control after a bunch of work
has already been done on its behalf. this will allow us to UBCify LFS,
which needs tighter control over its pages than other filesystems do.
writing a page to disk no longer requires making it read-only, which
allows us to write wired pages without causing all kinds of havoc.
- use a new PG_PAGEOUT flag to indicate that a page should be freed
on behalf of the pagedaemon when it's unlocked. this flag is very similar
to PG_RELEASED, but unlike PG_RELEASED, PG_PAGEOUT can be cleared if the
pageout fails due to eg. an indirect-block buffer being locked.
this allows us to remove the "version" field from struct vm_page,
and together with shrinking "loan_count" from 32 bits to 16,
struct vm_page is now 4 bytes smaller.
- no longer use PG_RELEASED for swap-backed pages. if the page is busy
because it's being paged out, we can't release the swap slot to be
reallocated until that write is complete, but unlike with vnodes we
don't keep a count of in-progress writes so there's no good way to
know when the write is done. instead, when we need to free a busy
swap-backed page, just sleep until we can get it busy ourselves.
- implement a fast-path for extending writes which allows us to avoid
zeroing new pages. this substantially reduces cpu usage.
- encapsulate the data used by the genfs code in a struct genfs_node,
which must be the first element of the filesystem-specific vnode data
for filesystems which use genfs_{get,put}pages().
- eliminate many of the UVM pagerops, since they aren't needed anymore
now that the pager "put" operation is a higher-level operation.
- enhance the genfs code to allow NFS to use the genfs_{get,put}pages
instead of a modified copy.
- clean up struct vnode by removing all the fields that used to be used by
the vfs_cluster.c code (which we don't use anymore with UBC).
- remove kmem_object and mb_object since they were useless.
instead of allocating pages to these objects, we now just allocate
pages with no object. such pages are mapped in the kernel until they
are freed, so we can use the mapping to find the page to free it.
this allows us to remove splvm() protection in several places.
The sum of all these changes improves write throughput on my
decstation 5000/200 to within 1% of the rate of NetBSD 1.5
and reduces the elapsed time for "make release" of a NetBSD 1.5
source tree on my 128MB pc to 10% less than a 1.5 kernel took.
2001-09-16 00:36:31 +04:00
|
|
|
"km_pgrm", 0);
|
2020-02-23 18:46:38 +03:00
|
|
|
rw_enter(uobj->vmobjlock, RW_WRITER);
|
a whole bunch of changes to improve performance and robustness under load:
- remove special treatment of pager_map mappings in pmaps. this is
required now, since I've removed the globals that expose the address range.
pager_map now uses pmap_kenter_pa() instead of pmap_enter(), so there's
no longer any need to special-case it.
- eliminate struct uvm_vnode by moving its fields into struct vnode.
- rewrite the pageout path. the pager is now responsible for handling the
high-level requests instead of only getting control after a bunch of work
has already been done on its behalf. this will allow us to UBCify LFS,
which needs tighter control over its pages than other filesystems do.
writing a page to disk no longer requires making it read-only, which
allows us to write wired pages without causing all kinds of havoc.
- use a new PG_PAGEOUT flag to indicate that a page should be freed
on behalf of the pagedaemon when it's unlocked. this flag is very similar
to PG_RELEASED, but unlike PG_RELEASED, PG_PAGEOUT can be cleared if the
pageout fails due to eg. an indirect-block buffer being locked.
this allows us to remove the "version" field from struct vm_page,
and together with shrinking "loan_count" from 32 bits to 16,
struct vm_page is now 4 bytes smaller.
- no longer use PG_RELEASED for swap-backed pages. if the page is busy
because it's being paged out, we can't release the swap slot to be
reallocated until that write is complete, but unlike with vnodes we
don't keep a count of in-progress writes so there's no good way to
know when the write is done. instead, when we need to free a busy
swap-backed page, just sleep until we can get it busy ourselves.
- implement a fast-path for extending writes which allows us to avoid
zeroing new pages. this substantially reduces cpu usage.
- encapsulate the data used by the genfs code in a struct genfs_node,
which must be the first element of the filesystem-specific vnode data
for filesystems which use genfs_{get,put}pages().
- eliminate many of the UVM pagerops, since they aren't needed anymore
now that the pager "put" operation is a higher-level operation.
- enhance the genfs code to allow NFS to use the genfs_{get,put}pages
instead of a modified copy.
- clean up struct vnode by removing all the fields that used to be used by
the vfs_cluster.c code (which we don't use anymore with UBC).
- remove kmem_object and mb_object since they were useless.
instead of allocating pages to these objects, we now just allocate
pages with no object. such pages are mapped in the kernel until they
are freed, so we can use the mapping to find the page to free it.
this allows us to remove splvm() protection in several places.
The sum of all these changes improves write throughput on my
decstation 5000/200 to within 1% of the rate of NetBSD 1.5
and reduces the elapsed time for "make release" of a NetBSD 1.5
source tree on my 128MB pc to 10% less than a 1.5 kernel took.
2001-09-16 00:36:31 +04:00
|
|
|
nextoff = curoff;
|
|
|
|
continue;
|
|
|
|
}
|
1998-03-09 03:58:55 +03:00
|
|
|
|
a whole bunch of changes to improve performance and robustness under load:
- remove special treatment of pager_map mappings in pmaps. this is
required now, since I've removed the globals that expose the address range.
pager_map now uses pmap_kenter_pa() instead of pmap_enter(), so there's
no longer any need to special-case it.
- eliminate struct uvm_vnode by moving its fields into struct vnode.
- rewrite the pageout path. the pager is now responsible for handling the
high-level requests instead of only getting control after a bunch of work
has already been done on its behalf. this will allow us to UBCify LFS,
which needs tighter control over its pages than other filesystems do.
writing a page to disk no longer requires making it read-only, which
allows us to write wired pages without causing all kinds of havoc.
- use a new PG_PAGEOUT flag to indicate that a page should be freed
on behalf of the pagedaemon when it's unlocked. this flag is very similar
to PG_RELEASED, but unlike PG_RELEASED, PG_PAGEOUT can be cleared if the
pageout fails due to eg. an indirect-block buffer being locked.
this allows us to remove the "version" field from struct vm_page,
and together with shrinking "loan_count" from 32 bits to 16,
struct vm_page is now 4 bytes smaller.
- no longer use PG_RELEASED for swap-backed pages. if the page is busy
because it's being paged out, we can't release the swap slot to be
reallocated until that write is complete, but unlike with vnodes we
don't keep a count of in-progress writes so there's no good way to
know when the write is done. instead, when we need to free a busy
swap-backed page, just sleep until we can get it busy ourselves.
- implement a fast-path for extending writes which allows us to avoid
zeroing new pages. this substantially reduces cpu usage.
- encapsulate the data used by the genfs code in a struct genfs_node,
which must be the first element of the filesystem-specific vnode data
for filesystems which use genfs_{get,put}pages().
- eliminate many of the UVM pagerops, since they aren't needed anymore
now that the pager "put" operation is a higher-level operation.
- enhance the genfs code to allow NFS to use the genfs_{get,put}pages
instead of a modified copy.
- clean up struct vnode by removing all the fields that used to be used by
the vfs_cluster.c code (which we don't use anymore with UBC).
- remove kmem_object and mb_object since they were useless.
instead of allocating pages to these objects, we now just allocate
pages with no object. such pages are mapped in the kernel until they
are freed, so we can use the mapping to find the page to free it.
this allows us to remove splvm() protection in several places.
The sum of all these changes improves write throughput on my
decstation 5000/200 to within 1% of the rate of NetBSD 1.5
and reduces the elapsed time for "make release" of a NetBSD 1.5
source tree on my 128MB pc to 10% less than a 1.5 kernel took.
2001-09-16 00:36:31 +04:00
|
|
|
/*
|
|
|
|
* free the swap slot, then the page.
|
|
|
|
*/
|
1999-05-26 00:30:08 +04:00
|
|
|
|
2001-11-06 11:07:49 +03:00
|
|
|
if (pg == NULL &&
|
2003-08-28 17:12:17 +04:00
|
|
|
uao_find_swslot(uobj, curoff >> PAGE_SHIFT) > 0) {
|
2001-11-06 11:07:49 +03:00
|
|
|
swpgonlydelta++;
|
a whole bunch of changes to improve performance and robustness under load:
- remove special treatment of pager_map mappings in pmaps. this is
required now, since I've removed the globals that expose the address range.
pager_map now uses pmap_kenter_pa() instead of pmap_enter(), so there's
no longer any need to special-case it.
- eliminate struct uvm_vnode by moving its fields into struct vnode.
- rewrite the pageout path. the pager is now responsible for handling the
high-level requests instead of only getting control after a bunch of work
has already been done on its behalf. this will allow us to UBCify LFS,
which needs tighter control over its pages than other filesystems do.
writing a page to disk no longer requires making it read-only, which
allows us to write wired pages without causing all kinds of havoc.
- use a new PG_PAGEOUT flag to indicate that a page should be freed
on behalf of the pagedaemon when it's unlocked. this flag is very similar
to PG_RELEASED, but unlike PG_RELEASED, PG_PAGEOUT can be cleared if the
pageout fails due to eg. an indirect-block buffer being locked.
this allows us to remove the "version" field from struct vm_page,
and together with shrinking "loan_count" from 32 bits to 16,
struct vm_page is now 4 bytes smaller.
- no longer use PG_RELEASED for swap-backed pages. if the page is busy
because it's being paged out, we can't release the swap slot to be
reallocated until that write is complete, but unlike with vnodes we
don't keep a count of in-progress writes so there's no good way to
know when the write is done. instead, when we need to free a busy
swap-backed page, just sleep until we can get it busy ourselves.
- implement a fast-path for extending writes which allows us to avoid
zeroing new pages. this substantially reduces cpu usage.
- encapsulate the data used by the genfs code in a struct genfs_node,
which must be the first element of the filesystem-specific vnode data
for filesystems which use genfs_{get,put}pages().
- eliminate many of the UVM pagerops, since they aren't needed anymore
now that the pager "put" operation is a higher-level operation.
- enhance the genfs code to allow NFS to use the genfs_{get,put}pages
instead of a modified copy.
- clean up struct vnode by removing all the fields that used to be used by
the vfs_cluster.c code (which we don't use anymore with UBC).
- remove kmem_object and mb_object since they were useless.
instead of allocating pages to these objects, we now just allocate
pages with no object. such pages are mapped in the kernel until they
are freed, so we can use the mapping to find the page to free it.
this allows us to remove splvm() protection in several places.
The sum of all these changes improves write throughput on my
decstation 5000/200 to within 1% of the rate of NetBSD 1.5
and reduces the elapsed time for "make release" of a NetBSD 1.5
source tree on my 128MB pc to 10% less than a 1.5 kernel took.
2001-09-16 00:36:31 +04:00
|
|
|
}
|
2001-11-06 11:07:49 +03:00
|
|
|
uao_dropswap(uobj, curoff >> PAGE_SHIFT);
|
|
|
|
if (pg != NULL) {
|
|
|
|
uvm_pagefree(pg);
|
1998-03-09 03:58:55 +03:00
|
|
|
}
|
1999-05-26 00:30:08 +04:00
|
|
|
}
|
2020-02-23 18:46:38 +03:00
|
|
|
rw_exit(uobj->vmobjlock);
|
2001-11-06 11:07:49 +03:00
|
|
|
|
2001-11-07 11:43:32 +03:00
|
|
|
if (swpgonlydelta > 0) {
|
2019-12-01 17:43:26 +03:00
|
|
|
KASSERT(uvmexp.swpgonly >= swpgonlydelta);
|
2019-12-01 17:40:31 +03:00
|
|
|
atomic_add_int(&uvmexp.swpgonly, -swpgonlydelta);
|
2001-11-07 11:43:32 +03:00
|
|
|
}
|
1999-05-26 00:30:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2005-04-01 15:59:21 +04:00
|
|
|
* uvm_km_pgremove_intrsafe: like uvm_km_pgremove(), but for non object backed
|
|
|
|
* regions.
|
1999-05-26 00:30:08 +04:00
|
|
|
*
|
|
|
|
* => when you unmap a part of anonymous kernel memory you want to toss
|
a whole bunch of changes to improve performance and robustness under load:
- remove special treatment of pager_map mappings in pmaps. this is
required now, since I've removed the globals that expose the address range.
pager_map now uses pmap_kenter_pa() instead of pmap_enter(), so there's
no longer any need to special-case it.
- eliminate struct uvm_vnode by moving its fields into struct vnode.
- rewrite the pageout path. the pager is now responsible for handling the
high-level requests instead of only getting control after a bunch of work
has already been done on its behalf. this will allow us to UBCify LFS,
which needs tighter control over its pages than other filesystems do.
writing a page to disk no longer requires making it read-only, which
allows us to write wired pages without causing all kinds of havoc.
- use a new PG_PAGEOUT flag to indicate that a page should be freed
on behalf of the pagedaemon when it's unlocked. this flag is very similar
to PG_RELEASED, but unlike PG_RELEASED, PG_PAGEOUT can be cleared if the
pageout fails due to eg. an indirect-block buffer being locked.
this allows us to remove the "version" field from struct vm_page,
and together with shrinking "loan_count" from 32 bits to 16,
struct vm_page is now 4 bytes smaller.
- no longer use PG_RELEASED for swap-backed pages. if the page is busy
because it's being paged out, we can't release the swap slot to be
reallocated until that write is complete, but unlike with vnodes we
don't keep a count of in-progress writes so there's no good way to
know when the write is done. instead, when we need to free a busy
swap-backed page, just sleep until we can get it busy ourselves.
- implement a fast-path for extending writes which allows us to avoid
zeroing new pages. this substantially reduces cpu usage.
- encapsulate the data used by the genfs code in a struct genfs_node,
which must be the first element of the filesystem-specific vnode data
for filesystems which use genfs_{get,put}pages().
- eliminate many of the UVM pagerops, since they aren't needed anymore
now that the pager "put" operation is a higher-level operation.
- enhance the genfs code to allow NFS to use the genfs_{get,put}pages
instead of a modified copy.
- clean up struct vnode by removing all the fields that used to be used by
the vfs_cluster.c code (which we don't use anymore with UBC).
- remove kmem_object and mb_object since they were useless.
instead of allocating pages to these objects, we now just allocate
pages with no object. such pages are mapped in the kernel until they
are freed, so we can use the mapping to find the page to free it.
this allows us to remove splvm() protection in several places.
The sum of all these changes improves write throughput on my
decstation 5000/200 to within 1% of the rate of NetBSD 1.5
and reduces the elapsed time for "make release" of a NetBSD 1.5
source tree on my 128MB pc to 10% less than a 1.5 kernel took.
2001-09-16 00:36:31 +04:00
|
|
|
* the pages right away. (this is called from uvm_unmap_...).
|
1999-05-26 00:30:08 +04:00
|
|
|
* => none of the pages will ever be busy, and none of them will ever
|
a whole bunch of changes to improve performance and robustness under load:
- remove special treatment of pager_map mappings in pmaps. this is
required now, since I've removed the globals that expose the address range.
pager_map now uses pmap_kenter_pa() instead of pmap_enter(), so there's
no longer any need to special-case it.
- eliminate struct uvm_vnode by moving its fields into struct vnode.
- rewrite the pageout path. the pager is now responsible for handling the
high-level requests instead of only getting control after a bunch of work
has already been done on its behalf. this will allow us to UBCify LFS,
which needs tighter control over its pages than other filesystems do.
writing a page to disk no longer requires making it read-only, which
allows us to write wired pages without causing all kinds of havoc.
- use a new PG_PAGEOUT flag to indicate that a page should be freed
on behalf of the pagedaemon when it's unlocked. this flag is very similar
to PG_RELEASED, but unlike PG_RELEASED, PG_PAGEOUT can be cleared if the
pageout fails due to eg. an indirect-block buffer being locked.
this allows us to remove the "version" field from struct vm_page,
and together with shrinking "loan_count" from 32 bits to 16,
struct vm_page is now 4 bytes smaller.
- no longer use PG_RELEASED for swap-backed pages. if the page is busy
because it's being paged out, we can't release the swap slot to be
reallocated until that write is complete, but unlike with vnodes we
don't keep a count of in-progress writes so there's no good way to
know when the write is done. instead, when we need to free a busy
swap-backed page, just sleep until we can get it busy ourselves.
- implement a fast-path for extending writes which allows us to avoid
zeroing new pages. this substantially reduces cpu usage.
- encapsulate the data used by the genfs code in a struct genfs_node,
which must be the first element of the filesystem-specific vnode data
for filesystems which use genfs_{get,put}pages().
- eliminate many of the UVM pagerops, since they aren't needed anymore
now that the pager "put" operation is a higher-level operation.
- enhance the genfs code to allow NFS to use the genfs_{get,put}pages
instead of a modified copy.
- clean up struct vnode by removing all the fields that used to be used by
the vfs_cluster.c code (which we don't use anymore with UBC).
- remove kmem_object and mb_object since they were useless.
instead of allocating pages to these objects, we now just allocate
pages with no object. such pages are mapped in the kernel until they
are freed, so we can use the mapping to find the page to free it.
this allows us to remove splvm() protection in several places.
The sum of all these changes improves write throughput on my
decstation 5000/200 to within 1% of the rate of NetBSD 1.5
and reduces the elapsed time for "make release" of a NetBSD 1.5
source tree on my 128MB pc to 10% less than a 1.5 kernel took.
2001-09-16 00:36:31 +04:00
|
|
|
* be on the active or inactive queues (because they have no object).
|
1999-05-26 00:30:08 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2008-12-01 13:54:57 +03:00
|
|
|
uvm_km_pgremove_intrsafe(struct vm_map *map, vaddr_t start, vaddr_t end)
|
1999-05-26 00:30:08 +04:00
|
|
|
{
|
2012-02-20 23:14:23 +04:00
|
|
|
#define __PGRM_BATCH 16
|
a whole bunch of changes to improve performance and robustness under load:
- remove special treatment of pager_map mappings in pmaps. this is
required now, since I've removed the globals that expose the address range.
pager_map now uses pmap_kenter_pa() instead of pmap_enter(), so there's
no longer any need to special-case it.
- eliminate struct uvm_vnode by moving its fields into struct vnode.
- rewrite the pageout path. the pager is now responsible for handling the
high-level requests instead of only getting control after a bunch of work
has already been done on its behalf. this will allow us to UBCify LFS,
which needs tighter control over its pages than other filesystems do.
writing a page to disk no longer requires making it read-only, which
allows us to write wired pages without causing all kinds of havoc.
- use a new PG_PAGEOUT flag to indicate that a page should be freed
on behalf of the pagedaemon when it's unlocked. this flag is very similar
to PG_RELEASED, but unlike PG_RELEASED, PG_PAGEOUT can be cleared if the
pageout fails due to eg. an indirect-block buffer being locked.
this allows us to remove the "version" field from struct vm_page,
and together with shrinking "loan_count" from 32 bits to 16,
struct vm_page is now 4 bytes smaller.
- no longer use PG_RELEASED for swap-backed pages. if the page is busy
because it's being paged out, we can't release the swap slot to be
reallocated until that write is complete, but unlike with vnodes we
don't keep a count of in-progress writes so there's no good way to
know when the write is done. instead, when we need to free a busy
swap-backed page, just sleep until we can get it busy ourselves.
- implement a fast-path for extending writes which allows us to avoid
zeroing new pages. this substantially reduces cpu usage.
- encapsulate the data used by the genfs code in a struct genfs_node,
which must be the first element of the filesystem-specific vnode data
for filesystems which use genfs_{get,put}pages().
- eliminate many of the UVM pagerops, since they aren't needed anymore
now that the pager "put" operation is a higher-level operation.
- enhance the genfs code to allow NFS to use the genfs_{get,put}pages
instead of a modified copy.
- clean up struct vnode by removing all the fields that used to be used by
the vfs_cluster.c code (which we don't use anymore with UBC).
- remove kmem_object and mb_object since they were useless.
instead of allocating pages to these objects, we now just allocate
pages with no object. such pages are mapped in the kernel until they
are freed, so we can use the mapping to find the page to free it.
this allows us to remove splvm() protection in several places.
The sum of all these changes improves write throughput on my
decstation 5000/200 to within 1% of the rate of NetBSD 1.5
and reduces the elapsed time for "make release" of a NetBSD 1.5
source tree on my 128MB pc to 10% less than a 1.5 kernel took.
2001-09-16 00:36:31 +04:00
|
|
|
struct vm_page *pg;
|
2012-02-20 23:14:23 +04:00
|
|
|
paddr_t pa[__PGRM_BATCH];
|
|
|
|
int npgrm, i;
|
|
|
|
vaddr_t va, batch_vastart;
|
|
|
|
|
2012-02-03 23:25:07 +04:00
|
|
|
UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
|
1999-05-26 00:30:08 +04:00
|
|
|
|
2008-12-01 13:54:57 +03:00
|
|
|
KASSERT(VM_MAP_IS_KERNEL(map));
|
2012-07-09 15:19:34 +04:00
|
|
|
KASSERTMSG(vm_map_min(map) <= start,
|
|
|
|
"vm_map_min(map) [%#"PRIxVADDR"] <= start [%#"PRIxVADDR"]"
|
|
|
|
" (size=%#"PRIxVSIZE")",
|
|
|
|
vm_map_min(map), start, end - start);
|
2005-04-01 15:59:21 +04:00
|
|
|
KASSERT(start < end);
|
2008-12-01 13:54:57 +03:00
|
|
|
KASSERT(end <= vm_map_max(map));
|
2005-04-01 15:59:21 +04:00
|
|
|
|
2012-02-20 23:14:23 +04:00
|
|
|
for (va = start; va < end;) {
|
|
|
|
batch_vastart = va;
|
|
|
|
/* create a batch of at most __PGRM_BATCH pages to free */
|
|
|
|
for (i = 0;
|
|
|
|
i < __PGRM_BATCH && va < end;
|
|
|
|
va += PAGE_SIZE) {
|
|
|
|
if (!pmap_extract(pmap_kernel(), va, &pa[i])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
npgrm = i;
|
|
|
|
/* now remove the mappings */
|
2012-03-13 01:37:12 +04:00
|
|
|
pmap_kremove(batch_vastart, va - batch_vastart);
|
2012-02-20 23:14:23 +04:00
|
|
|
/* and free the pages */
|
|
|
|
for (i = 0; i < npgrm; i++) {
|
|
|
|
pg = PHYS_TO_VM_PAGE(pa[i]);
|
|
|
|
KASSERT(pg);
|
|
|
|
KASSERT(pg->uobject == NULL && pg->uanon == NULL);
|
|
|
|
KASSERT((pg->flags & PG_BUSY) == 0);
|
|
|
|
uvm_pagefree(pg);
|
2000-11-24 10:07:27 +03:00
|
|
|
}
|
1999-05-26 00:30:08 +04:00
|
|
|
}
|
2012-02-20 23:14:23 +04:00
|
|
|
#undef __PGRM_BATCH
|
1998-02-05 09:25:08 +03:00
|
|
|
}
|
|
|
|
|
2005-04-01 15:59:21 +04:00
|
|
|
#if defined(DEBUG)
|
|
|
|
void
|
2008-12-01 13:54:57 +03:00
|
|
|
uvm_km_check_empty(struct vm_map *map, vaddr_t start, vaddr_t end)
|
2005-04-01 15:59:21 +04:00
|
|
|
{
|
|
|
|
vaddr_t va;
|
2012-02-03 23:25:07 +04:00
|
|
|
UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
|
2005-04-01 15:59:21 +04:00
|
|
|
|
2008-12-01 13:54:57 +03:00
|
|
|
KDASSERT(VM_MAP_IS_KERNEL(map));
|
|
|
|
KDASSERT(vm_map_min(map) <= start);
|
2005-04-01 15:59:21 +04:00
|
|
|
KDASSERT(start < end);
|
2008-12-01 13:54:57 +03:00
|
|
|
KDASSERT(end <= vm_map_max(map));
|
2005-04-01 15:59:21 +04:00
|
|
|
|
|
|
|
for (va = start; va < end; va += PAGE_SIZE) {
|
2019-12-14 20:28:58 +03:00
|
|
|
paddr_t pa;
|
|
|
|
|
2005-04-01 15:59:21 +04:00
|
|
|
if (pmap_extract(pmap_kernel(), va, &pa)) {
|
2005-04-20 18:10:03 +04:00
|
|
|
panic("uvm_km_check_empty: va %p has pa 0x%llx",
|
|
|
|
(void *)va, (long long)pa);
|
2005-04-01 15:59:21 +04:00
|
|
|
}
|
2019-12-14 20:28:58 +03:00
|
|
|
/*
|
|
|
|
* kernel_object should not have pages for the corresponding
|
|
|
|
* region. check it.
|
|
|
|
*
|
|
|
|
* why trylock? because:
|
|
|
|
* - caller might not want to block.
|
|
|
|
* - we can recurse when allocating radix_node for
|
|
|
|
* kernel_object.
|
|
|
|
*/
|
2020-02-23 18:46:38 +03:00
|
|
|
if (rw_tryenter(uvm_kernel_object->vmobjlock, RW_WRITER)) {
|
2019-12-14 20:28:58 +03:00
|
|
|
struct vm_page *pg;
|
|
|
|
|
|
|
|
pg = uvm_pagelookup(uvm_kernel_object,
|
|
|
|
va - vm_map_min(kernel_map));
|
2020-02-23 18:46:38 +03:00
|
|
|
rw_exit(uvm_kernel_object->vmobjlock);
|
2019-12-14 20:28:58 +03:00
|
|
|
if (pg) {
|
|
|
|
panic("uvm_km_check_empty: "
|
|
|
|
"has page hashed at %p",
|
|
|
|
(const void *)va);
|
|
|
|
}
|
2005-04-01 15:59:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* defined(DEBUG) */
|
1998-02-05 09:25:08 +03:00
|
|
|
|
|
|
|
/*
|
2005-04-01 15:59:21 +04:00
|
|
|
* uvm_km_alloc: allocate an area of kernel memory.
|
1998-02-05 09:25:08 +03:00
|
|
|
*
|
2005-04-01 15:59:21 +04:00
|
|
|
* => NOTE: we can return 0 even if we can wait if there is not enough
|
1998-02-05 09:25:08 +03:00
|
|
|
* free VM space in the map... caller should be prepared to handle
|
|
|
|
* this case.
|
|
|
|
* => we return KVA of memory allocated
|
|
|
|
*/
|
|
|
|
|
1998-08-13 06:10:37 +04:00
|
|
|
vaddr_t
|
2005-06-27 06:19:48 +04:00
|
|
|
uvm_km_alloc(struct vm_map *map, vsize_t size, vsize_t align, uvm_flag_t flags)
|
1998-02-05 09:25:08 +03:00
|
|
|
{
|
1998-08-13 06:10:37 +04:00
|
|
|
vaddr_t kva, loopva;
|
|
|
|
vaddr_t offset;
|
2001-04-13 01:08:25 +04:00
|
|
|
vsize_t loopsize;
|
1998-03-09 03:58:55 +03:00
|
|
|
struct vm_page *pg;
|
2005-04-01 15:59:21 +04:00
|
|
|
struct uvm_object *obj;
|
|
|
|
int pgaflags;
|
2016-07-27 19:45:00 +03:00
|
|
|
vm_prot_t prot, vaprot;
|
2005-04-01 15:59:21 +04:00
|
|
|
UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
|
1998-02-05 09:25:08 +03:00
|
|
|
|
2000-11-24 10:07:27 +03:00
|
|
|
KASSERT(vm_map_pmap(map) == pmap_kernel());
|
2005-04-01 15:59:21 +04:00
|
|
|
KASSERT((flags & UVM_KMF_TYPEMASK) == UVM_KMF_WIRED ||
|
|
|
|
(flags & UVM_KMF_TYPEMASK) == UVM_KMF_PAGEABLE ||
|
|
|
|
(flags & UVM_KMF_TYPEMASK) == UVM_KMF_VAONLY);
|
2011-09-01 10:40:28 +04:00
|
|
|
KASSERT((flags & UVM_KMF_VAONLY) != 0 || (flags & UVM_KMF_COLORMATCH) == 0);
|
|
|
|
KASSERT((flags & UVM_KMF_COLORMATCH) == 0 || (flags & UVM_KMF_VAONLY) != 0);
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
|
|
|
* setup for call
|
|
|
|
*/
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
kva = vm_map_min(map); /* hint */
|
2005-04-01 15:59:21 +04:00
|
|
|
size = round_page(size);
|
2007-07-21 23:21:53 +04:00
|
|
|
obj = (flags & UVM_KMF_PAGEABLE) ? uvm_kernel_object : NULL;
|
Update the kernhist(9) kernel history code to address issues identified
in PR kern/52639, as well as some general cleaning-up...
(As proposed on tech-kern@ with additional changes and enhancements.)
Details of changes:
* All history arguments are now stored as uintmax_t values[1], both in
the kernel and in the structures used for exporting the history data
to userland via sysctl(9). This avoids problems on some architectures
where passing a 64-bit (or larger) value to printf(3) can cause it to
process the value as multiple arguments. (This can be particularly
problematic when printf()'s format string is not a literal, since in
that case the compiler cannot know how large each argument should be.)
* Update the data structures used for exporting kernel history data to
include a version number as well as the length of history arguments.
* All [2] existing users of kernhist(9) have had their format strings
updated. Each format specifier now includes an explicit length
modifier 'j' to refer to numeric values of the size of uintmax_t.
* All [2] existing users of kernhist(9) have had their format strings
updated to replace uses of "%p" with "%#jx", and the pointer
arguments are now cast to (uintptr_t) before being subsequently cast
to (uintmax_t). This is needed to avoid compiler warnings about
casting "pointer to integer of a different size."
* All [2] existing users of kernhist(9) have had instances of "%s" or
"%c" format strings replaced with numeric formats; several instances
of mis-match between format string and argument list have been fixed.
* vmstat(1) has been modified to handle the new size of arguments in the
history data as exported by sysctl(9).
* vmstat(1) now provides a warning message if the history requested with
the -u option does not exist (previously, this condition was silently
ignored, with only a single blank line being printed).
* vmstat(1) now checks the version and argument length included in the
data exported via sysctl(9) and exits if they do not match the values
with which vmstat was built.
* The kernhist(9) man-page has been updated to note the additional
requirements imposed on the format strings, along with several other
minor changes and enhancements.
[1] It would have been possible to use an explicit length (for example,
uint64_t) for the history arguments. But that would require another
"rototill" of all the users in the future when we add support for an
architecture that supports a larger size. Also, the printf(3) format
specifiers for explicitly-sized values, such as "%"PRIu64, are much
more verbose (and less aesthetically appealing, IMHO) than simply
using "%ju".
[2] I've tried very hard to find "all [the] existing users of kernhist(9)"
but it is possible that I've missed some of them. I would be glad to
update any stragglers that anyone identifies.
2017-10-28 03:37:11 +03:00
|
|
|
UVMHIST_LOG(maphist," (map=0x%#jx, obj=0x%#jx, size=0x%jx, flags=%jd)",
|
|
|
|
(uintptr_t)map, (uintptr_t)obj, size, flags);
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
|
|
|
* allocate some virtual space
|
|
|
|
*/
|
1998-02-05 09:25:08 +03:00
|
|
|
|
2016-07-27 19:45:00 +03:00
|
|
|
vaprot = (flags & UVM_KMF_EXEC) ? UVM_PROT_ALL : UVM_PROT_RW;
|
2005-04-01 15:59:21 +04:00
|
|
|
if (__predict_false(uvm_map(map, &kva, size, obj, UVM_UNKNOWN_OFFSET,
|
2016-07-27 19:45:00 +03:00
|
|
|
align, UVM_MAPFLAG(vaprot, UVM_PROT_ALL, UVM_INH_NONE,
|
2005-04-01 15:59:21 +04:00
|
|
|
UVM_ADV_RANDOM,
|
2011-09-01 10:40:28 +04:00
|
|
|
(flags & (UVM_KMF_TRYLOCK | UVM_KMF_NOWAIT | UVM_KMF_WAITVA
|
2012-01-27 23:48:38 +04:00
|
|
|
| UVM_KMF_COLORMATCH)))) != 0)) {
|
1998-03-09 03:58:55 +03:00
|
|
|
UVMHIST_LOG(maphist, "<- done (no VM)",0,0,0,0);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if all we wanted was VA, return now
|
|
|
|
*/
|
|
|
|
|
2005-04-01 15:59:21 +04:00
|
|
|
if (flags & (UVM_KMF_VAONLY | UVM_KMF_PAGEABLE)) {
|
Update the kernhist(9) kernel history code to address issues identified
in PR kern/52639, as well as some general cleaning-up...
(As proposed on tech-kern@ with additional changes and enhancements.)
Details of changes:
* All history arguments are now stored as uintmax_t values[1], both in
the kernel and in the structures used for exporting the history data
to userland via sysctl(9). This avoids problems on some architectures
where passing a 64-bit (or larger) value to printf(3) can cause it to
process the value as multiple arguments. (This can be particularly
problematic when printf()'s format string is not a literal, since in
that case the compiler cannot know how large each argument should be.)
* Update the data structures used for exporting kernel history data to
include a version number as well as the length of history arguments.
* All [2] existing users of kernhist(9) have had their format strings
updated. Each format specifier now includes an explicit length
modifier 'j' to refer to numeric values of the size of uintmax_t.
* All [2] existing users of kernhist(9) have had their format strings
updated to replace uses of "%p" with "%#jx", and the pointer
arguments are now cast to (uintptr_t) before being subsequently cast
to (uintmax_t). This is needed to avoid compiler warnings about
casting "pointer to integer of a different size."
* All [2] existing users of kernhist(9) have had instances of "%s" or
"%c" format strings replaced with numeric formats; several instances
of mis-match between format string and argument list have been fixed.
* vmstat(1) has been modified to handle the new size of arguments in the
history data as exported by sysctl(9).
* vmstat(1) now provides a warning message if the history requested with
the -u option does not exist (previously, this condition was silently
ignored, with only a single blank line being printed).
* vmstat(1) now checks the version and argument length included in the
data exported via sysctl(9) and exits if they do not match the values
with which vmstat was built.
* The kernhist(9) man-page has been updated to note the additional
requirements imposed on the format strings, along with several other
minor changes and enhancements.
[1] It would have been possible to use an explicit length (for example,
uint64_t) for the history arguments. But that would require another
"rototill" of all the users in the future when we add support for an
architecture that supports a larger size. Also, the printf(3) format
specifiers for explicitly-sized values, such as "%"PRIu64, are much
more verbose (and less aesthetically appealing, IMHO) than simply
using "%ju".
[2] I've tried very hard to find "all [the] existing users of kernhist(9)"
but it is possible that I've missed some of them. I would be glad to
update any stragglers that anyone identifies.
2017-10-28 03:37:11 +03:00
|
|
|
UVMHIST_LOG(maphist,"<- done valloc (kva=0x%jx)", kva,0,0,0);
|
1998-03-09 03:58:55 +03:00
|
|
|
return(kva);
|
|
|
|
}
|
2000-11-24 10:07:27 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
|
|
|
* recover object offset from virtual address
|
|
|
|
*/
|
|
|
|
|
|
|
|
offset = kva - vm_map_min(kernel_map);
|
Update the kernhist(9) kernel history code to address issues identified
in PR kern/52639, as well as some general cleaning-up...
(As proposed on tech-kern@ with additional changes and enhancements.)
Details of changes:
* All history arguments are now stored as uintmax_t values[1], both in
the kernel and in the structures used for exporting the history data
to userland via sysctl(9). This avoids problems on some architectures
where passing a 64-bit (or larger) value to printf(3) can cause it to
process the value as multiple arguments. (This can be particularly
problematic when printf()'s format string is not a literal, since in
that case the compiler cannot know how large each argument should be.)
* Update the data structures used for exporting kernel history data to
include a version number as well as the length of history arguments.
* All [2] existing users of kernhist(9) have had their format strings
updated. Each format specifier now includes an explicit length
modifier 'j' to refer to numeric values of the size of uintmax_t.
* All [2] existing users of kernhist(9) have had their format strings
updated to replace uses of "%p" with "%#jx", and the pointer
arguments are now cast to (uintptr_t) before being subsequently cast
to (uintmax_t). This is needed to avoid compiler warnings about
casting "pointer to integer of a different size."
* All [2] existing users of kernhist(9) have had instances of "%s" or
"%c" format strings replaced with numeric formats; several instances
of mis-match between format string and argument list have been fixed.
* vmstat(1) has been modified to handle the new size of arguments in the
history data as exported by sysctl(9).
* vmstat(1) now provides a warning message if the history requested with
the -u option does not exist (previously, this condition was silently
ignored, with only a single blank line being printed).
* vmstat(1) now checks the version and argument length included in the
data exported via sysctl(9) and exits if they do not match the values
with which vmstat was built.
* The kernhist(9) man-page has been updated to note the additional
requirements imposed on the format strings, along with several other
minor changes and enhancements.
[1] It would have been possible to use an explicit length (for example,
uint64_t) for the history arguments. But that would require another
"rototill" of all the users in the future when we add support for an
architecture that supports a larger size. Also, the printf(3) format
specifiers for explicitly-sized values, such as "%"PRIu64, are much
more verbose (and less aesthetically appealing, IMHO) than simply
using "%ju".
[2] I've tried very hard to find "all [the] existing users of kernhist(9)"
but it is possible that I've missed some of them. I would be glad to
update any stragglers that anyone identifies.
2017-10-28 03:37:11 +03:00
|
|
|
UVMHIST_LOG(maphist, " kva=0x%jx, offset=0x%jx", kva, offset,0,0);
|
1998-03-09 03:58:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* now allocate and map in the memory... note that we are the only ones
|
|
|
|
* whom should ever get a handle on this area of VM.
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopva = kva;
|
2001-04-13 01:08:25 +04:00
|
|
|
loopsize = size;
|
2005-04-01 15:59:21 +04:00
|
|
|
|
2011-01-04 11:26:33 +03:00
|
|
|
pgaflags = UVM_FLAG_COLORMATCH;
|
2008-12-13 14:34:43 +03:00
|
|
|
if (flags & UVM_KMF_NOWAIT)
|
|
|
|
pgaflags |= UVM_PGA_USERESERVE;
|
2005-04-01 15:59:21 +04:00
|
|
|
if (flags & UVM_KMF_ZERO)
|
|
|
|
pgaflags |= UVM_PGA_ZERO;
|
2006-07-05 18:26:42 +04:00
|
|
|
prot = VM_PROT_READ | VM_PROT_WRITE;
|
|
|
|
if (flags & UVM_KMF_EXEC)
|
|
|
|
prot |= VM_PROT_EXECUTE;
|
2001-04-13 01:08:25 +04:00
|
|
|
while (loopsize) {
|
2012-01-31 04:30:52 +04:00
|
|
|
KASSERTMSG(!pmap_extract(pmap_kernel(), loopva, NULL),
|
|
|
|
"loopva=%#"PRIxVADDR, loopva);
|
2005-04-01 15:59:21 +04:00
|
|
|
|
2011-01-04 11:26:33 +03:00
|
|
|
pg = uvm_pagealloc_strat(NULL, offset, NULL, pgaflags,
|
|
|
|
#ifdef UVM_KM_VMFREELIST
|
|
|
|
UVM_PGA_STRAT_ONLY, UVM_KM_VMFREELIST
|
|
|
|
#else
|
|
|
|
UVM_PGA_STRAT_NORMAL, 0
|
|
|
|
#endif
|
|
|
|
);
|
2001-05-25 08:06:11 +04:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
|
|
|
* out of memory?
|
|
|
|
*/
|
|
|
|
|
2000-05-09 03:10:20 +04:00
|
|
|
if (__predict_false(pg == NULL)) {
|
2002-09-15 20:54:26 +04:00
|
|
|
if ((flags & UVM_KMF_NOWAIT) ||
|
2005-04-12 17:11:45 +04:00
|
|
|
((flags & UVM_KMF_CANFAIL) && !uvm_reclaimable())) {
|
1998-03-09 03:58:55 +03:00
|
|
|
/* free everything! */
|
2005-04-01 15:59:21 +04:00
|
|
|
uvm_km_free(map, kva, size,
|
|
|
|
flags & UVM_KMF_TYPEMASK);
|
2002-09-15 20:54:26 +04:00
|
|
|
return (0);
|
1998-03-09 03:58:55 +03:00
|
|
|
} else {
|
|
|
|
uvm_wait("km_getwait2"); /* sleep here */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2001-05-25 08:06:11 +04:00
|
|
|
|
2005-04-01 15:59:21 +04:00
|
|
|
pg->flags &= ~PG_BUSY; /* new page */
|
|
|
|
UVM_PAGE_OWN(pg, NULL);
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
a whole bunch of changes to improve performance and robustness under load:
- remove special treatment of pager_map mappings in pmaps. this is
required now, since I've removed the globals that expose the address range.
pager_map now uses pmap_kenter_pa() instead of pmap_enter(), so there's
no longer any need to special-case it.
- eliminate struct uvm_vnode by moving its fields into struct vnode.
- rewrite the pageout path. the pager is now responsible for handling the
high-level requests instead of only getting control after a bunch of work
has already been done on its behalf. this will allow us to UBCify LFS,
which needs tighter control over its pages than other filesystems do.
writing a page to disk no longer requires making it read-only, which
allows us to write wired pages without causing all kinds of havoc.
- use a new PG_PAGEOUT flag to indicate that a page should be freed
on behalf of the pagedaemon when it's unlocked. this flag is very similar
to PG_RELEASED, but unlike PG_RELEASED, PG_PAGEOUT can be cleared if the
pageout fails due to eg. an indirect-block buffer being locked.
this allows us to remove the "version" field from struct vm_page,
and together with shrinking "loan_count" from 32 bits to 16,
struct vm_page is now 4 bytes smaller.
- no longer use PG_RELEASED for swap-backed pages. if the page is busy
because it's being paged out, we can't release the swap slot to be
reallocated until that write is complete, but unlike with vnodes we
don't keep a count of in-progress writes so there's no good way to
know when the write is done. instead, when we need to free a busy
swap-backed page, just sleep until we can get it busy ourselves.
- implement a fast-path for extending writes which allows us to avoid
zeroing new pages. this substantially reduces cpu usage.
- encapsulate the data used by the genfs code in a struct genfs_node,
which must be the first element of the filesystem-specific vnode data
for filesystems which use genfs_{get,put}pages().
- eliminate many of the UVM pagerops, since they aren't needed anymore
now that the pager "put" operation is a higher-level operation.
- enhance the genfs code to allow NFS to use the genfs_{get,put}pages
instead of a modified copy.
- clean up struct vnode by removing all the fields that used to be used by
the vfs_cluster.c code (which we don't use anymore with UBC).
- remove kmem_object and mb_object since they were useless.
instead of allocating pages to these objects, we now just allocate
pages with no object. such pages are mapped in the kernel until they
are freed, so we can use the mapping to find the page to free it.
this allows us to remove splvm() protection in several places.
The sum of all these changes improves write throughput on my
decstation 5000/200 to within 1% of the rate of NetBSD 1.5
and reduces the elapsed time for "make release" of a NetBSD 1.5
source tree on my 128MB pc to 10% less than a 1.5 kernel took.
2001-09-16 00:36:31 +04:00
|
|
|
* map it in
|
1998-03-09 03:58:55 +03:00
|
|
|
*/
|
2000-11-24 10:07:27 +03:00
|
|
|
|
2009-11-07 10:27:40 +03:00
|
|
|
pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg),
|
2010-05-14 09:02:05 +04:00
|
|
|
prot, PMAP_KMPAGE);
|
1998-03-09 03:58:55 +03:00
|
|
|
loopva += PAGE_SIZE;
|
|
|
|
offset += PAGE_SIZE;
|
2001-04-13 01:08:25 +04:00
|
|
|
loopsize -= PAGE_SIZE;
|
1998-03-09 03:58:55 +03:00
|
|
|
}
|
2004-03-24 10:47:32 +03:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
pmap_update(pmap_kernel());
|
2004-03-24 10:47:32 +03:00
|
|
|
|
2018-12-03 00:00:13 +03:00
|
|
|
if ((flags & UVM_KMF_ZERO) == 0) {
|
Add support for Kernel Memory Sanitizer (kMSan). It detects uninitialized
memory used by the kernel at run time, and just like kASan and kCSan, it
is an excellent feature. It has already detected 38 uninitialized variables
in the kernel during my testing, which I have since discreetly fixed.
We use two shadows:
- "shad", to track uninitialized memory with a bit granularity (1:1).
Each bit set to 1 in the shad corresponds to one uninitialized bit of
real kernel memory.
- "orig", to track the origin of the memory with a 4-byte granularity
(1:1). Each uint32_t cell in the orig indicates the origin of the
associated uint32_t of real kernel memory.
The memory consumption of these shadows is consequent, so at least 4GB of
RAM is recommended to run kMSan.
The compiler inserts calls to specific __msan_* functions on each memory
access, to manage both the shad and the orig and detect uninitialized
memory accesses that change the execution flow (like an "if" on an
uninitialized variable).
We mark as uninit several types of memory buffers (stack, pools, kmem,
malloc, uvm_km), and check each buffer passed to copyout, copyoutstr,
bwrite, if_transmit_lock and DMA operations, to detect uninitialized memory
that leaves the system. This allows us to detect kernel info leaks in a way
that is more efficient and also more user-friendly than KLEAK.
Contrary to kASan, kMSan requires comprehensive coverage, ie we cannot
tolerate having one non-instrumented function, because this could cause
false positives. kMSan cannot instrument ASM functions, so I converted
most of them to __asm__ inlines, which kMSan is able to instrument. Those
that remain receive special treatment.
Contrary to kASan again, kMSan uses a TLS, so we must context-switch this
TLS during interrupts. We use different contexts depending on the interrupt
level.
The orig tracks precisely the origin of a buffer. We use a special encoding
for the orig values, and pack together in each uint32_t cell of the orig:
- a code designating the type of memory (Stack, Pool, etc), and
- a compressed pointer, which points either (1) to a string containing
the name of the variable associated with the cell, or (2) to an area
in the kernel .text section which we resolve to a symbol name + offset.
This encoding allows us not to consume extra memory for associating
information with each cell, and produces a precise output, that can tell
for example the name of an uninitialized variable on the stack, the
function in which it was pushed on the stack, and the function where we
accessed this uninitialized variable.
kMSan is available with LLVM, but not with GCC.
The code is organized in a way that is similar to kASan and kCSan, so it
means that other architectures than amd64 can be supported.
2019-11-14 19:23:52 +03:00
|
|
|
kmsan_orig((void *)kva, size, KMSAN_TYPE_UVM, __RET_ADDR);
|
|
|
|
kmsan_mark((void *)kva, size, KMSAN_STATE_UNINIT);
|
2018-12-03 00:00:13 +03:00
|
|
|
}
|
|
|
|
|
Update the kernhist(9) kernel history code to address issues identified
in PR kern/52639, as well as some general cleaning-up...
(As proposed on tech-kern@ with additional changes and enhancements.)
Details of changes:
* All history arguments are now stored as uintmax_t values[1], both in
the kernel and in the structures used for exporting the history data
to userland via sysctl(9). This avoids problems on some architectures
where passing a 64-bit (or larger) value to printf(3) can cause it to
process the value as multiple arguments. (This can be particularly
problematic when printf()'s format string is not a literal, since in
that case the compiler cannot know how large each argument should be.)
* Update the data structures used for exporting kernel history data to
include a version number as well as the length of history arguments.
* All [2] existing users of kernhist(9) have had their format strings
updated. Each format specifier now includes an explicit length
modifier 'j' to refer to numeric values of the size of uintmax_t.
* All [2] existing users of kernhist(9) have had their format strings
updated to replace uses of "%p" with "%#jx", and the pointer
arguments are now cast to (uintptr_t) before being subsequently cast
to (uintmax_t). This is needed to avoid compiler warnings about
casting "pointer to integer of a different size."
* All [2] existing users of kernhist(9) have had instances of "%s" or
"%c" format strings replaced with numeric formats; several instances
of mis-match between format string and argument list have been fixed.
* vmstat(1) has been modified to handle the new size of arguments in the
history data as exported by sysctl(9).
* vmstat(1) now provides a warning message if the history requested with
the -u option does not exist (previously, this condition was silently
ignored, with only a single blank line being printed).
* vmstat(1) now checks the version and argument length included in the
data exported via sysctl(9) and exits if they do not match the values
with which vmstat was built.
* The kernhist(9) man-page has been updated to note the additional
requirements imposed on the format strings, along with several other
minor changes and enhancements.
[1] It would have been possible to use an explicit length (for example,
uint64_t) for the history arguments. But that would require another
"rototill" of all the users in the future when we add support for an
architecture that supports a larger size. Also, the printf(3) format
specifiers for explicitly-sized values, such as "%"PRIu64, are much
more verbose (and less aesthetically appealing, IMHO) than simply
using "%ju".
[2] I've tried very hard to find "all [the] existing users of kernhist(9)"
but it is possible that I've missed some of them. I would be glad to
update any stragglers that anyone identifies.
2017-10-28 03:37:11 +03:00
|
|
|
UVMHIST_LOG(maphist,"<- done (kva=0x%jx)", kva,0,0,0);
|
1998-03-09 03:58:55 +03:00
|
|
|
return(kva);
|
1998-02-05 09:25:08 +03:00
|
|
|
}
|
|
|
|
|
2016-07-20 15:38:43 +03:00
|
|
|
/*
|
|
|
|
* uvm_km_protect: change the protection of an allocated area
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
uvm_km_protect(struct vm_map *map, vaddr_t addr, vsize_t size, vm_prot_t prot)
|
|
|
|
{
|
|
|
|
return uvm_map_protect(map, addr, addr + round_page(size), prot, false);
|
|
|
|
}
|
|
|
|
|
1998-02-05 09:25:08 +03:00
|
|
|
/*
|
|
|
|
* uvm_km_free: free an area of kernel memory
|
|
|
|
*/
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
void
|
2005-06-27 06:19:48 +04:00
|
|
|
uvm_km_free(struct vm_map *map, vaddr_t addr, vsize_t size, uvm_flag_t flags)
|
1998-02-05 09:25:08 +03:00
|
|
|
{
|
2012-02-03 23:25:07 +04:00
|
|
|
UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
|
1998-02-05 09:25:08 +03:00
|
|
|
|
2005-04-01 15:59:21 +04:00
|
|
|
KASSERT((flags & UVM_KMF_TYPEMASK) == UVM_KMF_WIRED ||
|
|
|
|
(flags & UVM_KMF_TYPEMASK) == UVM_KMF_PAGEABLE ||
|
|
|
|
(flags & UVM_KMF_TYPEMASK) == UVM_KMF_VAONLY);
|
|
|
|
KASSERT((addr & PAGE_MASK) == 0);
|
2000-11-24 10:07:27 +03:00
|
|
|
KASSERT(vm_map_pmap(map) == pmap_kernel());
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
size = round_page(size);
|
1998-07-25 00:28:48 +04:00
|
|
|
|
2005-04-01 15:59:21 +04:00
|
|
|
if (flags & UVM_KMF_PAGEABLE) {
|
|
|
|
uvm_km_pgremove(addr, addr + size);
|
|
|
|
} else if (flags & UVM_KMF_WIRED) {
|
2011-06-12 07:35:36 +04:00
|
|
|
/*
|
|
|
|
* Note: uvm_km_pgremove_intrsafe() extracts mapping, thus
|
|
|
|
* remove it after. See comment below about KVA visibility.
|
|
|
|
*/
|
2008-12-01 13:54:57 +03:00
|
|
|
uvm_km_pgremove_intrsafe(map, addr, addr + size);
|
2005-04-01 15:59:21 +04:00
|
|
|
}
|
2008-03-24 11:52:55 +03:00
|
|
|
|
|
|
|
/*
|
2011-06-12 07:35:36 +04:00
|
|
|
* Note: uvm_unmap_remove() calls pmap_update() for us, before
|
|
|
|
* KVA becomes globally available.
|
2008-03-24 11:52:55 +03:00
|
|
|
*/
|
2003-12-18 18:02:04 +03:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
uvm_unmap1(map, addr, addr + size, UVM_FLAG_VAONLY);
|
2003-12-18 18:02:04 +03:00
|
|
|
}
|
|
|
|
|
1998-07-25 00:28:48 +04:00
|
|
|
/* Sanity; must specify both or none. */
|
|
|
|
#if (defined(PMAP_MAP_POOLPAGE) || defined(PMAP_UNMAP_POOLPAGE)) && \
|
|
|
|
(!defined(PMAP_MAP_POOLPAGE) || !defined(PMAP_UNMAP_POOLPAGE))
|
|
|
|
#error Must specify MAP and UNMAP together.
|
|
|
|
#endif
|
|
|
|
|
2020-01-20 13:43:48 +03:00
|
|
|
#if defined(PMAP_ALLOC_POOLPAGE) && \
|
|
|
|
!defined(PMAP_MAP_POOLPAGE) && !defined(PMAP_UNMAP_POOLPAGE)
|
|
|
|
#error Must specify ALLOC with MAP and UNMAP
|
|
|
|
#endif
|
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
int
|
|
|
|
uvm_km_kmem_alloc(vmem_t *vm, vmem_size_t size, vm_flag_t flags,
|
|
|
|
vmem_addr_t *addr)
|
2005-01-02 00:08:02 +03:00
|
|
|
{
|
|
|
|
struct vm_page *pg;
|
2012-01-27 23:48:38 +04:00
|
|
|
vmem_addr_t va;
|
|
|
|
int rc;
|
|
|
|
vaddr_t loopva;
|
|
|
|
vsize_t loopsize;
|
2005-01-02 00:08:02 +03:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
size = round_page(size);
|
2005-01-02 00:08:02 +03:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
#if defined(PMAP_MAP_POOLPAGE)
|
|
|
|
if (size == PAGE_SIZE) {
|
2005-01-02 00:08:02 +03:00
|
|
|
again:
|
2012-01-27 23:48:38 +04:00
|
|
|
#ifdef PMAP_ALLOC_POOLPAGE
|
|
|
|
pg = PMAP_ALLOC_POOLPAGE((flags & VM_SLEEP) ?
|
|
|
|
0 : UVM_PGA_USERESERVE);
|
|
|
|
#else
|
|
|
|
pg = uvm_pagealloc(NULL, 0, NULL,
|
|
|
|
(flags & VM_SLEEP) ? 0 : UVM_PGA_USERESERVE);
|
|
|
|
#endif /* PMAP_ALLOC_POOLPAGE */
|
|
|
|
if (__predict_false(pg == NULL)) {
|
|
|
|
if (flags & VM_SLEEP) {
|
|
|
|
uvm_wait("plpg");
|
|
|
|
goto again;
|
|
|
|
}
|
2012-02-26 02:28:06 +04:00
|
|
|
return ENOMEM;
|
2005-01-02 00:08:02 +03:00
|
|
|
}
|
2012-01-27 23:48:38 +04:00
|
|
|
va = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg));
|
2018-11-04 16:48:27 +03:00
|
|
|
KASSERT(va != 0);
|
2012-01-27 23:48:38 +04:00
|
|
|
*addr = va;
|
|
|
|
return 0;
|
2005-01-02 00:08:02 +03:00
|
|
|
}
|
|
|
|
#endif /* PMAP_MAP_POOLPAGE */
|
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
rc = vmem_alloc(vm, size, flags, &va);
|
|
|
|
if (rc != 0)
|
|
|
|
return rc;
|
1998-07-25 00:28:48 +04:00
|
|
|
|
2012-09-03 19:55:42 +04:00
|
|
|
#ifdef PMAP_GROWKERNEL
|
|
|
|
/*
|
2012-09-07 10:45:04 +04:00
|
|
|
* These VA allocations happen independently of uvm_map
|
|
|
|
* so this allocation must not extend beyond the current limit.
|
2012-09-03 19:55:42 +04:00
|
|
|
*/
|
2012-09-07 10:45:04 +04:00
|
|
|
KASSERTMSG(uvm_maxkaddr >= va + size,
|
|
|
|
"%#"PRIxVADDR" %#"PRIxPTR" %#zx",
|
|
|
|
uvm_maxkaddr, va, size);
|
2012-09-03 19:55:42 +04:00
|
|
|
#endif
|
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
loopva = va;
|
|
|
|
loopsize = size;
|
2011-01-04 11:26:33 +03:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
while (loopsize) {
|
2017-03-20 02:44:34 +03:00
|
|
|
paddr_t pa __diagused;
|
2012-07-09 15:19:34 +04:00
|
|
|
KASSERTMSG(!pmap_extract(pmap_kernel(), loopva, &pa),
|
|
|
|
"loopva=%#"PRIxVADDR" loopsize=%#"PRIxVSIZE
|
|
|
|
" pa=%#"PRIxPADDR" vmem=%p",
|
|
|
|
loopva, loopsize, pa, vm);
|
2012-01-27 23:48:38 +04:00
|
|
|
|
2012-01-31 04:30:52 +04:00
|
|
|
pg = uvm_pagealloc(NULL, loopva, NULL,
|
2012-02-01 06:22:27 +04:00
|
|
|
UVM_FLAG_COLORMATCH
|
2012-01-31 04:30:52 +04:00
|
|
|
| ((flags & VM_SLEEP) ? 0 : UVM_PGA_USERESERVE));
|
2012-01-27 23:48:38 +04:00
|
|
|
if (__predict_false(pg == NULL)) {
|
|
|
|
if (flags & VM_SLEEP) {
|
|
|
|
uvm_wait("plpg");
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
uvm_km_pgremove_intrsafe(kernel_map, va,
|
|
|
|
va + size);
|
2012-04-13 19:34:42 +04:00
|
|
|
vmem_free(vm, va, size);
|
2012-01-27 23:48:38 +04:00
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
2012-02-26 02:28:06 +04:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
pg->flags &= ~PG_BUSY; /* new page */
|
|
|
|
UVM_PAGE_OWN(pg, NULL);
|
|
|
|
pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg),
|
|
|
|
VM_PROT_READ|VM_PROT_WRITE, PMAP_KMPAGE);
|
|
|
|
|
|
|
|
loopva += PAGE_SIZE;
|
|
|
|
loopsize -= PAGE_SIZE;
|
1998-08-29 00:05:48 +04:00
|
|
|
}
|
2012-01-27 23:48:38 +04:00
|
|
|
pmap_update(pmap_kernel());
|
1998-08-29 01:16:23 +04:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
*addr = va;
|
1998-07-25 00:28:48 +04:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
return 0;
|
|
|
|
}
|
1998-07-25 00:28:48 +04:00
|
|
|
|
2005-01-02 00:08:02 +03:00
|
|
|
void
|
2012-01-27 23:48:38 +04:00
|
|
|
uvm_km_kmem_free(vmem_t *vm, vmem_addr_t addr, size_t size)
|
2005-01-02 00:08:02 +03:00
|
|
|
{
|
2012-01-27 23:48:38 +04:00
|
|
|
|
|
|
|
size = round_page(size);
|
2005-01-02 00:08:02 +03:00
|
|
|
#if defined(PMAP_UNMAP_POOLPAGE)
|
2012-01-27 23:48:38 +04:00
|
|
|
if (size == PAGE_SIZE) {
|
|
|
|
paddr_t pa;
|
2005-01-02 00:08:02 +03:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
pa = PMAP_UNMAP_POOLPAGE(addr);
|
|
|
|
uvm_pagefree(PHYS_TO_VM_PAGE(pa));
|
2005-01-02 00:08:02 +03:00
|
|
|
return;
|
|
|
|
}
|
2012-01-27 23:48:38 +04:00
|
|
|
#endif /* PMAP_UNMAP_POOLPAGE */
|
|
|
|
uvm_km_pgremove_intrsafe(kernel_map, addr, addr + size);
|
2005-01-02 00:08:02 +03:00
|
|
|
pmap_update(pmap_kernel());
|
2012-01-27 23:48:38 +04:00
|
|
|
|
|
|
|
vmem_free(vm, addr, size);
|
2005-01-02 00:08:02 +03:00
|
|
|
}
|
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
bool
|
|
|
|
uvm_km_va_starved_p(void)
|
1998-07-25 00:28:48 +04:00
|
|
|
{
|
2012-01-27 23:48:38 +04:00
|
|
|
vmem_size_t total;
|
|
|
|
vmem_size_t free;
|
1998-07-25 00:28:48 +04:00
|
|
|
|
2012-09-07 10:45:04 +04:00
|
|
|
if (kmem_arena == NULL)
|
|
|
|
return false;
|
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
total = vmem_size(kmem_arena, VMEM_ALLOC|VMEM_FREE);
|
|
|
|
free = vmem_size(kmem_arena, VMEM_FREE);
|
|
|
|
|
|
|
|
return (free < (total / 10));
|
1998-07-25 00:28:48 +04:00
|
|
|
}
|