linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>,
	Al Viro <viro@zeniv.linux.org.uk>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: get_vmalloc_info() and /proc/meminfo insanely expensive
Date: Thu, 13 Aug 2015 11:32:52 -0700	[thread overview]
Message-ID: <CA+55aFxzOAx7365Mx2o55TZOS+bZGh_Pfr=vVF3QGg0btsDumg@mail.gmail.com> (raw)
In-Reply-To: <CA+55aFzjhAoa6aiEfuwM_XyYn0OWRjUuHyEasfj=q4tzOHFGYA@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1136 bytes --]

On Wed, Aug 12, 2015 at 10:52 PM, Linus Torvalds
<torvalds@linux-foundation.org> wrote:
>
> I get the feeling this file should be rewritten. But that's not going
> to happen. The "let's just cache the last value for one jiffy" seemed
> to be the minimal fixup to it.

Here's a totally untested patch (I'll reboot and test soon - it does
at least compile for me).

Notice the total lack of locking, which means that it's fundamentally
racy. I really can't find it inside myself to care. Introducing those
vmalloc fields was a mistake to begin with, any races here are "ok, we
get values that were valid at some point, but it might have been a
second ago".

And I also can't find it in myself to care about the "on 32-bit,
jiffies wraps in 49 days if HZ is 1000". If somebody carefully avoids
ever reading /proc/meminfo for 49 days, and then reads it in _just_
the right second, and gets a really stale value, I'm just going to do
my honey badger impression.

Because we really shouldn't have added the vmalloc information to
/proc/meminfo to begin with, and nobody ever cares about those values
anyway.

Comments?

                Linus

[-- Attachment #2: vmalloc.patch --]
[-- Type: text/x-patch, Size: 2253 bytes --]

 mm/vmalloc.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 48 insertions(+), 2 deletions(-)

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 2faaa2976447..0d0b96ed8948 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -2688,7 +2688,7 @@ static int __init proc_vmalloc_init(void)
 }
 module_init(proc_vmalloc_init);
 
-void get_vmalloc_info(struct vmalloc_info *vmi)
+static void calc_vmalloc_info(struct vmalloc_info *vmi)
 {
 	struct vmap_area *va;
 	unsigned long free_area_size;
@@ -2735,5 +2735,51 @@ void get_vmalloc_info(struct vmalloc_info *vmi)
 out:
 	rcu_read_unlock();
 }
-#endif
 
+/*
+ * Calculating the vmalloc information is expensive, and nobody really
+ * deeply cares about it anyway. Yet, some versions of glibc end up
+ * reading /proc/meminfo a lot, not because they care about the vmalloc
+ * fields, but because they care about the total memory info.
+ *
+ * So to alleviate that expense, we cache the vmalloc information for
+ * a second. NOTE! This is fundamentally racy, since the accesses to
+ * the two fields in "struct vmalloc_info" and the cache timeout are
+ * all entirely unsynchronized. We just don't care.
+ */
+void get_vmalloc_info(struct vmalloc_info *vmi)
+{
+	static unsigned long cache_timeout = INITIAL_JIFFIES;
+	static struct vmalloc_info cached_info;
+	unsigned long now = jiffies, last = READ_ONCE(cache_timeout);
+
+	if (now - last < HZ) {
+		*vmi = cached_info;
+		return;
+	}
+
+	/*
+	 * We update the cache timeout early, because we (again) do
+	 * not care if somebody else comes in and sees slightly stale
+	 * information. We'd rather return more stale information
+	 * than waste time with multiple CPU's all calculating the
+	 * new state.
+	 *
+	 * Note: the barriers are here not to fix any races, but to
+	 * avoid the compiling spreading out the updates to these
+	 * variables any more than necessary.
+	 *
+	 * Also note that we calculate the new state into the 'vmi'
+	 * buffer that is passed in, and private to the caller. That
+	 * is intentional: we do not want to update the cached info
+	 * incrementally during the calculations.
+	 */
+	WRITE_ONCE(cache_timeout, now);
+	barrier();
+
+	calc_vmalloc_info(vmi);
+
+	barrier();
+	cached_info = *vmi;
+}
+#endif

  parent reply	other threads:[~2015-08-13 18:32 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-13  3:29 get_vmalloc_info() and /proc/meminfo insanely expensive Linus Torvalds
2015-08-13  4:00 ` Andrew Morton
2015-08-13  5:52   ` Linus Torvalds
2015-08-13  7:42     ` Rasmus Villemoes
2015-08-13 16:50       ` Linus Torvalds
2015-08-13 18:32     ` Linus Torvalds [this message]
2015-08-13 18:52       ` Linus Torvalds
2015-08-13 19:50         ` David Rientjes
2015-08-13 20:04           ` Linus Torvalds
2015-08-13 21:15       ` Tony Luck
2015-08-13 22:56         ` Linus Torvalds
2015-08-13 20:26     ` Andrew Morton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CA+55aFxzOAx7365Mx2o55TZOS+bZGh_Pfr=vVF3QGg0btsDumg@mail.gmail.com' \
    --to=torvalds@linux-foundation.org \
    --cc=akpm@linux-foundation.org \
    --cc=iamjoonsoo.kim@lge.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).