diff mbox series

[v2] docs: proc.rst: meminfo: briefly describe gaps in memory accounting

Message ID 20210420121354.1160437-1-rppt@kernel.org (mailing list archive)
State New, archived
Headers show
Series [v2] docs: proc.rst: meminfo: briefly describe gaps in memory accounting | expand

Commit Message

Mike Rapoport April 20, 2021, 12:13 p.m. UTC
From: Mike Rapoport <rppt@linux.ibm.com>

Add a paragraph that explains that it may happen that the counters in
/proc/meminfo do not add up to the overall memory usage.

Signed-off-by: Mike Rapoport <rppt@linux.ibm.com>
---
v2:
* Add brief changelog
* Fix typo
* Update example about network memory usage according to Eric's comment at

https://lore.kernel.org/lkml/CANn89iKprp7WYeZy4RRO5jHykprnSCcVBc7Tk14Ui_MA9OK7Fg@mail.gmail.com

v1: Link: https://lore.kernel.org/lkml/20210420085105.1156640-1-rppt@kernel.org

 Documentation/filesystems/proc.rst | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

Comments

Mike Rapoport April 20, 2021, 12:21 p.m. UTC | #1
On Tue, Apr 20, 2021 at 03:13:54PM +0300, Mike Rapoport wrote:
> From: Mike Rapoport <rppt@linux.ibm.com>
> 
> Add a paragraph that explains that it may happen that the counters in
> /proc/meminfo do not add up to the overall memory usage.
> 
> Signed-off-by: Mike Rapoport <rppt@linux.ibm.com>

Ooops, forgot to add Michal's Ack, sorry.

> ---
> v2:
> * Add brief changelog
> * Fix typo
> * Update example about network memory usage according to Eric's comment at
> 
> https://lore.kernel.org/lkml/CANn89iKprp7WYeZy4RRO5jHykprnSCcVBc7Tk14Ui_MA9OK7Fg@mail.gmail.com
> 
> v1: Link: https://lore.kernel.org/lkml/20210420085105.1156640-1-rppt@kernel.org
> 
>  Documentation/filesystems/proc.rst | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems/proc.rst
> index 48fbfc336ebf..8c77a491c436 100644
> --- a/Documentation/filesystems/proc.rst
> +++ b/Documentation/filesystems/proc.rst
> @@ -929,8 +929,14 @@ meminfo
>  ~~~~~~~
>  
>  Provides information about distribution and utilization of memory.  This
> -varies by architecture and compile options.  The following is from a
> -16GB PIII, which has highmem enabled.  You may not have all of these fields.
> +varies by architecture and compile options. Please note that it may happen
> +that the memory accounted here does not add up to the overall memory usage
> +and the difference for some workloads can be substantial. In many cases there
> +are other means to find out additional memory using subsystem specific
> +interfaces, for instance /proc/net/sockstat for TCP memory allocations.
> +
> +The following is from a 16GB PIII, which has highmem enabled.
> +You may not have all of these fields.
>  
>  ::
>  
> -- 
> 2.29.2
>
Michal Hocko April 20, 2021, 1:12 p.m. UTC | #2
On Tue 20-04-21 15:21:08, Mike Rapoport wrote:
> On Tue, Apr 20, 2021 at 03:13:54PM +0300, Mike Rapoport wrote:
> > From: Mike Rapoport <rppt@linux.ibm.com>
> > 
> > Add a paragraph that explains that it may happen that the counters in
> > /proc/meminfo do not add up to the overall memory usage.
> > 
> > Signed-off-by: Mike Rapoport <rppt@linux.ibm.com>
> 
> Ooops, forgot to add Michal's Ack, sorry.

Let's make it more explicit
Acked-by: Michal Hocko <mhocko@suse.com>

Thanks!
Matthew Wilcox April 20, 2021, 1:24 p.m. UTC | #3
On Tue, Apr 20, 2021 at 03:13:54PM +0300, Mike Rapoport wrote:
> Add a paragraph that explains that it may happen that the counters in
> /proc/meminfo do not add up to the overall memory usage.

... that is, the sum may be lower because memory is allocated for other
purposes that is not reported here, right?

Is it ever possible for it to be higher?  Maybe due to a race when
sampling the counters?

>  Provides information about distribution and utilization of memory.  This
> -varies by architecture and compile options.  The following is from a
> -16GB PIII, which has highmem enabled.  You may not have all of these fields.
> +varies by architecture and compile options. Please note that it may happen
> +that the memory accounted here does not add up to the overall memory usage
> +and the difference for some workloads can be substantial. In many cases there
> +are other means to find out additional memory using subsystem specific
> +interfaces, for instance /proc/net/sockstat for TCP memory allocations.

