From patchwork Wed Aug 10 15:18:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12940668 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06082C00140 for ; Wed, 10 Aug 2022 15:19:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9AB706B0071; Wed, 10 Aug 2022 11:19:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F3216B0080; Wed, 10 Aug 2022 11:19:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6331A8E0001; Wed, 10 Aug 2022 11:19:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 48B796B0071 for ; Wed, 10 Aug 2022 11:19:02 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 3BBE5120602 for ; Wed, 10 Aug 2022 15:18:57 +0000 (UTC) X-FDA: 79784040714.15.85C2DA4 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) by imf14.hostedemail.com (Postfix) with ESMTP id DB08110005B for ; Wed, 10 Aug 2022 15:18:56 +0000 (UTC) Received: by mail-pf1-f176.google.com with SMTP id b133so13999023pfb.6 for ; Wed, 10 Aug 2022 08:18:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=ZnOgYzkZxt9WSL7A8CyXVeY/jkqQttbvdChy+LL/htY=; b=RdpQ34Fx0/VGaTzpvtFOmvGQm29Ipm2n0EBOGETRHgm3A09EU/Tb8O7IXin+zzvmEY PO6QZnDySzHolaWexsbO1IBZsdUkmtWLOTzpu3gJ3yJCFgYjuMfkBHsY7LQ6Q320Ja9o kXdGZ17ElSsWPXl9XcLt+P87a7rEonVkYIttScWZ5tVpGLSgw4r3Q3B1IaydFzkvDmSy XHE8q5rZGfq60l/BXlweZ54O5esZbCdWdiB7dCUd2e2Ut+usySL9vix+BUwtqNnrxUGS RtsdZeWMXd91o9UeL3YQURQ+byH41xnKdPyiwplFrghjMbdaROGzdiU7YelzziDYMQcG N2Ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=ZnOgYzkZxt9WSL7A8CyXVeY/jkqQttbvdChy+LL/htY=; b=L/oAkXU3QkkgpOz9hr5IJN3pO56FXvcMMJTdVfTd5QG++ozhYeYVuwnwfX4HLfOtD9 RzYpBjEZMYoXQg2so/iUue9P5f+xImGotiOd68wg6K1MDL/7c50OZWao8AYq/MzBh/Mc EVegaDyguFQ0FG1KhyxylebAzoaIqVfKeDU9qixqSXa/EnUOE0CjWcRpobGyQA+Pz44e QfAtjfvHof1rjwbsiUIBsOjQXvDqEDbsUQaFLMAzE9DZ7/HRR4UvHTqtYEApLTnaXiUl V/9mh4tKyDErtWpigILTlnoLrJdC5iHdlLtjKUZql5zg5L2kCgyOEBoRJpkmoiYZILUX v8OA== X-Gm-Message-State: ACgBeo2n4aKSCtOWw/miSp5MBTC5Cq/hBUgD0lGL9FtxWR9ATgseDZc0 SsWbW/Y1uy6lxgl0uImoMKM= X-Google-Smtp-Source: AA6agR7T+mXtw1Yp9VTWXrsQVNEQkxkQW0ET8/XsMOMA6C0eOOdQPQx2ixg3OaV1FWIqCJc3hFW1fw== X-Received: by 2002:a63:4c0d:0:b0:41a:77fe:2bc8 with SMTP id z13-20020a634c0d000000b0041a77fe2bc8mr23165203pga.82.1660144735958; Wed, 10 Aug 2022 08:18:55 -0700 (PDT) Received: from vultr.guest ([2001:19f0:6001:5c3e:5400:4ff:fe19:c3bc]) by smtp.gmail.com with ESMTPSA id 85-20020a621558000000b0052b6ed5ca40sm2071935pfv.192.2022.08.10.08.18.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Aug 2022 08:18:55 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, songmuchun@bytedance.com, akpm@linux-foundation.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-mm@kvack.org, Yafang Shao Subject: [PATCH bpf-next 06/15] bpf: Define bpf_map_{get,put}_memcg for !CONFIG_MEMCG_KMEM Date: Wed, 10 Aug 2022 15:18:31 +0000 Message-Id: <20220810151840.16394-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220810151840.16394-1-laoar.shao@gmail.com> References: <20220810151840.16394-1-laoar.shao@gmail.com> MIME-Version: 1.0 ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=RdpQ34Fx; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf14.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660144736; a=rsa-sha256; cv=none; b=OmXI9dowPbFPgqeHBT0MCDJ0mhtHOTLq3T4e6S57kxwXmdKtWPPwnqrmqhuPfnYLKvYQQt 4JGKFmII1JwEQ5ba25jgwc/l3hupxLQqOJZ8rC0J5TydbUUBxaGT+yXXf0jMoxQfgiuH5r 41cK/bP/ifnnwXWrIJJB5kyCw7iUNBE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660144736; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ZnOgYzkZxt9WSL7A8CyXVeY/jkqQttbvdChy+LL/htY=; b=pMcJs8f125yqADIHB2yRqXpkiYg+4yvAGxM1NPZ3n/ELKXHe1sKaFgYoUoQwP3oFSdueVu tTlRAH2WA3bxh//DRg0ptAOBwEDD5VFP5nDHvjkvlRCX7oE3PK9r/w0+nfTAljax3d34/P mrrO6IkL6076W1+lbavIyclwT5J1qoo= X-Rspamd-Server: rspam10 X-Rspam-User: Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=RdpQ34Fx; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf14.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com X-Rspamd-Queue-Id: DB08110005B X-Stat-Signature: 1tk79ogb67deucte3jqswru1cec7fhm4 X-HE-Tag: 1660144736-186478 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: We can use this helper when CONFIG_MEMCG_KMEM or CONFIG_MEMCG is not set. It also moves bpf_map_{get,put}_memcg into include/linux/bpf.h, so these two helpers can be used in other source files. Signed-off-by: Yafang Shao --- include/linux/bpf.h | 29 +++++++++++++++++++++++++++++ include/linux/memcontrol.h | 10 ++++++++++ kernel/bpf/syscall.c | 16 ---------------- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 20c26ae..fe3b565 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -27,6 +27,7 @@ #include #include #include +#include struct bpf_verifier_env; struct bpf_verifier_log; @@ -2571,4 +2572,32 @@ static inline void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype) {} static inline void bpf_cgroup_atype_put(int cgroup_atype) {} #endif /* CONFIG_BPF_LSM */ +#ifdef CONFIG_MEMCG_KMEM +static inline struct mem_cgroup *bpf_map_get_memcg(const struct bpf_map *map) +{ + if (map->objcg) + return get_mem_cgroup_from_objcg(map->objcg); + + return root_mem_cgroup; +} + +static inline void bpf_map_put_memcg(struct mem_cgroup *memcg) +{ + if (mem_cgroup_is_root(memcg)) + return; + + mem_cgroup_put(memcg); +} + +#else +static inline struct mem_cgroup *bpf_map_get_memcg(const struct bpf_map *map) +{ + return root_memcg(); +} + +static inline void bpf_map_put_memcg(struct mem_cgroup *memcg) +{ +} +#endif + #endif /* _LINUX_BPF_H */ diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 9ecead1..2f0a611 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -361,6 +361,11 @@ struct mem_cgroup { extern struct mem_cgroup *root_mem_cgroup; +static inline struct mem_cgroup *root_memcg(void) +{ + return root_mem_cgroup; +} + enum page_memcg_data_flags { /* page->memcg_data is a pointer to an objcgs vector */ MEMCG_DATA_OBJCGS = (1UL << 0), @@ -1138,6 +1143,11 @@ unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order, #define MEM_CGROUP_ID_SHIFT 0 #define MEM_CGROUP_ID_MAX 0 +static inline struct mem_cgroup *root_memcg(void) +{ + return NULL; +} + static inline struct mem_cgroup *folio_memcg(struct folio *folio) { return NULL; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 51ab8b1..19c3a81 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -433,22 +433,6 @@ static void bpf_map_release_memcg(struct bpf_map *map) obj_cgroup_put(map->objcg); } -static struct mem_cgroup *bpf_map_get_memcg(const struct bpf_map *map) -{ - if (map->objcg) - return get_mem_cgroup_from_objcg(map->objcg); - - return root_mem_cgroup; -} - -static void bpf_map_put_memcg(struct mem_cgroup *memcg) -{ - if (mem_cgroup_is_root(memcg)) - return; - - mem_cgroup_put(memcg); -} - void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags, int node) {