From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (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 C52DE2877E8 for ; Wed, 15 Jul 2026 13:48:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784123338; cv=none; b=fGLyFAAXVuwCFpSwItnthH6w7m9TqbFbD7NPQSaioNqsESNPJqEEx32vcDvIOGL6B4sAInuLZGl3ZwkULOdQhJ2PCRP1HkSQLRUqiPOeBLB9/TukTwCsPO2Gm5zASSv/VKY/hnWEInoxlwu1m6aSZoLwuTPmb6fpQS54uZYW8mU= 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=YOvMpb7s; arc=none smtp.client-ip=209.85.221.54 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="YOvMpb7s" Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-47dec32798aso5666297f8f.1 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=lists.linux.dev; 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=YOvMpb7sB2EPe6LV4cwBWS7/1K1ad4TRXd4hEWjoBveZZ1p2y2tGuNu7nCRVZetwAn csH+orU52eghmmR5w/S0PxrVMtoRacZpj/fiwQOiMY3tn58E2Z6SFjJnMFhztGexkEGk xTaKQozv+1B3xNg5oU93I9Vt1R3SmxRdRRUJQSMAoex8s0mCTYJJ788tRtWbWVbB2V2P /tvWWV4qm48smzJKQvSbDypq15J9BIVOabmllEzbRjsfFBDvaG2S/FbVOJppw5N4okWC Q/2pRnljE6bfSKDZtakV3fv/mgnbr5V1yEQWFOj560pb8a85Ueeq+7jNt498k/r1FcKM Z2lw== 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=o9wrvg8ReDnbOmbDmI5sGLEYrTg1DOyfDNR92LGsq5BQ6QDZEyt2DWFT8mBXWjeg/R vYSs4b0xOBFGtxz6ZDzZJEXR3JG7Wx6HBWjYsbir5TtkJBHGWuvv5xssH0zgUZ9jAYpq +yhsWOtoJL553tH7VcU6kiayE7YPfW9a68OqGCJD7aMg2Uf6JSpxXLL0dztL8iRe7DDT LnszwwcWG94N8ozwl71FN6vmaUkst2BmNmTWZHZ1t53zZ+WWWiX1g7RMZvQ7ArepR1lt D7dCnQSh2AEMKOxS1I9dsvWn/X6ONkLNes6ZT9oVTU/PU/0NW+m3CV0AVzFMUtwrijuQ lWBw== X-Forwarded-Encrypted: i=1; AHgh+RpTYSaGxoZFqf/YsdBTtbewcvrl+nkZZRa7/yi/LOzQA5vKHxGWgHLOOlGIpL2A6u1vsEG0Dw==@lists.linux.dev X-Gm-Message-State: AOJu0YyHBKzxF/jtYYF47GOMRzzf+vAvxcL5iSbxUKRQTrbwofkdhJtY neRnuDRPTgUcmtcK3LPlFruH4XTuMy8NgeS1NQobEaIJXjci5lIZUvZbkCXo3DY29g== X-Gm-Gg: AfdE7cmgaUWrlyaHTbwm7weNNENM25j4kaI5n3XryTqEf9eN1NiCCIl3C5SFIOV/KcJ A0Utun2DNGLHtvyqZwIfKEnDhHcnHcAoxlx6DFP6uCH42ioGEBIru0Vryy0mKHm0uEp+FDZ/uT/ r417HCe+wrRz5RtzTz0Q4hJXw9iEhFHAI5h34SAPRtsYU4XyCKmeihSLw5Mfq7GyrN0Pdh4stZ3 w3T4tNsD7XLUcfIMOKSXgS+RV+eks00CGBgzJ+ckbcc3+jibWFScKqgsfZSedHuZ08Y2xCmfjcL quXbktAKvt0HHAejaYu0TOaXqX6wqbPkDDQziJTiPE+lRtg4uMely1An6nBoJibqD2DIElBM9zO 5qCV1sCTIc0Au1W2jsf6GQySwPN0gmSUDco3D4V/qtj8qIwvBZUoZXEZJVeuu9NK9ET5voYyUiS v/D8J1D0yrg/PFejkO1y4Rxz8A/XPfmxNROb1bbj4u3ZHQT3jTv2o= 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: iommu@lists.linux.dev 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 >