From patchwork Thu Aug 18 14:31:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12948196 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 A1E1BC00140 for ; Thu, 18 Aug 2022 23:55:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 427EA8D0003; Thu, 18 Aug 2022 19:55:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3D7808D0001; Thu, 18 Aug 2022 19:55:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2789E8D0003; Thu, 18 Aug 2022 19:55:38 -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 1ADBF8D0001 for ; Thu, 18 Aug 2022 19:55:38 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E7D87C093C for ; Thu, 18 Aug 2022 23:55:37 +0000 (UTC) X-FDA: 79814373114.14.3EBF0AD Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) by imf08.hostedemail.com (Postfix) with ESMTP id BECF8160A4A for ; Thu, 18 Aug 2022 23:51:48 +0000 (UTC) Received: by mail-pj1-f48.google.com with SMTP id bf22so3112884pjb.4 for ; Thu, 18 Aug 2022 16:51:48 -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=ZuUr0Lmb/ZOxk9PY5AfEZVtv5LOeYubF7wGlzoETmHE=; b=d1Dmx23BuvVjJGEdABIJqux0UqGU7kR857GG+nUy3p5Gl+JFQRPnacYR5C1E11Ofv+ VQzRJXyagtlUyX2PsvUhvwbBEGI5jCo8K1MXP9YC9jrYwQ1ql3E23yGsomtt3aLtLkyG 6jlsslfX7hAUQnLnftrWYWKn1ckbPQdwKng7PSX/hzD0DU1ebls3NA0vJ28f+kCAbJzx Dnbnw+RjYZUrzVphtx6OF+YrhmHnSp3Z5NGEX6Pky5rHNvrD7tahg66442kAz7OJTR+Q Xfox1U4JtTXiJmuNH/IzeI1IAXmH9WYKpr67CkHaEi1FWvYNxiP6kgVw4yJMKC3uCG38 /OMQ== 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=ZuUr0Lmb/ZOxk9PY5AfEZVtv5LOeYubF7wGlzoETmHE=; b=5XrubypFsiZToS7j640cOOsrZUWTf+gXluAdJ0WXx4BgDMniWMGiBLMR04iHndbW0e mcYhRe1P548eXsiTDFGmI/KwFKXVng5jjJ8V3hf3hN9WuHoOur64rPBPlAZ6OT6ZstK9 zVfs2/R4fLoejx0nAQI1xM/fFJycnAWpR8z94InmCTPcEwk3zfzkwhZJfQEVtHDMAbpv OL1Y9+/+Gy3sRh1FpaKLYE57smwFPOTjt+pWHUIrO0H9r/jRxcp50cyxcqb3MeC2xb85 Ex0EpVH+OKWIC7hziZ79Rm6QD9T/LxQ9DIRwXAgJ4Rm+ucpRJGNZ7YQU1zESRny54q3m 2aUw== X-Gm-Message-State: ACgBeo0UkICXBfOmEP3BoqKn/f0JvkUFAzA2OGZfiRCOfbzRek9emnBn 4k8bopcMFtrnR4pYLaLwUMy0aXsgKnFmsoP2 X-Google-Smtp-Source: AA6agR7c8t/UNHEBNrrIWCnH4P2UoAcPiCKseHMm/BYgU8xvPNvxWD6o9PWbAC1k+VmB22rXaSPvww== X-Received: by 2002:a17:902:a5c5:b0:16f:1e31:daab with SMTP id t5-20020a170902a5c500b0016f1e31daabmr2876148plq.82.1660833111438; Thu, 18 Aug 2022 07:31:51 -0700 (PDT) Received: from vultr.guest ([45.32.72.237]) by smtp.gmail.com with ESMTPSA id h5-20020a63f905000000b003fdc16f5de2sm1379124pgi.15.2022.08.18.07.31.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Aug 2022 07:31:50 -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, tj@kernel.org, lizefan.x@bytedance.com Cc: cgroups@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-mm@kvack.org, Yafang Shao Subject: [PATCH bpf-next v2 04/12] bpf: Call bpf_map_init_from_attr() immediately after map creation Date: Thu, 18 Aug 2022 14:31:10 +0000 Message-Id: <20220818143118.17733-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220818143118.17733-1-laoar.shao@gmail.com> References: <20220818143118.17733-1-laoar.shao@gmail.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660866708; 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=ZuUr0Lmb/ZOxk9PY5AfEZVtv5LOeYubF7wGlzoETmHE=; b=pyutS/+ad3Xd2EJ76HXvqqp37COPk3qZCNYTLqBl/K84JACjRs/ESvQ/Yihfc7S+yAR/0f WnncjJ3l1QdscFPOjwGuMaooKre9okcwzs7MQQVl0Oug1C22bp6/G6Gl22rwkdtIaUbXmR E9J9wXW5P1aK7tL2tdHKKSYv+LEehVg= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=d1Dmx23B; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf08.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.48 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660866708; a=rsa-sha256; cv=none; b=HJ188JvFYdLDErX/M2ROM+rWc2mUdqAJ98fGsm4qimwPbpoNyRpbBlk5Bijx/l8ADYUL3X +4uCVee3tBT02Y4N6eqVioa4hlkdkol3VCJYO3TSUGL/+iOHAz3cmkjdkzaUDDJR6CYj6s PQ9hf+iFuxOG8BNHgzd6ZeGPK6xbGyU= Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=d1Dmx23B; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf08.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.48 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: BECF8160A4A X-Stat-Signature: 1re76rix8q7g4qqm5ai1pbgzgfpdgno6 X-Rspam-User: X-HE-Tag: 1660866708-432598 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: In order to make all other map related memory allocations been allocated after memcg is saved in the map, we should save the memcg immediately after map creation. But the map is created in bpf_map_area_alloc(), within which we can't get the related bpf_map (except with a pointer casting which may be error prone), so we can do it in bpf_map_init_from_attr(), which is used by all bpf maps. bpf_map_init_from_attr() is executed immediately after bpf_map_area_alloc() for almost all bpf maps except bpf_struct_ops, devmap and hashmap, so this patch changes these three maps. In the future we will change the return type of bpf_map_init_from_attr() from void to int for error cases, so put it immediately after bpf_map_area_alloc() will make it eary to handle the error case. Signed-off-by: Yafang Shao --- kernel/bpf/bpf_struct_ops.c | 2 +- kernel/bpf/devmap.c | 5 ++--- kernel/bpf/hashtab.c | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c index 84b2d9d..36f24f8 100644 --- a/kernel/bpf/bpf_struct_ops.c +++ b/kernel/bpf/bpf_struct_ops.c @@ -624,6 +624,7 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr) st_map->st_ops = st_ops; map = &st_map->map; + bpf_map_init_from_attr(map, attr); st_map->uvalue = bpf_map_area_alloc(vt->size, NUMA_NO_NODE); st_map->links = @@ -637,7 +638,6 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr) mutex_init(&st_map->lock); set_vm_flush_reset_perms(st_map->image); - bpf_map_init_from_attr(map, attr); return map; } diff --git a/kernel/bpf/devmap.c b/kernel/bpf/devmap.c index f9a87dc..20decc7 100644 --- a/kernel/bpf/devmap.c +++ b/kernel/bpf/devmap.c @@ -127,9 +127,6 @@ static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr) */ attr->map_flags |= BPF_F_RDONLY_PROG; - - bpf_map_init_from_attr(&dtab->map, attr); - if (attr->map_type == BPF_MAP_TYPE_DEVMAP_HASH) { dtab->n_buckets = roundup_pow_of_two(dtab->map.max_entries); @@ -167,6 +164,8 @@ static struct bpf_map *dev_map_alloc(union bpf_attr *attr) if (!dtab) return ERR_PTR(-ENOMEM); + bpf_map_init_from_attr(&dtab->map, attr); + err = dev_map_init_map(dtab, attr); if (err) { bpf_map_area_free(dtab); diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 8392f7f..48dc04c 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -499,10 +499,10 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr) if (!htab) return ERR_PTR(-ENOMEM); - lockdep_register_key(&htab->lockdep_key); - bpf_map_init_from_attr(&htab->map, attr); + lockdep_register_key(&htab->lockdep_key); + if (percpu_lru) { /* ensure each CPU's lru list has >=1 elements. * since we are at it, make each lru list has the same