From patchwork Fri Feb 9 04:05:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13550849 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 B06A2C48297 for ; Fri, 9 Feb 2024 04:06:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 40E8A8D0005; Thu, 8 Feb 2024 23:06:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BE608D0001; Thu, 8 Feb 2024 23:06:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 238BD8D0005; Thu, 8 Feb 2024 23:06:50 -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 111C38D0001 for ; Thu, 8 Feb 2024 23:06:50 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id D8DCB802DE for ; Fri, 9 Feb 2024 04:06:49 +0000 (UTC) X-FDA: 81770929338.14.9B36498 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf19.hostedemail.com (Postfix) with ESMTP id F15051A0011 for ; Fri, 9 Feb 2024 04:06:47 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=BaQ1HbKh; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf19.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707451608; 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=a7NNsrQXKlR8rhOFWaF2NfYAzQSQVYCfqzniPfBCXaG+7uHn8FPeQwfdWMvwB9fS0ij7yp LvdyYoEtdhgARMOZTnQ4g6GO0UW31Jh6GNEqTxx9URrNSS1e7C/QDaJQBdORr52A1CoSYC dEBq3fsO7k8qiTAJCUOM2Ouyvq8bX94= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=BaQ1HbKh; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf19.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707451608; a=rsa-sha256; cv=none; b=Rexb/6uYQMNCSShU0xtQXEWOileVf3/kBtcQgHDmsMO2cZdKgif7m5nzgX1yZtsoKHuGO1 TdllrYznbIP1v3bJFJIg5kOP5lRIgVc++Rmd9Ec5xY3ZVFIjqdWn78W7zs/Eq6uQmqr/aN HyNYqM0MqVQUZpCGNEIZHQkIbxiTExA= Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-5c66b093b86so1153443a12.0 for ; Thu, 08 Feb 2024 20:06:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707451607; x=1708056407; 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=BaQ1HbKhYZ312r4b8R231508PO8kDj1aA0QeE0Q9SafO+lzm9Ke9qq4KbIoArkNTgZ 3vWe7w6qBGjb0jCd6YiIU5T/ToKTHRpuOLcGG2R3UWv1qsaZzg4nkDF7xJlQoXRRFLY7 GgrE4BPi+L/qUb0+sWiGTVp556H3i9oPkDbs+WAn31JxZPPK0wPKqgU+mHa3DhHFoQXb 38fax+SBlW+pc944kHRgyt/Q4pCght7cWMLZ6kDAP3AB3ROCyTF1GyEIGWMURRWkrcwn ETUSCNjDv/1AoSRXgBoI4kBJWNU1kEgmJVDXYYjUpbTcgXchDQ7JA/rqjIJoEeYlTOdP jcTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707451607; x=1708056407; 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=aPkYKgdAjEdmf3djJmWAFp5MZ0cUPQlg/fEPxDpCOGOj8ZJfTdfvqkbW0Bd4g4wirM zfWzqKQDpCVQaExyB3z97TEfOduudy/ywfFZkEg1v80ow2Q+BtkMgkV6YYaNTne8BvBz 853MUNaAhaG7TDZsiFjPD3hemi8KLXoOFTcq6adhwKgbMsKkL78xBpqnYiUYjnmREFz6 7IUWpbK/JHxxNmvQT6Nqxza0bSPW4I31CwTw1VejE7uKa2idpDxdG+uRHjxOEpzr0uIf ACaZ20wytAZ5JuGtsT63R8x6YiatRtmzDmhZZCd6ef9C8SMQmjLNheI268cnTE/RYvWN 6L6A== X-Gm-Message-State: AOJu0Yza3K2C2Hm2MXPMNp52yrBYBMG1BJaXSTTV9kE7uIEmyLTUDB9j iI4hcbD84DJYD8kjdE9pbR3afZwdq9sOAu7bV2Znwxz+QriIYQQv X-Google-Smtp-Source: AGHT+IHuqaEsk6hY3MyoaXGt78Tnlu+oap9lHuWJS3aqvunCJMIGEmoL01w5jyIJ2MTY+5Q0V0YZ4g== X-Received: by 2002:a17:90a:c291:b0:296:fff3:cf33 with SMTP id f17-20020a17090ac29100b00296fff3cf33mr1016964pjt.8.1707451606688; Thu, 08 Feb 2024 20:06:46 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWYw8ifD9te6TKOQruqSWHsfeyVWflUrl1SO2obE7M4KjqFDUY4tyAd7G+fW4weZ61H3tBeM7iE9QrtVUgUuqPkA3BqbcV/uqVtAYEMOdzBtNJ7moG+4HEo4Yrf8pUTvj0KhqQp2Ld60XtdpyNfIMydcJU0tnH+GgOO/Ir40yeps53JAqQY/ou32WGYozZ2TQUdJn/A04Fs+czxvy4plVwfCv1hwqMwa2KJUO7JBAlDbj2wEEWXbOZcNbE/o8UjskjgDrBXgRZh0EXhh2tx/MKhK2g1C+sdFMWpUb6Rk0/njLS/4TTplFl0/c9Po2lLk7OZzJFytqqZ5A6DCtSe9uqoRXyqYMZDfBGZ7eXuH9eKfEt9KVfxjg== Received: from macbook-pro-49.dhcp.thefacebook.com ([2620:10d:c090:400::4:a894]) by smtp.gmail.com with ESMTPSA id gz21-20020a17090b0ed500b00296b90d93absm631254pjb.29.2024.02.08.20.06.44 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 08 Feb 2024 20:06:46 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, memxor@gmail.com, eddyz87@gmail.com, tj@kernel.org, brho@google.com, hannes@cmpxchg.org, lstoakes@gmail.com, akpm@linux-foundation.org, urezki@gmail.com, hch@infradead.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH v2 bpf-next 08/20] bpf: Add x86-64 JIT support for bpf_cast_user instruction. Date: Thu, 8 Feb 2024 20:05:56 -0800 Message-Id: <20240209040608.98927-9-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20240209040608.98927-1-alexei.starovoitov@gmail.com> References: <20240209040608.98927-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: yq9s13nbnar6fg1g1x18kftbkz5ehs8p X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: F15051A0011 X-HE-Tag: 1707451607-987369 X-HE-Meta: U2FsdGVkX19L7wEj9d6HREYxFWf26/dBYtvOkMXaMRpmBcSul0eIoKZHRpTiLGiwsb5diNrVOOHHXniQfFVF/gg33LsUB5BHm1ggwE+RS79xnMVuzMLsiTPTJjFgHeK1fO6eHVrMKuz1inp4cBrTUfnHFYoFGPUva7yysAhuJIgqytbWxrZvDpnY8VwLf/KT+zp+yoHjzOMZ0yTu9c48DgKkp4WfDCHMMhIb7tdTABSUK/h6hSK2sKj4hn25kr/4f4Y9jUZfYhE+tqohN0isHRivUCtY99L/vqZO+aCDcMM/Vpzaam4fqcevHqrT0Ncp7Yoo8Xy41aGktP+1gEhonfxkMNLwPBO5bSTxhIC0fk2MdX1QyeyC3Sss1/5lTR9vT2hlk2MRNLkNDM2xatq6n268YqolaXmX0qWSkmDI4ixXdGqnOmRv162j2p7tgkj9PrjPZdm3qgr3RCkJN5svjNGeStsIGEP0NAyK45ajAOUTRRltmKO4Fd+gJ9uxUpxz3MqxVeYtiInwMgu0avua9wGRikQaVQAml0batvw5RLJ9DAK0e5NNJsZ9cMPqLHmw1H1fiKa+H6y2xcDq/3m5b2OthYo3KszZKhgNYtGfDfPTNoLB1GaEVQPO3hfMaE+c2YfSjIDdu7nKf5v9kCYp05ck8qdk6oUIrx+igo3kC3j91Ug/g1NRthfGF6g4OJWlntKTNoynqcrP3eRF6FDBkMFJ9RdRCDq7CrrTXq9ezRXb0Fj2GobneYtm8RTgV2n9EU7AKr+yJfhobgpvXokyUIfvaZLse4aabVuGeE/7y3f7j0UpHoE7rl2HqIIHvwDziCRuHTppF53dVkesjqYHjFPG9SyikYNr9Zgx0havaFIobCAmhh+JASsmy0hKhQxp0+6gCSeoHeJ2mASvKo/+1alRQGrIEEWkPF1xUONBzZg2580vz+DFj2zxK6T51b7kea2OjUinHEGPgud3DU6 AX3+HrPg pBzzrlta3/TDTNFfsmcxPOKoZmvX2ggBH3tBNUKTsnHFAEDrR+EYuZZbVKwB+NSgrVA53kmtf2JyekAyS4TtPvrhC6JYP3a8ijKoSR/lAxe4sU4Pt74hK4v7XGcpIm2GLR7QHJFWqN2dvmeVlYgjA8LsylrlarhpyanMuy1lJ6QqZLNxWsTlo5ajcrQrDUGv1d+vVPi3bLzsPVcyzfPG0LLsx169MowE63mKpkC9zkCbKpGHHfmutrprQr0We6ZgzeM/Px2b5cw3BcPGcYsO1kYUz6VM4tmkgM1vkAgkaH+m2sCNcER4MTun4KCI57uv9BOuPR76IK0TR0zZStCS9hs2VKN56WQK5ef1aoQEG6X+08moxghWwbcaOmH83E2+tttdl0iWlVRhslMiSnKWJpGAfEjyW8Am3MgF6ttepgV7nNQ7WSVZxmOksfcGeatOuXHlsWYrKoPBH+zRvn0upTwzKguYWqvo1bs18DNslnvQHNtdhGRC8aQ4IczKTa/aFep5lZ/ix9j7SNIlK1tTkMUFo+pG/kgB6YUb2ct2vV1oxLzrgQzRqgWtHgu0/vMv+/UVnR5QR3QiJBo5FKT/bTSrTZKykMZBAQq/j56DlFFPG7qmahRB4Z2TptxSx9jEPtYL+ 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 Acked-by: Eduard Zingerman --- 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