Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp69250pxu; Tue, 24 Nov 2020 19:05:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJx5iwxRkVXuBtmbrCOB72c2IK3p7zCGLfBWsWYbmf8K7HXt9d1GEyvfQKSuDCzV+uMOehBR X-Received: by 2002:a17:906:86c7:: with SMTP id j7mr1334153ejy.301.1606273529954; Tue, 24 Nov 2020 19:05:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606273529; cv=none; d=google.com; s=arc-20160816; b=FKEGXb2pfDnviSz90xHGoZutoCncBq2THhgZKyowcptkRzpCDx0KA+XK1y6nS1gbdn y/gGcmU9sImwbRdoZDuEOhhKkt0MfW27i3xhxCP06vjWlia0eWVNEOtw2y7aqtrOM2j/ UX9GbcbJRICjXBP7/R8mvnNjlWhACcoSP+lOG8cG70+E+ew8uqkPt50mTfwyQj8RkdNC 1XMO9NVtwt49aczbQVKHCwRCrE8OiF/g98d5PrjxsR+ezMDtvqWFyFzTA4l9ZowISr/B s4DKg0YAtf+T+zHEAVZ+PCh7oL1TpY9ep6gVsEBYwT+EOWur0cxg0P1meH7Asl3Cfp/T pcvA== 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=JGw6o7TBx+ILLo4YTt2neoho4rwlX//7V56cYtCLLz8=; b=YYlVAxDO+kyV/h0N4AT0FFIOBdTe4kzVjUVHHeUG1PvrAe62OBU9wRv1f1FLInSSM0 fJhagbQadzbI9sbaHnqgiCzOcKZafpaD5/RPWpJ9yXEfLhzL24EhxDT72rXiAWWF6SIq x4UjJcCVaQuckgPT72tTke2eLJD3AJtFAQ6F9/Jm8SR4PFoAxLN4snRZ9BgJyX5AS2+l EgwXMLvJzklDqpBhgRx+khA2ZHfRIKv7IjWYlNNt4wZsNxunhQHyqyDG84/uD5MM/cP4 iuPynDCL7E/vRqfXsCZNfTXTiGv/R8u9aCpOklJm0sBhrq8si54GFwwEmlHD1tBEzGmh 3v0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b="Q6/tnZLE"; 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 a9si524308ejr.248.2020.11.24.19.05.07; Tue, 24 Nov 2020 19:05:29 -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="Q6/tnZLE"; 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 S1728202AbgKYDCg (ORCPT + 99 others); Tue, 24 Nov 2020 22:02:36 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:23540 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727496AbgKYDBf (ORCPT ); Tue, 24 Nov 2020 22:01:35 -0500 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 0AP2t4n7000631 for ; Tue, 24 Nov 2020 19:01:33 -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=JGw6o7TBx+ILLo4YTt2neoho4rwlX//7V56cYtCLLz8=; b=Q6/tnZLEHG5TYt+d2qleBucx10UHWwME1uLOp6Z8HrYM3Zx89AaB5yr+iH4P8dq3e+Nl zcJ7ddbIbqFUxvRW2DgNfqLo1jxnOKktsXZdhok6alTUB5HBVEjyczHZIQuDP6jG2c+J LtboRd9dh+uSsjnacS8wKI45c0oODQHZeTc= Received: from maileast.thefacebook.com ([163.114.130.16]) by m0089730.ppops.net with ESMTP id 351aqe0umw-7 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 24 Nov 2020 19:01:33 -0800 Received: from intmgw001.06.prn3.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::5) 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:32 -0800 Received: by devvm3388.prn0.facebook.com (Postfix, from userid 111017) id E127616A1885; Tue, 24 Nov 2020 19:01:21 -0800 (PST) From: Roman Gushchin To: CC: , , , , , , , Subject: [PATCH bpf-next v8 02/34] mm: memcontrol/slab: use helpers to access slab page's memcg_data Date: Tue, 24 Nov 2020 19:00:47 -0800 Message-ID: <20201125030119.2864302-3-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 spamscore=0 bulkscore=0 priorityscore=1501 lowpriorityscore=0 adultscore=0 impostorscore=0 malwarescore=0 mlxscore=0 clxscore=1015 phishscore=0 mlxlogscore=999 suspectscore=3 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 To gather all direct accesses to struct page's memcg_data field in one place, let's introduce 3 new helpers to use in the slab accounting code: struct obj_cgroup **page_objcgs(struct page *page); struct obj_cgroup **page_objcgs_check(struct page *page); bool set_page_objcgs(struct page *page, struct obj_cgroup **objcgs); They are similar to the corresponding API for generic pages, except that the setter can return false, indicating that the value has been already set from a different thread. Link: https://lkml.kernel.org/r/20201027001657.3398190-3-guro@fb.com Signed-off-by: Roman Gushchin Acked-by: Johannes Weiner Reviewed-by: Shakeel Butt Cc: Michal Hocko Signed-off-by: Andrew Morton --- include/linux/memcontrol.h | 64 ++++++++++++++++++++++++++++++++++++++ mm/memcontrol.c | 6 ++-- mm/slab.h | 35 +++++---------------- 3 files changed, 75 insertions(+), 30 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index f95c1433461c..c7ac0a5b8989 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -416,6 +416,70 @@ static inline struct mem_cgroup *page_memcg_check(stru= ct page *page) return (struct mem_cgroup *)memcg_data; } =20 +#ifdef CONFIG_MEMCG_KMEM +/* + * page_objcgs - get the object cgroups vector associated with a page + * @page: a pointer to the page struct + * + * Returns a pointer to the object cgroups vector associated with the page, + * or NULL. This function assumes that the page is known to have an + * associated object cgroups vector. It's not safe to call this function + * against pages, which might have an associated memory cgroup: e.g. + * kernel stack pages. + */ +static inline struct obj_cgroup **page_objcgs(struct page *page) +{ + return (struct obj_cgroup **)(READ_ONCE(page->memcg_data) & ~0x1UL); +} + +/* + * page_objcgs_check - get the object cgroups vector associated with a page + * @page: a pointer to the page struct + * + * Returns a pointer to the object cgroups vector associated with the page, + * or NULL. This function is safe to use if the page can be directly assoc= iated + * with a memory cgroup. + */ +static inline struct obj_cgroup **page_objcgs_check(struct page *page) +{ + unsigned long memcg_data =3D READ_ONCE(page->memcg_data); + + if (memcg_data && (memcg_data & 0x1UL)) + return (struct obj_cgroup **)(memcg_data & ~0x1UL); + + return NULL; +} + +/* + * 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 + * + * Atomically associates a page with a vector of object cgroups. + */ +static inline bool set_page_objcgs(struct page *page, + struct obj_cgroup **objcgs) +{ + return !cmpxchg(&page->memcg_data, 0, (unsigned long)objcgs | 0x1UL); +} +#else +static inline struct obj_cgroup **page_objcgs(struct page *page) +{ + return NULL; +} + +static inline struct obj_cgroup **page_objcgs_check(struct page *page) +{ + return NULL; +} + +static inline bool set_page_objcgs(struct page *page, + struct obj_cgroup **objcgs) +{ + return true; +} +#endif + static __always_inline bool memcg_stat_item_in_bytes(int idx) { if (idx =3D=3D MEMCG_PERCPU_B) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 3968d68503cb..0054b4846770 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2899,7 +2899,7 @@ int memcg_alloc_page_obj_cgroups(struct page *page, s= truct kmem_cache *s, if (!vec) return -ENOMEM; =20 - if (cmpxchg(&page->memcg_data, 0, (unsigned long)vec | 0x1UL)) + if (!set_page_objcgs(page, vec)) kfree(vec); else kmemleak_not_leak(vec); @@ -2933,12 +2933,12 @@ struct mem_cgroup *mem_cgroup_from_obj(void *p) * Memcg membership data for each individual object is saved in * the page->obj_cgroups. */ - if (page_has_obj_cgroups(page)) { + if (page_objcgs_check(page)) { struct obj_cgroup *objcg; unsigned int off; =20 off =3D obj_to_index(page->slab_cache, page, p); - objcg =3D page_obj_cgroups(page)[off]; + objcg =3D page_objcgs(page)[off]; if (objcg) return obj_cgroup_memcg(objcg); =20 diff --git a/mm/slab.h b/mm/slab.h index e2535cee0d33..9a54a0cb5cca 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -239,28 +239,12 @@ static inline bool kmem_cache_debug_flags(struct kmem= _cache *s, slab_flags_t fla } =20 #ifdef CONFIG_MEMCG_KMEM -static inline struct obj_cgroup **page_obj_cgroups(struct page *page) -{ - /* - * Page's memory cgroup and obj_cgroups vector 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 **)(page->memcg_data & ~0x1UL); -} - -static inline bool page_has_obj_cgroups(struct page *page) -{ - return page->memcg_data & 0x1UL; -} - int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s, gfp_t gfp); =20 static inline void memcg_free_page_obj_cgroups(struct page *page) { - kfree(page_obj_cgroups(page)); + kfree(page_objcgs(page)); page->memcg_data =3D 0; } =20 @@ -322,7 +306,7 @@ static inline void memcg_slab_post_alloc_hook(struct km= em_cache *s, if (likely(p[i])) { page =3D virt_to_head_page(p[i]); =20 - if (!page_has_obj_cgroups(page) && + if (!page_objcgs(page) && memcg_alloc_page_obj_cgroups(page, s, flags)) { obj_cgroup_uncharge(objcg, obj_full_size(s)); continue; @@ -330,7 +314,7 @@ static inline void memcg_slab_post_alloc_hook(struct km= em_cache *s, =20 off =3D obj_to_index(s, page, p[i]); obj_cgroup_get(objcg); - page_obj_cgroups(page)[off] =3D objcg; + page_objcgs(page)[off] =3D objcg; mod_objcg_state(objcg, page_pgdat(page), cache_vmstat_idx(s), obj_full_size(s)); } else { @@ -344,6 +328,7 @@ static inline void memcg_slab_free_hook(struct kmem_cac= he *s_orig, void **p, int objects) { struct kmem_cache *s; + struct obj_cgroup **objcgs; struct obj_cgroup *objcg; struct page *page; unsigned int off; @@ -357,7 +342,8 @@ static inline void memcg_slab_free_hook(struct kmem_cac= he *s_orig, continue; =20 page =3D virt_to_head_page(p[i]); - if (!page_has_obj_cgroups(page)) + objcgs =3D page_objcgs(page); + if (!objcgs) continue; =20 if (!s_orig) @@ -366,11 +352,11 @@ static inline void memcg_slab_free_hook(struct kmem_c= ache *s_orig, s =3D s_orig; =20 off =3D obj_to_index(s, page, p[i]); - objcg =3D page_obj_cgroups(page)[off]; + objcg =3D objcgs[off]; if (!objcg) continue; =20 - page_obj_cgroups(page)[off] =3D NULL; + objcgs[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)); @@ -379,11 +365,6 @@ static inline void memcg_slab_free_hook(struct kmem_ca= che *s_orig, } =20 #else /* CONFIG_MEMCG_KMEM */ -static inline bool page_has_obj_cgroups(struct page *page) -{ - return false; -} - static inline struct mem_cgroup *memcg_from_slab_obj(void *ptr) { return NULL; --=20 2.26.2