Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp109245imw; Tue, 12 Jul 2022 15:42:16 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sdyG7HL49UniJEb1SVcNSDyUU87CjX4JiJj2jAhNaw1Shak7TqcK53VVeWcADb7PmlKRH/ X-Received: by 2002:a63:c009:0:b0:415:d072:fb5d with SMTP id h9-20020a63c009000000b00415d072fb5dmr398518pgg.591.1657665736360; Tue, 12 Jul 2022 15:42:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657665736; cv=none; d=google.com; s=arc-20160816; b=THN818EJKRRrp/cXyYJOkskNrtk+62PpaEuvdkonuVCPhHaK64AjXMa4Uk/fbrjij8 LZLiwrJFQqmp7jhpindSjP6tx+aB2ZWD4GqgCNIBp+wvHvYw7/szo6HU2Pj36iUg4bnL ZIG4wPMyvw53iVSWL5zQV72XBB9Lcpizt4Vd3Zucm/lFjoBSf37enXdn8eO5TmS8SXQq zZylXd4+YbB0EbxQxjOGNSbdk2sF+SwQG9ePrOscHsHTjsXfKF7Ch7OnFZxiQ1ryL+f8 w11iSNxAjXWWSKMMcRUaioseR0xViqkkwvV0a9Jf0k2N2vO7hxf2QDx/1JLXZso13bX/ X4NA== 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=Ce9EbG1/r2mPoib1PqC7ad2wqeNTOryA+tq46J/Zn/Y=; b=PgjCfiu3ocdCAKOuHaU+xWY7spm89iXkkuhk/IJZJHkCiUIv3DpMB1vq97HhA8B+1S DcatwxGrsAod+mgSlsmePd1yToUNUub9hItbVNBrKVHno3rAJHbe0tS+c4XCb/Ee9M0f ZL51JnAncn4kLI3o1WBzA9XA+sWY3W1672e+FdSln8sBh14simGiv7hjaBBpkOIjgpbu gP6vQRJm/5Wzmk5IglBswSlnLsZN8KbJaln5fSxsqRQJokwOOHRiDkCFJkWLAnmrPHla bqvSwE8KNU3tc0/CtNKXa0Hw0BsiqQvv2QdwabAaCbhRDGl+Jp73CKaO1JVMaUlPrRUz CFPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="d2U9AN/1"; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q3-20020a170902eb8300b0016a581f9af8si12488819plg.52.2022.07.12.15.41.24; Tue, 12 Jul 2022 15:42:16 -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=@gmail.com header.s=20210112 header.b="d2U9AN/1"; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231230AbiGLWPn (ORCPT + 99 others); Tue, 12 Jul 2022 18:15:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229614AbiGLWPl (ORCPT ); Tue, 12 Jul 2022 18:15:41 -0400 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60DFDB0F92; Tue, 12 Jul 2022 15:15:40 -0700 (PDT) Received: by mail-ej1-x635.google.com with SMTP id sz17so16760721ejc.9; Tue, 12 Jul 2022 15:15:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Ce9EbG1/r2mPoib1PqC7ad2wqeNTOryA+tq46J/Zn/Y=; b=d2U9AN/1kRnlu3pMxoWN6MrYOl39nfb6Hz/V/0RaqPeKLJJnxkT4BrvKYaivISg2Ol H4roundp97p5LDgxQB8+YyY1Xz7TWmtiyaZ1x63d2KIRMDWAQ6un2Sp86+0KaGhU9Dh2 caXqzqyTDTC0JGb9Ia7s6+k0XgF0SPB1EBiBLahchr/ovFOLDajbARThIDrpxM6BNkw1 0wdMtZDAfJ/qpERnCYGyeYtHcD62NIVbltGG1H/71tekoCKJctQj02y/cbT4J3hxfh2f HdOfVmucz/FREg3HWPvQcqV/AeA8ZeYauw+ot8sJ6XF+jY9VSAtbqYM3Z5Wo6JXIWj2o fIaA== 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=Ce9EbG1/r2mPoib1PqC7ad2wqeNTOryA+tq46J/Zn/Y=; b=Czu9hvj4QxiBBy/ZYxSOJAOdY0omQPrBlgBB81ljBHzMeHWyGnbRsI1GkVcKDLP1DJ 8ElmQfSjN87EGJ3YXT9rTSPYH2CsY3Sms7VlXMgU+WLFxwvrJK3yxNrsCyZxoMpV6PYC JOxjgpHwPB/vFKHOyDsrAzp+5Sow5AypaLmrGEVPE8yuAzcixN6ZPobx2I+TwqA50y+t omDcmyTDobIOdClsaI8mJ5xmvqQJWXJVfH/PZZVRkldKIbPjFDLQ4HI8kjOyioKjToQT QpRdw+12aCy0lKPxmShUN7Wm4cYqWbhvf3yBXTTj7j1zuMx4AJPWeC9Q+/cX0mDoPjzS 65ZA== X-Gm-Message-State: AJIora/s3/iTpncvyWQYvNC22z20Hm0h3FP3aTizENpEnp2sXKuvQobO 96x3Vxw/9m9xIR7Xcr7oew2c9vURrLRkpCSgiOs= X-Received: by 2002:a17:906:9bdd:b0:72b:3cab:eade with SMTP id de29-20020a1709069bdd00b0072b3cabeademr310951ejc.58.1657664138953; Tue, 12 Jul 2022 15:15:38 -0700 (PDT) MIME-Version: 1.0 References: <20220711162827.184743-1-roman.gushchin@linux.dev> In-Reply-To: From: Alexei Starovoitov Date: Tue, 12 Jul 2022 15:15:27 -0700 Message-ID: Subject: Re: [PATCH bpf-next v2] bpf: reparent bpf maps on memcg offlining To: Shakeel Butt Cc: Roman Gushchin , bpf , Alexei Starovoitov , LKML Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 3:11 PM Shakeel Butt wrote: > > 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(). Ahh. I see that css = ss->css_alloc(NULL); css->flags |= CSS_NO_REF; now. Thanks.