Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1366370imu; Sat, 17 Nov 2018 23:48:54 -0800 (PST) X-Google-Smtp-Source: AJdET5c0lboQbFwmgJedcsEYcrRfsulGdpO4jzV1HUNm6Ig0L+PRFhf0ofwxg/HY5kQ15AVDhAZr X-Received: by 2002:a62:e044:: with SMTP id f65mr18064866pfh.208.1542527333959; Sat, 17 Nov 2018 23:48:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542527333; cv=none; d=google.com; s=arc-20160816; b=n7NKP754M+5u2TWSVmUpOHLURvnmBLFkzRWirLreZcaNHW4ieJuKcp1orgK0V6VSWg 7TVvYzd3Y4jwzO6a1SPDKXJhruS0GLIYYdvam7ZYU7yd0Bo7h9ghbuD1jjAG/d3X972V x3lV5w7HT0XGYSUSpDKUR7emYtBX4GYeZf6wF5dIAFIlBdS8S2WvCOTxhU86LfNERG6Y eKZAihmzVnuxjp5XCTkEWVTKO/SnVHvAPHSpQkj+tS/SpJZ4wuJ56xmQ6e3WlRqmRYE9 pwiboyEZRxgcpSkLjaHZosNjXnylbhaaxzF88vAI9DLWbCzjl9Ujgx4lsy9pczKL4qVV +ciw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=e9jUmSTLaGX0A7LyE7aM7FouS47os25e/5PoGajRO0w=; b=caOkASOjAQynMP3LldtCoUDyU6zW/y4y2A+LKqarljgvUJSllROXLaBV2u7mRDQWML e5DbeYyvOp8hbmpg7r2JYuY7+iOwzBtwVazwU7TmNa7UBgAHCErrxrNjOUQKmTq4NZ9Q k/vb9FeVrLrA5hCCsaC38er3aJpAqqQWc1OiuE5GepkM+QrHqZiKr6hFEocXb9E0oF9w lipHG2OHkjEtM/CHHkQc4Yrc8KNzzin4/5rCugQ+aNohhIJGsMtxIo3+jdrcvwHb/cgG RA2wifn2GRwQJUyVocOXO1xpPYoFAQ8KS/Y/9EW3+8os99frKJgPPzbsQUsLH9f0C20F 9jiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=jAN2iaJe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m12-v6si27216153pfi.286.2018.11.17.23.48.38; Sat, 17 Nov 2018 23:48:53 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=jAN2iaJe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727164AbeKRSH3 (ORCPT + 99 others); Sun, 18 Nov 2018 13:07:29 -0500 Received: from mail-qk1-f196.google.com ([209.85.222.196]:43201 "EHLO mail-qk1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726077AbeKRSH3 (ORCPT ); Sun, 18 Nov 2018 13:07:29 -0500 Received: by mail-qk1-f196.google.com with SMTP id r71so44095793qkr.10; Sat, 17 Nov 2018 23:47:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=e9jUmSTLaGX0A7LyE7aM7FouS47os25e/5PoGajRO0w=; b=jAN2iaJegYuO7y2ZKWZRRtWbl2StGyxw+Y3Ox6vHmmX1u2mpwTX33rfiW8Rnh0yT8U VVfx/9FWFpzETBNWLWvFFlz9/UMtPz62DIHQFevJZPuOsK3mGmkfS/8yBtfOWU6MLJQr ZPiSKBIMvUpuXc09hQBvCHHuNS5su52O3gDhV+N6L343+bjvYGBZlmwMcXyd4MqquJot xZAcShyQkOZyMJx1uPOyYChnnnG7qrY88Zhd8tL4i8+2aXc5SQJFLGJBAceou0Je/K7z vm3sHwjFX7SPavSdCrnjDcpaKyNhgIjiD+TnJJ7ri9uZ6eObpw/b+q63HlCLrWBxLcye Y7yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=e9jUmSTLaGX0A7LyE7aM7FouS47os25e/5PoGajRO0w=; b=cbUtn1X//Vf5/Ey3Esg0dgO2FShTCDo/mdJtpJ6oZz9KSkC9fdKcod4H+Tn9oiphLP Iy5KWtUYUuqH4z9UalSnF+t7xTkehi9eNRhG1JGNIfNreCfYP8MEpeVX08HbyjWlIrjG r1jXtWr1S0FSluGD2ENugCd2MPZBXcLDS0pIqmM3QwnOs9xtZayuDGE/2smudhMPAusm dWOCl2C70Vkkuhhh97YNIIFkTTzxpCRGhl03uEOADpOEEkU5dObV/oqjmvQfns/ez0oZ 3Yw9+DdIsnKU5i7yDaXntMTzHPr3TwsCcayBWrgt6i9U5cGz1F8DslquJsMnb9UIAZ5a M0yw== X-Gm-Message-State: AGRZ1gLzRo3IOidMtUa79+Nsp9pqdqTOJIGQVx6Na0PUkvLaKdFEpbtW /rvkrcSp1xt+nQstauYYHXGwUGDDvnoMdSuCiug= X-Received: by 2002:a0c:bf0d:: with SMTP id m13mr16906324qvi.139.1542527276900; Sat, 17 Nov 2018 23:47:56 -0800 (PST) MIME-Version: 1.0 References: <20181117185715.25198-1-ard.biesheuvel@linaro.org> <20181117185715.25198-3-ard.biesheuvel@linaro.org> In-Reply-To: <20181117185715.25198-3-ard.biesheuvel@linaro.org> From: Y Song Date: Sun, 18 Nov 2018 07:47:27 +0000 Message-ID: Subject: Re: [PATCH 2/4] net/bpf: refactor freeing of executable allocations To: ard.biesheuvel@linaro.org Cc: LKML , Daniel Borkmann , Alexei Starovoitov , rick.p.edgecombe@intel.com, eric.dumazet@gmail.com, jannh@google.com, Kees Cook , jeyu@kernel.org, arnd@arndb.de, catalin.marinas@arm.com, will.deacon@arm.com, mark.rutland@arm.com, ralf@linux-mips.org, paul.burton@mips.com, jhogan@kernel.org, benh@kernel.crashing.org, paulus@samba.org, mpe@ellerman.id.au, David Miller , linux-arm-kernel@lists.infradead.org, linux-mips@linux-mips.org, linuxppc-dev@lists.ozlabs.org, sparclinux@vger.kernel.org, netdev Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Nov 17, 2018 at 6:58 PM Ard Biesheuvel wrote: > > 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. > - */ Do you want to move the above comments to new weak function bpf_jit_binary_free? > -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)); > } > -- > 2.17.1 >