History

tmpfs: mem_cgroup charge fault to vm_mm not current mm

Although shmem_fault() has been careful to count a major fault to vm_mm, shmem_getpage_gfp() has been careless in charging a remote access fault to current->mm owner's memcg instead of to vma->vm_mm owner's memcg: that is inconsistent with all the mem_cgroup charging on remote access faults in mm/memory.c. Fix it by passing fault_mm along with fault_type to shmem_get_page_gfp(); but in that case, now knowing the right mm, it's better for it to handle the PGMAJFAULT updates itself. And let's keep this clutter out of most callers' way: change the common shmem_getpage() wrapper to hide fault_mm and fault_type as well as gfp.

Andres Lagar-Cavilla committed 10 years ago

Commit 9e18eb21 parent 75edd341 file · +34 −27Browse files

Unified · Split

mm/shmem.c+34−27
@@ -121,13 +121,14 @@ static bool shmem_should_replace_page(struct page *page, gfp_t gfp); · 120 unmodified lines
121121 static int shmem_replace_page(struct page **pagep, gfp_t gfp,
122122 struct shmem_inode_info *info, pgoff_t index);
123123 static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
124− struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
124+ struct page **pagep, enum sgp_type sgp,
125+ gfp_t gfp, struct mm_struct *fault_mm, int *fault_type);
125126
126127 static inline int shmem_getpage(struct inode *inode, pgoff_t index,
127− struct page **pagep, enum sgp_type sgp, int *fault_type)
128+ struct page **pagep, enum sgp_type sgp)
128129 {
129130 return shmem_getpage_gfp(inode, index, pagep, sgp,
130− mapping_gfp_mask(inode->i_mapping), fault_type);
131+ mapping_gfp_mask(inode->i_mapping), NULL, NULL);
131132 }
132133
133134 static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
@@ -527,7 +528,7 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, · 393 unmodified lines
527528
528529 if (partial_start) {
529530 struct page *page = NULL;
530− shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
531+ shmem_getpage(inode, start - 1, &page, SGP_READ);
531532 if (page) {
532533 unsigned int top = PAGE_SIZE;
533534 if (start > end) {
@@ -542,7 +543,7 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, · 8 unmodified lines
542543 }
543544 if (partial_end) {
544545 struct page *page = NULL;
545− shmem_getpage(inode, end, &page, SGP_READ, NULL);
546+ shmem_getpage(inode, end, &page, SGP_READ);
546547 if (page) {
547548 zero_user_segment(page, 0, partial_end);
548549 set_page_dirty(page);
@@ -1115,14 +1116,19 @@ static int shmem_replace_page(struct page **pagep, gfp_t gfp, · 566 unmodified lines
11151116 *
11161117 * If we allocate a new one we do not mark it dirty. That's up to the
11171118 * vm. If we swap it in we mark it dirty since we also free the swap
1118− * entry since a page cannot live in both the swap and page cache
1119+ * entry since a page cannot live in both the swap and page cache.
1120+ *
1121+ * fault_mm and fault_type are only supplied by shmem_fault:
1122+ * otherwise they are NULL.
11191123 */
11201124 static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
1121− struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
1125+ struct page **pagep, enum sgp_type sgp, gfp_t gfp,
1126+ struct mm_struct *fault_mm, int *fault_type)
11221127 {
11231128 struct address_space *mapping = inode->i_mapping;
11241129 struct shmem_inode_info *info;
11251130 struct shmem_sb_info *sbinfo;
1131+ struct mm_struct *charge_mm;
11261132 struct mem_cgroup *memcg;
11271133 struct page *page;
11281134 swp_entry_t swap;
@@ -1168,14 +1174,19 @@ repeat: · 39 unmodified lines
11681174 */
11691175 info = SHMEM_I(inode);
11701176 sbinfo = SHMEM_SB(inode->i_sb);
1177+ charge_mm = fault_mm ? : current->mm;
11711178
11721179 if (swap.val) {
11731180 /* Look it up and read it in.. */
11741181 page = lookup_swap_cache(swap);
11751182 if (!page) {
1176− /* here we actually do the io */
1177− if (fault_type)
1183+ /* Or update major stats only when swapin succeeds?? */
1184+ if (fault_type) {
11781185 *fault_type |= VM_FAULT_MAJOR;
1186+ count_vm_event(PGMAJFAULT);
1187+ mem_cgroup_count_vm_event(fault_mm, PGMAJFAULT);
1188+ }
1189+ /* Here we actually start the io */
11791190 page = shmem_swapin(swap, gfp, info, index);
11801191 if (!page) {
11811192 error = -ENOMEM;
@@ -1202,7 +1213,7 @@ repeat: · 20 unmodified lines
12021213 goto failed;
12031214 }
12041215
1205− error = mem_cgroup_try_charge(page, current->mm, gfp, &memcg,
1216+ error = mem_cgroup_try_charge(page, charge_mm, gfp, &memcg,
12061217 false);
12071218 if (!error) {
12081219 error = shmem_add_to_page_cache(page, mapping, index,
@@ -1263,7 +1274,7 @@ repeat: · 54 unmodified lines
12631274 if (sgp == SGP_WRITE)
12641275 __SetPageReferenced(page);
12651276
1266− error = mem_cgroup_try_charge(page, current->mm, gfp, &memcg,
1277+ error = mem_cgroup_try_charge(page, charge_mm, gfp, &memcg,
12671278 false);
12681279 if (error)
12691280 goto decused;
@@ -1352,6 +1363,7 @@ unlock: · 82 unmodified lines
13521363 static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
13531364 {
13541365 struct inode *inode = file_inode(vma->vm_file);
1366+ gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
13551367 int error;
13561368 int ret = VM_FAULT_LOCKED;
13571369
@@ -1413,14 +1425,10 @@ static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) · 55 unmodified lines
14131425 spin_unlock(&inode->i_lock);
14141426 }
14151427
1416− error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1428+ error = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, SGP_CACHE,
1429+ gfp, vma->vm_mm, &ret);
14171430 if (error)
14181431 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
1419−
1420− if (ret & VM_FAULT_MAJOR) {
1421− count_vm_event(PGMAJFAULT);
1422− mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1423− }
14241432 return ret;
14251433 }
14261434
@@ -1567,7 +1575,7 @@ shmem_write_begin(struct file *file, struct address_space *mapping, · 140 unmodified lines
15671575 return -EPERM;
15681576 }
15691577
1570− return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1578+ return shmem_getpage(inode, index, pagep, SGP_WRITE);
15711579 }
15721580
15731581 static int
@@ -1633,7 +1641,7 @@ static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) · 59 unmodified lines
16331641 break;
16341642 }
16351643
1636− error = shmem_getpage(inode, index, &page, sgp, NULL);
1644+ error = shmem_getpage(inode, index, &page, sgp);
16371645 if (error) {
16381646 if (error == -EINVAL)
16391647 error = 0;
@@ -1749,7 +1757,7 @@ static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, · 109 unmodified lines
17491757 error = 0;
17501758
17511759 while (spd.nr_pages < nr_pages) {
1752− error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1760+ error = shmem_getpage(inode, index, &page, SGP_CACHE);
17531761 if (error)
17541762 break;
17551763 unlock_page(page);
@@ -1771,8 +1779,7 @@ static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, · 15 unmodified lines
17711779 page = spd.pages[page_nr];
17721780
17731781 if (!PageUptodate(page) || page->mapping != mapping) {
1774− error = shmem_getpage(inode, index, &page,
1775− SGP_CACHE, NULL);
1782+ error = shmem_getpage(inode, index, &page, SGP_CACHE);
17761783 if (error)
17771784 break;
17781785 unlock_page(page);
@@ -2215,8 +2222,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset, · 436 unmodified lines
22152222 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
22162223 error = -ENOMEM;
22172224 else
2218− error = shmem_getpage(inode, index, &page, SGP_FALLOC,
2219− NULL);
2225+ error = shmem_getpage(inode, index, &page, SGP_FALLOC);
22202226 if (error) {
22212227 /* Remove the !PageUptodate pages we added */
22222228 shmem_undo_range(inode,
@@ -2534,7 +2540,7 @@ static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *s · 311 unmodified lines
25342540 inode->i_op = &shmem_short_symlink_operations;
25352541 } else {
25362542 inode_nohighmem(inode);
2537− error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
2543+ error = shmem_getpage(inode, 0, &page, SGP_WRITE);
25382544 if (error) {
25392545 iput(inode);
25402546 return error;
@@ -2575,7 +2581,7 @@ static const char *shmem_get_link(struct dentry *dentry, · 34 unmodified lines
25752581 return ERR_PTR(-ECHILD);
25762582 }
25772583 } else {
2578− error = shmem_getpage(inode, 0, &page, SGP_READ, NULL);
2584+ error = shmem_getpage(inode, 0, &page, SGP_READ);
25792585 if (error)
25802586 return ERR_PTR(error);
25812587 unlock_page(page);
@@ -3479,7 +3485,8 @@ struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, · 897 unmodified lines
34793485 int error;
34803486
34813487 BUG_ON(mapping->a_ops != &shmem_aops);
3482− error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
3488+ error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE,
3489+ gfp, NULL, NULL);
34833490 if (error)
34843491 page = ERR_PTR(error);
34853492 else

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