From patchwork Sun Jun 19 15:50:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12886754 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 C5293C433EF for ; Sun, 19 Jun 2022 15:50:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4E41B8D0007; Sun, 19 Jun 2022 11:50:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 494DC8D0006; Sun, 19 Jun 2022 11:50:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E7ED8D0007; Sun, 19 Jun 2022 11:50:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 1694B8D0006 for ; Sun, 19 Jun 2022 11:50:48 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id E49F680689 for ; Sun, 19 Jun 2022 15:50:47 +0000 (UTC) X-FDA: 79595423334.13.27D9B5B Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) by imf09.hostedemail.com (Postfix) with ESMTP id 9B72F1400A8 for ; Sun, 19 Jun 2022 15:50:47 +0000 (UTC) Received: by mail-pj1-f47.google.com with SMTP id x1-20020a17090abc8100b001ec7f8a51f5so4624473pjr.0 for ; Sun, 19 Jun 2022 08:50:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=s0+f5gHUcy4bOB49vr1foYREOsrIfIZFQCWb5SF1+Ms=; b=PosIsDNdyLPWrPtg4PrzsAVZoAXqL2g++Mki03DgUE/qzNvRlT8FvxmWGhIXyf6B2T S+SGm6A/MYaN4Zn5mylTRYZhOedr+gMVV9L3mcYuPJNxgmFgdPDfdhawpUff+Y38Jwya Wiw7zb7ZXfLw1RfhEv7uJtWYkhVtM/nG07kn9Q4PDgpVkpXzuZGjfwVCYD1PCstIVplC 3otx8kHv/cT0JpSNR9jDnw9x6LkkKJEHOLWrGwz/f1QXVprH7lWzBKcAqLQvUO0ZpdFa dez3HpGdPF7mH0pBtpM1IEsYhUhO1zgdUcgI4KeKXlM58/nk/5Lr5fsT/kzxW3ErZPBD 4EqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=s0+f5gHUcy4bOB49vr1foYREOsrIfIZFQCWb5SF1+Ms=; b=VNJ6TDR8QU8dNrZXigaHhaP2Udf287ZA1KwgPVtPkUfc8l6CaqXDTQpxA7J8jAyb1Z 77YT4dFouL5f0rCsnNiFTtDsYSkoBlzmZMnrtXG0a4ht04oGg37hvc35SZWUJVWmVCNt 8Hq4AQPAeD9ilMk1Qop11YR1PT16b/OY/aQfopnQ3VoVWvborjxhlFJAnMTIIR2IgXDf /BGN7sFdaMpO5YPNPdo6qm2E/0AfMcr69GSZ1XSAYyM+6Gf66w/bi6eID75YMnhWMTN8 hjWqGFyFSz5mRPjBe1A7sGZ46d19LtDkpOdqhTKLLTOVyOBpuOz6v3ZMdbnJSbCYsE4O jTjQ== X-Gm-Message-State: AJIora/iIMBbDywgJ0CP0tkkQsBMoYWZXzQyWMxVs3k5uEs40DY0I4yd BR1Z/kV3WxQB1QXAwfU47Ds= X-Google-Smtp-Source: AGRyM1sgtmrxoQfyITPlN/cfCgNmFWaIyPAqg6YjtKB9WTJgFrN1BLBYKOAWHzXeDwKE2MMfkvG0wA== X-Received: by 2002:a17:902:7d8e:b0:162:22ff:495b with SMTP id a14-20020a1709027d8e00b0016222ff495bmr19627141plm.1.1655653846678; Sun, 19 Jun 2022 08:50:46 -0700 (PDT) Received: from vultr.guest ([2001:19f0:6001:2b24:5400:4ff:fe09:b144]) by smtp.gmail.com with ESMTPSA id z10-20020a1709027e8a00b001690a7df347sm6381761pla.96.2022.06.19.08.50.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Jun 2022 08:50:45 -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, quentin@isovalent.com, hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, songmuchun@bytedance.com, akpm@linux-foundation.org, cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, vbabka@suse.cz Cc: linux-mm@kvack.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 02/10] bpftool: Show memcg info of bpf map Date: Sun, 19 Jun 2022 15:50:24 +0000 Message-Id: <20220619155032.32515-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220619155032.32515-1-laoar.shao@gmail.com> References: <20220619155032.32515-1-laoar.shao@gmail.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655653847; a=rsa-sha256; cv=none; b=tuX5/AMBuldmaIjidt/RRe135YJQVjFEMqfhR4515BVOnghQVuMDzZ4LcqryTP2DNplbQK PCEwfICIJRnK5ZeBASXtPl2bGjMNivKcUac0/+sz5OCGQUlcJIdiX3Rz8j+1gBfC1OFsSu fFZmMWP052ZI5yOGqZc9l4WWYIR+SWE= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=PosIsDNd; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf09.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.47 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655653847; 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=s0+f5gHUcy4bOB49vr1foYREOsrIfIZFQCWb5SF1+Ms=; b=qcjHufjVnLHVbhG5z1YH7Y/Qs2zHjcbvxAwPssB8bHSwri0f2RzZAEKLH1c1UVzMs3I9Tj 2dj+TWRVxWhJVWcHg1BDxqKVUapbFD2erlv/xjL1ZXFYq4fRQ3Xd/R2ylCMgXwarYNUgBs /1ZK4NQVjKELXuZJbD1wkK4VECPsTI8= Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=PosIsDNd; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf09.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.47 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com X-Rspam-User: X-Stat-Signature: ti5n4k9im5kx8mork95mn493jb113gfp X-Rspamd-Queue-Id: 9B72F1400A8 X-Rspamd-Server: rspam08 X-HE-Tag: 1655653847-474782 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: bpf map can be charged to a memcg, so we'd better show the memcg info to do better bpf memory management. This patch adds a new field "memcg_state" to show whether a bpf map is charged to a memcg and whether the memcg is offlined. Currently it has three values, 0 : belongs to root memcg -1 : its original memcg is not online now 1 : its original memcg is online For instance, $ bpftool map show 2: array name iterator.rodata flags 0x480 key 4B value 98B max_entries 1 memlock 4096B btf_id 240 frozen memcg_state 0 3: hash name calico_failsafe flags 0x1 key 4B value 1B max_entries 65535 memlock 524288B memcg_state 1 6: lru_hash name access_record flags 0x0 key 8B value 24B max_entries 102400 memlock 3276800B btf_id 256 memcg_state -1 Signed-off-by: Yafang Shao --- include/uapi/linux/bpf.h | 4 +++- kernel/bpf/syscall.c | 11 +++++++++++ tools/bpf/bpftool/map.c | 2 ++ tools/include/uapi/linux/bpf.h | 4 +++- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index e81362891596..f2f658e224a7 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -6092,7 +6092,9 @@ struct bpf_map_info { __u32 btf_id; __u32 btf_key_type_id; __u32 btf_value_type_id; - __u32 :32; /* alignment pad */ + __s8 memcg_state; + __s8 :8; /* alignment pad */ + __u16 :16; /* alignment pad */ __u64 map_extra; } __attribute__((aligned(8))); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 7d5af5b99f0d..d4659d58d288 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -4168,6 +4168,17 @@ static int bpf_map_get_info_by_fd(struct file *file, } info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; +#ifdef CONFIG_MEMCG_KMEM + if (map->memcg) { + struct mem_cgroup *memcg = map->memcg; + + if (memcg == root_mem_cgroup) + info.memcg_state = 0; + else + info.memcg_state = memcg_need_recharge(memcg) ? -1 : 1; + } +#endif + if (bpf_map_is_dev_bound(map)) { err = bpf_map_offload_info_fill(&info, map); if (err) diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index 38b6bc9c26c3..ba68512e83aa 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c @@ -525,6 +525,7 @@ static int show_map_close_json(int fd, struct bpf_map_info *info) jsonw_end_array(json_wtr); } + jsonw_int_field(json_wtr, "memcg_state", info->memcg_state); emit_obj_refs_json(refs_table, info->id, json_wtr); jsonw_end_object(json_wtr); @@ -615,6 +616,7 @@ static int show_map_close_plain(int fd, struct bpf_map_info *info) if (frozen) printf("%sfrozen", info->btf_id ? " " : ""); + printf("\n\tmemcg_state %d", info->memcg_state); emit_obj_refs_plain(refs_table, info->id, "\n\tpids "); printf("\n"); diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index e81362891596..f2f658e224a7 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -6092,7 +6092,9 @@ struct bpf_map_info { __u32 btf_id; __u32 btf_key_type_id; __u32 btf_value_type_id; - __u32 :32; /* alignment pad */ + __s8 memcg_state; + __s8 :8; /* alignment pad */ + __u16 :16; /* alignment pad */ __u64 map_extra; } __attribute__((aligned(8)));