From patchwork Tue Feb 6 22:04:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13547842 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2AA5AC48297 for ; Tue, 6 Feb 2024 22:05:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AEE276B0075; Tue, 6 Feb 2024 17:05:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A9BDB6B0093; Tue, 6 Feb 2024 17:05:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8EE4B6B0095; Tue, 6 Feb 2024 17:05:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 769DD6B0075 for ; Tue, 6 Feb 2024 17:05:16 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 451E1A152C for ; Tue, 6 Feb 2024 22:05:16 +0000 (UTC) X-FDA: 81762760632.13.8C2FA4A Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf05.hostedemail.com (Postfix) with ESMTP id 6E845100003 for ; Tue, 6 Feb 2024 22:05:14 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=FSVIF6Xd; spf=pass (imf05.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707257114; a=rsa-sha256; cv=none; b=eklufzEy92+zVAoOfRCAOsXFCp9TOXTItD8b0qds9R1SE0v05RZwdd22cpt2cNf2zoG8+t iXp09GUc65UgxuZjvWjec9nhTcIzIOKDCw5gGpBwvc5TfQAPgp6+emWZOj7yV2J+SLAdT6 /PjcoU0DJ0nGBmIaI95AADSMyht5UTA= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=FSVIF6Xd; spf=pass (imf05.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707257114; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=GxmcZQXB6hILUuERStuFRZ0MPT4uFMATBLnu6rXBqLE=; b=FidUVpO/AedMg6KAjiXK4sWxORQ/hlXabCPWGhiCxmWgP7m08FN45tEcNuxWg00YiWEyg0 F6GzfTCrrOyFprLhaKSojWgdPo4taJqJ+dausB7SAMLmaPqmlr+5s+gysrvKzh0mWY6hQw YSVbt8U5awr5WkZIXJzlQnrMoMKaXxo= Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-296dcd75be7so272515a91.2 for ; Tue, 06 Feb 2024 14:05:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707257113; x=1707861913; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GxmcZQXB6hILUuERStuFRZ0MPT4uFMATBLnu6rXBqLE=; b=FSVIF6XdUmP5EhTnYRwG7lEwIDgRdLf0sHK+7c0PH4w5gnPlDP/XklF/fFz3Xy0VFl 5OFuwzvZr9sm33I4WLA87yRpx0GhTrbFENsDnIZrvR5xmlePvJ75UWR2VkNQVRPyc/nZ J1oHpGuu4jgRBRS/BpFZfMG+74dXWqLR69d3fF1BWB4B9F1dAmP9dPCSy5tqPSKqQsrL YUddlJOPObK1Sn4JqWY0dWCY2u6vKt28S/UaKEV2FOGUlZljV2hxHZriDDfwQL57k2Ve HcXD9a2rziWLeAV5CRcX8T5lUmQQISJtpV4ZVpV/aCf6zHO7jnauUCMQieNRGcVSrLm/ c3SQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707257113; x=1707861913; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GxmcZQXB6hILUuERStuFRZ0MPT4uFMATBLnu6rXBqLE=; b=ZSP/M3XT/XE/kE0PrHBtdwJIAxKJPMAxQ7NIxaU/L+uyIkLL/yhKnzIUvnD4oIrtG2 M1vDT7boipqDxKhOBZkeY4vOaSTjjQ2fn0YeFo9lDm08yVea8nP+uYDLqQg4Y95ViLTL TU4zCTSQO1yPBO7HNMmWNqqTe5hN3siZHgkMy8u3WRRIOcojBryPe2UQ4TDpcQix2Kg/ hF38A26J/2ujFWkNtPYWDRj5WVL1mY5TyY2vqqVdYJBfhqGI7Ku13wDppKm/36icsvW6 W3U7b9VMtr9BjqKFlrs3FXaIUbMCncds0bHCan04ioOoPbFserzw1tDV3aWfHIjxZ6oz W6sA== X-Gm-Message-State: AOJu0YzL0el2UOb1Z3wmYkh/IQ6lDOJAjTe74K9drJeBrtDLA5KHcaTH +Y8EMeMVL0C/2/yGHb36aOYrDuts8VS5l9GqFQZqpKDeGtU6igad X-Google-Smtp-Source: AGHT+IG8oAHzR3nsr++sOa64aFqWnOY2L1+MMxXsvlNE4aY5ZiS0oyor0jQVKD0uoMrje1RiSvSdsA== X-Received: by 2002:a17:90b:350c:b0:296:c1a:f651 with SMTP id ls12-20020a17090b350c00b002960c1af651mr884749pjb.36.1707257113308; Tue, 06 Feb 2024 14:05:13 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXfwYB7FRrQvWfTk6tEr+uUBGbmaCq7+V+6esU7E5tJSAj1KK9tCNvR2xsEZBB081Xgy46YpR2sEOIbwIiK/1CsFdSNyQsZddCFwiLLxjRVFRaNhwuCFDq191OQ8uonJA+2UerpQK+JRy9zZAlFHa1wanOWMMIS+Hq7SpgXQH9IqaaVBQYUqYpa2d39hQfaUwO1B6BULxhIfZ+C2s4IrW++qdX5AoMMGyC9BAWf+AY6fWAWyCtugRHgQWZ74iaZkRKe4wMe8lkrZkmmvR1EPCVE9kBbPDe3DnDo Received: from localhost.localdomain ([2620:10d:c090:400::4:27bf]) by smtp.gmail.com with ESMTPSA id li4-20020a17090b48c400b00296b2f99946sm7168pjb.30.2024.02.06.14.05.11 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 06 Feb 2024 14:05:12 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, memxor@gmail.com, eddyz87@gmail.com, tj@kernel.org, brho@google.com, hannes@cmpxchg.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH bpf-next 07/16] bpf: Add x86-64 JIT support for bpf_cast_user instruction. Date: Tue, 6 Feb 2024 14:04:32 -0800 Message-Id: <20240206220441.38311-8-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20240206220441.38311-1-alexei.starovoitov@gmail.com> References: <20240206220441.38311-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 6E845100003 X-Stat-Signature: 3m49ue3uwijn9ubkf5xq3g9dufbj5ibr X-Rspam-User: X-HE-Tag: 1707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03wU1ufQycITSi+5i92R8dnd6Ra2sYzFOmOvI6+6jvIE509Q0C4Ftny+C1vKolWoPtRYK4I452l8o2jlz9dxgPm7ZGMO4hTP4r4RDuDhH1BYvabeYUC2qSTrHYvCzUnxUdifhdfDeLieaWcMJV25xUVNqOsP9XyzP1m8nFVfsyaC/s2NvDmFfQ6jf1NtV0xRHoM/NYu+ScVnDk+oLsaCRj6HZ+M+ja3bYD5gp64vghOuhxDcPbQAx97m4eX0DRKqqqKp1+FKPs7ML4/X03+DAyRK9Qq3WKwQvavVcTCxgbHYwi9XakYYN00HSHYlaxBfAvhHSGA4h+x8m2G5OjgwLCe8fcv6p+hr/+y7/jcXqI2WiA6PmjrAnYqsfb2tmRNlC92LAAVCdqMyE9XgoFQSbdAvH8nOx4TRNEn3BEnSSqkzEETHErEFZSndsNj4gS87awn3ofk1HQmn+wyg6tkJTvfGlGrdcJi1L0jTkeo8jxe X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Alexei Starovoitov LLVM generates bpf_cast_kern and bpf_cast_user instructions while translating pointers with __attribute__((address_space(1))). rX = cast_kern(rY) is processed by the verifier and converted to normal 32-bit move: wX = wY bpf_cast_user has to be converted by JIT. rX = cast_user(rY) is aux_reg = upper_32_bits of arena->user_vm_start aux_reg <<= 32 wX = wY // clear upper 32 bits of dst register if (wX) // if not zero add upper bits of user_vm_start wX |= aux_reg JIT can do it more efficiently: mov dst_reg32, src_reg32 // 32-bit move shl dst_reg, 32 or dst_reg, user_vm_start rol dst_reg, 32 xor r11, r11 test dst_reg32, dst_reg32 // check if lower 32-bit are zero cmove r11, dst_reg // if so, set dst_reg to zero // Intel swapped src/dst register encoding in CMOVcc Signed-off-by: Alexei Starovoitov --- arch/x86/net/bpf_jit_comp.c | 41 ++++++++++++++++++++++++++++++++++++- include/linux/filter.h | 1 + kernel/bpf/core.c | 5 +++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index 883b7f604b9a..a042ed57af7b 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -1272,13 +1272,14 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, u8 *rw_image bool tail_call_seen = false; bool seen_exit = false; u8 temp[BPF_MAX_INSN_SIZE + BPF_INSN_SAFETY]; - u64 arena_vm_start; + u64 arena_vm_start, user_vm_start; int i, excnt = 0; int ilen, proglen = 0; u8 *prog = temp; int err; arena_vm_start = bpf_arena_get_kern_vm_start(bpf_prog->aux->arena); + user_vm_start = bpf_arena_get_user_vm_start(bpf_prog->aux->arena); detect_reg_usage(insn, insn_cnt, callee_regs_used, &tail_call_seen); @@ -1346,6 +1347,39 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, u8 *rw_image break; case BPF_ALU64 | BPF_MOV | BPF_X: + if (insn->off == BPF_ARENA_CAST_USER) { + if (dst_reg != src_reg) + /* 32-bit mov */ + emit_mov_reg(&prog, false, dst_reg, src_reg); + /* shl dst_reg, 32 */ + maybe_emit_1mod(&prog, dst_reg, true); + EMIT3(0xC1, add_1reg(0xE0, dst_reg), 32); + + /* or dst_reg, user_vm_start */ + maybe_emit_1mod(&prog, dst_reg, true); + if (is_axreg(dst_reg)) + EMIT1_off32(0x0D, user_vm_start >> 32); + else + EMIT2_off32(0x81, add_1reg(0xC8, dst_reg), user_vm_start >> 32); + + /* rol dst_reg, 32 */ + maybe_emit_1mod(&prog, dst_reg, true); + EMIT3(0xC1, add_1reg(0xC0, dst_reg), 32); + + /* xor r11, r11 */ + EMIT3(0x4D, 0x31, 0xDB); + + /* test dst_reg32, dst_reg32; check if lower 32-bit are zero */ + maybe_emit_mod(&prog, dst_reg, dst_reg, false); + EMIT2(0x85, add_2reg(0xC0, dst_reg, dst_reg)); + + /* cmove r11, dst_reg; if so, set dst_reg to zero */ + /* WARNING: Intel swapped src/dst register encoding in CMOVcc !!! */ + maybe_emit_mod(&prog, AUX_REG, dst_reg, true); + EMIT3(0x0F, 0x44, add_2reg(0xC0, AUX_REG, dst_reg)); + break; + } + fallthrough; case BPF_ALU | BPF_MOV | BPF_X: if (insn->off == 0) emit_mov_reg(&prog, @@ -3424,6 +3458,11 @@ void bpf_arch_poke_desc_update(struct bpf_jit_poke_descriptor *poke, } } +bool bpf_jit_supports_arena(void) +{ + return true; +} + bool bpf_jit_supports_ptr_xchg(void) { return true; diff --git a/include/linux/filter.h b/include/linux/filter.h index cd76d43412d0..78ea63002531 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -959,6 +959,7 @@ bool bpf_jit_supports_kfunc_call(void); bool bpf_jit_supports_far_kfunc_call(void); bool bpf_jit_supports_exceptions(void); bool bpf_jit_supports_ptr_xchg(void); +bool bpf_jit_supports_arena(void); void arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie); bool bpf_helper_changes_pkt_data(void *func); diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 2539d9bfe369..2829077f0461 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -2926,6 +2926,11 @@ bool __weak bpf_jit_supports_far_kfunc_call(void) return false; } +bool __weak bpf_jit_supports_arena(void) +{ + return false; +} + /* Return TRUE if the JIT backend satisfies the following two conditions: * 1) JIT backend supports atomic_xchg() on pointer-sized words. * 2) Under the specific arch, the implementation of xchg() is the same