Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1238234 > unrolled thread
| Started by | Vlastimil Babka <vbabka@suse.cz> |
|---|---|
| First post | 2015-10-02 15:40 +0200 |
| Last post | 2015-10-05 10:00 +0200 |
| Articles | 7 — 6 participants |
Back to article view | Back to linux.kernel
[PATCH v4 0/4] enhance shmem process and swap accounting Vlastimil Babka <vbabka@suse.cz> - 2015-10-02 15:40 +0200
[PATCH v4 2/4] mm, proc: account for shmem swap in /proc/pid/smaps Vlastimil Babka <vbabka@suse.cz> - 2015-10-02 15:40 +0200
Re: [PATCH v4 2/4] mm, proc: account for shmem swap in /proc/pid/smaps Jerome Marchand <jmarchan@redhat.com> - 2015-10-02 17:10 +0200
Re: [PATCH v4 2/4] mm, proc: account for shmem swap in /proc/pid/smaps Michal Hocko <mhocko@kernel.org> - 2015-10-02 17:30 +0200
Re: [PATCH v4 2/4] mm, proc: account for shmem swap in /proc/pid/smaps Andrew Morton <akpm@linux-foundation.org> - 2015-10-03 00:40 +0200
Re: [PATCH v4 2/4] mm, proc: account for shmem swap in /proc/pid/smaps Hugh Dickins <hughd@google.com> - 2015-10-05 05:10 +0200
Re: [PATCH v4 2/4] mm, proc: account for shmem swap in /proc/pid/smaps Peter Zijlstra <peterz@infradead.org> - 2015-10-05 10:00 +0200
| From | Vlastimil Babka <vbabka@suse.cz> |
|---|---|
| Date | 2015-10-02 15:40 +0200 |
| Subject | [PATCH v4 0/4] enhance shmem process and swap accounting |
| Message-ID | <qf8J4-4MS-19@gated-at.bofh.it> |
Hugh, you commented on the earlier patches from Jerome, can you take a look
please so we can have this merged? Especially at the changes to shmem.c
in Patch 2 and if they are indeed safe without extra locking. Thanks!
Changes since v3:
o Rebase on next-20151002
o Apply (feedb)acks from Michal Hocko and Konstantin Khlebnikov (Thanks!)
- drop CONFIG_SHMEM ifdefs, as it was the 2nd suggestion already
- add comments about not taking i_mutex in patch 2
o Rename VmAnon/VmFile/VmShm to RssAnon/RssFile... to make it hopefully more
obvious that it's a breakdown of VmRSS. Naming things sucks.
Changes since v2:
o Rebase on next-20150805.
o This means that /proc/pid/maps has the proportional swap share (SwapPss:)
field as per https://lkml.org/lkml/2015/6/15/274
It's not clear what to do with shmem here so it's 0 for now.
- swapped out shmem doesn't have swap entries, so we would have to look at who
else has the shmem object (partially) mapped
- to be more precise we should also check if his range actually includes
the offset in question, which could get rather involved
- or is there some easy way I don't see?
o Konstantin suggested for patch 3/4 that I drop the CONFIG_SHMEM #ifdefs
I didn't see the point in going against tinyfication when the work is
already done, but I can do that if more people think it's better and it
would block the series.
Changes since v1:
o In Patch 2, rely on SHMEM_I(inode)->swapped if possible, and fallback to
radix tree iterator on partially mapped shmem objects, i.e. decouple shmem
swap usage determination from the page walk, for performance reasons.
Thanks to Jerome and Konstantin for the tips.
The downside is that mm/shmem.c had to be touched.
This series is based on Jerome Marchand's [1] so let me quote the first
paragraph from there:
There are several shortcomings with the accounting of shared memory
(sysV shm, shared anonymous mapping, mapping to a tmpfs file). The
values in /proc/<pid>/status and statm don't allow to distinguish
between shmem memory and a shared mapping to a regular file, even
though theirs implication on memory usage are quite different: at
reclaim, file mapping can be dropped or write back on disk while shmem
needs a place in swap. As for shmem pages that are swapped-out or in
swap cache, they aren't accounted at all.
The original motivation for myself is that a customer found (IMHO rightfully)
confusing that e.g. top output for process swap usage is unreliable with
respect to swapped out shmem pages, which are not accounted for.
The fundamental difference between private anonymous and shmem pages is that
the latter has PTE's converted to pte_none, and not swapents. As such, they are
not accounted to the number of swapents visible e.g. in /proc/pid/status VmSwap
row. It might be theoretically possible to use swapents when swapping out shmem
(without extra cost, as one has to change all mappers anyway), and on swap in
only convert the swapent for the faulting process, leaving swapents in other
processes until they also fault (so again no extra cost). But I don't know how
many assumptions this would break, and it would be too disruptive change for a
relatively small benefit.
Instead, my approach is to document the limitation of VmSwap, and provide means
to determine the swap usage for shmem areas for those who are interested and
willing to pay the price, using /proc/pid/smaps. Because outside of ipcs, I
don't think it's possible to currently to determine the usage at all. The
previous patchset [1] did introduce new shmem-specific fields into smaps
output, and functions to determine the values. I take a simpler approach,
noting that smaps output already has a "Swap: X kB" line, where currently X ==
0 always for shmem areas. I think we can just consider this a bug and provide
the proper value by consulting the radix tree, as e.g. mincore_page() does. In the
patch changelog I explain why this is also not perfect (and cannot be without
swapents), but still arguably much better than showing a 0.
The last two patches are adapted from Jerome's patchset and provide a VmRSS
breakdown to VmAnon, VmFile and VmShm in /proc/pid/status. Hugh noted that
this is a welcome addition, and I agree that it might help e.g. debugging
process memory usage at albeit non-zero, but still rather low cost of extra
per-mm counter and some page flag checks. I updated these patches to 4.0-rc1,
made them respect !CONFIG_SHMEM so that tiny systems don't pay the cost, and
optimized the page flag checking somewhat.
[1] http://lwn.net/Articles/611966/
Jerome Marchand (2):
mm, shmem: Add shmem resident memory accounting
mm, procfs: Display VmAnon, VmFile and VmShm in /proc/pid/status
Vlastimil Babka (2):
mm, documentation: clarify /proc/pid/status VmSwap limitations
mm, proc: account for shmem swap in /proc/pid/smaps
Documentation/filesystems/proc.txt | 18 +++++++++--
arch/s390/mm/pgtable.c | 5 +--
fs/proc/task_mmu.c | 65 ++++++++++++++++++++++++++++++++++++--
include/linux/mm.h | 18 ++++++++++-
include/linux/mm_types.h | 7 ++--
include/linux/shmem_fs.h | 6 ++++
kernel/events/uprobes.c | 2 +-
mm/memory.c | 30 ++++++------------
mm/oom_kill.c | 5 +--
mm/rmap.c | 12 ++-----
mm/shmem.c | 61 +++++++++++++++++++++++++++++++++++
11 files changed, 183 insertions(+), 46 deletions(-)
--
2.5.2
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
[toc] | [next] | [standalone]
| From | Vlastimil Babka <vbabka@suse.cz> |
|---|---|
| Date | 2015-10-02 15:40 +0200 |
| Subject | [PATCH v4 2/4] mm, proc: account for shmem swap in /proc/pid/smaps |
| Message-ID | <qf8J6-4MS-65@gated-at.bofh.it> |
| In reply to | #1238234 |
Currently, /proc/pid/smaps will always show "Swap: 0 kB" for shmem-backed
mappings, even if the mapped portion does contain pages that were swapped out.
This is because unlike private anonymous mappings, shmem does not change pte
to swap entry, but pte_none when swapping the page out. In the smaps page
walk, such page thus looks like it was never faulted in.
This patch changes smaps_pte_entry() to determine the swap status for such
pte_none entries for shmem mappings, similarly to how mincore_page() does it.
Swapped out pages are thus accounted for.
The accounting is arguably still not as precise as for private anonymous
mappings, since now we will count also pages that the process in question never
accessed, but only another process populated them and then let them become
swapped out. I believe it is still less confusing and subtle than not showing
any swap usage by shmem mappings at all. Also, swapped out pages only becomee a
performance issue for future accesses, and we cannot predict those for neither
kind of mapping.
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Acked-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
---
Documentation/filesystems/proc.txt | 6 ++--
fs/proc/task_mmu.c | 48 ++++++++++++++++++++++++++++++
include/linux/shmem_fs.h | 6 ++++
mm/shmem.c | 61 ++++++++++++++++++++++++++++++++++++++
4 files changed, 119 insertions(+), 2 deletions(-)
diff --git a/Documentation/filesystems/proc.txt b/Documentation/filesystems/proc.txt
index 7ef50cb..82d3657 100644
--- a/Documentation/filesystems/proc.txt
+++ b/Documentation/filesystems/proc.txt
@@ -457,8 +457,10 @@ accessed.
a mapping associated with a file may contain anonymous pages: when MAP_PRIVATE
and a page is modified, the file page is replaced by a private anonymous copy.
"Swap" shows how much would-be-anonymous memory is also used, but out on
-swap.
-"SwapPss" shows proportional swap share of this mapping.
+swap. For shmem mappings, "Swap" shows how much of the mapped portion of the
+underlying shmem object is on swap.
+"SwapPss" shows proportional swap share of this mapping. Shmem mappings will
+currently show 0 here.
"AnonHugePages" shows the ammount of memory backed by transparent hugepage.
"Shared_Hugetlb" and "Private_Hugetlb" show the ammounts of memory backed by
hugetlbfs page which is *not* counted in "RSS" or "PSS" field for historical
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 04999b2..103457c 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -14,6 +14,7 @@
#include <linux/swapops.h>
#include <linux/mmu_notifier.h>
#include <linux/page_idle.h>
+#include <linux/shmem_fs.h>
#include <asm/elf.h>
#include <asm/uaccess.h>
@@ -657,6 +658,51 @@ static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
}
#endif /* HUGETLB_PAGE */
+#ifdef CONFIG_SHMEM
+static unsigned long smaps_shmem_swap(struct vm_area_struct *vma)
+{
+ struct inode *inode;
+ unsigned long swapped;
+ pgoff_t start, end;
+
+ if (!vma->vm_file)
+ return 0;
+
+ inode = file_inode(vma->vm_file);
+
+ if (!shmem_mapping(inode->i_mapping))
+ return 0;
+
+ /*
+ * The easier cases are when the shmem object has nothing in swap, or
+ * we have the whole object mapped. Then we can simply use the stats
+ * that are already tracked by shmem.
+ */
+ swapped = shmem_swap_usage(inode);
+
+ if (swapped == 0)
+ return 0;
+
+ if (vma->vm_end - vma->vm_start >= inode->i_size)
+ return swapped;
+
+ /*
+ * Here we have to inspect individual pages in our mapped range to
+ * determine how much of them are swapped out. Thanks to RCU, we don't
+ * need i_mutex to protect against truncating or hole punching.
+ */
+ start = linear_page_index(vma, vma->vm_start);
+ end = linear_page_index(vma, vma->vm_end);
+
+ return shmem_partial_swap_usage(inode->i_mapping, start, end);
+}
+#else
+static unsigned long smaps_shmem_swap(struct vm_area_struct *vma)
+{
+ return 0;
+}
+#endif
+
static int show_smap(struct seq_file *m, void *v, int is_pid)
{
struct vm_area_struct *vma = v;
@@ -674,6 +720,8 @@ static int show_smap(struct seq_file *m, void *v, int is_pid)
/* mmap_sem is held in m_start */
walk_page_vma(vma, &smaps_walk);
+ mss.swap += smaps_shmem_swap(vma);
+
show_map_vma(m, vma, is_pid);
seq_printf(m,
diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h
index 50777b5..12519e4 100644
--- a/include/linux/shmem_fs.h
+++ b/include/linux/shmem_fs.h
@@ -60,6 +60,12 @@ extern struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
extern void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end);
extern int shmem_unuse(swp_entry_t entry, struct page *page);
+#ifdef CONFIG_SWAP
+extern unsigned long shmem_swap_usage(struct inode *inode);
+extern unsigned long shmem_partial_swap_usage(struct address_space *mapping,
+ pgoff_t start, pgoff_t end);
+#endif
+
static inline struct page *shmem_read_mapping_page(
struct address_space *mapping, pgoff_t index)
{
diff --git a/mm/shmem.c b/mm/shmem.c
index b543cc7..b0e9e30 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -360,6 +360,67 @@ static int shmem_free_swap(struct address_space *mapping,
}
/*
+ * Determine (in bytes) how much of the whole shmem object is swapped out.
+ */
+unsigned long shmem_swap_usage(struct inode *inode)
+{
+ struct shmem_inode_info *info = SHMEM_I(inode);
+ unsigned long swapped;
+
+ /* Mostly an overkill, but it's not atomic64_t */
+ spin_lock(&info->lock);
+ swapped = info->swapped;
+ spin_unlock(&info->lock);
+
+ return swapped << PAGE_SHIFT;
+}
+
+/*
+ * Determine (in bytes) how many pages within the given range are swapped out.
+ *
+ * Can be called without i_mutex or mapping->tree_lock thanks to RCU.
+ */
+unsigned long shmem_partial_swap_usage(struct address_space *mapping,
+ pgoff_t start, pgoff_t end)
+{
+ struct radix_tree_iter iter;
+ void **slot;
+ struct page *page;
+ unsigned long swapped = 0;
+
+ rcu_read_lock();
+
+restart:
+ radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
+ if (iter.index >= end)
+ break;
+
+ page = radix_tree_deref_slot(slot);
+
+ /*
+ * This should only be possible to happen at index 0, so we
+ * don't need to reset the counter, nor do we risk infinite
+ * restarts.
+ */
+ if (radix_tree_deref_retry(page))
+ goto restart;
+
+ if (radix_tree_exceptional_entry(page))
+ swapped++;
+
+ if (need_resched()) {
+ cond_resched_rcu();
+ start = iter.index + 1;
+ goto restart;
+ }
+ }
+
+ rcu_read_unlock();
+
+ return swapped << PAGE_SHIFT;
+}
+
+/*
* SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
*/
void shmem_unlock_mapping(struct address_space *mapping)
--
2.5.2
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
[toc] | [prev] | [next] | [standalone]
| From | Jerome Marchand <jmarchan@redhat.com> |
|---|---|
| Date | 2015-10-02 17:10 +0200 |
| Subject | Re: [PATCH v4 2/4] mm, proc: account for shmem swap in /proc/pid/smaps |
| Message-ID | <qfa8a-6Wx-11@gated-at.bofh.it> |
| In reply to | #1238237 |
[Multipart message — attachments visible in raw view] — view raw
On 10/02/2015 03:35 PM, Vlastimil Babka wrote: > Currently, /proc/pid/smaps will always show "Swap: 0 kB" for shmem-backed > mappings, even if the mapped portion does contain pages that were swapped out. > This is because unlike private anonymous mappings, shmem does not change pte > to swap entry, but pte_none when swapping the page out. In the smaps page > walk, such page thus looks like it was never faulted in. > > This patch changes smaps_pte_entry() to determine the swap status for such > pte_none entries for shmem mappings, similarly to how mincore_page() does it. > Swapped out pages are thus accounted for. > > The accounting is arguably still not as precise as for private anonymous > mappings, since now we will count also pages that the process in question never > accessed, but only another process populated them and then let them become > swapped out. I believe it is still less confusing and subtle than not showing > any swap usage by shmem mappings at all. Also, swapped out pages only becomee a > performance issue for future accesses, and we cannot predict those for neither > kind of mapping. Agreed, this is much better than the current situation. I don't think there is such a thing as a perfect accounting of shared pages anyway. > > Signed-off-by: Vlastimil Babka <vbabka@suse.cz> > Acked-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru> Acked-by: Jerome Marchand <jmarchan@redhat.com>
[toc] | [prev] | [next] | [standalone]
| From | Michal Hocko <mhocko@kernel.org> |
|---|---|
| Date | 2015-10-02 17:30 +0200 |
| Subject | Re: [PATCH v4 2/4] mm, proc: account for shmem swap in /proc/pid/smaps |
| Message-ID | <qfarw-7j6-17@gated-at.bofh.it> |
| In reply to | #1238237 |
On Fri 02-10-15 15:35:49, Vlastimil Babka wrote: > Currently, /proc/pid/smaps will always show "Swap: 0 kB" for shmem-backed > mappings, even if the mapped portion does contain pages that were swapped out. > This is because unlike private anonymous mappings, shmem does not change pte > to swap entry, but pte_none when swapping the page out. In the smaps page > walk, such page thus looks like it was never faulted in. > > This patch changes smaps_pte_entry() to determine the swap status for such > pte_none entries for shmem mappings, similarly to how mincore_page() does it. > Swapped out pages are thus accounted for. > > The accounting is arguably still not as precise as for private anonymous > mappings, since now we will count also pages that the process in question never > accessed, but only another process populated them and then let them become > swapped out. I believe it is still less confusing and subtle than not showing > any swap usage by shmem mappings at all. Also, swapped out pages only becomee a > performance issue for future accesses, and we cannot predict those for neither > kind of mapping. > > Signed-off-by: Vlastimil Babka <vbabka@suse.cz> > Acked-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru> Acked-by: Michal Hocko <mhocko@suse.com> But I think comments explaining why i_mutex is not needed are confusing and incomplete. [...] > + /* > + * Here we have to inspect individual pages in our mapped range to > + * determine how much of them are swapped out. Thanks to RCU, we don't > + * need i_mutex to protect against truncating or hole punching. > + */ > + start = linear_page_index(vma, vma->vm_start); > + end = linear_page_index(vma, vma->vm_end); > + > + return shmem_partial_swap_usage(inode->i_mapping, start, end); [...] > +/* > + * Determine (in bytes) how many pages within the given range are swapped out. > + * > + * Can be called without i_mutex or mapping->tree_lock thanks to RCU. > + */ > +unsigned long shmem_partial_swap_usage(struct address_space *mapping, > + pgoff_t start, pgoff_t end) AFAIU RCU only helps to prevent from accessing nodes which were freed from the radix tree. The reason why we do not need to hold i_mutex is that the radix tree iterator would break out of the loop if we entered node which backed truncated range. At least this is my understanding, I might be wrong here of course. -- Michal Hocko SUSE Labs -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
[toc] | [prev] | [next] | [standalone]
| From | Andrew Morton <akpm@linux-foundation.org> |
|---|---|
| Date | 2015-10-03 00:40 +0200 |
| Subject | Re: [PATCH v4 2/4] mm, proc: account for shmem swap in /proc/pid/smaps |
| Message-ID | <qfh9E-8tG-15@gated-at.bofh.it> |
| In reply to | #1238237 |
On Fri, 2 Oct 2015 15:35:49 +0200 Vlastimil Babka <vbabka@suse.cz> wrote: > Currently, /proc/pid/smaps will always show "Swap: 0 kB" for shmem-backed > mappings, even if the mapped portion does contain pages that were swapped out. > This is because unlike private anonymous mappings, shmem does not change pte > to swap entry, but pte_none when swapping the page out. In the smaps page > walk, such page thus looks like it was never faulted in. > > This patch changes smaps_pte_entry() to determine the swap status for such > pte_none entries for shmem mappings, similarly to how mincore_page() does it. > Swapped out pages are thus accounted for. > > The accounting is arguably still not as precise as for private anonymous > mappings, since now we will count also pages that the process in question never > accessed, but only another process populated them and then let them become > swapped out. I believe it is still less confusing and subtle than not showing > any swap usage by shmem mappings at all. Also, swapped out pages only becomee a > performance issue for future accesses, and we cannot predict those for neither > kind of mapping. > > ... > > --- a/include/linux/shmem_fs.h > +++ b/include/linux/shmem_fs.h > @@ -60,6 +60,12 @@ extern struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, > extern void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end); > extern int shmem_unuse(swp_entry_t entry, struct page *page); > > +#ifdef CONFIG_SWAP > +extern unsigned long shmem_swap_usage(struct inode *inode); > +extern unsigned long shmem_partial_swap_usage(struct address_space *mapping, > + pgoff_t start, pgoff_t end); > +#endif CONFIG_SWAP is wrong, isn't it? It should be CONFIG_SHMEM if anything. I'd just do --- a/include/linux/shmem_fs.h~mm-proc-account-for-shmem-swap-in-proc-pid-smaps-fix +++ a/include/linux/shmem_fs.h @@ -60,11 +60,9 @@ extern struct page *shmem_read_mapping_p extern void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end); extern int shmem_unuse(swp_entry_t entry, struct page *page); -#ifdef CONFIG_SWAP extern unsigned long shmem_swap_usage(struct inode *inode); extern unsigned long shmem_partial_swap_usage(struct address_space *mapping, pgoff_t start, pgoff_t end); -#endif static inline struct page *shmem_read_mapping_page( struct address_space *mapping, pgoff_t index) We don't need the ifdefs around declarations and they're a pain to maintain and they'd add a *ton* of clutter if we even tried to do this for real. -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
[toc] | [prev] | [next] | [standalone]
| From | Hugh Dickins <hughd@google.com> |
|---|---|
| Date | 2015-10-05 05:10 +0200 |
| Subject | Re: [PATCH v4 2/4] mm, proc: account for shmem swap in /proc/pid/smaps |
| Message-ID | <qg4k1-3aj-3@gated-at.bofh.it> |
| In reply to | #1238237 |
On Fri, 2 Oct 2015, Vlastimil Babka wrote:
> Currently, /proc/pid/smaps will always show "Swap: 0 kB" for shmem-backed
> mappings, even if the mapped portion does contain pages that were swapped out.
> This is because unlike private anonymous mappings, shmem does not change pte
> to swap entry, but pte_none when swapping the page out. In the smaps page
> walk, such page thus looks like it was never faulted in.
>
> This patch changes smaps_pte_entry() to determine the swap status for such
> pte_none entries for shmem mappings, similarly to how mincore_page() does it.
> Swapped out pages are thus accounted for.
>
> The accounting is arguably still not as precise as for private anonymous
> mappings, since now we will count also pages that the process in question never
> accessed, but only another process populated them and then let them become
> swapped out. I believe it is still less confusing and subtle than not showing
> any swap usage by shmem mappings at all. Also, swapped out pages only becomee a
> performance issue for future accesses, and we cannot predict those for neither
> kind of mapping.
>
> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
> Acked-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
Neither Ack nor Nack from me.
I don't want to stand in the way of this patch, if you and others
believe that it will help to diagnose problems in the field better
than what's shown at present; but to me it looks dangerously like
replacing no information by wrong information.
As you acknowledge in the commit message, if a file of 100 pages
were copied to tmpfs, and 100 tasks map its full extent, but they
all mess around with the first 50 pages and take no interest in
the last 50, then it's quite likely that that last 50 will get
swapped out; then with your patch, 100 tasks are each shown as
using 50 pages of swap, when none of them are actually using any.
It is rather as if we didn't bother to record Rss, and just put
Size in there instead: you are (for understandable reasons) treating
the virtual address space as if every page of it had been touched.
But I accept that there may well be a class of processes and problems
which would be better served by this fiction than the present: I expect
you have much more experience of helping out in such situations than I.
And perhaps you do balance it nicely by going to the opposite extreme
with SwapPss 0 for all (again for the eminently understandable reason,
that it would be a whole lot more new code to work out the right number).
Altogther, you're saying everyone's using more swap than they probably
are, but that's okay because it's infinitely shared.
I am not at all angling for you or anyone to make the changes necessary
to make those numbers accurate. I think there's a point at which we
stop cluttering up the core kernel code, just for the sake of
maintaining numbers for a /proc file someone thought was a good idea
at the time. But I am hoping that if this patch goes in, you will take
responsibility for batting away all the complaints that it doesn't work
as this or that person expected, rather than a long stream of patches
to refine it.
I think the root problem is that we're trying to use /proc/<pid>/smaps
for something that's independent of <pid> and its maps: a shmem object.
Would we be better served by a tmpfs-ish filesystem mounted somewhere,
which gives names to all the objects on the internal mount of tmpfs
(SysV SHM, memfds etc); and some fincore-ish syscalls which could be
used to interrogate how much swap any tmpfs file is using in any range?
(I am not volunteering to write this, not in the foreseeable future.)
I have no idea of the security implications of naming the hidden, it
may be a non-starter. And my guess is, it would be nice if it already
existed, but you need a solution today to some problems that have been
wasting your time; and grafting it into smaps looks to be good enough.
Some comments on your implementation below.
> ---
> Documentation/filesystems/proc.txt | 6 ++--
> fs/proc/task_mmu.c | 48 ++++++++++++++++++++++++++++++
> include/linux/shmem_fs.h | 6 ++++
> mm/shmem.c | 61 ++++++++++++++++++++++++++++++++++++++
> 4 files changed, 119 insertions(+), 2 deletions(-)
>
> diff --git a/Documentation/filesystems/proc.txt b/Documentation/filesystems/proc.txt
> index 7ef50cb..82d3657 100644
> --- a/Documentation/filesystems/proc.txt
> +++ b/Documentation/filesystems/proc.txt
> @@ -457,8 +457,10 @@ accessed.
> a mapping associated with a file may contain anonymous pages: when MAP_PRIVATE
> and a page is modified, the file page is replaced by a private anonymous copy.
> "Swap" shows how much would-be-anonymous memory is also used, but out on
> -swap.
> -"SwapPss" shows proportional swap share of this mapping.
> +swap. For shmem mappings, "Swap" shows how much of the mapped portion of the
> +underlying shmem object is on swap.
And for private mappings of tmpfs files? I expected it to show an
inderminate mixture of the two, but it looks like you treat the private
mapping just like a shared one, and take no notice of the COWed pages
out on swap which would have been reported before. Oh, no, I think
I misread, and you add the two together? I agree that's the easiest
thing to do, and therefore perhaps the best; but it doesn't fill me
with conviction that it's the right thing to do.
> +"SwapPss" shows proportional swap share of this mapping. Shmem mappings will
> +currently show 0 here.
Yes, my heart sank when I remembered SwapPss, and I wondered what you were
going to do with that. I was imagining that the Swap number would go into
SwapPss, but no, I prefer your choice to show 0 there (but depressed to
see the word "currently", which hints at grand schemes to plumb in another
radix_tree of swap counts, or more rmap_walks to calculate, or something).
> "AnonHugePages" shows the ammount of memory backed by transparent hugepage.
> "Shared_Hugetlb" and "Private_Hugetlb" show the ammounts of memory backed by
> hugetlbfs page which is *not* counted in "RSS" or "PSS" field for historical
> diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
> index 04999b2..103457c 100644
> --- a/fs/proc/task_mmu.c
> +++ b/fs/proc/task_mmu.c
> @@ -14,6 +14,7 @@
> #include <linux/swapops.h>
> #include <linux/mmu_notifier.h>
> #include <linux/page_idle.h>
> +#include <linux/shmem_fs.h>
>
> #include <asm/elf.h>
> #include <asm/uaccess.h>
> @@ -657,6 +658,51 @@ static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
> }
> #endif /* HUGETLB_PAGE */
>
> +#ifdef CONFIG_SHMEM
Correct.
> +static unsigned long smaps_shmem_swap(struct vm_area_struct *vma)
> +{
> + struct inode *inode;
> + unsigned long swapped;
> + pgoff_t start, end;
> +
> + if (!vma->vm_file)
> + return 0;
> +
> + inode = file_inode(vma->vm_file);
> +
> + if (!shmem_mapping(inode->i_mapping))
> + return 0;
Someone somewhere may ask for an ops method,
but that someone will certainly not be me.
> +
> + /*
> + * The easier cases are when the shmem object has nothing in swap, or
> + * we have the whole object mapped. Then we can simply use the stats
> + * that are already tracked by shmem.
> + */
> + swapped = shmem_swap_usage(inode);
> +
> + if (swapped == 0)
> + return 0;
You are absolutely right to go for that optimization, but please
please do it all inside one call to shmem.c: all you need is one
shmem_swap_usage(inode, start, end)
or
shmem_swap_usage(vma).
> +
> + if (vma->vm_end - vma->vm_start >= inode->i_size)
That must be wrong. It's probably right for all normal processes,
and you may not be interested in the rest; but anyone can set up
a mapping from end of file onwards, which won't intersect with the
swap at all. Just a little more thought on that test would be good.
> + return swapped;
> +
> + /*
> + * Here we have to inspect individual pages in our mapped range to
> + * determine how much of them are swapped out. Thanks to RCU, we don't
> + * need i_mutex to protect against truncating or hole punching.
> + */
> + start = linear_page_index(vma, vma->vm_start);
> + end = linear_page_index(vma, vma->vm_end);
> +
> + return shmem_partial_swap_usage(inode->i_mapping, start, end);
> +}
> +#else
> +static unsigned long smaps_shmem_swap(struct vm_area_struct *vma)
> +{
> + return 0;
> +}
> +#endif
> +
> static int show_smap(struct seq_file *m, void *v, int is_pid)
> {
> struct vm_area_struct *vma = v;
> @@ -674,6 +720,8 @@ static int show_smap(struct seq_file *m, void *v, int is_pid)
> /* mmap_sem is held in m_start */
> walk_page_vma(vma, &smaps_walk);
>
> + mss.swap += smaps_shmem_swap(vma);
> +
So, I think here you add the private swap to the object swap.
> show_map_vma(m, vma, is_pid);
>
> seq_printf(m,
> diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h
> index 50777b5..12519e4 100644
> --- a/include/linux/shmem_fs.h
> +++ b/include/linux/shmem_fs.h
> @@ -60,6 +60,12 @@ extern struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
> extern void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end);
> extern int shmem_unuse(swp_entry_t entry, struct page *page);
>
> +#ifdef CONFIG_SWAP
As Andrew said, better just drop the #ifdef here.
> +extern unsigned long shmem_swap_usage(struct inode *inode);
> +extern unsigned long shmem_partial_swap_usage(struct address_space *mapping,
> + pgoff_t start, pgoff_t end);
> +#endif
> +
> static inline struct page *shmem_read_mapping_page(
> struct address_space *mapping, pgoff_t index)
> {
> diff --git a/mm/shmem.c b/mm/shmem.c
> index b543cc7..b0e9e30 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
> @@ -360,6 +360,67 @@ static int shmem_free_swap(struct address_space *mapping,
> }
>
> /*
> + * Determine (in bytes) how much of the whole shmem object is swapped out.
> + */
> +unsigned long shmem_swap_usage(struct inode *inode)
> +{
> + struct shmem_inode_info *info = SHMEM_I(inode);
> + unsigned long swapped;
> +
> + /* Mostly an overkill, but it's not atomic64_t */
> + spin_lock(&info->lock);
Entirely overkill, what's atomic64_t got to do with it?
info->swapped is an unsigned long, 32-bit on 32-bit, 64-bit on 64-bit,
there are no atomicity issues. READ_ONCE if you like, but I can't even
see where it would read twice, or what bad consequence could result.
> + swapped = info->swapped;
> + spin_unlock(&info->lock);
> +
> + return swapped << PAGE_SHIFT;
> +}
> +
> +/*
> + * Determine (in bytes) how many pages within the given range are swapped out.
> + *
> + * Can be called without i_mutex or mapping->tree_lock thanks to RCU.
Correct.
> + */
> +unsigned long shmem_partial_swap_usage(struct address_space *mapping,
> + pgoff_t start, pgoff_t end)
> +{
> + struct radix_tree_iter iter;
> + void **slot;
> + struct page *page;
> + unsigned long swapped = 0;
> +
> + rcu_read_lock();
> +
> +restart:
> + radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
> + if (iter.index >= end)
> + break;
> +
> + page = radix_tree_deref_slot(slot);
> +
> + /*
> + * This should only be possible to happen at index 0, so we
> + * don't need to reset the counter, nor do we risk infinite
> + * restarts.
> + */
> + if (radix_tree_deref_retry(page))
> + goto restart;
> +
> + if (radix_tree_exceptional_entry(page))
> + swapped++;
> +
> + if (need_resched()) {
> + cond_resched_rcu();
> + start = iter.index + 1;
> + goto restart;
> + }
> + }
> +
> + rcu_read_unlock();
> +
> + return swapped << PAGE_SHIFT;
> +}
This is what you most wanted me to look at, but it looks perfect to me
(aside from my wanting one call into shmem.c instead of two).
Hugh
> +
> +/*
> * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
> */
> void shmem_unlock_mapping(struct address_space *mapping)
> --
> 2.5.2
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
[toc] | [prev] | [next] | [standalone]
| From | Peter Zijlstra <peterz@infradead.org> |
|---|---|
| Date | 2015-10-05 10:00 +0200 |
| Subject | Re: [PATCH v4 2/4] mm, proc: account for shmem swap in /proc/pid/smaps |
| Message-ID | <qg8QG-14p-7@gated-at.bofh.it> |
| In reply to | #1238237 |
On Fri, Oct 02, 2015 at 03:35:49PM +0200, Vlastimil Babka wrote:
> +static unsigned long smaps_shmem_swap(struct vm_area_struct *vma)
> +{
> + struct inode *inode;
> + unsigned long swapped;
> + pgoff_t start, end;
> +
> + if (!vma->vm_file)
> + return 0;
> +
> + inode = file_inode(vma->vm_file);
> +
> + if (!shmem_mapping(inode->i_mapping))
> + return 0;
> +
> + /*
> + * The easier cases are when the shmem object has nothing in swap, or
> + * we have the whole object mapped. Then we can simply use the stats
> + * that are already tracked by shmem.
> + */
> + swapped = shmem_swap_usage(inode);
> +
> + if (swapped == 0)
> + return 0;
> +
> + if (vma->vm_end - vma->vm_start >= inode->i_size)
> + return swapped;
> +
> + /*
> + * Here we have to inspect individual pages in our mapped range to
> + * determine how much of them are swapped out. Thanks to RCU, we don't
> + * need i_mutex to protect against truncating or hole punching.
> + */
At the very least put in an assertion that we hold the RCU read lock,
otherwise RCU doesn't guarantee anything and its not obvious it is held
here.
> + start = linear_page_index(vma, vma->vm_start);
> + end = linear_page_index(vma, vma->vm_end);
> +
> + return shmem_partial_swap_usage(inode->i_mapping, start, end);
> +}
> + * Determine (in bytes) how much of the whole shmem object is swapped out.
> + */
> +unsigned long shmem_swap_usage(struct inode *inode)
> +{
> + struct shmem_inode_info *info = SHMEM_I(inode);
> + unsigned long swapped;
> +
> + /* Mostly an overkill, but it's not atomic64_t */
Yeah, that don't make any kind of sense.
> + spin_lock(&info->lock);
> + swapped = info->swapped;
> + spin_unlock(&info->lock);
> +
> + return swapped << PAGE_SHIFT;
> +}
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web