Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1739653imu; Sun, 18 Nov 2018 07:56:27 -0800 (PST) X-Google-Smtp-Source: AJdET5f3NJ9nSCRVOe5V4nu9fSa5aTrC6lNJxz5YfhADrvx2cmpmpnQT99BrmaLbPiegWRhS5oWo X-Received: by 2002:a62:f247:: with SMTP id y7mr9752147pfl.25.1542556587086; Sun, 18 Nov 2018 07:56:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542556587; cv=none; d=google.com; s=arc-20160816; b=iIJw9LNIi2gZbaIVTjcG07eRsnPq4EkY/CdplZOxp4cVy+sw5foEPl5ugaJZHV0iIP fz7y+fX8CKWRLFYUGGAmeYYK1F/fdFjRKTJcA8Kud0UWCLPUGwbxp8JNS0G9npCiHMfi t9JQsc96qXsIrbSt/nMKwn9LFGBNmbBpTBvw+7PWqMTekSZLjTBNnwJXfPObS6I87gYy nYIUP/fwxwAXdXe+UySaxRpemGxsEpmrzUU6GIM7SbqYKqdltNmot8DLRQOE8P67Txme 4FCPH2eFLrqPJtHZzlzr6nVYDrJbOuf+P+TuT14Wuzp/t/utqPdyj7rFYO61WXBC4sS+ wI4g== 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=LVpHVlN8yMb+8QBmHy6R38Io2Rvx7iOnFnXQ1i2RcB0=; b=Yskbrl+XBC3diY/GVZgtBqBPZ1IFO/uwLvZP2jdV7MB0XnpORbvTncrNpZkV2D/AMj ZqzPMRd+vHLQ021ZHcBcKdK3Gn0n6Pl1nZ4wZcExx22tV3O8UxekCfUinxXdvy0dLZnE rauNrLLBMX3SZEDRq0+3CZIRRZySw4MbMTXmAulgdgAi/cmw9cMN4m4Y1espXX7dfY4L kmj8OOePHlPcmGGBd59t/TdnuyoyE8BngTQfC3boymYhQMKwqEOUb2D09k+eybkrZU7z nJYrk7kaFi9P5GrWWhMbvL15wOzau9E2c4D9F5uOruydL4ID+uZofR2Lt5BBjClvsufD YyAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=U4YH+vLZ; 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=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id bc3-v6si37929707plb.52.2018.11.18.07.56.12; Sun, 18 Nov 2018 07:56:27 -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=@linaro.org header.s=google header.b=U4YH+vLZ; 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=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727601AbeKSCQF (ORCPT + 99 others); Sun, 18 Nov 2018 21:16:05 -0500 Received: from mail-io1-f66.google.com ([209.85.166.66]:37981 "EHLO mail-io1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727188AbeKSCQF (ORCPT ); Sun, 18 Nov 2018 21:16:05 -0500 Received: by mail-io1-f66.google.com with SMTP id e9-v6so5210956iob.5 for ; Sun, 18 Nov 2018 07:55:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=LVpHVlN8yMb+8QBmHy6R38Io2Rvx7iOnFnXQ1i2RcB0=; b=U4YH+vLZlLB+8Lk1fLwwL9SqcRVKdWwEYNRWI29Tc2kuUJ6m4LvNFoAYY88XZbezRI jL+Uj+gZ15vaSPqfe1C5w38l7H/tUnQhdPqsTe5oC8bnov+61JhICme1/RWg1ppqNiZI ziqrJ5EiU8nbt66gD7FcGQ/P+iO/KFV8ZVSec= 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=LVpHVlN8yMb+8QBmHy6R38Io2Rvx7iOnFnXQ1i2RcB0=; b=PaPzZACPeyI93w8XssX0LaWVXo1Fahaa1T9B3FnAjFK7Vc1VPcAuUQ/DR7BmMXO2SE 3lk6uc8CKlzAR8aY08XSjDPCA/pZU+AZUBIr1NtGNG+AWBgyeywcVZYx+iNqPYF4n/G7 fDcGncsghg8NMJJ0RKX40tgUvlyYh6gvThxV0WcIKLX0k0r97v2FcOVvIopDiy+XMMFh 0t7Wjp7r8gR0WQt/Tc7bV7OPR+NTdnTwN2R52LfLtm/JCi1bN6sZnXb23cqv7/4XHMG8 y83UumDOC9CKnPBfswlvJKzOSDkF+NrBNP8+np9EgV2hHcOAqykx958dW4HO0kE7G3m4 NpLg== X-Gm-Message-State: AGRZ1gJDMzcGAsgM3CJfTItSB1VYNmNTesz65nALGGHI+v9SMLKwpNTL 7YpRYLxTmvpQdbc6vmltt29+6IP4GBJWOXdbLLbMMg== X-Received: by 2002:a6b:37c2:: with SMTP id e185-v6mr15113497ioa.173.1542556525869; Sun, 18 Nov 2018 07:55:25 -0800 (PST) MIME-Version: 1.0 References: <20181117185715.25198-1-ard.biesheuvel@linaro.org> <20181117185715.25198-3-ard.biesheuvel@linaro.org> In-Reply-To: From: Ard Biesheuvel Date: Sun, 18 Nov 2018 07:55:14 -0800 Message-ID: Subject: Re: [PATCH 2/4] net/bpf: refactor freeing of executable allocations To: ys114321@gmail.com Cc: Linux Kernel Mailing List , Daniel Borkmann , Alexei Starovoitov , Rick Edgecombe , Eric Dumazet , Jann Horn , Kees Cook , Jessica Yu , Arnd Bergmann , Catalin Marinas , Will Deacon , Mark Rutland , Ralf Baechle , Paul Burton , James Hogan , Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , "David S. Miller" , linux-arm-kernel , linux-mips , linuxppc-dev , sparclinux@vger.kernel.org, "" 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, 17 Nov 2018 at 23:47, Y Song wrote: > > 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? > 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?