1 | /* $Id: PDMBlkCache.cpp 86527 2020-10-11 18:41:41Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PDM Block Cache.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /** @page pg_pdm_block_cache PDM Block Cache - The I/O cache
|
---|
19 | * This component implements an I/O cache based on the 2Q cache algorithm.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*********************************************************************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *********************************************************************************************************************************/
|
---|
26 | #define LOG_GROUP LOG_GROUP_PDM_BLK_CACHE
|
---|
27 | #include "PDMInternal.h"
|
---|
28 | #include <iprt/asm.h>
|
---|
29 | #include <iprt/mem.h>
|
---|
30 | #include <iprt/path.h>
|
---|
31 | #include <iprt/string.h>
|
---|
32 | #include <iprt/trace.h>
|
---|
33 | #include <VBox/log.h>
|
---|
34 | #include <VBox/vmm/stam.h>
|
---|
35 | #include <VBox/vmm/uvm.h>
|
---|
36 | #include <VBox/vmm/vm.h>
|
---|
37 |
|
---|
38 | #include "PDMBlkCacheInternal.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | /*********************************************************************************************************************************
|
---|
42 | * Defined Constants And Macros *
|
---|
43 | *********************************************************************************************************************************/
|
---|
44 | #ifdef VBOX_STRICT
|
---|
45 | # define PDMACFILECACHE_IS_CRITSECT_OWNER(Cache) \
|
---|
46 | do \
|
---|
47 | { \
|
---|
48 | AssertMsg(RTCritSectIsOwner(&Cache->CritSect), \
|
---|
49 | ("Thread does not own critical section\n"));\
|
---|
50 | } while (0)
|
---|
51 |
|
---|
52 | # define PDMACFILECACHE_EP_IS_SEMRW_WRITE_OWNER(pEpCache) \
|
---|
53 | do \
|
---|
54 | { \
|
---|
55 | AssertMsg(RTSemRWIsWriteOwner(pEpCache->SemRWEntries), \
|
---|
56 | ("Thread is not exclusive owner of the per endpoint RW semaphore\n")); \
|
---|
57 | } while (0)
|
---|
58 |
|
---|
59 | # define PDMACFILECACHE_EP_IS_SEMRW_READ_OWNER(pEpCache) \
|
---|
60 | do \
|
---|
61 | { \
|
---|
62 | AssertMsg(RTSemRWIsReadOwner(pEpCache->SemRWEntries), \
|
---|
63 | ("Thread is not read owner of the per endpoint RW semaphore\n")); \
|
---|
64 | } while (0)
|
---|
65 |
|
---|
66 | #else
|
---|
67 | # define PDMACFILECACHE_IS_CRITSECT_OWNER(Cache) do { } while (0)
|
---|
68 | # define PDMACFILECACHE_EP_IS_SEMRW_WRITE_OWNER(pEpCache) do { } while (0)
|
---|
69 | # define PDMACFILECACHE_EP_IS_SEMRW_READ_OWNER(pEpCache) do { } while (0)
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | #define PDM_BLK_CACHE_SAVED_STATE_VERSION 1
|
---|
73 |
|
---|
74 | /* Enable to enable some tracing in the block cache code for investigating issues. */
|
---|
75 | /*#define VBOX_BLKCACHE_TRACING 1*/
|
---|
76 |
|
---|
77 |
|
---|
78 | /*********************************************************************************************************************************
|
---|
79 | * Internal Functions *
|
---|
80 | *********************************************************************************************************************************/
|
---|
81 |
|
---|
82 | static PPDMBLKCACHEENTRY pdmBlkCacheEntryAlloc(PPDMBLKCACHE pBlkCache,
|
---|
83 | uint64_t off, size_t cbData, uint8_t *pbBuffer);
|
---|
84 | static bool pdmBlkCacheAddDirtyEntry(PPDMBLKCACHE pBlkCache, PPDMBLKCACHEENTRY pEntry);
|
---|
85 |
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Add message to the VM trace buffer.
|
---|
89 | *
|
---|
90 | * @returns nothing.
|
---|
91 | * @param pBlkCache The block cache.
|
---|
92 | * @param pszFmt The format string.
|
---|
93 | * @param ... Additional parameters for the string formatter.
|
---|
94 | */
|
---|
95 | DECLINLINE(void) pdmBlkCacheR3TraceMsgF(PPDMBLKCACHE pBlkCache, const char *pszFmt, ...)
|
---|
96 | {
|
---|
97 | #if defined(VBOX_BLKCACHE_TRACING)
|
---|
98 | va_list va;
|
---|
99 | va_start(va, pszFmt);
|
---|
100 | RTTraceBufAddMsgV(pBlkCache->pCache->pVM->CTX_SUFF(hTraceBuf), pszFmt, va);
|
---|
101 | va_end(va);
|
---|
102 | #else
|
---|
103 | RT_NOREF2(pBlkCache, pszFmt);
|
---|
104 | #endif
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Decrement the reference counter of the given cache entry.
|
---|
109 | *
|
---|
110 | * @returns nothing.
|
---|
111 | * @param pEntry The entry to release.
|
---|
112 | */
|
---|
113 | DECLINLINE(void) pdmBlkCacheEntryRelease(PPDMBLKCACHEENTRY pEntry)
|
---|
114 | {
|
---|
115 | AssertMsg(pEntry->cRefs > 0, ("Trying to release a not referenced entry\n"));
|
---|
116 | ASMAtomicDecU32(&pEntry->cRefs);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Increment the reference counter of the given cache entry.
|
---|
121 | *
|
---|
122 | * @returns nothing.
|
---|
123 | * @param pEntry The entry to reference.
|
---|
124 | */
|
---|
125 | DECLINLINE(void) pdmBlkCacheEntryRef(PPDMBLKCACHEENTRY pEntry)
|
---|
126 | {
|
---|
127 | ASMAtomicIncU32(&pEntry->cRefs);
|
---|
128 | }
|
---|
129 |
|
---|
130 | #ifdef VBOX_STRICT
|
---|
131 | static void pdmBlkCacheValidate(PPDMBLKCACHEGLOBAL pCache)
|
---|
132 | {
|
---|
133 | /* Amount of cached data should never exceed the maximum amount. */
|
---|
134 | AssertMsg(pCache->cbCached <= pCache->cbMax,
|
---|
135 | ("Current amount of cached data exceeds maximum\n"));
|
---|
136 |
|
---|
137 | /* The amount of cached data in the LRU and FRU list should match cbCached */
|
---|
138 | AssertMsg(pCache->LruRecentlyUsedIn.cbCached + pCache->LruFrequentlyUsed.cbCached == pCache->cbCached,
|
---|
139 | ("Amount of cached data doesn't match\n"));
|
---|
140 |
|
---|
141 | AssertMsg(pCache->LruRecentlyUsedOut.cbCached <= pCache->cbRecentlyUsedOutMax,
|
---|
142 | ("Paged out list exceeds maximum\n"));
|
---|
143 | }
|
---|
144 | #endif
|
---|
145 |
|
---|
146 | DECLINLINE(void) pdmBlkCacheLockEnter(PPDMBLKCACHEGLOBAL pCache)
|
---|
147 | {
|
---|
148 | RTCritSectEnter(&pCache->CritSect);
|
---|
149 | #ifdef VBOX_STRICT
|
---|
150 | pdmBlkCacheValidate(pCache);
|
---|
151 | #endif
|
---|
152 | }
|
---|
153 |
|
---|
154 | DECLINLINE(void) pdmBlkCacheLockLeave(PPDMBLKCACHEGLOBAL pCache)
|
---|
155 | {
|
---|
156 | #ifdef VBOX_STRICT
|
---|
157 | pdmBlkCacheValidate(pCache);
|
---|
158 | #endif
|
---|
159 | RTCritSectLeave(&pCache->CritSect);
|
---|
160 | }
|
---|
161 |
|
---|
162 | DECLINLINE(void) pdmBlkCacheSub(PPDMBLKCACHEGLOBAL pCache, uint32_t cbAmount)
|
---|
163 | {
|
---|
164 | PDMACFILECACHE_IS_CRITSECT_OWNER(pCache);
|
---|
165 | pCache->cbCached -= cbAmount;
|
---|
166 | }
|
---|
167 |
|
---|
168 | DECLINLINE(void) pdmBlkCacheAdd(PPDMBLKCACHEGLOBAL pCache, uint32_t cbAmount)
|
---|
169 | {
|
---|
170 | PDMACFILECACHE_IS_CRITSECT_OWNER(pCache);
|
---|
171 | pCache->cbCached += cbAmount;
|
---|
172 | }
|
---|
173 |
|
---|
174 | DECLINLINE(void) pdmBlkCacheListAdd(PPDMBLKLRULIST pList, uint32_t cbAmount)
|
---|
175 | {
|
---|
176 | pList->cbCached += cbAmount;
|
---|
177 | }
|
---|
178 |
|
---|
179 | DECLINLINE(void) pdmBlkCacheListSub(PPDMBLKLRULIST pList, uint32_t cbAmount)
|
---|
180 | {
|
---|
181 | pList->cbCached -= cbAmount;
|
---|
182 | }
|
---|
183 |
|
---|
184 | #ifdef PDMACFILECACHE_WITH_LRULIST_CHECKS
|
---|
185 | /**
|
---|
186 | * Checks consistency of a LRU list.
|
---|
187 | *
|
---|
188 | * @returns nothing
|
---|
189 | * @param pList The LRU list to check.
|
---|
190 | * @param pNotInList Element which is not allowed to occur in the list.
|
---|
191 | */
|
---|
192 | static void pdmBlkCacheCheckList(PPDMBLKLRULIST pList, PPDMBLKCACHEENTRY pNotInList)
|
---|
193 | {
|
---|
194 | PPDMBLKCACHEENTRY pCurr = pList->pHead;
|
---|
195 |
|
---|
196 | /* Check that there are no double entries and no cycles in the list. */
|
---|
197 | while (pCurr)
|
---|
198 | {
|
---|
199 | PPDMBLKCACHEENTRY pNext = pCurr->pNext;
|
---|
200 |
|
---|
201 | while (pNext)
|
---|
202 | {
|
---|
203 | AssertMsg(pCurr != pNext,
|
---|
204 | ("Entry %#p is at least two times in list %#p or there is a cycle in the list\n",
|
---|
205 | pCurr, pList));
|
---|
206 | pNext = pNext->pNext;
|
---|
207 | }
|
---|
208 |
|
---|
209 | AssertMsg(pCurr != pNotInList, ("Not allowed entry %#p is in list\n", pCurr));
|
---|
210 |
|
---|
211 | if (!pCurr->pNext)
|
---|
212 | AssertMsg(pCurr == pList->pTail, ("End of list reached but last element is not list tail\n"));
|
---|
213 |
|
---|
214 | pCurr = pCurr->pNext;
|
---|
215 | }
|
---|
216 | }
|
---|
217 | #endif
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Unlinks a cache entry from the LRU list it is assigned to.
|
---|
221 | *
|
---|
222 | * @returns nothing.
|
---|
223 | * @param pEntry The entry to unlink.
|
---|
224 | */
|
---|
225 | static void pdmBlkCacheEntryRemoveFromList(PPDMBLKCACHEENTRY pEntry)
|
---|
226 | {
|
---|
227 | PPDMBLKLRULIST pList = pEntry->pList;
|
---|
228 | PPDMBLKCACHEENTRY pPrev, pNext;
|
---|
229 |
|
---|
230 | LogFlowFunc((": Deleting entry %#p from list %#p\n", pEntry, pList));
|
---|
231 |
|
---|
232 | AssertPtr(pList);
|
---|
233 |
|
---|
234 | #ifdef PDMACFILECACHE_WITH_LRULIST_CHECKS
|
---|
235 | pdmBlkCacheCheckList(pList, NULL);
|
---|
236 | #endif
|
---|
237 |
|
---|
238 | pPrev = pEntry->pPrev;
|
---|
239 | pNext = pEntry->pNext;
|
---|
240 |
|
---|
241 | AssertMsg(pEntry != pPrev, ("Entry links to itself as previous element\n"));
|
---|
242 | AssertMsg(pEntry != pNext, ("Entry links to itself as next element\n"));
|
---|
243 |
|
---|
244 | if (pPrev)
|
---|
245 | pPrev->pNext = pNext;
|
---|
246 | else
|
---|
247 | {
|
---|
248 | pList->pHead = pNext;
|
---|
249 |
|
---|
250 | if (pNext)
|
---|
251 | pNext->pPrev = NULL;
|
---|
252 | }
|
---|
253 |
|
---|
254 | if (pNext)
|
---|
255 | pNext->pPrev = pPrev;
|
---|
256 | else
|
---|
257 | {
|
---|
258 | pList->pTail = pPrev;
|
---|
259 |
|
---|
260 | if (pPrev)
|
---|
261 | pPrev->pNext = NULL;
|
---|
262 | }
|
---|
263 |
|
---|
264 | pEntry->pList = NULL;
|
---|
265 | pEntry->pPrev = NULL;
|
---|
266 | pEntry->pNext = NULL;
|
---|
267 | pdmBlkCacheListSub(pList, pEntry->cbData);
|
---|
268 | #ifdef PDMACFILECACHE_WITH_LRULIST_CHECKS
|
---|
269 | pdmBlkCacheCheckList(pList, pEntry);
|
---|
270 | #endif
|
---|
271 | }
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Adds a cache entry to the given LRU list unlinking it from the currently
|
---|
275 | * assigned list if needed.
|
---|
276 | *
|
---|
277 | * @returns nothing.
|
---|
278 | * @param pList List to the add entry to.
|
---|
279 | * @param pEntry Entry to add.
|
---|
280 | */
|
---|
281 | static void pdmBlkCacheEntryAddToList(PPDMBLKLRULIST pList, PPDMBLKCACHEENTRY pEntry)
|
---|
282 | {
|
---|
283 | LogFlowFunc((": Adding entry %#p to list %#p\n", pEntry, pList));
|
---|
284 | #ifdef PDMACFILECACHE_WITH_LRULIST_CHECKS
|
---|
285 | pdmBlkCacheCheckList(pList, NULL);
|
---|
286 | #endif
|
---|
287 |
|
---|
288 | /* Remove from old list if needed */
|
---|
289 | if (pEntry->pList)
|
---|
290 | pdmBlkCacheEntryRemoveFromList(pEntry);
|
---|
291 |
|
---|
292 | pEntry->pNext = pList->pHead;
|
---|
293 | if (pList->pHead)
|
---|
294 | pList->pHead->pPrev = pEntry;
|
---|
295 | else
|
---|
296 | {
|
---|
297 | Assert(!pList->pTail);
|
---|
298 | pList->pTail = pEntry;
|
---|
299 | }
|
---|
300 |
|
---|
301 | pEntry->pPrev = NULL;
|
---|
302 | pList->pHead = pEntry;
|
---|
303 | pdmBlkCacheListAdd(pList, pEntry->cbData);
|
---|
304 | pEntry->pList = pList;
|
---|
305 | #ifdef PDMACFILECACHE_WITH_LRULIST_CHECKS
|
---|
306 | pdmBlkCacheCheckList(pList, NULL);
|
---|
307 | #endif
|
---|
308 | }
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Destroys a LRU list freeing all entries.
|
---|
312 | *
|
---|
313 | * @returns nothing
|
---|
314 | * @param pList Pointer to the LRU list to destroy.
|
---|
315 | *
|
---|
316 | * @note The caller must own the critical section of the cache.
|
---|
317 | */
|
---|
318 | static void pdmBlkCacheDestroyList(PPDMBLKLRULIST pList)
|
---|
319 | {
|
---|
320 | while (pList->pHead)
|
---|
321 | {
|
---|
322 | PPDMBLKCACHEENTRY pEntry = pList->pHead;
|
---|
323 |
|
---|
324 | pList->pHead = pEntry->pNext;
|
---|
325 |
|
---|
326 | AssertMsg(!(pEntry->fFlags & (PDMBLKCACHE_ENTRY_IO_IN_PROGRESS | PDMBLKCACHE_ENTRY_IS_DIRTY)),
|
---|
327 | ("Entry is dirty and/or still in progress fFlags=%#x\n", pEntry->fFlags));
|
---|
328 |
|
---|
329 | RTMemPageFree(pEntry->pbData, pEntry->cbData);
|
---|
330 | RTMemFree(pEntry);
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * Tries to remove the given amount of bytes from a given list in the cache
|
---|
336 | * moving the entries to one of the given ghosts lists
|
---|
337 | *
|
---|
338 | * @returns Amount of data which could be freed.
|
---|
339 | * @param pCache Pointer to the global cache data.
|
---|
340 | * @param cbData The amount of the data to free.
|
---|
341 | * @param pListSrc The source list to evict data from.
|
---|
342 | * @param pGhostListDst Where the ghost list removed entries should be
|
---|
343 | * moved to, NULL if the entry should be freed.
|
---|
344 | * @param fReuseBuffer Flag whether a buffer should be reused if it has
|
---|
345 | * the same size
|
---|
346 | * @param ppbBuffer Where to store the address of the buffer if an
|
---|
347 | * entry with the same size was found and
|
---|
348 | * fReuseBuffer is true.
|
---|
349 | *
|
---|
350 | * @note This function may return fewer bytes than requested because entries
|
---|
351 | * may be marked as non evictable if they are used for I/O at the
|
---|
352 | * moment.
|
---|
353 | */
|
---|
354 | static size_t pdmBlkCacheEvictPagesFrom(PPDMBLKCACHEGLOBAL pCache, size_t cbData,
|
---|
355 | PPDMBLKLRULIST pListSrc, PPDMBLKLRULIST pGhostListDst,
|
---|
356 | bool fReuseBuffer, uint8_t **ppbBuffer)
|
---|
357 | {
|
---|
358 | size_t cbEvicted = 0;
|
---|
359 |
|
---|
360 | PDMACFILECACHE_IS_CRITSECT_OWNER(pCache);
|
---|
361 |
|
---|
362 | AssertMsg(cbData > 0, ("Evicting 0 bytes not possible\n"));
|
---|
363 | AssertMsg( !pGhostListDst
|
---|
364 | || (pGhostListDst == &pCache->LruRecentlyUsedOut),
|
---|
365 | ("Destination list must be NULL or the recently used but paged out list\n"));
|
---|
366 |
|
---|
367 | if (fReuseBuffer)
|
---|
368 | {
|
---|
369 | AssertPtr(ppbBuffer);
|
---|
370 | *ppbBuffer = NULL;
|
---|
371 | }
|
---|
372 |
|
---|
373 | /* Start deleting from the tail. */
|
---|
374 | PPDMBLKCACHEENTRY pEntry = pListSrc->pTail;
|
---|
375 |
|
---|
376 | while ((cbEvicted < cbData) && pEntry)
|
---|
377 | {
|
---|
378 | PPDMBLKCACHEENTRY pCurr = pEntry;
|
---|
379 |
|
---|
380 | pEntry = pEntry->pPrev;
|
---|
381 |
|
---|
382 | /* We can't evict pages which are currently in progress or dirty but not in progress */
|
---|
383 | if ( !(pCurr->fFlags & PDMBLKCACHE_NOT_EVICTABLE)
|
---|
384 | && (ASMAtomicReadU32(&pCurr->cRefs) == 0))
|
---|
385 | {
|
---|
386 | /* Ok eviction candidate. Grab the endpoint semaphore and check again
|
---|
387 | * because somebody else might have raced us. */
|
---|
388 | PPDMBLKCACHE pBlkCache = pCurr->pBlkCache;
|
---|
389 | RTSemRWRequestWrite(pBlkCache->SemRWEntries, RT_INDEFINITE_WAIT);
|
---|
390 |
|
---|
391 | if (!(pCurr->fFlags & PDMBLKCACHE_NOT_EVICTABLE)
|
---|
392 | && (ASMAtomicReadU32(&pCurr->cRefs) == 0))
|
---|
393 | {
|
---|
394 | LogFlow(("Evicting entry %#p (%u bytes)\n", pCurr, pCurr->cbData));
|
---|
395 |
|
---|
396 | if (fReuseBuffer && pCurr->cbData == cbData)
|
---|
397 | {
|
---|
398 | STAM_COUNTER_INC(&pCache->StatBuffersReused);
|
---|
399 | *ppbBuffer = pCurr->pbData;
|
---|
400 | }
|
---|
401 | else if (pCurr->pbData)
|
---|
402 | RTMemPageFree(pCurr->pbData, pCurr->cbData);
|
---|
403 |
|
---|
404 | pCurr->pbData = NULL;
|
---|
405 | cbEvicted += pCurr->cbData;
|
---|
406 |
|
---|
407 | pdmBlkCacheEntryRemoveFromList(pCurr);
|
---|
408 | pdmBlkCacheSub(pCache, pCurr->cbData);
|
---|
409 |
|
---|
410 | if (pGhostListDst)
|
---|
411 | {
|
---|
412 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
413 |
|
---|
414 | PPDMBLKCACHEENTRY pGhostEntFree = pGhostListDst->pTail;
|
---|
415 |
|
---|
416 | /* We have to remove the last entries from the paged out list. */
|
---|
417 | while ( pGhostListDst->cbCached + pCurr->cbData > pCache->cbRecentlyUsedOutMax
|
---|
418 | && pGhostEntFree)
|
---|
419 | {
|
---|
420 | PPDMBLKCACHEENTRY pFree = pGhostEntFree;
|
---|
421 | PPDMBLKCACHE pBlkCacheFree = pFree->pBlkCache;
|
---|
422 |
|
---|
423 | pGhostEntFree = pGhostEntFree->pPrev;
|
---|
424 |
|
---|
425 | RTSemRWRequestWrite(pBlkCacheFree->SemRWEntries, RT_INDEFINITE_WAIT);
|
---|
426 |
|
---|
427 | if (ASMAtomicReadU32(&pFree->cRefs) == 0)
|
---|
428 | {
|
---|
429 | pdmBlkCacheEntryRemoveFromList(pFree);
|
---|
430 |
|
---|
431 | STAM_PROFILE_ADV_START(&pCache->StatTreeRemove, Cache);
|
---|
432 | RTAvlrU64Remove(pBlkCacheFree->pTree, pFree->Core.Key);
|
---|
433 | STAM_PROFILE_ADV_STOP(&pCache->StatTreeRemove, Cache);
|
---|
434 |
|
---|
435 | RTMemFree(pFree);
|
---|
436 | }
|
---|
437 |
|
---|
438 | RTSemRWReleaseWrite(pBlkCacheFree->SemRWEntries);
|
---|
439 | }
|
---|
440 |
|
---|
441 | if (pGhostListDst->cbCached + pCurr->cbData > pCache->cbRecentlyUsedOutMax)
|
---|
442 | {
|
---|
443 | /* Couldn't remove enough entries. Delete */
|
---|
444 | STAM_PROFILE_ADV_START(&pCache->StatTreeRemove, Cache);
|
---|
445 | RTAvlrU64Remove(pCurr->pBlkCache->pTree, pCurr->Core.Key);
|
---|
446 | STAM_PROFILE_ADV_STOP(&pCache->StatTreeRemove, Cache);
|
---|
447 |
|
---|
448 | RTMemFree(pCurr);
|
---|
449 | }
|
---|
450 | else
|
---|
451 | pdmBlkCacheEntryAddToList(pGhostListDst, pCurr);
|
---|
452 | }
|
---|
453 | else
|
---|
454 | {
|
---|
455 | /* Delete the entry from the AVL tree it is assigned to. */
|
---|
456 | STAM_PROFILE_ADV_START(&pCache->StatTreeRemove, Cache);
|
---|
457 | RTAvlrU64Remove(pCurr->pBlkCache->pTree, pCurr->Core.Key);
|
---|
458 | STAM_PROFILE_ADV_STOP(&pCache->StatTreeRemove, Cache);
|
---|
459 |
|
---|
460 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
461 | RTMemFree(pCurr);
|
---|
462 | }
|
---|
463 | }
|
---|
464 |
|
---|
465 | }
|
---|
466 | else
|
---|
467 | LogFlow(("Entry %#p (%u bytes) is still in progress and can't be evicted\n", pCurr, pCurr->cbData));
|
---|
468 | }
|
---|
469 |
|
---|
470 | return cbEvicted;
|
---|
471 | }
|
---|
472 |
|
---|
473 | static bool pdmBlkCacheReclaim(PPDMBLKCACHEGLOBAL pCache, size_t cbData, bool fReuseBuffer, uint8_t **ppbBuffer)
|
---|
474 | {
|
---|
475 | size_t cbRemoved = 0;
|
---|
476 |
|
---|
477 | if ((pCache->cbCached + cbData) < pCache->cbMax)
|
---|
478 | return true;
|
---|
479 | else if ((pCache->LruRecentlyUsedIn.cbCached + cbData) > pCache->cbRecentlyUsedInMax)
|
---|
480 | {
|
---|
481 | /* Try to evict as many bytes as possible from A1in */
|
---|
482 | cbRemoved = pdmBlkCacheEvictPagesFrom(pCache, cbData, &pCache->LruRecentlyUsedIn,
|
---|
483 | &pCache->LruRecentlyUsedOut, fReuseBuffer, ppbBuffer);
|
---|
484 |
|
---|
485 | /*
|
---|
486 | * If it was not possible to remove enough entries
|
---|
487 | * try the frequently accessed cache.
|
---|
488 | */
|
---|
489 | if (cbRemoved < cbData)
|
---|
490 | {
|
---|
491 | Assert(!fReuseBuffer || !*ppbBuffer); /* It is not possible that we got a buffer with the correct size but we didn't freed enough data. */
|
---|
492 |
|
---|
493 | /*
|
---|
494 | * If we removed something we can't pass the reuse buffer flag anymore because
|
---|
495 | * we don't need to evict that much data
|
---|
496 | */
|
---|
497 | if (!cbRemoved)
|
---|
498 | cbRemoved += pdmBlkCacheEvictPagesFrom(pCache, cbData, &pCache->LruFrequentlyUsed,
|
---|
499 | NULL, fReuseBuffer, ppbBuffer);
|
---|
500 | else
|
---|
501 | cbRemoved += pdmBlkCacheEvictPagesFrom(pCache, cbData - cbRemoved, &pCache->LruFrequentlyUsed,
|
---|
502 | NULL, false, NULL);
|
---|
503 | }
|
---|
504 | }
|
---|
505 | else
|
---|
506 | {
|
---|
507 | /* We have to remove entries from frequently access list. */
|
---|
508 | cbRemoved = pdmBlkCacheEvictPagesFrom(pCache, cbData, &pCache->LruFrequentlyUsed,
|
---|
509 | NULL, fReuseBuffer, ppbBuffer);
|
---|
510 | }
|
---|
511 |
|
---|
512 | LogFlowFunc((": removed %u bytes, requested %u\n", cbRemoved, cbData));
|
---|
513 | return (cbRemoved >= cbData);
|
---|
514 | }
|
---|
515 |
|
---|
516 | DECLINLINE(int) pdmBlkCacheEnqueue(PPDMBLKCACHE pBlkCache, uint64_t off, size_t cbXfer, PPDMBLKCACHEIOXFER pIoXfer)
|
---|
517 | {
|
---|
518 | int rc = VINF_SUCCESS;
|
---|
519 |
|
---|
520 | LogFlowFunc(("%s: Enqueuing hIoXfer=%#p enmXferDir=%d\n",
|
---|
521 | __FUNCTION__, pIoXfer, pIoXfer->enmXferDir));
|
---|
522 |
|
---|
523 | ASMAtomicIncU32(&pBlkCache->cIoXfersActive);
|
---|
524 | pdmBlkCacheR3TraceMsgF(pBlkCache, "BlkCache: I/O req %#p (%RTbool , %d) queued (%u now active)",
|
---|
525 | pIoXfer, pIoXfer->fIoCache, pIoXfer->enmXferDir, pBlkCache->cIoXfersActive);
|
---|
526 |
|
---|
527 | switch (pBlkCache->enmType)
|
---|
528 | {
|
---|
529 | case PDMBLKCACHETYPE_DEV:
|
---|
530 | {
|
---|
531 | rc = pBlkCache->u.Dev.pfnXferEnqueue(pBlkCache->u.Dev.pDevIns,
|
---|
532 | pIoXfer->enmXferDir,
|
---|
533 | off, cbXfer,
|
---|
534 | &pIoXfer->SgBuf, pIoXfer);
|
---|
535 | break;
|
---|
536 | }
|
---|
537 | case PDMBLKCACHETYPE_DRV:
|
---|
538 | {
|
---|
539 | rc = pBlkCache->u.Drv.pfnXferEnqueue(pBlkCache->u.Drv.pDrvIns,
|
---|
540 | pIoXfer->enmXferDir,
|
---|
541 | off, cbXfer,
|
---|
542 | &pIoXfer->SgBuf, pIoXfer);
|
---|
543 | break;
|
---|
544 | }
|
---|
545 | case PDMBLKCACHETYPE_USB:
|
---|
546 | {
|
---|
547 | rc = pBlkCache->u.Usb.pfnXferEnqueue(pBlkCache->u.Usb.pUsbIns,
|
---|
548 | pIoXfer->enmXferDir,
|
---|
549 | off, cbXfer,
|
---|
550 | &pIoXfer->SgBuf, pIoXfer);
|
---|
551 | break;
|
---|
552 | }
|
---|
553 | case PDMBLKCACHETYPE_INTERNAL:
|
---|
554 | {
|
---|
555 | rc = pBlkCache->u.Int.pfnXferEnqueue(pBlkCache->u.Int.pvUser,
|
---|
556 | pIoXfer->enmXferDir,
|
---|
557 | off, cbXfer,
|
---|
558 | &pIoXfer->SgBuf, pIoXfer);
|
---|
559 | break;
|
---|
560 | }
|
---|
561 | default:
|
---|
562 | AssertMsgFailed(("Unknown block cache type!\n"));
|
---|
563 | }
|
---|
564 |
|
---|
565 | if (RT_FAILURE(rc))
|
---|
566 | {
|
---|
567 | pdmBlkCacheR3TraceMsgF(pBlkCache, "BlkCache: Queueing I/O req %#p failed %Rrc", pIoXfer, rc);
|
---|
568 | ASMAtomicDecU32(&pBlkCache->cIoXfersActive);
|
---|
569 | }
|
---|
570 |
|
---|
571 | LogFlowFunc(("%s: returns rc=%Rrc\n", __FUNCTION__, rc));
|
---|
572 | return rc;
|
---|
573 | }
|
---|
574 |
|
---|
575 | /**
|
---|
576 | * Initiates a read I/O task for the given entry.
|
---|
577 | *
|
---|
578 | * @returns VBox status code.
|
---|
579 | * @param pEntry The entry to fetch the data to.
|
---|
580 | */
|
---|
581 | static int pdmBlkCacheEntryReadFromMedium(PPDMBLKCACHEENTRY pEntry)
|
---|
582 | {
|
---|
583 | PPDMBLKCACHE pBlkCache = pEntry->pBlkCache;
|
---|
584 | LogFlowFunc((": Reading data into cache entry %#p\n", pEntry));
|
---|
585 |
|
---|
586 | /* Make sure no one evicts the entry while it is accessed. */
|
---|
587 | pEntry->fFlags |= PDMBLKCACHE_ENTRY_IO_IN_PROGRESS;
|
---|
588 |
|
---|
589 | PPDMBLKCACHEIOXFER pIoXfer = (PPDMBLKCACHEIOXFER)RTMemAllocZ(sizeof(PDMBLKCACHEIOXFER));
|
---|
590 | if (RT_UNLIKELY(!pIoXfer))
|
---|
591 | return VERR_NO_MEMORY;
|
---|
592 |
|
---|
593 | AssertMsg(pEntry->pbData, ("Entry is in ghost state\n"));
|
---|
594 |
|
---|
595 | pIoXfer->fIoCache = true;
|
---|
596 | pIoXfer->pEntry = pEntry;
|
---|
597 | pIoXfer->SgSeg.pvSeg = pEntry->pbData;
|
---|
598 | pIoXfer->SgSeg.cbSeg = pEntry->cbData;
|
---|
599 | pIoXfer->enmXferDir = PDMBLKCACHEXFERDIR_READ;
|
---|
600 | RTSgBufInit(&pIoXfer->SgBuf, &pIoXfer->SgSeg, 1);
|
---|
601 |
|
---|
602 | return pdmBlkCacheEnqueue(pBlkCache, pEntry->Core.Key, pEntry->cbData, pIoXfer);
|
---|
603 | }
|
---|
604 |
|
---|
605 | /**
|
---|
606 | * Initiates a write I/O task for the given entry.
|
---|
607 | *
|
---|
608 | * @returns nothing.
|
---|
609 | * @param pEntry The entry to read the data from.
|
---|
610 | */
|
---|
611 | static int pdmBlkCacheEntryWriteToMedium(PPDMBLKCACHEENTRY pEntry)
|
---|
612 | {
|
---|
613 | PPDMBLKCACHE pBlkCache = pEntry->pBlkCache;
|
---|
614 | LogFlowFunc((": Writing data from cache entry %#p\n", pEntry));
|
---|
615 |
|
---|
616 | /* Make sure no one evicts the entry while it is accessed. */
|
---|
617 | pEntry->fFlags |= PDMBLKCACHE_ENTRY_IO_IN_PROGRESS;
|
---|
618 |
|
---|
619 | PPDMBLKCACHEIOXFER pIoXfer = (PPDMBLKCACHEIOXFER)RTMemAllocZ(sizeof(PDMBLKCACHEIOXFER));
|
---|
620 | if (RT_UNLIKELY(!pIoXfer))
|
---|
621 | return VERR_NO_MEMORY;
|
---|
622 |
|
---|
623 | AssertMsg(pEntry->pbData, ("Entry is in ghost state\n"));
|
---|
624 |
|
---|
625 | pIoXfer->fIoCache = true;
|
---|
626 | pIoXfer->pEntry = pEntry;
|
---|
627 | pIoXfer->SgSeg.pvSeg = pEntry->pbData;
|
---|
628 | pIoXfer->SgSeg.cbSeg = pEntry->cbData;
|
---|
629 | pIoXfer->enmXferDir = PDMBLKCACHEXFERDIR_WRITE;
|
---|
630 | RTSgBufInit(&pIoXfer->SgBuf, &pIoXfer->SgSeg, 1);
|
---|
631 |
|
---|
632 | return pdmBlkCacheEnqueue(pBlkCache, pEntry->Core.Key, pEntry->cbData, pIoXfer);
|
---|
633 | }
|
---|
634 |
|
---|
635 | /**
|
---|
636 | * Passthrough a part of a request directly to the I/O manager handling the
|
---|
637 | * endpoint.
|
---|
638 | *
|
---|
639 | * @returns VBox status code.
|
---|
640 | * @param pBlkCache The endpoint cache.
|
---|
641 | * @param pReq The request.
|
---|
642 | * @param pSgBuf The scatter/gather buffer.
|
---|
643 | * @param offStart Offset to start transfer from.
|
---|
644 | * @param cbData Amount of data to transfer.
|
---|
645 | * @param enmXferDir The transfer type (read/write)
|
---|
646 | */
|
---|
647 | static int pdmBlkCacheRequestPassthrough(PPDMBLKCACHE pBlkCache, PPDMBLKCACHEREQ pReq,
|
---|
648 | PRTSGBUF pSgBuf, uint64_t offStart, size_t cbData,
|
---|
649 | PDMBLKCACHEXFERDIR enmXferDir)
|
---|
650 | {
|
---|
651 |
|
---|
652 | PPDMBLKCACHEIOXFER pIoXfer = (PPDMBLKCACHEIOXFER)RTMemAllocZ(sizeof(PDMBLKCACHEIOXFER));
|
---|
653 | if (RT_UNLIKELY(!pIoXfer))
|
---|
654 | return VERR_NO_MEMORY;
|
---|
655 |
|
---|
656 | ASMAtomicIncU32(&pReq->cXfersPending);
|
---|
657 | pIoXfer->fIoCache = false;
|
---|
658 | pIoXfer->pReq = pReq;
|
---|
659 | pIoXfer->enmXferDir = enmXferDir;
|
---|
660 | if (pSgBuf)
|
---|
661 | {
|
---|
662 | RTSgBufClone(&pIoXfer->SgBuf, pSgBuf);
|
---|
663 | RTSgBufAdvance(pSgBuf, cbData);
|
---|
664 | }
|
---|
665 |
|
---|
666 | return pdmBlkCacheEnqueue(pBlkCache, offStart, cbData, pIoXfer);
|
---|
667 | }
|
---|
668 |
|
---|
669 | /**
|
---|
670 | * Commit a single dirty entry to the endpoint
|
---|
671 | *
|
---|
672 | * @returns nothing
|
---|
673 | * @param pEntry The entry to commit.
|
---|
674 | */
|
---|
675 | static void pdmBlkCacheEntryCommit(PPDMBLKCACHEENTRY pEntry)
|
---|
676 | {
|
---|
677 | AssertMsg( (pEntry->fFlags & PDMBLKCACHE_ENTRY_IS_DIRTY)
|
---|
678 | && !(pEntry->fFlags & PDMBLKCACHE_ENTRY_IO_IN_PROGRESS),
|
---|
679 | ("Invalid flags set for entry %#p\n", pEntry));
|
---|
680 |
|
---|
681 | pdmBlkCacheEntryWriteToMedium(pEntry);
|
---|
682 | }
|
---|
683 |
|
---|
684 | /**
|
---|
685 | * Commit all dirty entries for a single endpoint.
|
---|
686 | *
|
---|
687 | * @returns nothing.
|
---|
688 | * @param pBlkCache The endpoint cache to commit.
|
---|
689 | */
|
---|
690 | static void pdmBlkCacheCommit(PPDMBLKCACHE pBlkCache)
|
---|
691 | {
|
---|
692 | uint32_t cbCommitted = 0;
|
---|
693 |
|
---|
694 | /* Return if the cache was suspended. */
|
---|
695 | if (pBlkCache->fSuspended)
|
---|
696 | return;
|
---|
697 |
|
---|
698 | RTSemRWRequestWrite(pBlkCache->SemRWEntries, RT_INDEFINITE_WAIT);
|
---|
699 |
|
---|
700 | /* The list is moved to a new header to reduce locking overhead. */
|
---|
701 | RTLISTANCHOR ListDirtyNotCommitted;
|
---|
702 |
|
---|
703 | RTSpinlockAcquire(pBlkCache->LockList);
|
---|
704 | RTListMove(&ListDirtyNotCommitted, &pBlkCache->ListDirtyNotCommitted);
|
---|
705 | RTSpinlockRelease(pBlkCache->LockList);
|
---|
706 |
|
---|
707 | if (!RTListIsEmpty(&ListDirtyNotCommitted))
|
---|
708 | {
|
---|
709 | PPDMBLKCACHEENTRY pEntry = RTListGetFirst(&ListDirtyNotCommitted, PDMBLKCACHEENTRY, NodeNotCommitted);
|
---|
710 |
|
---|
711 | while (!RTListNodeIsLast(&ListDirtyNotCommitted, &pEntry->NodeNotCommitted))
|
---|
712 | {
|
---|
713 | PPDMBLKCACHEENTRY pNext = RTListNodeGetNext(&pEntry->NodeNotCommitted, PDMBLKCACHEENTRY,
|
---|
714 | NodeNotCommitted);
|
---|
715 | pdmBlkCacheEntryCommit(pEntry);
|
---|
716 | cbCommitted += pEntry->cbData;
|
---|
717 | RTListNodeRemove(&pEntry->NodeNotCommitted);
|
---|
718 | pEntry = pNext;
|
---|
719 | }
|
---|
720 |
|
---|
721 | /* Commit the last endpoint */
|
---|
722 | Assert(RTListNodeIsLast(&ListDirtyNotCommitted, &pEntry->NodeNotCommitted));
|
---|
723 | pdmBlkCacheEntryCommit(pEntry);
|
---|
724 | cbCommitted += pEntry->cbData;
|
---|
725 | RTListNodeRemove(&pEntry->NodeNotCommitted);
|
---|
726 | AssertMsg(RTListIsEmpty(&ListDirtyNotCommitted),
|
---|
727 | ("Committed all entries but list is not empty\n"));
|
---|
728 | }
|
---|
729 |
|
---|
730 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
731 | AssertMsg(pBlkCache->pCache->cbDirty >= cbCommitted,
|
---|
732 | ("Number of committed bytes exceeds number of dirty bytes\n"));
|
---|
733 | uint32_t cbDirtyOld = ASMAtomicSubU32(&pBlkCache->pCache->cbDirty, cbCommitted);
|
---|
734 |
|
---|
735 | /* Reset the commit timer if we don't have any dirty bits. */
|
---|
736 | if ( !(cbDirtyOld - cbCommitted)
|
---|
737 | && pBlkCache->pCache->u32CommitTimeoutMs != 0)
|
---|
738 | TMTimerStop(pBlkCache->pCache->pTimerCommit);
|
---|
739 | }
|
---|
740 |
|
---|
741 | /**
|
---|
742 | * Commit all dirty entries in the cache.
|
---|
743 | *
|
---|
744 | * @returns nothing.
|
---|
745 | * @param pCache The global cache instance.
|
---|
746 | */
|
---|
747 | static void pdmBlkCacheCommitDirtyEntries(PPDMBLKCACHEGLOBAL pCache)
|
---|
748 | {
|
---|
749 | bool fCommitInProgress = ASMAtomicXchgBool(&pCache->fCommitInProgress, true);
|
---|
750 |
|
---|
751 | if (!fCommitInProgress)
|
---|
752 | {
|
---|
753 | pdmBlkCacheLockEnter(pCache);
|
---|
754 | Assert(!RTListIsEmpty(&pCache->ListUsers));
|
---|
755 |
|
---|
756 | PPDMBLKCACHE pBlkCache = RTListGetFirst(&pCache->ListUsers, PDMBLKCACHE, NodeCacheUser);
|
---|
757 | AssertPtr(pBlkCache);
|
---|
758 |
|
---|
759 | while (!RTListNodeIsLast(&pCache->ListUsers, &pBlkCache->NodeCacheUser))
|
---|
760 | {
|
---|
761 | pdmBlkCacheCommit(pBlkCache);
|
---|
762 |
|
---|
763 | pBlkCache = RTListNodeGetNext(&pBlkCache->NodeCacheUser, PDMBLKCACHE,
|
---|
764 | NodeCacheUser);
|
---|
765 | }
|
---|
766 |
|
---|
767 | /* Commit the last endpoint */
|
---|
768 | Assert(RTListNodeIsLast(&pCache->ListUsers, &pBlkCache->NodeCacheUser));
|
---|
769 | pdmBlkCacheCommit(pBlkCache);
|
---|
770 |
|
---|
771 | pdmBlkCacheLockLeave(pCache);
|
---|
772 | ASMAtomicWriteBool(&pCache->fCommitInProgress, false);
|
---|
773 | }
|
---|
774 | }
|
---|
775 |
|
---|
776 | /**
|
---|
777 | * Adds the given entry as a dirty to the cache.
|
---|
778 | *
|
---|
779 | * @returns Flag whether the amount of dirty bytes in the cache exceeds the threshold
|
---|
780 | * @param pBlkCache The endpoint cache the entry belongs to.
|
---|
781 | * @param pEntry The entry to add.
|
---|
782 | */
|
---|
783 | static bool pdmBlkCacheAddDirtyEntry(PPDMBLKCACHE pBlkCache, PPDMBLKCACHEENTRY pEntry)
|
---|
784 | {
|
---|
785 | bool fDirtyBytesExceeded = false;
|
---|
786 | PPDMBLKCACHEGLOBAL pCache = pBlkCache->pCache;
|
---|
787 |
|
---|
788 | /* If the commit timer is disabled we commit right away. */
|
---|
789 | if (pCache->u32CommitTimeoutMs == 0)
|
---|
790 | {
|
---|
791 | pEntry->fFlags |= PDMBLKCACHE_ENTRY_IS_DIRTY;
|
---|
792 | pdmBlkCacheEntryCommit(pEntry);
|
---|
793 | }
|
---|
794 | else if (!(pEntry->fFlags & PDMBLKCACHE_ENTRY_IS_DIRTY))
|
---|
795 | {
|
---|
796 | pEntry->fFlags |= PDMBLKCACHE_ENTRY_IS_DIRTY;
|
---|
797 |
|
---|
798 | RTSpinlockAcquire(pBlkCache->LockList);
|
---|
799 | RTListAppend(&pBlkCache->ListDirtyNotCommitted, &pEntry->NodeNotCommitted);
|
---|
800 | RTSpinlockRelease(pBlkCache->LockList);
|
---|
801 |
|
---|
802 | uint32_t cbDirty = ASMAtomicAddU32(&pCache->cbDirty, pEntry->cbData);
|
---|
803 |
|
---|
804 | /* Prevent committing if the VM was suspended. */
|
---|
805 | if (RT_LIKELY(!ASMAtomicReadBool(&pCache->fIoErrorVmSuspended)))
|
---|
806 | fDirtyBytesExceeded = (cbDirty + pEntry->cbData >= pCache->cbCommitDirtyThreshold);
|
---|
807 | else if (!cbDirty && pCache->u32CommitTimeoutMs > 0)
|
---|
808 | {
|
---|
809 | /* Arm the commit timer. */
|
---|
810 | TMTimerSetMillies(pCache->pTimerCommit, pCache->u32CommitTimeoutMs);
|
---|
811 | }
|
---|
812 | }
|
---|
813 |
|
---|
814 | return fDirtyBytesExceeded;
|
---|
815 | }
|
---|
816 |
|
---|
817 | static PPDMBLKCACHE pdmR3BlkCacheFindById(PPDMBLKCACHEGLOBAL pBlkCacheGlobal, const char *pcszId)
|
---|
818 | {
|
---|
819 | bool fFound = false;
|
---|
820 |
|
---|
821 | PPDMBLKCACHE pBlkCache;
|
---|
822 | RTListForEach(&pBlkCacheGlobal->ListUsers, pBlkCache, PDMBLKCACHE, NodeCacheUser)
|
---|
823 | {
|
---|
824 | if (!RTStrCmp(pBlkCache->pszId, pcszId))
|
---|
825 | {
|
---|
826 | fFound = true;
|
---|
827 | break;
|
---|
828 | }
|
---|
829 | }
|
---|
830 |
|
---|
831 | return fFound ? pBlkCache : NULL;
|
---|
832 | }
|
---|
833 |
|
---|
834 | /**
|
---|
835 | * Commit timer callback.
|
---|
836 | */
|
---|
837 | static DECLCALLBACK(void) pdmBlkCacheCommitTimerCallback(PVM pVM, PTMTIMER pTimer, void *pvUser)
|
---|
838 | {
|
---|
839 | PPDMBLKCACHEGLOBAL pCache = (PPDMBLKCACHEGLOBAL)pvUser;
|
---|
840 | NOREF(pVM); NOREF(pTimer);
|
---|
841 |
|
---|
842 | LogFlowFunc(("Commit interval expired, commiting dirty entries\n"));
|
---|
843 |
|
---|
844 | if ( ASMAtomicReadU32(&pCache->cbDirty) > 0
|
---|
845 | && !ASMAtomicReadBool(&pCache->fIoErrorVmSuspended))
|
---|
846 | pdmBlkCacheCommitDirtyEntries(pCache);
|
---|
847 |
|
---|
848 | LogFlowFunc(("Entries committed, going to sleep\n"));
|
---|
849 | }
|
---|
850 |
|
---|
851 | static DECLCALLBACK(int) pdmR3BlkCacheSaveExec(PVM pVM, PSSMHANDLE pSSM)
|
---|
852 | {
|
---|
853 | PPDMBLKCACHEGLOBAL pBlkCacheGlobal = pVM->pUVM->pdm.s.pBlkCacheGlobal;
|
---|
854 |
|
---|
855 | AssertPtr(pBlkCacheGlobal);
|
---|
856 |
|
---|
857 | pdmBlkCacheLockEnter(pBlkCacheGlobal);
|
---|
858 |
|
---|
859 | SSMR3PutU32(pSSM, pBlkCacheGlobal->cRefs);
|
---|
860 |
|
---|
861 | /* Go through the list and save all dirty entries. */
|
---|
862 | PPDMBLKCACHE pBlkCache;
|
---|
863 | RTListForEach(&pBlkCacheGlobal->ListUsers, pBlkCache, PDMBLKCACHE, NodeCacheUser)
|
---|
864 | {
|
---|
865 | uint32_t cEntries = 0;
|
---|
866 | PPDMBLKCACHEENTRY pEntry;
|
---|
867 |
|
---|
868 | RTSemRWRequestRead(pBlkCache->SemRWEntries, RT_INDEFINITE_WAIT);
|
---|
869 | SSMR3PutU32(pSSM, (uint32_t)strlen(pBlkCache->pszId));
|
---|
870 | SSMR3PutStrZ(pSSM, pBlkCache->pszId);
|
---|
871 |
|
---|
872 | /* Count the number of entries to safe. */
|
---|
873 | RTListForEach(&pBlkCache->ListDirtyNotCommitted, pEntry, PDMBLKCACHEENTRY, NodeNotCommitted)
|
---|
874 | {
|
---|
875 | cEntries++;
|
---|
876 | }
|
---|
877 |
|
---|
878 | SSMR3PutU32(pSSM, cEntries);
|
---|
879 |
|
---|
880 | /* Walk the list of all dirty entries and save them. */
|
---|
881 | RTListForEach(&pBlkCache->ListDirtyNotCommitted, pEntry, PDMBLKCACHEENTRY, NodeNotCommitted)
|
---|
882 | {
|
---|
883 | /* A few sanity checks. */
|
---|
884 | AssertMsg(!pEntry->cRefs, ("The entry is still referenced\n"));
|
---|
885 | AssertMsg(pEntry->fFlags & PDMBLKCACHE_ENTRY_IS_DIRTY, ("Entry is not dirty\n"));
|
---|
886 | AssertMsg(!(pEntry->fFlags & ~PDMBLKCACHE_ENTRY_IS_DIRTY), ("Invalid flags set\n"));
|
---|
887 | AssertMsg(!pEntry->pWaitingHead && !pEntry->pWaitingTail, ("There are waiting requests\n"));
|
---|
888 | AssertMsg( pEntry->pList == &pBlkCacheGlobal->LruRecentlyUsedIn
|
---|
889 | || pEntry->pList == &pBlkCacheGlobal->LruFrequentlyUsed,
|
---|
890 | ("Invalid list\n"));
|
---|
891 | AssertMsg(pEntry->cbData == pEntry->Core.KeyLast - pEntry->Core.Key + 1,
|
---|
892 | ("Size and range do not match\n"));
|
---|
893 |
|
---|
894 | /* Save */
|
---|
895 | SSMR3PutU64(pSSM, pEntry->Core.Key);
|
---|
896 | SSMR3PutU32(pSSM, pEntry->cbData);
|
---|
897 | SSMR3PutMem(pSSM, pEntry->pbData, pEntry->cbData);
|
---|
898 | }
|
---|
899 |
|
---|
900 | RTSemRWReleaseRead(pBlkCache->SemRWEntries);
|
---|
901 | }
|
---|
902 |
|
---|
903 | pdmBlkCacheLockLeave(pBlkCacheGlobal);
|
---|
904 |
|
---|
905 | /* Terminator */
|
---|
906 | return SSMR3PutU32(pSSM, UINT32_MAX);
|
---|
907 | }
|
---|
908 |
|
---|
909 | static DECLCALLBACK(int) pdmR3BlkCacheLoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
|
---|
910 | {
|
---|
911 | PPDMBLKCACHEGLOBAL pBlkCacheGlobal = pVM->pUVM->pdm.s.pBlkCacheGlobal;
|
---|
912 | uint32_t cRefs;
|
---|
913 |
|
---|
914 | NOREF(uPass);
|
---|
915 | AssertPtr(pBlkCacheGlobal);
|
---|
916 |
|
---|
917 | pdmBlkCacheLockEnter(pBlkCacheGlobal);
|
---|
918 |
|
---|
919 | if (uVersion != PDM_BLK_CACHE_SAVED_STATE_VERSION)
|
---|
920 | return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
|
---|
921 |
|
---|
922 | SSMR3GetU32(pSSM, &cRefs);
|
---|
923 |
|
---|
924 | /*
|
---|
925 | * Fewer users in the saved state than in the current VM are allowed
|
---|
926 | * because that means that there are only new ones which don't have any saved state
|
---|
927 | * which can get lost.
|
---|
928 | * More saved state entries than registered cache users are only allowed if the
|
---|
929 | * missing users don't have any data saved in the cache.
|
---|
930 | */
|
---|
931 | int rc = VINF_SUCCESS;
|
---|
932 | char *pszId = NULL;
|
---|
933 |
|
---|
934 | while ( cRefs > 0
|
---|
935 | && RT_SUCCESS(rc))
|
---|
936 | {
|
---|
937 | PPDMBLKCACHE pBlkCache = NULL;
|
---|
938 | uint32_t cbId = 0;
|
---|
939 |
|
---|
940 | SSMR3GetU32(pSSM, &cbId);
|
---|
941 | Assert(cbId > 0);
|
---|
942 |
|
---|
943 | cbId++; /* Include terminator */
|
---|
944 | pszId = (char *)RTMemAllocZ(cbId * sizeof(char));
|
---|
945 | if (!pszId)
|
---|
946 | {
|
---|
947 | rc = VERR_NO_MEMORY;
|
---|
948 | break;
|
---|
949 | }
|
---|
950 |
|
---|
951 | rc = SSMR3GetStrZ(pSSM, pszId, cbId);
|
---|
952 | AssertRC(rc);
|
---|
953 |
|
---|
954 | /* Search for the block cache with the provided id. */
|
---|
955 | pBlkCache = pdmR3BlkCacheFindById(pBlkCacheGlobal, pszId);
|
---|
956 |
|
---|
957 | /* Get the entries */
|
---|
958 | uint32_t cEntries;
|
---|
959 | SSMR3GetU32(pSSM, &cEntries);
|
---|
960 |
|
---|
961 | if (!pBlkCache && (cEntries > 0))
|
---|
962 | {
|
---|
963 | rc = SSMR3SetCfgError(pSSM, RT_SRC_POS,
|
---|
964 | N_("The VM is missing a block device and there is data in the cache. Please make sure the source and target VMs have compatible storage configurations"));
|
---|
965 | break;
|
---|
966 | }
|
---|
967 |
|
---|
968 | RTMemFree(pszId);
|
---|
969 | pszId = NULL;
|
---|
970 |
|
---|
971 | while (cEntries > 0)
|
---|
972 | {
|
---|
973 | PPDMBLKCACHEENTRY pEntry;
|
---|
974 | uint64_t off;
|
---|
975 | uint32_t cbEntry;
|
---|
976 |
|
---|
977 | SSMR3GetU64(pSSM, &off);
|
---|
978 | SSMR3GetU32(pSSM, &cbEntry);
|
---|
979 |
|
---|
980 | pEntry = pdmBlkCacheEntryAlloc(pBlkCache, off, cbEntry, NULL);
|
---|
981 | if (!pEntry)
|
---|
982 | {
|
---|
983 | rc = VERR_NO_MEMORY;
|
---|
984 | break;
|
---|
985 | }
|
---|
986 |
|
---|
987 | rc = SSMR3GetMem(pSSM, pEntry->pbData, cbEntry);
|
---|
988 | if (RT_FAILURE(rc))
|
---|
989 | {
|
---|
990 | RTMemFree(pEntry->pbData);
|
---|
991 | RTMemFree(pEntry);
|
---|
992 | break;
|
---|
993 | }
|
---|
994 |
|
---|
995 | /* Insert into the tree. */
|
---|
996 | bool fInserted = RTAvlrU64Insert(pBlkCache->pTree, &pEntry->Core);
|
---|
997 | Assert(fInserted); NOREF(fInserted);
|
---|
998 |
|
---|
999 | /* Add to the dirty list. */
|
---|
1000 | pdmBlkCacheAddDirtyEntry(pBlkCache, pEntry);
|
---|
1001 | pdmBlkCacheEntryAddToList(&pBlkCacheGlobal->LruRecentlyUsedIn, pEntry);
|
---|
1002 | pdmBlkCacheAdd(pBlkCacheGlobal, cbEntry);
|
---|
1003 | pdmBlkCacheEntryRelease(pEntry);
|
---|
1004 | cEntries--;
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | cRefs--;
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | if (pszId)
|
---|
1011 | RTMemFree(pszId);
|
---|
1012 |
|
---|
1013 | if (cRefs && RT_SUCCESS(rc))
|
---|
1014 | rc = SSMR3SetCfgError(pSSM, RT_SRC_POS,
|
---|
1015 | N_("Unexpected error while restoring state. Please make sure the source and target VMs have compatible storage configurations"));
|
---|
1016 |
|
---|
1017 | pdmBlkCacheLockLeave(pBlkCacheGlobal);
|
---|
1018 |
|
---|
1019 | if (RT_SUCCESS(rc))
|
---|
1020 | {
|
---|
1021 | uint32_t u32 = 0;
|
---|
1022 | rc = SSMR3GetU32(pSSM, &u32);
|
---|
1023 | if (RT_SUCCESS(rc))
|
---|
1024 | AssertMsgReturn(u32 == UINT32_MAX, ("%#x\n", u32), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | return rc;
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | int pdmR3BlkCacheInit(PVM pVM)
|
---|
1031 | {
|
---|
1032 | int rc = VINF_SUCCESS;
|
---|
1033 | PUVM pUVM = pVM->pUVM;
|
---|
1034 | PPDMBLKCACHEGLOBAL pBlkCacheGlobal;
|
---|
1035 |
|
---|
1036 | LogFlowFunc((": pVM=%p\n", pVM));
|
---|
1037 |
|
---|
1038 | VM_ASSERT_EMT(pVM);
|
---|
1039 |
|
---|
1040 | PCFGMNODE pCfgRoot = CFGMR3GetRoot(pVM);
|
---|
1041 | PCFGMNODE pCfgBlkCache = CFGMR3GetChild(CFGMR3GetChild(pCfgRoot, "PDM"), "BlkCache");
|
---|
1042 |
|
---|
1043 | pBlkCacheGlobal = (PPDMBLKCACHEGLOBAL)RTMemAllocZ(sizeof(PDMBLKCACHEGLOBAL));
|
---|
1044 | if (!pBlkCacheGlobal)
|
---|
1045 | return VERR_NO_MEMORY;
|
---|
1046 |
|
---|
1047 | RTListInit(&pBlkCacheGlobal->ListUsers);
|
---|
1048 | pBlkCacheGlobal->pVM = pVM;
|
---|
1049 | pBlkCacheGlobal->cRefs = 0;
|
---|
1050 | pBlkCacheGlobal->cbCached = 0;
|
---|
1051 | pBlkCacheGlobal->fCommitInProgress = false;
|
---|
1052 |
|
---|
1053 | /* Initialize members */
|
---|
1054 | pBlkCacheGlobal->LruRecentlyUsedIn.pHead = NULL;
|
---|
1055 | pBlkCacheGlobal->LruRecentlyUsedIn.pTail = NULL;
|
---|
1056 | pBlkCacheGlobal->LruRecentlyUsedIn.cbCached = 0;
|
---|
1057 |
|
---|
1058 | pBlkCacheGlobal->LruRecentlyUsedOut.pHead = NULL;
|
---|
1059 | pBlkCacheGlobal->LruRecentlyUsedOut.pTail = NULL;
|
---|
1060 | pBlkCacheGlobal->LruRecentlyUsedOut.cbCached = 0;
|
---|
1061 |
|
---|
1062 | pBlkCacheGlobal->LruFrequentlyUsed.pHead = NULL;
|
---|
1063 | pBlkCacheGlobal->LruFrequentlyUsed.pTail = NULL;
|
---|
1064 | pBlkCacheGlobal->LruFrequentlyUsed.cbCached = 0;
|
---|
1065 |
|
---|
1066 | do
|
---|
1067 | {
|
---|
1068 | rc = CFGMR3QueryU32Def(pCfgBlkCache, "CacheSize", &pBlkCacheGlobal->cbMax, 5 * _1M);
|
---|
1069 | AssertLogRelRCBreak(rc);
|
---|
1070 | LogFlowFunc(("Maximum number of bytes cached %u\n", pBlkCacheGlobal->cbMax));
|
---|
1071 |
|
---|
1072 | pBlkCacheGlobal->cbRecentlyUsedInMax = (pBlkCacheGlobal->cbMax / 100) * 25; /* 25% of the buffer size */
|
---|
1073 | pBlkCacheGlobal->cbRecentlyUsedOutMax = (pBlkCacheGlobal->cbMax / 100) * 50; /* 50% of the buffer size */
|
---|
1074 | LogFlowFunc(("cbRecentlyUsedInMax=%u cbRecentlyUsedOutMax=%u\n",
|
---|
1075 | pBlkCacheGlobal->cbRecentlyUsedInMax, pBlkCacheGlobal->cbRecentlyUsedOutMax));
|
---|
1076 |
|
---|
1077 | /** @todo r=aeichner: Experiment to find optimal default values */
|
---|
1078 | rc = CFGMR3QueryU32Def(pCfgBlkCache, "CacheCommitIntervalMs", &pBlkCacheGlobal->u32CommitTimeoutMs, 10000 /* 10sec */);
|
---|
1079 | AssertLogRelRCBreak(rc);
|
---|
1080 | rc = CFGMR3QueryU32Def(pCfgBlkCache, "CacheCommitThreshold", &pBlkCacheGlobal->cbCommitDirtyThreshold, pBlkCacheGlobal->cbMax / 2);
|
---|
1081 | AssertLogRelRCBreak(rc);
|
---|
1082 | } while (0);
|
---|
1083 |
|
---|
1084 | if (RT_SUCCESS(rc))
|
---|
1085 | {
|
---|
1086 | STAMR3Register(pVM, &pBlkCacheGlobal->cbMax,
|
---|
1087 | STAMTYPE_U32, STAMVISIBILITY_ALWAYS,
|
---|
1088 | "/PDM/BlkCache/cbMax",
|
---|
1089 | STAMUNIT_BYTES,
|
---|
1090 | "Maximum cache size");
|
---|
1091 | STAMR3Register(pVM, &pBlkCacheGlobal->cbCached,
|
---|
1092 | STAMTYPE_U32, STAMVISIBILITY_ALWAYS,
|
---|
1093 | "/PDM/BlkCache/cbCached",
|
---|
1094 | STAMUNIT_BYTES,
|
---|
1095 | "Currently used cache");
|
---|
1096 | STAMR3Register(pVM, &pBlkCacheGlobal->LruRecentlyUsedIn.cbCached,
|
---|
1097 | STAMTYPE_U32, STAMVISIBILITY_ALWAYS,
|
---|
1098 | "/PDM/BlkCache/cbCachedMruIn",
|
---|
1099 | STAMUNIT_BYTES,
|
---|
1100 | "Number of bytes cached in MRU list");
|
---|
1101 | STAMR3Register(pVM, &pBlkCacheGlobal->LruRecentlyUsedOut.cbCached,
|
---|
1102 | STAMTYPE_U32, STAMVISIBILITY_ALWAYS,
|
---|
1103 | "/PDM/BlkCache/cbCachedMruOut",
|
---|
1104 | STAMUNIT_BYTES,
|
---|
1105 | "Number of bytes cached in FRU list");
|
---|
1106 | STAMR3Register(pVM, &pBlkCacheGlobal->LruFrequentlyUsed.cbCached,
|
---|
1107 | STAMTYPE_U32, STAMVISIBILITY_ALWAYS,
|
---|
1108 | "/PDM/BlkCache/cbCachedFru",
|
---|
1109 | STAMUNIT_BYTES,
|
---|
1110 | "Number of bytes cached in FRU ghost list");
|
---|
1111 |
|
---|
1112 | #ifdef VBOX_WITH_STATISTICS
|
---|
1113 | STAMR3Register(pVM, &pBlkCacheGlobal->cHits,
|
---|
1114 | STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
1115 | "/PDM/BlkCache/CacheHits",
|
---|
1116 | STAMUNIT_COUNT, "Number of hits in the cache");
|
---|
1117 | STAMR3Register(pVM, &pBlkCacheGlobal->cPartialHits,
|
---|
1118 | STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
1119 | "/PDM/BlkCache/CachePartialHits",
|
---|
1120 | STAMUNIT_COUNT, "Number of partial hits in the cache");
|
---|
1121 | STAMR3Register(pVM, &pBlkCacheGlobal->cMisses,
|
---|
1122 | STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
1123 | "/PDM/BlkCache/CacheMisses",
|
---|
1124 | STAMUNIT_COUNT, "Number of misses when accessing the cache");
|
---|
1125 | STAMR3Register(pVM, &pBlkCacheGlobal->StatRead,
|
---|
1126 | STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
1127 | "/PDM/BlkCache/CacheRead",
|
---|
1128 | STAMUNIT_BYTES, "Number of bytes read from the cache");
|
---|
1129 | STAMR3Register(pVM, &pBlkCacheGlobal->StatWritten,
|
---|
1130 | STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
1131 | "/PDM/BlkCache/CacheWritten",
|
---|
1132 | STAMUNIT_BYTES, "Number of bytes written to the cache");
|
---|
1133 | STAMR3Register(pVM, &pBlkCacheGlobal->StatTreeGet,
|
---|
1134 | STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
|
---|
1135 | "/PDM/BlkCache/CacheTreeGet",
|
---|
1136 | STAMUNIT_TICKS_PER_CALL, "Time taken to access an entry in the tree");
|
---|
1137 | STAMR3Register(pVM, &pBlkCacheGlobal->StatTreeInsert,
|
---|
1138 | STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
|
---|
1139 | "/PDM/BlkCache/CacheTreeInsert",
|
---|
1140 | STAMUNIT_TICKS_PER_CALL, "Time taken to insert an entry in the tree");
|
---|
1141 | STAMR3Register(pVM, &pBlkCacheGlobal->StatTreeRemove,
|
---|
1142 | STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
|
---|
1143 | "/PDM/BlkCache/CacheTreeRemove",
|
---|
1144 | STAMUNIT_TICKS_PER_CALL, "Time taken to remove an entry an the tree");
|
---|
1145 | STAMR3Register(pVM, &pBlkCacheGlobal->StatBuffersReused,
|
---|
1146 | STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
1147 | "/PDM/BlkCache/CacheBuffersReused",
|
---|
1148 | STAMUNIT_COUNT, "Number of times a buffer could be reused");
|
---|
1149 | #endif
|
---|
1150 |
|
---|
1151 | /* Initialize the critical section */
|
---|
1152 | rc = RTCritSectInit(&pBlkCacheGlobal->CritSect);
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | if (RT_SUCCESS(rc))
|
---|
1156 | {
|
---|
1157 | /* Create the commit timer */
|
---|
1158 | if (pBlkCacheGlobal->u32CommitTimeoutMs > 0)
|
---|
1159 | rc = TMR3TimerCreateInternal(pVM, TMCLOCK_REAL,
|
---|
1160 | pdmBlkCacheCommitTimerCallback,
|
---|
1161 | pBlkCacheGlobal,
|
---|
1162 | "BlkCache-Commit",
|
---|
1163 | &pBlkCacheGlobal->pTimerCommit);
|
---|
1164 |
|
---|
1165 | if (RT_SUCCESS(rc))
|
---|
1166 | {
|
---|
1167 | /* Register saved state handler. */
|
---|
1168 | rc = SSMR3RegisterInternal(pVM, "pdmblkcache", 0, PDM_BLK_CACHE_SAVED_STATE_VERSION, pBlkCacheGlobal->cbMax,
|
---|
1169 | NULL, NULL, NULL,
|
---|
1170 | NULL, pdmR3BlkCacheSaveExec, NULL,
|
---|
1171 | NULL, pdmR3BlkCacheLoadExec, NULL);
|
---|
1172 | if (RT_SUCCESS(rc))
|
---|
1173 | {
|
---|
1174 | LogRel(("BlkCache: Cache successfully initialized. Cache size is %u bytes\n", pBlkCacheGlobal->cbMax));
|
---|
1175 | LogRel(("BlkCache: Cache commit interval is %u ms\n", pBlkCacheGlobal->u32CommitTimeoutMs));
|
---|
1176 | LogRel(("BlkCache: Cache commit threshold is %u bytes\n", pBlkCacheGlobal->cbCommitDirtyThreshold));
|
---|
1177 | pUVM->pdm.s.pBlkCacheGlobal = pBlkCacheGlobal;
|
---|
1178 | return VINF_SUCCESS;
|
---|
1179 | }
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 | RTCritSectDelete(&pBlkCacheGlobal->CritSect);
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 | if (pBlkCacheGlobal)
|
---|
1186 | RTMemFree(pBlkCacheGlobal);
|
---|
1187 |
|
---|
1188 | LogFlowFunc((": returns rc=%Rrc\n", rc));
|
---|
1189 | return rc;
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | void pdmR3BlkCacheTerm(PVM pVM)
|
---|
1193 | {
|
---|
1194 | PPDMBLKCACHEGLOBAL pBlkCacheGlobal = pVM->pUVM->pdm.s.pBlkCacheGlobal;
|
---|
1195 |
|
---|
1196 | if (pBlkCacheGlobal)
|
---|
1197 | {
|
---|
1198 | /* Make sure no one else uses the cache now */
|
---|
1199 | pdmBlkCacheLockEnter(pBlkCacheGlobal);
|
---|
1200 |
|
---|
1201 | /* Cleanup deleting all cache entries waiting for in progress entries to finish. */
|
---|
1202 | pdmBlkCacheDestroyList(&pBlkCacheGlobal->LruRecentlyUsedIn);
|
---|
1203 | pdmBlkCacheDestroyList(&pBlkCacheGlobal->LruRecentlyUsedOut);
|
---|
1204 | pdmBlkCacheDestroyList(&pBlkCacheGlobal->LruFrequentlyUsed);
|
---|
1205 |
|
---|
1206 | pdmBlkCacheLockLeave(pBlkCacheGlobal);
|
---|
1207 |
|
---|
1208 | RTCritSectDelete(&pBlkCacheGlobal->CritSect);
|
---|
1209 | RTMemFree(pBlkCacheGlobal);
|
---|
1210 | pVM->pUVM->pdm.s.pBlkCacheGlobal = NULL;
|
---|
1211 | }
|
---|
1212 | }
|
---|
1213 |
|
---|
1214 | int pdmR3BlkCacheResume(PVM pVM)
|
---|
1215 | {
|
---|
1216 | PPDMBLKCACHEGLOBAL pBlkCacheGlobal = pVM->pUVM->pdm.s.pBlkCacheGlobal;
|
---|
1217 |
|
---|
1218 | LogFlowFunc(("pVM=%#p\n", pVM));
|
---|
1219 |
|
---|
1220 | if ( pBlkCacheGlobal
|
---|
1221 | && ASMAtomicXchgBool(&pBlkCacheGlobal->fIoErrorVmSuspended, false))
|
---|
1222 | {
|
---|
1223 | /* The VM was suspended because of an I/O error, commit all dirty entries. */
|
---|
1224 | pdmBlkCacheCommitDirtyEntries(pBlkCacheGlobal);
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 | return VINF_SUCCESS;
|
---|
1228 | }
|
---|
1229 |
|
---|
1230 | static int pdmR3BlkCacheRetain(PVM pVM, PPPDMBLKCACHE ppBlkCache, const char *pcszId)
|
---|
1231 | {
|
---|
1232 | int rc = VINF_SUCCESS;
|
---|
1233 | PPDMBLKCACHE pBlkCache = NULL;
|
---|
1234 | PPDMBLKCACHEGLOBAL pBlkCacheGlobal = pVM->pUVM->pdm.s.pBlkCacheGlobal;
|
---|
1235 |
|
---|
1236 | if (!pBlkCacheGlobal)
|
---|
1237 | return VERR_NOT_SUPPORTED;
|
---|
1238 |
|
---|
1239 | /*
|
---|
1240 | * Check that no other user cache has the same id first,
|
---|
1241 | * Unique id's are necessary in case the state is saved.
|
---|
1242 | */
|
---|
1243 | pdmBlkCacheLockEnter(pBlkCacheGlobal);
|
---|
1244 |
|
---|
1245 | pBlkCache = pdmR3BlkCacheFindById(pBlkCacheGlobal, pcszId);
|
---|
1246 |
|
---|
1247 | if (!pBlkCache)
|
---|
1248 | {
|
---|
1249 | pBlkCache = (PPDMBLKCACHE)RTMemAllocZ(sizeof(PDMBLKCACHE));
|
---|
1250 |
|
---|
1251 | if (pBlkCache)
|
---|
1252 | pBlkCache->pszId = RTStrDup(pcszId);
|
---|
1253 |
|
---|
1254 | if ( pBlkCache
|
---|
1255 | && pBlkCache->pszId)
|
---|
1256 | {
|
---|
1257 | pBlkCache->fSuspended = false;
|
---|
1258 | pBlkCache->cIoXfersActive = 0;
|
---|
1259 | pBlkCache->pCache = pBlkCacheGlobal;
|
---|
1260 | RTListInit(&pBlkCache->ListDirtyNotCommitted);
|
---|
1261 |
|
---|
1262 | rc = RTSpinlockCreate(&pBlkCache->LockList, RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, "pdmR3BlkCacheRetain");
|
---|
1263 | if (RT_SUCCESS(rc))
|
---|
1264 | {
|
---|
1265 | rc = RTSemRWCreate(&pBlkCache->SemRWEntries);
|
---|
1266 | if (RT_SUCCESS(rc))
|
---|
1267 | {
|
---|
1268 | pBlkCache->pTree = (PAVLRU64TREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
|
---|
1269 | if (pBlkCache->pTree)
|
---|
1270 | {
|
---|
1271 | #ifdef VBOX_WITH_STATISTICS
|
---|
1272 | STAMR3RegisterF(pBlkCacheGlobal->pVM, &pBlkCache->StatWriteDeferred,
|
---|
1273 | STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS,
|
---|
1274 | STAMUNIT_COUNT, "Number of deferred writes",
|
---|
1275 | "/PDM/BlkCache/%s/Cache/DeferredWrites", pBlkCache->pszId);
|
---|
1276 | #endif
|
---|
1277 |
|
---|
1278 | /* Add to the list of users. */
|
---|
1279 | pBlkCacheGlobal->cRefs++;
|
---|
1280 | RTListAppend(&pBlkCacheGlobal->ListUsers, &pBlkCache->NodeCacheUser);
|
---|
1281 | pdmBlkCacheLockLeave(pBlkCacheGlobal);
|
---|
1282 |
|
---|
1283 | *ppBlkCache = pBlkCache;
|
---|
1284 | LogFlowFunc(("returns success\n"));
|
---|
1285 | return VINF_SUCCESS;
|
---|
1286 | }
|
---|
1287 |
|
---|
1288 | rc = VERR_NO_MEMORY;
|
---|
1289 | RTSemRWDestroy(pBlkCache->SemRWEntries);
|
---|
1290 | }
|
---|
1291 |
|
---|
1292 | RTSpinlockDestroy(pBlkCache->LockList);
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 | RTStrFree(pBlkCache->pszId);
|
---|
1296 | }
|
---|
1297 | else
|
---|
1298 | rc = VERR_NO_MEMORY;
|
---|
1299 |
|
---|
1300 | if (pBlkCache)
|
---|
1301 | RTMemFree(pBlkCache);
|
---|
1302 | }
|
---|
1303 | else
|
---|
1304 | rc = VERR_ALREADY_EXISTS;
|
---|
1305 |
|
---|
1306 | pdmBlkCacheLockLeave(pBlkCacheGlobal);
|
---|
1307 |
|
---|
1308 | LogFlowFunc(("Leave rc=%Rrc\n", rc));
|
---|
1309 | return rc;
|
---|
1310 | }
|
---|
1311 |
|
---|
1312 | VMMR3DECL(int) PDMR3BlkCacheRetainDriver(PVM pVM, PPDMDRVINS pDrvIns, PPPDMBLKCACHE ppBlkCache,
|
---|
1313 | PFNPDMBLKCACHEXFERCOMPLETEDRV pfnXferComplete,
|
---|
1314 | PFNPDMBLKCACHEXFERENQUEUEDRV pfnXferEnqueue,
|
---|
1315 | PFNPDMBLKCACHEXFERENQUEUEDISCARDDRV pfnXferEnqueueDiscard,
|
---|
1316 | const char *pcszId)
|
---|
1317 | {
|
---|
1318 | int rc = VINF_SUCCESS;
|
---|
1319 | PPDMBLKCACHE pBlkCache;
|
---|
1320 |
|
---|
1321 | rc = pdmR3BlkCacheRetain(pVM, &pBlkCache, pcszId);
|
---|
1322 | if (RT_SUCCESS(rc))
|
---|
1323 | {
|
---|
1324 | pBlkCache->enmType = PDMBLKCACHETYPE_DRV;
|
---|
1325 | pBlkCache->u.Drv.pfnXferComplete = pfnXferComplete;
|
---|
1326 | pBlkCache->u.Drv.pfnXferEnqueue = pfnXferEnqueue;
|
---|
1327 | pBlkCache->u.Drv.pfnXferEnqueueDiscard = pfnXferEnqueueDiscard;
|
---|
1328 | pBlkCache->u.Drv.pDrvIns = pDrvIns;
|
---|
1329 | *ppBlkCache = pBlkCache;
|
---|
1330 | }
|
---|
1331 |
|
---|
1332 | LogFlowFunc(("Leave rc=%Rrc\n", rc));
|
---|
1333 | return rc;
|
---|
1334 | }
|
---|
1335 |
|
---|
1336 | VMMR3DECL(int) PDMR3BlkCacheRetainDevice(PVM pVM, PPDMDEVINS pDevIns, PPPDMBLKCACHE ppBlkCache,
|
---|
1337 | PFNPDMBLKCACHEXFERCOMPLETEDEV pfnXferComplete,
|
---|
1338 | PFNPDMBLKCACHEXFERENQUEUEDEV pfnXferEnqueue,
|
---|
1339 | PFNPDMBLKCACHEXFERENQUEUEDISCARDDEV pfnXferEnqueueDiscard,
|
---|
1340 | const char *pcszId)
|
---|
1341 | {
|
---|
1342 | int rc = VINF_SUCCESS;
|
---|
1343 | PPDMBLKCACHE pBlkCache;
|
---|
1344 |
|
---|
1345 | rc = pdmR3BlkCacheRetain(pVM, &pBlkCache, pcszId);
|
---|
1346 | if (RT_SUCCESS(rc))
|
---|
1347 | {
|
---|
1348 | pBlkCache->enmType = PDMBLKCACHETYPE_DEV;
|
---|
1349 | pBlkCache->u.Dev.pfnXferComplete = pfnXferComplete;
|
---|
1350 | pBlkCache->u.Dev.pfnXferEnqueue = pfnXferEnqueue;
|
---|
1351 | pBlkCache->u.Dev.pfnXferEnqueueDiscard = pfnXferEnqueueDiscard;
|
---|
1352 | pBlkCache->u.Dev.pDevIns = pDevIns;
|
---|
1353 | *ppBlkCache = pBlkCache;
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 | LogFlowFunc(("Leave rc=%Rrc\n", rc));
|
---|
1357 | return rc;
|
---|
1358 |
|
---|
1359 | }
|
---|
1360 |
|
---|
1361 | VMMR3DECL(int) PDMR3BlkCacheRetainUsb(PVM pVM, PPDMUSBINS pUsbIns, PPPDMBLKCACHE ppBlkCache,
|
---|
1362 | PFNPDMBLKCACHEXFERCOMPLETEUSB pfnXferComplete,
|
---|
1363 | PFNPDMBLKCACHEXFERENQUEUEUSB pfnXferEnqueue,
|
---|
1364 | PFNPDMBLKCACHEXFERENQUEUEDISCARDUSB pfnXferEnqueueDiscard,
|
---|
1365 | const char *pcszId)
|
---|
1366 | {
|
---|
1367 | int rc = VINF_SUCCESS;
|
---|
1368 | PPDMBLKCACHE pBlkCache;
|
---|
1369 |
|
---|
1370 | rc = pdmR3BlkCacheRetain(pVM, &pBlkCache, pcszId);
|
---|
1371 | if (RT_SUCCESS(rc))
|
---|
1372 | {
|
---|
1373 | pBlkCache->enmType = PDMBLKCACHETYPE_USB;
|
---|
1374 | pBlkCache->u.Usb.pfnXferComplete = pfnXferComplete;
|
---|
1375 | pBlkCache->u.Usb.pfnXferEnqueue = pfnXferEnqueue;
|
---|
1376 | pBlkCache->u.Usb.pfnXferEnqueueDiscard = pfnXferEnqueueDiscard;
|
---|
1377 | pBlkCache->u.Usb.pUsbIns = pUsbIns;
|
---|
1378 | *ppBlkCache = pBlkCache;
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 | LogFlowFunc(("Leave rc=%Rrc\n", rc));
|
---|
1382 | return rc;
|
---|
1383 |
|
---|
1384 | }
|
---|
1385 |
|
---|
1386 | VMMR3DECL(int) PDMR3BlkCacheRetainInt(PVM pVM, void *pvUser, PPPDMBLKCACHE ppBlkCache,
|
---|
1387 | PFNPDMBLKCACHEXFERCOMPLETEINT pfnXferComplete,
|
---|
1388 | PFNPDMBLKCACHEXFERENQUEUEINT pfnXferEnqueue,
|
---|
1389 | PFNPDMBLKCACHEXFERENQUEUEDISCARDINT pfnXferEnqueueDiscard,
|
---|
1390 | const char *pcszId)
|
---|
1391 | {
|
---|
1392 | int rc = VINF_SUCCESS;
|
---|
1393 | PPDMBLKCACHE pBlkCache;
|
---|
1394 |
|
---|
1395 | rc = pdmR3BlkCacheRetain(pVM, &pBlkCache, pcszId);
|
---|
1396 | if (RT_SUCCESS(rc))
|
---|
1397 | {
|
---|
1398 | pBlkCache->enmType = PDMBLKCACHETYPE_INTERNAL;
|
---|
1399 | pBlkCache->u.Int.pfnXferComplete = pfnXferComplete;
|
---|
1400 | pBlkCache->u.Int.pfnXferEnqueue = pfnXferEnqueue;
|
---|
1401 | pBlkCache->u.Int.pfnXferEnqueueDiscard = pfnXferEnqueueDiscard;
|
---|
1402 | pBlkCache->u.Int.pvUser = pvUser;
|
---|
1403 | *ppBlkCache = pBlkCache;
|
---|
1404 | }
|
---|
1405 |
|
---|
1406 | LogFlowFunc(("Leave rc=%Rrc\n", rc));
|
---|
1407 | return rc;
|
---|
1408 |
|
---|
1409 | }
|
---|
1410 |
|
---|
1411 | /**
|
---|
1412 | * Callback for the AVL destroy routine. Frees a cache entry for this endpoint.
|
---|
1413 | *
|
---|
1414 | * @returns IPRT status code.
|
---|
1415 | * @param pNode The node to destroy.
|
---|
1416 | * @param pvUser Opaque user data.
|
---|
1417 | */
|
---|
1418 | static DECLCALLBACK(int) pdmBlkCacheEntryDestroy(PAVLRU64NODECORE pNode, void *pvUser)
|
---|
1419 | {
|
---|
1420 | PPDMBLKCACHEENTRY pEntry = (PPDMBLKCACHEENTRY)pNode;
|
---|
1421 | PPDMBLKCACHEGLOBAL pCache = (PPDMBLKCACHEGLOBAL)pvUser;
|
---|
1422 | PPDMBLKCACHE pBlkCache = pEntry->pBlkCache;
|
---|
1423 |
|
---|
1424 | while (ASMAtomicReadU32(&pEntry->fFlags) & PDMBLKCACHE_ENTRY_IO_IN_PROGRESS)
|
---|
1425 | {
|
---|
1426 | /* Leave the locks to let the I/O thread make progress but reference the entry to prevent eviction. */
|
---|
1427 | pdmBlkCacheEntryRef(pEntry);
|
---|
1428 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
1429 | pdmBlkCacheLockLeave(pCache);
|
---|
1430 |
|
---|
1431 | RTThreadSleep(250);
|
---|
1432 |
|
---|
1433 | /* Re-enter all locks */
|
---|
1434 | pdmBlkCacheLockEnter(pCache);
|
---|
1435 | RTSemRWRequestWrite(pBlkCache->SemRWEntries, RT_INDEFINITE_WAIT);
|
---|
1436 | pdmBlkCacheEntryRelease(pEntry);
|
---|
1437 | }
|
---|
1438 |
|
---|
1439 | AssertMsg(!(pEntry->fFlags & PDMBLKCACHE_ENTRY_IO_IN_PROGRESS),
|
---|
1440 | ("Entry is dirty and/or still in progress fFlags=%#x\n", pEntry->fFlags));
|
---|
1441 |
|
---|
1442 | bool fUpdateCache = pEntry->pList == &pCache->LruFrequentlyUsed
|
---|
1443 | || pEntry->pList == &pCache->LruRecentlyUsedIn;
|
---|
1444 |
|
---|
1445 | pdmBlkCacheEntryRemoveFromList(pEntry);
|
---|
1446 |
|
---|
1447 | if (fUpdateCache)
|
---|
1448 | pdmBlkCacheSub(pCache, pEntry->cbData);
|
---|
1449 |
|
---|
1450 | RTMemPageFree(pEntry->pbData, pEntry->cbData);
|
---|
1451 | RTMemFree(pEntry);
|
---|
1452 |
|
---|
1453 | return VINF_SUCCESS;
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | VMMR3DECL(void) PDMR3BlkCacheRelease(PPDMBLKCACHE pBlkCache)
|
---|
1457 | {
|
---|
1458 | PPDMBLKCACHEGLOBAL pCache = pBlkCache->pCache;
|
---|
1459 |
|
---|
1460 | /*
|
---|
1461 | * Commit all dirty entries now (they are waited on for completion during the
|
---|
1462 | * destruction of the AVL tree below).
|
---|
1463 | * The exception is if the VM was paused because of an I/O error before.
|
---|
1464 | */
|
---|
1465 | if (!ASMAtomicReadBool(&pCache->fIoErrorVmSuspended))
|
---|
1466 | pdmBlkCacheCommit(pBlkCache);
|
---|
1467 |
|
---|
1468 | /* Make sure nobody is accessing the cache while we delete the tree. */
|
---|
1469 | pdmBlkCacheLockEnter(pCache);
|
---|
1470 | RTSemRWRequestWrite(pBlkCache->SemRWEntries, RT_INDEFINITE_WAIT);
|
---|
1471 | RTAvlrU64Destroy(pBlkCache->pTree, pdmBlkCacheEntryDestroy, pCache);
|
---|
1472 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
1473 |
|
---|
1474 | RTSpinlockDestroy(pBlkCache->LockList);
|
---|
1475 |
|
---|
1476 | pCache->cRefs--;
|
---|
1477 | RTListNodeRemove(&pBlkCache->NodeCacheUser);
|
---|
1478 |
|
---|
1479 | pdmBlkCacheLockLeave(pCache);
|
---|
1480 |
|
---|
1481 | RTMemFree(pBlkCache->pTree);
|
---|
1482 | pBlkCache->pTree = NULL;
|
---|
1483 | RTSemRWDestroy(pBlkCache->SemRWEntries);
|
---|
1484 |
|
---|
1485 | #ifdef VBOX_WITH_STATISTICS
|
---|
1486 | STAMR3DeregisterF(pCache->pVM->pUVM, "/PDM/BlkCache/%s/Cache/DeferredWrites", pBlkCache->pszId);
|
---|
1487 | #endif
|
---|
1488 |
|
---|
1489 | RTStrFree(pBlkCache->pszId);
|
---|
1490 | RTMemFree(pBlkCache);
|
---|
1491 | }
|
---|
1492 |
|
---|
1493 | VMMR3DECL(void) PDMR3BlkCacheReleaseDevice(PVM pVM, PPDMDEVINS pDevIns)
|
---|
1494 | {
|
---|
1495 | LogFlow(("%s: pDevIns=%p\n", __FUNCTION__, pDevIns));
|
---|
1496 |
|
---|
1497 | /*
|
---|
1498 | * Validate input.
|
---|
1499 | */
|
---|
1500 | if (!pDevIns)
|
---|
1501 | return;
|
---|
1502 | VM_ASSERT_EMT(pVM);
|
---|
1503 |
|
---|
1504 | PPDMBLKCACHEGLOBAL pBlkCacheGlobal = pVM->pUVM->pdm.s.pBlkCacheGlobal;
|
---|
1505 | PPDMBLKCACHE pBlkCache, pBlkCacheNext;
|
---|
1506 |
|
---|
1507 | /* Return silently if not supported. */
|
---|
1508 | if (!pBlkCacheGlobal)
|
---|
1509 | return;
|
---|
1510 |
|
---|
1511 | pdmBlkCacheLockEnter(pBlkCacheGlobal);
|
---|
1512 |
|
---|
1513 | RTListForEachSafe(&pBlkCacheGlobal->ListUsers, pBlkCache, pBlkCacheNext, PDMBLKCACHE, NodeCacheUser)
|
---|
1514 | {
|
---|
1515 | if ( pBlkCache->enmType == PDMBLKCACHETYPE_DEV
|
---|
1516 | && pBlkCache->u.Dev.pDevIns == pDevIns)
|
---|
1517 | PDMR3BlkCacheRelease(pBlkCache);
|
---|
1518 | }
|
---|
1519 |
|
---|
1520 | pdmBlkCacheLockLeave(pBlkCacheGlobal);
|
---|
1521 | }
|
---|
1522 |
|
---|
1523 | VMMR3DECL(void) PDMR3BlkCacheReleaseDriver(PVM pVM, PPDMDRVINS pDrvIns)
|
---|
1524 | {
|
---|
1525 | LogFlow(("%s: pDrvIns=%p\n", __FUNCTION__, pDrvIns));
|
---|
1526 |
|
---|
1527 | /*
|
---|
1528 | * Validate input.
|
---|
1529 | */
|
---|
1530 | if (!pDrvIns)
|
---|
1531 | return;
|
---|
1532 | VM_ASSERT_EMT(pVM);
|
---|
1533 |
|
---|
1534 | PPDMBLKCACHEGLOBAL pBlkCacheGlobal = pVM->pUVM->pdm.s.pBlkCacheGlobal;
|
---|
1535 | PPDMBLKCACHE pBlkCache, pBlkCacheNext;
|
---|
1536 |
|
---|
1537 | /* Return silently if not supported. */
|
---|
1538 | if (!pBlkCacheGlobal)
|
---|
1539 | return;
|
---|
1540 |
|
---|
1541 | pdmBlkCacheLockEnter(pBlkCacheGlobal);
|
---|
1542 |
|
---|
1543 | RTListForEachSafe(&pBlkCacheGlobal->ListUsers, pBlkCache, pBlkCacheNext, PDMBLKCACHE, NodeCacheUser)
|
---|
1544 | {
|
---|
1545 | if ( pBlkCache->enmType == PDMBLKCACHETYPE_DRV
|
---|
1546 | && pBlkCache->u.Drv.pDrvIns == pDrvIns)
|
---|
1547 | PDMR3BlkCacheRelease(pBlkCache);
|
---|
1548 | }
|
---|
1549 |
|
---|
1550 | pdmBlkCacheLockLeave(pBlkCacheGlobal);
|
---|
1551 | }
|
---|
1552 |
|
---|
1553 | VMMR3DECL(void) PDMR3BlkCacheReleaseUsb(PVM pVM, PPDMUSBINS pUsbIns)
|
---|
1554 | {
|
---|
1555 | LogFlow(("%s: pUsbIns=%p\n", __FUNCTION__, pUsbIns));
|
---|
1556 |
|
---|
1557 | /*
|
---|
1558 | * Validate input.
|
---|
1559 | */
|
---|
1560 | if (!pUsbIns)
|
---|
1561 | return;
|
---|
1562 | VM_ASSERT_EMT(pVM);
|
---|
1563 |
|
---|
1564 | PPDMBLKCACHEGLOBAL pBlkCacheGlobal = pVM->pUVM->pdm.s.pBlkCacheGlobal;
|
---|
1565 | PPDMBLKCACHE pBlkCache, pBlkCacheNext;
|
---|
1566 |
|
---|
1567 | /* Return silently if not supported. */
|
---|
1568 | if (!pBlkCacheGlobal)
|
---|
1569 | return;
|
---|
1570 |
|
---|
1571 | pdmBlkCacheLockEnter(pBlkCacheGlobal);
|
---|
1572 |
|
---|
1573 | RTListForEachSafe(&pBlkCacheGlobal->ListUsers, pBlkCache, pBlkCacheNext, PDMBLKCACHE, NodeCacheUser)
|
---|
1574 | {
|
---|
1575 | if ( pBlkCache->enmType == PDMBLKCACHETYPE_USB
|
---|
1576 | && pBlkCache->u.Usb.pUsbIns == pUsbIns)
|
---|
1577 | PDMR3BlkCacheRelease(pBlkCache);
|
---|
1578 | }
|
---|
1579 |
|
---|
1580 | pdmBlkCacheLockLeave(pBlkCacheGlobal);
|
---|
1581 | }
|
---|
1582 |
|
---|
1583 | static PPDMBLKCACHEENTRY pdmBlkCacheGetCacheEntryByOffset(PPDMBLKCACHE pBlkCache, uint64_t off)
|
---|
1584 | {
|
---|
1585 | STAM_PROFILE_ADV_START(&pBlkCache->pCache->StatTreeGet, Cache);
|
---|
1586 |
|
---|
1587 | RTSemRWRequestRead(pBlkCache->SemRWEntries, RT_INDEFINITE_WAIT);
|
---|
1588 | PPDMBLKCACHEENTRY pEntry = (PPDMBLKCACHEENTRY)RTAvlrU64RangeGet(pBlkCache->pTree, off);
|
---|
1589 | if (pEntry)
|
---|
1590 | pdmBlkCacheEntryRef(pEntry);
|
---|
1591 | RTSemRWReleaseRead(pBlkCache->SemRWEntries);
|
---|
1592 |
|
---|
1593 | STAM_PROFILE_ADV_STOP(&pBlkCache->pCache->StatTreeGet, Cache);
|
---|
1594 |
|
---|
1595 | return pEntry;
|
---|
1596 | }
|
---|
1597 |
|
---|
1598 | /**
|
---|
1599 | * Return the best fit cache entries for the given offset.
|
---|
1600 | *
|
---|
1601 | * @returns nothing.
|
---|
1602 | * @param pBlkCache The endpoint cache.
|
---|
1603 | * @param off The offset.
|
---|
1604 | * @param ppEntryAbove Where to store the pointer to the best fit entry above
|
---|
1605 | * the given offset. NULL if not required.
|
---|
1606 | */
|
---|
1607 | static void pdmBlkCacheGetCacheBestFitEntryByOffset(PPDMBLKCACHE pBlkCache, uint64_t off, PPDMBLKCACHEENTRY *ppEntryAbove)
|
---|
1608 | {
|
---|
1609 | STAM_PROFILE_ADV_START(&pBlkCache->pCache->StatTreeGet, Cache);
|
---|
1610 |
|
---|
1611 | RTSemRWRequestRead(pBlkCache->SemRWEntries, RT_INDEFINITE_WAIT);
|
---|
1612 | if (ppEntryAbove)
|
---|
1613 | {
|
---|
1614 | *ppEntryAbove = (PPDMBLKCACHEENTRY)RTAvlrU64GetBestFit(pBlkCache->pTree, off, true /*fAbove*/);
|
---|
1615 | if (*ppEntryAbove)
|
---|
1616 | pdmBlkCacheEntryRef(*ppEntryAbove);
|
---|
1617 | }
|
---|
1618 |
|
---|
1619 | RTSemRWReleaseRead(pBlkCache->SemRWEntries);
|
---|
1620 |
|
---|
1621 | STAM_PROFILE_ADV_STOP(&pBlkCache->pCache->StatTreeGet, Cache);
|
---|
1622 | }
|
---|
1623 |
|
---|
1624 | static void pdmBlkCacheInsertEntry(PPDMBLKCACHE pBlkCache, PPDMBLKCACHEENTRY pEntry)
|
---|
1625 | {
|
---|
1626 | STAM_PROFILE_ADV_START(&pBlkCache->pCache->StatTreeInsert, Cache);
|
---|
1627 | RTSemRWRequestWrite(pBlkCache->SemRWEntries, RT_INDEFINITE_WAIT);
|
---|
1628 | bool fInserted = RTAvlrU64Insert(pBlkCache->pTree, &pEntry->Core);
|
---|
1629 | AssertMsg(fInserted, ("Node was not inserted into tree\n")); NOREF(fInserted);
|
---|
1630 | STAM_PROFILE_ADV_STOP(&pBlkCache->pCache->StatTreeInsert, Cache);
|
---|
1631 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
1632 | }
|
---|
1633 |
|
---|
1634 | /**
|
---|
1635 | * Allocates and initializes a new entry for the cache.
|
---|
1636 | * The entry has a reference count of 1.
|
---|
1637 | *
|
---|
1638 | * @returns Pointer to the new cache entry or NULL if out of memory.
|
---|
1639 | * @param pBlkCache The cache the entry belongs to.
|
---|
1640 | * @param off Start offset.
|
---|
1641 | * @param cbData Size of the cache entry.
|
---|
1642 | * @param pbBuffer Pointer to the buffer to use.
|
---|
1643 | * NULL if a new buffer should be allocated.
|
---|
1644 | * The buffer needs to have the same size of the entry.
|
---|
1645 | */
|
---|
1646 | static PPDMBLKCACHEENTRY pdmBlkCacheEntryAlloc(PPDMBLKCACHE pBlkCache, uint64_t off, size_t cbData, uint8_t *pbBuffer)
|
---|
1647 | {
|
---|
1648 | AssertReturn(cbData <= UINT32_MAX, NULL);
|
---|
1649 | PPDMBLKCACHEENTRY pEntryNew = (PPDMBLKCACHEENTRY)RTMemAllocZ(sizeof(PDMBLKCACHEENTRY));
|
---|
1650 |
|
---|
1651 | if (RT_UNLIKELY(!pEntryNew))
|
---|
1652 | return NULL;
|
---|
1653 |
|
---|
1654 | pEntryNew->Core.Key = off;
|
---|
1655 | pEntryNew->Core.KeyLast = off + cbData - 1;
|
---|
1656 | pEntryNew->pBlkCache = pBlkCache;
|
---|
1657 | pEntryNew->fFlags = 0;
|
---|
1658 | pEntryNew->cRefs = 1; /* We are using it now. */
|
---|
1659 | pEntryNew->pList = NULL;
|
---|
1660 | pEntryNew->cbData = (uint32_t)cbData;
|
---|
1661 | pEntryNew->pWaitingHead = NULL;
|
---|
1662 | pEntryNew->pWaitingTail = NULL;
|
---|
1663 | if (pbBuffer)
|
---|
1664 | pEntryNew->pbData = pbBuffer;
|
---|
1665 | else
|
---|
1666 | pEntryNew->pbData = (uint8_t *)RTMemPageAlloc(cbData);
|
---|
1667 |
|
---|
1668 | if (RT_UNLIKELY(!pEntryNew->pbData))
|
---|
1669 | {
|
---|
1670 | RTMemFree(pEntryNew);
|
---|
1671 | return NULL;
|
---|
1672 | }
|
---|
1673 |
|
---|
1674 | return pEntryNew;
|
---|
1675 | }
|
---|
1676 |
|
---|
1677 | /**
|
---|
1678 | * Checks that a set of flags is set/clear acquiring the R/W semaphore
|
---|
1679 | * in exclusive mode.
|
---|
1680 | *
|
---|
1681 | * @returns true if the flag in fSet is set and the one in fClear is clear.
|
---|
1682 | * false otherwise.
|
---|
1683 | * The R/W semaphore is only held if true is returned.
|
---|
1684 | *
|
---|
1685 | * @param pBlkCache The endpoint cache instance data.
|
---|
1686 | * @param pEntry The entry to check the flags for.
|
---|
1687 | * @param fSet The flag which is tested to be set.
|
---|
1688 | * @param fClear The flag which is tested to be clear.
|
---|
1689 | */
|
---|
1690 | DECLINLINE(bool) pdmBlkCacheEntryFlagIsSetClearAcquireLock(PPDMBLKCACHE pBlkCache,
|
---|
1691 | PPDMBLKCACHEENTRY pEntry,
|
---|
1692 | uint32_t fSet, uint32_t fClear)
|
---|
1693 | {
|
---|
1694 | uint32_t fFlags = ASMAtomicReadU32(&pEntry->fFlags);
|
---|
1695 | bool fPassed = ((fFlags & fSet) && !(fFlags & fClear));
|
---|
1696 |
|
---|
1697 | if (fPassed)
|
---|
1698 | {
|
---|
1699 | /* Acquire the lock and check again because the completion callback might have raced us. */
|
---|
1700 | RTSemRWRequestWrite(pBlkCache->SemRWEntries, RT_INDEFINITE_WAIT);
|
---|
1701 |
|
---|
1702 | fFlags = ASMAtomicReadU32(&pEntry->fFlags);
|
---|
1703 | fPassed = ((fFlags & fSet) && !(fFlags & fClear));
|
---|
1704 |
|
---|
1705 | /* Drop the lock if we didn't passed the test. */
|
---|
1706 | if (!fPassed)
|
---|
1707 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
1708 | }
|
---|
1709 |
|
---|
1710 | return fPassed;
|
---|
1711 | }
|
---|
1712 |
|
---|
1713 | /**
|
---|
1714 | * Adds a segment to the waiting list for a cache entry
|
---|
1715 | * which is currently in progress.
|
---|
1716 | *
|
---|
1717 | * @returns nothing.
|
---|
1718 | * @param pEntry The cache entry to add the segment to.
|
---|
1719 | * @param pWaiter The waiter entry to add.
|
---|
1720 | */
|
---|
1721 | DECLINLINE(void) pdmBlkCacheEntryAddWaiter(PPDMBLKCACHEENTRY pEntry,
|
---|
1722 | PPDMBLKCACHEWAITER pWaiter)
|
---|
1723 | {
|
---|
1724 | pWaiter->pNext = NULL;
|
---|
1725 |
|
---|
1726 | if (pEntry->pWaitingHead)
|
---|
1727 | {
|
---|
1728 | AssertPtr(pEntry->pWaitingTail);
|
---|
1729 |
|
---|
1730 | pEntry->pWaitingTail->pNext = pWaiter;
|
---|
1731 | pEntry->pWaitingTail = pWaiter;
|
---|
1732 | }
|
---|
1733 | else
|
---|
1734 | {
|
---|
1735 | Assert(!pEntry->pWaitingTail);
|
---|
1736 |
|
---|
1737 | pEntry->pWaitingHead = pWaiter;
|
---|
1738 | pEntry->pWaitingTail = pWaiter;
|
---|
1739 | }
|
---|
1740 | }
|
---|
1741 |
|
---|
1742 | /**
|
---|
1743 | * Add a buffer described by the I/O memory context
|
---|
1744 | * to the entry waiting for completion.
|
---|
1745 | *
|
---|
1746 | * @returns VBox status code.
|
---|
1747 | * @param pEntry The entry to add the buffer to.
|
---|
1748 | * @param pReq The request.
|
---|
1749 | * @param pSgBuf The scatter/gather buffer. Will be advanced by cbData.
|
---|
1750 | * @param offDiff Offset from the start of the buffer in the entry.
|
---|
1751 | * @param cbData Amount of data to wait for onthis entry.
|
---|
1752 | * @param fWrite Flag whether the task waits because it wants to write to
|
---|
1753 | * the cache entry.
|
---|
1754 | */
|
---|
1755 | static int pdmBlkCacheEntryWaitersAdd(PPDMBLKCACHEENTRY pEntry, PPDMBLKCACHEREQ pReq,
|
---|
1756 | PRTSGBUF pSgBuf, uint64_t offDiff, size_t cbData, bool fWrite)
|
---|
1757 | {
|
---|
1758 | PPDMBLKCACHEWAITER pWaiter = (PPDMBLKCACHEWAITER)RTMemAllocZ(sizeof(PDMBLKCACHEWAITER));
|
---|
1759 | if (!pWaiter)
|
---|
1760 | return VERR_NO_MEMORY;
|
---|
1761 |
|
---|
1762 | ASMAtomicIncU32(&pReq->cXfersPending);
|
---|
1763 | pWaiter->pReq = pReq;
|
---|
1764 | pWaiter->offCacheEntry = offDiff;
|
---|
1765 | pWaiter->cbTransfer = cbData;
|
---|
1766 | pWaiter->fWrite = fWrite;
|
---|
1767 | RTSgBufClone(&pWaiter->SgBuf, pSgBuf);
|
---|
1768 | RTSgBufAdvance(pSgBuf, cbData);
|
---|
1769 |
|
---|
1770 | pdmBlkCacheEntryAddWaiter(pEntry, pWaiter);
|
---|
1771 |
|
---|
1772 | return VINF_SUCCESS;
|
---|
1773 | }
|
---|
1774 |
|
---|
1775 | /**
|
---|
1776 | * Calculate aligned offset and size for a new cache entry which do not
|
---|
1777 | * intersect with an already existing entry and the file end.
|
---|
1778 | *
|
---|
1779 | * @returns The number of bytes the entry can hold of the requested amount
|
---|
1780 | * of bytes.
|
---|
1781 | * @param pBlkCache The endpoint cache.
|
---|
1782 | * @param off The start offset.
|
---|
1783 | * @param cb The number of bytes the entry needs to hold at
|
---|
1784 | * least.
|
---|
1785 | * @param pcbEntry Where to store the number of bytes the entry can hold.
|
---|
1786 | * Can be less than given because of other entries.
|
---|
1787 | */
|
---|
1788 | static uint32_t pdmBlkCacheEntryBoundariesCalc(PPDMBLKCACHE pBlkCache,
|
---|
1789 | uint64_t off, uint32_t cb,
|
---|
1790 | uint32_t *pcbEntry)
|
---|
1791 | {
|
---|
1792 | /* Get the best fit entries around the offset */
|
---|
1793 | PPDMBLKCACHEENTRY pEntryAbove = NULL;
|
---|
1794 | pdmBlkCacheGetCacheBestFitEntryByOffset(pBlkCache, off, &pEntryAbove);
|
---|
1795 |
|
---|
1796 | /* Log the info */
|
---|
1797 | LogFlow(("%sest fit entry above off=%llu (BestFit=%llu BestFitEnd=%llu BestFitSize=%u)\n",
|
---|
1798 | pEntryAbove ? "B" : "No b",
|
---|
1799 | off,
|
---|
1800 | pEntryAbove ? pEntryAbove->Core.Key : 0,
|
---|
1801 | pEntryAbove ? pEntryAbove->Core.KeyLast : 0,
|
---|
1802 | pEntryAbove ? pEntryAbove->cbData : 0));
|
---|
1803 |
|
---|
1804 | uint32_t cbNext;
|
---|
1805 | uint32_t cbInEntry;
|
---|
1806 | if ( pEntryAbove
|
---|
1807 | && off + cb > pEntryAbove->Core.Key)
|
---|
1808 | {
|
---|
1809 | cbInEntry = (uint32_t)(pEntryAbove->Core.Key - off);
|
---|
1810 | cbNext = (uint32_t)(pEntryAbove->Core.Key - off);
|
---|
1811 | }
|
---|
1812 | else
|
---|
1813 | {
|
---|
1814 | cbInEntry = cb;
|
---|
1815 | cbNext = cb;
|
---|
1816 | }
|
---|
1817 |
|
---|
1818 | /* A few sanity checks */
|
---|
1819 | AssertMsg(!pEntryAbove || off + cbNext <= pEntryAbove->Core.Key,
|
---|
1820 | ("Aligned size intersects with another cache entry\n"));
|
---|
1821 | Assert(cbInEntry <= cbNext);
|
---|
1822 |
|
---|
1823 | if (pEntryAbove)
|
---|
1824 | pdmBlkCacheEntryRelease(pEntryAbove);
|
---|
1825 |
|
---|
1826 | LogFlow(("off=%llu cbNext=%u\n", off, cbNext));
|
---|
1827 |
|
---|
1828 | *pcbEntry = cbNext;
|
---|
1829 |
|
---|
1830 | return cbInEntry;
|
---|
1831 | }
|
---|
1832 |
|
---|
1833 | /**
|
---|
1834 | * Create a new cache entry evicting data from the cache if required.
|
---|
1835 | *
|
---|
1836 | * @returns Pointer to the new cache entry or NULL
|
---|
1837 | * if not enough bytes could be evicted from the cache.
|
---|
1838 | * @param pBlkCache The endpoint cache.
|
---|
1839 | * @param off The offset.
|
---|
1840 | * @param cb Number of bytes the cache entry should have.
|
---|
1841 | * @param pcbData Where to store the number of bytes the new
|
---|
1842 | * entry can hold. May be lower than actually
|
---|
1843 | * requested due to another entry intersecting the
|
---|
1844 | * access range.
|
---|
1845 | */
|
---|
1846 | static PPDMBLKCACHEENTRY pdmBlkCacheEntryCreate(PPDMBLKCACHE pBlkCache, uint64_t off, size_t cb, size_t *pcbData)
|
---|
1847 | {
|
---|
1848 | uint32_t cbEntry = 0;
|
---|
1849 |
|
---|
1850 | *pcbData = pdmBlkCacheEntryBoundariesCalc(pBlkCache, off, (uint32_t)cb, &cbEntry);
|
---|
1851 | AssertReturn(cb <= UINT32_MAX, NULL);
|
---|
1852 |
|
---|
1853 | PPDMBLKCACHEGLOBAL pCache = pBlkCache->pCache;
|
---|
1854 | pdmBlkCacheLockEnter(pCache);
|
---|
1855 |
|
---|
1856 | PPDMBLKCACHEENTRY pEntryNew = NULL;
|
---|
1857 | uint8_t *pbBuffer = NULL;
|
---|
1858 | bool fEnough = pdmBlkCacheReclaim(pCache, cbEntry, true, &pbBuffer);
|
---|
1859 | if (fEnough)
|
---|
1860 | {
|
---|
1861 | LogFlow(("Evicted enough bytes (%u requested). Creating new cache entry\n", cbEntry));
|
---|
1862 |
|
---|
1863 | pEntryNew = pdmBlkCacheEntryAlloc(pBlkCache, off, cbEntry, pbBuffer);
|
---|
1864 | if (RT_LIKELY(pEntryNew))
|
---|
1865 | {
|
---|
1866 | pdmBlkCacheEntryAddToList(&pCache->LruRecentlyUsedIn, pEntryNew);
|
---|
1867 | pdmBlkCacheAdd(pCache, cbEntry);
|
---|
1868 | pdmBlkCacheLockLeave(pCache);
|
---|
1869 |
|
---|
1870 | pdmBlkCacheInsertEntry(pBlkCache, pEntryNew);
|
---|
1871 |
|
---|
1872 | AssertMsg( (off >= pEntryNew->Core.Key)
|
---|
1873 | && (off + *pcbData <= pEntryNew->Core.KeyLast + 1),
|
---|
1874 | ("Overflow in calculation off=%llu\n", off));
|
---|
1875 | }
|
---|
1876 | else
|
---|
1877 | pdmBlkCacheLockLeave(pCache);
|
---|
1878 | }
|
---|
1879 | else
|
---|
1880 | pdmBlkCacheLockLeave(pCache);
|
---|
1881 |
|
---|
1882 | return pEntryNew;
|
---|
1883 | }
|
---|
1884 |
|
---|
1885 | static PPDMBLKCACHEREQ pdmBlkCacheReqAlloc(void *pvUser)
|
---|
1886 | {
|
---|
1887 | PPDMBLKCACHEREQ pReq = (PPDMBLKCACHEREQ)RTMemAlloc(sizeof(PDMBLKCACHEREQ));
|
---|
1888 |
|
---|
1889 | if (RT_LIKELY(pReq))
|
---|
1890 | {
|
---|
1891 | pReq->pvUser = pvUser;
|
---|
1892 | pReq->rcReq = VINF_SUCCESS;
|
---|
1893 | pReq->cXfersPending = 0;
|
---|
1894 | }
|
---|
1895 |
|
---|
1896 | return pReq;
|
---|
1897 | }
|
---|
1898 |
|
---|
1899 | static void pdmBlkCacheReqComplete(PPDMBLKCACHE pBlkCache, PPDMBLKCACHEREQ pReq)
|
---|
1900 | {
|
---|
1901 | switch (pBlkCache->enmType)
|
---|
1902 | {
|
---|
1903 | case PDMBLKCACHETYPE_DEV:
|
---|
1904 | {
|
---|
1905 | pBlkCache->u.Dev.pfnXferComplete(pBlkCache->u.Dev.pDevIns,
|
---|
1906 | pReq->pvUser, pReq->rcReq);
|
---|
1907 | break;
|
---|
1908 | }
|
---|
1909 | case PDMBLKCACHETYPE_DRV:
|
---|
1910 | {
|
---|
1911 | pBlkCache->u.Drv.pfnXferComplete(pBlkCache->u.Drv.pDrvIns,
|
---|
1912 | pReq->pvUser, pReq->rcReq);
|
---|
1913 | break;
|
---|
1914 | }
|
---|
1915 | case PDMBLKCACHETYPE_USB:
|
---|
1916 | {
|
---|
1917 | pBlkCache->u.Usb.pfnXferComplete(pBlkCache->u.Usb.pUsbIns,
|
---|
1918 | pReq->pvUser, pReq->rcReq);
|
---|
1919 | break;
|
---|
1920 | }
|
---|
1921 | case PDMBLKCACHETYPE_INTERNAL:
|
---|
1922 | {
|
---|
1923 | pBlkCache->u.Int.pfnXferComplete(pBlkCache->u.Int.pvUser,
|
---|
1924 | pReq->pvUser, pReq->rcReq);
|
---|
1925 | break;
|
---|
1926 | }
|
---|
1927 | default:
|
---|
1928 | AssertMsgFailed(("Unknown block cache type!\n"));
|
---|
1929 | }
|
---|
1930 |
|
---|
1931 | RTMemFree(pReq);
|
---|
1932 | }
|
---|
1933 |
|
---|
1934 | static bool pdmBlkCacheReqUpdate(PPDMBLKCACHE pBlkCache, PPDMBLKCACHEREQ pReq,
|
---|
1935 | int rcReq, bool fCallHandler)
|
---|
1936 | {
|
---|
1937 | if (RT_FAILURE(rcReq))
|
---|
1938 | ASMAtomicCmpXchgS32(&pReq->rcReq, rcReq, VINF_SUCCESS);
|
---|
1939 |
|
---|
1940 | AssertMsg(pReq->cXfersPending > 0, ("No transfers are pending for this request\n"));
|
---|
1941 | uint32_t cXfersPending = ASMAtomicDecU32(&pReq->cXfersPending);
|
---|
1942 |
|
---|
1943 | if (!cXfersPending)
|
---|
1944 | {
|
---|
1945 | if (fCallHandler)
|
---|
1946 | pdmBlkCacheReqComplete(pBlkCache, pReq);
|
---|
1947 | return true;
|
---|
1948 | }
|
---|
1949 |
|
---|
1950 | LogFlowFunc(("pReq=%#p cXfersPending=%u\n", pReq, cXfersPending));
|
---|
1951 | return false;
|
---|
1952 | }
|
---|
1953 |
|
---|
1954 | VMMR3DECL(int) PDMR3BlkCacheRead(PPDMBLKCACHE pBlkCache, uint64_t off,
|
---|
1955 | PCRTSGBUF pSgBuf, size_t cbRead, void *pvUser)
|
---|
1956 | {
|
---|
1957 | int rc = VINF_SUCCESS;
|
---|
1958 | PPDMBLKCACHEGLOBAL pCache = pBlkCache->pCache;
|
---|
1959 | PPDMBLKCACHEENTRY pEntry;
|
---|
1960 | PPDMBLKCACHEREQ pReq;
|
---|
1961 |
|
---|
1962 | LogFlowFunc((": pBlkCache=%#p{%s} off=%llu pSgBuf=%#p cbRead=%u pvUser=%#p\n",
|
---|
1963 | pBlkCache, pBlkCache->pszId, off, pSgBuf, cbRead, pvUser));
|
---|
1964 |
|
---|
1965 | AssertPtrReturn(pBlkCache, VERR_INVALID_POINTER);
|
---|
1966 | AssertReturn(!pBlkCache->fSuspended, VERR_INVALID_STATE);
|
---|
1967 |
|
---|
1968 | RTSGBUF SgBuf;
|
---|
1969 | RTSgBufClone(&SgBuf, pSgBuf);
|
---|
1970 |
|
---|
1971 | /* Allocate new request structure. */
|
---|
1972 | pReq = pdmBlkCacheReqAlloc(pvUser);
|
---|
1973 | if (RT_UNLIKELY(!pReq))
|
---|
1974 | return VERR_NO_MEMORY;
|
---|
1975 |
|
---|
1976 | /* Increment data transfer counter to keep the request valid while we access it. */
|
---|
1977 | ASMAtomicIncU32(&pReq->cXfersPending);
|
---|
1978 |
|
---|
1979 | while (cbRead)
|
---|
1980 | {
|
---|
1981 | size_t cbToRead;
|
---|
1982 |
|
---|
1983 | pEntry = pdmBlkCacheGetCacheEntryByOffset(pBlkCache, off);
|
---|
1984 |
|
---|
1985 | /*
|
---|
1986 | * If there is no entry we try to create a new one eviciting unused pages
|
---|
1987 | * if the cache is full. If this is not possible we will pass the request through
|
---|
1988 | * and skip the caching (all entries may be still in progress so they can't
|
---|
1989 | * be evicted)
|
---|
1990 | * If we have an entry it can be in one of the LRU lists where the entry
|
---|
1991 | * contains data (recently used or frequently used LRU) so we can just read
|
---|
1992 | * the data we need and put the entry at the head of the frequently used LRU list.
|
---|
1993 | * In case the entry is in one of the ghost lists it doesn't contain any data.
|
---|
1994 | * We have to fetch it again evicting pages from either T1 or T2 to make room.
|
---|
1995 | */
|
---|
1996 | if (pEntry)
|
---|
1997 | {
|
---|
1998 | uint64_t offDiff = off - pEntry->Core.Key;
|
---|
1999 |
|
---|
2000 | AssertMsg(off >= pEntry->Core.Key,
|
---|
2001 | ("Overflow in calculation off=%llu OffsetAligned=%llu\n",
|
---|
2002 | off, pEntry->Core.Key));
|
---|
2003 |
|
---|
2004 | AssertPtr(pEntry->pList);
|
---|
2005 |
|
---|
2006 | cbToRead = RT_MIN(pEntry->cbData - offDiff, cbRead);
|
---|
2007 |
|
---|
2008 | AssertMsg(off + cbToRead <= pEntry->Core.Key + pEntry->Core.KeyLast + 1,
|
---|
2009 | ("Buffer of cache entry exceeded off=%llu cbToRead=%d\n",
|
---|
2010 | off, cbToRead));
|
---|
2011 |
|
---|
2012 | cbRead -= cbToRead;
|
---|
2013 |
|
---|
2014 | if (!cbRead)
|
---|
2015 | STAM_COUNTER_INC(&pCache->cHits);
|
---|
2016 | else
|
---|
2017 | STAM_COUNTER_INC(&pCache->cPartialHits);
|
---|
2018 |
|
---|
2019 | STAM_COUNTER_ADD(&pCache->StatRead, cbToRead);
|
---|
2020 |
|
---|
2021 | /* Ghost lists contain no data. */
|
---|
2022 | if ( (pEntry->pList == &pCache->LruRecentlyUsedIn)
|
---|
2023 | || (pEntry->pList == &pCache->LruFrequentlyUsed))
|
---|
2024 | {
|
---|
2025 | if (pdmBlkCacheEntryFlagIsSetClearAcquireLock(pBlkCache, pEntry,
|
---|
2026 | PDMBLKCACHE_ENTRY_IO_IN_PROGRESS,
|
---|
2027 | PDMBLKCACHE_ENTRY_IS_DIRTY))
|
---|
2028 | {
|
---|
2029 | /* Entry didn't completed yet. Append to the list */
|
---|
2030 | pdmBlkCacheEntryWaitersAdd(pEntry, pReq,
|
---|
2031 | &SgBuf, offDiff, cbToRead,
|
---|
2032 | false /* fWrite */);
|
---|
2033 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
2034 | }
|
---|
2035 | else
|
---|
2036 | {
|
---|
2037 | /* Read as much as we can from the entry. */
|
---|
2038 | RTSgBufCopyFromBuf(&SgBuf, pEntry->pbData + offDiff, cbToRead);
|
---|
2039 | }
|
---|
2040 |
|
---|
2041 | /* Move this entry to the top position */
|
---|
2042 | if (pEntry->pList == &pCache->LruFrequentlyUsed)
|
---|
2043 | {
|
---|
2044 | pdmBlkCacheLockEnter(pCache);
|
---|
2045 | pdmBlkCacheEntryAddToList(&pCache->LruFrequentlyUsed, pEntry);
|
---|
2046 | pdmBlkCacheLockLeave(pCache);
|
---|
2047 | }
|
---|
2048 | /* Release the entry */
|
---|
2049 | pdmBlkCacheEntryRelease(pEntry);
|
---|
2050 | }
|
---|
2051 | else
|
---|
2052 | {
|
---|
2053 | uint8_t *pbBuffer = NULL;
|
---|
2054 |
|
---|
2055 | LogFlow(("Fetching data for ghost entry %#p from file\n", pEntry));
|
---|
2056 |
|
---|
2057 | pdmBlkCacheLockEnter(pCache);
|
---|
2058 | pdmBlkCacheEntryRemoveFromList(pEntry); /* Remove it before we remove data, otherwise it may get freed when evicting data. */
|
---|
2059 | bool fEnough = pdmBlkCacheReclaim(pCache, pEntry->cbData, true, &pbBuffer);
|
---|
2060 |
|
---|
2061 | /* Move the entry to Am and fetch it to the cache. */
|
---|
2062 | if (fEnough)
|
---|
2063 | {
|
---|
2064 | pdmBlkCacheEntryAddToList(&pCache->LruFrequentlyUsed, pEntry);
|
---|
2065 | pdmBlkCacheAdd(pCache, pEntry->cbData);
|
---|
2066 | pdmBlkCacheLockLeave(pCache);
|
---|
2067 |
|
---|
2068 | if (pbBuffer)
|
---|
2069 | pEntry->pbData = pbBuffer;
|
---|
2070 | else
|
---|
2071 | pEntry->pbData = (uint8_t *)RTMemPageAlloc(pEntry->cbData);
|
---|
2072 | AssertPtr(pEntry->pbData);
|
---|
2073 |
|
---|
2074 | pdmBlkCacheEntryWaitersAdd(pEntry, pReq,
|
---|
2075 | &SgBuf, offDiff, cbToRead,
|
---|
2076 | false /* fWrite */);
|
---|
2077 | pdmBlkCacheEntryReadFromMedium(pEntry);
|
---|
2078 | /* Release the entry */
|
---|
2079 | pdmBlkCacheEntryRelease(pEntry);
|
---|
2080 | }
|
---|
2081 | else
|
---|
2082 | {
|
---|
2083 | RTSemRWRequestWrite(pBlkCache->SemRWEntries, RT_INDEFINITE_WAIT);
|
---|
2084 | STAM_PROFILE_ADV_START(&pCache->StatTreeRemove, Cache);
|
---|
2085 | RTAvlrU64Remove(pBlkCache->pTree, pEntry->Core.Key);
|
---|
2086 | STAM_PROFILE_ADV_STOP(&pCache->StatTreeRemove, Cache);
|
---|
2087 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
2088 |
|
---|
2089 | pdmBlkCacheLockLeave(pCache);
|
---|
2090 |
|
---|
2091 | RTMemFree(pEntry);
|
---|
2092 |
|
---|
2093 | pdmBlkCacheRequestPassthrough(pBlkCache, pReq,
|
---|
2094 | &SgBuf, off, cbToRead,
|
---|
2095 | PDMBLKCACHEXFERDIR_READ);
|
---|
2096 | }
|
---|
2097 | }
|
---|
2098 | }
|
---|
2099 | else
|
---|
2100 | {
|
---|
2101 | #ifdef VBOX_WITH_IO_READ_CACHE
|
---|
2102 | /* No entry found for this offset. Create a new entry and fetch the data to the cache. */
|
---|
2103 | PPDMBLKCACHEENTRY pEntryNew = pdmBlkCacheEntryCreate(pBlkCache,
|
---|
2104 | off, cbRead,
|
---|
2105 | &cbToRead);
|
---|
2106 |
|
---|
2107 | cbRead -= cbToRead;
|
---|
2108 |
|
---|
2109 | if (pEntryNew)
|
---|
2110 | {
|
---|
2111 | if (!cbRead)
|
---|
2112 | STAM_COUNTER_INC(&pCache->cMisses);
|
---|
2113 | else
|
---|
2114 | STAM_COUNTER_INC(&pCache->cPartialHits);
|
---|
2115 |
|
---|
2116 | pdmBlkCacheEntryWaitersAdd(pEntryNew, pReq,
|
---|
2117 | &SgBuf,
|
---|
2118 | off - pEntryNew->Core.Key,
|
---|
2119 | cbToRead,
|
---|
2120 | false /* fWrite */);
|
---|
2121 | pdmBlkCacheEntryReadFromMedium(pEntryNew);
|
---|
2122 | pdmBlkCacheEntryRelease(pEntryNew); /* it is protected by the I/O in progress flag now. */
|
---|
2123 | }
|
---|
2124 | else
|
---|
2125 | {
|
---|
2126 | /*
|
---|
2127 | * There is not enough free space in the cache.
|
---|
2128 | * Pass the request directly to the I/O manager.
|
---|
2129 | */
|
---|
2130 | LogFlow(("Couldn't evict %u bytes from the cache. Remaining request will be passed through\n", cbToRead));
|
---|
2131 |
|
---|
2132 | pdmBlkCacheRequestPassthrough(pBlkCache, pReq,
|
---|
2133 | &SgBuf, off, cbToRead,
|
---|
2134 | PDMBLKCACHEXFERDIR_READ);
|
---|
2135 | }
|
---|
2136 | #else
|
---|
2137 | /* Clip read size if necessary. */
|
---|
2138 | PPDMBLKCACHEENTRY pEntryAbove;
|
---|
2139 | pdmBlkCacheGetCacheBestFitEntryByOffset(pBlkCache, off, &pEntryAbove);
|
---|
2140 |
|
---|
2141 | if (pEntryAbove)
|
---|
2142 | {
|
---|
2143 | if (off + cbRead > pEntryAbove->Core.Key)
|
---|
2144 | cbToRead = pEntryAbove->Core.Key - off;
|
---|
2145 | else
|
---|
2146 | cbToRead = cbRead;
|
---|
2147 |
|
---|
2148 | pdmBlkCacheEntryRelease(pEntryAbove);
|
---|
2149 | }
|
---|
2150 | else
|
---|
2151 | cbToRead = cbRead;
|
---|
2152 |
|
---|
2153 | cbRead -= cbToRead;
|
---|
2154 | pdmBlkCacheRequestPassthrough(pBlkCache, pReq,
|
---|
2155 | &SgBuf, off, cbToRead,
|
---|
2156 | PDMBLKCACHEXFERDIR_READ);
|
---|
2157 | #endif
|
---|
2158 | }
|
---|
2159 | off += cbToRead;
|
---|
2160 | }
|
---|
2161 |
|
---|
2162 | if (!pdmBlkCacheReqUpdate(pBlkCache, pReq, rc, false))
|
---|
2163 | rc = VINF_AIO_TASK_PENDING;
|
---|
2164 | else
|
---|
2165 | {
|
---|
2166 | rc = pReq->rcReq;
|
---|
2167 | RTMemFree(pReq);
|
---|
2168 | }
|
---|
2169 |
|
---|
2170 | LogFlowFunc((": Leave rc=%Rrc\n", rc));
|
---|
2171 |
|
---|
2172 | return rc;
|
---|
2173 | }
|
---|
2174 |
|
---|
2175 | VMMR3DECL(int) PDMR3BlkCacheWrite(PPDMBLKCACHE pBlkCache, uint64_t off, PCRTSGBUF pSgBuf, size_t cbWrite, void *pvUser)
|
---|
2176 | {
|
---|
2177 | int rc = VINF_SUCCESS;
|
---|
2178 | PPDMBLKCACHEGLOBAL pCache = pBlkCache->pCache;
|
---|
2179 | PPDMBLKCACHEENTRY pEntry;
|
---|
2180 | PPDMBLKCACHEREQ pReq;
|
---|
2181 |
|
---|
2182 | LogFlowFunc((": pBlkCache=%#p{%s} off=%llu pSgBuf=%#p cbWrite=%u pvUser=%#p\n",
|
---|
2183 | pBlkCache, pBlkCache->pszId, off, pSgBuf, cbWrite, pvUser));
|
---|
2184 |
|
---|
2185 | AssertPtrReturn(pBlkCache, VERR_INVALID_POINTER);
|
---|
2186 | AssertReturn(!pBlkCache->fSuspended, VERR_INVALID_STATE);
|
---|
2187 |
|
---|
2188 | RTSGBUF SgBuf;
|
---|
2189 | RTSgBufClone(&SgBuf, pSgBuf);
|
---|
2190 |
|
---|
2191 | /* Allocate new request structure. */
|
---|
2192 | pReq = pdmBlkCacheReqAlloc(pvUser);
|
---|
2193 | if (RT_UNLIKELY(!pReq))
|
---|
2194 | return VERR_NO_MEMORY;
|
---|
2195 |
|
---|
2196 | /* Increment data transfer counter to keep the request valid while we access it. */
|
---|
2197 | ASMAtomicIncU32(&pReq->cXfersPending);
|
---|
2198 |
|
---|
2199 | while (cbWrite)
|
---|
2200 | {
|
---|
2201 | size_t cbToWrite;
|
---|
2202 |
|
---|
2203 | pEntry = pdmBlkCacheGetCacheEntryByOffset(pBlkCache, off);
|
---|
2204 | if (pEntry)
|
---|
2205 | {
|
---|
2206 | /* Write the data into the entry and mark it as dirty */
|
---|
2207 | AssertPtr(pEntry->pList);
|
---|
2208 |
|
---|
2209 | uint64_t offDiff = off - pEntry->Core.Key;
|
---|
2210 | AssertMsg(off >= pEntry->Core.Key, ("Overflow in calculation off=%llu OffsetAligned=%llu\n", off, pEntry->Core.Key));
|
---|
2211 |
|
---|
2212 | cbToWrite = RT_MIN(pEntry->cbData - offDiff, cbWrite);
|
---|
2213 | cbWrite -= cbToWrite;
|
---|
2214 |
|
---|
2215 | if (!cbWrite)
|
---|
2216 | STAM_COUNTER_INC(&pCache->cHits);
|
---|
2217 | else
|
---|
2218 | STAM_COUNTER_INC(&pCache->cPartialHits);
|
---|
2219 |
|
---|
2220 | STAM_COUNTER_ADD(&pCache->StatWritten, cbToWrite);
|
---|
2221 |
|
---|
2222 | /* Ghost lists contain no data. */
|
---|
2223 | if ( (pEntry->pList == &pCache->LruRecentlyUsedIn)
|
---|
2224 | || (pEntry->pList == &pCache->LruFrequentlyUsed))
|
---|
2225 | {
|
---|
2226 | /* Check if the entry is dirty. */
|
---|
2227 | if (pdmBlkCacheEntryFlagIsSetClearAcquireLock(pBlkCache, pEntry,
|
---|
2228 | PDMBLKCACHE_ENTRY_IS_DIRTY,
|
---|
2229 | 0))
|
---|
2230 | {
|
---|
2231 | /* If it is already dirty but not in progress just update the data. */
|
---|
2232 | if (!(pEntry->fFlags & PDMBLKCACHE_ENTRY_IO_IN_PROGRESS))
|
---|
2233 | RTSgBufCopyToBuf(&SgBuf, pEntry->pbData + offDiff, cbToWrite);
|
---|
2234 | else
|
---|
2235 | {
|
---|
2236 | /* The data isn't written to the file yet */
|
---|
2237 | pdmBlkCacheEntryWaitersAdd(pEntry, pReq,
|
---|
2238 | &SgBuf, offDiff, cbToWrite,
|
---|
2239 | true /* fWrite */);
|
---|
2240 | STAM_COUNTER_INC(&pBlkCache->StatWriteDeferred);
|
---|
2241 | }
|
---|
2242 |
|
---|
2243 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
2244 | }
|
---|
2245 | else /* Dirty bit not set */
|
---|
2246 | {
|
---|
2247 | /*
|
---|
2248 | * Check if a read is in progress for this entry.
|
---|
2249 | * We have to defer processing in that case.
|
---|
2250 | */
|
---|
2251 | if (pdmBlkCacheEntryFlagIsSetClearAcquireLock(pBlkCache, pEntry,
|
---|
2252 | PDMBLKCACHE_ENTRY_IO_IN_PROGRESS,
|
---|
2253 | 0))
|
---|
2254 | {
|
---|
2255 | pdmBlkCacheEntryWaitersAdd(pEntry, pReq,
|
---|
2256 | &SgBuf, offDiff, cbToWrite,
|
---|
2257 | true /* fWrite */);
|
---|
2258 | STAM_COUNTER_INC(&pBlkCache->StatWriteDeferred);
|
---|
2259 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
2260 | }
|
---|
2261 | else /* I/O in progress flag not set */
|
---|
2262 | {
|
---|
2263 | /* Write as much as we can into the entry and update the file. */
|
---|
2264 | RTSgBufCopyToBuf(&SgBuf, pEntry->pbData + offDiff, cbToWrite);
|
---|
2265 |
|
---|
2266 | bool fCommit = pdmBlkCacheAddDirtyEntry(pBlkCache, pEntry);
|
---|
2267 | if (fCommit)
|
---|
2268 | pdmBlkCacheCommitDirtyEntries(pCache);
|
---|
2269 | }
|
---|
2270 | } /* Dirty bit not set */
|
---|
2271 |
|
---|
2272 | /* Move this entry to the top position */
|
---|
2273 | if (pEntry->pList == &pCache->LruFrequentlyUsed)
|
---|
2274 | {
|
---|
2275 | pdmBlkCacheLockEnter(pCache);
|
---|
2276 | pdmBlkCacheEntryAddToList(&pCache->LruFrequentlyUsed, pEntry);
|
---|
2277 | pdmBlkCacheLockLeave(pCache);
|
---|
2278 | }
|
---|
2279 |
|
---|
2280 | pdmBlkCacheEntryRelease(pEntry);
|
---|
2281 | }
|
---|
2282 | else /* Entry is on the ghost list */
|
---|
2283 | {
|
---|
2284 | uint8_t *pbBuffer = NULL;
|
---|
2285 |
|
---|
2286 | pdmBlkCacheLockEnter(pCache);
|
---|
2287 | pdmBlkCacheEntryRemoveFromList(pEntry); /* Remove it before we remove data, otherwise it may get freed when evicting data. */
|
---|
2288 | bool fEnough = pdmBlkCacheReclaim(pCache, pEntry->cbData, true, &pbBuffer);
|
---|
2289 |
|
---|
2290 | if (fEnough)
|
---|
2291 | {
|
---|
2292 | /* Move the entry to Am and fetch it to the cache. */
|
---|
2293 | pdmBlkCacheEntryAddToList(&pCache->LruFrequentlyUsed, pEntry);
|
---|
2294 | pdmBlkCacheAdd(pCache, pEntry->cbData);
|
---|
2295 | pdmBlkCacheLockLeave(pCache);
|
---|
2296 |
|
---|
2297 | if (pbBuffer)
|
---|
2298 | pEntry->pbData = pbBuffer;
|
---|
2299 | else
|
---|
2300 | pEntry->pbData = (uint8_t *)RTMemPageAlloc(pEntry->cbData);
|
---|
2301 | AssertPtr(pEntry->pbData);
|
---|
2302 |
|
---|
2303 | pdmBlkCacheEntryWaitersAdd(pEntry, pReq,
|
---|
2304 | &SgBuf, offDiff, cbToWrite,
|
---|
2305 | true /* fWrite */);
|
---|
2306 | STAM_COUNTER_INC(&pBlkCache->StatWriteDeferred);
|
---|
2307 | pdmBlkCacheEntryReadFromMedium(pEntry);
|
---|
2308 |
|
---|
2309 | /* Release the reference. If it is still needed the I/O in progress flag should protect it now. */
|
---|
2310 | pdmBlkCacheEntryRelease(pEntry);
|
---|
2311 | }
|
---|
2312 | else
|
---|
2313 | {
|
---|
2314 | RTSemRWRequestWrite(pBlkCache->SemRWEntries, RT_INDEFINITE_WAIT);
|
---|
2315 | STAM_PROFILE_ADV_START(&pCache->StatTreeRemove, Cache);
|
---|
2316 | RTAvlrU64Remove(pBlkCache->pTree, pEntry->Core.Key);
|
---|
2317 | STAM_PROFILE_ADV_STOP(&pCache->StatTreeRemove, Cache);
|
---|
2318 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
2319 |
|
---|
2320 | pdmBlkCacheLockLeave(pCache);
|
---|
2321 |
|
---|
2322 | RTMemFree(pEntry);
|
---|
2323 | pdmBlkCacheRequestPassthrough(pBlkCache, pReq,
|
---|
2324 | &SgBuf, off, cbToWrite,
|
---|
2325 | PDMBLKCACHEXFERDIR_WRITE);
|
---|
2326 | }
|
---|
2327 | }
|
---|
2328 | }
|
---|
2329 | else /* No entry found */
|
---|
2330 | {
|
---|
2331 | /*
|
---|
2332 | * No entry found. Try to create a new cache entry to store the data in and if that fails
|
---|
2333 | * write directly to the file.
|
---|
2334 | */
|
---|
2335 | PPDMBLKCACHEENTRY pEntryNew = pdmBlkCacheEntryCreate(pBlkCache,
|
---|
2336 | off, cbWrite,
|
---|
2337 | &cbToWrite);
|
---|
2338 |
|
---|
2339 | cbWrite -= cbToWrite;
|
---|
2340 |
|
---|
2341 | if (pEntryNew)
|
---|
2342 | {
|
---|
2343 | uint64_t offDiff = off - pEntryNew->Core.Key;
|
---|
2344 |
|
---|
2345 | STAM_COUNTER_INC(&pCache->cHits);
|
---|
2346 |
|
---|
2347 | /*
|
---|
2348 | * Check if it is possible to just write the data without waiting
|
---|
2349 | * for it to get fetched first.
|
---|
2350 | */
|
---|
2351 | if (!offDiff && pEntryNew->cbData == cbToWrite)
|
---|
2352 | {
|
---|
2353 | RTSgBufCopyToBuf(&SgBuf, pEntryNew->pbData, cbToWrite);
|
---|
2354 |
|
---|
2355 | bool fCommit = pdmBlkCacheAddDirtyEntry(pBlkCache, pEntryNew);
|
---|
2356 | if (fCommit)
|
---|
2357 | pdmBlkCacheCommitDirtyEntries(pCache);
|
---|
2358 | STAM_COUNTER_ADD(&pCache->StatWritten, cbToWrite);
|
---|
2359 | }
|
---|
2360 | else
|
---|
2361 | {
|
---|
2362 | /* Defer the write and fetch the data from the endpoint. */
|
---|
2363 | pdmBlkCacheEntryWaitersAdd(pEntryNew, pReq,
|
---|
2364 | &SgBuf, offDiff, cbToWrite,
|
---|
2365 | true /* fWrite */);
|
---|
2366 | STAM_COUNTER_INC(&pBlkCache->StatWriteDeferred);
|
---|
2367 | pdmBlkCacheEntryReadFromMedium(pEntryNew);
|
---|
2368 | }
|
---|
2369 |
|
---|
2370 | pdmBlkCacheEntryRelease(pEntryNew);
|
---|
2371 | }
|
---|
2372 | else
|
---|
2373 | {
|
---|
2374 | /*
|
---|
2375 | * There is not enough free space in the cache.
|
---|
2376 | * Pass the request directly to the I/O manager.
|
---|
2377 | */
|
---|
2378 | LogFlow(("Couldn't evict %u bytes from the cache. Remaining request will be passed through\n", cbToWrite));
|
---|
2379 |
|
---|
2380 | STAM_COUNTER_INC(&pCache->cMisses);
|
---|
2381 |
|
---|
2382 | pdmBlkCacheRequestPassthrough(pBlkCache, pReq,
|
---|
2383 | &SgBuf, off, cbToWrite,
|
---|
2384 | PDMBLKCACHEXFERDIR_WRITE);
|
---|
2385 | }
|
---|
2386 | }
|
---|
2387 |
|
---|
2388 | off += cbToWrite;
|
---|
2389 | }
|
---|
2390 |
|
---|
2391 | if (!pdmBlkCacheReqUpdate(pBlkCache, pReq, rc, false))
|
---|
2392 | rc = VINF_AIO_TASK_PENDING;
|
---|
2393 | else
|
---|
2394 | {
|
---|
2395 | rc = pReq->rcReq;
|
---|
2396 | RTMemFree(pReq);
|
---|
2397 | }
|
---|
2398 |
|
---|
2399 | LogFlowFunc((": Leave rc=%Rrc\n", rc));
|
---|
2400 |
|
---|
2401 | return rc;
|
---|
2402 | }
|
---|
2403 |
|
---|
2404 | VMMR3DECL(int) PDMR3BlkCacheFlush(PPDMBLKCACHE pBlkCache, void *pvUser)
|
---|
2405 | {
|
---|
2406 | int rc = VINF_SUCCESS;
|
---|
2407 | PPDMBLKCACHEREQ pReq;
|
---|
2408 |
|
---|
2409 | LogFlowFunc((": pBlkCache=%#p{%s}\n", pBlkCache, pBlkCache->pszId));
|
---|
2410 |
|
---|
2411 | AssertPtrReturn(pBlkCache, VERR_INVALID_POINTER);
|
---|
2412 | AssertReturn(!pBlkCache->fSuspended, VERR_INVALID_STATE);
|
---|
2413 |
|
---|
2414 | /* Commit dirty entries in the cache. */
|
---|
2415 | pdmBlkCacheCommit(pBlkCache);
|
---|
2416 |
|
---|
2417 | /* Allocate new request structure. */
|
---|
2418 | pReq = pdmBlkCacheReqAlloc(pvUser);
|
---|
2419 | if (RT_UNLIKELY(!pReq))
|
---|
2420 | return VERR_NO_MEMORY;
|
---|
2421 |
|
---|
2422 | rc = pdmBlkCacheRequestPassthrough(pBlkCache, pReq, NULL, 0, 0,
|
---|
2423 | PDMBLKCACHEXFERDIR_FLUSH);
|
---|
2424 | AssertRC(rc);
|
---|
2425 |
|
---|
2426 | LogFlowFunc((": Leave rc=%Rrc\n", rc));
|
---|
2427 | return VINF_AIO_TASK_PENDING;
|
---|
2428 | }
|
---|
2429 |
|
---|
2430 | VMMR3DECL(int) PDMR3BlkCacheDiscard(PPDMBLKCACHE pBlkCache, PCRTRANGE paRanges,
|
---|
2431 | unsigned cRanges, void *pvUser)
|
---|
2432 | {
|
---|
2433 | int rc = VINF_SUCCESS;
|
---|
2434 | PPDMBLKCACHEGLOBAL pCache = pBlkCache->pCache;
|
---|
2435 | PPDMBLKCACHEENTRY pEntry;
|
---|
2436 | PPDMBLKCACHEREQ pReq;
|
---|
2437 |
|
---|
2438 | LogFlowFunc((": pBlkCache=%#p{%s} paRanges=%#p cRanges=%u pvUser=%#p\n",
|
---|
2439 | pBlkCache, pBlkCache->pszId, paRanges, cRanges, pvUser));
|
---|
2440 |
|
---|
2441 | AssertPtrReturn(pBlkCache, VERR_INVALID_POINTER);
|
---|
2442 | AssertReturn(!pBlkCache->fSuspended, VERR_INVALID_STATE);
|
---|
2443 |
|
---|
2444 | /* Allocate new request structure. */
|
---|
2445 | pReq = pdmBlkCacheReqAlloc(pvUser);
|
---|
2446 | if (RT_UNLIKELY(!pReq))
|
---|
2447 | return VERR_NO_MEMORY;
|
---|
2448 |
|
---|
2449 | /* Increment data transfer counter to keep the request valid while we access it. */
|
---|
2450 | ASMAtomicIncU32(&pReq->cXfersPending);
|
---|
2451 |
|
---|
2452 | for (unsigned i = 0; i < cRanges; i++)
|
---|
2453 | {
|
---|
2454 | uint64_t offCur = paRanges[i].offStart;
|
---|
2455 | size_t cbLeft = paRanges[i].cbRange;
|
---|
2456 |
|
---|
2457 | while (cbLeft)
|
---|
2458 | {
|
---|
2459 | size_t cbThisDiscard = 0;
|
---|
2460 |
|
---|
2461 | pEntry = pdmBlkCacheGetCacheEntryByOffset(pBlkCache, offCur);
|
---|
2462 |
|
---|
2463 | if (pEntry)
|
---|
2464 | {
|
---|
2465 | /* Write the data into the entry and mark it as dirty */
|
---|
2466 | AssertPtr(pEntry->pList);
|
---|
2467 |
|
---|
2468 | uint64_t offDiff = offCur - pEntry->Core.Key;
|
---|
2469 |
|
---|
2470 | AssertMsg(offCur >= pEntry->Core.Key,
|
---|
2471 | ("Overflow in calculation offCur=%llu OffsetAligned=%llu\n",
|
---|
2472 | offCur, pEntry->Core.Key));
|
---|
2473 |
|
---|
2474 | cbThisDiscard = RT_MIN(pEntry->cbData - offDiff, cbLeft);
|
---|
2475 |
|
---|
2476 | /* Ghost lists contain no data. */
|
---|
2477 | if ( (pEntry->pList == &pCache->LruRecentlyUsedIn)
|
---|
2478 | || (pEntry->pList == &pCache->LruFrequentlyUsed))
|
---|
2479 | {
|
---|
2480 | /* Check if the entry is dirty. */
|
---|
2481 | if (pdmBlkCacheEntryFlagIsSetClearAcquireLock(pBlkCache, pEntry,
|
---|
2482 | PDMBLKCACHE_ENTRY_IS_DIRTY,
|
---|
2483 | 0))
|
---|
2484 | {
|
---|
2485 | /* If it is dirty but not yet in progress remove it. */
|
---|
2486 | if (!(pEntry->fFlags & PDMBLKCACHE_ENTRY_IO_IN_PROGRESS))
|
---|
2487 | {
|
---|
2488 | pdmBlkCacheLockEnter(pCache);
|
---|
2489 | pdmBlkCacheEntryRemoveFromList(pEntry);
|
---|
2490 |
|
---|
2491 | STAM_PROFILE_ADV_START(&pCache->StatTreeRemove, Cache);
|
---|
2492 | RTAvlrU64Remove(pBlkCache->pTree, pEntry->Core.Key);
|
---|
2493 | STAM_PROFILE_ADV_STOP(&pCache->StatTreeRemove, Cache);
|
---|
2494 |
|
---|
2495 | pdmBlkCacheLockLeave(pCache);
|
---|
2496 |
|
---|
2497 | RTMemFree(pEntry);
|
---|
2498 | }
|
---|
2499 | else
|
---|
2500 | {
|
---|
2501 | #if 0
|
---|
2502 | /* The data isn't written to the file yet */
|
---|
2503 | pdmBlkCacheEntryWaitersAdd(pEntry, pReq,
|
---|
2504 | &SgBuf, offDiff, cbToWrite,
|
---|
2505 | true /* fWrite */);
|
---|
2506 | STAM_COUNTER_INC(&pBlkCache->StatWriteDeferred);
|
---|
2507 | #endif
|
---|
2508 | }
|
---|
2509 |
|
---|
2510 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
2511 | pdmBlkCacheEntryRelease(pEntry);
|
---|
2512 | }
|
---|
2513 | else /* Dirty bit not set */
|
---|
2514 | {
|
---|
2515 | /*
|
---|
2516 | * Check if a read is in progress for this entry.
|
---|
2517 | * We have to defer processing in that case.
|
---|
2518 | */
|
---|
2519 | if(pdmBlkCacheEntryFlagIsSetClearAcquireLock(pBlkCache, pEntry,
|
---|
2520 | PDMBLKCACHE_ENTRY_IO_IN_PROGRESS,
|
---|
2521 | 0))
|
---|
2522 | {
|
---|
2523 | #if 0
|
---|
2524 | pdmBlkCacheEntryWaitersAdd(pEntry, pReq,
|
---|
2525 | &SgBuf, offDiff, cbToWrite,
|
---|
2526 | true /* fWrite */);
|
---|
2527 | #endif
|
---|
2528 | STAM_COUNTER_INC(&pBlkCache->StatWriteDeferred);
|
---|
2529 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
2530 | pdmBlkCacheEntryRelease(pEntry);
|
---|
2531 | }
|
---|
2532 | else /* I/O in progress flag not set */
|
---|
2533 | {
|
---|
2534 | pdmBlkCacheLockEnter(pCache);
|
---|
2535 | pdmBlkCacheEntryRemoveFromList(pEntry);
|
---|
2536 |
|
---|
2537 | RTSemRWRequestWrite(pBlkCache->SemRWEntries, RT_INDEFINITE_WAIT);
|
---|
2538 | STAM_PROFILE_ADV_START(&pCache->StatTreeRemove, Cache);
|
---|
2539 | RTAvlrU64Remove(pBlkCache->pTree, pEntry->Core.Key);
|
---|
2540 | STAM_PROFILE_ADV_STOP(&pCache->StatTreeRemove, Cache);
|
---|
2541 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
2542 |
|
---|
2543 | pdmBlkCacheLockLeave(pCache);
|
---|
2544 |
|
---|
2545 | RTMemFree(pEntry);
|
---|
2546 | }
|
---|
2547 | } /* Dirty bit not set */
|
---|
2548 | }
|
---|
2549 | else /* Entry is on the ghost list just remove cache entry. */
|
---|
2550 | {
|
---|
2551 | pdmBlkCacheLockEnter(pCache);
|
---|
2552 | pdmBlkCacheEntryRemoveFromList(pEntry);
|
---|
2553 |
|
---|
2554 | RTSemRWRequestWrite(pBlkCache->SemRWEntries, RT_INDEFINITE_WAIT);
|
---|
2555 | STAM_PROFILE_ADV_START(&pCache->StatTreeRemove, Cache);
|
---|
2556 | RTAvlrU64Remove(pBlkCache->pTree, pEntry->Core.Key);
|
---|
2557 | STAM_PROFILE_ADV_STOP(&pCache->StatTreeRemove, Cache);
|
---|
2558 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
2559 |
|
---|
2560 | pdmBlkCacheLockLeave(pCache);
|
---|
2561 |
|
---|
2562 | RTMemFree(pEntry);
|
---|
2563 | }
|
---|
2564 | }
|
---|
2565 | /* else: no entry found. */
|
---|
2566 |
|
---|
2567 | offCur += cbThisDiscard;
|
---|
2568 | cbLeft -= cbThisDiscard;
|
---|
2569 | }
|
---|
2570 | }
|
---|
2571 |
|
---|
2572 | if (!pdmBlkCacheReqUpdate(pBlkCache, pReq, rc, false))
|
---|
2573 | rc = VINF_AIO_TASK_PENDING;
|
---|
2574 | else
|
---|
2575 | {
|
---|
2576 | rc = pReq->rcReq;
|
---|
2577 | RTMemFree(pReq);
|
---|
2578 | }
|
---|
2579 |
|
---|
2580 | LogFlowFunc((": Leave rc=%Rrc\n", rc));
|
---|
2581 |
|
---|
2582 | return rc;
|
---|
2583 | }
|
---|
2584 |
|
---|
2585 | /**
|
---|
2586 | * Completes a task segment freeing all resources and completes the task handle
|
---|
2587 | * if everything was transferred.
|
---|
2588 | *
|
---|
2589 | * @returns Next task segment handle.
|
---|
2590 | * @param pBlkCache The endpoint block cache.
|
---|
2591 | * @param pWaiter Task segment to complete.
|
---|
2592 | * @param rc Status code to set.
|
---|
2593 | */
|
---|
2594 | static PPDMBLKCACHEWAITER pdmBlkCacheWaiterComplete(PPDMBLKCACHE pBlkCache, PPDMBLKCACHEWAITER pWaiter, int rc)
|
---|
2595 | {
|
---|
2596 | PPDMBLKCACHEWAITER pNext = pWaiter->pNext;
|
---|
2597 | PPDMBLKCACHEREQ pReq = pWaiter->pReq;
|
---|
2598 |
|
---|
2599 | pdmBlkCacheReqUpdate(pBlkCache, pReq, rc, true);
|
---|
2600 |
|
---|
2601 | RTMemFree(pWaiter);
|
---|
2602 |
|
---|
2603 | return pNext;
|
---|
2604 | }
|
---|
2605 |
|
---|
2606 | static void pdmBlkCacheIoXferCompleteEntry(PPDMBLKCACHE pBlkCache, PPDMBLKCACHEIOXFER hIoXfer, int rcIoXfer)
|
---|
2607 | {
|
---|
2608 | PPDMBLKCACHEENTRY pEntry = hIoXfer->pEntry;
|
---|
2609 | PPDMBLKCACHEGLOBAL pCache = pBlkCache->pCache;
|
---|
2610 |
|
---|
2611 | /* Reference the entry now as we are clearing the I/O in progress flag
|
---|
2612 | * which protected the entry till now. */
|
---|
2613 | pdmBlkCacheEntryRef(pEntry);
|
---|
2614 |
|
---|
2615 | RTSemRWRequestWrite(pBlkCache->SemRWEntries, RT_INDEFINITE_WAIT);
|
---|
2616 | pEntry->fFlags &= ~PDMBLKCACHE_ENTRY_IO_IN_PROGRESS;
|
---|
2617 |
|
---|
2618 | /* Process waiting segment list. The data in entry might have changed in-between. */
|
---|
2619 | bool fDirty = false;
|
---|
2620 | PPDMBLKCACHEWAITER pComplete = pEntry->pWaitingHead;
|
---|
2621 | PPDMBLKCACHEWAITER pCurr = pComplete;
|
---|
2622 |
|
---|
2623 | AssertMsg((pCurr && pEntry->pWaitingTail) || (!pCurr && !pEntry->pWaitingTail),
|
---|
2624 | ("The list tail was not updated correctly\n"));
|
---|
2625 | pEntry->pWaitingTail = NULL;
|
---|
2626 | pEntry->pWaitingHead = NULL;
|
---|
2627 |
|
---|
2628 | if (hIoXfer->enmXferDir == PDMBLKCACHEXFERDIR_WRITE)
|
---|
2629 | {
|
---|
2630 | /*
|
---|
2631 | * An error here is difficult to handle as the original request completed already.
|
---|
2632 | * The error is logged for now and the VM is paused.
|
---|
2633 | * If the user continues the entry is written again in the hope
|
---|
2634 | * the user fixed the problem and the next write succeeds.
|
---|
2635 | */
|
---|
2636 | if (RT_FAILURE(rcIoXfer))
|
---|
2637 | {
|
---|
2638 | LogRel(("I/O cache: Error while writing entry at offset %llu (%u bytes) to medium \"%s\" (rc=%Rrc)\n",
|
---|
2639 | pEntry->Core.Key, pEntry->cbData, pBlkCache->pszId, rcIoXfer));
|
---|
2640 |
|
---|
2641 | if (!ASMAtomicXchgBool(&pCache->fIoErrorVmSuspended, true))
|
---|
2642 | {
|
---|
2643 | int rc = VMSetRuntimeError(pCache->pVM, VMSETRTERR_FLAGS_SUSPEND | VMSETRTERR_FLAGS_NO_WAIT, "BLKCACHE_IOERR",
|
---|
2644 | N_("The I/O cache encountered an error while updating data in medium \"%s\" (rc=%Rrc). "
|
---|
2645 | "Make sure there is enough free space on the disk and that the disk is working properly. "
|
---|
2646 | "Operation can be resumed afterwards"),
|
---|
2647 | pBlkCache->pszId, rcIoXfer);
|
---|
2648 | AssertRC(rc);
|
---|
2649 | }
|
---|
2650 |
|
---|
2651 | /* Mark the entry as dirty again to get it added to the list later on. */
|
---|
2652 | fDirty = true;
|
---|
2653 | }
|
---|
2654 |
|
---|
2655 | pEntry->fFlags &= ~PDMBLKCACHE_ENTRY_IS_DIRTY;
|
---|
2656 |
|
---|
2657 | while (pCurr)
|
---|
2658 | {
|
---|
2659 | AssertMsg(pCurr->fWrite, ("Completed write entries should never have read tasks attached\n"));
|
---|
2660 |
|
---|
2661 | RTSgBufCopyToBuf(&pCurr->SgBuf, pEntry->pbData + pCurr->offCacheEntry, pCurr->cbTransfer);
|
---|
2662 | fDirty = true;
|
---|
2663 | pCurr = pCurr->pNext;
|
---|
2664 | }
|
---|
2665 | }
|
---|
2666 | else
|
---|
2667 | {
|
---|
2668 | AssertMsg(hIoXfer->enmXferDir == PDMBLKCACHEXFERDIR_READ, ("Invalid transfer type\n"));
|
---|
2669 | AssertMsg(!(pEntry->fFlags & PDMBLKCACHE_ENTRY_IS_DIRTY),
|
---|
2670 | ("Invalid flags set\n"));
|
---|
2671 |
|
---|
2672 | while (pCurr)
|
---|
2673 | {
|
---|
2674 | if (pCurr->fWrite)
|
---|
2675 | {
|
---|
2676 | RTSgBufCopyToBuf(&pCurr->SgBuf, pEntry->pbData + pCurr->offCacheEntry, pCurr->cbTransfer);
|
---|
2677 | fDirty = true;
|
---|
2678 | }
|
---|
2679 | else
|
---|
2680 | RTSgBufCopyFromBuf(&pCurr->SgBuf, pEntry->pbData + pCurr->offCacheEntry, pCurr->cbTransfer);
|
---|
2681 |
|
---|
2682 | pCurr = pCurr->pNext;
|
---|
2683 | }
|
---|
2684 | }
|
---|
2685 |
|
---|
2686 | bool fCommit = false;
|
---|
2687 | if (fDirty)
|
---|
2688 | fCommit = pdmBlkCacheAddDirtyEntry(pBlkCache, pEntry);
|
---|
2689 |
|
---|
2690 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
2691 |
|
---|
2692 | /* Dereference so that it isn't protected anymore except we issued anyother write for it. */
|
---|
2693 | pdmBlkCacheEntryRelease(pEntry);
|
---|
2694 |
|
---|
2695 | if (fCommit)
|
---|
2696 | pdmBlkCacheCommitDirtyEntries(pCache);
|
---|
2697 |
|
---|
2698 | /* Complete waiters now. */
|
---|
2699 | while (pComplete)
|
---|
2700 | pComplete = pdmBlkCacheWaiterComplete(pBlkCache, pComplete, rcIoXfer);
|
---|
2701 | }
|
---|
2702 |
|
---|
2703 | VMMR3DECL(void) PDMR3BlkCacheIoXferComplete(PPDMBLKCACHE pBlkCache, PPDMBLKCACHEIOXFER hIoXfer, int rcIoXfer)
|
---|
2704 | {
|
---|
2705 | LogFlowFunc(("pBlkCache=%#p hIoXfer=%#p rcIoXfer=%Rrc\n", pBlkCache, hIoXfer, rcIoXfer));
|
---|
2706 |
|
---|
2707 | if (hIoXfer->fIoCache)
|
---|
2708 | pdmBlkCacheIoXferCompleteEntry(pBlkCache, hIoXfer, rcIoXfer);
|
---|
2709 | else
|
---|
2710 | pdmBlkCacheReqUpdate(pBlkCache, hIoXfer->pReq, rcIoXfer, true);
|
---|
2711 |
|
---|
2712 | ASMAtomicDecU32(&pBlkCache->cIoXfersActive);
|
---|
2713 | pdmBlkCacheR3TraceMsgF(pBlkCache, "BlkCache: I/O req %#p (%RTbool) completed (%u now active)",
|
---|
2714 | hIoXfer, hIoXfer->fIoCache, pBlkCache->cIoXfersActive);
|
---|
2715 | RTMemFree(hIoXfer);
|
---|
2716 | }
|
---|
2717 |
|
---|
2718 | /**
|
---|
2719 | * Callback for the AVL do with all routine. Waits for a cachen entry to finish any pending I/O.
|
---|
2720 | *
|
---|
2721 | * @returns IPRT status code.
|
---|
2722 | * @param pNode The node to destroy.
|
---|
2723 | * @param pvUser Opaque user data.
|
---|
2724 | */
|
---|
2725 | static DECLCALLBACK(int) pdmBlkCacheEntryQuiesce(PAVLRU64NODECORE pNode, void *pvUser)
|
---|
2726 | {
|
---|
2727 | PPDMBLKCACHEENTRY pEntry = (PPDMBLKCACHEENTRY)pNode;
|
---|
2728 | PPDMBLKCACHE pBlkCache = pEntry->pBlkCache;
|
---|
2729 | NOREF(pvUser);
|
---|
2730 |
|
---|
2731 | while (ASMAtomicReadU32(&pEntry->fFlags) & PDMBLKCACHE_ENTRY_IO_IN_PROGRESS)
|
---|
2732 | {
|
---|
2733 | /* Leave the locks to let the I/O thread make progress but reference the entry to prevent eviction. */
|
---|
2734 | pdmBlkCacheEntryRef(pEntry);
|
---|
2735 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
2736 |
|
---|
2737 | RTThreadSleep(1);
|
---|
2738 |
|
---|
2739 | /* Re-enter all locks and drop the reference. */
|
---|
2740 | RTSemRWRequestWrite(pBlkCache->SemRWEntries, RT_INDEFINITE_WAIT);
|
---|
2741 | pdmBlkCacheEntryRelease(pEntry);
|
---|
2742 | }
|
---|
2743 |
|
---|
2744 | AssertMsg(!(pEntry->fFlags & PDMBLKCACHE_ENTRY_IO_IN_PROGRESS),
|
---|
2745 | ("Entry is dirty and/or still in progress fFlags=%#x\n", pEntry->fFlags));
|
---|
2746 |
|
---|
2747 | return VINF_SUCCESS;
|
---|
2748 | }
|
---|
2749 |
|
---|
2750 | VMMR3DECL(int) PDMR3BlkCacheSuspend(PPDMBLKCACHE pBlkCache)
|
---|
2751 | {
|
---|
2752 | int rc = VINF_SUCCESS;
|
---|
2753 | LogFlowFunc(("pBlkCache=%#p\n", pBlkCache));
|
---|
2754 |
|
---|
2755 | AssertPtrReturn(pBlkCache, VERR_INVALID_POINTER);
|
---|
2756 |
|
---|
2757 | if (!ASMAtomicReadBool(&pBlkCache->pCache->fIoErrorVmSuspended))
|
---|
2758 | pdmBlkCacheCommit(pBlkCache); /* Can issue new I/O requests. */
|
---|
2759 | ASMAtomicXchgBool(&pBlkCache->fSuspended, true);
|
---|
2760 |
|
---|
2761 | /* Wait for all I/O to complete. */
|
---|
2762 | RTSemRWRequestWrite(pBlkCache->SemRWEntries, RT_INDEFINITE_WAIT);
|
---|
2763 | rc = RTAvlrU64DoWithAll(pBlkCache->pTree, true, pdmBlkCacheEntryQuiesce, NULL);
|
---|
2764 | AssertRC(rc);
|
---|
2765 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
2766 |
|
---|
2767 | return rc;
|
---|
2768 | }
|
---|
2769 |
|
---|
2770 | VMMR3DECL(int) PDMR3BlkCacheResume(PPDMBLKCACHE pBlkCache)
|
---|
2771 | {
|
---|
2772 | LogFlowFunc(("pBlkCache=%#p\n", pBlkCache));
|
---|
2773 |
|
---|
2774 | AssertPtrReturn(pBlkCache, VERR_INVALID_POINTER);
|
---|
2775 |
|
---|
2776 | ASMAtomicXchgBool(&pBlkCache->fSuspended, false);
|
---|
2777 |
|
---|
2778 | return VINF_SUCCESS;
|
---|
2779 | }
|
---|
2780 |
|
---|
2781 | VMMR3DECL(int) PDMR3BlkCacheClear(PPDMBLKCACHE pBlkCache)
|
---|
2782 | {
|
---|
2783 | int rc = VINF_SUCCESS;
|
---|
2784 | PPDMBLKCACHEGLOBAL pCache = pBlkCache->pCache;
|
---|
2785 |
|
---|
2786 | /*
|
---|
2787 | * Commit all dirty entries now (they are waited on for completion during the
|
---|
2788 | * destruction of the AVL tree below).
|
---|
2789 | * The exception is if the VM was paused because of an I/O error before.
|
---|
2790 | */
|
---|
2791 | if (!ASMAtomicReadBool(&pCache->fIoErrorVmSuspended))
|
---|
2792 | pdmBlkCacheCommit(pBlkCache);
|
---|
2793 |
|
---|
2794 | /* Make sure nobody is accessing the cache while we delete the tree. */
|
---|
2795 | pdmBlkCacheLockEnter(pCache);
|
---|
2796 | RTSemRWRequestWrite(pBlkCache->SemRWEntries, RT_INDEFINITE_WAIT);
|
---|
2797 | RTAvlrU64Destroy(pBlkCache->pTree, pdmBlkCacheEntryDestroy, pCache);
|
---|
2798 | RTSemRWReleaseWrite(pBlkCache->SemRWEntries);
|
---|
2799 |
|
---|
2800 | pdmBlkCacheLockLeave(pCache);
|
---|
2801 | return rc;
|
---|
2802 | }
|
---|
2803 |
|
---|