Hugepages for shm page cache (defrag)
Subsystems (2)
Hello,
This is may first try with Linux patch, so please do not blame me too much. Actually I started with small idea to add MAP_HUGTLB for /dev/shm but it grew up in something more like support for huge pages in page cache, but according to documentation to submit alpha-work too, I decided to send this.
-------------------
Idea is that I created defrag method which tries to defragment page cache to huge page, I added one flag for such compound page, as I want to treat it little different, for example any put/get unless/test zero will increase it count, so page will be freed if all "sub-pages" will be freed. From other side each tail page may have it's own LRU (why I don't know).
There is ofocourse much work to do in shm to make it hugepage aware, map pte(pde) etc.
At this stage I don't have BUGs and I can make 2MB shm area, read and delete it.
-------------------
Any suggestion are welcome.
If it's to early work I want to say sorry. I hope I fullfiled all code/submit conventions.
Regards,
Radosław Smogura
Quoted 6 lines · Radosław Smogura
Hello,
This is may first try with Linux patch, so please do not blame me too much. Actually I started with small idea to add
MAP_HUGTLBfor/dev/shmbut it grew up in something more like support for huge pages in page cache, but according to documentation to submit alpha-work too, I decided to send this.
Shouldn't this be rather integrated with the normal transparent huge pages? It seems odd to develop parallel infrastructure.
-Andi
Signature · 8 lines
[email protected] -- Speaking for myself only -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 9 lines · Andi Kleen
Quoted 6 lines · RadosA?aw Smogura
Hello,
This is may first try with Linux patch, so please do not blame me too much. Actually I started with small idea to add
MAP_HUGTLBfor/dev/shmbut it grew up in something more like support for huge pages in page cache, but according to documentation to submit alpha-work too, I decided to send this.
Quoted 4 lines
Shouldn't this be rather integrated with the normal transparent huge pages? It seems odd to develop parallel infrastructure.
-Andi
It's not quite good to ask me about this, as I'm starting hacker, but I think it should be treated as counterpart for page cache, and actually I got few "collisions" with THP.
High level design will probably be the same (e.g. I use defrag_, THP uses collapse_ for creating huge page), but in contrast I try to operate on page cache, so in some way file system must be huge page aware (shm fs is not, as it can move page from page cache to swap cache - it may silently fragment de-fragmented areas).
I put some requirements for work, e. g. mapping file as huge should not affect previous or future, even fancy, non huge mappings, both callers should succeed and get this what they asked for.
Of course I think how to make it more "transparent" without need of file system support, but I suppose it may be dead-corner.
I still want to emphasise it's really alpha version.
Regards,
Radek
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 13 lines · On Thu, 7 Jul 2011, [email protected] wrote:
Quoted 12 lines · Andi Kleen
Quoted 9 lines · Radosław Smogura
Hello,
This is may first try with Linux patch, so please do not blame me too much.
Actually I started with small idea to addMAP_HUGTLBfor/dev/shmbut it grew
up in something more like support for huge pages in page cache, but according
to documentation to submit alpha-work too, I decided to send this.
Quoted 4 lines
Quoted 4 lines
Shouldn't this be rather integrated with the normal transparent huge pages? It seems odd to develop parallel infrastructure.
-Andi
Although Andi's sig says "Speaking for myself only", he is very much speaking for me on this too ;)
There is definitely interest in extending Transparent Huge Pages to tmpfs; though so far as I know, nobody has yet had time to think through just what that will entail.
Correspondingly, I'm afraid there would be little interest in adding yet another variant of hugepages into the kernel - enough ugliness already!
Quoted 10 lines
It's not quite good to ask me about this, as I'm starting hacker, but I think it should be treated as counterpart for page cache, and actually I got few "collisions" with THP.
High level design will probably be the same (e.g. I use
defrag_, THP usescollapse_for creating huge page), but in contrast I try to operate on page cache, so in some way file system must be huge page aware (shm fs is not, as it can move page from page cache to swap cache - it may silently fragment de-fragmented areas).
Quoted 8 lines
I put some requirements for work, e. g. mapping file as huge should not affect previous or future, even fancy, non huge mappings, both callers should succeed and get this what they asked for.
Of course I think how to make it more "transparent" without need of file system support, but I suppose it may be dead-corner.
I still want to emphasise it's really alpha version.
I barely looked at it, but did notice that scripts/checkpatch.pl reports 127 errors and 111 warnings, plus it seems to be significantly incomplete (an extern declaration of defragPageCache() but not the function itself).
And it serves no purpose without the pte work you mention (there is no point to a shmem hugepage unless it is mapped in that way).
Sorry to be discouraging, but extending THP is likely to be the way to go.
Hugh
Quoted 57 lines · Hugh Dickins
Quoted 12 lines · On Thu, 7 Jul 2011, [email protected] wrote:
Quoted 5 lines · Andi Kleen
Quoted 3 lines · RadosA?aw Smogura
Hello,
This is may first try with Linux patch, so please do not blame
me too
Quoted 2 lines
Quoted 2 lines
much.
Actually I started with small idea to addMAP_HUGTLBfor
/dev/shmbut itQuoted 2 lines
Quoted 2 lines
grew
up in something more like support for huge pages in page cache,Quoted 10 lines
but
Quoted 2 lines
Quoted 2 lines
according
to documentation to submit alpha-work too, I decided to sendthis.
Quoted 2 lines
Shouldn't this be rather integrated with the normal transparent
huge
Quoted 3 lines
pages? It seems odd to develop parallel infrastructure.
-Andi
Although Andi's sig says "Speaking for myself only", he is very much speaking for me on this too ;)
There is definitely interest in extending Transparent Huge Pages to tmpfs; though so far as I know, nobody has yet had time to think through just what that will entail.
Correspondingly, I'm afraid there would be little interest in adding yet another variant of hugepages into the kernel - enough ugliness already!
Quoted 10 lines
It's not quite good to ask me about this, as I'm starting hacker, but I think it should be treated as counterpart for page cache, and actually I got few "collisions" with THP.
High level design will probably be the same (e.g. I use
defrag_, THP usescollapse_for creating huge page), but in contrast I try to operate on page cache, so in some way file system must be huge page aware (shm fs is not, as it can move page from page cache to swap cache - it may silently fragment de-fragmented areas).Quoted 8 lines
I put some requirements for work, e. g. mapping file as huge should not affect previous or future, even fancy, non huge mappings, both callers should succeed and get this what they asked for.
Of course I think how to make it more "transparent" without need of file system support, but I suppose it may be dead-corner.
I still want to emphasise it's really alpha version.
I barely looked at it, but did notice that
scripts/checkpatch.plreports 127 errors and 111 warnings, plus it seems to be significantly incomplete (an extern declaration ofdefragPageCache()but not the function itself).
Quoted 6 lines
And it serves no purpose without the pte work you mention (there is no point to a shmem hugepage unless it is mapped in that way).
Sorry to be discouraging, but extending THP is likely to be the way to go.
Hugh
Hi,
I working to remove errors from patch, and I integrated it with current THP infrastructure a little bit, but I want ask if following I do following - it's about get_page, put_page, get_page_unless_zero, put_page_test_zero.
I want following logic I think it may be better (in x86):
1) Each THP page will start with 512 refcount (self + 511 tails)
2) Each get/put will increment usage count only on this page, same test variants will do (currently those do not make this, so split is broken)
3) On compounds put page will call put_page_test_zero, if true, it will do compound lock, ask again if it has 0, if yes it will decrease refcount of head, if it will fall to zero compound will be freed (double check lock).
4) Compound lock is this what caller will need to establish if it needs to operate on transparent huge page in whole.
Motivation:
I operate on page cache, many assumptions about concurrent call of put/get_page are and plain using those causes memory leaks, faults, dangling pointers, etc when I'm going to split compound page.
Is this acceptable?
Regards,
Radek
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 2 lines · On Thu, 14 Jul 2011, [email protected] wrote:
I working to remove errors from patch, and I integrated it with current THP infrastructure a little bit,
Great, thank you.
Quoted 13 lines
but I want ask if following I do following -
it's aboutget_page,put_page,get_page_unless_zero,put_page_test_zero.I want following logic I think it may be better (in x86):
1) Each THP page will start with 512 refcount (self + 511 tails)
2) Each get/put will increment usage count only on this page, same test variants will do (currently those do not make this, so split is broken)
3) On compounds put page will callput_page_test_zero, if true, it will do compound lock, ask again if it has 0, if yes it will decrease refcount of head, if it will fall to zero compound will be freed (double check lock).
4) Compound lock is this what caller will need to establish if it needs to operate on transparent huge page in whole.
Quoted 6 lines
Motivation:
I operate on page cache, many assumptions about concurrent call of put/get_page are and plain using those causes memory leaks, faults, dangling pointers, etc when I'm going to split compound page.Is this acceptable?
Sounds plausible, but I really don't know.
I do remember that refcounting compounds by head or by tail always raises questions (and access via get_user_pages() is an easily-overlooked path that needs to be kept in mind). But where THP stands today, and how it needs to be changed for this, I have no idea - whereas Andrea, perhaps, will recognize some of your points above and have a more useful response.
It's clear that you have much more of a grip on these details than I have at present, so just be guided by the principle of not slowing down the common paths.
Hugh
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/1 Changes how ref-count of compound pages are managed.
WARNING. This change alone may at least break THP splitting. WARNING. Read end.
Each compound page is made from head and tail of pages. Compound huge page starts with head ref-count 2^order on head, and ref-count 1 for each tail, but this is, currently, caller responsibility to ensure this.
If ref-count of tail goes to 0 then ref-count of head is decreased by 1. This is changes previous behaviour which managed (only partially) head ref-count as head's individual counts + sum tail's ref-count.
To make this consistent page_count no longer returns count of head, but only count of tail. Changes has been made to put_page_test_zero, get_page_unless_zero.
Freeing compound page is protected by compound on head with double check locking.
This makes
- page splitting for transparent anonymous huge pages, and for transparent huge pages for page cache (WIP) more securely and simpler. Both of above to fix head ref-count may subtract values in block of compound lock and if get_page_unless_zero returned true,
- removes some atomic_inc, and dangling pointers.
WARNING.
This doesn't prevent situations when pages is splitted from larger compound page to smaller compound page. If this happens (currently no one makes this), then double-check locking is not enough. Imagine that you have page of order 2, you put last tail page (4th), some one splits it to two order 1 compound pages then head of 4th page will change to 3rd (was 1st). If splitting to smaller compound pages will be possible then, recheck if head doesn't changed and relocking of head will be required.
This patch doesn't comes with changes for others (see warning), and is made for preview.
--- include/linux/mm.h | 101 +++++++++++++++++++++++++----------- mm/swap.c | 142 +++++++++++++++++++++++++--------------------------- 2 files changed, 139 insertions(+), 104 deletions(-)
include/linux/mm.h +72 -33
@@ -267,18 +267,51 @@ struct inode;* routine so they can be sure the page doesn't go away from under them. */ + +/** Decreases refcount of page's head. <b>Refcount of {@code page} must be +* {@code 0}</b>. +* You may pass page head as {@code page} - in this case if {@code page} +* refcount is {@code 0} then compound page will be freed. +* +* @see get_page +*/ +void put_compound_head_of_page (struct page *page); + +static inline struct page *compound_head (struct page *page) +{ +if (unlikely (PageTail (page))) +return page->first_page; +return page; +} +/* * Drop a ref, return true if the refcount fell to zero (the page has no users) */ static inline int put_page_testzero (struct page *page) { +int result;VM_BUG_ON (atomic_read (&page->_count) ==0 ); -return atomic_dec_and_test (&page->_count); + + result =atomic_dec_and_test (&page->_count); +if (unlikely (PageTail (page) && result)) { +/* If below BUG will be shown, or "null" exception raised +* the page could be splitted from under us (but it should not). +*/ +VM_BUG_ON (atomic_read (&page->first_page->_count) ==0 ); +put_compound_head_of_page (page); + }else { +/* TODO Free page? - orignally page was not freed. +* Page can be head of compund or "stand alone" page. +*/ + } + +return result; }/* * Try to grab a ref unless the page has a refcount of zero, return false if * that is the case. +* @see get_page */ static inline int get_page_unless_zero (struct page *page) {
@@ -349,43 +382,49 @@ static inline void compound_unlock_irqrestore(struct page *page,#endif } -static inline struct page *compound_head (struct page *page) -{ -if (unlikely (PageTail (page))) -return page->first_page; -return page; -} - +/** Returns usage count of given page. +* @see get_page. +*/ static inline int page_count (struct page *page) { -return atomic_read (&compound_head (page)->_count); -} - +return atomic_read (&page->_count); +} + +/** Increases refcount of page. +* <h1>Logic of refcounting pages</h1> +* <h2>Normal pages (not compound)</h2> +* Getting or putting page will increase refcount of this page, when refcount +* falls to {@code 0} then page will be freed. +* <h2>Compound pages (transparent huge pages)</h2> +* Each compound page is made from {@code 2^order} of pages, 1st page is +* <b>head</b> rest pages are <b>tail</b> pages. Compound huge page +* starts (when allocated) with refcount {@code 2^order} on head, and refcount +* {@code 1} for each tail (developer may need to ensure this by self).<br/> +* Getting or putting any of page (head or tail) will increase / decrease +* refcount of this page <b>only</b>, but with <b>exceptions:</b>. +* <ol> +* <li> +* If refcount of tail page will fall to {@code 0}, then compound lock is +* aquired on page head (to prevent splitting by others), if page is still +* compound then refcount of head is decremented by {@code 1}. +* </li> +* <li> +* If recount of head will fall to {@code 0} then compound lock on head +* is aquired (to prevent background splitting), if page is still compound +* then it will be freed as compound. +* </li> +* </ol> +* This function will not free pages if those has been splitted before head +* refcount has been decremented (in particullary, if split occurs, then +* splitter is reponsible for freeing pages). +*/ static inline void get_page (struct page *page) { -/* -* Getting a normal page or the head of a compound page -* requires to already have an elevated page->_count. Only if -* we're getting a tail page, the elevated page->_count is -* required only in the head page, so for tail pages the -* bugcheck only verifies that the page->_count isn't -* negative. -*/ -VM_BUG_ON (atomic_read (&page->_count) < !PageTail (page)); +/* Disallow of getting any page (event tail) if it refcount felt +* to zero */ +VM_BUG_ON (atomic_read (&page->_count) <=0 ); +atomic_inc (&page->_count); -/* -* Getting a tail page will elevate both the head and tail -* page->_count(s). -*/ -if (unlikely (PageTail (page))) { -/* -* This is safe only because -* __split_huge_page_refcount can't run under -* get_page(). -*/ -VM_BUG_ON (atomic_read (&page->first_page->_count) <=0 ); -atomic_inc (&page->first_page->_count); - } }static inline struct page *virt_to_head_page (const void *x)
mm/swap.c +69 -73
159 lines not on this page · raw message
Signature · 8 lines
1.7.3.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andi Kleen <[email protected]> Thursday 07 of July 2011 07:28:59
Quoted 9 lines
Quoted 7 lines · Radosław Smogura
Hello,
This is may first try with Linux patch, so please do not blame me too much. Actually I started with small idea to add
MAP_HUGTLBfor/dev/shmbut it grew up in something more like support for huge pages in page cache, but according to documentation to submit alpha-work too, I decided to send this.
Quoted 4 lines
Shouldn't this be rather integrated with the normal transparent huge pages? It seems odd to develop parallel infrastructure.
-Andi
Hello,
I send as attachment some preview of work, last time I putted many work to make code less messy, etc, but it is still messy and in alpha stage.
=== Info about changes and how it's works ===
Current work is focused to give support for huge pages for tmpfs, but I designed it for private mappings too (my dream will be if I will be able to map execution portions of glibc as huge). I think there will be no big change to expose THP for general file system.
I putted some effort to do not create new type of huge pages, so I do not use longer special flag for managing this.
PMD is establish (from historical reasons) from pte fault, so I unmaps pte's from pmd (I think I have memory leak here).
Splitting of huge page for page cache still need some work, and it has different concept. Mainly I use currently compound_lock to prohibit concurrent splitting etc. Splitting acquires compound lock, then unmaps all pmds, and split occurs.
In splitting there is usage of new ref-counting. I may be wrong, but in other cases we will need to manage ref-count bit for compound pages (this what is included /mm.h, swap.c/ is slightly different from my previous send; I still think if I should use irqsave lock for compounds). I think included construct removes all "under us" risks.
I don't want to waste Your time for reviewing this work, but I will be glad if you will find some time. One thing I worried is about pde locking is it needed to do pmd locking, like for pte (or any other kind of locking? I have some ideas)?
I hope this time here is no missing files.
Regards,
Radosław Smogura
P. S. In one of mails in CC there is putted wrongly my e-mail address and name, please remove it to avoid unknown recipient.