From patchwork Wed Sep 21 16:59:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12984022 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 D1103C6FA82 for ; Wed, 21 Sep 2022 17:00:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 675FF6B0071; Wed, 21 Sep 2022 13:00:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 624F66B0072; Wed, 21 Sep 2022 13:00:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4C6A0940007; Wed, 21 Sep 2022 13:00:10 -0400 (EDT) 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 3C96D6B0071 for ; Wed, 21 Sep 2022 13:00:10 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 1922280E71 for ; Wed, 21 Sep 2022 17:00:10 +0000 (UTC) X-FDA: 79936705380.14.294C2D2 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) by imf06.hostedemail.com (Postfix) with ESMTP id C3E59180021 for ; Wed, 21 Sep 2022 17:00:09 +0000 (UTC) Received: by mail-pg1-f182.google.com with SMTP id r23so6528487pgr.6 for ; Wed, 21 Sep 2022 10:00:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date; bh=nx1Pb9dM/JhxWQDw5gPMNf7lql+4Go4MShpr0kH8cc0=; b=CSmmdZMqYw079U3775ip77HfU+VXCgRduxH7nLU4fKoetQbDbOwQj0N5VV1/R8yDTB C5GMBqy47IWpmoTOMmzVq61LzA369UyUVSLinzDwi61L1Axo/PtoSDz2JbP4bzVDzaWl 1yV5Q4v2g2Z3JLxsorCYmgdPG5ImO6aspZeVppBKF97s56qvy400HqJMer+UI3a14YRR gSkLqNo0MAdXwSJHLuriZIstp04xsabXLTTzSVuR4NtzxdYn7TiL3vyQmHPZCYD9N5AT 3qL2QoEN7swf8pRaRkA/ygy+yZTv4hx4oPBNO5o+2cICKWm6vzzgWsev4IqCrVliZOvV rnBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=nx1Pb9dM/JhxWQDw5gPMNf7lql+4Go4MShpr0kH8cc0=; b=upKen+FxIxojpfKZycVwdkwpRmWqwQU4wPM3bXNyZ5J/TLsZZ2KZa4ZVVbCJgD9gcK 9dZqII7XA/uo8mnDk2enNB+R+AT/1LePpHNdyfGU7h6lq4ZEaqHNOl6QxV/xw//Gv4AM u0+eAvT7rTxJm3alugfRMRdZ+vjtxAMnGnFSxXL+SKDUI4TuaXwRN0mt72QV2XK9gb7f XaU4RAiGcCPD/NI+LgzJoahmPOo8VxXB+sSZsVcZtYcSntnI0R0Rbf7DemjMGYjFaXa7 c7+3HOKfkPh2zYg8pHSDlypW+rdiitjwerq38fvRZYRRMicE7V1uJdUtMADWUWNrOmDM m3+Q== X-Gm-Message-State: ACrzQf1+rgA2droNdQ2tAdx4++C4De7UcGAZgCkBNuMKdfpwHomUosq1 u/Omzul4Q8B/GLUFOYJ5XC0= X-Google-Smtp-Source: AMsMyM5lFjvc00By5S97lTbkK6aQKp+jRrL0ah3mK3K52ySB+gICbHi4wmo/VDJyZQSp3t7PQblKqg== X-Received: by 2002:a05:6a00:248b:b0:542:6ae2:24d5 with SMTP id c11-20020a056a00248b00b005426ae224d5mr30838022pfv.65.1663779608600; Wed, 21 Sep 2022 10:00:08 -0700 (PDT) Received: from vultr.guest ([2001:19f0:6001:488e:5400:4ff:fe25:7db8]) by smtp.gmail.com with ESMTPSA id mp4-20020a17090b190400b002006f8e7688sm2102495pjb.32.2022.09.21.10.00.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Sep 2022 10:00:07 -0700 (PDT) From: Yafang Shao To: 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, songmuchun@bytedance.com, akpm@linux-foundation.org, tj@kernel.org, lizefan.x@bytedance.com Cc: cgroups@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-mm@kvack.org, Yafang Shao Subject: [RFC PATCH bpf-next 00/10] bpf, mm: Add a new item bpf into memory.stat for the observability of bpf memory Date: Wed, 21 Sep 2022 16:59:52 +0000 Message-Id: <20220921170002.29557-1-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663779609; a=rsa-sha256; cv=none; b=vky9OLAdniBaK083+3q1MWxg4WPFYd2ArGrWUy8mkfxIyScxfXgljK29gNya1X4FwU7tN4 +YVM3D1EPCzPCpqPli+gLMJDiqM4FbBzzmXCfR5AIeJ09xjfHcS9TiGNJV9uJfF4/O3vwR Zba6lUHSqT6/zbNm+SOCZzurhQ8OXKE= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=CSmmdZMq; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663779609; 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:references:dkim-signature; bh=nx1Pb9dM/JhxWQDw5gPMNf7lql+4Go4MShpr0kH8cc0=; b=Bgge+8rIRcE1GWm03ZWbEEQ6Te5GTMcI0mADsFxKE9PDhYpJ3gxLBaqrX7WyhOwqQqz4Dx J6iA8dKpvWdClqCiV/w2q0QduISNF5GhVAiA26fPIkRSSn17VuzgqL+ohL4xpSgfp3+y9p dSpacDVowxwy84A8egLGfKhbYOKwKAs= X-Rspam-User: X-Stat-Signature: q7manrhkthrmmzsy3uy14fwwzu8zga7f X-Rspamd-Queue-Id: C3E59180021 Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=CSmmdZMq; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com X-Rspamd-Server: rspam07 X-HE-Tag: 1663779609-544932 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: This patchset adds a new item 'bpf' into memory.stat to show the bpf memory usage in each memcg (except the root memcg because kmem is not charged into root memcg now). The bpf memory usage is not trivial, so it deserves a new item. Patches #1 ~ #8 are from another series[1] which try to fix the pinned bpf issues, but it seems to be a long way to go. So let's do the observability first, which has been in my local repo for a long time. We use the scope-based accouting/unaccouthing to track the bpf memory usage, which is similar with the way how we charge bpf memory. We have to annotate both allocations and releases of bpf memory because we don't want to add something into struct page currently. The allocations and releases of bpf memory are very clear, so it won't be a trouble. This patchset only tracks the memory of bpf-map currently. Future works: - track the memory of bpf-prog - observe system-wide bpf memory usage by adding this item into root memcg - per-map and per-prog bpf memory usage in bpftool or something else - give user an option to disable memcg-based bpf accouting [2] Any feedback is welcomed. [1]. https://lore.kernel.org/bpf/20220902023003.47124-1-laoar.shao@gmail.com/ [2]. https://lore.kernel.org/bpf/CALOAHbAOkUpDWaL2kP8ntBe6sj8S0thLmAwZXhG5kFKBunHt_w@mail.gmail.com/T/#m3597928c7161b206cb9218c80d9e58a42128d31a Yafang Shao (10): bpf: Introduce new helper bpf_map_put_memcg() bpf: Define bpf_map_{get,put}_memcg for !CONFIG_MEMCG_KMEM bpf: Call bpf_map_init_from_attr() immediately after map creation bpf: Save memcg in bpf_map_init_from_attr() bpf: Use scoped-based charge in bpf_map_area_alloc bpf: Introduce new helpers bpf_ringbuf_pages_{alloc,free} bpf: Use bpf_map_kzalloc in arraymap bpf: Use bpf_map_kvcalloc in bpf_local_storage bpf: Add bpf map free helpers bpf, memcg: Add new item bpf into memory.stat include/linux/bpf.h | 70 ++++++++++++++++++- include/linux/memcontrol.h | 11 +++ include/linux/sched.h | 1 + include/linux/sched/mm.h | 24 +++++++ kernel/bpf/arraymap.c | 30 ++++----- kernel/bpf/bloom_filter.c | 4 +- kernel/bpf/bpf_local_storage.c | 20 +++--- kernel/bpf/bpf_struct_ops.c | 14 ++-- kernel/bpf/cpumap.c | 24 +++---- kernel/bpf/devmap.c | 36 +++++----- kernel/bpf/hashtab.c | 24 ++++--- kernel/bpf/helpers.c | 2 +- kernel/bpf/local_storage.c | 14 ++-- kernel/bpf/lpm_trie.c | 6 +- kernel/bpf/memalloc.c | 10 +++ kernel/bpf/offload.c | 6 +- kernel/bpf/queue_stack_maps.c | 4 +- kernel/bpf/reuseport_array.c | 4 +- kernel/bpf/ringbuf.c | 106 ++++++++++++++++++++--------- kernel/bpf/stackmap.c | 13 ++-- kernel/bpf/syscall.c | 149 ++++++++++++++++++++++++++++++----------- kernel/fork.c | 1 + mm/memcontrol.c | 20 ++++++ net/core/sock_map.c | 22 +++--- net/xdp/xskmap.c | 6 +- 25 files changed, 439 insertions(+), 182 deletions(-)