From patchwork Sun Feb 5 06:58:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13128981 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 86BA9C636CC for ; Sun, 5 Feb 2023 06:58:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 25B576B0078; Sun, 5 Feb 2023 01:58:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 20B976B007B; Sun, 5 Feb 2023 01:58:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0D3436B007D; Sun, 5 Feb 2023 01:58:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id F260D6B0078 for ; Sun, 5 Feb 2023 01:58:44 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 79C221C5FD2 for ; Sun, 5 Feb 2023 06:58:44 +0000 (UTC) X-FDA: 80432335368.29.370F71E Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) by imf21.hostedemail.com (Postfix) with ESMTP id AB3601C0003 for ; Sun, 5 Feb 2023 06:58:41 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=gVVFqTvJ; spf=pass (imf21.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.182 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=1675580321; 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=PJFamjlYxQHTtnnrxr9/CB6pQuqIvvtPFL7L8VSX2/I=; b=7G+ZRQvP6K4RzbvZDAzPagUIEBd28B17sXdnHtvaV+CKWk7DsQEQN24Y4CwFoPgFH50qWL ah5L6oA63rcOL3hmydEC0XwG4eQBQLT+YhiL/9dlVq3gwFN5DT8hDn2iPRnTW0Y14Ze++s uizIjbMC78vLdJWA/8A85ic/ucGBkJ8= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=gVVFqTvJ; spf=pass (imf21.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.182 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=1675580321; a=rsa-sha256; cv=none; b=cZOPVudd3b6NnsOWyFZhmX/P9u2t+3jLFCYXnHdjom9M407mI0xm+jp7vNMyNDKEBvt96L 8XggevBf1XnCNLG+nM300LsyuvVvgbQGR8aTeSzCAcuCVeIxBr/iZasYtIPOEQCbs5rj9R RhInVH6+kwSNJHoNdG0OUeWnW/0ccSE= Received: by mail-pg1-f182.google.com with SMTP id 78so6242090pgb.8 for ; Sat, 04 Feb 2023 22:58:41 -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=PJFamjlYxQHTtnnrxr9/CB6pQuqIvvtPFL7L8VSX2/I=; b=gVVFqTvJAZvfk8KKjegWy9aL927a4bXCh4S7/MsZ+dJ0CQpxeUSN6EnF7tyCklTTVQ OVsq1QzMjsCmcd4BOylXTpoB3f2DZmVqjGVsbDRHa5Lie0k/KIbmAGfFc/MYK1Jdcvp8 kXbE1kfU4oELctIwFWc7kLsfhakbRJ8kr6itCLvgz2YzqPxDbiE1XUY/QiK1L4g+zGnf eI6yV7bDEnOzZWom77cfc2Ly8cBIzfy+2ALeyzmgSBVV37qlT46yiT8rx5iPwXyumGu9 Ts4yaKM1P5BfT/IhvtQg1f+x7ANzPnIYfqWkXtcAVNgbvrPeUMWaS7CyzW7PnviXTyq1 BwOA== 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=PJFamjlYxQHTtnnrxr9/CB6pQuqIvvtPFL7L8VSX2/I=; b=lhY4W7wS7blwfkbqL7OGCwrR+kB6h5RStWWv40RJ2jnjPJx9hIbE0J2frc99edTBzu zMXcBYjgbnJQyVL3yqPSE4VVhiTxMWpzmwEYu3coptwmmSAsYnlSs8TTirILREtHVQPe 0jaSQ0owPRgs7VL4C7QKNLjjDxeycktKCtFR8XGYYVzMA44+8t5dREg/pAKhc2tBrcfC /X81INcLF/vr4t8FTUi1eMelLCpD1vqnSqBiR256b7Li9XDlwyXzJwy9mAVG4+3yclm2 e4uGMlNiT48FxvYCL11pss8OT90ziEEeiIMsKx/3whWT80yS5tKWwgd9NDsWdIafzfbj e8Mw== X-Gm-Message-State: AO0yUKUGBjlkm5QeCFZrwU64O55wbPYyFpL7VDlxzftdMV0idbCYxmHo WuiSMNEQYIh34xWMBSlGFGA= X-Google-Smtp-Source: AK7set+glF6tWjUrkVQNHbTTC350mIZnSUIpc9KCsFFirDloxXVTbOwqRd1Jx1RWYbBp3T31d8U1EA== X-Received: by 2002:a05:6a00:1626:b0:586:b33c:be2 with SMTP id e6-20020a056a00162600b00586b33c0be2mr13886223pfc.26.1675580320593; Sat, 04 Feb 2023 22:58:40 -0800 (PST) Received: from vultr.guest ([2401:c080:1c02:6a5:5400:4ff:fe4b:6fe6]) by smtp.gmail.com with ESMTPSA id 144-20020a621596000000b00593ce7ebbaasm4596114pfv.184.2023.02.04.22.58.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Feb 2023 22:58:40 -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 4/5] bpf: allow to disable bpf map memory accounting Date: Sun, 5 Feb 2023 06:58:04 +0000 Message-Id: <20230205065805.19598-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230205065805.19598-1-laoar.shao@gmail.com> References: <20230205065805.19598-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: AB3601C0003 X-Stat-Signature: dom5hikj6ripzmj6rrbjxiexx7eo1tnb X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1675580321-426911 X-HE-Meta: U2FsdGVkX19v67ehUXMfrEKSYxnWCVemSobGLV/wmp2bTI2laxh1zOy87uOhq70SJvwEnJyuKWkEALSETKzx3uNsUkBGoYbdF5R45lMCdQ47FwXK827z27tBeNP+8tYsZOGiuIrYp4C2SwoUY9XE6scsuVQg9R/hMf3uFDgljWk7qNnvQRQEalX796wr0V5amQelN9nl69ltdPOm/eTkzFwWnNFnHbb8W1j9oJpAbw8I9xitvfqoAXhaHyHzRbSiJPI4PxNIaWQdaHNBuQtsc+MmFjZ+2fID+G3E0ub6h3qFaxswqxWo0tI8Zs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NzN1myK09X16yOQUzN56zmLNS9qApySGX33VKTZFeP1AqSCDKonukCzuA1ApaE/+HP8N1oUBAM= 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 --- kernel/bpf/memalloc.c | 3 ++- kernel/bpf/syscall.c | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) 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)