From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A048DC169C4 for ; Thu, 31 Jan 2019 09:27:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6FB7A20881 for ; Thu, 31 Jan 2019 09:27:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="TW4SlRS8" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728895AbfAaJ1f (ORCPT ); Thu, 31 Jan 2019 04:27:35 -0500 Received: from out1-smtp.messagingengine.com ([66.111.4.25]:40337 "EHLO out1-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725932AbfAaJ1f (ORCPT ); Thu, 31 Jan 2019 04:27:35 -0500 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 7808E220B8; Thu, 31 Jan 2019 04:27:34 -0500 (EST) Received: from web6 ([10.202.2.216]) by compute6.internal (MEProxy); Thu, 31 Jan 2019 04:27:34 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=CKCGwkG4MMBc01mZxBWEK/xYTI5a5Kv5cs6e7ZFRM +w=; b=TW4SlRS8foOYc3BKtzq/lvJM2M/xOto1UoeVvJdKTwqOlV0e8w6dXd+v9 TOGMgiMZfJArh3Ivy3D2sMulc3glPpvfJmqUphmgnXoFXNO7dLBVVpruaQJfMEmi SC05oPIv8C3wgk16UpArnIt+8Yg2ymu0d7zNW9zs/AoghXtOqWdu3vaWuDOnSarA 9UYg34IAodvQeO6NDmHmKzsM51urlrfFDO8IXE/8NrJZ5oLkv7xn96vs5thLRkaa PH0vFzjSx+pQzANfWstLW2+ig+oWcdTSrT04s5UaSZ6R9zkUOPn9Jgg3SrQs7ftj 4WK8JL/l0IIdi0x91HSSTO3LK9btA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrjeeigddtgecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfhuthenuceurghilhhouhhtmecufedt tdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhepkffhvfgggfgtof hfjgfuffesthejredtredtjeenucfhrhhomhepofgrrhhthihnrghsuceomheslhgrmhgs uggrrdhltheqnecurfgrrhgrmhepmhgrihhlfhhrohhmpehmsehlrghmsggurgdrlhhtne cuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 99) id C4EE341C6; Thu, 31 Jan 2019 04:27:33 -0500 (EST) Message-Id: <1548926853.2458259.1647648632.4E972DCA@webmail.messagingengine.com> From: Martynas To: Y Song Cc: netdev , Alexei Starovoitov , Daniel Borkmann MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" X-Mailer: MessagingEngine.com Webmail Interface - ajax-ec01da05 References: <20190130140251.23784-1-m@lambda.lt> In-Reply-To: Subject: Re: [PATCH bpf-next] bpf: add optional memory accounting for maps Date: Thu, 31 Jan 2019 11:27:33 +0200 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Thanks for testing. Sure, I will re-submit with the required changes. On Thu, Jan 31, 2019, at 9:15 AM, Y Song wrote: > On Wed, Jan 30, 2019 at 6:05 AM Martynas Pumputis wrote: > > > > Previously, memory allocated for a map was not accounted. Therefore, > > this memory could not be taken into consideration by the cgroups > > memory controller. > > > > This patch introduces the "BPF_F_ACCOUNT_MEM" flag which enables > > the memory accounting for a map, and it can be set during > > the map creation ("BPF_MAP_CREATE") in "map_flags". > > > > When enabled, we account only that amount of memory which is charged > > against the "RLIMIT_MEMLOCK" limit. > > > > To validate the change, first we create the memory cgroup "test-map": > > > > # mkdir /sys/fs/cgroup/memory/test-map > > > > And then we run the following program against the cgroup: > > > > $ cat test_map.c > > <..> > > int main() { > > usleep(3 * 1000000); > > assert(bpf_create_map(BPF_MAP_TYPE_HASH, 8, 16, 65536, 0) > 0); > > usleep(3 * 1000000); > > } > > # cgexec -g memory:test-map ./test_map & > > # cat /sys/fs/cgroup/memory/test-map/memory{,.kmem}.usage_in_bytes > > 397312 > > 258048 > > > > > > > > # bpftool map list > > 19: hash flags 0x0 > > key 8B value 16B max_entries 65536 memlock 5771264B > > # cat /sys/fs/cgroup/memory/test-map/memory{,.kmem}.usage_in_bytes > > 401408 > > 262144 > > > > As we can see, the memory allocated for map is not accounted, as > > 397312B + 5771264B > 401408B. > > > > Next, we enabled the accounting and re-run the test: > > > > $ cat test_map.c > > <..> > > int main() { > > usleep(3 * 1000000); > > assert(bpf_create_map(BPF_MAP_TYPE_HASH, 8, 16, 65536, BPF_F_ACCOUNT_MEM) > 0); > > usleep(3 * 1000000); > > } > > # cgexec -g memory:test-map ./test_map & > > # cat /sys/fs/cgroup/memory/test-map/memory{,.kmem}.usage_in_bytes > > 450560 > > 307200 > > > > > > > > # bpftool map list > > 20: hash flags 0x80 > > key 8B value 16B max_entries 65536 memlock 5771264B > > # cat /sys/fs/cgroup/memory/test-map/memory{,.kmem}.usage_in_bytes > > 6221824 > > 6078464 > > > > This time, the memory (including kmem) is accounted, as > > 450560B + 5771264B <= 6221824B > > The above test result is for cgroup v1. > I also tested the patch for cgroup v2. It works fine too. > > > > > Signed-off-by: Martynas Pumputis > > --- > > include/linux/bpf.h | 5 +++-- > > include/uapi/linux/bpf.h | 2 ++ > > kernel/bpf/arraymap.c | 14 +++++++++----- > > kernel/bpf/bpf_lru_list.c | 11 +++++++++-- > > kernel/bpf/bpf_lru_list.h | 1 + > > kernel/bpf/cpumap.c | 12 +++++++++--- > > kernel/bpf/devmap.c | 10 ++++++++-- > > kernel/bpf/hashtab.c | 19 ++++++++++++++----- > > kernel/bpf/lpm_trie.c | 19 +++++++++++++------ > > kernel/bpf/queue_stack_maps.c | 5 +++-- > > kernel/bpf/reuseport_array.c | 3 ++- > > kernel/bpf/stackmap.c | 12 ++++++++---- > > kernel/bpf/syscall.c | 12 ++++++++---- > > kernel/bpf/xskmap.c | 9 +++++++-- > > net/core/sock_map.c | 13 +++++++++---- > > tools/include/uapi/linux/bpf.h | 3 +++ > > 16 files changed, 108 insertions(+), 42 deletions(-) > > > > diff --git a/include/linux/bpf.h b/include/linux/bpf.h > > index e734f163bd0b..353a3f4304fe 100644 > > --- a/include/linux/bpf.h > > +++ b/include/linux/bpf.h > > @@ -79,7 +79,8 @@ struct bpf_map { > > u32 btf_value_type_id; > > struct btf *btf; > > bool unpriv_array; > > - /* 55 bytes hole */ > > + bool account_mem; > > + /* 54 bytes hole */ > > > > /* The 3rd and 4th cacheline with misc members to avoid false sharing > > * particularly with refcounting. > > @@ -506,7 +507,7 @@ void bpf_map_put(struct bpf_map *map); > > int bpf_map_precharge_memlock(u32 pages); > > int bpf_map_charge_memlock(struct bpf_map *map, u32 pages); > > void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages); > > -void *bpf_map_area_alloc(size_t size, int numa_node); > > +void *bpf_map_area_alloc(size_t size, int numa_node, bool account_mem); > > void bpf_map_area_free(void *base); > > void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr); > > > > diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h > > index 91c43884f295..a374ccbaa51b 100644 > > --- a/include/uapi/linux/bpf.h > > +++ b/include/uapi/linux/bpf.h > > @@ -278,6 +278,8 @@ enum bpf_attach_type { > > #define BPF_F_NO_COMMON_LRU (1U << 1) > > /* Specify numa node during map creation */ > > #define BPF_F_NUMA_NODE (1U << 2) > > +/* Enable memory accounting for map */ > > +#define BPF_F_ACCOUNT_MEM (1U << 7) > > Could you put the above definition after BPF_F_ZERO_SEED (1U << 6)? > The same for tools. > With this change, you can add my ack: > Acked-by: Yonghong Song