From patchwork Fri Aug 26 02:44:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12955507 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 4754DECAAA2 for ; Fri, 26 Aug 2022 02:45:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D62CD80012; Thu, 25 Aug 2022 22:45:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D12C180007; Thu, 25 Aug 2022 22:45:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BDC8780012; Thu, 25 Aug 2022 22:45:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id AC74280007 for ; Thu, 25 Aug 2022 22:45:34 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 9090CA06C8 for ; Fri, 26 Aug 2022 02:45:34 +0000 (UTC) X-FDA: 79840202988.06.FAD0C6F Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) by imf11.hostedemail.com (Postfix) with ESMTP id 4C42D40006 for ; Fri, 26 Aug 2022 02:45:34 +0000 (UTC) Received: by mail-pg1-f173.google.com with SMTP id s206so220065pgs.3 for ; Thu, 25 Aug 2022 19:45:34 -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=5QfgEJRNKJb9Ft7vW5lcHKllT7kM9P++zdYvapMUmos=; b=Dt5UAkUDbBjefvOUaA3JwJDmn9c263KT6p0l55eczhC/30hED/rRBGmjSwM0KunqG4 XWSyRDDWW0oi+0b3rG1gG2EULZXUh9E9j/3ppRq5edceNB475Yx3ArA4hEoNXzrVRYmO g38h7PcJo+xdIeR7QCHxdd8km1QFh9SF+1GuLBuVV8HAPYRPnzRpSTFhrWsqFda6wwkt AER/Ggv2GXk0zRUmjXl8rc+/x0PvViOMJMeUcSzRs2c86Y0A3keROwxMXKO2XPA9UwK1 B4W4jazxD64nmx4EIXvj9EH03fG9QfX0eHdzaYuiLrEVZL4vCiprb721PjLG/Vr7MMky NjOA== 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=5QfgEJRNKJb9Ft7vW5lcHKllT7kM9P++zdYvapMUmos=; b=OYjVIO6tp0+Wk2QFAPF9tuzuMJ+g4bC/AM5FGIVHLGuKNpxk6rFyTAqO0qBw1JRO1h yovkHV7SXo0mGmWnGbBQOE+y6r2ijqrwbBAVxsF+WndP1R9YwhTvDBh4Oa5zhllkrY0w nnMovk2p+U8t1lun/Cu9GzJqEq7oBpINvU6zRaaejmK0GoYPswFQndUvInKhhpFMpj6K h6F7rh+Mmxoto15aYCUFHzSLDpQHRyi+tWuT8SftIbMc9LTAVOxi6R8XuikoF5iUtL/m Su/16ZJH/a01zWacOPTp9pOYrR476GVC2Bp6zr+fp5nj/oEJsdJeGnHXi0B015n9iL+M 4Zsg== X-Gm-Message-State: ACgBeo0Swd+lEWBPrmcxtfULFmVeuZvXCOF0RkOoqeDxW12jc6DfsSwv c1k4kIyZdvTPTriMgRF+GAY= X-Google-Smtp-Source: AA6agR6UaEvgCLR+vyotD4u6O6LzVm1i7qrfbDu+HjQg3lGZNJbmdv8TJVzSmCnmZmdlYcZ4lg8wLw== X-Received: by 2002:aa7:940b:0:b0:537:97d1:7897 with SMTP id x11-20020aa7940b000000b0053797d17897mr1941202pfo.26.1661481933321; Thu, 25 Aug 2022 19:45:33 -0700 (PDT) Received: from macbook-pro-3.dhcp.thefacebook.com ([2620:10d:c090:400::5:15dc]) by smtp.gmail.com with ESMTPSA id q1-20020a6557c1000000b0042b5b036da4sm284087pgr.68.2022.08.25.19.45.31 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 25 Aug 2022 19:45:32 -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 v4 bpf-next 15/15] bpf: Introduce sysctl kernel.bpf_force_dyn_alloc. Date: Thu, 25 Aug 2022 19:44:30 -0700 Message-Id: <20220826024430.84565-16-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220826024430.84565-1-alexei.starovoitov@gmail.com> References: <20220826024430.84565-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Dt5UAkUD; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.215.173 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661481934; a=rsa-sha256; cv=none; b=oThKOifSQlk+h/d1khBXm6oPr0QoDaL0FZsDJNJF/aHnhPL4FzVBWiXF4ZLIptA/xZFVrN F4E5yx0YKXlkQnmBFGdQ2f3k0GyRKmNpsqyZ7PamhJP1yJqOe7Sw/QDbnto+D5BiFbjo7F AK6uMUDwKjHsXPneCJVChvZtya6pybw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661481934; 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=5QfgEJRNKJb9Ft7vW5lcHKllT7kM9P++zdYvapMUmos=; b=rFYAGBg/PRqHyPc/fdtNKnwzf3AQLunyfgf6X3FNJCQ2uVbS7cE7HELR14bP8RFxXuQ1dq 7cSbU/aHKzeMlXDcq6cxP5lWtS9VQsfo/GeCHuBho8cCiehwpkCqOvmUSsWb/zkhVD2K+7 LwBqIpaDfFP9PBo07hEi8xuTzB9GeS8= X-Stat-Signature: wnjzo9djygg7us4coaqojn79kff3cw4d Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Dt5UAkUD; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.215.173 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 4C42D40006 X-Rspam-User: X-HE-Tag: 1661481934-700531 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. Acked-by: Kumar Kartikeya Dwivedi 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 074c901fbb4e..5c631244b63b 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -5299,6 +5299,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, + }, { } };