History

mm,thp: avoid writes to file with THP in pagecache

In previous patch, an application could put part of its text section in THP via madvise(). These THPs will be protected from writes when the application is still running (TXTBSY). However, after the application exits, the file is available for writes. This patch avoids writes to file THP by dropping page cache for the file when the file is open for write. A new counter nr_thps is added to struct address_space. In do_dentry_open(), if the file is open for write and nr_thps is non-zero, we drop page cache for the whole file.

Song Liu committed 7 years ago

Commit 09d91cd1 parent 99cb0db5 files · +47 −1Browse files

Unified · Split

5 files
  1. fs/inode.c +3 −0
  2. fs/open.c +8 −0
  3. include/linux/fs.h +32 −0
  4. mm/filemap.c +1 −0
  5. mm/khugepaged.c +3 −1
fs/inode.c+3−0
@@ -181,6 +181,9 @@ int inode_init_always(struct super_block *sb, struct inode *inode) · 180 unmodified lines
181 mapping->flags = 0;181 mapping->flags = 0;
182 mapping->wb_err = 0;182 mapping->wb_err = 0;
183 atomic_set(&mapping->i_mmap_writable, 0);183 atomic_set(&mapping->i_mmap_writable, 0);
184#ifdef CONFIG_READ_ONLY_THP_FOR_FS
185 atomic_set(&mapping->nr_thps, 0);
186#endif
184 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);187 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
185 mapping->private_data = NULL;188 mapping->private_data = NULL;
186 mapping->writeback_index = 0;189 mapping->writeback_index = 0;
fs/open.c+8−0
@@ -818,6 +818,14 @@ static int do_dentry_open(struct file *f, · 817 unmodified lines
818 if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO)818 if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO)
819 return -EINVAL;819 return -EINVAL;
820 }820 }
821
822 /*
823 * XXX: Huge page cache doesn't support writing yet. Drop all page
824 * cache for this file before processing writes.
825 */
826 if ((f->f_mode & FMODE_WRITE) && filemap_nr_thps(inode->i_mapping))
827 truncate_pagecache(inode, 0);
828
821 return 0;829 return 0;
822 830
823cleanup_all:831cleanup_all:
include/linux/fs.h+32−0
@@ -429,6 +429,7 @@ int pagecache_write_end(struct file *, struct address_space *mapping, · 428 unmodified lines
429 * @i_pages: Cached pages.429 * @i_pages: Cached pages.
430 * @gfp_mask: Memory allocation flags to use for allocating pages.430 * @gfp_mask: Memory allocation flags to use for allocating pages.
431 * @i_mmap_writable: Number of VM_SHARED mappings.431 * @i_mmap_writable: Number of VM_SHARED mappings.
432 * @nr_thps: Number of THPs in the pagecache (non-shmem only).
432 * @i_mmap: Tree of private and shared mappings.433 * @i_mmap: Tree of private and shared mappings.
433 * @i_mmap_rwsem: Protects @i_mmap and @i_mmap_writable.434 * @i_mmap_rwsem: Protects @i_mmap and @i_mmap_writable.
434 * @nrpages: Number of page entries, protected by the i_pages lock.435 * @nrpages: Number of page entries, protected by the i_pages lock.
@@ -446,6 +447,10 @@ struct address_space { · 11 unmodified lines
446 struct xarray i_pages;447 struct xarray i_pages;
447 gfp_t gfp_mask;448 gfp_t gfp_mask;
448 atomic_t i_mmap_writable;449 atomic_t i_mmap_writable;
450#ifdef CONFIG_READ_ONLY_THP_FOR_FS
451 /* number of thp, only for non-shmem files */
452 atomic_t nr_thps;
453#endif
449 struct rb_root_cached i_mmap;454 struct rb_root_cached i_mmap;
450 struct rw_semaphore i_mmap_rwsem;455 struct rw_semaphore i_mmap_rwsem;
451 unsigned long nrpages;456 unsigned long nrpages;
@@ -2798,6 +2803,33 @@ static inline errseq_t filemap_sample_wb_err(struct address_space *mapping) · 2346 unmodified lines
2798 return errseq_sample(&mapping->wb_err);2803 return errseq_sample(&mapping->wb_err);
2799}2804}
2800 2805
2806static inline int filemap_nr_thps(struct address_space *mapping)
2807{
2808#ifdef CONFIG_READ_ONLY_THP_FOR_FS
2809 return atomic_read(&mapping->nr_thps);
2810#else
2811 return 0;
2812#endif
2813}
2814
2815static inline void filemap_nr_thps_inc(struct address_space *mapping)
2816{
2817#ifdef CONFIG_READ_ONLY_THP_FOR_FS
2818 atomic_inc(&mapping->nr_thps);
2819#else
2820 WARN_ON_ONCE(1);
2821#endif
2822}
2823
2824static inline void filemap_nr_thps_dec(struct address_space *mapping)
2825{
2826#ifdef CONFIG_READ_ONLY_THP_FOR_FS
2827 atomic_dec(&mapping->nr_thps);
2828#else
2829 WARN_ON_ONCE(1);
2830#endif
2831}
2832
2801extern int vfs_fsync_range(struct file *file, loff_t start, loff_t end,2833extern int vfs_fsync_range(struct file *file, loff_t start, loff_t end,
2802 int datasync);2834 int datasync);
2803extern int vfs_fsync(struct file *file, int datasync);2835extern int vfs_fsync(struct file *file, int datasync);
mm/filemap.c+1−0
@@ -205,6 +205,7 @@ static void unaccount_page_cache_page(struct address_space *mapping, · 204 unmodified lines
205 __dec_node_page_state(page, NR_SHMEM_THPS);205 __dec_node_page_state(page, NR_SHMEM_THPS);
206 } else if (PageTransHuge(page)) {206 } else if (PageTransHuge(page)) {
207 __dec_node_page_state(page, NR_FILE_THPS);207 __dec_node_page_state(page, NR_FILE_THPS);
208 filemap_nr_thps_dec(mapping);
208 }209 }
209 210
210 /*211 /*
mm/khugepaged.c+3−1
@@ -1514,8 +1514,10 @@ out_unlock: · 1513 unmodified lines
1514 1514
1515 if (is_shmem)1515 if (is_shmem)
1516 __inc_node_page_state(new_page, NR_SHMEM_THPS);1516 __inc_node_page_state(new_page, NR_SHMEM_THPS);
1517 else1517 else {
1518 __inc_node_page_state(new_page, NR_FILE_THPS);1518 __inc_node_page_state(new_page, NR_FILE_THPS);
1519 filemap_nr_thps_inc(mapping);
1520 }
1519 1521
1520 if (nr_none) {1522 if (nr_none) {
1521 struct zone *zone = page_zone(new_page);1523 struct zone *zone = page_zone(new_page);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t