From patchwork Fri Jul 29 15:23:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12932535 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 E730DC19F2B for ; Fri, 29 Jul 2022 15:23:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6FEE16B0078; Fri, 29 Jul 2022 11:23:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6AEB46B007B; Fri, 29 Jul 2022 11:23:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 556178E0001; Fri, 29 Jul 2022 11:23:33 -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 4817E6B0078 for ; Fri, 29 Jul 2022 11:23:33 -0400 (EDT) Received: from smtpin31.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 1313A1C6B3E for ; Fri, 29 Jul 2022 15:23:33 +0000 (UTC) X-FDA: 79740506706.31.6EA5BED Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf05.hostedemail.com (Postfix) with ESMTP id 6E49C1000BC for ; Fri, 29 Jul 2022 15:23:32 +0000 (UTC) Received: by mail-pj1-f42.google.com with SMTP id a7-20020a17090a008700b001f325db8b90so4153781pja.0 for ; Fri, 29 Jul 2022 08:23:32 -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=3d+Vnnj0Wn2TotffdpnitiR4y4GtmpBbmAGTyIaTSkM=; b=idobMl1R4oCtQYLGjlYto1s6UlhcTJgFu3NUkjfEi5j6NUZx/r/8NUrkKFKQkM8u9x pEjnY9LgsdKpeXwc5YSZDJmaxyDUa9ivpvl188U8XXHGn5XzMAVN3T9OutqAU7BFa1tB RHLfjrBhaeV/pigJlqITo6VJ6jwgEuSkMlIKHOx6qfNfvTbBGTisqrwfyhKZjcuvTWj2 ue3m7bUU2oS9KYQvKvEEgqk+WIWEySkGfnHoWRYj2vtQlFrR4QQDbLc2GnWwuYyrnj2p /DU304Bsu8zNJH+SRzWorjTy/+gukbTajj1B+VAk4F9X6tWiejJF+TyNBni7hBaYzHZp lgog== 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=3d+Vnnj0Wn2TotffdpnitiR4y4GtmpBbmAGTyIaTSkM=; b=fSAxx2QIA4OH+n3ghirGuWKGG68wXIVcWLq3at+HWb49WLYR7h5OJFnv4ATHO9NCJZ Nz6mtcCXwNDi0tnI1z2Djz/3MMsWG98wTGHjwYtS9JAmQSWEleab/fuuy9Y31M0/U4xh 0+LMIHOuVSXIYlsN7/3NRWHxzYB0WhkDbD/q0TMxP2HQwhNOPHpOeZPiMfIqIY7hzXER XZexiW/CGWJVFWv5HZXJr91PBeTPSECAx41WoP1M8TQp7JnYB3dRtXl+zhYmDyJ0H3PR VjvUcuVMy3awJ9WW16eUiAgANTZgUv2BYN3a+EhFtDut93pltAelo/JG8V+Ltj5hN1Af gqXw== X-Gm-Message-State: ACgBeo2gbBRWgNuyYd4LzewVtSKkEbD8VJvgQhTy79TNDtD/q8RaXFQJ goAY3HZdkSsrp32yOEEAMLiE/P8lTwp8WBFI X-Google-Smtp-Source: AA6agR5ZlXbmQGLf5QjvBmdvX0UV8lkVnYqG2atAs/tSFEhkNBwSchlIEUS8VEdkWcaJra/qvhUN2w== X-Received: by 2002:a17:902:e8c6:b0:16d:ab3a:1d42 with SMTP id v6-20020a170902e8c600b0016dab3a1d42mr4150046plg.28.1659108211481; Fri, 29 Jul 2022 08:23:31 -0700 (PDT) Received: from vultr.guest ([2001:19f0:6001:2912:5400:4ff:fe16:4344]) by smtp.gmail.com with ESMTPSA id b12-20020a1709027e0c00b0016d3a354cffsm3714219plm.89.2022.07.29.08.23.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Jul 2022 08:23:30 -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 Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-mm@kvack.org, Yafang Shao Subject: [RFC PATCH bpf-next 05/15] bpf: Introduce helpers for container of struct bpf_map Date: Fri, 29 Jul 2022 15:23:06 +0000 Message-Id: <20220729152316.58205-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220729152316.58205-1-laoar.shao@gmail.com> References: <20220729152316.58205-1-laoar.shao@gmail.com> MIME-Version: 1.0 ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=idobMl1R; spf=pass (imf05.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.42 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=1659108212; a=rsa-sha256; cv=none; b=BXxjPDI0NbjN8JhCdo48TFlvoiQ8Srk7yxiQaPYSpXgp19FUhZwOoosHGsm5Kdeenu6v3c AzhWciks0l8E/EFqbm0b23utcNSZ6JQPaSbx+0aytG0m3Ms7b0mHGnCrUMfC8jpmrKxyP8 EgPbYxnFbOz2gUJ2EargAaZxpCEuAVM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1659108212; 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=3d+Vnnj0Wn2TotffdpnitiR4y4GtmpBbmAGTyIaTSkM=; b=MqeHV87fvCggGO2MwxwMaG1vPGxrBMOiFpSipAtHLlvvzN/65yqhSYPu3ETCc9bP/e9cfX 1PP/5SjczHFOkQ+8gcgueULeDiU6CZfUsJmtljHdzStXL9aiRjlDj2OIlrLx5aiyIb744G oESJ5STkhrBjfK2syPuD3c7DFETkGG4= Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=idobMl1R; spf=pass (imf05.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspam-User: X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 6E49C1000BC X-Stat-Signature: xb868jncudozymbqnxeyk46xa6uxm6ko X-HE-Tag: 1659108212-804689 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: Currently bpf_map_area_alloc() is used to allocate a container of struct bpf_map or members in this container. To distinguish the map creation and other members, let split it into two different helpers, - bpf_map_container_alloc() Used to allocate a container of struct bpf_map, the container is as follows, struct bpf_map_container { struct bpf_map map; // the map must be the first member .... }; Pls. note that the struct bpf_map_contianer is a abstract one, which can be struct bpf_array, struct bpf_bloom_filter and etc. In this helper, it will call bpf_map_save_memcg() to init memcg relevant data in the bpf map. And these data will be cleared in bpf_map_container_free(). - bpf_map_area_alloc() Now it is used to allocate the members in a contianer only. Signed-off-by: Yafang Shao --- include/linux/bpf.h | 4 ++++ kernel/bpf/syscall.c | 56 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 20c26aed7896..2d971b0eb24b 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1634,9 +1634,13 @@ void bpf_map_inc_with_uref(struct bpf_map *map); struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map); void bpf_map_put_with_uref(struct bpf_map *map); void bpf_map_put(struct bpf_map *map); +void *bpf_map_container_alloc(u64 size, int numa_node); +void *bpf_map_container_mmapable_alloc(u64 size, int numa_node, + u32 align, u32 offset); void *bpf_map_area_alloc(u64 size, int numa_node); void *bpf_map_area_mmapable_alloc(u64 size, int numa_node); void bpf_map_area_free(void *base); +void bpf_map_container_free(void *base); bool bpf_map_write_active(const struct bpf_map *map); void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr); int generic_map_lookup_batch(struct bpf_map *map, diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 83c7136c5788..1a1a81a11b37 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -495,6 +495,62 @@ static void bpf_map_release_memcg(struct bpf_map *map) } #endif +/* + * The return pointer is a bpf_map container, as follow, + * struct bpf_map_container { + * struct bpf_map map; + * ... + * }; + * + * It is used in map creation path. + */ +void *bpf_map_container_alloc(u64 size, int numa_node) +{ + struct bpf_map *map; + void *container; + + container = __bpf_map_area_alloc(size, numa_node, false); + if (!container) + return NULL; + + map = (struct bpf_map *)container; + bpf_map_save_memcg(map); + + return container; +} + +void *bpf_map_container_mmapable_alloc(u64 size, int numa_node, u32 align, + u32 offset) +{ + struct bpf_map *map; + void *container; + void *ptr; + + /* kmalloc'ed memory can't be mmap'ed, use explicit vmalloc */ + ptr = __bpf_map_area_alloc(size, numa_node, true); + if (!ptr) + return NULL; + + container = ptr + align - offset; + map = (struct bpf_map *)container; + bpf_map_save_memcg(map); + + return ptr; +} + +void bpf_map_container_free(void *container) +{ + struct bpf_map *map; + + if (!container) + return; + + map = (struct bpf_map *)container; + bpf_map_release_memcg(map); + + kvfree(container); +} + static int bpf_map_kptr_off_cmp(const void *a, const void *b) { const struct bpf_map_value_off_desc *off_desc1 = a, *off_desc2 = b;