Skip to contentSkip to conversation

ADJ_OFFSET_SS_READ bug?

DISCUSSION Michael Kerrisk 4 participants 5 messages Jun 23, 2008 – Jul 21, 2008 Timekeeping, clocksource core, NTP, alarmtimer

lore
Share & tools
lore
#0 lore raw

Roman, John

John, thanks for ADJ_OFFSET_SS_READ, which fixed my bug report
(http://sourceware.org/bugzilla/show_bug?id=2449,
http://bugzilla.kernel.org/show_bug.cgi?id=6761)

Roman, thanks for fixing John's fix ;-)

However, I'm wondering if there is a potential bug in the implementation of this flag. Note the following definitions from include/linux/timex.h:

#define ADJ_OFFSET 0x0001 /* time offset */
[...]

#define ADJ_OFFSET_SINGLESHOT   0x8001  /* old-fashioned adjtime */
#define ADJ_OFFSET_SS_READ      0xa001  /* read-only adjtime */

Using the the above value for ADJ_OFFSET_SS_READ, where the bits match those in ADJ_OFFSET and ADJ_OFFSET_SINGLESHOT, seems unnecessary as far as I can see. Why was that done?

More to the point, it looks like it creates a bug, since the "read-only adjtime" triggers the code path for ADJ_OFFSET:

        if (txc->modes) {
                ...
                if (txc->modes & ADJ_OFFSET) {
                        if (txc->modes == ADJ_OFFSET_SINGLESHOT)
                                /* adjtime() is independent from ntp_adjtime() */
                                time_adjust = txc->offset;
                        else
                                ntp_update_offset(txc->offset); /*XXX*/
                }
                if (txc->modes & ADJ_TICK)
                        tick_usec = txc->tick;

                if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
                        ntp_update_frequency(); /*XXX*/
        }

Unless I misunderstood something, ADJ_OFFSET_SS_READ causes the code marked XXX to be executed, but I don't think that is what is desired. Is that true?

Cheers,

Michael

#1 lore raw
Quoted 14 lines · Michael Kerrisk

Roman, John

John, thanks for ADJ_OFFSET_SS_READ, which fixed my bug report
(http://sourceware.org/bugzilla/show_bug?id=2449,
http://bugzilla.kernel.org/show_bug.cgi?id=6761)

Roman, thanks for fixing John's fix ;-)

However, I'm wondering if there is a potential bug in the implementation of this flag. Note the following definitions from include/linux/timex.h:

#define ADJ_OFFSET 0x0001 /* time offset */
[...]

Quoted 7 lines
#define ADJ_OFFSET_SINGLESHOT   0x8001  /* old-fashioned adjtime */
#define ADJ_OFFSET_SS_READ      0xa001  /* read-only adjtime */

Using the the above value for ADJ_OFFSET_SS_READ, where the bits match those in ADJ_OFFSET and ADJ_OFFSET_SINGLESHOT, seems unnecessary as far as I can see. Why was that done?

Hrm. My original fix was to use 0x2000, but from the commit Ingo changed it at Ulrich's suggestion. Had something to do with old glibc's doing the right thing?

Quoted 19 lines

More to the point, it looks like it creates a bug, since the "read-only adjtime" triggers the code path for ADJ_OFFSET:

         if (txc->modes) {
                 ...
                 if (txc->modes & ADJ_OFFSET) {
                         if (txc->modes == ADJ_OFFSET_SINGLESHOT)
                                 /* adjtime() is independent from ntp_adjtime() */
                                 time_adjust = txc->offset;
                         else
                                 ntp_update_offset(txc->offset); /*XXX*/
                 }
                 if (txc->modes & ADJ_TICK)
                         tick_usec = txc->tick;

                 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
                         ntp_update_frequency(); /*XXX*/
         }
Quoted 2 lines

Unless I misunderstood something, ADJ_OFFSET_SS_READ causes the code marked XXX to be executed, but I don't think that is what is desired. Is that true?

Yea. That does look like an issue. Thanks for the close inspection and review!

Sort of a quick off the cuff patch, but does the following look like the right fix to you?

Roman: your thoughts?

kernel/time/ntp.c +3 -2
index 5125ddd..7842a8d 100644
@@ -379,13 +379,14 @@ int do_adjtimex(struct timex *txc)
 			if (txc->modes == ADJ_OFFSET_SINGLESHOT)
 				/* adjtime() is independent from ntp_adjtime() */
 				time_adjust = txc->offset;
-			else
+			else if (txc->modes != ADJ_OFFSET_SS_READ)
 				ntp_update_offset(txc->offset);
 		}
 		if (txc->modes & ADJ_TICK)
 			tick_usec = txc->tick;
 
-		if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
+		if ((txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET)) &&
+				(txc->modes != ADJ_OFFSET_SS_READ))
 			ntp_update_frequency();
 	}
 
