From patchwork Thu Feb 22 11:16:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Borisov X-Patchwork-Id: 204682 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:aa16:b0:108:e6aa:91d0 with SMTP id by22csp181820dyb; Thu, 22 Feb 2024 03:17:55 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWkDD6Y+72ybMRbBEepluZx5ENRG996yv5bm7S3J/9aooTlLU2dAkAJzduKVBixEg8CrV1IWe2JFboKoZznT9D2sn4Frw== X-Google-Smtp-Source: AGHT+IHYzFKwsXPgs1ia+OKBnrgT0Ix6p27jp9c4fhoX7rGu6P3HFe3drLKR+glsVUEdk9Ntxwda X-Received: by 2002:a05:6402:12c7:b0:564:65a5:b942 with SMTP id k7-20020a05640212c700b0056465a5b942mr8171132edx.6.1708600675032; Thu, 22 Feb 2024 03:17:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708600675; cv=pass; d=google.com; s=arc-20160816; b=zYbPnKcqYzMSSV5gWfF9tQY9FTdImdk5tn4dJ71rJ9raInHeEKCLMpRXeM2h5yhB0c +O4cFkLd5We+w4yqUUoH0R+73Fr96CTMP/tmqW0bEXMXUNl9dSLh9euLuWjNdLDnOyLZ j7OoBh3oye9GzjBkib6rwssDvySyx+sKmV72zLI7Pwp027dAoYsicAicVnIF+eeU77Te kVTKrHBT5KRkoMszO1tHxe5/ld53oqmAGmgXFL+F/MO8tJWBY4YjGbfbF8gsJGC+PnyR oAVZ9sL3Fjg27Q7qPW+xbRky2Fzj699XubczoPZst4plXE59ko0T5BD7qTig60D/Nwls L7aA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature:dkim-signature; bh=4KjgXYrHVvwADgI5kTKZFgaMxkaodrMCR2ZZQJa+QmU=; fh=Hu45STTqb7AiaRzx9E09MDox39bxU5VqqiroUJtANag=; b=BmRsDmNKCyuRIjKNW3YA3vly3iR9+85vAm8qs9P7cBlHfIMtCkX53LmT9ZOt7S1/uo SQcYuyFyXm9YBia9hvxxJUkOTrpTGbdcEYkrrGZjgpHUhADSuXWheWkhJU9EHLzBylcP zn+G35QyD/PQzzU2AtU7IQqcqlgHRbxkAHCCpmmNt+LQYU5vYf7+uCSv1RrpUOarpEYx JCQ21q+OvNAGRaPiYcDRaMfDvvuYfI9EIvRIkaQ/YXLPh8lkRZgtceN6I9OcgqBfp4eg Pybbymig6TGLwKWggedJCxwKb3JNeozsfTRcOGjRjbZJuo3BDQKWyXa6pddOmhyVvkDA h2/A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=XLibcITl; dkim=pass header.i=@suse.com header.s=susede1 header.b=XLibcITl; arc=pass (i=1 spf=pass spfdomain=suse.com dkim=pass dkdomain=suse.com dkim=pass dkdomain=suse.com dmarc=pass fromdomain=suse.com); spf=pass (google.com: domain of linux-kernel+bounces-76392-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-76392-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id df9-20020a05640230a900b005641883f18dsi5226151edb.315.2024.02.22.03.17.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 03:17:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-76392-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=XLibcITl; dkim=pass header.i=@suse.com header.s=susede1 header.b=XLibcITl; arc=pass (i=1 spf=pass spfdomain=suse.com dkim=pass dkdomain=suse.com dkim=pass dkdomain=suse.com dmarc=pass fromdomain=suse.com); spf=pass (google.com: domain of linux-kernel+bounces-76392-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-76392-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 503EF1F255D6 for ; Thu, 22 Feb 2024 11:17:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5E57D46453; Thu, 22 Feb 2024 11:16:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="XLibcITl"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="XLibcITl" Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0F3FC405D4 for ; Thu, 22 Feb 2024 11:16:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708600613; cv=none; b=DXdkYPi0koPT+Rx2WznXqPCS8Ubqf8fVME777TsM4oHEtDC6ZvCS67ZkF1vih53jx55+FxldLQtHrz0kwoUSkGRCdUBaD2aEWwNCPy/LqZ24O/fMd2b/ygA+gc5c6gEJdPDX150hxXnbPSfVf0xhJJI8Ha5/Wy2/+Pw1L94fJlU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708600613; c=relaxed/simple; bh=rxtY7H0wM9qx+Z0fjosj7tqZaxRDqcbDj1LTjD9zRNg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RF85Xdm/pETk8fzEqMwi88wDIoMrPttz4kSN3BnZWRADjSQfkc+HJk5Hu2Yr1WNEYSWiuZM9Rp8l7dCIzRQF8xuoNrlAxc6XDpjuEzk4w5bX6g3cCRroEMgaHeuC901jMwR8+tZO8B+LaORzbu2/RvzGblXucmz/C8oMsigrDqQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=XLibcITl; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=XLibcITl; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 4311D222B0; Thu, 22 Feb 2024 11:16:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1708600610; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4KjgXYrHVvwADgI5kTKZFgaMxkaodrMCR2ZZQJa+QmU=; b=XLibcITlVhR+Y+l1qkAS5iRuNT5Orwn94ePgaAe51uiCgaYEOb2m6Dc8FuvB9XbOUxGI0z k6Y6N2x1SGBC5P7YyNYCOSZ736GXFkVFyEgZiEmGbhgXiNQZN2obAKey1ifobTl13NN+W0 gIEeMBxQxxAz1S87iZtL4u0ZGS/+jZo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1708600610; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4KjgXYrHVvwADgI5kTKZFgaMxkaodrMCR2ZZQJa+QmU=; b=XLibcITlVhR+Y+l1qkAS5iRuNT5Orwn94ePgaAe51uiCgaYEOb2m6Dc8FuvB9XbOUxGI0z k6Y6N2x1SGBC5P7YyNYCOSZ736GXFkVFyEgZiEmGbhgXiNQZN2obAKey1ifobTl13NN+W0 gIEeMBxQxxAz1S87iZtL4u0ZGS/+jZo= Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 0452A13ADA; Thu, 22 Feb 2024 11:16:49 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap1.dmz-prg2.suse.org with ESMTPSA id GFUhOiEt12VrGgAAD6G6ig (envelope-from ); Thu, 22 Feb 2024 11:16:49 +0000 From: Nikolay Borisov To: bp@alien8.de Cc: linux-kernel@vger.kernel.org, x86@kernel.org, Nikolay Borisov Subject: [PATCH 1/2] x86/insn: Remove superfluous checks from instruction decoding routines Date: Thu, 22 Feb 2024 13:16:35 +0200 Message-Id: <20240222111636.2214523-2-nik.borisov@suse.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240222111636.2214523-1-nik.borisov@suse.com> References: <20240222111636.2214523-1-nik.borisov@suse.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Authentication-Results: smtp-out1.suse.de; none X-Spamd-Result: default: False [6.00 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; BAYES_SPAM(5.10)[100.00%]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[4]; R_MISSING_CHARSET(2.50)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; REPLY(-4.00)[]; BROKEN_CONTENT_TYPE(1.50)[]; RCVD_COUNT_THREE(0.00)[3]; TO_DN_SOME(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; MID_CONTAINS_FROM(1.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_ALL(0.00)[] X-Spam-Level: ****** X-Spam-Score: 6.00 X-Spam-Flag: NO X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791597661298912160 X-GMAIL-MSGID: 1791597661298912160 It's pointless checking if a particular part of an instruction is decoded before calling the routine responsible for decoding it as this check is duplicated in the routines itself. Streamline the code by removing the superfluous checks. No functional difference. Signed-off-by: Nikolay Borisov --- arch/x86/lib/insn.c | 56 +++++++++++++++------------------------ tools/arch/x86/lib/insn.c | 56 +++++++++++++++------------------------ 2 files changed, 42 insertions(+), 70 deletions(-) diff --git a/arch/x86/lib/insn.c b/arch/x86/lib/insn.c index 55e371cc69fd..3946bdc75087 100644 --- a/arch/x86/lib/insn.c +++ b/arch/x86/lib/insn.c @@ -268,11 +268,9 @@ int insn_get_opcode(struct insn *insn) if (opcode->got) return 0; - if (!insn->prefixes.got) { - ret = insn_get_prefixes(insn); - if (ret) - return ret; - } + ret = insn_get_prefixes(insn); + if (ret) + return ret; /* Get first opcode */ op = get_next(insn_byte_t, insn); @@ -339,11 +337,9 @@ int insn_get_modrm(struct insn *insn) if (modrm->got) return 0; - if (!insn->opcode.got) { - ret = insn_get_opcode(insn); - if (ret) - return ret; - } + ret = insn_get_opcode(insn); + if (ret) + return ret; if (inat_has_modrm(insn->attr)) { mod = get_next(insn_byte_t, insn); @@ -386,11 +382,9 @@ int insn_rip_relative(struct insn *insn) if (!insn->x86_64) return 0; - if (!modrm->got) { - ret = insn_get_modrm(insn); - if (ret) - return 0; - } + ret = insn_get_modrm(insn); + if (ret) + return 0; /* * For rip-relative instructions, the mod field (top 2 bits) * is zero and the r/m field (bottom 3 bits) is 0x5. @@ -417,11 +411,9 @@ int insn_get_sib(struct insn *insn) if (insn->sib.got) return 0; - if (!insn->modrm.got) { - ret = insn_get_modrm(insn); - if (ret) - return ret; - } + ret = insn_get_modrm(insn); + if (ret) + return ret; if (insn->modrm.nbytes) { modrm = insn->modrm.bytes[0]; @@ -460,11 +452,9 @@ int insn_get_displacement(struct insn *insn) if (insn->displacement.got) return 0; - if (!insn->sib.got) { - ret = insn_get_sib(insn); - if (ret) - return ret; - } + ret = insn_get_sib(insn); + if (ret) + return ret; if (insn->modrm.nbytes) { /* @@ -628,11 +618,9 @@ int insn_get_immediate(struct insn *insn) if (insn->immediate.got) return 0; - if (!insn->displacement.got) { - ret = insn_get_displacement(insn); - if (ret) - return ret; - } + ret = insn_get_displacement(insn); + if (ret) + return ret; if (inat_has_moffset(insn->attr)) { if (!__get_moffset(insn)) @@ -703,11 +691,9 @@ int insn_get_length(struct insn *insn) if (insn->length) return 0; - if (!insn->immediate.got) { - ret = insn_get_immediate(insn); - if (ret) - return ret; - } + ret = insn_get_immediate(insn); + if (ret) + return ret; insn->length = (unsigned char)((unsigned long)insn->next_byte - (unsigned long)insn->kaddr); diff --git a/tools/arch/x86/lib/insn.c b/tools/arch/x86/lib/insn.c index 8fd63a067308..5d81924478d9 100644 --- a/tools/arch/x86/lib/insn.c +++ b/tools/arch/x86/lib/insn.c @@ -268,11 +268,9 @@ int insn_get_opcode(struct insn *insn) if (opcode->got) return 0; - if (!insn->prefixes.got) { - ret = insn_get_prefixes(insn); - if (ret) - return ret; - } + ret = insn_get_prefixes(insn); + if (ret) + return ret; /* Get first opcode */ op = get_next(insn_byte_t, insn); @@ -339,11 +337,9 @@ int insn_get_modrm(struct insn *insn) if (modrm->got) return 0; - if (!insn->opcode.got) { - ret = insn_get_opcode(insn); - if (ret) - return ret; - } + ret = insn_get_opcode(insn); + if (ret) + return ret; if (inat_has_modrm(insn->attr)) { mod = get_next(insn_byte_t, insn); @@ -386,11 +382,9 @@ int insn_rip_relative(struct insn *insn) if (!insn->x86_64) return 0; - if (!modrm->got) { - ret = insn_get_modrm(insn); - if (ret) - return 0; - } + ret = insn_get_modrm(insn); + if (ret) + return 0; /* * For rip-relative instructions, the mod field (top 2 bits) * is zero and the r/m field (bottom 3 bits) is 0x5. @@ -417,11 +411,9 @@ int insn_get_sib(struct insn *insn) if (insn->sib.got) return 0; - if (!insn->modrm.got) { - ret = insn_get_modrm(insn); - if (ret) - return ret; - } + ret = insn_get_modrm(insn); + if (ret) + return ret; if (insn->modrm.nbytes) { modrm = insn->modrm.bytes[0]; @@ -460,11 +452,9 @@ int insn_get_displacement(struct insn *insn) if (insn->displacement.got) return 0; - if (!insn->sib.got) { - ret = insn_get_sib(insn); - if (ret) - return ret; - } + ret = insn_get_sib(insn); + if (ret) + return ret; if (insn->modrm.nbytes) { /* @@ -628,11 +618,9 @@ int insn_get_immediate(struct insn *insn) if (insn->immediate.got) return 0; - if (!insn->displacement.got) { - ret = insn_get_displacement(insn); - if (ret) - return ret; - } + ret = insn_get_displacement(insn); + if (ret) + return ret; if (inat_has_moffset(insn->attr)) { if (!__get_moffset(insn)) @@ -703,11 +691,9 @@ int insn_get_length(struct insn *insn) if (insn->length) return 0; - if (!insn->immediate.got) { - ret = insn_get_immediate(insn); - if (ret) - return ret; - } + ret = insn_get_immediate(insn); + if (ret) + return ret; insn->length = (unsigned char)((unsigned long)insn->next_byte - (unsigned long)insn->kaddr);