How about just:

+varies by architecture and compile options.  The memory reported here
+may not add up to the overall memory usage and the difference for some
+workloads can be substantial. [...]

But I'd like to be a bit more explicit about the reason, hence my question
above to be sure I understand.


It's also not entirely clear which of the fields in meminfo can be
usefully summed.  VmallocTotal is larger than MemTotal, for example.
But I know that KernelStack is allocated through vmalloc these days,
and I don't know whether VmallocUsed includes KernelStack or whether I
can sum them.  Similarly, is Mlocked a subset of Unevictable?

There is some attempt at explaining how these numbers fit together, but
it's outdated, and doesn't include Mlocked, Unevictable or KernelStack
Michal Hocko April 20, 2021, 1:57 p.m. UTC | #4
On Tue 20-04-21 14:24:30, Matthew Wilcox wrote:
> On Tue, Apr 20, 2021 at 03:13:54PM +0300, Mike Rapoport wrote:
> > Add a paragraph that explains that it may happen that the counters in
> > /proc/meminfo do not add up to the overall memory usage.
> 
> ... that is, the sum may be lower because memory is allocated for other
> purposes that is not reported here, right?

yes. Many direct page allocator users are not accounted in any of the
existing counters.

> Is it ever possible for it to be higher?  Maybe due to a race when
> sampling the counters?

Yes likely possible. You will never get an atomic snapshot of all
counters.

> >  Provides information about distribution and utilization of memory.  This
> > -varies by architecture and compile options.  The following is from a
> > -16GB PIII, which has highmem enabled.  You may not have all of these fields.
> > +varies by architecture and compile options. Please note that it may happen
> > +that the memory accounted here does not add up to the overall memory usage
> > +and the difference for some workloads can be substantial. In many cases there
> > +are other means to find out additional memory using subsystem specific
> > +interfaces, for instance /proc/net/sockstat for TCP memory allocations.
> 
> How about just:
> 
> +varies by architecture and compile options.  The memory reported here
> +may not add up to the overall memory usage and the difference for some
> +workloads can be substantial. [...]
> 
> But I'd like to be a bit more explicit about the reason, hence my question
> above to be sure I understand.
> 
> 
> It's also not entirely clear which of the fields in meminfo can be
> usefully summed.  VmallocTotal is larger than MemTotal, for example.

Yes. Many/Most counters cannot be simply sumed up. A trivial example would be
Active/Inactive is a sum of both anona and file. Mlocked will be
accounted in LRU pages and Unevictable. MemAvailable is not really a
counter... 

Usual memory consumption is usually something like LRU pages + Slab
memory + kernel stack + vmalloc used + pcp.

> But I know that KernelStack is allocated through vmalloc these days,
> and I don't know whether VmallocUsed includes KernelStack or whether I
> can sum them.  Similarly, is Mlocked a subset of Unevictable?
> 
> There is some attempt at explaining how these numbers fit together, but
> it's outdated, and doesn't include Mlocked, Unevictable or KernelStack

Agreed there is a lot of tribal knowledge or even misconceptions flying
around and it will take much more work to put everything into shape.
This is only one tiny step forward.
Michal Hocko April 20, 2021, 2:05 p.m. UTC | #5
On Tue 20-04-21 15:57:08, Michal Hocko wrote:
[...]
> Usual memory consumption is usually something like LRU pages + Slab
> memory + kernel stack + vmalloc used + pcp.
> 
> > But I know that KernelStack is allocated through vmalloc these days,
> > and I don't know whether VmallocUsed includes KernelStack or whether I
> > can sum them.  Similarly, is Mlocked a subset of Unevictable?

Forgot to reply to these two. Yes they do. So if we want to be precise
then you have to check the stack allocation configuration. There are
just so many traps lurking here. Something you get used to over time
but this is certainly far far away from an ideal state. What else we can
expect from an ad hoc approach to providing data to userspace that was
historically applied to this and many other proc interfaces. We are
trying to be strict for some time but some mistakes are simply hard to
fix up (e.g. accounting shmem as a page cache to name some more).
Mike Rapoport April 20, 2021, 2:51 p.m. UTC | #6
On Tue, Apr 20, 2021 at 02:24:30PM +0100, Matthew Wilcox wrote:
> On Tue, Apr 20, 2021 at 03:13:54PM +0300, Mike Rapoport wrote:
> > Add a paragraph that explains that it may happen that the counters in
> > /proc/meminfo do not add up to the overall memory usage.
> 
> ... that is, the sum may be lower because memory is allocated for other
> purposes that is not reported here, right?
> 
> Is it ever possible for it to be higher?  Maybe due to a race when
> sampling the counters?
> 
> >  Provides information about distribution and utilization of memory.  This
> > -varies by architecture and compile options.  The following is from a
> > -16GB PIII, which has highmem enabled.  You may not have all of these fields.
> > +varies by architecture and compile options. Please note that it may happen
> > +that the memory accounted here does not add up to the overall memory usage
> > +and the difference for some workloads can be substantial. In many cases there
> > +are other means to find out additional memory using subsystem specific
> > +interfaces, for instance /proc/net/sockstat for TCP memory allocations.
> 
> How about just:
> 
> +varies by architecture and compile options.  The memory reported here
> +may not add up to the overall memory usage and the difference for some
> +workloads can be substantial. [...]

