Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp315791ybm; Tue, 26 May 2020 18:01:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwt0OiKhBZkBTly6EXH/nrLM1jng4rK5/HkkEmh+QNWZI2p9BetXyj6sQIM6orkecopgoDT X-Received: by 2002:a17:906:2b04:: with SMTP id a4mr308423ejg.98.1590541292501; Tue, 26 May 2020 18:01:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590541292; cv=none; d=google.com; s=arc-20160816; b=J0LdCT4CjTeI7F9tLJHMVV21IYaKbaH6rV4xc6Aipt6rkFedmf7S/FYYzX7exU1bJ/ SkWXAIe/SdK3UE46SfDSjGiFsvOFQ7YRUnnbHbycW+io7DFZ6v1uBckyj/eKQgpDP8g9 LdOM+mRZp9/YDngD9cr7g26EkQOmokT9oA1qC5lUU1dmbtO4quOYRjeV9ZX7YSffz5wm tD9j4873GjHcShPZ7ufcXvvOeSQ7TEvJIjRHG/QpuwVapXPW0d1BLPpumVhpZj8r9in0 onzx0j2xsE0E3/HrNy1u5naiuo83T4x0yTt51iOMJWqgttw+Z+ap2Wq48ly5uEiUzTvh rOvg== 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=1kqerDXZTOHqAcU3DocgBcDskPlCF507AdFdXHpsqHY=; b=BUnxTLKr/AP2Udo5wgVujXGSqXgtzyJi/JR/eCmP/009Dv5IqY6x4tDVBFtec56ST/ Ui68br+Ys1Lry/Q1N8lCtyoXRVJ/3bQcmX0VsAUN8rQLieHivgPS9IacxBJcAYvZc+K1 6mpO1q8shMDyy+jyp1D4dfyvEhPWXjIO6HzTQwl5S3UVD5+AZt/BQCyfreT0TIZYBe8a 03neCtT8JoxPvEK/4rkw5Mc10MxBk0obiJgTySXTqKr8Z1zgCvRDOgwwDsXYx+dW6ZVM 93uojOnrZ71o7APJsmTQrG18AEE10M2czOlB3T1C7Q4wnRX2PE1CfAcg2+okomWgCJWe Sb5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=cUpjQMyQ; 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 dn18si842557edb.348.2020.05.26.18.01.09; Tue, 26 May 2020 18:01:32 -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=cUpjQMyQ; 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 S2391219AbgEZVng (ORCPT + 99 others); Tue, 26 May 2020 17:43:36 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:28954 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390836AbgEZVms (ORCPT ); Tue, 26 May 2020 17:42:48 -0400 Received: from pps.filterd (m0148460.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 04QLen85001032 for ; Tue, 26 May 2020 14:42:46 -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=1kqerDXZTOHqAcU3DocgBcDskPlCF507AdFdXHpsqHY=; b=cUpjQMyQzOqZeUfju2CAozl43dTTFo5TMYdT7aBhTianCnixR7Nx4SvPqPrTJRHyit6C N7hiN/83UgCFeBmlv+x6vUX06H7/Mancgm6rOJH723uUntXucymrt8R1uUlIJT5iy5Z3 +uQ7tC43i26R1/v57mGbepZ2VEHJ6fZbJPE= Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com with ESMTP id 3171bpcyf6-9 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 26 May 2020 14:42:46 -0700 Received: from intmgw002.41.prn1.facebook.com (2620:10d:c085:208::11) by mail.thefacebook.com (2620:10d:c085:11d::7) 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:42 -0700 Received: by devvm1291.vll0.facebook.com (Postfix, from userid 111017) id 5CDEC15EA7D4; 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 07/19] mm: memcg/slab: allocate obj_cgroups for non-root slab pages Date: Tue, 26 May 2020 14:42:15 -0700 Message-ID: <20200526214227.989341-8-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 impostorscore=0 malwarescore=0 lowpriorityscore=0 priorityscore=1501 cotscore=-2147483648 mlxlogscore=895 bulkscore=0 phishscore=0 adultscore=0 clxscore=1015 spamscore=0 suspectscore=2 mlxscore=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 Allocate and release memory to store obj_cgroup pointers for each non-root slab page. Reuse page->mem_cgroup pointer to store a pointer to the allocated space. To distinguish between obj_cgroups and memcg pointers in case when it's not obvious which one is used (as in page_cgroup_ino()), let's always set the lowest bit in the obj_cgroup case. Signed-off-by: Roman Gushchin Reviewed-by: Vlastimil Babka --- include/linux/mm_types.h | 5 +++- include/linux/slab_def.h | 6 +++++ include/linux/slub_def.h | 5 ++++ mm/memcontrol.c | 17 +++++++++++--- mm/slab.h | 49 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 4 deletions(-) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 64ede5f150dc..0277fbab7c93 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -198,7 +198,10 @@ struct page { atomic_t _refcount; =20 #ifdef CONFIG_MEMCG - struct mem_cgroup *mem_cgroup; + union { + struct mem_cgroup *mem_cgroup; + struct obj_cgroup **obj_cgroups; + }; #endif =20 /* diff --git a/include/linux/slab_def.h b/include/linux/slab_def.h index abc7de77b988..ccda7b9669a5 100644 --- a/include/linux/slab_def.h +++ b/include/linux/slab_def.h @@ -114,4 +114,10 @@ static inline unsigned int obj_to_index(const struct= kmem_cache *cache, return reciprocal_divide(offset, cache->reciprocal_buffer_size); } =20 +static inline int objs_per_slab_page(const struct kmem_cache *cache, + const struct page *page) +{ + return cache->num; +} + #endif /* _LINUX_SLAB_DEF_H */ diff --git a/include/linux/slub_def.h b/include/linux/slub_def.h index 30e91c83d401..f87302dcfe8c 100644 --- a/include/linux/slub_def.h +++ b/include/linux/slub_def.h @@ -198,4 +198,9 @@ static inline unsigned int obj_to_index(const struct = kmem_cache *cache, return __obj_to_index(cache, page_address(page), obj); } =20 +static inline int objs_per_slab_page(const struct kmem_cache *cache, + const struct page *page) +{ + return page->objects; +} #endif /* _LINUX_SLUB_DEF_H */ diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 7d4cbd594b83..c0f1abfea30e 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -569,10 +569,21 @@ ino_t page_cgroup_ino(struct page *page) unsigned long ino =3D 0; =20 rcu_read_lock(); - if (PageSlab(page) && !PageTail(page)) + if (PageSlab(page) && !PageTail(page)) { memcg =3D memcg_from_slab_page(page); - else - memcg =3D READ_ONCE(page->mem_cgroup); + } else { + memcg =3D page->mem_cgroup; + + /* + * The lowest bit set means that memcg isn't a valid + * memcg pointer, but a obj_cgroups pointer. + * In this case the page is shared and doesn't belong + * to any specific memory cgroup. + */ + if ((unsigned long) memcg & 0x1UL) + memcg =3D NULL; + } + while (memcg && !(memcg->css.flags & CSS_ONLINE)) memcg =3D parent_mem_cgroup(memcg); if (memcg) diff --git a/mm/slab.h b/mm/slab.h index 8a574d9361c1..a1633ea15fbf 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -319,6 +319,18 @@ static inline struct kmem_cache *memcg_root_cache(st= ruct kmem_cache *s) return s->memcg_params.root_cache; } =20 +static inline struct obj_cgroup **page_obj_cgroups(struct page *page) +{ + /* + * page->mem_cgroup and page->obj_cgroups are sharing the same + * space. To distinguish between them in case we don't know for sure + * that the page is a slab page (e.g. page_cgroup_ino()), let's + * always set the lowest bit of obj_cgroups. + */ + return (struct obj_cgroup **) + ((unsigned long)page->obj_cgroups & ~0x1UL); +} + /* * Expects a pointer to a slab page. Please note, that PageSlab() check * isn't sufficient, as it returns true also for tail compound slab page= s, @@ -406,6 +418,26 @@ static __always_inline void memcg_uncharge_slab(stru= ct page *page, int order, percpu_ref_put_many(&s->memcg_params.refcnt, nr_pages); } =20 +static inline int memcg_alloc_page_obj_cgroups(struct page *page, + struct kmem_cache *s, gfp_t gfp) +{ + unsigned int objects =3D objs_per_slab_page(s, page); + void *vec; + + vec =3D kcalloc(objects, sizeof(struct obj_cgroup *), gfp); + if (!vec) + return -ENOMEM; + + page->obj_cgroups =3D (struct obj_cgroup **) ((unsigned long)vec | 0x1U= L); + return 0; +} + +static inline void memcg_free_page_obj_cgroups(struct page *page) +{ + kfree(page_obj_cgroups(page)); + page->obj_cgroups =3D NULL; +} + extern void slab_init_memcg_params(struct kmem_cache *); extern void memcg_link_cache(struct kmem_cache *s, struct mem_cgroup *me= mcg); =20 @@ -455,6 +487,16 @@ static inline void memcg_uncharge_slab(struct page *= page, int order, { } =20 +static inline int memcg_alloc_page_obj_cgroups(struct page *page, + struct kmem_cache *s, gfp_t gfp) +{ + return 0; +} + +static inline void memcg_free_page_obj_cgroups(struct page *page) +{ +} + static inline void slab_init_memcg_params(struct kmem_cache *s) { } @@ -481,12 +523,18 @@ 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; } =20 + ret =3D memcg_alloc_page_obj_cgroups(page, s, gfp); + if (ret) + return ret; + return memcg_charge_slab(page, gfp, order, s); } =20 @@ -499,6 +547,7 @@ static __always_inline void uncharge_slab_page(struct= page *page, int order, return; } =20 + memcg_free_page_obj_cgroups(page); memcg_uncharge_slab(page, order, s); } =20 --=20 2.25.4