linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/5] mm: improve performance of accounted kernel memory allocations
@ 2023-10-10  0:09 Roman Gushchin
  2023-10-10  0:09 ` [PATCH v2 1/5] mm: kmem: optimize get_obj_cgroup_from_current() Roman Gushchin
                   ` (4 more replies)
  0 siblings, 5 replies; 11+ messages in thread
From: Roman Gushchin @ 2023-10-10  0:09 UTC (permalink / raw)
  To: linux-mm
  Cc: linux-kernel, cgroups, Johannes Weiner, Michal Hocko,
	Shakeel Butt, Muchun Song, Dennis Zhou, Andrew Morton,
	David Rientjes, Vlastimil Babka, Naresh Kamboju, Roman Gushchin

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.

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 |  14 ++-
 include/linux/sched.h      |   4 +
 mm/memcontrol.c            | 204 ++++++++++++++++++++++++++++++++-----
 mm/percpu.c                |   8 +-
 mm/slab.h                  |  10 +-
 5 files changed, 202 insertions(+), 38 deletions(-)

-- 
2.42.0



^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-10-12 13:48 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-10  0:09 [PATCH v2 0/5] mm: improve performance of accounted kernel memory allocations Roman Gushchin
2023-10-10  0:09 ` [PATCH v2 1/5] mm: kmem: optimize get_obj_cgroup_from_current() Roman Gushchin
2023-10-10  0:09 ` [PATCH v2 2/5] mm: kmem: add direct objcg pointer to task_struct Roman Gushchin
2023-10-11 18:01   ` Shakeel Butt
2023-10-11 18:05     ` Roman Gushchin
2023-10-10  0:09 ` [PATCH v2 3/5] mm: kmem: make memcg keep a reference to the original objcg Roman Gushchin
2023-10-11 19:51   ` Shakeel Butt
2023-10-10  0:09 ` [PATCH v2 4/5] mm: kmem: scoped objcg protection Roman Gushchin
2023-10-12 13:47   ` Shakeel Butt
2023-10-10  0:09 ` [PATCH v2 5/5] percpu: " Roman Gushchin
2023-10-12 13:48   ` Shakeel Butt

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).