From 7771d27525f68fc1514e3446d6608c0cc01d0557 Mon Sep 17 00:00:00 2001 From: Daniel Leung Date: Tue, 13 Jul 2021 14:08:05 -0700 Subject: [PATCH] kernel: mmu: move when page fault is counted The beginning of code in do_page_fault() is to pin the page in memory if it is already present in physical memory. It is there so that if a page is not present, it can proceed to perform page-in and then pin it. So the counting of page faults needs to be moved after the pinning code so it actually counts page faults, and not counting pinning operations when the page is already present. Also clarify the comment on the goto statement as it is not correct. Signed-off-by: Daniel Leung --- kernel/mmu.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/kernel/mmu.c b/kernel/mmu.c index 1da5496e966..26a4ae2958a 100644 --- a/kernel/mmu.c +++ b/kernel/mmu.c @@ -1239,8 +1239,6 @@ static bool do_page_fault(void *addr, bool pin) } result = true; - paging_stats_faults_inc(faulting_thread, key); - if (status == ARCH_PAGE_LOCATION_PAGED_IN) { if (pin) { /* It's a physical memory address */ @@ -1249,12 +1247,19 @@ static bool do_page_fault(void *addr, bool pin) pf = z_phys_to_page_frame(phys); pf->flags |= Z_PAGE_FRAME_PINNED; } - /* We raced before locking IRQs, re-try */ + + /* This if-block is to pin the page if it is + * already present in physical memory. There is + * no need to go through the following code to + * pull in the data pages. So skip to the end. + */ goto out; } __ASSERT(status == ARCH_PAGE_LOCATION_PAGED_OUT, "unexpected status value %d", status); + paging_stats_faults_inc(faulting_thread, key); + pf = free_page_frame_list_get(); if (pf == NULL) { /* Need to evict a page frame */