From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 322DFC282CC for ; Tue, 5 Feb 2019 22:54:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0026D2175B for ; Tue, 5 Feb 2019 22:54:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730436AbfBEWyT (ORCPT ); Tue, 5 Feb 2019 17:54:19 -0500 Received: from mga12.intel.com ([192.55.52.136]:13888 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730279AbfBEWyN (ORCPT ); Tue, 5 Feb 2019 17:54:13 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 05 Feb 2019 14:54:12 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,337,1544515200"; d="scan'208";a="140983156" Received: from linksys13920.jf.intel.com (HELO rpedgeco-DESK5.jf.intel.com) ([10.54.75.11]) by fmsmga002.fm.intel.com with ESMTP; 05 Feb 2019 14:54:12 -0800 From: Rick Edgecombe To: daniel@iogearbox.net, ast@fb.com Cc: netdev@vger.kernel.org, ard.biesheuvel@linaro.org, dave.hansen@intel.com, kristen@linux.intel.com, Rick Edgecombe Subject: [RFC PATCH 3/4] bpf: Charge bpf jit limit in bpf_jit_alloc_exec Date: Tue, 5 Feb 2019 14:51:02 -0800 Message-Id: <20190205225103.28296-4-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190205225103.28296-1-rick.p.edgecombe@intel.com> References: <20190205225103.28296-1-rick.p.edgecombe@intel.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This moves the calls to bpf_jit_charge_modmem and bpf_jit_uncharge_modmem to bpf_jit_alloc_exec so the behavior can be overridden for arch specific capabilities, for example if the arch supports for allocating outside the module space. Cc: Daniel Borkmann Cc: Alexei Starovoitov Signed-off-by: Rick Edgecombe --- include/linux/filter.h | 3 +++ kernel/bpf/core.c | 20 +++++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/include/linux/filter.h b/include/linux/filter.h index ad106d845b22..33c0ae5990e1 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -859,6 +859,9 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, unsigned int alignment, bpf_jit_fill_hole_t bpf_fill_ill_insns); void bpf_jit_binary_free(struct bpf_binary_header *hdr); +int bpf_jit_charge_modmem(u32 pages); +void bpf_jit_uncharge_modmem(u32 pages); + void bpf_jit_free(struct bpf_prog *fp); diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index f908b9356025..ce08a09839c2 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -722,7 +722,7 @@ static int __init bpf_jit_charge_init(void) } pure_initcall(bpf_jit_charge_init); -static int bpf_jit_charge_modmem(u32 pages) +int bpf_jit_charge_modmem(u32 pages) { if (atomic_long_add_return(pages, &bpf_jit_current) > (bpf_jit_limit >> PAGE_SHIFT)) { @@ -735,14 +735,22 @@ static int bpf_jit_charge_modmem(u32 pages) return 0; } -static void bpf_jit_uncharge_modmem(u32 pages) +void bpf_jit_uncharge_modmem(u32 pages) { atomic_long_sub(pages, &bpf_jit_current); } void *__weak bpf_jit_alloc_exec(unsigned long size) { - return module_alloc(size); + void *ret; + u32 pages = size / PAGE_SIZE; + + if (bpf_jit_charge_modmem(pages)) + return NULL; + ret = module_alloc(size); + if (!ret) + bpf_jit_uncharge_modmem(pages); + return ret; } void __weak bpf_jit_free_exec(void *addr) @@ -765,13 +773,7 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE); pages = size / PAGE_SIZE; - if (bpf_jit_charge_modmem(pages)) - return NULL; hdr = bpf_jit_alloc_exec(size); - if (!hdr) { - bpf_jit_uncharge_modmem(pages); - return NULL; - } /* Fill space with illegal/arch-dep instructions. */ bpf_fill_ill_insns(hdr, size); -- 2.17.1