public inbox for cgroups@vger.kernel.org
 help / color / mirror / Atom feed
From: Roman Gushchin <roman.gushchin@linux.dev>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org,
	Johannes Weiner <hannes@cmpxchg.org>,
	Michal Hocko <mhocko@kernel.org>,
	Shakeel@vger.kernel.org, Butt@vger.kernel.org,
	shakeelb@google.com, Muchun Song <muchun.song@linux.dev>,
	Dennis Zhou <dennis@kernel.org>,
	David Rientjes <rientjes@google.com>,
	Vlastimil Babka <vbabka@suse.cz>,
	Naresh Kamboju <naresh.kamboju@linaro.org>,
	Roman Gushchin <roman.gushchin@linux.dev>
Subject: [PATCH v3 0/5] mm: improve performance of accounted kernel memory allocations
Date: Mon, 16 Oct 2023 15:18:55 -0700	[thread overview]
Message-ID: <20231016221900.4031141-1-roman.gushchin@linux.dev> (raw)

This patchset improves the performance of accounted kernel memory allocations
by ~30% as measured by a micro-benchmark [1]. The benchmark is very
straightforward: 1M of 64 bytes-large kmalloc() allocations.

Below are results with the disabled kernel memory accounting, the original state
and with this patchset applied.

|             | Kmem disabled | Original | Patched |  Delta |
|-------------+---------------+----------+---------+--------|
| User cgroup |         29764 |    84548 |   59078 | -30.0% |
| Root cgroup |         29742 |    48342 |   31501 | -34.8% |

As we can see, the patchset removes the majority of the overhead when there is
no actual accounting (a task belongs to the root memory cgroup) and almost
halves the accounting overhead otherwise.

The main idea is to get rid of unnecessary memcg to objcg conversions and switch
to a scope-based protection of objcgs, which eliminates extra operations with
objcg reference counters under a rcu read lock. More details are provided in
individual commit descriptions.

v3:
	- fixed a bug spotted by Shakeel
	- added some comments, per Shakeel
v2:
	- fixed a bug discovered by Naresh Kamboju
	- code changes asked by Johannes (added comments, open-coded bit ops)
	- merged in a couple of small fixes
v1:
	- made the objcg update fully lockless
	- fixed !CONFIG_MMU build issues
rfc:
	https://lwn.net/Articles/945722/

--
[1]:

static int memory_alloc_test(struct seq_file *m, void *v)
{
       unsigned long i, j;
       void **ptrs;
       ktime_t start, end;
       s64 delta, min_delta = LLONG_MAX;

       ptrs = kvmalloc(sizeof(void *) * 1000000, GFP_KERNEL);
       if (!ptrs)
               return -ENOMEM;

       for (j = 0; j < 100; j++) {
               start = ktime_get();
               for (i = 0; i < 1000000; i++)
                       ptrs[i] = kmalloc(64, GFP_KERNEL_ACCOUNT);
               end = ktime_get();

               delta = ktime_us_delta(end, start);
               if (delta < min_delta)
                       min_delta = delta;

               for (i = 0; i < 1000000; i++)
                       kfree(ptrs[i]);
       }

       kvfree(ptrs);
       seq_printf(m, "%lld us\n", min_delta);

       return 0;
}

--

Signed-off-by: Roman Gushchin (Cruise) <roman.gushchin@linux.dev>


Roman Gushchin (5):
  mm: kmem: optimize get_obj_cgroup_from_current()
  mm: kmem: add direct objcg pointer to task_struct
  mm: kmem: make memcg keep a reference to the original objcg
  mm: kmem: scoped objcg protection
  percpu: scoped objcg protection

 include/linux/memcontrol.h |  17 +++-
 include/linux/sched.h      |   4 +
 include/linux/sched/mm.h   |   4 +
 mm/memcontrol.c            | 204 ++++++++++++++++++++++++++++++++-----
 mm/percpu.c                |   8 +-
 mm/slab.h                  |  15 +--
 6 files changed, 214 insertions(+), 38 deletions(-)

-- 
2.42.0


             reply	other threads:[~2023-10-16 22:19 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-16 22:18 Roman Gushchin [this message]
2023-10-16 22:18 ` [PATCH v3 1/5] mm: kmem: optimize get_obj_cgroup_from_current() Roman Gushchin
2023-10-17  9:57   ` Vlastimil Babka
2023-10-16 22:18 ` [PATCH v3 2/5] mm: kmem: add direct objcg pointer to task_struct Roman Gushchin
2023-10-16 22:34   ` Shakeel Butt
2023-10-18  9:52   ` Vlastimil Babka
2023-10-18 14:11     ` Vlastimil Babka
2023-10-18 15:29     ` Shakeel Butt
2023-10-18 17:22     ` Roman Gushchin
2023-10-18 18:26       ` Shakeel Butt
2023-10-18 22:37         ` Roman Gushchin
2023-10-19 16:36           ` Shakeel Butt
2023-10-16 22:18 ` [PATCH v3 3/5] mm: kmem: make memcg keep a reference to the original objcg Roman Gushchin
2023-10-18 11:58   ` Vlastimil Babka
2023-10-18 14:06   ` Vlastimil Babka
2023-10-16 22:18 ` [PATCH v3 4/5] mm: kmem: scoped objcg protection Roman Gushchin
2023-10-18 14:04   ` Vlastimil Babka
2023-10-16 22:19 ` [PATCH v3 5/5] percpu: " Roman Gushchin
2023-10-18 14:23   ` Vlastimil Babka

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=20231016221900.4031141-1-roman.gushchin@linux.dev \
    --to=roman.gushchin@linux.dev \
    --cc=Butt@vger.kernel.org \
    --cc=Shakeel@vger.kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=cgroups@vger.kernel.org \
    --cc=dennis@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mhocko@kernel.org \
    --cc=muchun.song@linux.dev \
    --cc=naresh.kamboju@linaro.org \
    --cc=rientjes@google.com \
    --cc=shakeelb@google.com \
    --cc=vbabka@suse.cz \
    /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