From patchwork Thu Jan 12 15:53:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13098273 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 D5AA8C63797 for ; Thu, 12 Jan 2023 15:53:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F3ADB8E000B; Thu, 12 Jan 2023 10:53:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E73AC8E0001; Thu, 12 Jan 2023 10:53:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C9EC88E000B; Thu, 12 Jan 2023 10:53:48 -0500 (EST) 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 A02218E0001 for ; Thu, 12 Jan 2023 10:53:48 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 66297120BCA for ; Thu, 12 Jan 2023 15:53:48 +0000 (UTC) X-FDA: 80346592536.19.10A5A26 Received: from mail-qt1-f176.google.com (mail-qt1-f176.google.com [209.85.160.176]) by imf29.hostedemail.com (Postfix) with ESMTP id B8F0F120002 for ; Thu, 12 Jan 2023 15:53:46 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=UwdPEQKp; spf=pass (imf29.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.160.176 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1673538826; 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=rS5gOGm/FV0EomE+G/HxAF0nnvFi+9pGVhYFw6UMCCU=; b=qHZLP9n+x55bPvhpOx2gwuDqHUmHoRgbOn8cYs6lSBRF7+Bd03elC+YTLNAqPC+Bhy+CIw 9/tYYRfP7pywylVHFp7u7qb92h4p/5ySuX7D295kVb4BMuIheX9gqAU6bmaBIwzNMUQuMH 1R4TLyWTST76G93Ob5M/ucb8Gu/Uqz8= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=UwdPEQKp; spf=pass (imf29.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.160.176 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=1673538826; a=rsa-sha256; cv=none; b=YCMuAqqvJxl1Whk7OC0plHWwy+3d7f3ah2gZOZHoqE0SVXi1KQ9q045lyXdE3Wvhlvl0zb L0T2sWo/kwd11anSXDQVJXScyYH9OyLIsGBsOGsVtikonzXdzAOL/Kyw6omg8cLoh2iz54 okhFFyF2tjC+nlVuOmz6bjaE2He0Azs= Received: by mail-qt1-f176.google.com with SMTP id e22so4306061qts.1 for ; Thu, 12 Jan 2023 07:53:46 -0800 (PST) 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:subject:date :message-id:reply-to; bh=rS5gOGm/FV0EomE+G/HxAF0nnvFi+9pGVhYFw6UMCCU=; b=UwdPEQKpeaenmGBhuznEUg2Sgbzj2CbVaC5pOx/wXR0lBhCD+Kmr+Ukcq32XrTVLeX QD5J4wEyXv36kjjM5LJZD2ViCBneCDE+D38KzvcyswB6pmGebWYMatgB/TlTiTBbJVJt 4X/EnkVd1Js9zemKZm9qMUdchkr7bKUQEeIoIcwG+HebxpzFEJ7wnAtwcbpJMOpR3QxL UZcwi5dRYZOX6riW+E4KupYoGQEPruyPGDOYSOkjE/yH8L2ypWd/VEi6SQRHHnZdlRUW IVP5tx2gBV77V9Fjfr9na9W1tsh3ny+CcrEIlk/NIilzOE1vy8vTwWzy6zZ8H7H4nh2D 69RA== 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 :subject:date:message-id:reply-to; bh=rS5gOGm/FV0EomE+G/HxAF0nnvFi+9pGVhYFw6UMCCU=; b=yEufzjCTFT4hv+Cej1DFcvh7IMKk2E+hMXZP/NHNQcuVCzjJrLSSS4j/re0/PzySyd Na1k5LcJBmpbQIE5U2oxFUJ2Ri8yp0PYeMp0MZuYNWP8J3BVDYzR9jUyDQ91KxhkIkK9 iwkjyjHzIp5vR8x5VNhvjgtILkea8cwqJHaakQS2RRN+oFeBpplD9WfNMth12BWE4HQV igPJhiBt7mP4iZzwM+ZoQIIScBl/EHwzHPfEQMuKHm0JDPcpr/v/Is8nyuxmxaCHDaJY GByddRnbDx7yWwaIC5uJrqRJWS+vG7S5rNT9PGiLNr6mOR1BTXeBUWUSq6HztvVSIJW1 NhUA== X-Gm-Message-State: AFqh2kp8NAwD+g5+25NQMhCecXVpdp+4p+KQXQtNAV1nWAbPjkCqO/Uk GK6qd0QUnv36ugV58RNGRb8= X-Google-Smtp-Source: AMrXdXs89lOju7eofF5NQ1SL2Vit25d1eDBdT+g9j1z0eBLF0PzlschIVu6iMcAjd8ub9ViVM50vww== X-Received: by 2002:ac8:5e90:0:b0:3a7:ea9b:5627 with SMTP id r16-20020ac85e90000000b003a7ea9b5627mr13990618qtx.13.1673538825926; Thu, 12 Jan 2023 07:53:45 -0800 (PST) Received: from vultr.guest ([173.199.122.241]) by smtp.gmail.com with ESMTPSA id l17-20020ac848d1000000b003ab43dabfb1sm9280836qtr.55.2023.01.12.07.53.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Jan 2023 07:53:45 -0800 (PST) From: Yafang Shao To: 42.hyeyoo@gmail.com, vbabka@suse.cz, 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, tj@kernel.org, dennis@kernel.org, cl@linux.com, akpm@linux-foundation.org, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, roman.gushchin@linux.dev Cc: linux-mm@kvack.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next v2 09/11] bpf: use bpf_map_kvcalloc in bpf_local_storage Date: Thu, 12 Jan 2023 15:53:24 +0000 Message-Id: <20230112155326.26902-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230112155326.26902-1-laoar.shao@gmail.com> References: <20230112155326.26902-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: B8F0F120002 X-Stat-Signature: qbrkbr4bg7nimbr3fcrh6zktze9ww6io X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1673538826-568721 X-HE-Meta: U2FsdGVkX1/MJ01hPm268CboSd7hykt+6zLPFMiyJg+rKU0PxX5w736CA7OCDw95ueDPgm3C0fgWu3Y0SL74gtp/wVbTonQqGSkcBHGzEYPKa7hgsuYyPydxXXLNXcNcnuIgALQ6MPgmyYG0z9455/HrqjQVCTBsaQVGUBlUKbYawts5IhzvIUj71VvDzY34qPPrWl8q8ze278Cr0U/FuLRvcC4JSTqrJwUrsa8FXhtlTTDT5Rlq3eZ9kq7/FgbAsGdXjdJ52YeCJtshUyS1PxLOeSq/+P8Z0ZyepP1W6Wz4+J+iU4kBpDJmzm0o5BEcbdIMaEEHKuJi+4qUlsVpiNhGoUc+pWrU1qVBrSU5mxy+D4n2M1gSMCsVWNgFrNRCbTB2/xHuEdENZjKPIlSYHLybBkxNvlgQeWtW+pJJOvWNhmGbSvWRgm/DnXZjgxEMNHI3+I7beAVgNzbj3DMus2J8mPvh3DRYo0OH02TqznpV+eAtV9QA39qUtNVTloH65172uFQ/c73sk+KAoxUtcPDZJjCvkK8WhOlpyAEDxjqn1cyOdaMscmD5vk6/OlCRZvBJ5vjstIAvm2P73teBEuGIsMhhfnmxcCz12xGFD6XLLan5+cmnfUXK7GszJVodQoYKWjNYg8gqA93MjpqgOy48O8lTK3/U76+5Cq3Rnadr0EwfUkhejZeWDwRnl34DWpX2xbPKm0Ihuy68haJxOhvNdu/c2niu3Z0JJKHh65IqlNYlE8uXQFhI1zYY53jL9Haf6H9+2wfIOgjXjdC6OhG7fvvrYBzp77/l85rLOYnov6/lVVhhm3VYKbxOAGZFiJQZYc2+FyodA8gXHtXxoExb/R9oEX4BUs2aSAQ2yZJioTAbdr3nwl4SMQLafwI54ZR0Ejb+4UisJu63HqUEZei1YhhU2De6TZ9a2AmsM+mGePOWMTvW+M8ACQbcPC6dSW53K2H3bV6O2EkM+l5 gCbf54YY BLtZBmEniA28IuBrVkiK1UieE4o2YAno4T+t+q7QH2Q86Fw0N0ZkvK+qZajqgEvxC9ZRRcANgAU3mgwqW+9YdMirgfiuDcNsiTyCWhXOokPL297SGnS/6eaFSe5aQGqiRmWAt/YfW1bcw7b+rpeSBLgULONhTjZHQmQ7U9xLEt/h9k3hJsbaNtwJsJsjbHP44B5yRvgNkeI4ifcvE7cjFj+/LJGudbND7PvXzGJGWYGB99SezBr94/5r6gQko9kXFU9XVjJHOivucbT2tlJYZnBFcJA== 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: Introduce new helper bpf_map_kvcalloc() for this memory allocation. Signed-off-by: Yafang Shao --- include/linux/bpf.h | 8 ++++++++ kernel/bpf/bpf_local_storage.c | 4 ++-- kernel/bpf/syscall.c | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index ae7771c..fb14cc6 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1873,6 +1873,8 @@ int generic_map_delete_batch(struct bpf_map *map, void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags, int node); void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags); +void *bpf_map_kvcalloc(struct bpf_map *map, size_t n, size_t size, + gfp_t flags); void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align, gfp_t flags); #else @@ -1889,6 +1891,12 @@ void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, return kzalloc(size, flags); } +static inline void * +bpf_map_kvcalloc(struct bpf_map *map, size_t n, size_t size, gfp_t flags) +{ + return kvcalloc(n, size, flags); +} + static inline void __percpu * bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align, gfp_t flags) diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c index 373c3c2..35f4138 100644 --- a/kernel/bpf/bpf_local_storage.c +++ b/kernel/bpf/bpf_local_storage.c @@ -568,8 +568,8 @@ static struct bpf_local_storage_map *__bpf_local_storage_map_alloc(union bpf_att nbuckets = max_t(u32, 2, nbuckets); smap->bucket_log = ilog2(nbuckets); - smap->buckets = kvcalloc(sizeof(*smap->buckets), nbuckets, - GFP_USER | __GFP_NOWARN | __GFP_ACCOUNT); + smap->buckets = bpf_map_kvcalloc(&smap->map, sizeof(*smap->buckets), + nbuckets, GFP_USER | __GFP_NOWARN); if (!smap->buckets) { bpf_map_area_free(smap); return ERR_PTR(-ENOMEM); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 35ffd80..9e266e8 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -470,6 +470,21 @@ void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags) return ptr; } +void *bpf_map_kvcalloc(struct bpf_map *map, size_t n, size_t size, + gfp_t flags) +{ + struct mem_cgroup *memcg, *old_memcg; + void *ptr; + + memcg = bpf_map_get_memcg(map); + old_memcg = set_active_memcg(memcg); + ptr = kvcalloc(n, size, flags | __GFP_ACCOUNT); + set_active_memcg(old_memcg); + mem_cgroup_put(memcg); + + return ptr; +} + void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align, gfp_t flags) {