From: john stultz <johnstul@us.ibm.com>
To: lkml <linux-kernel@vger.kernel.org>
Cc: George Anzinger <george@mvista.com>,
frank@tuxrocks.com, Anton Blanchard <anton@samba.org>,
benh@kernel.crashing.org, Nishanth Aravamudan <nacc@us.ibm.com>,
Roman Zippel <zippel@linux-m68k.org>
Subject: [RFC][PATCH - 6/12] NTP cleanup: Clean up ntp_adjtimex() arguement checking
Date: Fri, 15 Jul 2005 20:02:05 -0700 [thread overview]
Message-ID: <1121482925.25236.42.camel@cog.beaverton.ibm.com> (raw)
In-Reply-To: <1121482804.25236.39.camel@cog.beaverton.ibm.com>
All,
Currently ntp_adjtimex() checks the validity of a few arguments values
then takes the xtime_lock then checks the validity of more arguments
while it parses them. This separates the logic so we check the validity
of all arguments before aquiring the xtime lock. This greatly improves
the readability of the code.
Any comments or feedback would be greatly appreciated.
thanks
-john
linux-2.6.13-rc3_timeofday-ntp-part6_B4.patch
============================================
diff --git a/kernel/ntp.c b/kernel/ntp.c
--- a/kernel/ntp.c
+++ b/kernel/ntp.c
@@ -267,21 +267,44 @@ int ntp_adjtimex(struct timex *txc)
/* Now we validate the data before disabling interrupts */
- if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
- /* singleshot must not be used with any other mode bits */
- if (txc->modes != ADJ_OFFSET_SINGLESHOT)
- return -EINVAL;
-
- if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
- /* adjustment Offset limited to +- .512 seconds */
- if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
- return -EINVAL;
-
- /* if the quartz is off by more than 10% something is VERY wrong ! */
- if (txc->modes & ADJ_TICK)
- if (txc->tick < 900000/USER_HZ ||
- txc->tick > 1100000/USER_HZ)
- return -EINVAL;
+ /* frequency adjustment limited to +/- MAXFREQ */
+ if ((txc->modes & ADJ_FREQUENCY)
+ && (abs(txc->freq) > MAXFREQ))
+ return -EINVAL;
+
+ /* maxerror adjustment limited to NTP_PHASE_LIMIT */
+ if ((txc->modes & ADJ_MAXERROR)
+ && (txc->maxerror < 0
+ || txc->maxerror >= NTP_PHASE_LIMIT))
+ return -EINVAL;
+
+ /* esterror adjustment limited to NTP_PHASE_LIMIT */
+ if ((txc->modes & ADJ_ESTERROR)
+ && (txc->esterror < 0
+ || txc->esterror >= NTP_PHASE_LIMIT))
+ return -EINVAL;
+
+ /* constant adjustment must be positive */
+ if ((txc->modes & ADJ_TIMECONST)
+ && (txc->constant < 0)) /* NTP v4 uses values > 6 */
+ return -EINVAL;
+
+ /* Single shot mode can only be used by itself */
+ if (((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
+ && (txc->modes != ADJ_OFFSET_SINGLESHOT))
+ return -EINVAL;
+
+ /* offset adjustment limited to +/- MAXPHASE */
+ if ((txc->modes != ADJ_OFFSET_SINGLESHOT)
+ && (txc->modes & ADJ_OFFSET)
+ && (abs(txc->offset)>= MAXPHASE))
+ return -EINVAL;
+
+ /* tick adjustment limited to 10% */
+ if ((txc->modes & ADJ_TICK)
+ && ((txc->tick < 900000/USER_HZ)
+ ||(txc->tick > 11000000/USER_HZ)))
+ return -EINVAL;
write_seqlock_irq(&xtime_lock);
result = time_state; /* mostly `TIME_OK' */
@@ -290,65 +313,42 @@ int ntp_adjtimex(struct timex *txc)
save_adjust = time_next_adjust ? time_next_adjust : time_adjust;
/* If there are input parameters, then process them */
- if (txc->modes) {
- if (txc->modes & ADJ_STATUS) /* only set allowed bits */
- time_status = (txc->status & ~STA_RONLY) |
- (time_status & STA_RONLY);
-
- if (txc->modes & ADJ_FREQUENCY) { /* p. 22 */
- if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
- result = -EINVAL;
- goto leave;
- }
- time_freq = txc->freq;
- }
-
- if (txc->modes & ADJ_MAXERROR) {
- if (txc->maxerror < 0
- || txc->maxerror >= NTP_PHASE_LIMIT) {
- result = -EINVAL;
- goto leave;
- }
- time_maxerror = txc->maxerror;
- }
-
- if (txc->modes & ADJ_ESTERROR) {
- if (txc->esterror < 0
- || txc->esterror >= NTP_PHASE_LIMIT) {
- result = -EINVAL;
- goto leave;
- }
- time_esterror = txc->esterror;
- }
- if (txc->modes & ADJ_TIMECONST) { /* p. 24 */
- if (txc->constant < 0) { /* NTP v4 uses values > 6 */
- result = -EINVAL;
- goto leave;
- }
- time_constant = txc->constant;
- }
+ if (txc->modes & ADJ_STATUS) /* only set allowed bits */
+ time_status = (txc->status & ~STA_RONLY) |
+ (time_status & STA_RONLY);
+
+ if (txc->modes & ADJ_FREQUENCY)
+ time_freq = txc->freq;
+
+ if (txc->modes & ADJ_MAXERROR)
+ time_maxerror = txc->maxerror;
+
+ if (txc->modes & ADJ_ESTERROR)
+ time_esterror = txc->esterror;
+
+ if (txc->modes & ADJ_TIMECONST)
+ time_constant = txc->constant;
+
+ if (txc->modes & ADJ_OFFSET) {
+ if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
+ /* adjtime() is independent from ntp_adjtime() */
+ if ((time_next_adjust = txc->offset) == 0)
+ time_adjust = 0;
+ } else if (ntp_hardupdate(txc->offset, xtime))
+ result = TIME_ERROR;
+ }
- if (txc->modes & ADJ_OFFSET) { /* values checked earlier */
- if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
- /* adjtime() is independent from ntp_adjtime() */
- if ((time_next_adjust = txc->offset) == 0)
- time_adjust = 0;
- else if (ntp_hardupdate(txc->offset, xtime))
- result = TIME_ERROR;
- }
- } /* txc->modes & ADJ_OFFSET */
-
- if (txc->modes & ADJ_TICK) {
- tick_usec = txc->tick;
- tick_nsec = TICK_USEC_TO_NSEC(tick_usec);
- }
- } /* txc->modes */
-leave:
+ if (txc->modes & ADJ_TICK) {
+ tick_usec = txc->tick;
+ tick_nsec = TICK_USEC_TO_NSEC(tick_usec);
+ }
if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
result = TIME_ERROR;
+ /* write kernel state to user timex values*/
+
if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
txc->offset = save_adjust;
else {
next prev parent reply other threads:[~2005-07-16 3:04 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-07-16 2:55 [RFC - 0/12] NTP cleanup work (v. B4) john stultz
2005-07-16 2:57 ` [RFC][PATCH - 1/12] NTP cleanup: Move NTP code into ntp.c john stultz
2005-07-16 2:57 ` [RFC][PATCH - 2/12] NTP cleanup: Move arches to new ntp interfaces john stultz
2005-07-16 2:58 ` [RFC][PATCH - 3/12] NTP cleanup: Remove unused NTP PPS code john stultz
2005-07-16 2:59 ` [RFC][PATCH - 4/12] NTP cleanup: Breakup ntp_adjtimex() john stultz
2005-07-16 3:00 ` [RFC][PATCH - 5/12] NTP cleanup: Break out leapsecond processing john stultz
2005-07-16 3:02 ` john stultz [this message]
2005-07-16 3:04 ` [RFC][PATCH - 7/12] NTP cleanup: Cleanup signed shifting logic john stultz
2005-07-16 3:05 ` [RFC][PATCH - 8/12] NTP cleanup: Integrate second_overflow() logic john stultz
2005-07-16 3:06 ` [RFC][PATCH - 9/12] NTP cleanup: Improve NTP variable names john stultz
2005-07-16 3:06 ` [RFC][PATCH - 10/12] NTP cleanup: Use ntp_lock instead of xtime_lock john stultz
2005-07-16 3:07 ` [RFC][PATCH - 11/12] NTP cleanup: Introduce PPM adjustment variables john stultz
2005-07-16 3:09 ` [RFC][PATCH - 12/12] NTP cleanup: use ppm instead of unit adj returned by ntp_advance john stultz
2005-07-18 11:42 ` [RFC][PATCH - 1/12] NTP cleanup: Move NTP code into ntp.c Pavel Machek
2005-07-20 20:44 ` john stultz
2005-07-17 18:00 ` [RFC - 0/12] NTP cleanup work (v. B4) Roman Zippel
2005-07-20 16:26 ` john stultz
2005-07-21 10:39 ` Roman Zippel
2005-07-26 1:03 ` john stultz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1121482925.25236.42.camel@cog.beaverton.ibm.com \
--to=johnstul@us.ibm.com \
--cc=anton@samba.org \
--cc=benh@kernel.crashing.org \
--cc=frank@tuxrocks.com \
--cc=george@mvista.com \
--cc=linux-kernel@vger.kernel.org \
--cc=nacc@us.ibm.com \
--cc=zippel@linux-m68k.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®