From patchwork Fri Feb 9 04:05:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13550847 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 61E09C4829B for ; Fri, 9 Feb 2024 04:06:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E21818D0002; Thu, 8 Feb 2024 23:06:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DAA1F8D0001; Thu, 8 Feb 2024 23:06:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C4A026B0092; Thu, 8 Feb 2024 23:06:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id ADC4A6B008A for ; Thu, 8 Feb 2024 23:06:41 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7A69B12017E for ; Fri, 9 Feb 2024 04:06:41 +0000 (UTC) X-FDA: 81770929002.29.CAB78AF Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf25.hostedemail.com (Postfix) with ESMTP id A25ACA0013 for ; Fri, 9 Feb 2024 04:06:39 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=fuRKxMxu; spf=pass (imf25.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.182 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=1707451599; 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=Dj+O15ERlc2oNccgk7u96NynUdtw7IljkhhXG+cbct8=; b=XvDiOaEDafIF0Tu7ossazH3FFCHm6DtbU77oc0tWsEDF5r42UlZcfAIMk62L17tii/iOOn ZpXHmTalrFq1SM+mIyxoUWt7sC1eJT8EqYOILtEhadGkAXIYLuYcxJ73ussTJbCWu+1Gee alCu8dTLk9gC/pH/l9HxR7PwTeQkG1E= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707451599; a=rsa-sha256; cv=none; b=yXDAA7EI2v5hsYMaAonYayR1RKjiZVI2MekmA16/4ga9AJBGW2jLPVgGO6zy/s7kykKd39 zOxYVtnePjEnCKb8XI+PfjhKxiWjyGQgPUf3cpMaxjISci3Q+jhYmWphM8hcmy+dW0YJn+ aFCh6V9Cona72Q3CoSnTfKtEUCg4TIU= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=fuRKxMxu; spf=pass (imf25.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1d98fc5ebceso3216325ad.1 for ; Thu, 08 Feb 2024 20:06:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707451598; x=1708056398; 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=Dj+O15ERlc2oNccgk7u96NynUdtw7IljkhhXG+cbct8=; b=fuRKxMxupdvujWNcYPBuh+orlZGnmBqVZHRe0WVTX/FMQclTZQ7o+1Hu/aZgOn25O3 uID/thtdOSPn3S0OBPMNCZFE8HypzqSGhMAUQqDZf07jEKiV9Pc7tsfSVklfTLG7mbjT UO25UZNiiHtofcXiYe4JxRRVRx3zz4ck2FbT56I6VRJiJ4RZh0rbhSeTpzdadJ/20fER HGi3dwwOYnHEsgblrDB5qieif7L2IGZP924yoCLzkQNXKgea/n3C7POmvYlycvxKU6OS VS5eQoxXVFYIoJ03lwBmAg3dqS5KnCxGFSTPS2PVHdziSgMIzycgI1Y+iqHsv26RG5Qj 3CMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707451598; x=1708056398; 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=Dj+O15ERlc2oNccgk7u96NynUdtw7IljkhhXG+cbct8=; b=t4CsPILCfGYJySPKcKmhzsJQX0CilRjTjwHhpli4I8hbiy3mLi4GGyyI+KoZcxQcCR 8LaRGheM3HsXpzJWYV/2GRZhrTsgYeX2xcE8kYHCUtsbWr5q9bqq8/xXZ7BHjSbex1Ke A7JR6J3kMojIILL6ciG9jEw9EGjhf1AT+3S6WziBz5N2QkfXeXUc7gx1IFQde/45RAWQ 3njx3vEh4RazrOH93oVfavyimuZmq4FQxwKHianqavFRvQBNa43vDvhd5HZpVilxdMbh +cNizZDBk1dc75MNe+c8bN60uaPB+jfr+xciSgCRksbfXgvptMgI8k0OJgjh1cGo5J+U cZeA== X-Gm-Message-State: AOJu0YzJGuryfmYeqgXUqO0659+okUB1EQLjuEuLZYQBSCrHx9JqB2D0 2OEbLikzGTa7gOW+KSr/yiOOVjNQw5jTG2bMgQwSQNXUEUtTqHxd X-Google-Smtp-Source: AGHT+IH2IsgScbF5ztEZpq6NjR9/2tA7q8l7Ix0S1q4VHpCiUufQi2AhA2u+G2VsbGb/+uLf6rgQHA== X-Received: by 2002:a17:902:b68c:b0:1d8:fae3:2216 with SMTP id c12-20020a170902b68c00b001d8fae32216mr419187pls.35.1707451598204; Thu, 08 Feb 2024 20:06:38 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCVCvhcfuaPez5OC/0K6lsZftyKKLtOFjodjSkRdPq0l1nidkiVoJHU0Trfc2t9CHoxVoGuR75BnUBtDC7gLBQ5y1kSWyuyg0x40itdsIGSi0vL6t/hDpiz3VGWvzx1E7jyVPl9K9LQX+7fbCFhxUC3CdIANDI84yBGyKa04JykPcZxl05c6A0hQNL7dkYjfPxslz4jAHOWN1SA3/EFocm4/Y4mzyEFTltE88QxdJlxI/+lGIt1LRaqh1R6CISaCy56IDDTUokGPy6rscJPCxBOK6mSWq+iGAF9teb5RqL2aAEQNZwCC6Nscq4hnc7lq7PODCpjmijWqNsxvLhCBkxJ8QMIxLowrFG/g1w/GsVw1Akxvfy/DcA== Received: from macbook-pro-49.dhcp.thefacebook.com ([2620:10d:c090:400::4:a894]) by smtp.gmail.com with ESMTPSA id lh16-20020a170903291000b001d9537cf238sm538602plb.295.2024.02.08.20.06.36 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 08 Feb 2024 20:06:37 -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 06/20] bpf: Disasm support for cast_kern/user instructions. Date: Thu, 8 Feb 2024 20:05:54 -0800 Message-Id: <20240209040608.98927-7-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-Stat-Signature: k15sb1z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fR2 /kDtaURqWtUqnNtENby7EPyQEOrN4uNTr8MK+wcyU7l6mBbq88mgfhAuIzVqkw2lcsGA8Fc/zWtKONI3vq3xGd8o7Bf/AGOSF1g8JNJqcpV2LyTrKX33Ma5lgiv6p0CiePzEI+zBUwRKQA4aRx4Qk2fRC1uIH6qsQy5I34BGI4gigR9eVOW2ISYM7tx185T9IIsfjXV170zRKWmQabH+YWbY1TTzw6KEhHErvDKQBvwZbJYxPCbuM6ADe/Nd92RwL78rjGLnq2X9I+jKC5BcTpMROXjeFJAbHLaiHXW9brwkaE0R89EXCbYUwJpaHjToz2BYztRbn64VfA61bQN16nJ5UFMcjCoXHS78s5KuWZTtvUWxpzMCDtzdLrrTVovMVdOfM0ohe9NmpPnUUNigYT9op6mqIsEv/04X1UmqxRqnd0O/vtfct1VsILzgT7E67eD0W0B8q/AgMgz9P0wWSKALwqWoa8KY9NLZe9QYvwulBJgs/QOwRglufsEAMHTPljdDtKHKa4B39SXxxyIvZ5b6lvnLKVYsczugP77qJJU3QMQSv5WinBDNzfiB+eWsmiyNhXNNObXwUVV6a1k6jt2h/uVB5uA9cPQe5 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 rX = bpf_cast_kern/_user(rY, address_space) instructions when pointers in non-zero address space are used by the bpf program. Signed-off-by: Alexei Starovoitov --- include/uapi/linux/bpf.h | 5 +++++ kernel/bpf/disasm.c | 11 +++++++++++ tools/include/uapi/linux/bpf.h | 5 +++++ 3 files changed, 21 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index f6648851eae6..3de1581379d4 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1313,6 +1313,11 @@ enum { */ #define BPF_PSEUDO_KFUNC_CALL 2 +enum bpf_arena_cast_kinds { + BPF_ARENA_CAST_KERN = 1, + BPF_ARENA_CAST_USER = 2, +}; + /* flags for BPF_MAP_UPDATE_ELEM command */ enum { BPF_ANY = 0, /* create new element or update existing */ diff --git a/kernel/bpf/disasm.c b/kernel/bpf/disasm.c index 49940c26a227..37d9b37b34f7 100644 --- a/kernel/bpf/disasm.c +++ b/kernel/bpf/disasm.c @@ -166,6 +166,12 @@ static bool is_movsx(const struct bpf_insn *insn) (insn->off == 8 || insn->off == 16 || insn->off == 32); } +static bool is_arena_cast(const struct bpf_insn *insn) +{ + return insn->code == (BPF_ALU64 | BPF_MOV | BPF_X) && + (insn->off == BPF_ARENA_CAST_KERN || insn->off == BPF_ARENA_CAST_USER); +} + void print_bpf_insn(const struct bpf_insn_cbs *cbs, const struct bpf_insn *insn, bool allow_ptr_leaks) @@ -184,6 +190,11 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs, insn->code, class == BPF_ALU ? 'w' : 'r', insn->dst_reg, class == BPF_ALU ? 'w' : 'r', insn->dst_reg); + } else if (is_arena_cast(insn)) { + verbose(cbs->private_data, "(%02x) r%d = cast_%s(r%d, %d)\n", + insn->code, insn->dst_reg, + insn->off == BPF_ARENA_CAST_KERN ? "kern" : "user", + insn->src_reg, insn->imm); } else if (BPF_SRC(insn->code) == BPF_X) { verbose(cbs->private_data, "(%02x) %c%d %s %s%c%d\n", insn->code, class == BPF_ALU ? 'w' : 'r', diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index f6648851eae6..3de1581379d4 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1313,6 +1313,11 @@ enum { */ #define BPF_PSEUDO_KFUNC_CALL 2 +enum bpf_arena_cast_kinds { + BPF_ARENA_CAST_KERN = 1, + BPF_ARENA_CAST_USER = 2, +}; + /* flags for BPF_MAP_UPDATE_ELEM command */ enum { BPF_ANY = 0, /* create new element or update existing */