From mboxrd@z Thu Jan 1 00:00:00 1970 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 AE3F2C00140 for ; Tue, 2 Aug 2022 13:47:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 00A2E8E0002; Tue, 2 Aug 2022 09:47:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EFB538E0001; Tue, 2 Aug 2022 09:47:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DC36B8E0002; Tue, 2 Aug 2022 09:47:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id CDA6F8E0001 for ; Tue, 2 Aug 2022 09:47:40 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 999C881338 for ; Tue, 2 Aug 2022 13:47:40 +0000 (UTC) X-FDA: 79754780280.03.C15E719 Received: from mail-vs1-f49.google.com (mail-vs1-f49.google.com [209.85.217.49]) by imf08.hostedemail.com (Postfix) with ESMTP id A6A87160114 for ; Tue, 2 Aug 2022 13:47:39 +0000 (UTC) Received: by mail-vs1-f49.google.com with SMTP id j2so8096945vsp.1 for ; Tue, 02 Aug 2022 06:47:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc; bh=r0gY8IDHBQw9zV+tm+wIldwERHTU2a9jVP4S6o+Vte4=; b=J/efUqCYkkHHwLdVoM8ZLUajg0xrGGY1WhxJCO7QQ9ZGLZjbteoD1BajhDJn43PcBR 5tipzx9Pug+5//1e53CnvMWHxBvpd0rKVpc8/niitWMYjDlxBi5kB9YnIAFbcaud1wn8 eyEKxfC89VvKaAze52JeiN6tegMoWJf0TidKrRbhHCoYQ/W3mHIdq3AUvZiZsmU1c+4f Bl5FAtdoiGfF9Dpct+ycw2Kb4YaP/JINPTbuk2XHkFhHEW7305Ca6p1DTTKYM8fTRho8 v4eNLKLeX0gEd9VSuK5TZIGE3fXk8qLljyH8aRrOdAWCm4feRM9r2J2ofYVOjjjXV+Zw GFiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=r0gY8IDHBQw9zV+tm+wIldwERHTU2a9jVP4S6o+Vte4=; b=RFQHkQOFKcGrOEKmabgm5ctEqJS+ZIaD0qm2KQ0hbXzSW7O69HvER7VnVdB5q9wlxN Nja8XUQeTzqvh6a6VFltmtb+lc0bjZR0SlJjmRI8QIEg6/aixmLUor+DFKHG+QZFPaZ5 lPO5XZSq63sYGzoDhC7gaidNLrrHaXYaF2d62riaqLW8g26azr1r6Nx6bcVv8H4Mk3cb 5Gal6SRr4lUDRjnElXfAVfVu0CMN7LJIV9laiMNYAzP8UT4a8NvbDp8BguFaftSfJiu0 Vt/U6zMtnpQrc4tC1a35/V1hjxV+EuRazs7Qp0NI745OfXEuDx6CQhXva3BBGCOs/FCU heLg== X-Gm-Message-State: ACgBeo0Ypqf75tb9uewdCTO+lFmy3pMjWsX3JgKkOT0AHDUkNSMYG8nv s+NwLIfHzcqi3wQg4ucb5wJmDhQYkZpvB/QM97Y= X-Google-Smtp-Source: AA6agR4FIozwmF8WXJkvqaJsLvMa6V6YF2nj3uO403m3sxZU6AWbs4TsOYQV/stQMyWl40OOSZqPZFA2NMJhpmaF/hE= X-Received: by 2002:a05:6102:750:b0:381:feef:d966 with SMTP id v16-20020a056102075000b00381feefd966mr4212270vsg.35.1659448058860; Tue, 02 Aug 2022 06:47:38 -0700 (PDT) MIME-Version: 1.0 References: <20220729152316.58205-1-laoar.shao@gmail.com> <20220729152316.58205-16-laoar.shao@gmail.com> <20220802045531.6oi2pt3fyjhotmjo@macbook-pro-3.dhcp.thefacebook.com> In-Reply-To: <20220802045531.6oi2pt3fyjhotmjo@macbook-pro-3.dhcp.thefacebook.com> From: Yafang Shao Date: Tue, 2 Aug 2022 21:47:02 +0800 Message-ID: Subject: Re: [RFC PATCH bpf-next 15/15] bpf: Introduce selectable memcg for bpf map To: Alexei Starovoitov Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin Lau , Song Liu , Yonghong Song , john fastabend , KP Singh , Stanislav Fomichev , Hao Luo , jolsa@kernel.org, Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , netdev , bpf , Linux MM Content-Type: text/plain; charset="UTF-8" ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="J/efUqCY"; spf=pass (imf08.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.217.49 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1659448059; a=rsa-sha256; cv=none; b=3sZuxLRi8cblk92u+MyWZWJI3YOJN6zaHjfqSK/LcftsmudBSVFwFstO8wgL8/fD9oO+MD xuPW1+EuS3lpDmMJ4mf9l/CfmLWpF358Wx0gqv+9DvR8s7IGTeiWA7x+vIP4Rdd21TNfpw Fgl5lIvQNunH1vGGnB38Bcg5ewSte18= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1659448059; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=r0gY8IDHBQw9zV+tm+wIldwERHTU2a9jVP4S6o+Vte4=; b=XIo5MlZe2p+IczGTRuuaaOdtnDjhQJcPsmS+uItvVPcuL+zNAKD2NgompsV033Ze5CZI3a x0DEdZjBtzp+bWsKeejZ4+ArZEjJxAo4tSMZJ3EGEqFLHCbqRF6zvGsLxFJ9fUZRkyBwqW W7PdhMIIuV68Y6rdj+CHXphhsRGilGw= X-Stat-Signature: mdf5zr6onxrwztzmwuxgaqabryuzr8ia X-Rspamd-Queue-Id: A6A87160114 X-Rspam-User: Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="J/efUqCY"; spf=pass (imf08.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.217.49 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam04 X-HE-Tag: 1659448059-619908 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: On Tue, Aug 2, 2022 at 12:55 PM Alexei Starovoitov wrote: > > On Fri, Jul 29, 2022 at 03:23:16PM +0000, Yafang Shao wrote: > > A new member memcg_fd is introduced into bpf attr of BPF_MAP_CREATE > > command, which is the fd of an opened cgroup directory. In this cgroup, > > the memory subsystem must be enabled. This value is valid only when > > BPF_F_SELECTABLE_MEMCG is set in map_flags. Once the kernel get the > > memory cgroup from this fd, it will set this memcg into bpf map, then > > all the subsequent memory allocation of this map will be charge to the > > memcg. > > > > The map creation paths in libbpf are also changed consequently. > > > > Currently it is only supported for cgroup2 directory. > > > > The usage of this new member as follows, > > struct bpf_map_create_opts map_opts = { > > .sz = sizeof(map_opts), > > .map_flags = BPF_F_SELECTABLE_MEMCG, > > }; > > int memcg_fd, int map_fd; > > int key, value; > > > > memcg_fd = open("/cgroup2", O_DIRECTORY); > > if (memcg_fd < 0) { > > perror("memcg dir open"); > > return -1; > > } > > > > map_opts.memcg_fd = memcg_fd; > > map_fd = bpf_map_create(BPF_MAP_TYPE_HASH, "map_for_memcg", > > sizeof(key), sizeof(value), > > 1024, &map_opts); > > if (map_fd <= 0) { > > perror("map create"); > > return -1; > > } > > Overall the api extension makes sense. > The flexibility of selecting memcg is useful. > Thanks! > > Signed-off-by: Yafang Shao > > --- > > include/uapi/linux/bpf.h | 2 ++ > > kernel/bpf/syscall.c | 47 ++++++++++++++++++++++++++-------- > > tools/include/uapi/linux/bpf.h | 2 ++ > > tools/lib/bpf/bpf.c | 1 + > > tools/lib/bpf/bpf.h | 3 ++- > > tools/lib/bpf/libbpf.c | 2 ++ > > 6 files changed, 46 insertions(+), 11 deletions(-) > > > > diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h > > index d5fc1ea70b59..a6e02c8be924 100644 > > --- a/include/uapi/linux/bpf.h > > +++ b/include/uapi/linux/bpf.h > > @@ -1296,6 +1296,8 @@ union bpf_attr { > > * struct stored as the > > * map value > > */ > > + __s32 memcg_fd; /* selectable memcg */ > > + __s32 :32; /* hole */ > > new fields cannot be inserted in the middle of uapi struct. > There's a "#define BPF_MAP_CREATE_LAST_FIELD map_extra" in kernel/bpf/syscall.c, and thus I thought it may have some special meaning, so I put the new field above it. Now that it doesn't have any special meaning, I will change it as you suggested. > > /* Any per-map-type extra fields > > * > > * BPF_MAP_TYPE_BLOOM_FILTER - the lowest 4 bits indicate the > > diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c > > index 6401cc417fa9..9900e2b87315 100644 > > --- a/kernel/bpf/syscall.c > > +++ b/kernel/bpf/syscall.c > > @@ -402,14 +402,30 @@ void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock) > > } > > > > #ifdef CONFIG_MEMCG_KMEM > > -static void bpf_map_save_memcg(struct bpf_map *map) > > +static int bpf_map_save_memcg(struct bpf_map *map, union bpf_attr *attr) > > { > > - /* Currently if a map is created by a process belonging to the root > > - * memory cgroup, get_obj_cgroup_from_current() will return NULL. > > - * So we have to check map->objcg for being NULL each time it's > > - * being used. > > - */ > > - map->objcg = get_obj_cgroup_from_current(); > > + struct obj_cgroup *objcg; > > + struct cgroup *cgrp; > > + > > + if (attr->map_flags & BPF_F_SELECTABLE_MEMCG) { > > The flag is unnecessary. Just add memcg_fd to the end of attr and use != 0 > as a condition that it should be used instead of get_obj_cgroup_from_current(). > There are other parts of bpf uapi that have similar fd handling logic. > Right. There's a ensure_good_fd() to make the fd a positive number. I will change it. > > + cgrp = cgroup_get_from_fd(attr->memcg_fd); > > + if (IS_ERR(cgrp)) > > + return -EINVAL; > > + > > + objcg = get_obj_cgroup_from_cgroup(cgrp); > > + if (IS_ERR(objcg)) > > + return PTR_ERR(objcg); > > + } else { > > + /* Currently if a map is created by a process belonging to the root > > + * memory cgroup, get_obj_cgroup_from_current() will return NULL. > > + * So we have to check map->objcg for being NULL each time it's > > + * being used. > > + */ > > + objcg = get_obj_cgroup_from_current(); > > + } > > + > > + map->objcg = objcg; > > + return 0; > > } > > > > static void bpf_map_release_memcg(struct bpf_map *map) > > @@ -485,8 +501,9 @@ void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, > > } > > > > #else > > -static void bpf_map_save_memcg(struct bpf_map *map) > > +static int bpf_map_save_memcg(struct bpf_map *map, union bpf_attr *attr) > > { > > + return 0; > > } > > > > static void bpf_map_release_memcg(struct bpf_map *map) > > @@ -530,13 +547,18 @@ void *bpf_map_container_alloc(union bpf_attr *attr, u64 size, int numa_node) > > High level uapi struct should not be passed into low level helper like this. > Pls pass memcg_fd instead. > Sure, I will do it. -- Regards Yafang