1fe267a55SPedro F. Giffuni /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni *
4774d251dSAttilio Rao * Copyright (c) 2013 EMC Corp.
5774d251dSAttilio Rao * Copyright (c) 2011 Jeffrey Roberson <jeff@freebsd.org>
6774d251dSAttilio Rao * Copyright (c) 2008 Mayur Shardul <mayur.shardul@gmail.com>
7774d251dSAttilio Rao * All rights reserved.
8774d251dSAttilio Rao *
9774d251dSAttilio Rao * Redistribution and use in source and binary forms, with or without
10774d251dSAttilio Rao * modification, are permitted provided that the following conditions
11774d251dSAttilio Rao * are met:
12774d251dSAttilio Rao * 1. Redistributions of source code must retain the above copyright
13774d251dSAttilio Rao * notice, this list of conditions and the following disclaimer.
14774d251dSAttilio Rao * 2. Redistributions in binary form must reproduce the above copyright
15774d251dSAttilio Rao * notice, this list of conditions and the following disclaimer in the
16774d251dSAttilio Rao * documentation and/or other materials provided with the distribution.
17774d251dSAttilio Rao *
18774d251dSAttilio Rao * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19774d251dSAttilio Rao * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20774d251dSAttilio Rao * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21774d251dSAttilio Rao * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22774d251dSAttilio Rao * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23774d251dSAttilio Rao * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24774d251dSAttilio Rao * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25774d251dSAttilio Rao * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26774d251dSAttilio Rao * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27774d251dSAttilio Rao * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28774d251dSAttilio Rao * SUCH DAMAGE.
29774d251dSAttilio Rao *
30774d251dSAttilio Rao */
31774d251dSAttilio Rao
32774d251dSAttilio Rao /*
33774d251dSAttilio Rao * Path-compressed radix trie implementation.
34774d251dSAttilio Rao * The following code is not generalized into a general purpose library
35774d251dSAttilio Rao * because there are way too many parameters embedded that should really
36774d251dSAttilio Rao * be decided by the library consumers. At the same time, consumers
37774d251dSAttilio Rao * of this code must achieve highest possible performance.
38774d251dSAttilio Rao *
39774d251dSAttilio Rao * The implementation takes into account the following rationale:
40774d251dSAttilio Rao * - Size of the nodes should be as small as possible but still big enough
41774d251dSAttilio Rao * to avoid a large maximum depth for the trie. This is a balance
42774d251dSAttilio Rao * between the necessity to not wire too much physical memory for the nodes
43774d251dSAttilio Rao * and the necessity to avoid too much cache pollution during the trie
44774d251dSAttilio Rao * operations.
45774d251dSAttilio Rao * - There is not a huge bias toward the number of lookup operations over
46774d251dSAttilio Rao * the number of insert and remove operations. This basically implies
47774d251dSAttilio Rao * that optimizations supposedly helping one operation but hurting the
48774d251dSAttilio Rao * other might be carefully evaluated.
49774d251dSAttilio Rao * - On average not many nodes are expected to be fully populated, hence
50774d251dSAttilio Rao * level compression may just complicate things.
51774d251dSAttilio Rao */
52774d251dSAttilio Rao
53774d251dSAttilio Rao #include <sys/cdefs.h>
54774d251dSAttilio Rao #include "opt_ddb.h"
55774d251dSAttilio Rao
56774d251dSAttilio Rao #include <sys/param.h>
576cec93daSDoug Moore #include <sys/systm.h>
586cec93daSDoug Moore #include <sys/kernel.h>
596cec93daSDoug Moore #include <sys/libkern.h>
60429c871dSDoug Moore #include <sys/pctrie.h>
616cec93daSDoug Moore #include <sys/proc.h>
626cec93daSDoug Moore #include <sys/vmmeter.h>
636cec93daSDoug Moore #include <sys/smr.h>
646cec93daSDoug Moore #include <sys/smr_types.h>
65774d251dSAttilio Rao
66774d251dSAttilio Rao #include <vm/uma.h>
67774d251dSAttilio Rao #include <vm/vm.h>
68774d251dSAttilio Rao #include <vm/vm_radix.h>
69774d251dSAttilio Rao
706cec93daSDoug Moore static uma_zone_t vm_radix_node_zone;
71429c871dSDoug Moore smr_t vm_radix_smr;
726cec93daSDoug Moore
73429c871dSDoug Moore void *
vm_radix_node_alloc(struct pctrie * ptree)74429c871dSDoug Moore vm_radix_node_alloc(struct pctrie *ptree)
75da72505fSDoug Moore {
76429c871dSDoug Moore return (uma_zalloc_smr(vm_radix_node_zone, M_NOWAIT));
77da72505fSDoug Moore }
78da72505fSDoug Moore
79429c871dSDoug Moore void
vm_radix_node_free(struct pctrie * ptree,void * node)80429c871dSDoug Moore vm_radix_node_free(struct pctrie *ptree, void *node)
81da72505fSDoug Moore {
82429c871dSDoug Moore uma_zfree_smr(vm_radix_node_zone, node);
832d2bcba7SDoug Moore }
842d2bcba7SDoug Moore
85*da76d349SBojan Novković #ifndef UMA_USE_DMAP
86ae941b1bSGleb Smirnoff void vm_radix_reserve_kva(void);
87774d251dSAttilio Rao /*
88e946b949SAttilio Rao * Reserve the KVA necessary to satisfy the node allocation.
89e946b949SAttilio Rao * This is mandatory in architectures not supporting direct
90e946b949SAttilio Rao * mapping as they will need otherwise to carve into the kernel maps for
91e946b949SAttilio Rao * every node allocation, resulting into deadlocks for consumers already
92e946b949SAttilio Rao * working with kernel maps.
93774d251dSAttilio Rao */
94ae941b1bSGleb Smirnoff void
vm_radix_reserve_kva(void)95ae941b1bSGleb Smirnoff vm_radix_reserve_kva(void)
96774d251dSAttilio Rao {
97774d251dSAttilio Rao
98880659feSAlan Cox /*
99880659feSAlan Cox * Calculate the number of reserved nodes, discounting the pages that
100880659feSAlan Cox * are needed to store them.
101880659feSAlan Cox */
102e946b949SAttilio Rao if (!uma_zone_reserve_kva(vm_radix_node_zone,
10344f1c916SBryan Drewery ((vm_paddr_t)vm_cnt.v_page_count * PAGE_SIZE) / (PAGE_SIZE +
10410db91ecSDoug Moore pctrie_node_size())))
105e946b949SAttilio Rao panic("%s: unable to reserve KVA", __func__);
106774d251dSAttilio Rao }
107e946b949SAttilio Rao #endif
108774d251dSAttilio Rao
109774d251dSAttilio Rao /*
110774d251dSAttilio Rao * Initialize the UMA slab zone.
111774d251dSAttilio Rao */
112774d251dSAttilio Rao void
vm_radix_zinit(void)113cd1241fbSKonstantin Belousov vm_radix_zinit(void)
114774d251dSAttilio Rao {
115774d251dSAttilio Rao
116429c871dSDoug Moore vm_radix_node_zone = uma_zcreate("RADIX NODE", pctrie_node_size(),
117429c871dSDoug Moore NULL, NULL, pctrie_zone_init, NULL,
118429c871dSDoug Moore PCTRIE_PAD, UMA_ZONE_VM | UMA_ZONE_SMR);
1191ddda2ebSJeff Roberson vm_radix_smr = uma_zone_get_smr(vm_radix_node_zone);
120774d251dSAttilio Rao }
121774d251dSAttilio Rao
1228d6fbbb8SJeff Roberson void
vm_radix_wait(void)1238d6fbbb8SJeff Roberson vm_radix_wait(void)
1248d6fbbb8SJeff Roberson {
1258d6fbbb8SJeff Roberson uma_zwait(vm_radix_node_zone);
1268d6fbbb8SJeff Roberson }
127