From mboxrd@z Thu Jan 1 00:00:00 1970 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=-6.6 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,FSL_HELO_FAKE, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=no 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 63602C4338F for ; Mon, 26 Jul 2021 13:15:13 +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 2BBCA6056C for ; Mon, 26 Jul 2021 13:15:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 2BBCA6056C 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:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ismow6yWTuV+R5Z1cW9gx5xCi8Klve2Jl7wjuoTh5B8=; b=IcpSxxMwWSrae0 7+HUxwpTIuK76e0s3Os28KNpWlGF+X4rUOQi670mIZ/RlTzhVabWZQmxTpoW8gpA5KMQeuqYqEwiH Zzw2PybtvczSeqgYp0R9uVyDe701kkDh+NIWrdrTPREIGVw2Ec+1HaPgI/m/Py6/vWP/XFprnL4+G k4PYsiPG1caFnSnaQWQGhHzIn1tk8vojwwtQy0SGyICvNcqi8tzYXGe1YwswudfnwrI7jEJWQwZpW JxUSNopSdKnZR7bj55ISFoRK/4Vbwc0M6ZDUzvQ+j/BIic3rHP1aEoGOt4CQ8QiRf/EDWW4ncLQTs DWd0ho6ex7p1DULGEz+Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m80Pz-00BHjc-Mk; Mon, 26 Jul 2021 13:13:15 +0000 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m80Pv-00BHil-0l for linux-arm-kernel@lists.infradead.org; Mon, 26 Jul 2021 13:13:12 +0000 Received: by mail-wm1-x332.google.com with SMTP id e25-20020a05600c4b99b0290253418ba0fbso149353wmp.1 for ; Mon, 26 Jul 2021 06:13:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=ZtU7fvZEyyOLQgnySOlDvGY7f3ikYJEH6+mfO2JIeiw=; b=YFyKwtLN1bSSFHloIhy0fW6Dhxolu5g2e/6SEkdyGH1aiDKhpgyhOGUJHd1xSooBXA UpvvIqh+avoVzy3OYPWTNNy5ahUcREJDczLQ01VVwk3UKTyvHawERdAeLrXAEa0Jj+k+ adbZCEMgpl2zRM89TwupWHr68qDbMaFItJ+MTlezkUjQSwzdN1HIBhE2/bqOxmbv3wz7 Wi/T620Hs0l757Y8lVpPETU0D1YKKVdWNKKRcEeBGAUCnENLQ5LFdNONRT2I1Re/VAdB xXsmlyhQtfVry/uGikW3+jSh0sfhWnOqAu/I7Wclztk+OLiUdmTEBfWCQXpB2xvd3ju9 GyDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=ZtU7fvZEyyOLQgnySOlDvGY7f3ikYJEH6+mfO2JIeiw=; b=uZT5KYGJMhjVI0YmptW1j2T2rdxUYk7qtHwxqHtDyHdHDrvtQU+AlJJFXhXrXxibQj KkRVImHdHBDfc3UI74GeRKiXt7gnDtnnSl0X1rPZL9zd8u8ES8/bc1CIAOOaRAlsvrO7 9I2KJt4J9O+DRVNvGruTumvRIJI/Ulz3BhJhUGf0bBQXWLIvXSh2hLxG9GErQd1WZHD1 ti3mVffVvixg6vgZLmlmZ8ATXcjxU9y3WNj9Ua7dkvSJzxHotRj5n3HFhsJ34o6EeuQa +iW0zOcF2goLoGVfLPOqs0/mxfNEEZV8uCb8A/rP51/ztN4feQNjF/px1a9hAbxqgSTJ drDA== X-Gm-Message-State: AOAM5334bX308E0B20pcE2U9Dy/rX423oxNCp7J4X10T+eCMm8enjXFm /03brOW5vQ5mygX2f9Pboa43zQ== X-Google-Smtp-Source: ABdhPJw3iV0iD0VNKcKvIFGEO/4SIbrCzeF0z8XajY3jrjx5HJnK1HWQwzfNfl/GjgZNs2EuDMsB/A== X-Received: by 2002:a1c:3886:: with SMTP id f128mr1889070wma.85.1627305189353; Mon, 26 Jul 2021 06:13:09 -0700 (PDT) Received: from google.com ([2a00:79e0:d:210:c468:e033:fa2b:3a6]) by smtp.gmail.com with ESMTPSA id x8sm42479052wrt.93.2021.07.26.06.13.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jul 2021 06:13:09 -0700 (PDT) Date: Mon, 26 Jul 2021 14:13:06 +0100 From: Quentin Perret To: Marc Zyngier Cc: james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, will@kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, ardb@kernel.org, qwandor@google.com, tabba@google.com, dbrazdil@google.com, kernel-team@android.com Subject: Re: [PATCH v2 04/16] KVM: arm64: Optimize host memory aborts Message-ID: References: <20210726092905.2198501-1-qperret@google.com> <20210726092905.2198501-5-qperret@google.com> <87pmv5e529.wl-maz@kernel.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <87pmv5e529.wl-maz@kernel.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210726_061311_106110_F9CB477C X-CRM114-Status: GOOD ( 23.14 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Monday 26 Jul 2021 at 11:35:10 (+0100), Marc Zyngier wrote: > On Mon, 26 Jul 2021 10:28:53 +0100, > Quentin Perret wrote: > > +static int host_stage2_find_range(u64 addr, struct kvm_mem_range *range) > > nit: I find 'find_range' a bit odd. We already have found a > range. We're just trying to narrow it down to something that fits in a > single block mapping. How about 'host_stage2_adjust_range'? Ack. > > +{ > > + u64 granule, start, end; > > + kvm_pte_t pte; > > + u32 level; > > + int ret; > > + > > + ret = kvm_pgtable_get_leaf(&host_kvm.pgt, addr, &pte, &level); > > + if (ret) > > + return ret; > > + > > + if (kvm_pte_valid(pte)) > > + return -EAGAIN; > > + > > + if (pte) > > + return -EPERM; > > + > > + do { > > + granule = kvm_granule_size(level); > > + start = ALIGN_DOWN(addr, granule); > > + end = start + granule; > > + level++; > > + } while ((level < KVM_PGTABLE_MAX_LEVELS) && > > + (!kvm_level_supports_block_mapping(level) || > > + start < range->start || range->end < end)); > > + > > This expression does my head in. You are trying to find the largest > block mapping that entirely fits in range, right? Can we just express > that directly (with a global negation for the purpose of the loop)? > > do { > [...] > } while (level < KVM_PGTABLE_MAX_LEVELS && > !(kvm_level_supports_block_mapping(level) && > start >= range->start && > end <= range->end)); > > I personally find this much more readable, because it expresses the > condition we are looking for rather than a lot of conditions forcing > us to continue. > > You could also use a kvm_mem_range for the iteration, and add a helper > that checks for the inclusion. Something like this (untested)? diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c index 75273166d2c5..07d228163090 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -234,9 +234,15 @@ static inline int __host_stage2_idmap(u64 start, u64 end, __ret; \ }) +static inline bool range_included(struct kvm_mem_range *child, + struct kvm_mem_range *parent) +{ + return parent->start <= child->start && child->end <= parent->end; +} + static int host_stage2_find_range(u64 addr, struct kvm_mem_range *range) { - u64 granule, start, end; + struct kvm_mem_range cur; kvm_pte_t pte; u32 level; int ret; @@ -252,16 +258,15 @@ static int host_stage2_find_range(u64 addr, struct kvm_mem_range *range) return -EPERM; do { - granule = kvm_granule_size(level); - start = ALIGN_DOWN(addr, granule); - end = start + granule; + u64 granule = kvm_granule_size(level); + cur.start = ALIGN_DOWN(addr, granule); + cur.end = cur.start + granule; level++; } while ((level < KVM_PGTABLE_MAX_LEVELS) && - (!kvm_level_supports_block_mapping(level) || - start < range->start || range->end < end)); + !(kvm_level_supports_block_mapping(level) && + range_included(&cur, parent))); - range->start = start; - range->end = end; + *range = cur; return 0; } _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel