From patchwork Tue Nov 10 19:57:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roman Gushchin X-Patchwork-Id: 11895275 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 AAFCD1391 for ; Tue, 10 Nov 2020 19:58:16 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 59BAF20731 for ; Tue, 10 Nov 2020 19:58:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="SZbOMRLG" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 59BAF20731 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject 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 72F316B0036; Tue, 10 Nov 2020 14:58:13 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 6DF996B005C; Tue, 10 Nov 2020 14:58:13 -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 5CFA56B005D; Tue, 10 Nov 2020 14:58:13 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0150.hostedemail.com [216.40.44.150]) by kanga.kvack.org (Postfix) with ESMTP id 30DC96B0036 for ; Tue, 10 Nov 2020 14:58:13 -0500 (EST) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id CBC138249980 for ; Tue, 10 Nov 2020 19:58:12 +0000 (UTC) X-FDA: 77469570024.24.books27_1110712272f7 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin24.hostedemail.com (Postfix) with ESMTP id AFBFC1A4A0 for ; Tue, 10 Nov 2020 19:58:12 +0000 (UTC) X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,prvs=8583d35c3c=guro@fb.com,,RULES_HIT:30001:30054:30064:30070,0,RBL:67.231.145.42:@fb.com:.lbl8.mailshell.net-64.10.201.10 62.18.0.100;04yf8s9t56kz1ip5spanhd7wuap86ypg4iibd3cfktg7i84ie89qp174mhoom6x.ngut975hgu9d1inubmydg3u6pjwo7ibjar9z8m7zrjoofngnhusc5gbpddqz44y.h-lbl8.mailshell.net-223.238.255.100,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:70,LUA_SUMMARY:none X-HE-Tag: books27_1110712272f7 X-Filterd-Recvd-Size: 8175 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by imf02.hostedemail.com (Postfix) with ESMTP for ; Tue, 10 Nov 2020 19:58:11 +0000 (UTC) Received: from pps.filterd (m0109334.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 0AAJiWgo012499 for ; Tue, 10 Nov 2020 11:58:10 -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-transfer-encoding : content-type; s=facebook; bh=SDYx2t+dMle8yU1XBf2ct6vuamgyRUmM0G9FpunbHRY=; b=SZbOMRLG32g9x0kOKUowEBZXhnKvSUXGf8/H+8SRpG6FBM57Od46NVmjwkcFx/rdxDbB Kr6lv3DxH2sisVAhowspNbDUXJqwSiTs3rPG6BEsBkapeu27z5QLqr6rkdoeF/4xqyyJ RMyFSCGlwwpc8/Wx9w0OqvwIrEBAbCAWoY8= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 34pc9qcvjs-10 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 10 Nov 2020 11:58:10 -0800 Received: from intmgw002.06.prn3.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Tue, 10 Nov 2020 11:58:05 -0800 Received: by devvm1755.vll0.facebook.com (Postfix, from userid 111017) id 34213238DAA7; Tue, 10 Nov 2020 11:57:59 -0800 (PST) From: Roman Gushchin To: Andrew Morton , CC: Shakeel Butt , Johannes Weiner , Michal Hocko , Christoph Lameter , , , Roman Gushchin Subject: [PATCH 2/2] mm: memcg/slab: pre-allocate obj_cgroups for slab caches with SLAB_ACCOUNT Date: Tue, 10 Nov 2020 11:57:53 -0800 Message-ID: <20201110195753.530157-2-guro@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20201110195753.530157-1-guro@fb.com> References: <20201110195753.530157-1-guro@fb.com> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.312,18.0.737 definitions=2020-11-10_07:2020-11-10,2020-11-10 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 mlxscore=0 spamscore=0 mlxlogscore=919 phishscore=0 adultscore=0 suspectscore=2 priorityscore=1501 bulkscore=0 impostorscore=0 lowpriorityscore=0 clxscore=1015 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2011100135 X-FB-Internal: deliver X-Bogosity: Ham, tests=bogofilter, spamicity=0.000572, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: In general it's unknown in advance if a slab page will contain accounted objects or not. In order to avoid memory waste, an obj_cgroup vector is allocated dynamically when a need to account of a new object arises. Such approach is memory efficient, but requires an expensive cmpxchg() to set up the memcg/objcgs pointer, because an allocation can race with a different allocation on another cpu. But in some common cases it's known for sure that a slab page will contain accounted objects: if the page belongs to a slab cache with a SLAB_ACCOUNT flag set. It includes such popular objects like vm_area_struct, anon_vma, task_struct, etc. In such cases we can pre-allocate the objcgs vector and simple assign it to the page without any atomic operations, because at this early stage the page is not visible to anyone else. Signed-off-by: Roman Gushchin Signed-off-by: Roman Gushchin Signed-off-by: Roman Gushchin Signed-off-by: Andrew Morton Acked-by: Johannes Weiner Reviewed-by: Shakeel Butt --- include/linux/memcontrol.h | 14 ++++++++++---- mm/memcontrol.c | 4 ++-- mm/slab.c | 2 +- mm/slab.h | 14 ++++++++++---- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 20108e426f84..8271f11152e6 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -485,14 +485,20 @@ static inline struct obj_cgroup **page_objcgs_check(struct page *page) * set_page_objcgs - associate a page with a object cgroups vector * @page: a pointer to the page struct * @objcgs: a pointer to the object cgroups vector + * @atomic: save the value atomically * * Atomically associates a page with a vector of object cgroups. */ static inline bool set_page_objcgs(struct page *page, - struct obj_cgroup **objcgs) + struct obj_cgroup **objcgs, bool atomic) { - return !cmpxchg(&page->memcg_data, 0, (unsigned long)objcgs | - MEMCG_DATA_OBJCGS); + unsigned long memcg_data = (unsigned long) objcgs | MEMCG_DATA_OBJCGS; + + if (atomic) + return !cmpxchg(&page->memcg_data, 0, memcg_data); + + page->memcg_data = memcg_data; + return true; } #else static inline struct obj_cgroup **page_objcgs(struct page *page) @@ -506,7 +512,7 @@ static inline struct obj_cgroup **page_objcgs_check(struct page *page) } static inline bool set_page_objcgs(struct page *page, - struct obj_cgroup **objcgs) + struct obj_cgroup **objcgs, bool atomic) { return true; } diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 69a2893a6455..37bffd336235 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2874,7 +2874,7 @@ static void commit_charge(struct page *page, struct mem_cgroup *memcg) #ifdef CONFIG_MEMCG_KMEM int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s, - gfp_t gfp) + gfp_t gfp, bool atomic) { unsigned int objects = objs_per_slab_page(s, page); void *vec; @@ -2884,7 +2884,7 @@ int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s, if (!vec) return -ENOMEM; - if (!set_page_objcgs(page, vec)) + if (!set_page_objcgs(page, vec, atomic)) kfree(vec); else kmemleak_not_leak(vec); diff --git a/mm/slab.c b/mm/slab.c index c0ea4b1c7088..df0299e1d0b9 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -1380,7 +1380,7 @@ static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, return NULL; } - account_slab_page(page, cachep->gfporder, cachep); + account_slab_page(page, cachep->gfporder, cachep, flags); __SetPageSlab(page); /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */ if (sk_memalloc_socks() && page_is_pfmemalloc(page)) diff --git a/mm/slab.h b/mm/slab.h index c73050654b8a..f1d6ba09b630 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -240,7 +240,7 @@ static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t fla #ifdef CONFIG_MEMCG_KMEM int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s, - gfp_t gfp); + gfp_t gfp, bool atomic); static inline void memcg_free_page_obj_cgroups(struct page *page) { @@ -307,7 +307,8 @@ static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s, page = virt_to_head_page(p[i]); if (!page_objcgs(page) && - memcg_alloc_page_obj_cgroups(page, s, flags)) { + memcg_alloc_page_obj_cgroups(page, s, flags, + true)) { obj_cgroup_uncharge(objcg, obj_full_size(s)); continue; } @@ -371,7 +372,8 @@ static inline struct mem_cgroup *memcg_from_slab_obj(void *ptr) } static inline int memcg_alloc_page_obj_cgroups(struct page *page, - struct kmem_cache *s, gfp_t gfp) + struct kmem_cache *s, gfp_t gfp, + bool atomic) { return 0; } @@ -412,8 +414,12 @@ static inline struct kmem_cache *virt_to_cache(const void *obj) } static __always_inline void account_slab_page(struct page *page, int order, - struct kmem_cache *s) + struct kmem_cache *s, + gfp_t gfp) { + if (memcg_kmem_enabled() && (s->flags & SLAB_ACCOUNT)) + memcg_alloc_page_obj_cgroups(page, s, gfp, false); + mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s), PAGE_SIZE << order); }