From patchwork Sat Nov 17 18:57:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 999401 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42y4Pf593Sz9s3x for ; Sun, 18 Nov 2018 06:08:22 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="cObMuJm7"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 42y4Pf3gsVzF3Tq for ; Sun, 18 Nov 2018 06:08:22 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="cObMuJm7"; dkim-atps=neutral X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=linaro.org (client-ip=2607:f8b0:4864:20::543; helo=mail-pg1-x543.google.com; envelope-from=ard.biesheuvel@linaro.org; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="cObMuJm7"; dkim-atps=neutral Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 42y49328QWzDr5Y for ; Sun, 18 Nov 2018 05:57:24 +1100 (AEDT) Received: by mail-pg1-x543.google.com with SMTP id d72so8580130pga.9 for ; Sat, 17 Nov 2018 10:57:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=FFyK4Oz1QZNyPCsvoKn4MQd4VSSgFUXXhCBgp8QbhcQ=; b=cObMuJm7WoQyjusLl+MSm4ZIOr4K71j4aNd5tPxwLRhwvXT/sYcvL7RDfA86wcLwA1 MBgV1MZKVJe1BNu/nkLqb6788B61g2+j8XPg6DFJaa+9SwGmzpOHS6y6Xqbefq4fvVkj +/MH7MuuSDpYgCOZS+o5G38042o7Ps9gEKLjw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=FFyK4Oz1QZNyPCsvoKn4MQd4VSSgFUXXhCBgp8QbhcQ=; b=ZdOGjJPBlE+/1dvlrwJXzsFaTw92TccLVQaikO+hHQCcHuhQEwdc8JtJqEQjgFm/Ka kyu7Kcjs/o2jmEPBlEc0bit66OktVGbq4bORsPTj+fFEiJ2OIC/OoPIzRKEKAqpk+zI5 2sNmM1fjNpnrivqJZ5Oxsyn2IUB2wQCjlSFD0yWwiBEage1nsxsf7gT92Rp0FcxZgLiQ 1TbztkCudwHyKjyYbsTllX7erdRNvVde7n0YMdATnysOHTLdieWbH/dIF9hnIyT0WdCM YiaxODcnQeIhdJAnk2B2Bafeh6SK4h1Gxv8VttUNZ/XpMM0WPEYN3FJKHQboEUBen/m7 uLnw== X-Gm-Message-State: AGRZ1gJPSITHGcpwdiszavOSoQXaqevLrFy5ctl9rlwnccgOqyeu8xn3 5qFEA4KXintXmg9wq9q6BpBH4A== X-Google-Smtp-Source: AJdET5eTfTm7MZO1Ys5mfFPU5yjtvrYsgtZia1/NgQb951W2tIaAMrSSEf/Q1za/zZx27f//YDj5kQ== X-Received: by 2002:a62:da5a:: with SMTP id w26mr3755848pfl.106.1542481042106; Sat, 17 Nov 2018 10:57:22 -0800 (PST) Received: from mba13.psav.com ([64.114.255.114]) by smtp.gmail.com with ESMTPSA id u76-v6sm49550745pfa.176.2018.11.17.10.57.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Nov 2018 10:57:21 -0800 (PST) From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Subject: [PATCH 2/4] net/bpf: refactor freeing of executable allocations Date: Sat, 17 Nov 2018 10:57:13 -0800 Message-Id: <20181117185715.25198-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181117185715.25198-1-ard.biesheuvel@linaro.org> References: <20181117185715.25198-1-ard.biesheuvel@linaro.org> X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , linux-mips@linux-mips.org, Will Deacon , Alexei Starovoitov , Paul Mackerras , sparclinux@vger.kernel.org, Daniel Borkmann , Catalin Marinas , James Hogan , Kees Cook , Arnd Bergmann , Jann Horn , linux-arm-kernel@lists.infradead.org, Eric Dumazet , Ard Biesheuvel , netdev@vger.kernel.org, Ralf Baechle , Paul Burton , Jessica Yu , Rick Edgecombe , linuxppc-dev@lists.ozlabs.org, "David S. Miller" Errors-To: linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" All arch overrides of the __weak bpf_jit_free() amount to the same thing: the allocated memory was never mapped read-only, and so it does not have to be remapped to read-write before being freed. So in preparation of permitting arches to serve allocations for BPF JIT programs from other regions than the module region, refactor the existing bpf_jit_free() implementations to use the shared code where possible, and only specialize the remap and free operations. Signed-off-by: Ard Biesheuvel --- arch/mips/net/bpf_jit.c | 7 ++----- arch/powerpc/net/bpf_jit_comp.c | 7 ++----- arch/powerpc/net/bpf_jit_comp64.c | 9 +++------ arch/sparc/net/bpf_jit_comp_32.c | 7 ++----- kernel/bpf/core.c | 15 +++++---------- 5 files changed, 14 insertions(+), 31 deletions(-) diff --git a/arch/mips/net/bpf_jit.c b/arch/mips/net/bpf_jit.c index 1b69897274a1..5696bd7dccc7 100644 --- a/arch/mips/net/bpf_jit.c +++ b/arch/mips/net/bpf_jit.c @@ -1261,10 +1261,7 @@ void bpf_jit_compile(struct bpf_prog *fp) kfree(ctx.offsets); } -void bpf_jit_free(struct bpf_prog *fp) +void bpf_jit_binary_free(struct bpf_binary_header *hdr) { - if (fp->jited) - bpf_jit_binary_free(bpf_jit_binary_hdr(fp)); - - bpf_prog_unlock_free(fp); + module_memfree(hdr); } diff --git a/arch/powerpc/net/bpf_jit_comp.c b/arch/powerpc/net/bpf_jit_comp.c index a1ea1ea6b40d..5b5ce4a1b44b 100644 --- a/arch/powerpc/net/bpf_jit_comp.c +++ b/arch/powerpc/net/bpf_jit_comp.c @@ -680,10 +680,7 @@ void bpf_jit_compile(struct bpf_prog *fp) return; } -void bpf_jit_free(struct bpf_prog *fp) +void bpf_jit_binary_free(struct bpf_binary_header *hdr) { - if (fp->jited) - bpf_jit_binary_free(bpf_jit_binary_hdr(fp)); - - bpf_prog_unlock_free(fp); + module_memfree(hdr); } diff --git a/arch/powerpc/net/bpf_jit_comp64.c b/arch/powerpc/net/bpf_jit_comp64.c index 84c8f013a6c6..f64f1294bd62 100644 --- a/arch/powerpc/net/bpf_jit_comp64.c +++ b/arch/powerpc/net/bpf_jit_comp64.c @@ -1021,11 +1021,8 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp) return fp; } -/* Overriding bpf_jit_free() as we don't set images read-only. */ -void bpf_jit_free(struct bpf_prog *fp) +/* Overriding bpf_jit_binary_free() as we don't set images read-only. */ +void bpf_jit_binary_free(struct bpf_binary_header *hdr) { - if (fp->jited) - bpf_jit_binary_free(bpf_jit_binary_hdr(fp)); - - bpf_prog_unlock_free(fp); + module_memfree(hdr); } diff --git a/arch/sparc/net/bpf_jit_comp_32.c b/arch/sparc/net/bpf_jit_comp_32.c index 01bda6bc9e7f..589950d152cc 100644 --- a/arch/sparc/net/bpf_jit_comp_32.c +++ b/arch/sparc/net/bpf_jit_comp_32.c @@ -756,10 +756,7 @@ cond_branch: f_offset = addrs[i + filter[i].jf]; return; } -void bpf_jit_free(struct bpf_prog *fp) +void bpf_jit_binary_free(struct bpf_binary_header *hdr) { - if (fp->jited) - bpf_jit_binary_free(bpf_jit_binary_hdr(fp)); - - bpf_prog_unlock_free(fp); + module_memfree(hdr); } diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 1a796e0799ec..29f766dac203 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -646,25 +646,20 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, return hdr; } -void bpf_jit_binary_free(struct bpf_binary_header *hdr) +void __weak bpf_jit_binary_free(struct bpf_binary_header *hdr) { - u32 pages = hdr->pages; - + bpf_jit_binary_unlock_ro(hdr); module_memfree(hdr); - bpf_jit_uncharge_modmem(pages); } -/* This symbol is only overridden by archs that have different - * requirements than the usual eBPF JITs, f.e. when they only - * implement cBPF JIT, do not set images read-only, etc. - */ -void __weak bpf_jit_free(struct bpf_prog *fp) +void bpf_jit_free(struct bpf_prog *fp) { if (fp->jited) { struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp); + u32 pages = hdr->pages; - bpf_jit_binary_unlock_ro(hdr); bpf_jit_binary_free(hdr); + bpf_jit_uncharge_modmem(pages); WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp)); }