From patchwork Fri Feb 10 15:47:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13135959 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 F3D8BC636D7 for ; Fri, 10 Feb 2023 15:48:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 902EE6B0155; Fri, 10 Feb 2023 10:48:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 88C6C6B0157; Fri, 10 Feb 2023 10:48:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7062B6B0158; Fri, 10 Feb 2023 10:48:03 -0500 (EST) 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 5DB086B0155 for ; Fri, 10 Feb 2023 10:48:03 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 379F2C0247 for ; Fri, 10 Feb 2023 15:48:03 +0000 (UTC) X-FDA: 80451813246.25.375CBAD Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) by imf04.hostedemail.com (Postfix) with ESMTP id 609EB4000C for ; Fri, 10 Feb 2023 15:48:01 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Jn96c5OS; spf=pass (imf04.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.171 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=1676044081; 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=KSCoSnxCH+oujuHlV2ueRvXQv9wOk6fb9r3EfXFy+Xw=; b=QcyDcIHWHFqoxC/pMc258Xl1iOEeMCA5+N5t0olIhV0HQoIlpCweNzKhVQ38pgpK5WnelS tdAl10kPdJGrDRGUKG2hJeAlKaolvR3RavFHk+xfC3uG2jqEo5cLO+C17hy2DgMG1umC5d a5PxYl8IIEMSoQXd0hAJo8xx0HKFeTA= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Jn96c5OS; spf=pass (imf04.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.171 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=1676044081; a=rsa-sha256; cv=none; b=Np6Gaijw2hWcz5LLH7WatwGKJ+NkuSq3uffkQz/yzuIqLew4TOhAGw9eKbgC9fc+IRZAjZ Tzq9TIW7RJuleweSOqcfOT1VyuzRKBiD0IDaNW/j4A3+5Pb9DJTuhCbJXdMx0QqOxDr7qf qvn9QqNX+QwXnH/Sb5E3r/t20LL6O5U= Received: by mail-pf1-f171.google.com with SMTP id g9so3721738pfo.5 for ; Fri, 10 Feb 2023 07:48:01 -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=KSCoSnxCH+oujuHlV2ueRvXQv9wOk6fb9r3EfXFy+Xw=; b=Jn96c5OSkEZ79jtvvUAgxCfdeK/vT5MXjAismyeWwqGOzgWUC7tOQxF5/wxGDKBd1+ NQDqmadFBtS46XsKBvPQanuRJ4CwVD32afSccI431oQcKgoPI4yakO5ePhv3luG5q4Kq G1OmhSBaxjhnTpnCLISjIYzOyY4hc8swEO+5XOnMEsBhV8Ya3KN0EuX4XEAqdxIK1Qux u/rEUAhudYR05Nnyx77XVHNXBp+2OJQuOwOWJb7EYssyfW/YRSbb/a/Nj8C/Epr16s8S l5ToX2SA9BGj3aEmQ8g2629uZDbs5iDhJl7GvAh40gqex9vmEEaTyV1bl6WUkifgR+Ui sbUw== 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=KSCoSnxCH+oujuHlV2ueRvXQv9wOk6fb9r3EfXFy+Xw=; b=I6OdMfByeblFWPj4OEH6cFTfkflxBfY6FvcZBxvVu563J46TXVCOcPDokH0WM9re+4 hCMdDVS0gpCOFR2E/S/N9ruh9plbxjJ9p2h0UFHhoWoyDW7eDLzF52YnK0Jce7Hv1nfr YNlkeHvw6YtxaC3bmIeFPPBS3KiaITbGcStRNoH5WIRaKpDRvmznLgqV3cUZ3YBdOgb6 nktub8Ea+1QMgWe1i4FPJaCJ0Fl67EHb9eFguNwQWPpOWk+pbO2PVm2SEDkFzbpnKb6c CnkDQi1OOibom+qsPNi1Bbb1VLVX0kE5c86ZDtsdZ/LFbq+9M3hJpn+02Kv09sDdxFFb InsQ== X-Gm-Message-State: AO0yUKUN/BP7LZk+hdo/lczme3JX+SBNxk9fnvfip8SJlFRx+n3fWPDr DDx/CDGcETvnOOt/fYtqoRk= X-Google-Smtp-Source: AK7set+0d0W5Q0AkiHpDwGMIBAsDbfXJdRCxZFFceps6/hPlTFfDvxf+o5NgKqtSmcIekhrFGRYC2A== X-Received: by 2002:a62:8490:0:b0:5a8:4b27:5db1 with SMTP id k138-20020a628490000000b005a84b275db1mr8495229pfd.3.1676044080310; Fri, 10 Feb 2023 07:48:00 -0800 (PST) Received: from vultr.guest ([2001:19f0:7001:2f6a:5400:4ff:fe4c:e050]) by smtp.gmail.com with ESMTPSA id t20-20020aa79394000000b005921c46cbadsm3520069pfe.99.2023.02.10.07.47.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Feb 2023 07:47:59 -0800 (PST) From: Yafang Shao To: tj@kernel.org, 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, muchun.song@linux.dev, akpm@linux-foundation.org Cc: bpf@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Yafang Shao Subject: [PATCH bpf-next v2 3/4] bpf: allow to disable bpf map memory accounting Date: Fri, 10 Feb 2023 15:47:33 +0000 Message-Id: <20230210154734.4416-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230210154734.4416-1-laoar.shao@gmail.com> References: <20230210154734.4416-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 609EB4000C X-Stat-Signature: 8nbhmsnp31oi4q88i4jw5fonqzmts7mc X-HE-Tag: 1676044081-358307 X-HE-Meta: U2FsdGVkX1+rvhasTJRpZ7B5O0rJq8eJQ0FAO8K4dVG/eVx8B2E795BUF42bgn5BYZWi6vi1029sZ+dgHEu9+cBKf3zxktsXR7g/6yO5HNOyceXpj5Usan0tWvKWMBnDAR8XfAytBIUi9CUTRHM5Kh3XxeGoYLwad+RW5EgUf6rnvKQfU9J35C1vpUmuumIagMPuWPz2pnxDh4W0uRC7r3lchRLAZO8waIAf+RLWloT0MlKjgm6tilDCEJYLjghrPn6F0ovUp3/I7PZpk5UXCK7Jacml/6/fmY4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eTKNzCkXne2JYcHnqqQoDVHTpjFmcKEQLUQ8xSZsdavS8WHUaqxXWaX2F4zfWN1xdnemlbprMraGaEphOkfoDj3WLSrmoau9JwH63aTWti40DKvbG0AoD/clR/Yu6UH9K+iZxqsRPEv26leQYUoZ35bUApEOV0VLA9aVutcNhSZFrD0568no/pCSD0sPGeMwkmCwSBbxXmnDSffHhuT7xUmCOccu0= 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: We can simply set root memcg as the map's memcg to disable bpf memory accounting. bpf_map_area_alloc is a little special as it gets the memcg from current rather than from the map, so we need to disable GFP_ACCOUNT specifically for it. Signed-off-by: Yafang Shao Acked-by: Johannes Weiner Acked-by: Roman Gushchin --- include/linux/bpf.h | 8 ++++++++ kernel/bpf/memalloc.c | 3 ++- kernel/bpf/syscall.c | 5 +++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index fe0bf48..4385418 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -28,6 +28,7 @@ #include #include #include +#include struct bpf_verifier_env; struct bpf_verifier_log; @@ -2933,4 +2934,11 @@ static inline bool type_is_alloc(u32 type) return type & MEM_ALLOC; } +static inline gfp_t bpf_memcg_flags(gfp_t flags) +{ + if (memcg_bpf_enabled()) + return flags | __GFP_ACCOUNT; + return flags; +} + #endif /* _LINUX_BPF_H */ diff --git a/kernel/bpf/memalloc.c b/kernel/bpf/memalloc.c index ebcc3dd..6da9051 100644 --- a/kernel/bpf/memalloc.c +++ b/kernel/bpf/memalloc.c @@ -395,7 +395,8 @@ int bpf_mem_alloc_init(struct bpf_mem_alloc *ma, int size, bool percpu) unit_size = size; #ifdef CONFIG_MEMCG_KMEM - objcg = get_obj_cgroup_from_current(); + if (memcg_bpf_enabled()) + objcg = get_obj_cgroup_from_current(); #endif for_each_possible_cpu(cpu) { c = per_cpu_ptr(pc, cpu); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 9d94a35..cda8d00 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -309,7 +309,7 @@ static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable) * __GFP_RETRY_MAYFAIL to avoid such situations. */ - const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT; + gfp_t gfp = bpf_memcg_flags(__GFP_NOWARN | __GFP_ZERO); unsigned int flags = 0; unsigned long align = 1; void *area; @@ -418,7 +418,8 @@ static void bpf_map_save_memcg(struct bpf_map *map) * So we have to check map->objcg for being NULL each time it's * being used. */ - map->objcg = get_obj_cgroup_from_current(); + if (memcg_bpf_enabled()) + map->objcg = get_obj_cgroup_from_current(); } static void bpf_map_release_memcg(struct bpf_map *map)