Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp437486ybe; Fri, 6 Sep 2019 01:47:11 -0700 (PDT) X-Google-Smtp-Source: APXvYqydZ51yI/pkiMeE0iWJGoTXntSFbgHkgBDRHeKeoBMUeKJ2qw/lOFpP/GDTBxgdylEm6wrI X-Received: by 2002:a62:6045:: with SMTP id u66mr8985940pfb.261.1567759631832; Fri, 06 Sep 2019 01:47:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567759631; cv=none; d=google.com; s=arc-20160816; b=ICliRoFqJGI17eDm6pzcn9838m29wPKSQoubXHFOT8aWLU3bAE2f5KWDjGgJ5FC/Zd 2r/xTtDCw6evSNnUa1Lv9g393yZ+lRQds/Pwd+6ojNo14UylcqvUuDzx58xrVkBw/L8J 8uMJr0jcJoayxXYAaTsfiBJJpL6OVRHSV38rkImc66KoyGUXcty9xh6osAp0eA43gYex 0vBkQlAr22Vizebvzig4wru7XxQYM0T+5qjRIQH0GILVgoS7ahKxXWI6Y02F0BRQTzZ0 yVcfBn3pDRHxNla29JGDAud1CN8vgozoOJsUaIIMR52HYvV68syZz5Z0OVkmcEmtMYk+ 7lbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject :smtp-origin-cluster:cc:to:smtp-origin-hostname:from :smtp-origin-hostprefix:dkim-signature; bh=6+x6B4MHxrLYAe7QaNtIURxgkLbofTz5KDyctfNFi2k=; b=CkXDd3mhUQlRD3w6ZD1GtgH37PocI9eOgGq0QXGTONGn5DIAQxVoSqXXqy0CD0zFZD EpisX5byyIRDN6OJIlk/Jk8s4NiOBC/3hVJsguLRLYvnTRSThhfgBg403mXnAuJzOhCf CB74o0Fkl1400Cs82l+JGcEgG6MCZ54GGtnOo+qTlYgvsz/sRNM5vRQqTkuPucgEwfqt jOBfx6ue/PYjUPvU0URZVosF7r+ILDfxRHLj2Nv4AfzBCn8pwdqa8ZRebiVlpWinPLxE BCF/KTwcIOa8IJ3o/iHffjlDNB8QvxHs6VI5Ho93MvRvM9sq7XnQC3QsaeHOXr23EZnU SeuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=db5moETJ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o21si3840580pgm.453.2019.09.06.01.46.55; Fri, 06 Sep 2019 01:47:11 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=db5moETJ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390950AbfIEVqP (ORCPT + 99 others); Thu, 5 Sep 2019 17:46:15 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:24268 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389650AbfIEVqM (ORCPT ); Thu, 5 Sep 2019 17:46:12 -0400 Received: from pps.filterd (m0044010.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id x85LjCGa007194 for ; Thu, 5 Sep 2019 14:46:11 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : mime-version : content-type; s=facebook; bh=6+x6B4MHxrLYAe7QaNtIURxgkLbofTz5KDyctfNFi2k=; b=db5moETJTxmxMRmjEX8PmSCqZrcoPN29twcu753JN6vBw9lTsZ31xABBo5BmhovG2T41 8IRdwcqmL0YQbc2DwiBsRg8qUCUgF4TFsxjm6pXLUzt1U9ErhqQxPpb3AqIEDodUnUuT jGMlB46e5cWUrEmvUhPJoEbNVNAQqxK1cNc= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 2utkkxwup9-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Thu, 05 Sep 2019 14:46:11 -0700 Received: from mx-out.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:82::f) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1713.5; Thu, 5 Sep 2019 14:46:08 -0700 Received: by devvm2643.prn2.facebook.com (Postfix, from userid 111017) id A051F17229DF6; Thu, 5 Sep 2019 14:46:06 -0700 (PDT) Smtp-Origin-Hostprefix: devvm From: Roman Gushchin Smtp-Origin-Hostname: devvm2643.prn2.facebook.com To: CC: Michal Hocko , Johannes Weiner , , , Shakeel Butt , Vladimir Davydov , Waiman Long , Roman Gushchin Smtp-Origin-Cluster: prn2c23 Subject: [PATCH RFC 00/14] The new slab memory controller Date: Thu, 5 Sep 2019 14:45:44 -0700 Message-ID: <20190905214553.1643060-1-guro@fb.com> X-Mailer: git-send-email 2.17.1 X-FB-Internal: Safe MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.70,1.0.8 definitions=2019-09-05_08:2019-09-04,2019-09-05 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 malwarescore=0 lowpriorityscore=0 spamscore=0 priorityscore=1501 phishscore=0 mlxlogscore=999 mlxscore=0 clxscore=1015 adultscore=0 suspectscore=0 impostorscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-1906280000 definitions=main-1909050203 X-FB-Internal: deliver Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The existing slab memory controller is based on the idea of replicating slab allocator internals for each memory cgroup. This approach promises a low memory overhead (one pointer per page), and isn't adding too much code on hot allocation and release paths. But is has a very serious flaw: it leads to a low slab utilization. Using a drgn* script I've got an estimation of slab utilization on a number of machines running different production workloads. In most cases it was between 45% and 65%, and the best number I've seen was around 85%. Turning kmem accounting off brings it to high 90s. Also it brings back 30-50% of slab memory. It means that the real price of the existing slab memory controller is way bigger than a pointer per page. The real reason why the existing design leads to a low slab utilization is simple: slab pages are used exclusively by one memory cgroup. If there are only few allocations of certain size made by a cgroup, or if some active objects (e.g. dentries) are left after the cgroup is deleted, or the cgroup contains a single-threaded application which is barely allocating any kernel objects, but does it every time on a new CPU: in all these cases the resulting slab utilization is very low. If kmem accounting is off, the kernel is able to use free space on slab pages for other allocations. Arguably it wasn't an issue back to days when the kmem controller was introduced and was an opt-in feature, which had to be turned on individually for each memory cgroup. But now it's turned on by default on both cgroup v1 and v2. And modern systemd-based systems tend to create a large number of cgroups. This patchset provides a new implementation of the slab memory controller, which aims to reach a much better slab utilization by sharing slab pages between multiple memory cgroups. Below is the short description of the new design (more details in commit messages). Accounting is performed per-object instead of per-page. Slab-related vmstat counters are converted to bytes. Charging is performed on page-basis, with rounding up and remembering leftovers. Memcg ownership data is stored in a per-slab-page vector: for each slab page a vector of corresponding size is allocated. To keep slab memory reparenting working, instead of saving a pointer to the memory cgroup directly an intermediate object is used. It's simply a pointer to a memcg (which can be easily changed to the parent) with a built-in reference counter. This scheme allows to reparent all allocated objects without walking them over and changing memcg pointer to the parent. Instead of creating an individual set of kmem_caches for each memory cgroup, two global sets are used: the root set for non-accounted and root-cgroup allocations and the second set for all other allocations. This allows to simplify the lifetime management of individual kmem_caches: they are destroyed with root counterparts. It allows to remove a good amount of code and make things generally simpler. The patchset contains a couple of semi-independent parts, which can find their usage outside of the slab memory controller too: 1) subpage charging API, which can be used in the future for accounting of other non-page-sized objects, e.g. percpu allocations. 2) mem_cgroup_ptr API (refcounted pointers to a memcg, can be reused for the efficient reparenting of other objects, e.g. pagecache. The patchset has been tested on a number of different workloads in our production. In all cases, it saved hefty amounts of memory: 1) web frontend, 650-700 Mb, ~42% of slab memory 2) database cache, 750-800 Mb, ~35% of slab memory 3) dns server, 700 Mb, ~36% of slab memory So far I haven't found any regression on all tested workloads, but potential CPU regression caused by more precise accounting is a concern. Obviously the amount of saved memory depend on the number of memory cgroups, uptime and specific workloads, but overall it feels like the new controller saves 30-40% of slab memory, sometimes more. Additionally, it should lead to a lower memory fragmentation, just because of a smaller number of non-movable pages and also because there is no more need to move all slab objects to a new set of pages when a workload is restarted in a new memory cgroup. * https://github.com/osandov/drgn Roman Gushchin (14): mm: memcg: subpage charging API mm: memcg: introduce mem_cgroup_ptr mm: vmstat: use s32 for vm_node_stat_diff in struct per_cpu_nodestat mm: vmstat: convert slab vmstat counter to bytes mm: memcg/slab: allocate space for memcg ownership data for non-root slabs mm: slub: implement SLUB version of obj_to_index() mm: memcg/slab: save memcg ownership data for non-root slab objects mm: memcg: move memcg_kmem_bypass() to memcontrol.h mm: memcg: introduce __mod_lruvec_memcg_state() mm: memcg/slab: charge individual slab objects instead of pages mm: memcg: move get_mem_cgroup_from_current() to memcontrol.h mm: memcg/slab: replace memcg_from_slab_page() with memcg_from_slab_obj() mm: memcg/slab: use one set of kmem_caches for all memory cgroups mm: slab: remove redundant check in memcg_accumulate_slabinfo() drivers/base/node.c | 11 +- fs/proc/meminfo.c | 4 +- include/linux/memcontrol.h | 102 ++++++++- include/linux/mm_types.h | 5 +- include/linux/mmzone.h | 12 +- include/linux/slab.h | 3 +- include/linux/slub_def.h | 9 + include/linux/vmstat.h | 8 + kernel/power/snapshot.c | 2 +- mm/list_lru.c | 12 +- mm/memcontrol.c | 431 +++++++++++++++++++++-------------- mm/oom_kill.c | 2 +- mm/page_alloc.c | 8 +- mm/slab.c | 37 ++- mm/slab.h | 300 +++++++++++++------------ mm/slab_common.c | 449 ++++--------------------------------- mm/slob.c | 12 +- mm/slub.c | 63 ++---- mm/vmscan.c | 3 +- mm/vmstat.c | 38 +++- mm/workingset.c | 6 +- 21 files changed, 683 insertions(+), 834 deletions(-) -- 2.21.0