Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp89996pxb; Mon, 16 Aug 2021 00:14:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxgO1VAlymKnI5bAz066aVk85zpNM24V7RjsvRTBX1QDuBqhjFtC/DvQ1C7M8H2Gy5yvL2q X-Received: by 2002:a92:6909:: with SMTP id e9mr10854313ilc.231.1629098049268; Mon, 16 Aug 2021 00:14:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629098049; cv=none; d=google.com; s=arc-20160816; b=mFIUsJHk/4x6vwdyT67vYty8rRD7R7IpMPH/T+HV3p5B3iVVnCzkKo3Gzc1LKK11m1 WTEwNwx16h1URsad59ZcAhygO/fanQfk8qAjwuVpBm7LyNz/cbePUua6gFXKvKBrNkQi Z+pebTjkbKHwLoakmRSDvyoI6MHmeTjeA7nRpGeC/xOBaoaQr7szMJPmUxQ5uwtenEc5 Agpvh6o4n0d1ywmEbHuCiBSX6TdQ+bEc25gkSvOao/V2a3WJD9RM4kG7gwH+A8331HmB iEFcLfmOfM/9mi2u/fr9ePD3dmICeS73XYE1NS1Wq5yI3EQcbNRt0m1M0HftozdJg+e+ LWDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=7jrT0wboeRYViLyC0XzkO82L2nxEhRBVHI3KI9D42xA=; b=IjVtkMoONxIJzkgfDV2wkFIMXc+0PmZZJleWcruaKRF44VOjihvBQXk9unQ2HZ/ubi 6II3jhFC78Cp9wnVHzuryrrlBqo7oJINlcVHJLytAh40HN4meozGYr4Oq1/yESvscnY7 ZCKUvOKnp8JiwH7FpQQkvhxKZ+Iw1AtwcNkNXQJtddp/KzEFoTXYBN8KIQlfPYKPZ7gE 2jh8ykwjOlsqRFJQCsVP2r/gQO04zq6MgPhG9i7DMDKDaobXE0HzlDYWU4aLQzv6bK6g iSzPg6YzU6RHumzXro0EZIygNRfMToovHTxzSg7glrVdggKgtdl1vKpeFQ/6ZYfILCf0 0Swg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h199si5118687iof.50.2021.08.16.00.13.58; Mon, 16 Aug 2021 00:14:09 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234234AbhHPHNc (ORCPT + 99 others); Mon, 16 Aug 2021 03:13:32 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:13429 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233985AbhHPHNV (ORCPT ); Mon, 16 Aug 2021 03:13:21 -0400 Received: from dggemv711-chm.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Gp4yn2xmzzdbLZ; Mon, 16 Aug 2021 15:09:05 +0800 (CST) Received: from dggema756-chm.china.huawei.com (10.1.198.198) by dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.1.2176.2; Mon, 16 Aug 2021 15:12:46 +0800 Received: from localhost.localdomain (10.175.112.125) by dggema756-chm.china.huawei.com (10.1.198.198) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2176.2; Mon, 16 Aug 2021 15:12:46 +0800 From: Chen Huang To: Roman Gushchin , Muchun Song , "Wang Hai" , Greg Kroah-Hartman CC: , , , Chen Huang , Andrew Morton , Alexei Starovoitov Subject: [PATCH 5.10.y 03/11] mm: Introduce page memcg flags Date: Mon, 16 Aug 2021 07:21:39 +0000 Message-ID: <20210816072147.3481782-4-chenhuang5@huawei.com> X-Mailer: git-send-email 2.18.0.huawei.25 In-Reply-To: <20210816072147.3481782-1-chenhuang5@huawei.com> References: <20210816072147.3481782-1-chenhuang5@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggema756-chm.china.huawei.com (10.1.198.198) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Roman Gushchin The lowest bit in page->memcg_data is used to distinguish between struct memory_cgroup pointer and a pointer to a objcgs array. All checks and modifications of this bit are open-coded. Let's formalize it using page memcg flags, defined in enum page_memcg_data_flags. Additional flags might be added later. Signed-off-by: Roman Gushchin Signed-off-by: Andrew Morton Signed-off-by: Alexei Starovoitov Reviewed-by: Shakeel Butt Acked-by: Johannes Weiner Acked-by: Michal Hocko Link: https://lkml.kernel.org/r/20201027001657.3398190-4-guro@fb.com Link: https://lore.kernel.org/bpf/20201201215900.3569844-4-guro@fb.com Signed-off-by: Chen Huang --- include/linux/memcontrol.h | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 2805fe81f97d..4a0feb9d4b82 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -343,6 +343,15 @@ struct mem_cgroup { 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 = (1UL << 0), + /* the next bit after the last actual flag */ + __NR_MEMCG_DATA_FLAGS = (1UL << 1), +}; + +#define MEMCG_DATA_FLAGS_MASK (__NR_MEMCG_DATA_FLAGS - 1) + /* * page_memcg - get the memory cgroup associated with a page * @page: a pointer to the page struct @@ -404,13 +413,7 @@ static inline struct mem_cgroup *page_memcg_check(struct page *page) */ unsigned long memcg_data = READ_ONCE(page->memcg_data); - /* - * 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 (memcg_data & 0x1UL) + if (memcg_data & MEMCG_DATA_OBJCGS) return NULL; return (struct mem_cgroup *)memcg_data; @@ -429,7 +432,11 @@ static inline struct mem_cgroup *page_memcg_check(struct page *page) */ static inline struct obj_cgroup **page_objcgs(struct page *page) { - return (struct obj_cgroup **)(READ_ONCE(page->memcg_data) & ~0x1UL); + unsigned long memcg_data = READ_ONCE(page->memcg_data); + + VM_BUG_ON_PAGE(memcg_data && !(memcg_data & MEMCG_DATA_OBJCGS), page); + + return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK); } /* @@ -444,10 +451,10 @@ static inline struct obj_cgroup **page_objcgs_check(struct page *page) { unsigned long memcg_data = READ_ONCE(page->memcg_data); - if (memcg_data && (memcg_data & 0x1UL)) - return (struct obj_cgroup **)(memcg_data & ~0x1UL); + if (!memcg_data || !(memcg_data & MEMCG_DATA_OBJCGS)) + return NULL; - return NULL; + return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK); } /* @@ -460,7 +467,8 @@ static inline struct obj_cgroup **page_objcgs_check(struct page *page) static inline bool set_page_objcgs(struct page *page, struct obj_cgroup **objcgs) { - return !cmpxchg(&page->memcg_data, 0, (unsigned long)objcgs | 0x1UL); + return !cmpxchg(&page->memcg_data, 0, (unsigned long)objcgs | + MEMCG_DATA_OBJCGS); } #else static inline struct obj_cgroup **page_objcgs(struct page *page) -- 2.18.0.huawei.25