Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 80f22571 authored by Ingo Molnar's avatar Ingo Molnar
Browse files

time: ntp: refactor do_adjtimex()



Impact: cleanup, no functionality changed

do_adjtimex() is currently a monster function with a maze of
branches. Refactor the txc->modes setting aspects of it into
two new helper functions:

	process_adj_status()
	process_adjtimex_modes()

kernel/time/ntp.o:

   text	   data	    bss	    dec	    hex	filename
   2512	    114	    136	   2762	    aca	ntp.o.before
   2512	    114	    136	   2762	    aca	ntp.o.after

Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent 10dd31a7
Loading
Loading
Loading
Loading
+99 −83
Original line number Diff line number Diff line
@@ -332,68 +332,21 @@ static void notify_cmos_timer(void)
static inline void notify_cmos_timer(void) { }
#endif

/*
 * adjtimex mainly allows reading (and writing, if superuser) of
 * kernel time-keeping variables. used by xntpd.
 */
int do_adjtimex(struct timex *txc)
{
	struct timespec ts;
	int result;

	/* Validate the data before disabling interrupts */
	if (txc->modes & ADJ_ADJTIME) {
		/* singleshot must not be used with any other mode bits */
		if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
			return -EINVAL;
		if (!(txc->modes & ADJ_OFFSET_READONLY) &&
		    !capable(CAP_SYS_TIME))
			return -EPERM;
	} else {
		/* In order to modify anything, you gotta be super-user! */
		 if (txc->modes && !capable(CAP_SYS_TIME))
			return -EPERM;

/*
		 * if the quartz is off by more than 10% then
		 * something is VERY wrong!
 * Propagate a new txc->status value into the NTP state:
 */
		if (txc->modes & ADJ_TICK &&
		    (txc->tick <  900000/USER_HZ ||
		     txc->tick > 1100000/USER_HZ))
				return -EINVAL;

		if (txc->modes & ADJ_STATUS && time_state != TIME_OK)
			hrtimer_cancel(&leap_timer);
	}

	getnstimeofday(&ts);

	write_seqlock_irq(&xtime_lock);

	/* If there are input parameters, then process them */
	if (txc->modes & ADJ_ADJTIME) {
		long save_adjust = time_adjust;

		if (!(txc->modes & ADJ_OFFSET_READONLY)) {
			/* adjtime() is independent from ntp_adjtime() */
			time_adjust = txc->offset;
			ntp_update_frequency();
		}
		txc->offset = save_adjust;
		goto adj_done;
	}
	if (txc->modes) {
		long sec;
static inline void process_adj_status(struct timex *txc, struct timespec *ts)
{
	long now;

		if (txc->modes & ADJ_STATUS) {
			if ((time_status & STA_PLL) &&
			    !(txc->status & STA_PLL)) {
	if ((time_status & STA_PLL) && !(txc->status & STA_PLL)) {
		time_state = TIME_OK;
		time_status = STA_UNSYNC;
	}
	/* only set allowed bits */
	time_status &= STA_RONLY;

	/*
	 * If we turn on PLL adjustments then reset the
	 * reference time to current time.
@@ -406,22 +359,21 @@ int do_adjtimex(struct timex *txc)
	switch (time_state) {
	case TIME_OK:
	start_timer:
				sec = ts.tv_sec;
		now = ts->tv_sec;
		if (time_status & STA_INS) {
			time_state = TIME_INS;
					sec += 86400 - sec % 86400;
					hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
			now += 86400 - now % 86400;
			hrtimer_start(&leap_timer, ktime_set(now, 0), HRTIMER_MODE_ABS);
		} else if (time_status & STA_DEL) {
			time_state = TIME_DEL;
					sec += 86400 - (sec + 1) % 86400;
					hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
			now += 86400 - (now + 1) % 86400;
			hrtimer_start(&leap_timer, ktime_set(now, 0), HRTIMER_MODE_ABS);
		}
		break;
	case TIME_INS:
	case TIME_DEL:
		time_state = TIME_OK;
		goto start_timer;
				break;
	case TIME_WAIT:
		if (!(time_status & (STA_INS | STA_DEL)))
			time_state = TIME_OK;
@@ -431,6 +383,14 @@ int do_adjtimex(struct timex *txc)
		break;
	}
}
/*
 * Called with the xtime lock held, so we can access and modify
 * all the global NTP state:
 */
static inline void process_adjtimex_modes(struct timex *txc, struct timespec *ts)
{
	if (txc->modes & ADJ_STATUS)
		process_adj_status(txc, ts);

	if (txc->modes & ADJ_NANO)
		time_status |= STA_NANO;
@@ -468,6 +428,62 @@ int do_adjtimex(struct timex *txc)
		ntp_update_frequency();
}

/*
 * adjtimex mainly allows reading (and writing, if superuser) of
 * kernel time-keeping variables. used by xntpd.
 */
int do_adjtimex(struct timex *txc)
{
	struct timespec ts;
	int result;

	/* Validate the data before disabling interrupts */
	if (txc->modes & ADJ_ADJTIME) {
		/* singleshot must not be used with any other mode bits */
		if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
			return -EINVAL;
		if (!(txc->modes & ADJ_OFFSET_READONLY) &&
		    !capable(CAP_SYS_TIME))
			return -EPERM;
	} else {
		/* In order to modify anything, you gotta be super-user! */
		 if (txc->modes && !capable(CAP_SYS_TIME))
			return -EPERM;

		/*
		 * if the quartz is off by more than 10% then
		 * something is VERY wrong!
		 */
		if (txc->modes & ADJ_TICK &&
		    (txc->tick <  900000/USER_HZ ||
		     txc->tick > 1100000/USER_HZ))
				return -EINVAL;

		if (txc->modes & ADJ_STATUS && time_state != TIME_OK)
			hrtimer_cancel(&leap_timer);
	}

	getnstimeofday(&ts);

	write_seqlock_irq(&xtime_lock);

	/* If there are input parameters, then process them */
	if (txc->modes & ADJ_ADJTIME) {
		long save_adjust = time_adjust;

		if (!(txc->modes & ADJ_OFFSET_READONLY)) {
			/* adjtime() is independent from ntp_adjtime() */
			time_adjust = txc->offset;
			ntp_update_frequency();
		}
		txc->offset = save_adjust;
		goto adj_done;
	}

	/* If there are input parameters, then process them: */
	if (txc->modes)
		process_adjtimex_modes(txc, &ts);

	txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
				  NTP_SCALE_SHIFT);
	if (!(time_status & STA_NANO))