LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-27-2023, 01:33 AM   #1
huang-jl
LQ Newbie
 
Registered: Jun 2023
Posts: 3

Rep: Reputation: 0
Will the implementation of mremap free all physical pages ?


Recently, I am reading the memory management part of linux kernel code (the version I am reading is 4.10). I find that mremap is strange.

In short, sys_mremap() will call move_vma(), which basically will do the following:

1. copy_vma()
2. move_page_tables()
3. do_munmap()

The step 1 and 2 are easy to understand, they just copy from the old memory mapping into the new mapping. However, they do not increment the refcount of struct page while copying to new memory mapping in these two steps (I do not find it in source code).

Then in step 3, kernel will unmap the old mappings. It iterates every vma and every pages, decrement the refcount of struct page and finally call tlb_flush_mmu_free() to free all pages.

My question is:
1. if the kernel implement mremap like this, the following access to the pages will cause page fault?
2. why kernel does not increment the refcount when move_vma()? It will keep pages in memory after mremap and will not casue the unnecessary page fault.
3. What about anonymous and private mapped pages? How can user access the content after this kind of pages has been freed?

Last edited by huang-jl; 06-27-2023 at 01:47 AM.
 
Old 06-28-2023, 11:52 AM   #2
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Quote:
Originally Posted by huang-jl View Post
Recently, I am reading the memory management part of linux kernel code (the version I am reading is 4.10). I find that mremap is strange.
Just so you are aware.
Very few if any distros still use the kernels that are older than 5.10. Thus the code you are reading is very out-of-date and much has changed. The current kernel version being worked on today is 6.4 and most of the distros are using either a very late 5.X version or the early 6.X versions.

Although some things are still the same, much has been updated. Working with a newer version will be closer to the present code levels.
 
Old 06-28-2023, 11:39 PM   #3
huang-jl
LQ Newbie
 
Registered: Jun 2023
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by computersavvy View Post
Just so you are aware.
Although some things are still the same, much has been updated. Working with a newer version will be closer to the present code levels.
I understand that and any explaination to my question based on newer linux version is fine. I post the code version I am reading just for clarity. And the mremap() is a relatively stable syscall, so the implementation might not changed too much in newer version.
 
Old 06-29-2023, 01:39 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
It seems to me this would be better off in the Programming forum.
You can ask the Mods to move it via the 'Report' button.
 
Old 06-29-2023, 01:53 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,863

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
Quote:
Originally Posted by huang-jl View Post
1. if the kernel implement mremap like this, the following access to the pages will cause page fault?
Obviously it works, there was no any problem with memory management. I think you need to go deep into the details to understand how does it really work.
https://docs.kernel.org/admin-guide/mm/index.html. From my side it is not an easy topic.
Quote:
Originally Posted by huang-jl View Post
2. why kernel does not increment the refcount when move_vma()? It will keep pages in memory after mremap and will not casue the unnecessary page fault.
I don't understand why should it increment refcount in case of a move.
by the way, this document has a nice picture about the call graph (page 73)
Quote:
Originally Posted by huang-jl View Post
3. What about anonymous and private mapped pages? How can user access the content after this kind of pages has been freed?
This is definitely documented and explained. Otherwise I don't really understand this question, if a page is freed it is not in use and cannot be accessed by apps - if I understand well.

https://unix.stackexchange.com/quest...physical-pages
 
Old 06-29-2023, 11:55 PM   #6
huang-jl
LQ Newbie
 
Registered: Jun 2023
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pan64 View Post
I don't understand why should it increment refcount in case of a move.
What move_vma() actually does is

(1) create a new mapping at destination.
(2) copy old mapping from source to the new mapping created in (1).
(3) unmap old mapping.

But the code shows that it will decrement refcount of struct page at (3) but does not increment the refcount of struct page at (1) or (2). So after move_vma(), there is still a memory mapping (at destination) needs those pages but it has been cleaned in step (3).

So I wonder why kernel does not increment refcount of struct page in (1) or (2), so after the decrement in (3) the pages will not be freed.

Quote:
Originally Posted by pan64 View Post
This is definitely documented and explained. Otherwise I don't really understand this question, if a page is freed it is not in use and cannot be accessed by apps - if I understand well
So as I explained, the struct page has been freed in (3) and the physical memory page's content may disappear. If it is an anonymous and private pages, there is no place to find the memory content if user access it through the new mapping after calling sys_mremap().


Thanks for your reply and discussion!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: ARM Processor and Physical IP Deliver Groundbreaking 750 MHz ARM11 Implementation LXer Syndicated Linux News 1 01-25-2006 03:56 PM
When will the mremap() kernel bug get patched? KingofBLASH Slackware 4 03-08-2004 05:53 PM
Yet another mremap critical flaw? chort Linux - Security 5 03-08-2004 01:31 PM
Second mremap critical bug zuessh Linux - Security 19 02-24-2004 06:24 PM
should I be worried about the Second mremap critical bug? Mandrake 9.2 user Fear58 Linux - Security 3 02-21-2004 12:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration