From patchwork Tue Feb 6 22:04:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13547840 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 41974C48297 for ; Tue, 6 Feb 2024 22:05:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B865B6B0089; Tue, 6 Feb 2024 17:05:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AE8D36B008A; Tue, 6 Feb 2024 17:05:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 962776B0092; Tue, 6 Feb 2024 17:05:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 821736B0089 for ; Tue, 6 Feb 2024 17:05:08 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 5D8FE1408B6 for ; Tue, 6 Feb 2024 22:05:08 +0000 (UTC) X-FDA: 81762760296.05.21E58E0 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) by imf12.hostedemail.com (Postfix) with ESMTP id 5A1804001D for ; Tue, 6 Feb 2024 22:05:06 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ig1Vh2+m; spf=pass (imf12.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.173 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=1707257106; a=rsa-sha256; cv=none; b=jaBeH4TH3z2Vql0PG1d6vDL3m7iItDymVzrvoxhDTMvC6xi3+ne6pjf1NqVzSqpQB7CzLT IluYvB7fGxdbGHLyyRlDBh8E43R8fK41hQO+V7SyDSrBpdLxibpE5HjP8xNZYiA5qGOglv SHCEqX1de4Zygt6hE6+JVZBpZjeilRA= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ig1Vh2+m; spf=pass (imf12.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.173 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=1707257106; 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=8MScsbwT1vbeNZ1bMHv4ZpqU6jS347PsNSKHFWi/xLuMhONNKBePQMRGIwL6KCSH0n0CDA 6oraOFFzJXwGuZKxLsC9HNAT0euVlOhnjJCm1zL8r1RulTbyFCyOnFJySU4+kawOkC3EDL QSwp9c6ASBx0ZaVqzno7wfaS2X8/RAE= Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-6e04f29c7bcso20654b3a.0 for ; Tue, 06 Feb 2024 14:05:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707257105; x=1707861905; 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=ig1Vh2+mbeCOarpR9SDVfWtsX6NxvDCGlgaFaGjpmMxxtiyugJ2/z/6nqWeokBAvrW Acr2WyMn4X5Zk45TrFsrvzyoIqdiD1zwHKLImXiiO4+17IxJAiru/mIEkxUmz95xmTnv qmYSxUbmFFIDfF6NVaWVAzXZzKvg/B8hmsj/6xFyDBEBBWIOMj446smqDVRW4ITiXhqG Ph9wp4Hsc46x04bupNktnhhnVJhbhb61lzDw9XYJGuRny6bxAIVXk2xKQr3Qzpq+z/tp K+ZPOx4kvS7Hbwb9GItsy5ZPwbdze9tjNsSE04CPfmaJuyGP0qEizu+L7/pthX3JzPXQ kgCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707257105; x=1707861905; 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=CAAjREAdZ7PahcBOexlBYxkQxk7FQGf6fdoTPSf5hm4Z0ZrH0qadCrLfgRQTr5nMBg nZRRFzUThEtK2KkRz8R6Hb7aANYo9TuW2WvkgHqAq+z5zeY+mb2pLwqfjoG5PC3LRLRi HNl1vYFcZ6VgdV0tE0YBrbGj+2ausyI6ZtUSC9FQD+xqKdqIy8B1o+mCBy4jFqhbcwZK fHTV054Mbfl6JySA3NgcOYGfHbIKbpGFRy9TT5CidjScse8d4R2RRO37WBdC7Jf5L9WA neTORFC+fWBfeXDWnQqzMTSR0OakThU+vifT+jf0ltd17EHgnfUolGhmKYY9lq7h9OpN JhKw== X-Forwarded-Encrypted: i=1; AJvYcCUGZy5iol3mad8OkNxJXHEMAPi/15Ynw3VL6SBVr5hHlz64vENEJv1cI6fMY1532GFHNZ3tkLKZNT/52RE+MFHjD60= X-Gm-Message-State: AOJu0Yxqyl1OgJ1NR6gkb935Bw8educBbey8gtRDM8jZ6cYBEFt1+pTD mp3JJzeKKvFyLikTlgr3OeKrCcTVtbyTwWA0GxSMAKlBdo4r4YvT X-Google-Smtp-Source: AGHT+IF7ufxUlX2aXoymen9Yws0uT15WBLonSQYDvE1rmVUQQKdyNHEjxdlBQPSGpbNEV8D1c9ZwsQ== X-Received: by 2002:a05:6a21:3a97:b0:19e:a19f:f4de with SMTP id zv23-20020a056a213a9700b0019ea19ff4demr1218529pzb.41.1707257105150; Tue, 06 Feb 2024 14:05:05 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXj2AKcZu7kXri6G+OwPqAfvSb7LujQdy2VsFPv9eQ5TTr8zwqyW+OhhQE42K7slkCGEV7sQJxPbSPGLxYy8wHDClhLz9RnrYV7FZ+YKL6xYzjNjxvM+EanhnVJ7dKX24XsaCGCfAiX9N3zPMVgAVfAp5dRizJWf8lYj3JR7V1VJkb1D7jhXy5H1h4GflKBzqGRglGhP2MSRAtPNqQSMMAPaPzrVj0mH2kZaapd1a8eYTmbX1n+M3iNC1d1P9HzYC0d5elXhE2qkBHHLnqX9578jlxiWafpgkAE Received: from localhost.localdomain ([2620:10d:c090:400::4:27bf]) by smtp.gmail.com with ESMTPSA id e26-20020aa798da000000b006dd810cdd91sm2519731pfm.88.2024.02.06.14.05.03 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 06 Feb 2024 14:05:04 -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 05/16] bpf: Disasm support for cast_kern/user instructions. Date: Tue, 6 Feb 2024 14:04:30 -0800 Message-Id: <20240206220441.38311-6-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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NIHLIto76k6 iNPploOO 6t90KmYKCx8RZ6Mj/w9nmqiy7Y2AxXEIv3gxM/dSWDwEP8lifNOXOk79qJFBi9HLhnaGKnI3s9TzNslLYAdnZB6tbXUuWaMKfNJfa2zYkHrTIHAps4oRMdeNBgGpx2VCpicUsFe+C0YmXf2zIvg+7jkX+dOrLNtaqkFyz2kG0RR2Lyw9nyL+AC40DAMtedt/Rb43gBQI65eAHmqtKG06uyR3BqpNIfS9txELy4A4BR74vmuIJYbCdAzkUvbdA3Ia0FLfw2qDapTYSqUGJMQXA/lKE4Eq6UCXJxW4+aW+znAvdsks6KRMq8H4MBHU0AFydElsXdarD7jCvm441jKiQhgABJO0nNZd38w/U8I1kcilov3yMJQAuCwhl/+E0A/++08W5UUK+Fur5X5MevrVd9x9yhU/S733qLY+niApSoBDIjFqeIzM6QPXiMNVf1TIMsZOYpi2kGXfJr5rn5ksXGOdt6G2uYCl+1sJHaUfMjF8ndUVEtEVTpP3Q2ZTyXS2GkJk8vLzmmFbEnzQp2kvS03BJAjfpyz2YqugzWoR2DZwTljmzY50uKWoiyHodbCol6FJnP5LVEzKXVVC0wWYJjsGejG9fnmdhO21i 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 */