From patchwork Fri Aug 19 21:42:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12949278 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 E161BC32771 for ; Fri, 19 Aug 2022 21:43:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7E8688D0005; Fri, 19 Aug 2022 17:43:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7716C6B0074; Fri, 19 Aug 2022 17:43:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5EAE38D0005; Fri, 19 Aug 2022 17:43:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 499D76B0073 for ; Fri, 19 Aug 2022 17:43:31 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 26C78A1AFA for ; Fri, 19 Aug 2022 21:43:31 +0000 (UTC) X-FDA: 79817669022.19.9DA3228 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf24.hostedemail.com (Postfix) with ESMTP id E4A0D18000B for ; Fri, 19 Aug 2022 21:43:30 +0000 (UTC) Received: by mail-pj1-f42.google.com with SMTP id o14-20020a17090a0a0e00b001fabfd3369cso6008325pjo.5 for ; Fri, 19 Aug 2022 14:43:30 -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=hlhfOcaAISwN7vZVN/qowrmOmS21KIl/i8kYcPwYBWw=; b=Bo7ngEGAgqshf64Hk3UhRCye2nuSJtKiFuIEWtkcQrpI3Bp5Rc0Ivs8kuR2tm/Uqd4 Gq9K4X9mkcd9GoK8DhXBxcwPbDrVnU/IZgkLWZ2ZY3VX3/XqDtPo0PSDXQME6kYZZgy9 SOWxP+QrgzwqNI0KD36PGnUNtRaDAz+5fGRCUvwek0erbNqC9c75KYwk5SUtf55Y6nrV h2qnCbxyWH48zBO8tEMoguAl2Fmh8iIzmNFTTLBxXxBzr4SfhxDkx9DTTdX6FBUw2cbv Q6bcs+bT8f5dNRU4Tjt2soXAl/EaPj6paYfRtmM/zBqPNzW2oYcCQptEQzA3Y0IclSiv u8lQ== 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=hlhfOcaAISwN7vZVN/qowrmOmS21KIl/i8kYcPwYBWw=; b=YemBcmghRjqTz3Z95tFWKSusVznO7Q1DgvXWNjXOUKn3sLxXZZYgLFRFtCo8QhT3z1 OCcZBB7Iu/CfGl0zf7hDCxmq6qhj2DmDVYDH/MuGBr2MOFnlYVn7KOBFHiut9FyDus5o KM5geaMpA56LE1bYK+mn+zdBNAoSVPDNUflSQGojbdyCIrhGK2Hz3gF2EQlqQ3eaRsoa LMq3G5hgYZ/6/0SVb1jD/5awtJtswnIBmLQwslzg4zJs58PKCXAaSzfdYFOLW8N2B9IN 5vDxSn0R7sI9nyASw6K1PU2sbFRN/1z5bM8593y4E+NyGiTk8a4oPOuqLqJvSO3whxYw fGTw== X-Gm-Message-State: ACgBeo1q8Zjak2XJTb0ewT5U+UYptD7mP9/7zTjMPTtnGSoNFdL1aqhz /isq01ImJ6irqaUq+DeySm8= X-Google-Smtp-Source: AA6agR7dFh/IrUInZSWKnwoEMLFomgZNUtWGBrCJIt/X6KdkfB0oFBN+QytwAbcGDFIfY/yhtxOZVQ== X-Received: by 2002:a17:90a:9f96:b0:1fa:b4fb:6297 with SMTP id o22-20020a17090a9f9600b001fab4fb6297mr10349716pjp.80.1660945409961; Fri, 19 Aug 2022 14:43:29 -0700 (PDT) Received: from localhost.localdomain ([2620:10d:c090:500::1:c4b1]) by smtp.gmail.com with ESMTPSA id d135-20020a621d8d000000b0052d4b0d0c74sm3893099pfd.70.2022.08.19.14.43.28 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 19 Aug 2022 14:43:29 -0700 (PDT) From: Alexei Starovoitov To: davem@davemloft.net Cc: daniel@iogearbox.net, andrii@kernel.org, tj@kernel.org, memxor@gmail.com, delyank@fb.com, linux-mm@kvack.org, bpf@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v3 bpf-next 15/15] bpf: Introduce sysctl kernel.bpf_force_dyn_alloc. Date: Fri, 19 Aug 2022 14:42:32 -0700 Message-Id: <20220819214232.18784-16-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220819214232.18784-1-alexei.starovoitov@gmail.com> References: <20220819214232.18784-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660945410; 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=hlhfOcaAISwN7vZVN/qowrmOmS21KIl/i8kYcPwYBWw=; b=Cu/8uQaaae1gzgsRlYZf5yh0fLoxX3tWZ+D1xDu0j374dG3E1IPoV9bhp6+88jBkOKMeEa giyTSYB1uqKGpqWWRzON4jQpDB29SFRsVjABBTc+eGv3seOQrRDWiPb1VELhfKF2MaC25z BbwPMe/ABQEBdqf9+24JcDDUefkrW5U= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Bo7ngEGA; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660945410; a=rsa-sha256; cv=none; b=zAnuFS090/N71qhTckfqDhXbOokcA1JYYvWqbaWhjF0Ik09xZ+MvlcVHqOdYLfUQztXaKi WU24BikWwULs/OS6j6UVUwnAr7TLx/ltxuxFjdoy14d2WQxMvv9w1UxefFiLUyH1KQwYIS Z4SEOn5wYqTvDq0dIzAzkFMlxTZmjWY= Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Bo7ngEGA; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: E4A0D18000B X-Stat-Signature: bn19681b7fmp4qeajc19p9bpffbjgmt6 X-Rspam-User: X-HE-Tag: 1660945410-711894 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: From: Alexei Starovoitov Introduce sysctl kernel.bpf_force_dyn_alloc to force dynamic allocation in bpf hash map. All selftests/bpf should pass with bpf_force_dyn_alloc 0 or 1 and all bpf programs (both sleepable and not) should not see any functional difference. The sysctl's observable behavior should only be improved memory usage. Signed-off-by: Alexei Starovoitov --- include/linux/filter.h | 2 ++ kernel/bpf/core.c | 2 ++ kernel/bpf/hashtab.c | 5 +++++ kernel/bpf/syscall.c | 9 +++++++++ 4 files changed, 18 insertions(+) diff --git a/include/linux/filter.h b/include/linux/filter.h index a5f21dc3c432..eb4d4a0c0bde 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -1009,6 +1009,8 @@ bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk, } #endif +extern int bpf_force_dyn_alloc; + #ifdef CONFIG_BPF_JIT extern int bpf_jit_enable; extern int bpf_jit_harden; diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 639437f36928..a13e78ea4b90 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -533,6 +533,8 @@ void bpf_prog_kallsyms_del_all(struct bpf_prog *fp) bpf_prog_kallsyms_del(fp); } +int bpf_force_dyn_alloc __read_mostly; + #ifdef CONFIG_BPF_JIT /* All BPF JIT sysctl knobs here. */ int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON); diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 89f26cbddef5..f68a3400939e 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -505,6 +505,11 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr) bpf_map_init_from_attr(&htab->map, attr); + if (!lru && bpf_force_dyn_alloc) { + prealloc = false; + htab->map.map_flags |= BPF_F_NO_PREALLOC; + } + if (percpu_lru) { /* ensure each CPU's lru list has >=1 elements. * since we are at it, make each lru list has the same diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 850270a72350..c201796f4997 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -5297,6 +5297,15 @@ static struct ctl_table bpf_syscall_table[] = { .mode = 0644, .proc_handler = bpf_stats_handler, }, + { + .procname = "bpf_force_dyn_alloc", + .data = &bpf_force_dyn_alloc, + .maxlen = sizeof(int), + .mode = 0600, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_ONE, + }, { } };