mm/fadvise: introduce POSIX_FADV_MLOCK
Subsystems (2)
RFC PATCH 1/1 mm/fadvise: introduce POSIX_FADV_MLOCK
This patch introduces a new POSIX_FADV_MLOCK which 1) invalidates the range of cached pages, 2) sets the mapping as inaccessible, 3) POSIX_FADV_WILLNEED loads pages directly to the inaccessible mapping.
The inaccessible pages will be invalidated by evict_inode or explicit munlock().
--- include/uapi/linux/fadvise.h | 2 ++ mm/fadvise.c | 14 ++++++++++++++ 2 files changed, 16 insertions(+)
include/uapi/linux/fadvise.h +2 -0
@@ -19,4 +19,6 @@#define POSIX_FADV_NOREUSE 5/* Data will be accessed once. */ #endif +#define POSIX_FADV_MLOCK 8/* Load pages into inaccessible map. */ +#endif /* FADVISE_H_INCLUDED */
mm/fadvise.c +14 -0
@@ -56,6 +56,7 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)case POSIX_FADV_WILLNEED :case POSIX_FADV_NOREUSE :case POSIX_FADV_DONTNEED : +case POSIX_FADV_MLOCK :/* no bad return value, but ignore advice */ break ;default :
@@ -93,6 +94,19 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice) file->f_mode &= ~FMODE_RANDOM ;spin_unlock (&file->f_lock);break ; +case POSIX_FADV_MLOCK : +/* Remove the cached pages. */ +if (!mapping_unevictable (mapping)) { +invalidate_inode_pages2_range (mapping, + offset >>PAGE_SHIFT , + (offset + len -1 ) >>PAGE_SHIFT ); + +/* set the mapping is unevictable */ +filemap_invalidate_lock (mapping); +mapping_set_inaccessible (mapping); +filemap_invalidate_unlock (mapping); + } + fallthrough;case POSIX_FADV_WILLNEED :/* First and last PARTIAL page! */ start_index = offset >>PAGE_SHIFT ;
Signature · 1 line
2.52.0.487.g5c8c507ade-goog
Quoted 3 lines · Jaegeuk Kim
This patch introduces a new
POSIX_FADV_MLOCKwhich 1) invalidates the range of cached pages, 2) sets the mapping as inaccessible, 3)POSIX_FADV_WILLNEEDloads pages directly to the inaccessible mapping.
... what?
This seems like something which is completely different from mlock(). So it needs a different name.
But I don't understand the point of this, whatever it's called. Need more information.
Quoted 56 lines
The inaccessible pages will be invalidated by
evict_inodeor explicitmunlock().Cc Matthew Wilcox (Oracle) <[email protected]>Cc Christian Brauner <[email protected]>Signed-off-by Jaegeuk Kim <[email protected]>--- include/uapi/linux/fadvise.h | 2 ++ mm/fadvise.c | 14 ++++++++++++++ 2 files changed, 16 insertions(+)include/uapi/linux/fadvise.h +2
-0@@ -19,4 +19,6 @@#define POSIX_FADV_NOREUSE 5/* Data will be accessed once. */ #endif +#define POSIX_FADV_MLOCK 8/* Load pages into inaccessible map. */ +#endif /* FADVISE_H_INCLUDED */ mm/fadvise.c +14
-0@@ -56,6 +56,7 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)case POSIX_FADV_WILLNEED :case POSIX_FADV_NOREUSE :case POSIX_FADV_DONTNEED : +case POSIX_FADV_MLOCK :/* no bad return value, but ignore advice */ break ;default :@@ -93,6 +94,19 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice) file->f_mode &= ~FMODE_RANDOM ;spin_unlock (&file->f_lock);break ; +case POSIX_FADV_MLOCK : +/* Remove the cached pages. */ +if (!mapping_unevictable (mapping)) { +invalidate_inode_pages2_range (mapping, + offset >>PAGE_SHIFT , + (offset + len -1 ) >>PAGE_SHIFT ); + +/* set the mapping is unevictable */ +filemap_invalidate_lock (mapping); +mapping_set_inaccessible (mapping); +filemap_invalidate_unlock (mapping); + } + fallthrough;case POSIX_FADV_WILLNEED :/* First and last PARTIAL page! */ start_index = offset >>PAGE_SHIFT ;Signature · 1 line
2.52.0.487.g5c8c507ade-goog
Quoted 5 lines · Matthew Wilcox
Quoted 3 lines · Jaegeuk Kim
This patch introduces a new
POSIX_FADV_MLOCKwhich 1) invalidates the range of cached pages, 2) sets the mapping as inaccessible, 3)POSIX_FADV_WILLNEEDloads pages directly to the inaccessible mapping.
Quoted 7 lines
... what?
This seems like something which is completely different from
mlock(). So it needs a different name.But I don't understand the point of this, whatever it's called. Need more information.
So, the sequence that I'd like to optimize is mmap(MAP_POPULATE) followed by mlock(). For example, mmap() takes 1 second to load 4GB data, and mlock() takes 330ms additionally in order to migrate all the pages into inaccessible map, IIUC.
So, I'm thinking to combine two operations into single fadvise() with whatever advise. Does it make sense?
Quoted 57 lines
Quoted 57 lines
The inaccessible pages will be invalidated by
evict_inodeor explicitmunlock().Cc Matthew Wilcox (Oracle) <[email protected]>Cc Christian Brauner <[email protected]>Signed-off-by Jaegeuk Kim <[email protected]>--- include/uapi/linux/fadvise.h | 2 ++ mm/fadvise.c | 14 ++++++++++++++ 2 files changed, 16 insertions(+)include/uapi/linux/fadvise.h +2
-0@@ -19,4 +19,6 @@#define POSIX_FADV_NOREUSE 5/* Data will be accessed once. */ #endif +#define POSIX_FADV_MLOCK 8/* Load pages into inaccessible map. */ +#endif /* FADVISE_H_INCLUDED */ mm/fadvise.c +14
-0@@ -56,6 +56,7 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)case POSIX_FADV_WILLNEED :case POSIX_FADV_NOREUSE :case POSIX_FADV_DONTNEED : +case POSIX_FADV_MLOCK :/* no bad return value, but ignore advice */ break ;default :@@ -93,6 +94,19 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice) file->f_mode &= ~FMODE_RANDOM ;spin_unlock (&file->f_lock);break ; +case POSIX_FADV_MLOCK : +/* Remove the cached pages. */ +if (!mapping_unevictable (mapping)) { +invalidate_inode_pages2_range (mapping, + offset >>PAGE_SHIFT , + (offset + len -1 ) >>PAGE_SHIFT ); + +/* set the mapping is unevictable */ +filemap_invalidate_lock (mapping); +mapping_set_inaccessible (mapping); +filemap_invalidate_unlock (mapping); + } + fallthrough;case POSIX_FADV_WILLNEED :/* First and last PARTIAL page! */ start_index = offset >>PAGE_SHIFT ;Signature · 1 line
2.52.0.487.g5c8c507ade-goog
Quoted 11 lines · Jaegeuk Kim
Quoted 10 lines · Matthew Wilcox
Quoted 3 lines · Jaegeuk Kim
This patch introduces a new
POSIX_FADV_MLOCKwhich 1) invalidates the range of cached pages, 2) sets the mapping as inaccessible, 3)POSIX_FADV_WILLNEEDloads pages directly to the inaccessible mapping.... what?
This seems like something which is completely different from
mlock(). So it needs a different name.
Quoted 7 lines
Quoted 2 lines
But I don't understand the point of this, whatever it's called. Need more information.
So, the sequence that I'd like to optimize is
mmap(MAP_POPULATE)followed bymlock(). For example,mmap()takes 1 second to load 4GB data, andmlock()takes 330ms additionally in order to migrate all the pages into inaccessible map, IIUC.
Oh, so the MLOCK part is right, but the inaccessible() part is wrong. Inaccessible is special weird guest_memfd crap that has all kinds of side-effects that you don't want.
Wouldn't you get the same effect by calling mlock2(MLOCK_ONFAULT) and then calling readahead() for the desired range?
Quoted 20 lines · Matthew Wilcox
Quoted 11 lines · Jaegeuk Kim
Quoted 10 lines · Matthew Wilcox
Quoted 3 lines · Jaegeuk Kim
This patch introduces a new
POSIX_FADV_MLOCKwhich 1) invalidates the range of cached pages, 2) sets the mapping as inaccessible, 3)POSIX_FADV_WILLNEEDloads pages directly to the inaccessible mapping.... what?
This seems like something which is completely different from
mlock(). So it needs a different name.Quoted 7 lines
Quoted 2 lines
But I don't understand the point of this, whatever it's called. Need more information.
So, the sequence that I'd like to optimize is
mmap(MAP_POPULATE)followed bymlock(). For example,mmap()takes 1 second to load 4GB data, andmlock()takes 330ms additionally in order to migrate all the pages into inaccessible map, IIUC.
Quoted 6 lines
Oh, so the MLOCK part is right, but the
inaccessible()part is wrong. Inaccessible is special weirdguest_memfdcrap that has all kinds of side-effects that you don't want.Wouldn't you get the same effect by calling
mlock2(MLOCK_ONFAULT)and then callingreadahead()for the desired range?
Oh, thank you. Let me try.
Quoted 21 lines · Jaegeuk Kim
Quoted 20 lines · Matthew Wilcox
Quoted 18 lines · Jaegeuk Kim
Quoted 5 lines · Matthew Wilcox
Quoted 3 lines · Jaegeuk Kim
This patch introduces a new
POSIX_FADV_MLOCKwhich 1) invalidates the range of cached pages, 2) sets the mapping as inaccessible, 3)POSIX_FADV_WILLNEEDloads pages directly to the inaccessible mapping.Quoted 7 lines
... what?
This seems like something which is completely different from
mlock(). So it needs a different name.But I don't understand the point of this, whatever it's called. Need more information.
So, the sequence that I'd like to optimize is
mmap(MAP_POPULATE)followed bymlock(). For example,mmap()takes 1 second to load 4GB data, andmlock()takes 330ms additionally in order to migrate all the pages into inaccessible map, IIUC.
Quoted 8 lines
Quoted 6 lines
Oh, so the MLOCK part is right, but the
inaccessible()part is wrong. Inaccessible is special weirdguest_memfdcrap that has all kinds of side-effects that you don't want.Wouldn't you get the same effect by calling
mlock2(MLOCK_ONFAULT)and then callingreadahead()for the desired range?Oh, thank you. Let me try.
After checking the code and experiment, I don't think that gives what we need. That flag skips populate_vma_page_range only, but we need to allocate pages in the inaccessible mapping and fill the pages afterwards.
Quoted 26 lines · Jaegeuk Kim
Quoted 25 lines · Jaegeuk Kim
Quoted 24 lines · Matthew Wilcox
Quoted 11 lines · Jaegeuk Kim
Quoted 10 lines · Matthew Wilcox
Quoted 3 lines · Jaegeuk Kim
This patch introduces a new
POSIX_FADV_MLOCKwhich 1) invalidates the range of cached pages, 2) sets the mapping as inaccessible, 3)POSIX_FADV_WILLNEEDloads pages directly to the inaccessible mapping.... what?
This seems like something which is completely different from
mlock(). So it needs a different name.Quoted 7 lines
Quoted 2 lines
But I don't understand the point of this, whatever it's called. Need more information.
So, the sequence that I'd like to optimize is
mmap(MAP_POPULATE)followed bymlock(). For example,mmap()takes 1 second to load 4GB data, andmlock()takes 330ms additionally in order to migrate all the pages into inaccessible map, IIUC.Oh, so the MLOCK part is right, but the
inaccessible()part is wrong. Inaccessible is special weirdguest_memfdcrap that has all kinds of side-effects that you don't want.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Wouldn't you get the same effect by calling
mlock2(MLOCK_ONFAULT)and then callingreadahead()for the desired range?Oh, thank you. Let me try.
After checking the code and experiment, I don't think that gives what we need. That flag skips
populate_vma_page_rangeonly, but we need to allocate pages in the inaccessible mapping and fill the pages afterwards.
Then either I don't understand what you're trying to do, or you don't understand what the inaccessible mapping is for. Is this just for speeding up mlock() as you suggested earlier, or are you genuinely trying to do something with the inaccessible mapping?
Quoted 32 lines · Matthew Wilcox
Quoted 31 lines · Jaegeuk Kim
Quoted 29 lines · Jaegeuk Kim
Quoted 20 lines · Matthew Wilcox
Quoted 11 lines · Jaegeuk Kim
Quoted 10 lines · Matthew Wilcox
Quoted 3 lines · Jaegeuk Kim
This patch introduces a new
POSIX_FADV_MLOCKwhich 1) invalidates the range of cached pages, 2) sets the mapping as inaccessible, 3)POSIX_FADV_WILLNEEDloads pages directly to the inaccessible mapping.... what?
This seems like something which is completely different from
mlock(). So it needs a different name.Quoted 7 lines
Quoted 2 lines
But I don't understand the point of this, whatever it's called. Need more information.
So, the sequence that I'd like to optimize is
mmap(MAP_POPULATE)followed bymlock(). For example,mmap()takes 1 second to load 4GB data, andmlock()takes 330ms additionally in order to migrate all the pages into inaccessible map, IIUC.Quoted 6 lines
Oh, so the MLOCK part is right, but the
inaccessible()part is wrong. Inaccessible is special weirdguest_memfdcrap that has all kinds of side-effects that you don't want.Wouldn't you get the same effect by calling
mlock2(MLOCK_ONFAULT)and then callingreadahead()for the desired range?Oh, thank you. Let me try.
Quoted 8 lines
Quoted 3 lines
After checking the code and experiment, I don't think that gives what we need. That flag skips
populate_vma_page_rangeonly, but we need to allocate pages in the inaccessible mapping and fill the pages afterwards.Then either I don't understand what you're trying to do, or you don't understand what the inaccessible mapping is for. Is this just for speeding up
mlock()as you suggested earlier, or are you genuinely trying to do something with the inaccessible mapping?
The latter. I'd like to propose a new read flow with the inaccessible mapping.
As-Is:
mmap() -> fadvise(fd, POSIX_FADV_WILLNEED) -> mlock()
1. fadvise() proposal
mmap() -> fadvise(fd, POSIX_FADV_MLOCK)
: all the pages will be loaded into inaccessible page cache directly
2. mlock2() proposal
mmap() -> mlock2(MLOCK_ONFAULT) -> madvise(MADV_POPULATE_READ)
If you mean #2, I need to find whether we can get the space for madvise, since we have only fd when reading the pages. And, also I need to find a way to handle the folio order directly instead of starging from 0 in madvise() path. Let me think about it.
Quoted 37 lines · Jaegeuk Kim
Quoted 36 lines · Matthew Wilcox
Quoted 34 lines · Jaegeuk Kim
Quoted 21 lines · Jaegeuk Kim
Quoted 20 lines · Matthew Wilcox
Quoted 18 lines · Jaegeuk Kim
Quoted 5 lines · Matthew Wilcox
Quoted 3 lines · Jaegeuk Kim
This patch introduces a new
POSIX_FADV_MLOCKwhich 1) invalidates the range of cached pages, 2) sets the mapping as inaccessible, 3)POSIX_FADV_WILLNEEDloads pages directly to the inaccessible mapping.Quoted 7 lines
... what?
This seems like something which is completely different from
mlock(). So it needs a different name.But I don't understand the point of this, whatever it's called. Need more information.
So, the sequence that I'd like to optimize is
mmap(MAP_POPULATE)followed bymlock(). For example,mmap()takes 1 second to load 4GB data, andmlock()takes 330ms additionally in order to migrate all the pages into inaccessible map, IIUC.Quoted 8 lines
Quoted 6 lines
Oh, so the MLOCK part is right, but the
inaccessible()part is wrong. Inaccessible is special weirdguest_memfdcrap that has all kinds of side-effects that you don't want.Wouldn't you get the same effect by calling
mlock2(MLOCK_ONFAULT)and then callingreadahead()for the desired range?Oh, thank you. Let me try.
After checking the code and experiment, I don't think that gives what we need. That flag skips
populate_vma_page_rangeonly, but we need to allocate pages in the inaccessible mapping and fill the pages afterwards.
Quoted 6 lines
Quoted 4 lines
Then either I don't understand what you're trying to do, or you don't understand what the inaccessible mapping is for. Is this just for speeding up
mlock()as you suggested earlier, or are you genuinely trying to do something with the inaccessible mapping?The latter. I'd like to propose a new read flow with the inaccessible mapping.
You REALLY REALLY REALLY need to explain what you're doing because this all sounds completely bogus.
The inaccessible mapping is something special that guest_memfd does. But here you are talking about it like it's some kind of normal filesystem thing.
So, from the top. What are you trying to accomplish? Starting from "We have application A. It wants to ..."
Quoted 14 lines
As-Is:
mmap()->fadvise(fd, POSIX_FADV_WILLNEED)->mlock()1.
fadvise()proposal
mmap()->fadvise(fd, POSIX_FADV_MLOCK)
: all the pages will be loaded into inaccessible page cache directly2.
mlock2()proposal
mmap()->mlock2(MLOCK_ONFAULT)->madvise(MADV_POPULATE_READ)If you mean #2, I need to find whether we can get the space for madvise, since we have only fd when reading the pages. And, also I need to find a way to handle the folio order directly instead of starging from 0 in
madvise()path. Let me think about it.