From: Y Song <ys114321@gmail.com>
To: ard.biesheuvel@linaro.org
Cc: LKML <linux-kernel@vger.kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Alexei Starovoitov <ast@kernel.org>,
rick.p.edgecombe@intel.com, eric.dumazet@gmail.com,
jannh@google.com, Kees Cook <keescook@chromium.org>,
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 <davem@davemloft.net>,
linux-arm-kernel@lists.infradead.org, linux-mips@linux-mips.org,
linuxppc-dev@lists.ozlabs.org, sparclinux@vger.kernel.org,
netdev <netdev@vger.kernel.org>
Subject: Re: [PATCH 2/4] net/bpf: refactor freeing of executable allocations
Date: Sun, 18 Nov 2018 20:20:41 +0000 [thread overview]
Message-ID: <CAH3MdRXESr62o2XnQ0L7GGZRfM_GpbFpG2xmk93LDLL2es8y3A@mail.gmail.com> (raw)
In-Reply-To: <CAKv+Gu9hB3a5LfFQskNzXZDmgps_+9dewbC1E5GFndcQZ-KgQg@mail.gmail.com>
On Sun, Nov 18, 2018 at 3:55 PM Ard Biesheuvel
<ard.biesheuvel@linaro.org> wrote:
>
> On Sat, 17 Nov 2018 at 23:47, Y Song <ys114321@gmail.com> wrote:
> >
> > On Sat, Nov 17, 2018 at 6:58 PM Ard Biesheuvel
> > <ard.biesheuvel@linaro.org> 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 <ard.biesheuvel@linaro.org>
> > > ---
> > > 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?
> >
>
> Perhaps. But one thing I don't understand, looking at this again, is
> why we have these overrides in the first place. module_memfree() just
> calls vfree(), which takes down the mapping entirely (along with any
> updated permissions), and so remapping it back to r/w right before
> that seems rather pointless imo.
>
> Can we get rid of bpf_jit_binary_unlock_ro() entirely, and along with
> it, all these overrides for the free() path?
Maybe based on current implementation. Just a pure speculation.
module_memfree() can be overwritten by arch specific implementation.
The intention could be restoring the allocated page to its original permission
just in case arch specific implementation of module_memfree()
does different thing than default vfee().
next prev parent reply other threads:[~2018-11-18 20:20 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-11-17 18:57 [PATCH 0/4] bpf: permit JIT allocations to be served outside the module region Ard Biesheuvel
2018-11-17 18:57 ` [PATCH 1/4] bpf: account for freed JIT allocations in arch code Ard Biesheuvel
2018-11-19 10:37 ` Daniel Borkmann
2018-11-19 15:37 ` Ard Biesheuvel
2018-11-17 18:57 ` [PATCH 2/4] net/bpf: refactor freeing of executable allocations Ard Biesheuvel
2018-11-18 7:47 ` Y Song
2018-11-18 15:55 ` Ard Biesheuvel
2018-11-18 20:20 ` Y Song [this message]
2018-11-17 18:57 ` [PATCH 3/4] bpf: add __weak hook for allocating executable memory Ard Biesheuvel
2018-11-17 18:57 ` [PATCH 4/4] arm64/bpf: don't allocate BPF JIT programs in module memory Ard Biesheuvel
2018-11-18 7:48 ` [PATCH 0/4] bpf: permit JIT allocations to be served outside the module region Y Song
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAH3MdRXESr62o2XnQ0L7GGZRfM_GpbFpG2xmk93LDLL2es8y3A@mail.gmail.com \
--to=ys114321@gmail.com \
--cc=ard.biesheuvel@linaro.org \
--cc=arnd@arndb.de \
--cc=ast@kernel.org \
--cc=benh@kernel.crashing.org \
--cc=catalin.marinas@arm.com \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eric.dumazet@gmail.com \
--cc=jannh@google.com \
--cc=jeyu@kernel.org \
--cc=jhogan@kernel.org \
--cc=keescook@chromium.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mips@linux-mips.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=mark.rutland@arm.com \
--cc=mpe@ellerman.id.au \
--cc=netdev@vger.kernel.org \
--cc=paul.burton@mips.com \
--cc=paulus@samba.org \
--cc=ralf@linux-mips.org \
--cc=rick.p.edgecombe@intel.com \
--cc=sparclinux@vger.kernel.org \
--cc=will.deacon@arm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).