0

I am a beginner in Linux and have always been confused about the difference between kernel space and user space. I am currently researching memory management and would like to know if the virtual address of a page of a user process belongs to user space or kernel space when swapping in or out?

(as in 64 bit Linux

User state virtual address range: 0x0000000 000000-0x00007FFFFFFFFFFFFF

Kernel state virtual address range: 0xFFFF8000000000-0xFFFFFFFFFFFFFFFFFFFF)

linux-6.5.1/mm/memory.c

mm_account_fault()
    if (major) {
        struct major_page_fault_hash_entry *temp = NULL;
        unsigned long pfn = virt_to_pfn(address);
        printk("[%lu][i][%lu]", pfn, address);
        perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
    }

linux-6.5.1/mm/swap_state.c

bool add_to_swap(struct folio *folio)
    long pages_number = folio_nr_pages(folio);
    unsigned long start_pfn = folio_pfn(folio);

    for (int i = 0; i < pages_number; i++) {
        struct page *current_page = pfn_to_page(start_pfn + i);
        unsigned long current_virt_addr = (unsigned long)page_to_virt(current_page);
        printk("[%lu][o][%lu]\n",start_pfn + i, current_virt_addr);
    }
7
  • Whether an address is in user or kernel space doesn't change over time. Swapping in/out has no effect on it.
    – Barmar
    Commented Jul 5 at 15:59
  • As you pointed out, these are just defined by address ranges. Since the virtual address doesn't change when it's swapped in or out, how could that affect which space it belongs to?
    – Barmar
    Commented Jul 5 at 16:01
  • @Barmar I didn't mean the change of virtual address caused by page swapping. Do you mean that if the virtual address of the obtained page belongs to user space or kernel space, then the page has already been determined when allocation? By modifying the kernel, the virtual address of the page swapping out I get is located in kernel space, so I don't quite understand that the virtual address of a user mode process's page is is kernel space..
    – Mia8Cew
    Commented Jul 5 at 16:04
  • I'm not sure what you mean by that. The virtual address doesn't change when a page is being swapped out. I think you may be misunderstanding what you're seeing in the kernel modification.
    – Barmar
    Commented Jul 5 at 16:07
  • @Barmar The analysis result I obtained is that the virtual address of the swapped out page is in kernel space, and the virtual address of the swapped in page is in user space. Is this related to the page allocation of the operating system or to the program settings in user mode? Thanks a lot for you explanation!
    – Mia8Cew
    Commented Jul 5 at 16:09

1 Answer 1

1

You are printing a user virtual address (and the PFN it is pointing to) on fault, and a kernel virtual address (and the PFN it is pointing to) on add_to_swap(). It doesn't make much sense to compare the two things.

A physical memory page can have any number of virtual addresses pointing to it, both in kernel and in user space, at any moment. Furthermore, a page can be moved around in physical memory and its PFN can change over time. A page being swapped out only means that the bits of its page table entry will be updated to indicate that the page is swapped, so that it can be correctly swapped in on the next page fault. No change of existing user virtual addresses will occur or can occur.


My purpose in collecting these data is to check if the same page has been swapped in and out multiple times during a user process

You should be able to do this from userspace through /proc/[pid]/pagemap. See the documentation here. In particular, bit 62 tells you whether the page is swapped or not, so if you can query that bit often enough you can get the information you want.

1
  • Thanks for you explanation! My purpose in collecting these data is to check if the same page has been swapped in and out multiple times during a user process, but now I have found that PFN is not fixed. So what about virtual addresses? Do you have any good ideas that can help me achieve my goals
    – Mia8Cew
    Commented Jul 7 at 1:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.