Dynamic timecounter changes

From: Sebastian Huber <sebastian.huber_at_embedded-brains.de>
Date: Thu, 28 Oct 2021 08:52:59 UTC
Hello,

there was a recent change which protected timecounter changes with a mutex:

https://github.com/freebsd/freebsd-src/commit/621fd9dcb2d83daab477c130bc99b905f6fc27dc

If the timecounter can change dynamically, could tc_windup() see 
different timercounter here:

	/*
	 * Capture a timecounter delta on the current timecounter and if
	 * changing timecounters, a counter value from the new timecounter.
	 * Update the offset fields accordingly.
	 */
	delta = tc_delta(th);
	if (th->th_counter != timecounter)
		ncount = timecounter->tc_get_timecount(timecounter);
	else
		ncount = 0;

and here:

	/* Now is a good time to change timecounters. */
	if (th->th_counter != timecounter) {
#ifndef __arm__
		if ((timecounter->tc_flags & TC_FLAGS_C2STOP) != 0)
			cpu_disable_c2_sleep++;
		if ((th->th_counter->tc_flags & TC_FLAGS_C2STOP) != 0)
			cpu_disable_c2_sleep--;
#endif
		th->th_counter = timecounter;
		th->th_offset_count = ncount;
		tc_min_ticktock_freq = max(1, timecounter->tc_frequency /
		    (((uint64_t)timecounter->tc_counter_mask + 1) / 3));
		recalculate_scaling_factor_and_large_delta(th);
#ifdef FFCLOCK
		ffclock_change_tc(th);
#endif
	}

An ncount value from two different timecounter would be used in this 
case.  Maybe the "timecounter" global variable should be just read once 
into a local variable.

-- 
embedded brains GmbH
Herr Sebastian HUBER
Dornierstr. 4
82178 Puchheim
Germany
email: sebastian.huber@embedded-brains.de
phone: +49-89-18 94 741 - 16
fax:   +49-89-18 94 741 - 08

Registergericht: Amtsgericht München
Registernummer: HRB 157899
Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
Unsere Datenschutzerklärung finden Sie hier:
https://embedded-brains.de/datenschutzerklaerung/