#2 lore raw
Quoted 47 lines · john stultz
Quoted 14 lines · Michael Kerrisk

Roman, John

John, thanks for ADJ_OFFSET_SS_READ, which fixed my bug report
(http://sourceware.org/bugzilla/show_bug?id=2449,
http://bugzilla.kernel.org/show_bug.cgi?id=6761)

Roman, thanks for fixing John's fix ;-)

However, I'm wondering if there is a potential bug in the implementation of this flag. Note the following definitions from include/linux/timex.h:

#define ADJ_OFFSET 0x0001 /* time offset */
[...]

Quoted 7 lines
#define ADJ_OFFSET_SINGLESHOT   0x8001  /* old-fashioned adjtime */
#define ADJ_OFFSET_SS_READ      0xa001  /* read-only adjtime */

Using the the above value for ADJ_OFFSET_SS_READ, where the bits match those in ADJ_OFFSET and ADJ_OFFSET_SINGLESHOT, seems unnecessary as far as I can see. Why was that done?

Hrm. My original fix was to use 0x2000, but from the commit Ingo changed it at Ulrich's suggestion. Had something to do with old glibc's doing the right thing?

Quoted 19 lines

More to the point, it looks like it creates a bug, since the "read-only adjtime" triggers the code path for ADJ_OFFSET:

         if (txc->modes) {
                 ...
                 if (txc->modes & ADJ_OFFSET) {
                         if (txc->modes == ADJ_OFFSET_SINGLESHOT)
                                 /* adjtime() is independent from ntp_adjtime() */
                                 time_adjust = txc->offset;
                         else
                                 ntp_update_offset(txc->offset); /*XXX*/
                 }
                 if (txc->modes & ADJ_TICK)
                         tick_usec = txc->tick;

                 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
                         ntp_update_frequency(); /*XXX*/
         }
Quoted 5 lines
Quoted 2 lines

Unless I misunderstood something, ADJ_OFFSET_SS_READ causes the code marked XXX to be executed, but I don't think that is what is desired. Is that true?

Yea. That does look like an issue. Thanks for the close inspection and review!

You're welcome -- thanks for getting back to me (I was beginning to wonder if my mail got dropped somewhere)/

Quoted 2 lines

Sort of a quick off the cuff patch, but does the following look like the right fix to you?

I haven't tested this, but given your statement about maintaining old glibc behavior, this looks like the riht fix, so:

Quoted 29 lines

Roman: your thoughts?

kernel/time/ntp.c +3 -2
index 5125ddd..7842a8d 100644
@@ -379,13 +379,14 @@ int do_adjtimex(struct timex *txc)
                       if (txc->modes == ADJ_OFFSET_SINGLESHOT)
                               /* adjtime() is independent from ntp_adjtime() */
                               time_adjust = txc->offset;
-                       else
+                       else if (txc->modes != ADJ_OFFSET_SS_READ)
                               ntp_update_offset(txc->offset);
               }
               if (txc->modes & ADJ_TICK)
                       tick_usec = txc->tick;
 
-               if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
+               if ((txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET)) &&
+                               (txc->modes != ADJ_OFFSET_SS_READ))
                       ntp_update_frequency();
       }
 
Signature · 4 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
man-pages online: http://www.kernel.org/doc/man-pages/online_pages.html
Found a bug? http://www.kernel.org/doc/man-pages/reporting_bugs.html
#3 lore raw
Quoted 57 lines · Michael Kerrisk
Quoted 47 lines · john stultz
Quoted 14 lines · Michael Kerrisk

Roman, John