I like this. I also for adding a sentence about overlap in the counters:

+varies by architecture and compile options.  Some of the counters reported
+here overlap.  The memory reported by the non overlapping counters may not
+add up to the overall memory usage and the difference for some workloads
can be substantial. [...]
 
> But I'd like to be a bit more explicit about the reason, hence my question
> above to be sure I understand.
> 
> It's also not entirely clear which of the fields in meminfo can be
> usefully summed.  VmallocTotal is larger than MemTotal, for example.
> But I know that KernelStack is allocated through vmalloc these days,
> and I don't know whether VmallocUsed includes KernelStack or whether I
> can sum them.  Similarly, is Mlocked a subset of Unevictable?
> 
> There is some attempt at explaining how these numbers fit together, but
> it's outdated, and doesn't include Mlocked, Unevictable or KernelStack

Fixing the outdated docs and adding more detailed explanation is obviously
welcome, but it's beyond the scope of the current patch.
Alexey Dobriyan April 20, 2021, 5:58 p.m. UTC | #7
On Tue, Apr 20, 2021 at 03:57:07PM +0200, Michal Hocko wrote:
> On Tue 20-04-21 14:24:30, Matthew Wilcox wrote:
> > On Tue, Apr 20, 2021 at 03:13:54PM +0300, Mike Rapoport wrote:
> > > Add a paragraph that explains that it may happen that the counters in
> > > /proc/meminfo do not add up to the overall memory usage.
> > 
> > ... that is, the sum may be lower because memory is allocated for other
> > purposes that is not reported here, right?
> 
> yes. Many direct page allocator users are not accounted in any of the
> existing counters.

Does virtio_balloon dereserve special mention?

From inside VM memory borrowing looks like one giant memory leak resulting
in support tickets (not that people who file them read internal kernel
documentation...)
Michal Hocko April 21, 2021, 6:35 a.m. UTC | #8
On Tue 20-04-21 20:58:56, Alexey Dobriyan wrote:
> On Tue, Apr 20, 2021 at 03:57:07PM +0200, Michal Hocko wrote:
> > On Tue 20-04-21 14:24:30, Matthew Wilcox wrote:
> > > On Tue, Apr 20, 2021 at 03:13:54PM +0300, Mike Rapoport wrote:
> > > > Add a paragraph that explains that it may happen that the counters in
> > > > /proc/meminfo do not add up to the overall memory usage.
> > > 
> > > ... that is, the sum may be lower because memory is allocated for other
> > > purposes that is not reported here, right?
> > 
> > yes. Many direct page allocator users are not accounted in any of the
> > existing counters.
> 
> Does virtio_balloon dereserve special mention?

Yes

> From inside VM memory borrowing looks like one giant memory leak resulting
> in support tickets (not that people who file them read internal kernel
> documentation...)

Even if people do not read that documentation it is really good to have
a reference you can send when you are dealing with bug reports.

Thanks!
diff mbox series

Patch

diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems/proc.rst
index 48fbfc336ebf..8c77a491c436 100644
--- a/Documentation/filesystems/proc.rst
+++ b/Documentation/filesystems/proc.rst
@@ -929,8 +929,14 @@  meminfo
 ~~~~~~~
 
 Provides information about distribution and utilization of memory.  This
-varies by architecture and compile options.  The following is from a
-16GB PIII, which has highmem enabled.  You may not have all of these fields.
+varies by architecture and compile options. Please note that it may happen
+that the memory accounted here does not add up to the overall memory usage
+and the difference for some workloads can be substantial. In many cases there
+are other means to find out additional memory using subsystem specific
+interfaces, for instance /proc/net/sockstat for TCP memory allocations.
+
+The following is from a 16GB PIII, which has highmem enabled.
+You may not have all of these fields.
 
 ::