From patchwork Mon Jan 27 17:34:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roman Gushchin X-Patchwork-Id: 11353007 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CF0F41395 for ; Mon, 27 Jan 2020 17:43:07 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8D7C321739 for ; Mon, 27 Jan 2020 17:43:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="mKHpOvzc" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8D7C321739 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=fb.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id D40DE6B0003; Mon, 27 Jan 2020 12:43:04 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id C329D6B0275; Mon, 27 Jan 2020 12:43:04 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A7F086B0273; Mon, 27 Jan 2020 12:43:04 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0254.hostedemail.com [216.40.44.254]) by kanga.kvack.org (Postfix) with ESMTP id 8FB186B0272 for ; Mon, 27 Jan 2020 12:43:04 -0500 (EST) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with SMTP id 4D56F2C12 for ; Mon, 27 Jan 2020 17:43:04 +0000 (UTC) X-FDA: 76424135088.27.drug46_8f29140c68d07 X-Spam-Summary: 2,0,0,c00259e5f8883654,d41d8cd98f00b204,prvs=829571e488=guro@fb.com,::akpm@linux-foundation.org:mhocko@kernel.org:hannes@cmpxchg.org:shakeelb@google.com:vdavydov.dev@gmail.com:linux-kernel@vger.kernel.org:kernel-team@fb.com:bharata@linux.ibm.com:laoar.shao@gmail.com:guro@fb.com,RULES_HIT:2:41:355:379:541:800:960:966:973:988:989:1260:1261:1277:1313:1314:1345:1359:1437:1516:1518:1535:1605:1730:1747:1777:1792:2194:2196:2198:2199:2200:2201:2393:2559:2562:2689:2731:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3872:3874:4049:4120:4321:4385:4605:5007:6261:6653:8957:10004:11026:11233:11473:11658:11914:12043:12291:12296:12297:12438:12555:12683:12895:12986:14096:14097:14394:21080:21627:21987:21990:30054:30064:30070,0,RBL:67.231.145.42:@fb.com:.lbl8.mailshell.net-62.12.0.100 64.201.201.201,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: drug46_8f29140c68d07 X-Filterd-Recvd-Size: 9838 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by imf36.hostedemail.com (Postfix) with ESMTP for ; Mon, 27 Jan 2020 17:43:03 +0000 (UTC) Received: from pps.filterd (m0044012.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 00RHg9Ol017410 for ; Mon, 27 Jan 2020 09:43:02 -0800 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-type; s=facebook; bh=njtZoBprfzkBv2sebCGvemaI+52tmlo1FiaaBvQxQ3k=; b=mKHpOvzcj4Rut53Cn3QKQSp5KQExw6wdvHVKgdBNr+K766VoWNGVoIdPYD5P4JDKVQsX Vi8GRUWtqpB28tvsunXtggm6Xh1AmEt9SycsCnClUvbZ8dqAKfcuI75A3rbj4u5qPWJL t5rKFITrqixNslMCPtmNAkPVklBTUjMf6nc= Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com with ESMTP id 2xs6a2p7u9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Mon, 27 Jan 2020 09:43:02 -0800 Received: from intmgw002.41.prn1.facebook.com (2620:10d:c085:108::4) by mail.thefacebook.com (2620:10d:c085:21d::4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1779.2; Mon, 27 Jan 2020 09:43:02 -0800 Received: by devvm2643.prn2.facebook.com (Postfix, from userid 111017) id BF7301DFEFC97; Mon, 27 Jan 2020 09:35:07 -0800 (PST) Smtp-Origin-Hostprefix: devvm From: Roman Gushchin Smtp-Origin-Hostname: devvm2643.prn2.facebook.com To: , Andrew Morton CC: Michal Hocko , Johannes Weiner , Shakeel Butt , Vladimir Davydov , , , Bharata B Rao , Yafang Shao , Roman Gushchin Smtp-Origin-Cluster: prn2c23 Subject: [PATCH v2 16/28] mm: memcg/slab: allocate obj_cgroups for non-root slab pages Date: Mon, 27 Jan 2020 09:34:41 -0800 Message-ID: <20200127173453.2089565-17-guro@fb.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200127173453.2089565-1-guro@fb.com> References: <20200127173453.2089565-1-guro@fb.com> X-FB-Internal: Safe MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.138,18.0.572 definitions=2020-01-27_06:2020-01-24,2020-01-27 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 phishscore=0 mlxlogscore=881 mlxscore=0 clxscore=1015 suspectscore=2 malwarescore=0 bulkscore=0 lowpriorityscore=0 impostorscore=0 adultscore=0 spamscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-1911200001 definitions=main-2001270143 X-FB-Internal: deliver X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- include/linux/mm.h | 25 ++++++++++++++++++-- include/linux/mm_types.h | 5 +++- mm/memcontrol.c | 5 ++-- mm/slab.c | 3 ++- mm/slab.h | 51 +++++++++++++++++++++++++++++++++++++++- mm/slub.c | 2 +- 6 files changed, 83 insertions(+), 8 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 080f8ac8bfb7..65224becc4ca 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1264,12 +1264,33 @@ static inline void set_page_links(struct page *page, enum zone_type zone, #ifdef CONFIG_MEMCG static inline struct mem_cgroup *page_memcg(struct page *page) { - return page->mem_cgroup; + struct mem_cgroup *memcg = 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 + * isn't charged to any specific memory cgroup. Return NULL. + */ + if ((unsigned long) memcg & 0x1UL) + memcg = NULL; + + return memcg; } static inline struct mem_cgroup *page_memcg_rcu(struct page *page) { + struct mem_cgroup *memcg = READ_ONCE(page->mem_cgroup); + WARN_ON_ONCE(!rcu_read_lock_held()); - return READ_ONCE(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 + * isn't charged to any specific memory cgroup. Return NULL. + */ + if ((unsigned long) memcg & 0x1UL) + memcg = NULL; + + return memcg; } #else static inline struct mem_cgroup *page_memcg(struct page *page) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 270aa8fd2800..5102f00f3336 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -198,7 +198,10 @@ struct page { atomic_t _refcount; #ifdef CONFIG_MEMCG - struct mem_cgroup *mem_cgroup; + union { + struct mem_cgroup *mem_cgroup; + struct obj_cgroup **obj_cgroups; + }; #endif /* diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 9aa37bc61db5..94337ab1ebe9 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -545,7 +545,8 @@ ino_t page_cgroup_ino(struct page *page) if (PageSlab(page) && !PageTail(page)) memcg = memcg_from_slab_page(page); else - memcg = READ_ONCE(page->mem_cgroup); + memcg = page_memcg_rcu(page); + while (memcg && !(memcg->css.flags & CSS_ONLINE)) memcg = parent_mem_cgroup(memcg); if (memcg) @@ -2783,7 +2784,7 @@ struct mem_cgroup *mem_cgroup_from_obj(void *p) return memcg_from_slab_page(page); /* All other pages use page->mem_cgroup */ - return page->mem_cgroup; + return page_memcg(page); } static int memcg_alloc_cache_id(void) diff --git a/mm/slab.c b/mm/slab.c index a89633603b2d..22e161b57367 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -1370,7 +1370,8 @@ static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, return NULL; } - if (charge_slab_page(page, flags, cachep->gfporder, cachep)) { + if (charge_slab_page(page, flags, cachep->gfporder, cachep, + cachep->num)) { __free_pages(page, cachep->gfporder); return NULL; } diff --git a/mm/slab.h b/mm/slab.h index 7925f7005161..8ee8c3a250ac 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -319,6 +319,18 @@ static inline struct kmem_cache *memcg_root_cache(struct kmem_cache *s) return s->memcg_params.root_cache; } +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 pages, @@ -406,6 +418,25 @@ static __always_inline void memcg_uncharge_slab(struct page *page, int order, percpu_ref_put_many(&s->memcg_params.refcnt, nr_pages); } +static inline int memcg_alloc_page_obj_cgroups(struct page *page, gfp_t gfp, + unsigned int objects) +{ + void *vec; + + vec = kcalloc(objects, sizeof(struct obj_cgroup *), gfp); + if (!vec) + return -ENOMEM; + + page->obj_cgroups = (struct obj_cgroup **) ((unsigned long)vec | 0x1UL); + return 0; +} + +static inline void memcg_free_page_obj_cgroups(struct page *page) +{ + kfree(page_obj_cgroups(page)); + page->obj_cgroups = NULL; +} + extern void slab_init_memcg_params(struct kmem_cache *); extern void memcg_link_cache(struct kmem_cache *s, struct mem_cgroup *memcg); @@ -455,6 +486,16 @@ static inline void memcg_uncharge_slab(struct page *page, int order, { } +static inline int memcg_alloc_page_obj_cgroups(struct page *page, gfp_t gfp, + unsigned int objects) +{ + return 0; +} + +static inline void memcg_free_page_obj_cgroups(struct page *page) +{ +} + static inline void slab_init_memcg_params(struct kmem_cache *s) { } @@ -479,14 +520,21 @@ static inline struct kmem_cache *virt_to_cache(const void *obj) static __always_inline int charge_slab_page(struct page *page, gfp_t gfp, int order, - struct kmem_cache *s) + struct kmem_cache *s, + unsigned int objects) { + int ret; + if (is_root_cache(s)) { mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s), PAGE_SIZE << order); return 0; } + ret = memcg_alloc_page_obj_cgroups(page, gfp, objects); + if (ret) + return ret; + return memcg_charge_slab(page, gfp, order, s); } @@ -499,6 +547,7 @@ static __always_inline void uncharge_slab_page(struct page *page, int order, return; } + memcg_free_page_obj_cgroups(page); memcg_uncharge_slab(page, order, s); } diff --git a/mm/slub.c b/mm/slub.c index ed6aea234400..165e43076c8b 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -1516,7 +1516,7 @@ static inline struct page *alloc_slab_page(struct kmem_cache *s, else page = __alloc_pages_node(node, flags, order); - if (page && charge_slab_page(page, flags, order, s)) { + if (page && charge_slab_page(page, flags, order, s, oo_objects(oo))) { __free_pages(page, order); page = NULL; }