1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Copyright (C) 2026 Richard Qin */
/*
* page_cache.cc — Minimal page cache implementation
*
* Open-addressing hash table indexed by (mapping, index) pairs.
* Fixed-size bucket array allocated at init time via slab.
*
* This is intentionally simple: no LRU, no writeback, no eviction.
* The interface in page_cache.h is designed so that upgrading to a
* radix tree + LRU scheme later only changes this file.
*
* Concurrency: protected by a single spinlock. Acceptable for
* initial bring-up; will be replaced by per-bucket or RCU-based
* locking when contention becomes measurable.
*/
#include <lume/page_cache.h>
#include <lume/page.h>
#include <lume/pmm.h>
#include <lume/errno.h>
#include <lume/new.h>
#include <kernel/sync/spinlock.h>
/* ------------------------------------------------------------------ */
/* Hash table internals */
/* ------------------------------------------------------------------ */
struct CacheEntry {
void* mapping; // nullptr = empty slot
uint64 index;
Page* page;
};
namespace {
constexpr uint64 kCacheBuckets = 1024;
CacheEntry g_buckets[kCacheBuckets];
Spinlock g_cache_lock;
uint64 hash_key(void* mapping, uint64 index)
{
uint64 m = reinterpret_cast<uint64>(mapping);
/* FNV-1a inspired mixing */
uint64 h = m ^ (index * 0x517cc1b727220a95ULL);
h ^= h >> 17;
h *= 0xbf58476d1ce4e5b9ULL;
h ^= h >> 31;
return h % kCacheBuckets;
}
/* Linear probe to find an entry or an empty slot. */
CacheEntry* find_slot(void* mapping, uint64 index)
{
uint64 start = hash_key(mapping, index);
for (uint64 i = 0; i < kCacheBuckets; i++) {
uint64 idx = (start + i) % kCacheBuckets;
CacheEntry* e = &g_buckets[idx];
if (e->mapping == nullptr) {
return e; // Empty slot
}
if (e->mapping == mapping && e->index == index) {
return e; // Found
}
}
return nullptr; // Table full
}
/* Find an existing entry (not an empty slot). */
CacheEntry* find_entry(void* mapping, uint64 index)
{
uint64 start = hash_key(mapping, index);
for (uint64 i = 0; i < kCacheBuckets; i++) {
uint64 idx = (start + i) % kCacheBuckets;
CacheEntry* e = &g_buckets[idx];
if (e->mapping == nullptr) {
return nullptr; // Empty = not found
}
if (e->mapping == mapping && e->index == index) {
return e;
}
}
return nullptr;
}
/*
* Remove an entry and fix up the linear-probe chain.
* Standard open-addressing deletion with backward shift.
*/
void remove_entry(CacheEntry* entry)
{
uint64 removed = static_cast<uint64>(entry - g_buckets);
entry->mapping = nullptr;
entry->page = nullptr;
/* Backward-shift deletion to maintain probe chain integrity */
uint64 cur = (removed + 1) % kCacheBuckets;
while (g_buckets[cur].mapping != nullptr) {
uint64 ideal = hash_key(g_buckets[cur].mapping, g_buckets[cur].index);
/* Check if cur's ideal position is "before" removed in probe order */
bool needs_shift = false;
if (removed <= cur) {
needs_shift = (ideal <= removed || ideal > cur);
} else {
needs_shift = (ideal <= removed && ideal > cur);
}
if (needs_shift) {
g_buckets[removed] = g_buckets[cur];
g_buckets[cur].mapping = nullptr;
g_buckets[cur].page = nullptr;
removed = cur;
}
cur = (cur + 1) % kCacheBuckets;
}
}
} // namespace
/* ------------------------------------------------------------------ */
/* Public API */
/* ------------------------------------------------------------------ */
void page_cache_init()
{
g_cache_lock.init("page_cache");
for (uint64 i = 0; i < kCacheBuckets; i++) {
g_buckets[i].mapping = nullptr;
g_buckets[i].index = 0;
g_buckets[i].page = nullptr;
}
}
Page* page_cache_get(void* mapping, uint64 index)
{
LockGuard guard(g_cache_lock);
CacheEntry* e = find_entry(mapping, index);
if (!e) {
return nullptr;
}
page_incref(e->page);
return e->page;
}
int page_cache_insert(void* mapping, uint64 index, Page* page)
{
if (!mapping || !page) {
return -EINVAL;
}
LockGuard guard(g_cache_lock);
/* Check for existing entry */
CacheEntry* existing = find_entry(mapping, index);
if (existing) {
return -EEXIST;
}
/* Find an empty slot */
CacheEntry* slot = find_slot(mapping, index);
if (!slot || slot->mapping != nullptr) {
return -ENOMEM; // Table full
}
slot->mapping = mapping;
slot->index = index;
slot->page = page;
page_incref(page); // Cache holds a reference
return 0;
}
void page_cache_put(Page* page)
{
if (!page) {
return;
}
page_decref(page);
}
Page* page_cache_remove(void* mapping, uint64 index)
{
LockGuard guard(g_cache_lock);
CacheEntry* e = find_entry(mapping, index);
if (!e) {
return nullptr;
}
Page* page = e->page;
remove_entry(e);
page_decref(page); // Release cache's reference
return page;
}
void page_cache_invalidate(void* mapping)
{
if (!mapping) {
return;
}
LockGuard guard(g_cache_lock);
/* Scan all buckets — acceptable for current table size */
for (uint64 i = 0; i < kCacheBuckets; i++) {
if (g_buckets[i].mapping == mapping) {
Page* page = g_buckets[i].page;
remove_entry(&g_buckets[i]);
page_decref(page);
/* After removal, re-check this index since backward shift
* may have moved an entry into this slot. */
i--;
}
}
}
Page* page_cache_get_or_alloc(void* mapping, uint64 index)
{
/* Fast path: already cached */
Page* page = page_cache_get(mapping, index);
if (page) {
return page;
}
/* Slow path: allocate and insert */
page = pmm_alloc_page();
if (!page) {
return nullptr;
}
int ret = page_cache_insert(mapping, index, page);
if (ret == -EEXIST) {
/* Race: another thread inserted between our get and insert.
* Free our allocation and return the existing page. */
pmm_free_page(page);
return page_cache_get(mapping, index);
}
if (ret < 0) {
pmm_free_page(page);
return nullptr;
}
/* Caller gets a ref (insert already added cache's ref,
* we need one more for the caller). */
/* Actually, insert did incref for cache. The page already has
* refcount=1 from alloc + 1 from insert = 2.
* We return it to caller who must put() when done.
* So we incref once more for the caller's reference. */
page_incref(page);
return page;
}
|