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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 81287C43334 for ; Sun, 12 Jun 2022 08:59:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235140AbiFLI7f (ORCPT ); Sun, 12 Jun 2022 04:59:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33758 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233187AbiFLI7e (ORCPT ); Sun, 12 Jun 2022 04:59:34 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C654A45516 for ; Sun, 12 Jun 2022 01:59:33 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 846BEB80B68 for ; Sun, 12 Jun 2022 08:59:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 40D6CC34115; Sun, 12 Jun 2022 08:59:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1655024371; bh=lAZd4om3O/HhLmnjR6wY2GSgGFgCHW1e+OFvSSyBIds=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=tBA2DhCeU+J+TQVwkHA+0m0syzbtrutzouveFNnovfkfbe0tG/RExhAV406Y2kiw0 1GtK06OLvCwuNweJo2nu3xZf7rAT0PgP7KWuIAEt2AZNbQZKJaiUrQ6IbcWwPql/VR C5BcXeACz3JXJwg9ujtbrl4FKnqRoF0CEWXydbtWRiovHIHNqqYQXwnd6yMC9nnl9w O4npE1M3QOAr1s09VQyFzx+OrtcZeSEawgDxlV/72FkmhLryGee1R37WtvoOhw9z6v inxqYzTUdYWnhDUMuxNxfBwJeEeyY1yxP1xPiVSgSINnJz87s7Jz9jKRJF8nvWDqZW rlapsgc4Mg+JA== Received: from sofa.misterjones.org ([185.219.108.64] helo=wait-a-minute.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1o0JRQ-00HVLI-W9; Sun, 12 Jun 2022 09:59:29 +0100 Date: Sun, 12 Jun 2022 09:59:03 +0100 Message-ID: <87zgiip8a0.wl-maz@kernel.org> From: Marc Zyngier To: Reiji Watanabe Cc: kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, Linux ARM , James Morse , Suzuki K Poulose , Alexandru Elisei , Oliver Upton , Will Deacon , Fuad Tabba , Quentin Perret , Mark Brown , kernel-team@android.com Subject: Re: [PATCH v2 05/19] KVM: arm64: Add helpers to manipulate vcpu flags among a set In-Reply-To: References: <20220610092838.1205755-1-maz@kernel.org> <20220610092838.1205755-6-maz@kernel.org> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/27.1 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: reijiw@google.com, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, james.morse@arm.com, suzuki.poulose@arm.com, alexandru.elisei@arm.com, oupton@google.com, will@kernel.org, tabba@google.com, qperret@google.com, broonie@kernel.org, kernel-team@android.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org On Sat, 11 Jun 2022 19:37:50 +0100, Reiji Watanabe wrote: > > On Fri, Jun 10, 2022 at 2:28 AM Marc Zyngier wrote: > > > > Careful analysis of the vcpu flags show that this is a mix of > > configuration, communication between the host and the hypervisor, > > as well as anciliary state that has no consistency. It'd be a lot > > better if we could split these flags into consistent categories. > > > > However, even if we split these flags apart, we want to make sure > > that each flag can only be applied to its own set, and not across > > sets. > > > > To achieve this, use a preprocessor hack so that each flag is always > > associated with: > > > > - the set that contains it, > > > > - a mask that describe all the bits that contain it (for a simple > > flag, this is the same thing as the flag itself, but we will > > eventually have values that cover multiple bits at once). > > > > Each flag is thus a triplet that is not directly usable as a value, > > but used by three helpers that allow the flag to be set, cleared, > > and fetched. By mandating the use of such helper, we can easily > > enforce that a flag can only be used with the set it belongs to. > > > > Finally, one last helper "unpacks" the raw value from the triplet > > that represents a flag, which is useful for multi-bit values that > > need to be enumerated (in a switch statement, for example). > > > > Further patches will start making use of this infrastructure. > > > > Signed-off-by: Marc Zyngier > > --- > > arch/arm64/include/asm/kvm_host.h | 44 +++++++++++++++++++++++++++++++ > > 1 file changed, 44 insertions(+) > > > > diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h > > index 372c5642cfab..6d30ac7e3164 100644 > > --- a/arch/arm64/include/asm/kvm_host.h > > +++ b/arch/arm64/include/asm/kvm_host.h > > @@ -415,6 +415,50 @@ struct kvm_vcpu_arch { > > } steal; > > }; > > > > +/* > > + * Each 'flag' is composed of a comma-separated triplet: > > + * > > + * - the flag-set it belongs to in the vcpu->arch structure > > + * - the value for that flag > > + * - the mask for that flag > > + * > > + * __vcpu_single_flag() builds such a triplet for a single-bit flag. > > + * unpack_vcpu_flag() extract the flag value from the triplet for > > + * direct use outside of the flag accessors. > > + */ > > +#define __vcpu_single_flag(_set, _f) _set, (_f), (_f) > > + > > +#define __unpack_flag(_set, _f, _m) _f > > +#define unpack_vcpu_flag(...) __unpack_flag(__VA_ARGS__) > > + > > +#define __vcpu_get_flag(v, flagset, f, m) \ > > + ({ \ > > + v->arch.flagset & (m); \ > > + }) > > + > > +#define __vcpu_set_flag(v, flagset, f, m) \ > > + do { \ > > + typeof(v->arch.flagset) *fset; \ > > + \ > > + fset = &v->arch.flagset; \ > > + if (HWEIGHT(m) > 1) \ > > + *fset &= ~(m); \ > > + *fset |= (f); \ > > + } while (0) > > + > > +#define __vcpu_clear_flag(v, flagset, f, m) \ > > + do { \ > > + typeof(v->arch.flagset) *fset; \ > > + \ > > + fset = &v->arch.flagset; \ > > + *fset &= ~(m); \ > > + } while (0) > > Reviewed-by: Reiji Watanabe > > IMHO I would prefer to have 'v' enclosed in parentheses in the > implementation of __vcpu_{get,set,clear}_flag rather than in > the implementation of vcpu_{get,set,clear}_flag though. > (That was what I meant in my comment for v1) I understood what you were asking the first place, but I don't think this has any advantage over what is above. __vcpu_{get,set,clear}_flag are not meant to be used directly, and adding extra bracketing to these only makes them more painful to read. If you can show a case where it breaks, I'll be happy to revisit this. Thanks, M. -- Without deviation from the norm, progress is not possible.