Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp110311imw; Tue, 12 Jul 2022 15:43:41 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sdivsZ7HBU6jZ99U+qqhvCTSGLHyst0/CV+cyLkxTC4g/OulfesHoFnBlqNFHJI0ToB4yJ X-Received: by 2002:a17:90b:4d8d:b0:1ef:e3a1:d1bd with SMTP id oj13-20020a17090b4d8d00b001efe3a1d1bdmr6687784pjb.175.1657665820875; Tue, 12 Jul 2022 15:43:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657665820; cv=none; d=google.com; s=arc-20160816; b=bsayFb7lTP9OcUlZcwyfRzjAmng0dBz6B4Gc3dD+aKWy5gw23l/kWPaY8I1jReCU74 obLQqGh2dk6c8Pkz3yPVw0WfwF2nEC9BDY1advVLIMHAJU0FC2nRpSTZjCr2houPp/wr jFHuPvaq2gii77Z6L14LTa0mGD+hhH3O5X42tk9HJCUCV+OGhFuK1jfxXej9LOOM7QyH kOsk9Z0CLo9vzpMw+zwTSbXuJsb/RLpn1G2R2rgLJtHgwBU+XgSy5ggwDUDCo7LSG4F9 u9Xbps8iC3VIQwOXphBieM2ykMrHxnNa5bmNU/7pon4xfu1J5lFavWKbPbOLtTeRgdSg wqTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=V5+SKXw7xQ8pOW3xLBv/UilgsLJssuxU4DHDUS6CbQc=; b=DMu8YDX8PKv67XtnG/C3UswDptJXNtzSDXytyqqH7i2htBQbY6Tmq+FzCxdmaMwPCM C6CIBDk7RCuo6HpZ1n2EI/0AcsvSPWaViJz/zEbkkloUrkvR4ZPf8oRwZn3OiVmYqcGj BnUDD81NlJhIN491b8mTHH3Db0BGLLZdkdbrEjzD6VFNo0tzpeh/KEKD0qYtn/MzSXvw CD9IG9kWZu/Du8hymqVcV2PFeVM5PQpAjv1yzxTxsob6JlcmTwu1ssO+CsO3udeTN6sW 5GTITqyZlh2nX2iflKonYW4necdsCzEESYPOatEOksm59c7mhMVajO8RrER67xNGrsMZ HFEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=M0mF9jUF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t10-20020a056a0021ca00b0052ae60ddfcbsi4099804pfj.374.2022.07.12.15.43.24; Tue, 12 Jul 2022 15:43:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=M0mF9jUF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231599AbiGLWLU (ORCPT + 99 others); Tue, 12 Jul 2022 18:11:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231343AbiGLWLO (ORCPT ); Tue, 12 Jul 2022 18:11:14 -0400 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B87CEC48EF for ; Tue, 12 Jul 2022 15:11:13 -0700 (PDT) Received: by mail-pj1-x1034.google.com with SMTP id z12-20020a17090a7b8c00b001ef84000b8bso589632pjc.1 for ; Tue, 12 Jul 2022 15:11:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=V5+SKXw7xQ8pOW3xLBv/UilgsLJssuxU4DHDUS6CbQc=; b=M0mF9jUFX4FVreFrTePPsEIkrtpYgoEBr1di/wduOo2KoxQ0jSOypHtBVeXIllLvXn yyPgcxKzbCEeqxVvRHjofm7TboLZfOFUWUCMxGANS88cuOSTX+KhXqfzkEMBJ94XciFJ 8kb53RPFjzoQqA1BFWZzbm63AHFLTzbrgTKJLbN6RFG/ZMI8Bt2tsQTq6EdagRCwbdRz WQipvDGM+3aNXKbF7hruJgugaYTATQ0FzpGG38H1dP8ZL1GN+5LTaMHqk7+RFaHwOdx2 cAICVm2/WKiTIDYmlq1sIjtlvsHcztIzdsF5KzgF+24K6i/Ywiph7L+z+IytXUyXSevg MPwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=V5+SKXw7xQ8pOW3xLBv/UilgsLJssuxU4DHDUS6CbQc=; b=JlZLZkA2QngJp4jp++ke0m147phjCThtux23rLFJqJz+DigZty5HoTV21xDRhOtRlC PBXN0rGZAwA+KXzG4f4Qbch4WkRK5aNnB/qD2eyY/qFoHVg3l3SUp0wwUfdpAYw39e+t IN9JLQNv1nj0uJexsihApCzkG6LKTzCnonI2UP9t9kaNb4FoFKk1pY94tVICMBKrnAm3 pqZHi9GTj0o+8paz3wlS+fQPqu42XohDihb9y8n7AiEmIJyNvAvTA5h612VPComCdPl4 yHqyTleEUuMeRv59d+VNi876kE1nIBjg4mf6IELRxQwxlxhOkNWOA7XmhQaqarf2WyvG Hhfg== X-Gm-Message-State: AJIora+RDUE90uvWerfcK1JPv9anFBBWFQnRz5jfRKiURDV42f7A6qr/ XbDAQ2L93E69MFwHhHbU9scKulGjPHqE92CW1RDn2Q== X-Received: by 2002:a17:902:e746:b0:16c:4eb6:915d with SMTP id p6-20020a170902e74600b0016c4eb6915dmr160322plf.106.1657663873107; Tue, 12 Jul 2022 15:11:13 -0700 (PDT) MIME-Version: 1.0 References: <20220711162827.184743-1-roman.gushchin@linux.dev> In-Reply-To: From: Shakeel Butt Date: Tue, 12 Jul 2022 15:11:02 -0700 Message-ID: Subject: Re: [PATCH bpf-next v2] bpf: reparent bpf maps on memcg offlining To: Alexei Starovoitov Cc: Roman Gushchin , bpf , Alexei Starovoitov , LKML Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jul 12, 2022 at 2:49 PM Alexei Starovoitov wrote: > > On Mon, Jul 11, 2022 at 9:28 AM Roman Gushchin wrote: > > > > The memory consumed by a mpf map is always accounted to the memory > > cgroup of the process which created the map. The map can outlive > > the memory cgroup if it's used by processes in other cgroups or > > is pinned on bpffs. In this case the map pins the original cgroup > > in the dying state. > > > > For other types of objects (slab objects, non-slab kernel allocations, > > percpu objects and recently LRU pages) there is a reparenting process > > implemented: on cgroup offlining charged objects are getting > > reassigned to the parent cgroup. Because all charges and statistics > > are fully recursive it's a fairly cheap operation. > > > > For efficiency and consistency with other types of objects, let's do > > the same for bpf maps. Fortunately thanks to the objcg API, the > > required changes are minimal. > > > > Please, note that individual allocations (slabs, percpu and large > > kmallocs) already have the reparenting mechanism. This commit adds > > it to the saved map->memcg pointer by replacing it to map->objcg. > > Because dying cgroups are not visible for a user and all charges are > > recursive, this commit doesn't bring any behavior changes for a user. > > > > v2: > > added a missing const qualifier > > > > Signed-off-by: Roman Gushchin > > Reviewed-by: Shakeel Butt > > --- > > include/linux/bpf.h | 2 +- > > kernel/bpf/syscall.c | 35 +++++++++++++++++++++++++++-------- > > 2 files changed, 28 insertions(+), 9 deletions(-) > > > > diff --git a/include/linux/bpf.h b/include/linux/bpf.h > > index 2b21f2a3452f..85a4db3e0536 100644 > > --- a/include/linux/bpf.h > > +++ b/include/linux/bpf.h > > @@ -221,7 +221,7 @@ struct bpf_map { > > u32 btf_vmlinux_value_type_id; > > struct btf *btf; > > #ifdef CONFIG_MEMCG_KMEM > > - struct mem_cgroup *memcg; > > + struct obj_cgroup *objcg; > > #endif > > char name[BPF_OBJ_NAME_LEN]; > > struct bpf_map_off_arr *off_arr; > > diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c > > index ab688d85b2c6..ef60dbc21b17 100644 > > --- a/kernel/bpf/syscall.c > > +++ b/kernel/bpf/syscall.c > > @@ -419,35 +419,52 @@ 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) > > { > > - map->memcg = get_mem_cgroup_from_mm(current->mm); > > + /* 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(); > > } > > > > static void bpf_map_release_memcg(struct bpf_map *map) > > { > > - mem_cgroup_put(map->memcg); > > + if (map->objcg) > > + 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; > > } > > > > void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags, > > int node) > > { > > - struct mem_cgroup *old_memcg; > > + struct mem_cgroup *memcg, *old_memcg; > > void *ptr; > > > > - old_memcg = set_active_memcg(map->memcg); > > + memcg = bpf_map_get_memcg(map); > > + old_memcg = set_active_memcg(memcg); > > ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node); > > set_active_memcg(old_memcg); > > + mem_cgroup_put(memcg); > > Here we might css_put root_mem_cgroup. > Should we css_get it when returning or > it's marked as CSS_NO_REF ? > But mem_cgroup_alloc() doesn't seem to be doing that marking. > I'm lost at that code. CSS_NO_REF is set for root_mem_cgroup in cgroup_init_subsys().