Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp407752ybm; Tue, 26 May 2020 21:14:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzHqMyXxqlMZNOEhDBo5MOHPouNS9Sc/v3k0+kf2HtIf94o5OyFvNH6zCM+yAqwgfoV+8Tw X-Received: by 2002:a05:6402:6c6:: with SMTP id n6mr6118135edy.277.1590552853283; Tue, 26 May 2020 21:14:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590552853; cv=none; d=google.com; s=arc-20160816; b=g1UmAXaL2cuVzEJW1PlhZ3oA8wS77bcNUU8LiJn1lxU87pO26bE334toVSvqA866M0 bZ3spsWOYs0Q33dvwHR0PZ+t2okkfYtrr7X4ApkvffVu28J0E8Xu75VJJn+V0FWx8YP2 w918cWxxsFY5yvbHJHHJ+Gp9gxA51PSJIXlxOUEVj34L4+yuQOgYEz4y12EApmrsg+Vp u4sQrJRwWQezS4782xuxbZLAbSn3TrTAYqP9+Qk97QtBLYv/9NEYATV6eEVFVFCADNk+ RVASmjBN++XCHT3pWDB04W1A+EEx5Mu1Rrc4lMU6jLXe75yUW4cdCri1X3+r91yaCANc MXwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:smtp-origin-cluster :cc:to:smtp-origin-hostname:from:smtp-origin-hostprefix :dkim-signature; bh=cugBKwoeKftvWxkJwY+OzQ/BYGIKUP/EFF3JonuT84o=; b=Udj62mynkeW8I9D5kuAlqQnVqnSTVZVPzHOZd1g0nxySqecxXuudchzKJyI6yVjpXN Te6Zx1Q/nlRI+eiu5HAxQFVMeor3T5/V7BYCq420luMRsaW/4E7Y+eN82GwnYxKNnqYV m5GquIo+o+vggAnJvhzQDQDK1T4PDnE/3xhQAOALCIS0UN/qrQUHmp364Z5ES9kKzUjK M8enM8swcwdT2PktyUg7jTGOwB9AeoK2GibcnYr5jW4nDAWj3Rl5yySMALtKUdogrCau LnA4QczqWpYa8EYbMx+45YHZ+W0pDwInVqTwqgUUkFrBnYrCleN21D7w3l0tPrwaTFGJ gcyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=gWasY5pj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d21si1084791eja.353.2020.05.26.21.13.50; Tue, 26 May 2020 21:14:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=gWasY5pj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390548AbgEZVmp (ORCPT + 99 others); Tue, 26 May 2020 17:42:45 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:45416 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2390364AbgEZVml (ORCPT ); Tue, 26 May 2020 17:42:41 -0400 Received: from pps.filterd (m0089730.ppops.net [127.0.0.1]) by m0089730.ppops.net (8.16.0.42/8.16.0.42) with SMTP id 04QLdwNU019454 for ; Tue, 26 May 2020 14:42:40 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=cugBKwoeKftvWxkJwY+OzQ/BYGIKUP/EFF3JonuT84o=; b=gWasY5pjp51sC3dsCLbc8ZZMZ8Ra8inxc7+yqqZhiwNInQpsJhN/KK/82QDfqGBOrMed 7d1DAS3O63KVGKmrBe1+wkHmy4tEOX47cgx21Md1qqwCO/ngchk6tcCESKEh4rloVjmg e6LQcUT1N7O3XFVLCHvYK3cpj65l5stRK3E= Received: from maileast.thefacebook.com ([163.114.130.16]) by m0089730.ppops.net with ESMTP id 316yb14wk5-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 26 May 2020 14:42:40 -0700 Received: from intmgw002.06.prn3.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:82::c) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Tue, 26 May 2020 14:42:39 -0700 Received: by devvm1291.vll0.facebook.com (Postfix, from userid 111017) id 68ED115EA7D8; Tue, 26 May 2020 14:42:33 -0700 (PDT) Smtp-Origin-Hostprefix: devvm From: Roman Gushchin Smtp-Origin-Hostname: devvm1291.vll0.facebook.com To: Andrew Morton , Christoph Lameter CC: Johannes Weiner , Michal Hocko , Shakeel Butt , , Vlastimil Babka , , , Roman Gushchin Smtp-Origin-Cluster: vll0c01 Subject: [PATCH v4 09/19] mm: memcg/slab: charge individual slab objects instead of pages Date: Tue, 26 May 2020 14:42:17 -0700 Message-ID: <20200526214227.989341-10-guro@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200526214227.989341-1-guro@fb.com> References: <20200526214227.989341-1-guro@fb.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.216,18.0.687 definitions=2020-05-26_02:2020-05-26,2020-05-26 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 phishscore=0 clxscore=1015 adultscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 cotscore=-2147483648 spamscore=0 priorityscore=1501 suspectscore=2 bulkscore=0 malwarescore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2004280000 definitions=main-2005260166 X-FB-Internal: deliver Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Switch to per-object accounting of non-root slab objects. Charging is performed using obj_cgroup API in the pre_alloc hook. Obj_cgroup is charged with the size of the object and the size of metadata: as now it's the size of an obj_cgroup pointer. If the amount of memory has been charged successfully, the actual allocation code is executed. Otherwise, -ENOMEM is returned. In the post_alloc hook if the actual allocation succeeded, corresponding vmstats are bumped and the obj_cgroup pointer is saved. Otherwise, the charge is canceled. On the free path obj_cgroup pointer is obtained and used to uncharge the size of the releasing object. Memcg and lruvec counters are now representing only memory used by active slab objects and do not include the free space. The free space is shared and doesn't belong to any specific cgroup. Global per-node slab vmstats are still modified from (un)charge_slab_page= () functions. The idea is to keep all slab pages accounted as slab pages on system level. Signed-off-by: Roman Gushchin Reviewed-by: Vlastimil Babka --- mm/slab.h | 173 ++++++++++++++++++++++++------------------------------ 1 file changed, 77 insertions(+), 96 deletions(-) diff --git a/mm/slab.h b/mm/slab.h index 8bca0cb4b928..f219a29052d9 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -352,72 +352,6 @@ static inline struct mem_cgroup *memcg_from_slab_pag= e(struct page *page) return NULL; } =20 -/* - * Charge the slab page belonging to the non-root kmem_cache. - * Can be called for non-root kmem_caches only. - */ -static __always_inline int memcg_charge_slab(struct page *page, - gfp_t gfp, int order, - struct kmem_cache *s) -{ - unsigned int nr_pages =3D 1 << order; - struct mem_cgroup *memcg; - struct lruvec *lruvec; - int ret; - - rcu_read_lock(); - memcg =3D READ_ONCE(s->memcg_params.memcg); - while (memcg && !css_tryget_online(&memcg->css)) - memcg =3D parent_mem_cgroup(memcg); - rcu_read_unlock(); - - if (unlikely(!memcg || mem_cgroup_is_root(memcg))) { - mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s), - nr_pages << PAGE_SHIFT); - percpu_ref_get_many(&s->memcg_params.refcnt, nr_pages); - return 0; - } - - ret =3D memcg_kmem_charge(memcg, gfp, nr_pages); - if (ret) - goto out; - - lruvec =3D mem_cgroup_lruvec(memcg, page_pgdat(page)); - mod_lruvec_state(lruvec, cache_vmstat_idx(s), nr_pages << PAGE_SHIFT); - - percpu_ref_get_many(&s->memcg_params.refcnt, nr_pages); -out: - css_put(&memcg->css); - return ret; -} - -/* - * Uncharge a slab page belonging to a non-root kmem_cache. - * Can be called for non-root kmem_caches only. - */ -static __always_inline void memcg_uncharge_slab(struct page *page, int o= rder, - struct kmem_cache *s) -{ - unsigned int nr_pages =3D 1 << order; - struct mem_cgroup *memcg; - struct lruvec *lruvec; - - rcu_read_lock(); - memcg =3D READ_ONCE(s->memcg_params.memcg); - if (likely(!mem_cgroup_is_root(memcg))) { - lruvec =3D mem_cgroup_lruvec(memcg, page_pgdat(page)); - mod_lruvec_state(lruvec, cache_vmstat_idx(s), - -(nr_pages << PAGE_SHIFT)); - memcg_kmem_uncharge(memcg, nr_pages); - } else { - mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s), - -(nr_pages << PAGE_SHIFT)); - } - rcu_read_unlock(); - - percpu_ref_put_many(&s->memcg_params.refcnt, nr_pages); -} - static inline int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s, gfp_t gfp) { @@ -438,6 +372,47 @@ static inline void memcg_free_page_obj_cgroups(struc= t page *page) page->obj_cgroups =3D NULL; } =20 +static inline size_t obj_full_size(struct kmem_cache *s) +{ + /* + * For each accounted object there is an extra space which is used + * to store obj_cgroup membership. Charge it too. + */ + return s->size + sizeof(struct obj_cgroup *); +} + +static inline struct kmem_cache *memcg_slab_pre_alloc_hook(struct kmem_c= ache *s, + struct obj_cgroup **objcgp, + size_t objects, gfp_t flags) +{ + struct kmem_cache *cachep; + + cachep =3D memcg_kmem_get_cache(s, objcgp); + if (is_root_cache(cachep)) + return s; + + if (obj_cgroup_charge(*objcgp, flags, objects * obj_full_size(s))) { + memcg_kmem_put_cache(cachep); + cachep =3D NULL; + } + + return cachep; +} + +static inline void mod_objcg_state(struct obj_cgroup *objcg, + struct pglist_data *pgdat, + int idx, int nr) +{ + struct mem_cgroup *memcg; + struct lruvec *lruvec; + + rcu_read_lock(); + memcg =3D obj_cgroup_memcg(objcg); + lruvec =3D mem_cgroup_lruvec(memcg, pgdat); + mod_memcg_lruvec_state(lruvec, idx, nr); + rcu_read_unlock(); +} + static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s, struct obj_cgroup *objcg, size_t size, void **p) @@ -452,6 +427,10 @@ static inline void memcg_slab_post_alloc_hook(struct= kmem_cache *s, off =3D obj_to_index(s, page, p[i]); obj_cgroup_get(objcg); page_obj_cgroups(page)[off] =3D objcg; + mod_objcg_state(objcg, page_pgdat(page), + cache_vmstat_idx(s), obj_full_size(s)); + } else { + obj_cgroup_uncharge(objcg, obj_full_size(s)); } } obj_cgroup_put(objcg); @@ -470,6 +449,11 @@ static inline void memcg_slab_free_hook(struct kmem_= cache *s, struct page *page, off =3D obj_to_index(s, page, p); objcg =3D page_obj_cgroups(page)[off]; page_obj_cgroups(page)[off] =3D NULL; + + obj_cgroup_uncharge(objcg, obj_full_size(s)); + mod_objcg_state(objcg, page_pgdat(page), cache_vmstat_idx(s), + -obj_full_size(s)); + obj_cgroup_put(objcg); } =20 @@ -511,17 +495,6 @@ static inline struct mem_cgroup *memcg_from_slab_pag= e(struct page *page) return NULL; } =20 -static inline int memcg_charge_slab(struct page *page, gfp_t gfp, int or= der, - struct kmem_cache *s) -{ - return 0; -} - -static inline void memcg_uncharge_slab(struct page *page, int order, - struct kmem_cache *s) -{ -} - static inline int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s, gfp_t gfp) { @@ -532,6 +505,13 @@ static inline void memcg_free_page_obj_cgroups(struc= t page *page) { } =20 +static inline struct kmem_cache *memcg_slab_pre_alloc_hook(struct kmem_c= ache *s, + struct obj_cgroup **objcgp, + size_t objects, gfp_t flags) +{ + return NULL; +} + static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s, struct obj_cgroup *objcg, size_t size, void **p) @@ -569,32 +549,33 @@ static __always_inline int charge_slab_page(struct = page *page, gfp_t gfp, int order, struct kmem_cache *s) { - int ret; - - if (is_root_cache(s)) { - mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s), - PAGE_SIZE << order); - return 0; - } +#ifdef CONFIG_MEMCG_KMEM + if (memcg_kmem_enabled() && !is_root_cache(s)) { + int ret; =20 - ret =3D memcg_alloc_page_obj_cgroups(page, s, gfp); - if (ret) - return ret; + ret =3D memcg_alloc_page_obj_cgroups(page, s, gfp); + if (ret) + return ret; =20 - return memcg_charge_slab(page, gfp, order, s); + percpu_ref_get_many(&s->memcg_params.refcnt, 1 << order); + } +#endif + mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s), + PAGE_SIZE << order); + return 0; } =20 static __always_inline void uncharge_slab_page(struct page *page, int or= der, struct kmem_cache *s) { - if (is_root_cache(s)) { - mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s), - -(PAGE_SIZE << order)); - return; +#ifdef CONFIG_MEMCG_KMEM + if (memcg_kmem_enabled() && !is_root_cache(s)) { + memcg_free_page_obj_cgroups(page); + percpu_ref_put_many(&s->memcg_params.refcnt, 1 << order); } - - memcg_free_page_obj_cgroups(page); - memcg_uncharge_slab(page, order, s); +#endif + mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s), + -(PAGE_SIZE << order)); } =20 static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, vo= id *x) @@ -666,7 +647,7 @@ static inline struct kmem_cache *slab_pre_alloc_hook(= struct kmem_cache *s, =20 if (memcg_kmem_enabled() && ((flags & __GFP_ACCOUNT) || (s->flags & SLAB_ACCOUNT))) - return memcg_kmem_get_cache(s, objcgp); + return memcg_slab_pre_alloc_hook(s, objcgp, size, flags); =20 return s; } --=20 2.25.4