Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp1463837pxt; Sat, 7 Aug 2021 11:37:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy/xTeA3cNazlD4EgSJahQtMsHduUNtTdCHmqCW2q9LHDE9whZUC+5d0GI45sQziamWwihz X-Received: by 2002:aa7:cdcc:: with SMTP id h12mr20173450edw.39.1628361450235; Sat, 07 Aug 2021 11:37:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628361450; cv=none; d=google.com; s=arc-20160816; b=Y5AsBfcZG0SdZ+cZgxOZSXTt0S3EFwJkDRDzOjgybUZbqhLKmGl2yZJjzomchUqpKu iUZa5sxyWVVn4hrMSROPpDZtSEEQSGqkEmKIhHnpg3dJuMzWoiGfeCY7IZhLyfrCtZhu RusIEkineyniSj3h4QeAgEgGfz3IQ3aVYeVXUJk42zAixWFEDWO30vDH18u2DGseQAfT ZaIB1ao6Nd4un/7ALrO6p62LWu1wRATGuP/Ye6tFp6ZVusRfgR63p4Kz+Wrl1q5gW1eX iM2UTzqCh9jXI8HLTAoBV3RsLJJ8W0kAjatBWEEy6SEbJD58fGKHXpyYEPP9SFKR2Gkt BtcQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=zMQAIZcGPoAZR0d005NxFzAT1cH+2rezexuQq2s5Nmg=; b=o8IZzFHqPjla6/sa5dZ/FgXdzRx+iQ4gueM+0E2VCVNiKRitRtU3c3Kkq38FTKo+Wg kW8TPTgXt5ugRATqvIwoscVTAbcyZmjctXYODUws99nVAe5vI8vwqTCBQdxX4ln/RtcA oa85GIKE0dqZT0eViT+tOG9fEd7pDy7MaZQiTLhwwY4KgJeN9pINEL6ocQt0xu+Vatro 95wGsFmCcAykJPqY2lGhnc99WCN/AAh9pDv7d21qUTlNkxSIHvhyOPBXO0MR2itoRLfE yGgbhnAmfvTVmJ2dJ7uXJaFQQyq/YhYJ5+ULcQ9IvwP4Tl7Vq5ZErsRpfQldhxXxrU/P A4eA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=WnqZF1wE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id re14si12223651ejb.640.2021.08.07.11.37.06; Sat, 07 Aug 2021 11:37:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=WnqZF1wE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229681AbhHGSfy (ORCPT + 99 others); Sat, 7 Aug 2021 14:35:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229464AbhHGSfx (ORCPT ); Sat, 7 Aug 2021 14:35:53 -0400 Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F565C0613CF for ; Sat, 7 Aug 2021 11:35:35 -0700 (PDT) Received: by mail-lf1-x12d.google.com with SMTP id g13so25026946lfj.12 for ; Sat, 07 Aug 2021 11:35:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=zMQAIZcGPoAZR0d005NxFzAT1cH+2rezexuQq2s5Nmg=; b=WnqZF1wEg6hyBHE1RzeEjFG5+hzHVSxSnAZMpPbOlBMQooBO0RA5IbY8DNrVoN9RWx YpbjI/bV25mEJ/9jHY/aJAmGRxNv2Fk5za8qj3d0cwtcZLzU/kQmlEg0ynbgZbHQBtAJ SyMK7xg17VCUnLo4AJBAdxelTzmhVa5HpGVB9ZPDtKsbaHae2XHB6P6SMU6vlxd21Ze6 yl2hzxQ0FM1OL+qomTp3V3CFT0G5MeoHCssKqTVTXbwk+DrVHQdjJgtrO0vOm6AMYGwZ amrH2P+NRNa1ElDrLw6fk3cP4rU/v89a2cXFCXEOT4pcdePV4qAe33rNYeLyTIneOY3W yZBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=zMQAIZcGPoAZR0d005NxFzAT1cH+2rezexuQq2s5Nmg=; b=r6zNuiM7hHUwsxyZU35jlFm6ZW9FVXfzURLkTSjtILBW9n2rnjGrrgvntjEHfO3NJQ VsCQ5vJia2qHKlXSdQY4Ui/odInyVwHVyuPaK5i4VohVuwUO+ekg9EShkcJcRjm/qi3s M8okH0V/oIHMC0XwDKpYVoVCjnJT3nJ+yYPj5gBDcb1Nd5cZLeJn+1OONmpeLPr3Vbnu IQUW5LV4oo2lji93z7Us0eU+AFSKIEENmm6jQ8/Z8ge7jwSyjtR+OOshNXRZWEWMw3aQ bq5zdJ6UwvMyUgLfBnSVNFzKs9xhW59WGllKHjpH1kZby5oIi9OKamVXDFbjYHVzwprI mWEQ== X-Gm-Message-State: AOAM530KRJFivScNEpQLckjkPLPfvtg+xJIi49JA0CBBqhhZhFzbwA/L nwvsqqmKhT5G4dh+KEyqsvYQcHZFS4D4hKWGkaUzcw== X-Received: by 2002:a05:6512:6c9:: with SMTP id u9mr9486675lff.411.1628361333155; Sat, 07 Aug 2021 11:35:33 -0700 (PDT) MIME-Version: 1.0 References: <20210806182126.2842876-1-oupton@google.com> <87pmup1q8w.wl-maz@kernel.org> In-Reply-To: <87pmup1q8w.wl-maz@kernel.org> From: Oliver Upton Date: Sat, 7 Aug 2021 11:35:21 -0700 Message-ID: Subject: Re: [RESEND PATCH] clocksource/arm_arch_timer: Fix masking for high freq counters To: Marc Zyngier Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Mark Rutland , Daniel Lezcano , Thomas Gleixner , Peter Shier , Raghavendra Rao Ananta , Ricardo Koller Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Marc, On Sat, Aug 7, 2021 at 3:52 AM Marc Zyngier wrote: > > Hi Oliver, > > On Fri, 06 Aug 2021 19:21:26 +0100, > Oliver Upton wrote: > > > > Unfortunately, the architecture provides no means to determine the bit > > width of the system counter. However, we do know the following from the > > specification: > > > > - the system counter is at least 56 bits wide > > - Roll-over time of not less than 40 years > > > > To date, the arch timer driver has depended on the first property, > > assuming any system counter to be 56 bits wide and masking off the rest. > > However, combining a narrow clocksource mask with a high frequency > > counter could result in prematurely wrapping the system counter by a > > significant margin. For example, a 56 bit wide, 1GHz system counter > > would wrap in a mere 2.28 years! > > > > This is a problem for two reasons: v8.6+ implementations are required to > > provide a 64 bit, 1GHz system counter. Furthermore, before v8.6, > > implementers may select a counter frequency of their choosing. > > > > Fix the issue by deriving a valid clock mask based on the second > > property from above. Set the floor at 56 bits, since we know no system > > counter is narrower than that. > > > > Suggested-by: Marc Zyngier > > Signed-off-by: Oliver Upton > > --- > > This patch was tested with QEMU, tweaked to provide a 1GHz system > > counter frequency, as I could not easily figure out how to tweak the > > base FVP to provide a 1GHz counter. > > > "bp.refcounter.base_frequency" is the property you are looking for. In > general, passing --list-params to the model reveals a treasure trove > of weird and wonderful options that can be used to configure the model > to your liking. > Ah! Thanks for the tip. I didn't find the fast models documentation to be too terribly helpful. > > > > +/* > > + * The minimum amount of time a generic timer is guaranteed to not roll over > > nit: s/timer/counter/ > Ack. > > + * (40 years) > > For later reference, could you add the section of the ARMv8 ARM where > this is mentioned? Something like 'ARM DDI 0487G.a D11.1.2 ("The > system counter")', either here or in the comment further down. > Sure thing. I think I'll drop it on the function below since we depend on two properties of the system counter there. > > +static int __init arch_counter_get_width(void) > > +{ > > + u64 min_cycles = MIN_ROLLOVER_SECS * arch_timer_get_cntfrq(); > > That's unfortunately wishful thinking. We have stupidly broken systems > out there that do not set CNTFRQ_EL0, and instead rely on DT > properties to describe the counter frequency. You're likely to end up > with a glorious zero as a result, with interesting consequences... Blech. I didn't read the DT hooks for the driver. That's awful. > Use arch_timer_rate instead, which will be set as by the time you > register the counter. Ack. > > > + > > + /* guarantee the returned width is within the valid range */ > > + return max(56, min(64, ilog2(min_cycles))); > > Maybe better written as "clamp_val(ilog2(min_cycles), 56, 64);". > Agreed. > > + width = arch_counter_get_width(); > > + clocksource_counter.mask = CLOCKSOURCE_MASK(width); > > + cyclecounter.mask = CLOCKSOURCE_MASK(width); > > Since you move this to be computed at runtime, how about dropping the > static initialisation of the mask fields? > Yep, already made the change locally after mailing out :) > > + > > if (!arch_counter_suspend_stop) > > clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP; > > start_count = arch_timer_read_counter(); > > @@ -1040,8 +1065,7 @@ static void __init arch_counter_register(unsigned type) > > timecounter_init(&arch_timer_kvm_info.timecounter, > > &cyclecounter, start_count); > > > > - /* 56 bits minimum, so we assume worst case rollover */ > > - sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate); > > + sched_clock_register(arch_timer_read_counter, width, arch_timer_rate); > > } > > > > static void arch_timer_stop(struct clock_event_device *clk) > > Another thing that needs addressing for high frequency counter support > is to move away from TVAL programming and switch to CVAL, as the > maximum deadline we can currently program is 4.2s at 1GHz. > > Fun fact: it has the interesting consequence of breaking XGene-1, > which implemented CVAL in terms of TVAL instead of the other way > around (what were these guys thinking?) Yikes! > , though I don't think anyone > will notice in practice. I have a preliminary patch on a branch > somewhere that I'll try to dust up and post in the coming days. Sounds good, I'll clean this one up and send it out too. -- Thanks, Oliver