From patchwork Wed Jul 28 15:32:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Brazdil X-Patchwork-Id: 12406195 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3A18AC4338F for ; Wed, 28 Jul 2021 15:35:02 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 09C2660E09 for ; Wed, 28 Jul 2021 15:35:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 09C2660E09 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=QUo3P5ETYznaqgwMT4SAB5vD1sNIcA5gzGPyUAAuSbo=; b=SwxFvMDCBqsv5I7iATthUysphY Cm7EVwyDInAiqcJk66Xg1g6Xta3cWYlYlNshM4LB96fac67kgwsGGWukynMIAy2wNaRWNNbYkSyJ7 rI50MbvOO0Y8vz+MHXXbC2k8hix3mAHqW1iD7nu45UFTK7YMBPDSQuej04xMfm9cd/rFyYW5ZyoZJ XP7j6lnnIxUFnZJEXXPQj9G0r/NyqccdR8I3eyX3pDnZVkcEFlV6k3K7DvDnJUxUkC70LrQJCqyBP biByNQXZ69hcrGG0tpyFSkhby6IvEZ404yr9RGWv2r5z3/b7jUpbTxErkTxXVG7SfLYDU4I2igq/i 1qLbHqYg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m8lYK-001K77-48; Wed, 28 Jul 2021 15:33:00 +0000 Received: from mail-wm1-x349.google.com ([2a00:1450:4864:20::349]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m8lY4-001K1j-Nd for linux-arm-kernel@lists.infradead.org; Wed, 28 Jul 2021 15:32:45 +0000 Received: by mail-wm1-x349.google.com with SMTP id o26-20020a05600c511ab0290252d0248251so1074145wms.1 for ; Wed, 28 Jul 2021 08:32:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=wsuCFl3zJw45VJsq2CCpitdGwDgZYGVgJGVdlnAA78Y=; b=H8AiMx+L6JXn5Q+T+lMqgWRxobIRqr1+MD1xsZdr4m6FSo/4YE+a379Kuo8gyFA44C VJH3lsBWEIBwuqw4RWtBjV/tN0ISN2k68TGgN8BSEKrL0hD+GIIpEmg7bAQ3PxjKkBUS lmm/xrYOZ1ljLtSJwWL/yt3uv5QLcvQPySXsZGT/ev7ua1Z27AkwLUbkQNm6RG4rnl5+ TkDkcFzqPQz/VFhensW0nxv8xcGF5dBu/mwQdIA+v/wiqyydrUvaAxKqNGg0FX6QRFOZ FEghjQ51cpIQVaISj8htiqpUP7Fpkh/kpbqUVbJJgN+xGUBdgVaH7cUvOkKQoqG4Xps1 +P5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=wsuCFl3zJw45VJsq2CCpitdGwDgZYGVgJGVdlnAA78Y=; b=Bdqut5rvIu6S0JRKjESEhWijlzHsn8YZgIPlkjLgh1eRao/OnDLFgRw/0vLWH2vLzO BQb7Y+tv/wn1zf14amjcUAj6OwqNyou2X8sJdDJKPRzm0KW7iyl9yf+8sbI3PiykOOvM vBXabTO7AFt/nqWVGpnUZx5x/9xiL6ZkPTOXYV+ZRehnfPc4Rp14+z0MR9fZXHdbQOLT 13WSVo2+0+X1wmc4T+YCebLRolXevrfgbXhvhGl6UvCXxSZplZmAucvoxVlZFd+ryeO9 JVgQUoKyr/RJoIBwK6XbZ0JeWQS3SJ6BxHFFjwYne7Yx8JVU0zE9Wp2iSM/HfSctqKPW 6Cxg== X-Gm-Message-State: AOAM532CN0pV6sbF/OaVGxCdKwY5DW8sTeykNGjowi7fdZYaQu+bCVl2 0U00dfhvUHkor5kjpTY0dK+NBW9e6FvP+g== X-Google-Smtp-Source: ABdhPJxuEfipJsCbHRn1LIm1ICWET52ttWIgJnQxaUq0o234lBR+T/9ot6YarV6e8XqcOQE6CGdknEA+TvYf8A== X-Received: from dbrazdil.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:7f9b]) (user=dbrazdil job=sendgmr) by 2002:a7b:cb4d:: with SMTP id v13mr10176875wmj.68.1627486362229; Wed, 28 Jul 2021 08:32:42 -0700 (PDT) Date: Wed, 28 Jul 2021 15:32:31 +0000 In-Reply-To: <20210728153232.1018911-1-dbrazdil@google.com> Message-Id: <20210728153232.1018911-2-dbrazdil@google.com> Mime-Version: 1.0 References: <20210728153232.1018911-1-dbrazdil@google.com> X-Mailer: git-send-email 2.32.0.432.gabb21c7263-goog Subject: [PATCH 1/2] KVM: arm64: Fix off-by-one in range_is_memory From: David Brazdil To: kvmarm@lists.cs.columbia.edu Cc: Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Will Deacon , Quentin Perret , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, David Brazdil X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210728_083244_812108_5C2D6B8C X-CRM114-Status: GOOD ( 12.70 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hyp checks whether an address range only covers RAM by checking the start/endpoints against a list of memblock_region structs. However, the endpoint here is exclusive but internally is treated as inclusive. Fix the off-by-one error that caused valid address ranges to be rejected. Cc: Quentin Perret Fixes: 90134ac9cabb6 ("KVM: arm64: Protect the .hyp sections from the host") Signed-off-by: David Brazdil Reviewed-by: Quentin Perret --- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c index d938ce95d3bd..a6ce991b1467 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -193,7 +193,7 @@ static bool range_is_memory(u64 start, u64 end) { struct kvm_mem_range r1, r2; - if (!find_mem_range(start, &r1) || !find_mem_range(end, &r2)) + if (!find_mem_range(start, &r1) || !find_mem_range(end - 1, &r2)) return false; if (r1.start != r2.start) return false; From patchwork Wed Jul 28 15:32:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Brazdil X-Patchwork-Id: 12406197 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D91BC4338F for ; Wed, 28 Jul 2021 15:35:08 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3138460F91 for ; Wed, 28 Jul 2021 15:35:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 3138460F91 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=DCW/LfphghUV3zP8AgcQR0BuTV9zuqp55k9EFZTkGoQ=; b=y2DJi//o8QYwcdxRo2uMr6+mOM pN62/n8TaJlq0DTJFohsadsy2vhNm+cQZUnzLLHEGAn+HmW0ewde2cwI6QODSH2WED8JFO1RR5/rc Tvt04A+kh8Oj/Adt5E04COxk5Fd9Y411tNP7mVBzJlLRSo0Rz1OgmGSd7o2f7yslviHj0LDzNCmsx cZceLNSe56tJAnN43739CsfR4NwwS9xClPgIcqf3CPw9N0V+CKb+TcxWcukV9fzIlxvWXbELTyLja WPjkb4pk0NFEfodsdoapt5lzWwbQOqOQcHTpdBD38SeKs3LuaqENnFlfzkT7VAXrlR1FJTqlAEJx0 Z3MCYNQA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m8lYX-001KAh-0j; Wed, 28 Jul 2021 15:33:13 +0000 Received: from mail-wr1-x449.google.com ([2a00:1450:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m8lY6-001K2O-Ft for linux-arm-kernel@lists.infradead.org; Wed, 28 Jul 2021 15:32:47 +0000 Received: by mail-wr1-x449.google.com with SMTP id n1-20020a5d59810000b029013cd60e9baaso1077635wri.7 for ; Wed, 28 Jul 2021 08:32:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=qzw4XcX4w+ny84Qln9d15Ry0/+5tfssrXUUN0rRDVVc=; b=G3I/3fhsNE6Og2SNbp7xijYfhxbbi8EEXzZO7tjVLLwHEwaGAf2WH4WDSAI9RuBK2v y7MINQTjeOU9oU90HjjguDGAwa4tof8t/8vq/m2JE5pv2a/YNi09Y4IPEqg/Mq+fBEPD Z0d/8fw19uWXRPZVYkNEbf40fgTfDWXSKtSXKuEpcdnImMph1V/8lTDcxryYsvCOGEJx bdhwsqESBm+n86hEdsfP11ImWIAtFpMKBxxxA86Z4l4ZbGbeKxemOU97Z+VC4VrnXV3V xL3tHvSnAfmB7Ax5kGhpV8zZqSzrsqsT6CwvAXBclwHpCvv307H5rs9J7qPyzU9u9jLx Izwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=qzw4XcX4w+ny84Qln9d15Ry0/+5tfssrXUUN0rRDVVc=; b=XbnEmPU5C9baYxDlRVN54RrElHci27mXuAz7csQRchXF22S97e0hYxTjXbSqhiB6rF fo76fR7HlJDZSiIb5woOAcFiDPm2JDqign4G76r8VVokYS4g0RuEJHO94Ch8YCj6Tj2o hVEaSxu7+KmJ179m+8oAomtpJwv1CYQRvtuIV8zMDFLfgjz09eCJw3xK5oYvjMclnxIt pz4DkgzLoPWpo2aGxJmNpGUuAM99YEscGxTMmFMbL9QHPrn9bDtecsSrgbA2kQw7d9+q xiN2JI9VdA/vWyfsMBrsE9SnFWIMKZzHO2i7+szW69Vs/xyUzBTNV0S4GyYzdesdOrG9 YOjA== X-Gm-Message-State: AOAM533ray6sbXvs10gRExt28VYN3MLULuTneqFSbD8WaA6hZF/vd+z2 1o4nmZqDogqnKIL2Urpiz5H6Pjmh6Or4CQ== X-Google-Smtp-Source: ABdhPJx3RYSTCqCub5lOGXrHgg5jPvAqfn3GPah0zCRIxkTx6aci9KeM6XDoX5DGxOYQRxwXx1QUpmsxtpyJMg== X-Received: from dbrazdil.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:7f9b]) (user=dbrazdil job=sendgmr) by 2002:a05:600c:4101:: with SMTP id j1mr308618wmi.110.1627486363975; Wed, 28 Jul 2021 08:32:43 -0700 (PDT) Date: Wed, 28 Jul 2021 15:32:32 +0000 In-Reply-To: <20210728153232.1018911-1-dbrazdil@google.com> Message-Id: <20210728153232.1018911-3-dbrazdil@google.com> Mime-Version: 1.0 References: <20210728153232.1018911-1-dbrazdil@google.com> X-Mailer: git-send-email 2.32.0.432.gabb21c7263-goog Subject: [PATCH 2/2] KVM: arm64: Minor optimization of range_is_memory From: David Brazdil To: kvmarm@lists.cs.columbia.edu Cc: Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Will Deacon , Quentin Perret , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, David Brazdil X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210728_083246_561074_94F677FB X-CRM114-Status: GOOD ( 12.28 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Currently range_is_memory finds the corresponding struct memblock_region for both the lower and upper bounds of the given address range with two rounds of binary search, and then checks that the two memblocks are the same. Simplify this by only doing binary search on the lower bound and then checking that the upper bound is in the same memblock. Signed-off-by: David Brazdil Reviewed-by: Quentin Perret --- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c index a6ce991b1467..37d73af69634 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -189,13 +189,18 @@ static bool find_mem_range(phys_addr_t addr, struct kvm_mem_range *range) return false; } +static bool is_in_mem_range(phys_addr_t addr, struct kvm_mem_range *range) +{ + return range->start <= addr && addr < range->end; +} + static bool range_is_memory(u64 start, u64 end) { - struct kvm_mem_range r1, r2; + struct kvm_mem_range r; - if (!find_mem_range(start, &r1) || !find_mem_range(end - 1, &r2)) + if (!find_mem_range(start, &r)) return false; - if (r1.start != r2.start) + if (!is_in_mem_range(end - 1, &r)) return false; return true;