Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp69177pxu; Tue, 24 Nov 2020 19:05:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJyi8eUVH1puYrAGIUUs3eJ6nBSjJ4MrFXWyIFV321MJABtLCwoPJe8OTegTr8rHaU1U6317 X-Received: by 2002:aa7:ce8d:: with SMTP id y13mr1596380edv.65.1606273522554; Tue, 24 Nov 2020 19:05:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606273522; cv=none; d=google.com; s=arc-20160816; b=XpF8cslU/YIUbjDw+dfTGwX63faB0CZgQF3Xulm3fEVgDcRAzEABBVdSrUYjlgvlxi vZdURDy3obqO5RljyvVQmSC8c7xHAGT8XvkH9NbQcj9hzY19RVcoxSeQo7NpjNFzpn3J mxiClu+aZ5LUH3+hrSxFqkxV1Jg/8se4WWeiY1ihvTJ3xs0G70Bu2u2SH5/AbnK7Y0fc jGv+5+SNw9VSY1+gNtSaGVfk2qpUTiH0MJvWpKtPdEmLa6VGCAV9sYTVgVCKzV2kQYqY Q9fHKNrc5NrWUcPtDA9KvZX9x3tQPgF+SwnhvmgvsPLP/I14n+X5v4ADfP6s57vB+X3s yR3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=LrQC28K2WsL9UuG+4e/dXoM0Ws5Ciafct7wggEglOtU=; b=ldlN+9Gu9J755ziVx92A69xi6xu+T8oPQ42XnvGsmIXMIM6JymWis3sGyK9oX6v+Dr MdsT2xbHBF+kq0RGMP2S/f307zCC3DrcmaIFAisZulSVq7k+HK9dqjyv6MvMche1PxOw Gv23afa6jMZNkg1/zZTuGKU+hkYTJrfO65dwXgZy2+4df8VY9t89r7y8NLYf0H3praHy 49xj3MqlnT/rrph5B2A8FTsLLDinT3+D4yPIdUfdlejCz/rVxV+pP9n5d9yn+WXga4Mk eSM+2c3yaWdk8oRwNfokLPOzXPOdw9KwJbLWBTI2KIH8iW5BO2DUdWnRKLNM7ipFqrjt eVvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=kp7Mq3Fs; 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=REJECT sp=REJECT 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 dn20si651608ejc.189.2020.11.24.19.05.00; Tue, 24 Nov 2020 19:05:22 -0800 (PST) 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=kp7Mq3Fs; 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=REJECT sp=REJECT dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728084AbgKYDCH (ORCPT + 99 others); Tue, 24 Nov 2020 22:02:07 -0500 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:14792 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727741AbgKYDBh (ORCPT ); Tue, 24 Nov 2020 22:01:37 -0500 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 0AP2t5SO015062 for ; Tue, 24 Nov 2020 19:01:37 -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 : content-type : content-transfer-encoding : mime-version; s=facebook; bh=LrQC28K2WsL9UuG+4e/dXoM0Ws5Ciafct7wggEglOtU=; b=kp7Mq3FsDTM7p0FohnNE6umgp9w6mFv5+8sLokg4qCYl7vEJeJ1gzuTPS1CS3/oWMJ4B I+3rw5TRF685nCupzZ6pSNzr7Vo4BW+V4Yo6+Ex3+si/PLmWjokc3Ty44iJPaFFUhsvd aXTbDFagzCcFUrP7BRx5ESD99HceJPUWc6w= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 34ykkr4smw-16 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 24 Nov 2020 19:01:37 -0800 Received: from intmgw001.06.prn3.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Tue, 24 Nov 2020 19:01:28 -0800 Received: by devvm3388.prn0.facebook.com (Postfix, from userid 111017) id ECFAC16A1889; Tue, 24 Nov 2020 19:01:21 -0800 (PST) From: Roman Gushchin To: CC: , , , , , , , Subject: [PATCH bpf-next v8 04/34] mm: convert page kmemcg type to a page memcg flag Date: Tue, 24 Nov 2020 19:00:49 -0800 Message-ID: <20201125030119.2864302-5-guro@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20201125030119.2864302-1-guro@fb.com> References: <20201125030119.2864302-1-guro@fb.com> X-FB-Internal: Safe Content-Type: text/plain Content-Transfer-Encoding: quoted-printable X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.312,18.0.737 definitions=2020-11-24_11:2020-11-24,2020-11-24 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 phishscore=0 mlxlogscore=999 lowpriorityscore=0 impostorscore=0 mlxscore=0 suspectscore=3 clxscore=1015 spamscore=0 malwarescore=0 priorityscore=1501 bulkscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2011250018 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org PageKmemcg flag is currently defined as a page type (like buddy, offline, table and guard). Semantically it means that the page was accounted as a kernel memory by the page allocator and has to be uncharged on the release. As a side effect of defining the flag as a page type, the accounted page can't be mapped to userspace (look at page_has_type() and comments above). In particular, this blocks the accounting of vmalloc-backed memory used by some bpf maps, because these maps do map the memory to userspace. One option is to fix it by complicating the access to page->mapcount, which provides some free bits for page->page_type. But it's way better to move this flag into page->memcg_data flags. Indeed, the flag makes no sense without enabled memory cgroups and memory cgroup pointer set in particular. This commit replaces PageKmemcg() and __SetPageKmemcg() with PageMemcgKmem() and an open-coded OR operation setting the memcg pointer with the MEMCG_DATA_KMEM bit. __ClearPageKmemcg() can be simple deleted, as the whole memcg_data is zeroed at once. As a bonus, on !CONFIG_MEMCG build the PageMemcgKmem() check will be compiled out. Link: https://lkml.kernel.org/r/20201027001657.3398190-5-guro@fb.com Signed-off-by: Roman Gushchin Acked-by: Johannes Weiner Reviewed-by: Shakeel Butt Acked-by: Michal Hocko Signed-off-by: Andrew Morton --- include/linux/memcontrol.h | 37 +++++++++++++++++++++++++++++++++---- include/linux/page-flags.h | 11 ++--------- mm/memcontrol.c | 16 +++++----------- mm/page_alloc.c | 4 ++-- 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 99a4841d658b..7c9d43476166 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -346,8 +346,10 @@ extern struct mem_cgroup *root_mem_cgroup; enum page_memcg_data_flags { /* page->memcg_data is a pointer to an objcgs vector */ MEMCG_DATA_OBJCGS =3D (1UL << 0), + /* page has been accounted as a non-slab kernel page */ + MEMCG_DATA_KMEM =3D (1UL << 1), /* the next bit after the last actual flag */ - __NR_MEMCG_DATA_FLAGS =3D (1UL << 1), + __NR_MEMCG_DATA_FLAGS =3D (1UL << 2), }; =20 #define MEMCG_DATA_FLAGS_MASK (__NR_MEMCG_DATA_FLAGS - 1) @@ -369,8 +371,12 @@ enum page_memcg_data_flags { */ static inline struct mem_cgroup *page_memcg(struct page *page) { + unsigned long memcg_data =3D page->memcg_data; + VM_BUG_ON_PAGE(PageSlab(page), page); - return (struct mem_cgroup *)page->memcg_data; + VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_OBJCGS, page); + + return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK); } =20 /* @@ -387,7 +393,8 @@ static inline struct mem_cgroup *page_memcg_rcu(struct = page *page) VM_BUG_ON_PAGE(PageSlab(page), page); WARN_ON_ONCE(!rcu_read_lock_held()); =20 - return (struct mem_cgroup *)READ_ONCE(page->memcg_data); + return (struct mem_cgroup *)(READ_ONCE(page->memcg_data) & + ~MEMCG_DATA_FLAGS_MASK); } =20 /* @@ -416,7 +423,21 @@ static inline struct mem_cgroup *page_memcg_check(stru= ct page *page) if (memcg_data & MEMCG_DATA_OBJCGS) return NULL; =20 - return (struct mem_cgroup *)memcg_data; + return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK); +} + +/* + * PageMemcgKmem - check if the page has MemcgKmem flag set + * @page: a pointer to the page struct + * + * Checks if the page has MemcgKmem flag set. The caller must ensure that + * the page has an associated memory cgroup. It's not safe to call this fu= nction + * against some types of pages, e.g. slab pages. + */ +static inline bool PageMemcgKmem(struct page *page) +{ + VM_BUG_ON_PAGE(page->memcg_data & MEMCG_DATA_OBJCGS, page); + return page->memcg_data & MEMCG_DATA_KMEM; } =20 #ifdef CONFIG_MEMCG_KMEM @@ -435,6 +456,7 @@ static inline struct obj_cgroup **page_objcgs(struct pa= ge *page) unsigned long memcg_data =3D READ_ONCE(page->memcg_data); =20 VM_BUG_ON_PAGE(memcg_data && !(memcg_data & MEMCG_DATA_OBJCGS), page); + VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, page); =20 return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK); } @@ -454,6 +476,8 @@ static inline struct obj_cgroup **page_objcgs_check(str= uct page *page) if (!memcg_data || !(memcg_data & MEMCG_DATA_OBJCGS)) return NULL; =20 + VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, page); + return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK); } =20 @@ -1109,6 +1133,11 @@ static inline struct mem_cgroup *page_memcg_check(st= ruct page *page) return NULL; } =20 +static inline bool PageMemcgKmem(struct page *page) +{ + return false; +} + static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg) { return true; diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index 4f6ba9379112..fc0e1bd48e73 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -715,9 +715,8 @@ PAGEFLAG_FALSE(DoubleMap) #define PAGE_MAPCOUNT_RESERVE -128 #define PG_buddy 0x00000080 #define PG_offline 0x00000100 -#define PG_kmemcg 0x00000200 -#define PG_table 0x00000400 -#define PG_guard 0x00000800 +#define PG_table 0x00000200 +#define PG_guard 0x00000400 =20 #define PageType(page, flag) \ ((page->page_type & (PAGE_TYPE_BASE | flag)) =3D=3D PAGE_TYPE_BASE) @@ -768,12 +767,6 @@ PAGE_TYPE_OPS(Buddy, buddy) */ PAGE_TYPE_OPS(Offline, offline) =20 -/* - * If kmemcg is enabled, the buddy allocator will set PageKmemcg() on - * pages allocated with __GFP_ACCOUNT. It gets cleared on page free. - */ -PAGE_TYPE_OPS(Kmemcg, kmemcg) - /* * Marks pages in use as page tables. */ diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 0054b4846770..e0366e306221 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3090,8 +3090,8 @@ int __memcg_kmem_charge_page(struct page *page, gfp_t= gfp, int order) if (memcg && !mem_cgroup_is_root(memcg)) { ret =3D __memcg_kmem_charge(memcg, gfp, 1 << order); if (!ret) { - page->memcg_data =3D (unsigned long)memcg; - __SetPageKmemcg(page); + page->memcg_data =3D (unsigned long)memcg | + MEMCG_DATA_KMEM; return 0; } css_put(&memcg->css); @@ -3116,10 +3116,6 @@ void __memcg_kmem_uncharge_page(struct page *page, i= nt order) __memcg_kmem_uncharge(memcg, nr_pages); page->memcg_data =3D 0; css_put(&memcg->css); - - /* slab pages do not have PageKmemcg flag set */ - if (PageKmemcg(page)) - __ClearPageKmemcg(page); } =20 static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_by= tes) @@ -6877,12 +6873,10 @@ static void uncharge_page(struct page *page, struct= uncharge_gather *ug) nr_pages =3D compound_nr(page); ug->nr_pages +=3D nr_pages; =20 - if (!PageKmemcg(page)) { - ug->pgpgout++; - } else { + if (PageMemcgKmem(page)) ug->nr_kmem +=3D nr_pages; - __ClearPageKmemcg(page); - } + else + ug->pgpgout++; =20 ug->dummy_page =3D page; page->memcg_data =3D 0; diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 271133b8243b..3c53018c9c61 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1214,7 +1214,7 @@ static __always_inline bool free_pages_prepare(struct= page *page, * Do not let hwpoison pages hit pcplists/buddy * Untie memcg state and reset page's owner */ - if (memcg_kmem_enabled() && PageKmemcg(page)) + if (memcg_kmem_enabled() && PageMemcgKmem(page)) __memcg_kmem_uncharge_page(page, order); reset_page_owner(page, order); return false; @@ -1244,7 +1244,7 @@ static __always_inline bool free_pages_prepare(struct= page *page, } if (PageMappingFlags(page)) page->mapping =3D NULL; - if (memcg_kmem_enabled() && PageKmemcg(page)) + if (memcg_kmem_enabled() && PageMemcgKmem(page)) __memcg_kmem_uncharge_page(page, order); if (check_free) bad +=3D check_free_page(page); --=20 2.26.2