From patchwork Thu Aug 31 13:12:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Puranjay Mohan X-Patchwork-Id: 13371558 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 BEC3BC83F33 for ; Thu, 31 Aug 2023 13:12:48 +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:References:In-Reply-To: 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: List-Owner; bh=QIRsOt8NfAZ9Ct/b816kOksusJleiuw9R9Ds7OLFAlI=; b=HoMRypeRALlAnc mt9i1udpP3vEsnQ732AUG6+Hn58ELnzpz7791uSfkNYjSPKEkz1O98qxAuhS9Xu7iijTlTEa6Dt5m OzQnXC4m7gRpKnD2pd4J+lR8Xo59xwuSWKYUZ5eWrDpI618Dyv3NsKP+5H12dczW3bLNyP8MRgHmz +yyioVX+4DCi78Zc12mAYXzfE7RLBqm6UYr7RuB5TIxkeRcQD107FvzIGwtfHALusI7o2aCjn4EGc uzHABKFniLlNsjLhSgzuHsjXvVh7WFT57ycdZgxStTucgP0FzTs/0UITWSJtkB4EbwCTLVJ+pysQ4 CoF30S6gBN3s8rZghftg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qbhTY-00FLDW-0Q; Thu, 31 Aug 2023 13:12:44 +0000 Received: from mail-wr1-x433.google.com ([2a00:1450:4864:20::433]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qbhTQ-00FLA1-1q for linux-riscv@lists.infradead.org; Thu, 31 Aug 2023 13:12:37 +0000 Received: by mail-wr1-x433.google.com with SMTP id ffacd0b85a97d-31aeee69de0so604712f8f.2 for ; Thu, 31 Aug 2023 06:12:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693487552; x=1694092352; darn=lists.infradead.org; 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=Vw9aCO6QF7ehSGg3eSofLWFhJ8EHSCsmR6IG4YjeGA0=; b=U697WiVWH2LdzgL3eVe5M+9/IimkGdZC/G2X8uY4SNFguL+NYYd6sNLsiuvdHmsYw8 8jCHbU3pK+UVKMBmlClNCcVC8S1+tLpAsCOnRk1J8Srts68P4toP8qvRaRCx/02sXMX8 M0dFoNPv1dtR1am+vTnbFTsQNOl+cR1E7i+4Bu5tWViUonYqOUDk2OPWftC7Hv3jtCvc VcPDquB7dd1ljNUThmb6qOhRWrpWaJ7l4PqemKB2JSAkCRueK7s4gXibN55G5XtrLGS8 tehH4XO4pz+vaxJrGAC/1JrojgXDgGYmlbaQsSojec0YetbOp00AEef/9TfPIIpryr3Z lYmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693487552; x=1694092352; 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=Vw9aCO6QF7ehSGg3eSofLWFhJ8EHSCsmR6IG4YjeGA0=; b=Im4Sq4MfDOa/LMue9SOEN00NzP37rNGwWyq3NLjqnuLrNz+5DNUwf0BVu7j19N5h00 1zUERFSh8+tlujWQyec7hRxVDdVQZZZKJvexLyv6N+l7f2inVV7RrHFfwRNy2WqIGU0o Q0sff8KcyCIWSjfhhUyPuvUJVCZDST1GjCSAbS/y0mRs7PArpPn0+kCwQURsTyv1Nm9+ MbkRBsmRhCRIDFMjBnkgS9msgUfKYaOtPU7jwjvsDvigFMco2Z45btfeU2h3grLgR8nQ tJv1noIpIGPjbRsAFzzeTN8/lk8frBNHWRiVZ0/3zVdRV9lLyW9MM1BCi7fk/I3BGHG4 WW6Q== X-Gm-Message-State: AOJu0YwevEYNmnQGtg4oWK2XMr8/UHNNgFv3U05QpoF8sSt1eV1bIlfI qY56tU1Dnod82oZ1v6Aq+lEJJBjWjrfbu6nEtVk= X-Google-Smtp-Source: AGHT+IEV1nZTWVMPYeNOWwrZQScu0+JsXp36Y/oH9nCnr88bGLZYxfwLclYlSX63KCt8UHKIEmHBIA== X-Received: by 2002:adf:e54f:0:b0:319:82c9:8e7d with SMTP id z15-20020adfe54f000000b0031982c98e7dmr3698994wrm.31.1693487551537; Thu, 31 Aug 2023 06:12:31 -0700 (PDT) Received: from ip-172-31-30-46.eu-west-1.compute.internal (ec2-54-170-241-106.eu-west-1.compute.amazonaws.com. [54.170.241.106]) by smtp.gmail.com with ESMTPSA id a28-20020a5d457c000000b00317f70240afsm2206607wrc.27.2023.08.31.06.12.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 06:12:31 -0700 (PDT) From: Puranjay Mohan To: paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, pulehui@huawei.com, conor.dooley@microchip.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, bjorn@kernel.org, bpf@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: puranjay12@gmail.com Subject: [PATCH bpf-next v4 1/4] bpf: make bpf_prog_pack allocator portable Date: Thu, 31 Aug 2023 13:12:26 +0000 Message-Id: <20230831131229.497941-2-puranjay12@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230831131229.497941-1-puranjay12@gmail.com> References: <20230831131229.497941-1-puranjay12@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230831_061236_614231_C1D56E75 X-CRM114-Status: GOOD ( 13.39 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The bpf_prog_pack allocator currently uses module_alloc() and module_memfree() to allocate and free memory. This is not portable because different architectures use different methods for allocating memory for BPF programs. Like ARM64 and riscv use vmalloc()/vfree(). Use bpf_jit_alloc_exec() and bpf_jit_free_exec() for memory management in bpf_prog_pack allocator. Other architectures can override these with their implementation and will be able to use bpf_prog_pack directly. On architectures that don't override bpf_jit_alloc/free_exec() this is basically a NOP. Signed-off-by: Puranjay Mohan Acked-by: Song Liu Acked-by: Björn Töpel Tested-by: Björn Töpel --- kernel/bpf/core.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 0f8f036d8bd1..4e3ce0542e31 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -870,7 +870,7 @@ static struct bpf_prog_pack *alloc_new_pack(bpf_jit_fill_hole_t bpf_fill_ill_ins GFP_KERNEL); if (!pack) return NULL; - pack->ptr = module_alloc(BPF_PROG_PACK_SIZE); + pack->ptr = bpf_jit_alloc_exec(BPF_PROG_PACK_SIZE); if (!pack->ptr) { kfree(pack); return NULL; @@ -894,7 +894,7 @@ void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns) mutex_lock(&pack_mutex); if (size > BPF_PROG_PACK_SIZE) { size = round_up(size, PAGE_SIZE); - ptr = module_alloc(size); + ptr = bpf_jit_alloc_exec(size); if (ptr) { bpf_fill_ill_insns(ptr, size); set_vm_flush_reset_perms(ptr); @@ -932,7 +932,7 @@ void bpf_prog_pack_free(struct bpf_binary_header *hdr) mutex_lock(&pack_mutex); if (hdr->size > BPF_PROG_PACK_SIZE) { - module_memfree(hdr); + bpf_jit_free_exec(hdr); goto out; } @@ -956,7 +956,7 @@ void bpf_prog_pack_free(struct bpf_binary_header *hdr) if (bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0, BPF_PROG_CHUNK_COUNT, 0) == 0) { list_del(&pack->list); - module_memfree(pack->ptr); + bpf_jit_free_exec(pack->ptr); kfree(pack); } out: