Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

786 lines
24 KiB

  1. /*++
  2. Copyright (c) 1999-2000 Microsoft Corporation
  3. Module Name:
  4. fsbpool.c
  5. Abstract:
  6. This file contains the implementation of fixed-size block pool.
  7. Author:
  8. Shaun Cox (shaunco) 10-Dec-1999
  9. --*/
  10. #include "ntddk.h"
  11. #include "fsbpool.h"
  12. typedef PVOID LPVOID;
  13. #include "align.h" // Macros: ROUND_UP_POINTER, POINTER_IS_ALIGNED
  14. #define FSB_SCAVENGE_PERIOD_IN_SECONDS 30
  15. #define FSB_MINIMUM_PAGE_LIFETIME_IN_SECONDS 20
  16. #if defined (_WIN64)
  17. #define MAX_CACHE_LINE_SIZE 128
  18. #else
  19. #define MAX_CACHE_LINE_SIZE 64
  20. #endif
  21. // The following structures are used in the single allocation that
  22. // a pool handle points to.
  23. // PoolHandle ---> [FSB_POOL_HEADER + FSB_CPU_POOL_HEADER for cpu 0 +
  24. // FSB_CPU_POOL_HEADER for cpu 1 + ...
  25. // FSB_CPU_POOL_HEADER for cpu N]
  26. //
  27. // FSB_POOL_HEADER is the data common to all CPUs for a given pool.
  28. //
  29. typedef struct _FSB_POOL_HEADER
  30. {
  31. // cache-line -----
  32. struct _FSB_POOL_HEADER_BASE
  33. {
  34. ULONG Tag;
  35. USHORT CallerBlockSize; // caller's requested block size
  36. USHORT AlignedBlockSize; // ALIGN_UP(CallerBlockSize, PVOID)
  37. USHORT BlocksPerPage;
  38. USHORT FreeBlockLinkOffset;
  39. PFSB_BUILDBLOCK_FUNCTION BuildFunction;
  40. PVOID Allocation;
  41. };
  42. UCHAR Alignment[MAX_CACHE_LINE_SIZE
  43. - (sizeof(struct _FSB_POOL_HEADER_BASE) % MAX_CACHE_LINE_SIZE)];
  44. } FSB_POOL_HEADER, *PFSB_POOL_HEADER;
  45. C_ASSERT(sizeof(FSB_POOL_HEADER) % MAX_CACHE_LINE_SIZE == 0);
  46. // FSB_CPU_POOL_HEADER is the data specific to a CPU for a given pool.
  47. //
  48. typedef struct _FSB_CPU_POOL_HEADER
  49. {
  50. // cache-line -----
  51. struct _FSB_CPU_POOL_HEADER_BASE
  52. {
  53. // The doubly-linked list of pages that make up this processor's pool.
  54. // These pages have one or more free blocks available.
  55. //
  56. LIST_ENTRY PageList;
  57. // The doubly-linked list of pages that are fully in use. This list
  58. // is separate from the above list so that we do not spend time walking
  59. // a very long list during FsbAllocate when many pages are fully used.
  60. //
  61. LIST_ENTRY UsedPageList;
  62. // The next scheduled time (in units of KeQueryTickCount()) for
  63. // scavenging this pool. The next scavenge will happen no earlier
  64. // than this.
  65. //
  66. LARGE_INTEGER NextScavengeTick;
  67. // The number of the processor that owns this pool.
  68. //
  69. ULONG OwnerCpu;
  70. ULONG TotalBlocksAllocated;
  71. ULONG TotalBlocksFreed;
  72. ULONG PeakBlocksInUse;
  73. ULONG TotalPagesAllocated;
  74. ULONG TotalPagesFreed;
  75. ULONG PeakPagesInUse;
  76. };
  77. UCHAR Alignment[MAX_CACHE_LINE_SIZE
  78. - (sizeof(struct _FSB_CPU_POOL_HEADER_BASE) % MAX_CACHE_LINE_SIZE)];
  79. } FSB_CPU_POOL_HEADER, *PFSB_CPU_POOL_HEADER;
  80. C_ASSERT(sizeof(FSB_CPU_POOL_HEADER) % MAX_CACHE_LINE_SIZE == 0);
  81. // FSB_PAGE_HEADER is the data at the beginning of each allocated pool page
  82. // that describes the current state of the blocks on the page.
  83. //
  84. typedef struct _FSB_PAGE_HEADER
  85. {
  86. // cache-line -----
  87. // Back pointer to the owning cpu pool.
  88. //
  89. PFSB_CPU_POOL_HEADER Pool;
  90. // Linkage entry for the list of pages managed by the cpu pool.
  91. //
  92. LIST_ENTRY PageLink;
  93. // Number of blocks built so far on this page. Blocks are built on
  94. // demand. When this number reaches Pool->BlocksPerPage, all blocks on
  95. // this page have been built.
  96. //
  97. USHORT BlocksBuilt;
  98. // Boolean indicator of whether or not this page is on the cpu pool's
  99. // used-page list. This is checked during FsbFree to see if the page
  100. // should be moved back to the normal page list.
  101. // (it is a USHORT, instead of BOOLEAN, for proper padding)
  102. //
  103. USHORT OnUsedPageList;
  104. // List of free blocks on this page.
  105. //
  106. SLIST_HEADER FreeList;
  107. // The value of KeQueryTickCount (normalized to units of seconds)
  108. // which represents the time after which this page can be freed back
  109. // to the system's pool. This time is only used once the depth of
  110. // FreeList is Pool->BlocksPerPage. (i.e. this time is only used if
  111. // the page is completely unused.)
  112. //
  113. LARGE_INTEGER LastUsedTick;
  114. } FSB_PAGE_HEADER, *PFSB_PAGE_HEADER;
  115. // Get a pointer to the overall pool given a pointer to one of
  116. // the per-processor pools within it.
  117. //
  118. __inline
  119. PFSB_POOL_HEADER
  120. PoolFromCpuPool(
  121. IN PFSB_CPU_POOL_HEADER CpuPool
  122. )
  123. {
  124. return (PFSB_POOL_HEADER)(CpuPool - CpuPool->OwnerCpu) - 1;
  125. }
  126. __inline
  127. VOID
  128. ConvertSecondsToTicks(
  129. IN ULONG Seconds,
  130. OUT PLARGE_INTEGER Ticks
  131. )
  132. {
  133. // If the following assert fires, you need to cast Seconds below to
  134. // ULONGLONG so that 64 bit multiplication and division are used.
  135. // The current code assumes less than 430 seconds so that the
  136. // 32 multiplication below won't overflow.
  137. //
  138. ASSERT(Seconds < 430);
  139. Ticks->HighPart = 0;
  140. Ticks->LowPart = (Seconds * 10*1000*1000) / KeQueryTimeIncrement();
  141. }
  142. // Build the next block on the specified pool page.
  143. // This can only be called if not all of the blocks have been built yet.
  144. //
  145. PUCHAR
  146. FsbpBuildNextBlock(
  147. IN const FSB_POOL_HEADER* Pool,
  148. IN OUT PFSB_PAGE_HEADER Page
  149. )
  150. {
  151. PUCHAR Block;
  152. ASSERT(Page->BlocksBuilt < Pool->BlocksPerPage);
  153. ASSERT((PAGE_SIZE - sizeof(FSB_PAGE_HEADER)) / Pool->AlignedBlockSize
  154. == Pool->BlocksPerPage);
  155. ASSERT(Pool->CallerBlockSize <= Pool->AlignedBlockSize);
  156. Block = (PUCHAR)(Page + 1) + (Page->BlocksBuilt * Pool->AlignedBlockSize);
  157. ASSERT(PAGE_ALIGN(Block) == Page);
  158. if (Pool->BuildFunction) {
  159. Pool->BuildFunction(Block, Pool->CallerBlockSize);
  160. }
  161. Page->BlocksBuilt++;
  162. return Block;
  163. }
  164. // Allocate a new pool page and insert it at the head of the specified
  165. // CPU pool. Build the first block on the new page and return a pointer
  166. // to it.
  167. //
  168. PUCHAR
  169. FsbpAllocateNewPageAndBuildOneBlock(
  170. IN const FSB_POOL_HEADER* Pool,
  171. IN PFSB_CPU_POOL_HEADER CpuPool
  172. )
  173. {
  174. PFSB_PAGE_HEADER Page;
  175. PUCHAR Block = NULL;
  176. ULONG PagesInUse;
  177. ASSERT(Pool);
  178. Page = ExAllocatePoolWithTagPriority(NonPagedPool, PAGE_SIZE, Pool->Tag,
  179. NormalPoolPriority);
  180. if (Page)
  181. {
  182. ASSERT(Page == PAGE_ALIGN(Page));
  183. RtlZeroMemory(Page, sizeof(FSB_PAGE_HEADER));
  184. Page->Pool = CpuPool;
  185. ExInitializeSListHead(&Page->FreeList);
  186. // Insert the page at the head of the cpu's pool.
  187. //
  188. InsertHeadList(&CpuPool->PageList, &Page->PageLink);
  189. CpuPool->TotalPagesAllocated++;
  190. // Update the pool's statistics.
  191. //
  192. PagesInUse = CpuPool->TotalPagesAllocated - CpuPool->TotalPagesFreed;
  193. if (PagesInUse > CpuPool->PeakPagesInUse)
  194. {
  195. CpuPool->PeakPagesInUse = PagesInUse;
  196. }
  197. Block = FsbpBuildNextBlock(Pool, Page);
  198. ASSERT(Block);
  199. }
  200. return Block;
  201. }
  202. // Free the specified pool page back to the system's pool.
  203. //
  204. VOID
  205. FsbpFreePage(
  206. IN PFSB_CPU_POOL_HEADER CpuPool,
  207. IN PFSB_PAGE_HEADER Page
  208. )
  209. {
  210. ASSERT(Page == PAGE_ALIGN(Page));
  211. ASSERT(Page->Pool == CpuPool);
  212. ExFreePool(Page);
  213. CpuPool->TotalPagesFreed++;
  214. ASSERT(CpuPool->TotalPagesFreed <= CpuPool->TotalPagesAllocated);
  215. }
  216. // Free the specified pool page list back to the system's pool.
  217. //
  218. VOID
  219. FsbpFreeList(
  220. IN PFSB_CPU_POOL_HEADER CpuPool,
  221. IN PLIST_ENTRY Head
  222. )
  223. {
  224. PFSB_POOL_HEADER Pool;
  225. PFSB_PAGE_HEADER Page;
  226. PLIST_ENTRY Scan;
  227. PLIST_ENTRY Next;
  228. BOOLEAN UsedPageList;
  229. Pool = PoolFromCpuPool(CpuPool);
  230. UsedPageList = (Head == &CpuPool->UsedPageList);
  231. for (Scan = Head->Flink; Scan != Head; Scan = Next)
  232. {
  233. Page = CONTAINING_RECORD(Scan, FSB_PAGE_HEADER, PageLink);
  234. ASSERT(Page == PAGE_ALIGN(Page));
  235. ASSERT(CpuPool == Page->Pool);
  236. ASSERT(UsedPageList ? Page->OnUsedPageList : !Page->OnUsedPageList);
  237. ASSERT(Page->BlocksBuilt <= Pool->BlocksPerPage);
  238. ASSERT(Page->BlocksBuilt == ExQueryDepthSList(&Page->FreeList));
  239. // Step to the next link before we free this page.
  240. //
  241. Next = Scan->Flink;
  242. RemoveEntryList(Scan);
  243. FsbpFreePage(CpuPool, Page);
  244. }
  245. }
  246. // Reclaim the memory consumed by completely unused pool pages belonging
  247. // to the specified per-processor pool.
  248. //
  249. // Caller IRQL: [DISPATCH_LEVEL]
  250. //
  251. VOID
  252. FsbpScavengePool(
  253. IN OUT PFSB_CPU_POOL_HEADER CpuPool
  254. )
  255. {
  256. PFSB_POOL_HEADER Pool;
  257. PFSB_PAGE_HEADER Page;
  258. PLIST_ENTRY Scan;
  259. PLIST_ENTRY Next;
  260. LARGE_INTEGER Ticks;
  261. LARGE_INTEGER TicksDelta;
  262. // We must not only be at DISPATCH_LEVEL (or higher), we must also
  263. // be called on the processor that owns the specified pool.
  264. //
  265. ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL);
  266. ASSERT((ULONG)KeGetCurrentProcessorNumber() == CpuPool->OwnerCpu);
  267. Pool = PoolFromCpuPool(CpuPool);
  268. KeQueryTickCount(&Ticks);
  269. // Compute the next tick value which represents the earliest time
  270. // that we will scavenge this pool again.
  271. //
  272. ConvertSecondsToTicks(FSB_SCAVENGE_PERIOD_IN_SECONDS, &TicksDelta);
  273. CpuPool->NextScavengeTick.QuadPart = Ticks.QuadPart + TicksDelta.QuadPart;
  274. // Compute the tick value which represents the last point at which
  275. // its okay to free a page.
  276. //
  277. ConvertSecondsToTicks(FSB_MINIMUM_PAGE_LIFETIME_IN_SECONDS, &TicksDelta);
  278. Ticks.QuadPart = Ticks.QuadPart - TicksDelta.QuadPart;
  279. Scan = CpuPool->PageList.Flink;
  280. while (Scan != &CpuPool->PageList)
  281. {
  282. Page = CONTAINING_RECORD(Scan, FSB_PAGE_HEADER, PageLink);
  283. ASSERT(Page == PAGE_ALIGN(Page));
  284. ASSERT(CpuPool == Page->Pool);
  285. ASSERT(!Page->OnUsedPageList);
  286. // Step to the next link before we possibly unlink this page.
  287. //
  288. Next = Scan->Flink;
  289. if ((Pool->BlocksPerPage == ExQueryDepthSList(&Page->FreeList)) &&
  290. (Ticks.QuadPart > Page->LastUsedTick.QuadPart))
  291. {
  292. RemoveEntryList(Scan);
  293. FsbpFreePage(CpuPool, Page);
  294. }
  295. Scan = Next;
  296. }
  297. // Scan the used pages to see if they can be moved back to the normal
  298. // list. This can happen if too many frees by non-owning processors
  299. // are done. In that case, the pages get orphaned on the used-page
  300. // list after all of their blocks have been freed to the page. Un-orphan
  301. // them here.
  302. //
  303. Scan = CpuPool->UsedPageList.Flink;
  304. while (Scan != &CpuPool->UsedPageList)
  305. {
  306. Page = CONTAINING_RECORD(Scan, FSB_PAGE_HEADER, PageLink);
  307. ASSERT(Page == PAGE_ALIGN(Page));
  308. ASSERT(CpuPool == Page->Pool);
  309. ASSERT(Page->OnUsedPageList);
  310. // Step to the next link before we possibly unlink this page.
  311. //
  312. Next = Scan->Flink;
  313. if (0 != ExQueryDepthSList(&Page->FreeList))
  314. {
  315. RemoveEntryList(Scan);
  316. Page->OnUsedPageList = FALSE;
  317. InsertTailList(&CpuPool->PageList, Scan);
  318. }
  319. Scan = Next;
  320. }
  321. }
  322. // Creates a pool of fixed-size blocks built over non-paged pool. Each
  323. // block is BlockSize bytes long. If NULL is not returned,
  324. // FsbDestroyPool should be called at a later time to reclaim the
  325. // resources used by the pool.
  326. //
  327. // Arguments:
  328. // BlockSize - The size, in bytes, of each block.
  329. // FreeBlockLinkOffset - The offset, in bytes, from the beginning of a block
  330. // that represenets a pointer-sized storage location that the pool can
  331. // use to chain free blocks together. Most often this will be zero
  332. // (meaning use the first pointer-size bytes of the block.)
  333. // Tag - The pool tag to be used internally for calls to
  334. // ExAllocatePoolWithTag. This allows callers to track
  335. // memory consumption for different pools.
  336. // BuildFunction - An optional pointer to a function which initializes
  337. // blocks when they are first allocated by the pool. This allows the
  338. // caller to perform custom, on-demand initialization of each block.
  339. //
  340. // Returns the handle used to identify the pool.
  341. //
  342. // Caller IRQL: [PASSIVE_LEVEL, DISPATCH_LEVEL]
  343. //
  344. HANDLE
  345. FsbCreatePool(
  346. IN USHORT BlockSize,
  347. IN USHORT FreeBlockLinkOffset,
  348. IN ULONG Tag,
  349. IN PFSB_BUILDBLOCK_FUNCTION BuildFunction OPTIONAL
  350. )
  351. {
  352. SIZE_T Size;
  353. PVOID Allocation;
  354. PFSB_POOL_HEADER Pool = NULL;
  355. PFSB_CPU_POOL_HEADER CpuPool;
  356. #if MILLEN
  357. CCHAR NumberCpus = 1;
  358. #else // MILLEN
  359. CCHAR NumberCpus = KeNumberProcessors;
  360. #endif // !MILLEN
  361. CCHAR i;
  362. // We need at least a pointer size worth of space to manage free
  363. // blocks and we don't impose any per-block overhead, so we borrow it
  364. // from the block itself.
  365. //
  366. ASSERT(BlockSize >= FreeBlockLinkOffset + sizeof(PVOID));
  367. // This implementation shouldn't be used if we are not going to get more
  368. // than about 8 blocks per page. Blocks bigger than this should probably
  369. // be allocated with multiple pages at a time.
  370. //
  371. ASSERT(BlockSize < PAGE_SIZE / 8);
  372. // Compute the size of our pool header allocation.
  373. // Add padding to ensure that the pool header can begin on a cache line.
  374. //
  375. Size = sizeof(FSB_POOL_HEADER) + (sizeof(FSB_CPU_POOL_HEADER) * NumberCpus)
  376. + (MAX_CACHE_LINE_SIZE - MEMORY_ALLOCATION_ALIGNMENT);
  377. // Allocate the pool header.
  378. //
  379. Allocation = ExAllocatePoolWithTag(NonPagedPool, Size, ' bsF');
  380. if (Allocation)
  381. {
  382. ASSERT(POINTER_IS_ALIGNED(Allocation, MEMORY_ALLOCATION_ALIGNMENT));
  383. RtlZeroMemory(Allocation, Size);
  384. Pool = ROUND_UP_POINTER(Allocation, MAX_CACHE_LINE_SIZE);
  385. // Initialize the pool header fields.
  386. //
  387. Pool->Tag = Tag;
  388. Pool->CallerBlockSize = BlockSize;
  389. Pool->AlignedBlockSize = (USHORT)ALIGN_UP(BlockSize, PVOID);
  390. Pool->BlocksPerPage = (PAGE_SIZE - sizeof(FSB_PAGE_HEADER))
  391. / Pool->AlignedBlockSize;
  392. Pool->FreeBlockLinkOffset = FreeBlockLinkOffset;
  393. Pool->BuildFunction = BuildFunction;
  394. Pool->Allocation = Allocation;
  395. // Initialize the per-cpu pool headers.
  396. //
  397. CpuPool = (PFSB_CPU_POOL_HEADER)(Pool + 1);
  398. for (i = 0; i < NumberCpus; i++)
  399. {
  400. InitializeListHead(&CpuPool[i].PageList);
  401. InitializeListHead(&CpuPool[i].UsedPageList);
  402. CpuPool[i].OwnerCpu = i;
  403. }
  404. }
  405. return Pool;
  406. }
  407. // Destroys a pool of fixed-size blocks previously created by a call to
  408. // FsbCreatePool.
  409. //
  410. // Arguments:
  411. // PoolHandle - Handle which identifies the pool being destroyed.
  412. //
  413. // Caller IRQL: [PASSIVE_LEVEL, DISPATCH_LEVEL]
  414. //
  415. VOID
  416. FsbDestroyPool(
  417. IN HANDLE PoolHandle
  418. )
  419. {
  420. PFSB_POOL_HEADER Pool;
  421. PFSB_CPU_POOL_HEADER CpuPool;
  422. #if MILLEN
  423. CCHAR NumberCpus = 1;
  424. #else // MILLEN
  425. CCHAR NumberCpus = KeNumberProcessors;
  426. #endif // !MILLEN
  427. CCHAR i;
  428. Pool = (PFSB_POOL_HEADER)PoolHandle;
  429. if (!Pool)
  430. {
  431. return;
  432. }
  433. for (i = 0, CpuPool = (PFSB_CPU_POOL_HEADER)(Pool + 1);
  434. i < NumberCpus;
  435. i++, CpuPool++)
  436. {
  437. ASSERT(CpuPool->OwnerCpu == (ULONG)i);
  438. FsbpFreeList(CpuPool, &CpuPool->PageList);
  439. FsbpFreeList(CpuPool, &CpuPool->UsedPageList);
  440. ASSERT(CpuPool->TotalPagesAllocated == CpuPool->TotalPagesFreed);
  441. ASSERT(CpuPool->TotalBlocksAllocated == CpuPool->TotalBlocksFreed);
  442. }
  443. ASSERT(Pool == ROUND_UP_POINTER(Pool->Allocation, MAX_CACHE_LINE_SIZE));
  444. ExFreePool(Pool->Allocation);
  445. }
  446. // Returns a pointer to a block allocated from a pool. NULL is returned if
  447. // the request could not be granted. The returned pointer is guaranteed to
  448. // have 8 byte alignment.
  449. //
  450. // Arguments:
  451. // PoolHandle - Handle which identifies the pool being allocated from.
  452. //
  453. // Caller IRQL: [PASSIVE_LEVEL, DISPATCH_LEVEL]
  454. //
  455. PUCHAR
  456. FsbAllocate(
  457. IN HANDLE PoolHandle
  458. )
  459. {
  460. PFSB_POOL_HEADER Pool;
  461. PFSB_CPU_POOL_HEADER CpuPool;
  462. PFSB_PAGE_HEADER Page;
  463. PSLIST_ENTRY BlockLink;
  464. PUCHAR Block = NULL;
  465. KIRQL OldIrql;
  466. ULONG Cpu;
  467. LARGE_INTEGER Ticks;
  468. ASSERT(PoolHandle);
  469. Pool = (PFSB_POOL_HEADER)PoolHandle;
  470. // Raise IRQL before saving the processor number since there is chance
  471. // it could have changed if we saved it while at passive.
  472. //
  473. OldIrql = KeRaiseIrqlToDpcLevel();
  474. Cpu = KeGetCurrentProcessorNumber();
  475. CpuPool = (PFSB_CPU_POOL_HEADER)(Pool + 1) + Cpu;
  476. // See if the minimum time has passed since we last scavenged
  477. // the pool. If it has, we'll scavenge again. Normally, scavenging
  478. // should only be performed when we free. However, for the case when
  479. // the caller constantly frees on a non-owning processor, we'll
  480. // take this chance to do the scavenging.
  481. //
  482. KeQueryTickCount(&Ticks);
  483. if (Ticks.QuadPart > CpuPool->NextScavengeTick.QuadPart)
  484. {
  485. FsbpScavengePool(CpuPool);
  486. }
  487. if (!IsListEmpty(&CpuPool->PageList))
  488. {
  489. Page = CONTAINING_RECORD(CpuPool->PageList.Flink, FSB_PAGE_HEADER, PageLink);
  490. ASSERT(Page == PAGE_ALIGN(Page));
  491. ASSERT(CpuPool == Page->Pool);
  492. ASSERT(!Page->OnUsedPageList);
  493. BlockLink = InterlockedPopEntrySList(&Page->FreeList);
  494. if (BlockLink)
  495. {
  496. Block = (PUCHAR)BlockLink - Pool->FreeBlockLinkOffset;
  497. }
  498. else
  499. {
  500. // If there were no blocks on this page's free list, it had better
  501. // mean we haven't yet built all of the blocks on the page.
  502. // (Otherwise, what is a fully used page doing on the page list
  503. // and not on the used-page list?)
  504. //
  505. ASSERT(Page->BlocksBuilt < Pool->BlocksPerPage);
  506. Block = FsbpBuildNextBlock(Pool, Page);
  507. ASSERT(Block);
  508. }
  509. // Got a block. Now check to see if it was the last one on a fully
  510. // built page. If so, move the page to the used-page list.
  511. //
  512. if ((0 == ExQueryDepthSList(&Page->FreeList)) &&
  513. (Page->BlocksBuilt == Pool->BlocksPerPage))
  514. {
  515. PLIST_ENTRY PageLink;
  516. PageLink = RemoveHeadList(&CpuPool->PageList);
  517. InsertTailList(&CpuPool->UsedPageList, PageLink);
  518. Page->OnUsedPageList = TRUE;
  519. ASSERT(Page == CONTAINING_RECORD(PageLink, FSB_PAGE_HEADER, PageLink));
  520. }
  521. ASSERT(Block);
  522. goto GotABlock;
  523. }
  524. else
  525. {
  526. // The page list is empty so we have to allocate and add a new page.
  527. //
  528. Block = FsbpAllocateNewPageAndBuildOneBlock(Pool, CpuPool);
  529. }
  530. // If we are returning an block, update the statistics.
  531. //
  532. if (Block)
  533. {
  534. ULONG BlocksInUse;
  535. GotABlock:
  536. CpuPool->TotalBlocksAllocated++;
  537. BlocksInUse = CpuPool->TotalBlocksAllocated - CpuPool->TotalBlocksFreed;
  538. if (BlocksInUse > CpuPool->PeakBlocksInUse)
  539. {
  540. CpuPool->PeakBlocksInUse = BlocksInUse;
  541. }
  542. // Don't give anyone ideas about where this might point. I don't
  543. // want anyone trashing my pool because they thought this field
  544. // was valid for some reason.
  545. //
  546. ((PSINGLE_LIST_ENTRY)((PUCHAR)Block + Pool->FreeBlockLinkOffset))->Next = NULL;
  547. }
  548. KeLowerIrql(OldIrql);
  549. return Block;
  550. }
  551. // Free a block back to the pool from which it was allocated.
  552. //
  553. // Arguments:
  554. // Block - A block returned from a prior call to FsbAllocate.
  555. //
  556. // Caller IRQL: [PASSIVE_LEVEL, DISPATCH_LEVEL]
  557. //
  558. VOID
  559. FsbFree(
  560. IN PUCHAR Block
  561. )
  562. {
  563. PFSB_PAGE_HEADER Page;
  564. PFSB_CPU_POOL_HEADER CpuPool;
  565. PFSB_POOL_HEADER Pool;
  566. LARGE_INTEGER Ticks;
  567. LOGICAL PageIsOnUsedPageList;
  568. LOGICAL Scavenge = FALSE;
  569. ASSERT(Block);
  570. // Get the address of the page that this block lives on. This is where
  571. // our page header is stored.
  572. //
  573. Page = PAGE_ALIGN(Block);
  574. // Follow the back pointer in the page header to locate the owning
  575. // cpu's pool.
  576. //
  577. CpuPool = Page->Pool;
  578. // Locate the pool header.
  579. //
  580. Pool = PoolFromCpuPool(CpuPool);
  581. // See if the minimum time has passed since we last scavenged
  582. // the pool. If it has, we'll scavenge again.
  583. //
  584. KeQueryTickCount(&Ticks);
  585. if (Ticks.QuadPart > CpuPool->NextScavengeTick.QuadPart)
  586. {
  587. Scavenge = TRUE;
  588. }
  589. // Note the tick that this page was last used. If this is the last block
  590. // to be returned to this page, this sets the minimum time that this page
  591. // will continue to live unless it gets re-used.
  592. //
  593. Page->LastUsedTick.QuadPart = Ticks.QuadPart;
  594. // If this page is on the used-page list, we'll put it back on the normal
  595. // page list (only after pushing the block back on the page's free list)
  596. // if, after raising IRQL, we are on the processor that owns this
  597. // pool.
  598. //
  599. PageIsOnUsedPageList = Page->OnUsedPageList;
  600. InterlockedIncrement(&CpuPool->TotalBlocksFreed);
  601. // Now return the block to the page's free list.
  602. //
  603. InterlockedPushEntrySList(
  604. &Page->FreeList,
  605. (PSLIST_ENTRY)((PUCHAR)Block + Pool->FreeBlockLinkOffset));
  606. //
  607. // Warning: Now that the block is back on the page, one cannot *reliably*
  608. // dereference anything through 'Page' anymore. It may have just been
  609. // scavenged by its owning processor and subsequently freed. This is a
  610. // particularly rare condition given that MINIMUM_PAGE_LIFETIME_IN_SECONDS
  611. // is 20s, so we choose to live with it. The alternative would be to walk
  612. // the UsedPageList whenever PageIsOnUsedPageList is true, making the
  613. // FsbFree operation potentially expensive. We saved off the value of
  614. // Page->OnUsedPageList before returning the block so we would not risk
  615. // touching Page to get this value only to find that it was false.
  616. //
  617. // If we need to move the page from the used-page list to the normal
  618. // page list, or if we need to scavenge, we need to be at DISPATCH_LEVEL
  619. // and be executing on the processor that owns this pool.
  620. // Find out if the CPU we are executing on right now owns this pool.
  621. // Note that if we are running at PASSIVE_LEVEL, the current CPU may
  622. // change over the duration of this function call, so this value is
  623. // not absolute over the life of the function.
  624. //
  625. if ((PageIsOnUsedPageList || Scavenge) &&
  626. ((ULONG)KeGetCurrentProcessorNumber() == CpuPool->OwnerCpu))
  627. {
  628. KIRQL OldIrql;
  629. OldIrql = KeRaiseIrqlToDpcLevel();
  630. // Now that we are at DISPATCH_LEVEL, perform the work if we are still
  631. // executing on the processor that owns the pool.
  632. //
  633. if ((ULONG)KeGetCurrentProcessorNumber() == CpuPool->OwnerCpu)
  634. {
  635. // If the page is still on the used-page list (meaning another
  636. // FsbFree didn't just sneak by) and still has a free block (for
  637. // instance, an FsbAllocate might sneak in on its owning processor,
  638. // scavenge the page, and allocate the block we just freed; thereby
  639. // putting it back on the used-page list), then put the page on the
  640. // normal list. Very important to do this after (not before)
  641. // returning the block to the free list because FsbAllocate expects
  642. // blocks to be available from pages on the page list.
  643. //
  644. if (PageIsOnUsedPageList &&
  645. Page->OnUsedPageList &&
  646. (0 != ExQueryDepthSList(&Page->FreeList)))
  647. {
  648. RemoveEntryList(&Page->PageLink);
  649. Page->OnUsedPageList = FALSE;
  650. InsertTailList(&CpuPool->PageList, &Page->PageLink);
  651. }
  652. // Perform the scavenge if we previously noted we needed to do so.
  653. //
  654. if (Scavenge)
  655. {
  656. FsbpScavengePool(CpuPool);
  657. }
  658. }
  659. KeLowerIrql(OldIrql);
  660. }
  661. }