From patchwork Fri Mar 8 01:08:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13586410 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 1E8E9C54E49 for ; Fri, 8 Mar 2024 01:08:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9A8A16B02F6; Thu, 7 Mar 2024 20:08:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 930A36B02F9; Thu, 7 Mar 2024 20:08:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 75CE96B02FA; Thu, 7 Mar 2024 20:08:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 634356B02F6 for ; Thu, 7 Mar 2024 20:08:33 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3D3F2A1A25 for ; Fri, 8 Mar 2024 01:08:33 +0000 (UTC) X-FDA: 81872086506.15.49AB16B Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf22.hostedemail.com (Postfix) with ESMTP id 7D9BEC001D for ; Fri, 8 Mar 2024 01:08:30 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=i1+qdkPs; spf=pass (imf22.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.180 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=1709860111; 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=9lNO6MUM1XQN/fA7/+ZFYcvnsx/bodyKeSUW0Kv64/w=; b=vhbz9fjoZO1GrOsEtb8LeSCFNXe+cYkgvKqo+gw8lqP9AixF47g2tEVCrzKl1Vb6sW7zru Qs7quB8O38JbidB0gHU/w7XN6jkE9AqiGGLxeCMNXSo3v+YHCmrEtINHMFOn7UuquU+UMr LkXVwhDTznpKivQ5LG3AvBhPi7GfT+A= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709860111; a=rsa-sha256; cv=none; b=ARgo5qShm8aeL9LXWaohw90+8O54AXJIaJpoORTMiI5htcAf/8OEl2vTNm3ErZ1hOMJYNI pU0VqH53XLuM7h3ONxkBDubCy7TOKrarsHFybnMGy/9dtGHJf/qtI4QjmgoB1IP3egY43e XSIwQfkTArQqLEL9wffA36yGQbw942k= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=i1+qdkPs; spf=pass (imf22.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1dd5df90170so7896375ad.0 for ; Thu, 07 Mar 2024 17:08:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709860110; x=1710464910; 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=9lNO6MUM1XQN/fA7/+ZFYcvnsx/bodyKeSUW0Kv64/w=; b=i1+qdkPsP0waIBDUmpSdhMd51m68h2Rq1d72blfK2Wt1hp/oSgwxu8DHpNR8qPCg9S QqzRMBYfInFYfGVVu0ibpcJ2cOE3zXdaAzsVA/fxrv/6xVTKt2Uq3UodtVT7Ld7XdzcF X6pNYq6ogj+G/c4SzRmQuKgltGZwbsJYmwqoy5lDdg1S2YIGdr3YFU9S6YQXofJSRPhr n9bHLAqBNWTM+jKOaG4LDzfiKp02f93dl+bWcD3MYk9uKOfjv6mwIhwrfxShpx5Npkf9 Ou+xFx2/bovIWpJWeACqF3Bey9xAIzC6soGU50YSTXfD2KeNxavQ6ebzVH7gWtD3MGu1 DW9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709860110; x=1710464910; 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=9lNO6MUM1XQN/fA7/+ZFYcvnsx/bodyKeSUW0Kv64/w=; b=Q0M0vl+EjFvMn8gZ40RHF6PS8Mm9xMzpF7jVXAn9Q63U6ppbV7dahPjWpjM2+Hn8O8 jvepR5/gRcb+ZyRqcUrxB+XBhKlO7DKuYOphNJpKf/kWF4QBS7vqLY8yC3T5Ufsrurix 71pCLk12Dcdfp1xY9FxPSV4QHpseAyoBGh9zlwl7UXEdZHxhHDg3Ebu75maIa67gZcYN hQDHUmleTGmnrNrmv3oYO9Y9oFvgHVi1+ezfHyji/mjC1TH6I1JoCKRYQEFql2SId7kl y9kg89I9X5c5THQBnL4LP1EDXcM38JfoWpCbS6JxpqzgudVfEoD1rfAyPX9+IL9dmHdI 0HwA== X-Forwarded-Encrypted: i=1; AJvYcCXXYtIYxlV/PGak5tdsYEI6TXlFFNnOiX/83DWHuUm5quk0uqJfQHtQw7hoNEwS36JRV0SQHY3T9gMcXZ0VXto26OE= X-Gm-Message-State: AOJu0YwpGlTocQESEuNSRFRsaMKMVwUtiRVxS0/K0d1KO4d6vEHA+M65 OpPQ+u164SY9QndOhBXF2c0Vs8k+xyZhBWr9u91Lo9ifgaZe3KB5 X-Google-Smtp-Source: AGHT+IH3LmY8um4QfWO9Vz5IlIKIu4RcjPZ/VOanHoOrMmCGtDlzXHJV3149i9SmiZdoH8wCtAcUkw== X-Received: by 2002:a17:902:d511:b0:1dd:91c:249f with SMTP id b17-20020a170902d51100b001dd091c249fmr4447234plg.24.1709860110405; Thu, 07 Mar 2024 17:08:30 -0800 (PST) Received: from localhost.localdomain ([2620:10d:c090:400::5:428c]) by smtp.gmail.com with ESMTPSA id w1-20020a1709026f0100b001dd66642652sm360454plk.190.2024.03.07.17.08.29 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 07 Mar 2024 17:08:30 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, torvalds@linux-foundation.org, brho@google.com, hannes@cmpxchg.org, akpm@linux-foundation.org, urezki@gmail.com, hch@infradead.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH v3 bpf-next 04/14] bpf: Add x86-64 JIT support for bpf_addr_space_cast instruction. Date: Thu, 7 Mar 2024 17:08:02 -0800 Message-Id: <20240308010812.89848-5-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20240308010812.89848-1-alexei.starovoitov@gmail.com> References: <20240308010812.89848-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 7D9BEC001D X-Rspam-User: X-Stat-Signature: iyiy9axeo57kbk18bhyqrnkp9esmjkzf X-Rspamd-Server: rspam03 X-HE-Tag: 1709860110-40243 X-HE-Meta: U2FsdGVkX18iyyorfhOBIRy3pEA7EnQm3SRMlltFpzgPgBUxNGazHqPhapYGEOYE/MO6aeh6tp3LlgLpOrKbe3bJQQJqoxFNNCtlF5FRd8kb6dX4OocNQdyb2hcSvn7cqcgVWMvBOZkDX98Ez5Jqsot0YZfFYgbc7tGwmPWBfRMPlPImIuZBxxYFPvKN0IZ7l81l8LcYyVXK/cVtkUXx9I1GjQZP9qianOyD2fiH6vHgCh4x9Ym6Jc0kkVadtmFE4QZV6080498R95Ww3Kx+yN2tfJ+EaE7YliGhidolm3r4d5e++U0G+T2BqTWR09MJemMvZ09K9jJfDvvKw7b3ZbnUYNGmT7kIHNHLYB0NsmUSdJDm9YOA6TL01Yglk84xnvm3G2p7+weeygLAZGmYLh52syg/LX+Cn7fmvK2H5xmCF+OS+CxttDRU88QfQBfC6fiB/7wqnHCe3ql1+a30ciKc/p/oMKSysoU+1UCF1GBJOvF3i55Kjo2ZEkSxR24HvDbDUYK+mZ1bNwHczm4UOAc4msRt8tl8vGS+UVroZmlKYwtIZ9Zy17jH3da7+yTmovKgNTYT8NcdNkscXt/iI6/e3gOP/cZQmOwTmJl6gKi7C4LVC3SieiU7OUPyObZQOKg4sDmqO6snM4xdpir5lo+bmBv2BjYCOqYThal3wlIRP9VCdRfQlZlNtOKQqlBUasoA1GrDZk5wzpybQ0M1Xk4GJ67klsoCJmiFsh85E8LwDaW810AKWIINUqflIrBYgESx+4uN066/fzEzt0xpYbLcfuBnA+vf14EQjKAWexsa2EKEDztVkxIMGzQPnK3K2ifA72x48JHhhQ/rWFU0UWBCLtYMyHiFrtQIRevlGwle0zITdLKs/WcnP4onNghGx2rZfQGZll5pVCCkRx/5p7uMaZHliKU+9i1WsY21QaQOiasfKn5jqpFbrIPv0lp9aoQygbzF6YyqJaMsaJL 5uISoKqk TfrZUuhcL48aXACP3cKpZv5cIbOsXuaLIJhTV2aubpoEc7doizWsCKRqrCF90y+np/7sKXpNa8IC0yEb7NGZ2fTHLSSNA15MRkwhhwoWOprLGqeDDFKgAV3qA5hIBEbvdRz2hV17AjIjQKQlxxWE6jTM2RJDh4gr4FEN9P6ITyU2/oDJ+71PFeHt43LLFYbNOfHD5 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_addr_space_cast instruction while translating pointers between native (zero) address space and __attribute__((address_space(N))). The addr_space=1 is reserved as bpf_arena address space. rY = addr_space_cast(rX, 0, 1) is processed by the verifier and converted to normal 32-bit move: wX = wY rY = addr_space_cast(rX, 1, 0) has to be converted by JIT: 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 Acked-by: Eduard Zingerman Signed-off-by: Alexei Starovoitov --- arch/x86/net/bpf_jit_comp.c | 42 ++++++++++++++++++++++++++++++++++++- include/linux/filter.h | 1 + kernel/bpf/core.c | 5 +++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index 38705a1abe62..27058d7395f6 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -1276,13 +1276,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); @@ -1350,6 +1351,40 @@ 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_ADDR_SPACE_CAST && + insn->imm == 1U << 16) { + 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, @@ -3432,6 +3467,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 b119f04ecb0b..c99bc3df2d28 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -962,6 +962,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 a8ecf69c7754..bdbdc75cdcd5 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -2932,6 +2932,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