John, thanks for ADJ_OFFSET_SS_READ, which fixed my bug report
(http://sourceware.org/bugzilla/show_bug?id=2449,
http://bugzilla.kernel.org/show_bug.cgi?id=6761)

Roman, thanks for fixing John's fix ;-)

However, I'm wondering if there is a potential bug in the implementation of this flag. Note the following definitions from include/linux/timex.h:

#define ADJ_OFFSET 0x0001 /* time offset */
[...]

Quoted 7 lines
#define ADJ_OFFSET_SINGLESHOT   0x8001  /* old-fashioned adjtime */
#define ADJ_OFFSET_SS_READ      0xa001  /* read-only adjtime */

Using the the above value for ADJ_OFFSET_SS_READ, where the bits match those in ADJ_OFFSET and ADJ_OFFSET_SINGLESHOT, seems unnecessary as far as I can see. Why was that done?

Hrm. My original fix was to use 0x2000, but from the commit Ingo changed it at Ulrich's suggestion. Had something to do with old glibc's doing the right thing?

Quoted 19 lines

More to the point, it looks like it creates a bug, since the "read-only adjtime" triggers the code path for ADJ_OFFSET:

         if (txc->modes) {
                 ...
                 if (txc->modes & ADJ_OFFSET) {
                         if (txc->modes == ADJ_OFFSET_SINGLESHOT)
                                 /* adjtime() is independent from ntp_adjtime() */
                                 time_adjust = txc->offset;
                         else
                                 ntp_update_offset(txc->offset); /*XXX*/
                 }
                 if (txc->modes & ADJ_TICK)
                         tick_usec = txc->tick;

                 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
                         ntp_update_frequency(); /*XXX*/
         }
Quoted 5 lines
Quoted 2 lines

Unless I misunderstood something, ADJ_OFFSET_SS_READ causes the code marked XXX to be executed, but I don't think that is what is desired. Is that true?

Yea. That does look like an issue. Thanks for the close inspection and review!

You're welcome -- thanks for getting back to me (I was beginning to wonder if my mail got dropped somewhere)/

Quoted 7 lines
Quoted 2 lines

Sort of a quick off the cuff patch, but does the following look like the right fix to you?

I haven't tested this, but given your statement about maintaining old glibc behavior, this looks like the riht fix, so:

John,

Are you pushing this into 2.6.27-rc1?

Cheers,

Michael

Quoted 38 lines
Quoted 19 lines

Roman: your thoughts?

kernel/time/ntp.c +1 -1
index 5125ddd..7842a8d 100644
@@ -379,13 +379,14 @@ int do_adjtimex(struct timex *txc)
                       if (txc->modes == ADJ_OFFSET_SINGLESHOT)
                               /* adjtime() is independent from ntp_adjtime() */
                               time_adjust = txc->offset;
-                       else
+                       else if (txc->modes != ADJ_OFFSET_SS_READ)
                               ntp_update_offset(txc->offset);

kernel/time/ntp.c int do_adjtimex(struct timex *txc) · line 392

Quoted 10 lines
kernel/time/ntp.c +2 -1
               }
               if (txc->modes & ADJ_TICK)
                       tick_usec = txc->tick;
 
-               if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
+               if ((txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET)) &&
+                               (txc->modes != ADJ_OFFSET_SS_READ))
                       ntp_update_frequency();
       }
 

--
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
man-pages online: http://www.kernel.org/doc/man-pages/online_pages.html
Found a bug? http://www.kernel.org/doc/man-pages/reporting_bugs.html

Signature · 4 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
man-pages online: http://www.kernel.org/doc/man-pages/online_pages.html
Found a bug? http://www.kernel.org/doc/man-pages/reporting_bugs.html
#4 lore raw

Hi,

Quoted 2 lines · Michael Kerrisk
Quoted 2 lines
Quoted 2 lines

Sort of a quick off the cuff patch, but does the following look like the right fix to you?

I would more prefer a patch like below, instead of adding tests for this all over the place this separates the code more clearly, as this syscall is seriously overloaded.

bye, Roman

[PATCH] fix ADJ_OFFSET_SS_READ bug and do_adjtimex cleanup

Thanks to the review by Michael Kerrisk <[email protected]> a bug in the recent ADJ_OFFSET_SS_READ option was discovered, where the ntp time_offset was inadvertently set by it. This fixes this by making the adjtime code more separate from the ntp_adjtime code (both of which really want to be separate syscalls).

---
 include/linux/timex.h |    9 +++++
 kernel/time/ntp.c     |   76 ++++++++++++++++++++++++++------------------------
 2 files changed, 48 insertions(+), 37 deletions(-)
include/linux/timex.h +8 -1
Index: linux-2.6/include/linux/timex.h
===================================================================
@@ -141,8 +141,15 @@ struct timex {
 #define ADJ_MICRO		0x1000	/* select microsecond resolution */
 #define ADJ_NANO		0x2000	/* select nanosecond resolution */
 #define ADJ_TICK		0x4000	/* tick value */
+
+#ifdef __KERNEL__
+#define ADJ_ADJTIME		0x8000	/* switch between adjtime/adjtimex modes */
+#define ADJ_OFFSET_SINGLESHOT	0x0001	/* old-fashioned adjtime */
+#define ADJ_OFFSET_READONLY	0x2000	/* read-only adjtime */
+#else
 #define ADJ_OFFSET_SINGLESHOT	0x8001	/* old-fashioned adjtime */
-#define ADJ_OFFSET_SS_READ	0xa001  /* read-only adjtime */
+#define ADJ_OFFSET_SS_READ	0xa001	/* read-only adjtime */
+#endif
 
 /* xntp 3.4 compatibility names */
 #define MOD_OFFSET	ADJ_OFFSET
kernel/time/ntp.c +40 -36

110 lines not on this page · raw message