From patchwork Wed Nov 3 09:49:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12600495 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 401DEC433F5 for ; Wed, 3 Nov 2021 09:50:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1E932611AD for ; Wed, 3 Nov 2021 09:50:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231757AbhKCJxU (ORCPT ); Wed, 3 Nov 2021 05:53:20 -0400 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:19802 "EHLO mx0a-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231614AbhKCJxS (ORCPT ); Wed, 3 Nov 2021 05:53:18 -0400 Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 1A38Yh98000561; Wed, 3 Nov 2021 09:49:48 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-type : mime-version; s=corp-2021-07-09; bh=vpuA0NXZZF7huJS24oR1AIv2Iv6uC9hMogsdMvqMV/k=; b=l2JCTpamiCJ2oFeMnzMMdBHiBK6SroyFu9gK7iaKHhQRiI5td0eAxjS+S1ypdWKwVnG2 D50/uzNIPoQRz/4fJgLRo/fZltGOt7xjjEJzvjz+u76XwU0sLf6kNT3KicCMWi1+B7F8 g8HSCaBK6TwmVzAVa3EY13P99kFuXg+N4m5xYMti3s/tb5uysYjkWQIgQz9g/bAXOi22 BQ2omCfOMsT5K2FQq0Lg7UfNwCfr0FNOblcX7yFkKvgd4+WNG2YDcWl+RMT2oGf2rcwM WI/xmLnbn9FvriZ0ABdZKOIr45c2twWoGt8bZzAXfkaVOyuDoPBnbtqU4mnX7vtEIo1F VA== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by mx0b-00069f02.pphosted.com with ESMTP id 3c3q1n8bwt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 03 Nov 2021 09:49:48 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 1A39kJJZ187155; Wed, 3 Nov 2021 09:49:46 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2174.outbound.protection.outlook.com [104.47.59.174]) by userp3020.oracle.com with ESMTP id 3c1khv93qp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 03 Nov 2021 09:49:46 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=g1+3udbXD50aDmqOLH31+Prg2Z2q3NLvJqxE+486RYJUtoOTRMZF1DOw23rpT4nbeOgKv0oNz34aUt8rWTS5l0eOJkYO1D79xjKgLiIxQxVRx/960nRZgaWansYfZAy3taFhtEP3OOBOEap4jOwlsirqMSn7Q+Hw9Gq99qMtCAKr0O8tQv0qPN6I543f8zKEZS1w0DC8LtNR06LzE/F53Ivl83lLGjGBreGxvXUmDLajcw9RGU3anoxPd/7lvWQCtNFuSlU8EI8J/c5EJkQzJZdz7gFB37pQIcGSf6CTaCAMTOTz6F9x4Kzo/raA40V4gapExaFZQkUZkEDbqMwp5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=vpuA0NXZZF7huJS24oR1AIv2Iv6uC9hMogsdMvqMV/k=; b=Ja92uJcyE2zimUoYQ2tqUFPyI9rgpVP860ETuoTa2eiaeCUDGTTKX23uJCdh3DHeHaeUDGiDFuEyuSTtitYSyhE+mvHrt512yBTHX4m4Hycui+10HfM+kW5GfIne6ii0RBXl5zQcDkwS1J0xxkllyO3ONI4Mu/2QTc7eTgyLTvOEavYU17PKzNeM0Zjj1SLhotwKSiXq2Nf0xC7Mq5hNq4jcqufG/E3tPlZCcOerqJFP40rXqxhbY3RAXz2rcpeWLu3z7eJ7+6nab0jrzGSLo5/HePvHphrIFKvc3ZU2Ho4DlZPrvT5UP+zcvEIr9DGC6tzcNzlbV1L7ZxIb9MYKeQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=vpuA0NXZZF7huJS24oR1AIv2Iv6uC9hMogsdMvqMV/k=; b=MkkCDJ5C+TqZrt3fF8QSO5tuqOV5f2s/lph4kLUXWlohOGnoMODerYRDGUtjNvgfl83slO8hZQX40ItTRRL8fLqmH6kJCEH+xBwY47i/9psdNHKHMcDKQA1+seTxyidun4AyVlW5jHn2ZACXZU/LpmE4SCjMIUbdNu4XZ3Qtcj4= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=oracle.com; Received: from DS7PR10MB5278.namprd10.prod.outlook.com (2603:10b6:5:3a5::9) by DM6PR10MB2490.namprd10.prod.outlook.com (2603:10b6:5:ae::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4649.17; Wed, 3 Nov 2021 09:49:44 +0000 Received: from DS7PR10MB5278.namprd10.prod.outlook.com ([fe80::6c17:986b:dd58:431d]) by DS7PR10MB5278.namprd10.prod.outlook.com ([fe80::6c17:986b:dd58:431d%7]) with mapi id 15.20.4649.018; Wed, 3 Nov 2021 09:49:44 +0000 From: Alan Maguire To: ardb@kernel.org, catalin.marinas@arm.com, will@kernel.org, daniel@iogearbox.net, ast@kernel.org Cc: zlim.lnx@gmail.com, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mark.rutland@arm.com, samitolvanen@google.com, joey.gouly@arm.com, maz@kernel.org, daizhiyuan@phytium.com.cn, jthierry@redhat.com, tiantao6@hisilicon.com, pcc@google.com, akpm@linux-foundation.org, rppt@kernel.org, Jisheng.Zhang@synaptics.com, liu.hailong6@zte.com.cn, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Russell King Subject: [PATCH bpf-next 1/2] arm64/bpf: remove 128MB limit for BPF JIT programs Date: Wed, 3 Nov 2021 09:49:28 +0000 Message-Id: <1635932969-13149-2-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1635932969-13149-1-git-send-email-alan.maguire@oracle.com> References: <1635932969-13149-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: AM3PR07CA0089.eurprd07.prod.outlook.com (2603:10a6:207:6::23) To DS7PR10MB5278.namprd10.prod.outlook.com (2603:10b6:5:3a5::9) MIME-Version: 1.0 Received: from localhost.uk.oracle.com (138.3.204.46) by AM3PR07CA0089.eurprd07.prod.outlook.com (2603:10a6:207:6::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4690.4 via Frontend Transport; Wed, 3 Nov 2021 09:49:39 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 83f72bdc-7bb6-443b-abed-08d99eaf4375 X-MS-TrafficTypeDiagnostic: DM6PR10MB2490: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3968; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: c7i9TD6h/YOgNhdOoRXRyISFY4T+D7oUpwlQodTAKsE7OII763JkMerPWdTuF5saDUYKw5hv31Rx2RmIPHTr0C/L8L+lqPhBKEJEXpJlmZ0fomauiuNUppCrLO0ykX0VSCaquxfSa+q9kGxSlKpvTBTOKNcNIJvwV07kd+5o4GgiQqr3dOMhqeD6uBJu4LrEtIhkUnX3bQJRATjGOAr7yM07C8QD3rw3fSRKyHm8lL8DTJuLNRI1LqyUbLqEFFYDLkxJUjnApiMd8BJNRWXZ3WKNq/B8fil9u2WqCL5oHmWa9+wPzU5apdWhWM/FJcAZLf7pEXQH6hbd4V6UxWxuoZS0WchidPrdSu5FzZk+pqD9xRHvAixa6wYHA53P/5ZR0MoR3V9FpgZPJy/AumVEyK6LY+gyYz/WAKQpbQEWP4XmOr+wtDwe4avr8slVYcTuSKvp9KnvwAwJ+2tDez54cVVLbbqJFCDo5iinkBUU8Cm2o39e3lzwhP3hqu64WXaJhDTHUjJtuxRYe/d36MmgSB1Pb6F2hTbFo331QTta17oGuKhoYN2ixFcvGSxfRYRbj1hg7X488J5jSMoQBF4TeVlQMG/8hnGcE3GM2AImDRpLDl1ngZbCwpLMZcJEvFs0p/2WGAL+Srpm06yxNwRYwS/wFfIE6Xm/qDmdNseEb6Qb8txzNbGLpXgs0azdHddLsdDC75gLiOzkehbiq0eiyA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS7PR10MB5278.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(316002)(36756003)(2906002)(66556008)(26005)(86362001)(8676002)(508600001)(83380400001)(6666004)(7416002)(8936002)(7406005)(38350700002)(956004)(2616005)(186003)(44832011)(107886003)(6486002)(38100700002)(66476007)(7696005)(66946007)(4326008)(52116002)(5660300002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 1mCW4xftSMo/QzJJChuCJFKtIy3Tq69Z+JooY61TSmuNzGVDskKsye8ib4NVo2I3kro0Hj+udvmP8OJvQbGAPGKUZ0n1fltWekW0On/SoaRadDStBuR0f/J3U5pHWreLMAn8r4mHKbx44Z1hxqMu5CeMXyf79gO4Nr74KBfPRMoMZ9HYJE1hSSP5eBW3uZOIsCtHgjOGrqhK5HF9ONGXsXvvRCPFGvZERw/eU+XlfYhmKJP9aMw2H8RV4MJjwKvFzLHnLHDZ340ugVdq+NA6kKFEBGU6LXYAk+iYvsKSOlDdrLzrFZ5AHmN2XldHdkTRonET/q8nQu/5J5DgnnSvbzvyW46wBebxit4JKepcYGwJxcjASeWGTGs+4ErtqM+gpKay1O2IiKUfkXocv31t/85ZZkeHjhwsukOI+xqMKzUKC96tsCLeOOn9fWWzZEJD4qyEqLtlfInicbbGG9Eyif+sXfpOsjdlEehVQ5R0Fyw9z+ay39IrMbFJDKSam5xITNzTXCaJf17vkYXdR1V0zg1Fq+6M1nmdfjQisb29tbEdO3Zd+0YUwZgLX8PddaDtky9p+eDppsYDpd0nbAA0tsDj49vvqDIzCpgukCZpuAFVjrmrrB0KlEps24wpNhEUuY4Keoh9sQOYWnPnmHphp+2LYQ792JKkxMpHXjhyhnPXJ8Ftb3F89R6er/n2ihqGaprXqzxZpCfoysp8GJhzC19sUeAum/MwOWDrxdkGcSCujp5w6KRoNFmCIxltmTP/ZlyLBeWKurSICgrNyu6RNZ94L0UDnrxULTjb7dzem/fDdCIqeeU+6Whwp4noGRDh4uHFLaUH8Cssp8CUJuMttmbUltpCK7s75EHB2eBwiMmIr8zOy65M80rIPk3JrtsbztrEKNjz0Ccuf/sHLMSzlSavS6OCAl/z9KLegCJPxZP3tDihL3oT2+ZD5ttfjvZxzHqxPStBrene4AO1myDGxTufcqjwnnVUFDcGmFPPpbuONMIX1fXQTMWRocGpcaHjBUAc1d9pHkRtghUtbqs0Wv4zGXkJYaf37cxW+i0fMex+4Q1jbUqhFtlcsJu0mS450HhVsdC5xPf4iSIdMbGCeSZI3Mphv5Uy+6qfcYyWNEJbfYwEbfWPqK5X4A8Rfo/OjUg1Yyy3gsam3ovKNfPIRrzbM37LRscGt4TRxEB6jdo7yDCYf/ktGOWWl1WQfmdB949T1Ph8bO3g/FCDe6JoZEPJBKlbgyjkUxlHUjpedWl3IVTML/7LobaNJijAeFyQk65c+39C/lhO+WcRrB64Kss/XcwaFx+DMhk0UzQL//9WoCPauq86jbPzfk2sjJheqGZKeDDO09F6a8G9+977u4caew94eVohH1JzZxXAxBX6u0lABEpP4L1nobLlMt1RQpuvyTg9GKI6lyxeNTZ+yU7kiH81XD4wM8F2bpaF1JkMWvkDHysxDwI3bTihplpky6pv66b5GG6jdKhfO2aZLQe1f111NAgYmERL9piBIpyyOD/Q8wVbSafP9i+dk7A/TqSHsAbfE/uvUkSJPRRrMcVmQUKlHzOuMdg1863nuOL8qRJU0b43XSiVdE17OVPO99fJKn1F1qQTORFOjcf8nmLJHNyX/MvktNJpwk7hBgE= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 83f72bdc-7bb6-443b-abed-08d99eaf4375 X-MS-Exchange-CrossTenant-AuthSource: DS7PR10MB5278.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Nov 2021 09:49:44.0524 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: RScEQ2eEtUF9VDigVxtdIVQYs1zZY9uq/8Et64bifT4Z4iJ4DRhfjpARkYumeT61u6hV5IPf9D3VajDk5F4LVw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB2490 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10156 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 adultscore=0 suspectscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2111030055 X-Proofpoint-ORIG-GUID: W9jSigDcE-Hl3flK4qujncZc3IaqcAD_ X-Proofpoint-GUID: W9jSigDcE-Hl3flK4qujncZc3IaqcAD_ Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Russell King commit 91fc957c9b1d ("arm64/bpf: don't allocate BPF JIT programs in module memory") ...restricts BPF JIT program allocation to a 128MB region to ensure BPF programs are still in branching range of each other. However this restriction should not apply to the aarch64 JIT, since BPF_JMP | BPF_CALL are implemented as a 64-bit move into a register and then a BLR instruction - which has the effect of being able to call anything without proximity limitation. Removing the contiguous JIT region requires explicitly searching the bpf exception tables first in fixup_exception(), since they are formatted differently from the rest of the exception tables. Previously we used the fact that the JIT memory was contiguous to identify the fact that the context for the exception (the program counter) is a BPF program. The practical reason to relax this restriction on JIT memory is that 128MB of JIT memory can be quickly exhausted, especially where PAGE_SIZE is 64KB - one page is needed per program. In cases where seccomp filters are applied to multiple VMs on VM launch - such filters are classic BPF but converted to BPF - this can severely limit the number of VMs that can be launched. In a world where we support BPF JIT always on, turning off the JIT isn't always an option either. Fixes: 91fc957c9b1d ("arm64/bpf: don't allocate BPF JIT programs in module memory") Suggested-by: Ard Biesheuvel Signed-off-by: Russell King Tested-by: Alan Maguire --- arch/arm64/include/asm/extable.h | 9 --------- arch/arm64/include/asm/memory.h | 5 +---- arch/arm64/kernel/traps.c | 2 +- arch/arm64/mm/extable.c | 13 +++++++++---- arch/arm64/mm/ptdump.c | 2 -- arch/arm64/net/bpf_jit_comp.c | 10 ++++++---- 6 files changed, 17 insertions(+), 24 deletions(-) diff --git a/arch/arm64/include/asm/extable.h b/arch/arm64/include/asm/extable.h index b15eb4a..840a35e 100644 --- a/arch/arm64/include/asm/extable.h +++ b/arch/arm64/include/asm/extable.h @@ -22,15 +22,6 @@ struct exception_table_entry #define ARCH_HAS_RELATIVE_EXTABLE -static inline bool in_bpf_jit(struct pt_regs *regs) -{ - if (!IS_ENABLED(CONFIG_BPF_JIT)) - return false; - - return regs->pc >= BPF_JIT_REGION_START && - regs->pc < BPF_JIT_REGION_END; -} - #ifdef CONFIG_BPF_JIT int arm64_bpf_fixup_exception(const struct exception_table_entry *ex, struct pt_regs *regs); diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h index f1745a8..0588632 100644 --- a/arch/arm64/include/asm/memory.h +++ b/arch/arm64/include/asm/memory.h @@ -44,11 +44,8 @@ #define _PAGE_OFFSET(va) (-(UL(1) << (va))) #define PAGE_OFFSET (_PAGE_OFFSET(VA_BITS)) #define KIMAGE_VADDR (MODULES_END) -#define BPF_JIT_REGION_START (_PAGE_END(VA_BITS_MIN)) -#define BPF_JIT_REGION_SIZE (SZ_128M) -#define BPF_JIT_REGION_END (BPF_JIT_REGION_START + BPF_JIT_REGION_SIZE) #define MODULES_END (MODULES_VADDR + MODULES_VSIZE) -#define MODULES_VADDR (BPF_JIT_REGION_END) +#define MODULES_VADDR (_PAGE_END(VA_BITS_MIN)) #define MODULES_VSIZE (SZ_128M) #define VMEMMAP_START (-(UL(1) << (VA_BITS - VMEMMAP_SHIFT))) #define VMEMMAP_END (VMEMMAP_START + VMEMMAP_SIZE) diff --git a/arch/arm64/kernel/traps.c b/arch/arm64/kernel/traps.c index b03e383..fe0cd05 100644 --- a/arch/arm64/kernel/traps.c +++ b/arch/arm64/kernel/traps.c @@ -988,7 +988,7 @@ static int bug_handler(struct pt_regs *regs, unsigned int esr) static int reserved_fault_handler(struct pt_regs *regs, unsigned int esr) { pr_err("%s generated an invalid instruction at %pS!\n", - in_bpf_jit(regs) ? "BPF JIT" : "Kernel text patching", + "Kernel text patching", (void *)instruction_pointer(regs)); /* We cannot handle this */ diff --git a/arch/arm64/mm/extable.c b/arch/arm64/mm/extable.c index aa00601..60a8b6a 100644 --- a/arch/arm64/mm/extable.c +++ b/arch/arm64/mm/extable.c @@ -9,14 +9,19 @@ int fixup_exception(struct pt_regs *regs) { const struct exception_table_entry *fixup; + unsigned long addr; - fixup = search_exception_tables(instruction_pointer(regs)); - if (!fixup) - return 0; + addr = instruction_pointer(regs); - if (in_bpf_jit(regs)) + /* Search the BPF tables first, these are formatted differently */ + fixup = search_bpf_extables(addr); + if (fixup) return arm64_bpf_fixup_exception(fixup, regs); + fixup = search_exception_tables(addr); + if (!fixup) + return 0; + regs->pc = (unsigned long)&fixup->fixup + fixup->fixup; return 1; } diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c index 1c40353..9bc4066 100644 --- a/arch/arm64/mm/ptdump.c +++ b/arch/arm64/mm/ptdump.c @@ -41,8 +41,6 @@ enum address_markers_idx { { 0 /* KASAN_SHADOW_START */, "Kasan shadow start" }, { KASAN_SHADOW_END, "Kasan shadow end" }, #endif - { BPF_JIT_REGION_START, "BPF start" }, - { BPF_JIT_REGION_END, "BPF end" }, { MODULES_VADDR, "Modules start" }, { MODULES_END, "Modules end" }, { VMALLOC_START, "vmalloc() area" }, diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index 41c23f4..465c44d 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -1136,12 +1136,14 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) return prog; } +u64 bpf_jit_alloc_exec_limit(void) +{ + return VMALLOC_END - VMALLOC_START; +} + void *bpf_jit_alloc_exec(unsigned long size) { - return __vmalloc_node_range(size, PAGE_SIZE, BPF_JIT_REGION_START, - BPF_JIT_REGION_END, GFP_KERNEL, - PAGE_KERNEL, 0, NUMA_NO_NODE, - __builtin_return_address(0)); + return vmalloc(size); } void bpf_jit_free_exec(void *addr) From patchwork Wed Nov 3 09:49:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12600497 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 963AFC433F5 for ; Wed, 3 Nov 2021 09:50:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7C24360720 for ; Wed, 3 Nov 2021 09:50:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231949AbhKCJxX (ORCPT ); Wed, 3 Nov 2021 05:53:23 -0400 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:19520 "EHLO mx0a-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230097AbhKCJxR (ORCPT ); Wed, 3 Nov 2021 05:53:17 -0400 Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 1A39jNBc001867; Wed, 3 Nov 2021 09:49:53 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-type : mime-version; s=corp-2021-07-09; bh=PmaE9TWkSMPOx8p0OiHjtrQfRXGyvONNCgsAEfhasss=; b=YLQe8PY/wGMqaTCCtw4u91JaWJtbepM8lSIPSfMS1BCKRLWT/VlSUSaoDNU5p6a6v5DA VXxpP1GhnE1fyVd1yRjjot9bGIAzby1VCurxd7aLS6GwFYUNP6S5teD25KrHIVw/k3K/ uDiHyrYoug5IPBu1oGUJ7oynuF2KXaY6HA5ekUu8CP1UOEmd5dzpKdP3O4LcWzMPpC9V A2G5K1yiBGAnvGLjLXx7zbSCCQAuQMWel/AxXYeJqEWvXOpmFR1flSzgq05ta0ueamku 3U+J/LatdV1S5BPI/D96ReM4hqlWTqA/eRTJamF9nP4PzGmMaJaqcC+H9OMZF2GNnFO3 Qw== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 3c3mt58xhp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 03 Nov 2021 09:49:52 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 1A39juGs029532; Wed, 3 Nov 2021 09:49:51 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2174.outbound.protection.outlook.com [104.47.58.174]) by aserp3020.oracle.com with ESMTP id 3c0wv604tc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 03 Nov 2021 09:49:51 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Uwk5X5NP81pt3ehWhyU14BdqkSvv3CIHXw3UlM43XCWS0dIBUY4P/prArBHZYdjX1GKqqulkCyI5NB986yXPKYQvgXMa9mOPRmxUnuNhfGR8Z+Xgs8DgkH14OdsaIB7VHuoWyW6S3Ecj71EGqIL1mPxttfsydaSWaACAi79viM7fTSa6Gebd0bLt0KFRVkzrDhQxCcG+CuvbY3t6LMaBjh5iEt5/WxrLNscdYiJCbw47Q6pFhSaRWsSJ1AibI5CG59EcltcE0bmeBi1fshEjWaIh1JKmWfOB/P4fmjdCYJUYZqw17xVY9xCMMH8+sQiWSLGko+Q4TOIno9XtG7g1fQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=PmaE9TWkSMPOx8p0OiHjtrQfRXGyvONNCgsAEfhasss=; b=AHL3whcO+kaBoMl+EwxNgZmqrIxv3dtBQ0SlzbRh639id1Rd9cILVesaKZ+cMttRNzrTmjRmzxB2Uond7nli6azsa0tzBrl/L2wHI5GKmuZZlmtp6i0eokGbpgU0lUEyoPzj6KE5NgNJzcBBbK1eie5TF6mB9AP6IVkABlDmZShweV+7/t1DkJidnArXNwY2SrC9uD9FWDssOfz/v5KE2TBZnK3gaEYk+a9ADYn0Cq1wtuSLAzLkJXL65wsxUBnDBoGgkagVqbGbOVWbnNhBvEzKxKAXTq4lXvNhE+y5hsxTwfcji1qHkjlKH26fFUVWQq5vak1woYBGwYuoFI5pHQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=PmaE9TWkSMPOx8p0OiHjtrQfRXGyvONNCgsAEfhasss=; b=RDocs0SaE79rs+wgR5FIKc3qiLJD+lpFPYGT/JT6MutMrxlJKiG/ollRFidoS8PYrOvteIDCyYS7JXGNTg6hjlYKTPgeFb2neSss874h7bu/oxIDuJjTKEwsridRg+pJyvlJaCbdqSZ1l/Qj5lKe6Xn6buEq+jQaYkZZnQdZlws= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=oracle.com; Received: from DS7PR10MB5278.namprd10.prod.outlook.com (2603:10b6:5:3a5::9) by DM6PR10MB2490.namprd10.prod.outlook.com (2603:10b6:5:ae::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4649.17; Wed, 3 Nov 2021 09:49:49 +0000 Received: from DS7PR10MB5278.namprd10.prod.outlook.com ([fe80::6c17:986b:dd58:431d]) by DS7PR10MB5278.namprd10.prod.outlook.com ([fe80::6c17:986b:dd58:431d%7]) with mapi id 15.20.4649.018; Wed, 3 Nov 2021 09:49:49 +0000 From: Alan Maguire To: ardb@kernel.org, catalin.marinas@arm.com, will@kernel.org, daniel@iogearbox.net, ast@kernel.org Cc: zlim.lnx@gmail.com, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mark.rutland@arm.com, samitolvanen@google.com, joey.gouly@arm.com, maz@kernel.org, daizhiyuan@phytium.com.cn, jthierry@redhat.com, tiantao6@hisilicon.com, pcc@google.com, akpm@linux-foundation.org, rppt@kernel.org, Jisheng.Zhang@synaptics.com, liu.hailong6@zte.com.cn, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Alan Maguire Subject: [PATCH bpf-next 2/2] selftests/bpf: add exception handling selftests for tp_bpf program Date: Wed, 3 Nov 2021 09:49:29 +0000 Message-Id: <1635932969-13149-3-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1635932969-13149-1-git-send-email-alan.maguire@oracle.com> References: <1635932969-13149-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: AM3PR07CA0089.eurprd07.prod.outlook.com (2603:10a6:207:6::23) To DS7PR10MB5278.namprd10.prod.outlook.com (2603:10b6:5:3a5::9) MIME-Version: 1.0 Received: from localhost.uk.oracle.com (138.3.204.46) by AM3PR07CA0089.eurprd07.prod.outlook.com (2603:10a6:207:6::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4690.4 via Frontend Transport; Wed, 3 Nov 2021 09:49:44 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a624aba9-91d4-4eec-6acb-08d99eaf468c X-MS-TrafficTypeDiagnostic: DM6PR10MB2490: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5797; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: d98Ibjwvb1UITU67vBkqwBhRta9Y0BMV56bQY5d1nCXAOjHWJUB6y7lVxQKE9CyW10607O8NQ+67+GP0BNtnrYUKjoRG5nwnF0DEhqp9hatRsL77jE3eyoVNPkvWkP42BkrV+JHQmKV3TdTd2JRkMUtaUc9LltLa0Qj9mXu6Xnuv1dcjBFmP2z6NeBrOtsEk5NLikZS+33fK3bzicZON5myW/Bnj+aV19CzlXM/qmq5mmlfSTJOWVtDeKBxWxUI1nam+/yANc5o0BWFgEHBtWTXFqDIJPIag2QY3tUehp+L/j0N1aRfQ0yXu5c+pVyCf0eDWS2wzWrRhbjHB0K9qLXJt9sLnrHNqp2bE543GYbkUkhQVtQKY4CZLrCuflPS3scEKaeKW52ZpG1zjBagapvj4iuAQVy/NSX3Z5U76Rhx5LzD2xT3wt2y5wzDBqwO/9F3eAl957SVDstOE1237i+3z3BvxlrhO4MTP6sqVog0Id/Wj0RXDi6c03SyfJCdhvv3uaJecbRWUErsibbqJenoF7tb78O6FCFvz+OgBrmjOWS5hpY2ilX8ANANCTXYKWmdqoAdfi6xNwqUtt8mrvFy1F9MFZPRN50B/MYhLvrBATQJhG6LyJljHNpVr1696/Yc374jozf8xB6CyTldPj+Vt0WVfbBMV4cJyaPQkrIv2+qyNWIBcGwVVqHc2aUqpv0LJMg30vUb5Bm80V6xrBQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS7PR10MB5278.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(316002)(36756003)(2906002)(66556008)(26005)(86362001)(8676002)(508600001)(83380400001)(6666004)(7416002)(8936002)(7406005)(38350700002)(956004)(2616005)(186003)(44832011)(107886003)(6486002)(38100700002)(66476007)(7696005)(66946007)(4326008)(52116002)(5660300002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: dL0Lm8pbDUTwCfPSmODCl92EP6qHV/FD88gEotLPBlr/jC+Af7bMbk5xqy38mlGj3R9LQdsBpwMXMKO5fteC3HyAiYBbygwYWdVSxt5CVO94YNcx2REDS1otcoAXBvSZpHFA3pVIFsmqAbkH7p06zMHhDwiU0lviDQQNCBpzqYqJC8j3iUsTSTAKLFhQVZdmQpDnBgrDx48ESSCwoQoQ3sET3s7wKGxewFWE9q0Ms79e2O83E3WIfSuOobJtT1h9Btrp3ugY9COx9LJ8S8kSoVIhMTrjKqcARulcyLulH5VqHVN2cl1d2n3f6vfSvLAo7xSBY5UvH6aQEghiVpdrg4EhRyOZUKRN/kmWgk99GhbRVIRL9oLS82mw8eK1R6tORFhyQvCSbBO2pC2B/nk7d5DLhNwMESTvF2OpNh6Num6s4iSCWBd1KvQMqPVRTsrhmvF6aL03IfSj4tnuGsadeyWBiuV2FTToIBparsfIpliuraexo0FEnH1vUQcJwHuUx9wf19J6nguKQh0DsSrn4jP4tbVJw9GmMgSr6bhbmzTiELuBqtxPrBoQf2QMrnG7RyZzdLTHhAFQp5gj1pi3ARyYHvXZQd5Ppn/w98u9tgd6yz8unXmojScafS/k3rpkqX1X9e756QxBtSS71/j8BMZFyOACiDOi7gNI+Ot6C51lWnzaVyVyfQCau2Z5vGFnXb+gz8ob/6Y7tsWmTb/5rLmdwtJwUhjRW6R+hs2g3c2Shkud+Akj2AKpkAsp+++9Z9X7R531RAPXbW0Gl8jMtnt5ZA5IycWneaCDwunHhGqhj84VuIX7FpTgOAKtsALcd7Io5oR/InKHN01+JvmCpuHEDqGtVFt4To9KFyiy2gy26KQ6m9tyOba+3oBZU2WQozHAxhHY1eXPoJCrHhErVvDnJCO5Gkh1caSTMFUT1ZisXveOUw2JxyH+i7h3Dlif3cidOPF2kDADHCVCymlp25F5NSRku/KgrPhaTROBzVacLjPrLRnNP8DafKtrtG5MLS/7doasrZRoYpfPW7aKF8de4+K4jXcoznfcfpK4/FCypVgC+2pjIRyPISye+Zmmd6BPMUxUzHMn1w7W0ONnY/q/Frk/fk9wPxdw4tewjK3K2FEU9DEXIXmQGLDMjDVK3t05oPvjg9hLrl5AyffrJdzQw1SLYv2a5WGqq/PcWpewIEH3UlFvHJxc3OIfpGsHfLyc3NnRy7HY8GBfeosekPayJj9vlnGHSl22iHZyoTkZR/dB2rbOq6uRrqIk4yZt0ZQc2aRauEW3c6WlE9rOoB7dsJIu96vHRkzd1FpchOAAQhyMMpCF8hqdE/MpbTgjpQe2ZPX/nhzlv8ZzhNXl2OxU/z1wu1ym4XtP56NVbmGoICw4745z/t21hLOQnrnl1xGf4J91WWyw0rOVP9rPzUL50J5TxaBbrzUiiBRp4ljFLtSTmA+8cKlm9/GLe7A4DV7tMNnLXA+UgBzv8+0vLQS8e/Vlr2/kzHhz+sxYy39HZoTy9M72VIBAlxEoZlEJrb1GLtC23xMs5wRqUUfbBFMVA8mm7rTjGlSE78WGC+2R4IbUPvK83ld+EN41cqf2+Fu86dlayPNQaQdXrEJ4rrqPfJaHZ7sBE9sfnR5vr5M= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: a624aba9-91d4-4eec-6acb-08d99eaf468c X-MS-Exchange-CrossTenant-AuthSource: DS7PR10MB5278.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Nov 2021 09:49:49.2436 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: qeKx8dezlbsR8PAd8yuRwKP+AuMPJ3GcDNvlztxFAAENU0Z31J2ctHZrZFoWshFBMlmcZuR5Nz2byOjB8dheJQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB2490 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10156 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 phishscore=0 malwarescore=0 mlxscore=0 suspectscore=0 mlxlogscore=999 adultscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2111030055 X-Proofpoint-ORIG-GUID: Sg-vJPoLRvC8oyn5yzt-B-MIo9Ce3Y9s X-Proofpoint-GUID: Sg-vJPoLRvC8oyn5yzt-B-MIo9Ce3Y9s Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Exception handling is triggered in BPF tracing programs when a NULL pointer is dereferenced; the exception handler zeroes the target register and execution of the BPF program progresses. To test exception handling then, we need to trigger a NULL pointer dereference for a field which should never be zero; if it is, the only explanation is the exception handler ran. The skb->sk is the NULL pointer chosen (for a ping received for 127.0.0.1 there is no associated socket), and the sk_sndbuf size is chosen as the "should never be 0" field. Test verifies sk is NULL and sk_sndbuf is zero. Signed-off-by: Alan Maguire --- tools/testing/selftests/bpf/prog_tests/exhandler.c | 45 ++++++++++++++++++++++ tools/testing/selftests/bpf/progs/exhandler_kern.c | 35 +++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/exhandler.c create mode 100644 tools/testing/selftests/bpf/progs/exhandler_kern.c diff --git a/tools/testing/selftests/bpf/prog_tests/exhandler.c b/tools/testing/selftests/bpf/prog_tests/exhandler.c new file mode 100644 index 0000000..5999498 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/exhandler.c @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021, Oracle and/or its affiliates. */ + +#include + +/* Test that verifies exception handling is working; ping to localhost + * will result in a receive with a NULL skb->sk; our BPF program + * then dereferences the an sk field which shouldn't be 0, and if we + * see 0 we can conclude the exception handler ran when we attempted to + * dereference the NULL sk and zeroed the destination register. + */ +#include "exhandler_kern.skel.h" + +#define SYSTEM(...) \ + (env.verbosity >= VERBOSE_VERY ? \ + system(__VA_ARGS__) : system(__VA_ARGS__ " >/dev/null 2>&1")) + +void test_exhandler(void) +{ + struct exhandler_kern *skel; + struct exhandler_kern__bss *bss; + int err = 0, duration = 0; + + skel = exhandler_kern__open_and_load(); + if (CHECK(!skel, "skel_load", "skeleton failed: %d\n", err)) + goto cleanup; + + bss = skel->bss; + + err = exhandler_kern__attach(skel); + if (CHECK(err, "attach", "attach failed: %d\n", err)) + goto cleanup; + + if (CHECK(SYSTEM("ping -c 1 127.0.0.1"), + "ping localhost", + "ping localhost failed\n")) + goto cleanup; + + if (CHECK(bss->exception_triggered == 0, + "verify exceptions were triggered", + "no exceptions were triggered\n")) + goto cleanup; +cleanup: + exhandler_kern__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/exhandler_kern.c b/tools/testing/selftests/bpf/progs/exhandler_kern.c new file mode 100644 index 0000000..4049450 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/exhandler_kern.c @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021, Oracle and/or its affiliates. */ + +#include "vmlinux.h" + +#include +#include +#include + +char _license[] SEC("license") = "GPL"; + +unsigned int exception_triggered; + +/* TRACE_EVENT(netif_rx, + * TP_PROTO(struct sk_buff *skb), + */ +SEC("tp_btf/netif_rx") +int BPF_PROG(trace_netif_rx, struct sk_buff *skb) +{ + struct sock *sk; + int sndbuf; + + /* To verify we hit an exception we dereference skb->sk->sk_sndbuf; + * sndbuf size should never be zero, so if it is we know the exception + * handler triggered and zeroed the destination register. + */ + __builtin_preserve_access_index(({ + sk = skb->sk; + sndbuf = sk->sk_sndbuf; + })); + + if (!sk && !sndbuf) + exception_triggered++; + return 0; +}