https://git.reactos.org/?p=reactos.git;a=commitdiff;h=d7de53b6d585a1048902b0376a16f85d8ebee114

commit d7de53b6d585a1048902b0376a16f85d8ebee114
Author:     Timo Kreuzer <timo.kreu...@reactos.org>
AuthorDate: Sat Apr 8 19:27:15 2023 +0300
Commit:     Timo Kreuzer <timo.kreu...@reactos.org>
CommitDate: Sat Jul 29 14:00:44 2023 +0300

    [NTOS:Mm] Bail out in MmTrimUserMemory, when all LRU pages have been looped 
through
---
 ntoskrnl/mm/balance.c | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/ntoskrnl/mm/balance.c b/ntoskrnl/mm/balance.c
index 0c9d34ce54f..a8c9e4c6482 100644
--- a/ntoskrnl/mm/balance.c
+++ b/ntoskrnl/mm/balance.c
@@ -138,14 +138,15 @@ MiTrimMemoryConsumer(ULONG Consumer, ULONG InitialTarget)
 NTSTATUS
 MmTrimUserMemory(ULONG Target, ULONG Priority, PULONG NrFreedPages)
 {
-    PFN_NUMBER CurrentPage;
+    PFN_NUMBER FirstPage, CurrentPage;
     NTSTATUS Status;
 
     (*NrFreedPages) = 0;
 
     DPRINT1("MM BALANCER: %s\n", Priority ? "Paging out!" : "Removing access 
bit!");
 
-    CurrentPage = MmGetLRUFirstUserPage();
+    FirstPage = MmGetLRUFirstUserPage();
+    CurrentPage = FirstPage;
     while (CurrentPage != 0 && Target > 0)
     {
         if (Priority)
@@ -156,6 +157,10 @@ MmTrimUserMemory(ULONG Target, ULONG Priority, PULONG 
NrFreedPages)
                 DPRINT("Succeeded\n");
                 Target--;
                 (*NrFreedPages)++;
+                if (CurrentPage == FirstPage)
+                {
+                    FirstPage = 0;
+                }
             }
         }
         else
@@ -245,8 +250,14 @@ MmTrimUserMemory(ULONG Target, ULONG Priority, PULONG 
NrFreedPages)
                 /* Nobody accessed this page since the last time we check. 
Time to clean up */
 
                 Status = MmPageOutPhysicalAddress(CurrentPage);
+                if (NT_SUCCESS(Status))
+                {
+                    if (CurrentPage == FirstPage)
+                    {
+                        FirstPage = 0;
+                    }
+                }
                 // DPRINT1("Paged-out one page: %s\n", NT_SUCCESS(Status) ? 
"Yes" : "No");
-                (void)Status;
             }
 
             /* Done for this page. */
@@ -254,6 +265,15 @@ MmTrimUserMemory(ULONG Target, ULONG Priority, PULONG 
NrFreedPages)
         }
 
         CurrentPage = MmGetLRUNextUserPage(CurrentPage, TRUE);
+        if (FirstPage == 0)
+        {
+            FirstPage = CurrentPage;
+        }
+        else if (CurrentPage == FirstPage)
+        {
+            DPRINT1("We are back at the start, abort!\n");
+            return STATUS_SUCCESS;
+        }
     }
 
     if (CurrentPage)

Reply via email to