From patchwork Fri Sep 8 14:43:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Puranjay Mohan X-Patchwork-Id: 13377566 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9B5EAEE8011 for ; Fri, 8 Sep 2023 14:44:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=1pVqlIOBWGpGbqPQ0bHCwjzCXoMApPUA8O5jj6HBCl4=; b=C+XAKUx595ohm5 +h5gjPMk7pM64l3uZ/E17TU91fiFKtVb9iZog0BWNKEbKlBJGKA5iogIsQ1xY4MqcnGYg4FSdxCz9 QIqOsif821+ZBMERxH0GVdgmrFk4kP0bTLFmSem1rTzr8ArTzMEFYoFzV0zN4G6gn4TyEyOhRnO/i YQCXSYz6T0ars5YBYGsVMyigcf2ewEw1PMIGQeZKUaAF7neOGptybmeF8cEttPrR29LnmpzVvHIjt xoCxkjoKahrodO9dxym6wVvnElGDeShPIbDBISgL0MKxeN0JAWP7PeM1xt5n0A/uITlHKn7pLLewg EH4eqdJmMJIETt/DIv/A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qechl-00DwMl-17; Fri, 08 Sep 2023 14:43:29 +0000 Received: from mail-lj1-x232.google.com ([2a00:1450:4864:20::232]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qechh-00DwLC-2N for linux-arm-kernel@lists.infradead.org; Fri, 08 Sep 2023 14:43:27 +0000 Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-2bcde83ce9fso36533031fa.1 for ; Fri, 08 Sep 2023 07:43:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694184202; x=1694789002; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=w8HwV+J/7uNnqFbijDhQaafWl9GR5aB5QOka+RcaLbE=; b=NIvtRpyRsYECAm6MJBY+bzQ7y2cZvU+gvNthcZ+jTMIE2exe23yZqixGcLlz09XmNn wJH+HWoLUA3XPsfrDY6bbFH+UxHCFFqxvOXFm/+BO+l9oz90P01uqXFxJXTsPwDK+Wd2 2UmYWighz934OvvHnCLRICTcIfOUgzKHLj07eLnkNCV7am/fAFCDxk6OilFj+5SzewpM ytG97pp4Zj2LNnDesaxMTNR9CesbUs5v9doTNO+B0v5nMtrMRMl4ZELh49LBef1USiGK ESoU0sSHSVCXVlSRz8N8FRNPmWxQ1CngnHuW4fCJDMEie87HVqq2K3TG3e7vweqOjHlf OBjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694184202; x=1694789002; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=w8HwV+J/7uNnqFbijDhQaafWl9GR5aB5QOka+RcaLbE=; b=BGTp3KJBb2x2M2jkj0Uku9GsOWglA+nlm00Oh24ekNoyEIDukBcgVivDzoyj2SejDt LFh2cbWPibtwF/3MP0UxlgHMjGIKRMf/X9tF+Vi7Ab67dZyIUdMLdxovQ+3x3gPjxx3L VfvYa+S66Gv40hZDnIf1B/NuGaxLXIU9DkSjT+NAjxaW6bdbG9WJwbYNo67VCZFTiStb uGJq53fZ9HHTd3bsvBDFhRH2drFCwNIh219XmFPOqt7KdXg//zN0Z0a19hw38IzRDiRU hHP4na3nUmKPC2G+E3Cv36swztll3wUWrUC20zUw9Z7oarf46I03hPN/ZAQAG8qutts0 h6wA== X-Gm-Message-State: AOJu0YwY6w6NiUADzw+72cTi3gbpM0bQ5z4TYBhFA05YJNcR10/pjkRJ 43rst0njg3kiQfLgjZA5It8= X-Google-Smtp-Source: AGHT+IFytL8L4GmYN1lwr1zUUD8lffspTsgM5GvKOTcdz6bFTFuST3e9LT9M6Av3AlU+swSps1K05g== X-Received: by 2002:a2e:3c18:0:b0:2bd:1615:f9f8 with SMTP id j24-20020a2e3c18000000b002bd1615f9f8mr2033400lja.45.1694184201563; Fri, 08 Sep 2023 07:43:21 -0700 (PDT) Received: from ip-172-31-30-46.eu-west-1.compute.internal (ec2-54-217-129-48.eu-west-1.compute.amazonaws.com. [54.217.129.48]) by smtp.gmail.com with ESMTPSA id lz5-20020a170906fb0500b0098e78ff1a87sm1099436ejb.120.2023.09.08.07.43.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Sep 2023 07:43:21 -0700 (PDT) From: Puranjay Mohan To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, catalin.marinas@arm.com, mark.rutland@arm.com, bpf@vger.kernel.org, kpsingh@kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: puranjay12@gmail.com Subject: [PATCH bpf-next v5 0/3] bpf, arm64: use BPF prog pack allocator in BPF JIT Date: Fri, 8 Sep 2023 14:43:17 +0000 Message-Id: <20230908144320.2474-1-puranjay12@gmail.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230908_074325_794061_C0580C6D X-CRM114-Status: GOOD ( 18.81 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Changes in V4 => v5: 1. Remove the patch for making prog pack allocator portable as it will come through the RISCV tree[1]. 2. Add a new function aarch64_insn_set() to be used in bpf_arch_text_invalidate() for putting illegal instructions after a program is removed. The earlier implementation of bpf_arch_text_invalidate() was calling aarch64_insn_patch_text_nosync() in a loop and making it slow because each call invalidated the cache. Here is test_tag now: [root@ip-172-31-6-176 bpf]# time ./test_tag test_tag: OK (40945 tests) real 0m19.695s user 0m1.514s sys 0m17.841s test_tag without these patches: [root@ip-172-31-6-176 bpf]# time ./test_tag test_tag: OK (40945 tests) real 0m21.487s user 0m1.647s sys 0m19.106s test_tag in the previous version was really slow > 2 minutes. see [2] 3. Add cache invalidation in aarch64_insn_copy() so other users can call the function without worrying about the cache. Currently only bpf_arch_text_copy() is using it, but there might be more users in the future. Chanes in V3 => V4: Changes only in 3rd patch 1. Fix the I-cache maintenance: Clean the data cache and invalidate the i-Cache only *after* the instructions have been copied to the ROX region. Chanes in V2 => V3: Changes only in 3rd patch 1. Set prog = orig_prog; in the failure path of bpf_jit_binary_pack_finalize() call. 2. Add comments explaining the usage of the offsets in the exception table. Changes in v1 => v2: 1. Make the naming consistent in the 3rd patch: ro_image and image ro_header and header ro_image_ptr and image_ptr 2. Use names dst/src in place of addr/opcode in second patch. 3. Add Acked-by: Song Liu in 1st and 2nd patch. BPF programs currently consume a page each on ARM64. For systems with many BPF programs, this adds significant pressure to instruction TLB. High iTLB pressure usually causes slow down for the whole system. Song Liu introduced the BPF prog pack allocator[3] to mitigate the above issue. It packs multiple BPF programs into a single huge page. It is currently only enabled for the x86_64 BPF JIT. This patch series enables the BPF prog pack allocator for the ARM64 BPF JIT. ==================================================== Performance Analysis of prog pack allocator on ARM64 ==================================================== To test the performance of the BPF prog pack allocator on ARM64, a stresser tool[4] was built. This tool loads 8 BPF programs on the system and triggers 5 of them in an infinite loop by doing system calls. The runner script starts 20 instances of the above which loads 8*20=160 BPF programs on the system, 5*20=100 of which are being constantly triggered. In the above environment we try to build Python-3.8.4 and try to find different iTLB metrics for the compilation done by gcc-12.2.0. The source code[5] is configured with the following command: ./configure --enable-optimizations --with-ensurepip=install Then the runner script is executed with the following command: ./run.sh "perf stat -e ITLB_WALK,L1I_TLB,INST_RETIRED,iTLB-load-misses -a make -j32" This builds Python while 160 BPF programs are loaded and 100 are being constantly triggered and measures iTLB related metrics. The output of the above command is discussed below before and after enabling the BPF prog pack allocator. The tests were run on qemu-system-aarch64 with 32 cpus, 4G memory, -machine virt, -cpu host, and -enable-kvm. Results ------- Before enabling prog pack allocator: ------------------------------------ Performance counter stats for 'system wide': 333278635 ITLB_WALK 6762692976558 L1I_TLB 25359571423901 INST_RETIRED 15824054789 iTLB-load-misses 189.029769053 seconds time elapsed After enabling prog pack allocator: ----------------------------------- Performance counter stats for 'system wide': 190333544 ITLB_WALK 6712712386528 L1I_TLB 25278233304411 INST_RETIRED 5716757866 iTLB-load-misses 185.392650561 seconds time elapsed Improvements in metrics ----------------------- Compilation time ---> 1.92% faster iTLB-load-misses/Sec (Less is better) ---> 63.16% decrease ITLB_WALK/1000 INST_RETIRED (Less is better) ---> 42.71% decrease ITLB_Walk/L1I_TLB (Less is better) ---> 42.47% decrease [1] https://git.kernel.org/pub/scm/linux/kernel/git/riscv/linux.git/commit/?h=for-next&id=20e490adea279d49d57b800475938f5b67926d98 [2] https://lore.kernel.org/all/CANk7y0gcP3dF2mESLp5JN1+9iDfgtiWRFGqLkCgZD6wby1kQOw@mail.gmail.com/ [3] https://lore.kernel.org/bpf/20220204185742.271030-1-song@kernel.org/ [4] https://github.com/puranjaymohan/BPF-Allocator-Bench [5] https://www.python.org/ftp/python/3.8.4/Python-3.8.4.tgz Puranjay Mohan (3): arm64: patching: Add aarch64_insn_copy() arm64: patching: Add aarch64_insn_set() bpf, arm64: use bpf_jit_binary_pack_alloc arch/arm64/include/asm/patching.h | 2 + arch/arm64/kernel/patching.c | 81 ++++++++++++++++++ arch/arm64/net/bpf_jit_comp.c | 136 ++++++++++++++++++++++++------ 3 files changed, 195 insertions(+), 24 deletions(-)