From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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 D7CD62BE639 for ; Wed, 15 Jul 2026 13:48:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784123338; cv=none; b=A9k44RbZ6GB7nZE5wEZvM5MKxRvn39XJve1Ere7/h1s0zFZs/3x41/JAjYftbZwXKjjie3KgKEK0BKn85KfilN+z3y92+bdH0cHMnW9eIoG+lofhuEl1A2xx21sOHtxPCN44JvuxoOJ53rvmML06M4mcFQ9dxG4RgmldkJURRyk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784123338; c=relaxed/simple; bh=R31e1SoerTZ8of3ARHekbq4thNd6ZQJ/0j9Rx7xDzHs=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=X9jiAsVBHPIEn8OGuMZWSKAsZyZJOTTXOI4nmRWWS8Jw5vq+2Z9M2l2M4giwfX1BtrEL5jZEwxH32aMTWuS9dK/eCcNRdp9iubHuRbK6WjMhHCYgaNrtVkipUWWJSvOme0lrS+BL0FjEwXOc0TeImRMBoJPdziw6QtT89e6D7XU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=u0OP8JQj; arc=none smtp.client-ip=209.85.221.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="u0OP8JQj" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-476d8e647e9so5216191f8f.0 for ; Wed, 15 Jul 2026 06:48:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784123335; x=1784728135; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=OSkZawG7pcsTcJF66DLNdFJcFWhB9tSjWpIVbvD9zzY=; b=u0OP8JQjRWYmZSbjB6nc/EVtfcZg+a0p0iI3xk1u2GkjLHyQm8n3t01+jelnkOetTV pUTPOS6LxcTS4eU4uHA4vU6RGDYRuYP1M5fgOyVEVjwv4YgAjPQt3ADfvgFwUsXJVwUD UQ6SOZcKrY61DS8JG6yL4l1MAwXI2biQ5YWnIsn3hg9l+5UOJkKy72TCZWWyriRYwfN6 gJ/Nhoi87jTYatob+6TiwMq06SabF4N2TfFrbMgN8UcwgBPnOIbDZwrt4SLWLti8hIeX chZ+OOvjN6m5ZhULC1z6IjVRDH9l0OVA1BpR/nblMlmph9kVqfLVHWqD7AlYqabttEJf Exng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784123335; x=1784728135; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=OSkZawG7pcsTcJF66DLNdFJcFWhB9tSjWpIVbvD9zzY=; b=Y+rdtcf9D7NrDzypPB9CzddOhh9lJkKYX8S32MFvKsXkEGTkWBTSHDs2nsT/xgX11X MrLiLwCv9uI2SAgL1CdjJRCQNPw5dyonjkHsVE04Vyy624NHj76zMBMVi+K8aaAn9TIc bSTvs3SSigXzPtLMc104Cfk5HKF/t+KvD3zj/i0toi7Y7AG/Cm8WYMb+3r+Z2tSf/d6Y JRySM0z6NEnr9c7cM6dHDRSnOEJI5bv+dueqD+f2cHtcdvZUtBBXxCfohozquyQ9KSSg L4/Ng88Q4Q/rBfASJ2AYsB1oMTjdUvLBTBEuG1VmoYogav9o9ooxexq74MgtCEUJnNJW uQwg== X-Forwarded-Encrypted: i=1; AHgh+RqyDTrZnHdsfYry45rRJV5RDe8iZBJ/t80koVYDJZ003LICXhovQNjZSrkD1ltHPx74TTSHSr5a6tHAm+0=@vger.kernel.org X-Gm-Message-State: AOJu0YzzX09wZZQkRQDeV2oaBWNYhwX1GQi1t6HDIKwhH6xHmUy9i224 RDpfDZV/3d+Rw7+N6Wt8/KyUZK6p9k/wEVy7xRDNiS9wUulhjvTgUu1Y84lpTESpad1UKiSuulI BV8qnxQ== X-Gm-Gg: AfdE7ckUOZlDyabQF69Cym4323q4/uCpX/E1ESREjhjBdcroW6M8vV1f1dmgZIs8XJ5 22LiXyRg0H7anucsBu0S6L9DV62lA9AopW+e9J0LRDA0kEUwjU3W88ub60+5ZG8VnmOD4RxEovX 4O8KMaR7kJa6Os52NPLF4syBXpfcloCCbjfnGdEX5XSUBu0dQZBXmMKKevWuPKd6ZMv//kqIqjj P+9tq1GEpyZoPWB4pNHgaTdStG15FQTw3dNixxRT83L2KLz3nbsubjwrEKKjk4jbhPU8l0XY2Ii pRTUpEWc57q3Yt2nAX+yF+me7R2uKmHoSsJhjMAa7IDG3tAUAcGZn9l9EEWKWQSgG04jN2tsvcY xsvVnEFkO7G1WKVAM6WTTVNUOoDNI4L89xkcOoeSywgwEzqpohqv7IUmPQqFXEuPXm4KiniZLfB qPifo/15swnz8iwXcgoVgJdNlZQK8rkemTMaeUu4m6d4NN01QxzSY= X-Received: by 2002:a05:6000:491a:b0:473:24e3:fa8f with SMTP id ffacd0b85a97d-47f488a7656mr7712739f8f.47.1784123334603; Wed, 15 Jul 2026 06:48:54 -0700 (PDT) Received: from google.com (137.69.77.34.bc.googleusercontent.com. [34.77.69.137]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47f476e9d02sm16837588f8f.19.2026.07.15.06.48.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jul 2026 06:48:53 -0700 (PDT) Date: Wed, 15 Jul 2026 14:48:50 +0100 From: Vincent Donnefort To: Mostafa Saleh Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev, iommu@lists.linux.dev, catalin.marinas@arm.com, will@kernel.org, maz@kernel.org, oliver.upton@linux.dev, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, joro@8bytes.org, jgg@ziepe.ca, mark.rutland@arm.com, qperret@google.com, tabba@google.com, sebastianene@google.com, keirf@google.com Subject: Re: [PATCH v7 01/24] KVM: arm64: Add a generic clock Message-ID: References: <20260715115906.2664882-1-smostafa@google.com> <20260715115906.2664882-2-smostafa@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260715115906.2664882-2-smostafa@google.com> [...] > diff --git a/arch/arm64/kvm/hyp/nvhe/clock.c b/arch/arm64/kvm/hyp/nvhe/clock.c > index f3e2619db4e4..43d2cba4f810 100644 > --- a/arch/arm64/kvm/hyp/nvhe/clock.c > +++ b/arch/arm64/kvm/hyp/nvhe/clock.c > @@ -8,7 +8,12 @@ > > #include > #include > +#include > +#include > > +static u32 timer_freq; > + > +#ifdef CONFIG_NVHE_EL2_TRACING > static struct clock_data { > struct { > u32 mult; > @@ -66,3 +71,27 @@ u64 trace_hyp_clock(void) > > return (u64)ns + clock->data[bank].epoch_ns; > } > +#endif /* CONFIG_NVHE_EL2_TRACING */ > + > +int hyp_clock_init(void) > +{ > + timer_freq = read_sysreg(cntfrq_el0); > + /* > + * KVM will not initialize if FW didn't set cntfrq_el0, that is already > + * part of the boot protocol. > + */ > + if (!timer_freq) > + return -ENODEV; > + > + /* Timer freq can't be larger than 1Ghz by spec. */ > + if (timer_freq > NSEC_PER_SEC) > + return -EINVAL; > + > + return 0; > +} > + > +/* Return time in ns. */ > +u64 hyp_clock_ns(void) > +{ > + return mul_u64_u32_div(__arch_counter_get_cntvct(), NSEC_PER_SEC, timer_freq); IIUC, this will overflow the u64 mult very quickly (in few minutes) and also I see that we don't need such small nanoseconds accuracy. So here we could always fallback to 128-bits mult... or update the epoch from time to time. But I have something completely different to propose: Instead of using a "clock" how about we just modify smmu_wait? #define smmu_wait() { static u32 window = arch_timer_get_cntfrq() / ARM_SMMU_EL2_POLL_TIMEOUT_US u64 timeout = __arch_counter_get_cntvct() + window; u64 cur; ... while (!(__cond)) { ... cur = __arch_counter_get_cntvct(); if (cur >= timeout) __ret = -ETIMEOUT; No risk of overflowing u64 No init necessary > +} > diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setup.c > index 75b00c323310..970c5cf342f5 100644 > --- a/arch/arm64/kvm/hyp/nvhe/setup.c > +++ b/arch/arm64/kvm/hyp/nvhe/setup.c > @@ -10,6 +10,7 @@ > #include > #include > > +#include > #include > #include > #include > @@ -320,6 +321,10 @@ void __noreturn __pkvm_init_finalise(void) > if (ret) > goto out; > > + ret = hyp_clock_init(); > + if (ret) > + goto out; > + > ret = fix_host_ownership(); > if (ret) > goto out; > -- > 2.55.0.141.g00534a21ce-goog >