You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "hujun260 (via GitHub)" <gi...@apache.org> on 2025/01/03 04:36:23 UTC

[PR] up_rtc_gettime: add spinlock to protect up_rtc_gettime [nuttx]

hujun260 opened a new pull request, #15413:
URL: https://github.com/apache/nuttx/pull/15413

   
   ## Summary
   up_rtc_gettime: add spinlock to protect up_rtc_gettime
   
   ## Impact
   up_rtc_gettime
   
   ## Testing
   ci
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] up_rtc_gettime: add spinlock to protect up_rtc_gettime [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 merged PR #15413:
URL: https://github.com/apache/nuttx/pull/15413


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] up_rtc_gettime: add spinlock to protect up_rtc_gettime [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #15413:
URL: https://github.com/apache/nuttx/pull/15413#discussion_r1903193518


##########
arch/x86_64/src/intel64/intel64_rtc.c:
##########
@@ -140,11 +145,14 @@ int up_rtc_initialize(void)
 
 int up_rtc_gettime(struct timespec *tp)
 {
+  irqstate_t flags;
   uint64_t tmp;
 
+  flags = spin_lock_irqsave(&g_rtc_lock);

Review Comment:
   move before line 94, or use atomic operation directly



##########
arch/arm/src/sam34/sam_rtc.c:
##########
@@ -759,11 +764,14 @@ int up_rtc_gettime(struct timespec *tp)
 {
   /* This is a hack to emulate a high resolution rtc using the rtt */
 
+  irqstate_t flags;
   uint32_t rtc_cal;
   uint32_t rtc_tim;
   uint32_t rtt_val;
   struct tm t;
 
+  flags = spin_lock_irqsave(&g_rtc_lock);
+  sched_lock();

Review Comment:
   remove sched_lock



##########
arch/arm/src/sam34/sam_rtc.c:
##########
@@ -794,6 +802,8 @@ int up_rtc_gettime(struct timespec *tp)
   tp->tv_nsec = (((rtt_val - g_rtt_offset) & (CONFIG_RTC_FREQUENCY - 1)) *
                    1000000000ull) / CONFIG_RTC_FREQUENCY;
 
+  spin_unlock_irqrestore(&g_rtc_lock, flags);

Review Comment:
   move before line 785



##########
arch/renesas/src/rx65n/rx65n_rtc.c:
##########
@@ -476,6 +480,9 @@ int up_rtc_gettime(struct timespec *tp)
   uint8_t regval;
   struct tm t;
 
+  flags = spin_lock_irqsave(&g_rtc_lock);
+  sched_lock();

Review Comment:
   don't need sched_lock



##########
arch/arm/src/max326xx/max32660/max32660_rtc.c:
##########
@@ -424,6 +427,7 @@ int up_rtc_gettime(struct timespec *tp)
 
   tp->tv_sec  = sec;
   tp->tv_nsec = (long)tmp;
+  spin_unlock_irqrestore(&g_rtc_lock, flags);

Review Comment:
   move before line 424



##########
arch/renesas/src/rx65n/rx65n_rtc.c:
##########
@@ -916,7 +926,8 @@ int rx65n_rtc_setalarm(struct alm_setalarm_s *alminfo)
 
   /* Is there already something waiting on the ALARM? */
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&g_rtc_lock);
+  sched_lock();

Review Comment:
   why need sched_lock



##########
arch/arm/src/cxd56xx/cxd56_rtc.c:
##########
@@ -410,6 +413,8 @@ int up_rtc_gettime(struct timespec *tp)
   tp->tv_nsec = (count % CONFIG_RTC_FREQUENCY) *
                 (NSEC_PER_SEC / CONFIG_RTC_FREQUENCY);
 
+  spin_unlock_irqrestore(&g_rtc_lock, flags);

Review Comment:
   move before line 410



##########
arch/x86_64/src/intel64/intel64_rtc.c:
##########
@@ -140,11 +145,14 @@ int up_rtc_initialize(void)
 
 int up_rtc_gettime(struct timespec *tp)
 {
+  irqstate_t flags;
   uint64_t tmp;
 
+  flags = spin_lock_irqsave(&g_rtc_lock);
   tmp = rtc_read();
   tp->tv_sec  = (tmp / NS_PER_SEC);
   tp->tv_nsec = (tmp - (tp->tv_sec) * NS_PER_SEC);
+  spin_unlock_irqrestore(&g_rtc_lock, flags);

Review Comment:
   move after line 99



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] up_rtc_gettime: add spinlock to protect up_rtc_gettime [nuttx]

Posted by "hujun260 (via GitHub)" <gi...@apache.org>.
hujun260 commented on code in PR #15413:
URL: https://github.com/apache/nuttx/pull/15413#discussion_r1903402339


##########
arch/renesas/src/rx65n/rx65n_rtc.c:
##########
@@ -476,6 +480,9 @@ int up_rtc_gettime(struct timespec *tp)
   uint8_t regval;
   struct tm t;
 
+  flags = spin_lock_irqsave(&g_rtc_lock);
+  sched_lock();

Review Comment:
   up_rtc_gettime will call timegm 
   and  timegm may print log
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] up_rtc_gettime: add spinlock to protect up_rtc_gettime [nuttx]

Posted by "lupyuen (via GitHub)" <gi...@apache.org>.
lupyuen commented on PR #15413:
URL: https://github.com/apache/nuttx/pull/15413#issuecomment-2574261855

   Sorry @hujun260 could you check if the PR above is causing these build errors in `sim/segger`? Thanks :-)
   https://github.com/NuttX/nuttx/actions/runs/12637024742/job/35210493873#step:7:287
   ```text
   Configuration/Tool: sim/segger
   In file included from segger/SystemView/SEGGER/SEGGER_SYSVIEW.h:65,
                    from segger/SystemView/SEGGER/SEGGER_SYSVIEW_Int.h:64,
                    from segger/SystemView/SEGGER/SEGGER_SYSVIEW.c:146:
   segger/SystemView/SEGGER/SEGGER_SYSVIEW.c: In function '_VPrintHost':
   Error: segger/SystemView/SEGGER/SEGGER_SYSVIEW_ConfDefaults.h:557:51: error: implicit declaration of function 'SEGGER_RTT_LOCK'; did you mean 'SEGGER_RTT_Read'? [-Werror=implicit-function-declaration]
     557 |   #define SEGGER_SYSVIEW_LOCK()                   SEGGER_RTT_LOCK()
         |                                                   ^~~~~~~~~~~~~~~
   segger/SystemView/SEGGER/SEGGER_SYSVIEW.c:387:35: note: in expansion of macro 'SEGGER_SYSVIEW_LOCK'
     387 | #define RECORD_START(PacketSize)  SEGGER_SYSVIEW_LOCK();                            \
         |                                   ^~~~~~~~~~~~~~~~~~~
   segger/SystemView/SEGGER/SEGGER_SYSVIEW.c:1005:5: note: in expansion of macro 'RECORD_START'
    1005 |     RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + SEGGER_SYSVIEW_MAX_STRING_LEN + 2 * SEGGER_SYSVIEW_QUANTA_U32 + SEGGER_SYSVIEW_MAX_ARGUMENTS * SEGGER_SYSVIEW_QUANTA_U32);
         |     ^~~~~~~~~~~~
   Error: segger/SystemView/SEGGER/SEGGER_SYSVIEW_ConfDefaults.h:570:51: error: implicit declaration of function 'SEGGER_RTT_UNLOCK'; did you mean 'SEGGER_RTT_Init'? [-Werror=implicit-function-declaration]
     570 |   #define SEGGER_SYSVIEW_UNLOCK()                 SEGGER_RTT_UNLOCK()
         |                                                   ^~~~~~~~~~~~~~~~~
   segger/SystemView/SEGGER/SEGGER_SYSVIEW.c:390:35: note: in expansion of macro 'SEGGER_SYSVIEW_UNLOCK'
     390 | #define RECORD_END()              SEGGER_SYSVIEW_UNLOCK()
         |                                   ^~~~~~~~~~~~~~~~~~~~~
   segger/SystemView/SEGGER/SEGGER_SYSVIEW.c:1015:5: note: in expansion of macro 'RECORD_END'
    1015 |     RECORD_END();
         |     ^~~~~~~~~~
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] up_rtc_gettime: add spinlock to protect up_rtc_gettime [nuttx]

Posted by "nuttxpr (via GitHub)" <gi...@apache.org>.
nuttxpr commented on PR #15413:
URL: https://github.com/apache/nuttx/pull/15413#issuecomment-2568686816

   [**\[Experimental Bot, please feedback here\]**](https://github.com/search?q=repo%3Aapache%2Fnuttx+13552&type=issues)
   
   
   
   No, this PR does not fully meet the NuttX requirements. While it provides a summary of the change, it lacks crucial details. Here's a breakdown:
   
   **Missing Information:**
   
   * **Summary:**  While the title mentions adding a spinlock, the *why* is missing.  What race condition or problem does this fix? Which part of the code is changed (the specific function is mentioned, but a more general area like "RTC driver" would be helpful)? *How* does the spinlock solve the problem?  Issue numbers are missing.
   * **Impact:**  The impact section is severely lacking.  It just repeats the function name.  All the YES/NO questions need to be answered with explanations. Does this change affect any particular architectures, boards, or drivers?  Does it change the API?  Does it require documentation updates?  Are there security implications of adding this lock?
   * **Testing:** "ci" is insufficient.  What specific targets were tested? What tests were run?  Actual log output (or a link to it) demonstrating the problem before the change and the successful operation after the change is needed.  Host build environment information is also missing.
   
   **Example of how to improve the Summary:**
   
   "The `up_rtc_gettime` function was prone to race conditions when accessed concurrently from multiple threads, potentially leading to incorrect time readings.  This change adds a spinlock to protect access to the RTC hardware during the `up_rtc_gettime` operation within the common RTC driver code. This fixes [Issue #XYZ]. This change also affects the `up_rtc_settime` function, which now also acquires the same spinlock.
   
   **Example of how to improve the Impact:**
   
   * Is new feature added? NO
   * Is existing feature changed? YES, the `up_rtc_gettime` and `up_rtc_settime` functions are now thread-safe.
   * Impact on user (will user need to adapt to change)? NO
   * Impact on build (will build process change)? NO
   * Impact on hardware (will arch(s) / board(s) / driver(s) change)?  NO. This change is within the common RTC driver code, so all architectures using this driver benefit.
   * Impact on documentation (is update required / provided)?  YES, the documentation for `up_rtc_gettime` and `up_rtc_settime` should mention the thread-safety improvements.  A documentation update is included in this PR.
   * Impact on security (any sort of implications)? NO
   * Impact on compatibility (backward/forward/interoperability)? NO
   * Anything else to consider? The addition of the spinlock introduces a small performance overhead.
   
   
   **Example of how to improve the Testing:**
   
   * Build Host(s): Linux (Ubuntu 20.04), x86_64, GCC 9.4.0
   * Target(s): sim:qemu-arm, stm32f4discovery:nsh
   
   Testing logs before change:  (demonstrate the race condition - e.g., inconsistent time readings from two threads accessing the RTC simultaneously)
   
   ```
   ... log output showing the problem ...
   ```
   
   Testing logs after change: (show consistent time readings and no errors)
   
   ```
   ... log output showing the fix ...
   ```
     Or:
   Testing logs are available in the CI run for this PR: [Link to CI run]
   
   
   By providing this level of detail, reviewers can quickly understand the change, its impact, and verify its correctness, leading to a faster and smoother review process.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] up_rtc_gettime: add spinlock to protect up_rtc_gettime [nuttx]

Posted by "hujun260 (via GitHub)" <gi...@apache.org>.
hujun260 commented on code in PR #15413:
URL: https://github.com/apache/nuttx/pull/15413#discussion_r1903399825


##########
arch/renesas/src/rx65n/rx65n_rtc.c:
##########
@@ -916,7 +926,8 @@ int rx65n_rtc_setalarm(struct alm_setalarm_s *alminfo)
 
   /* Is there already something waiting on the ALARM? */
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&g_rtc_lock);
+  sched_lock();

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] up_rtc_gettime: add spinlock to protect up_rtc_gettime [nuttx]

Posted by "hujun260 (via GitHub)" <gi...@apache.org>.
hujun260 commented on PR #15413:
URL: https://github.com/apache/nuttx/pull/15413#issuecomment-2574336604

   > Sorry @hujun260 could you check if the PR above is causing these build errors in `sim:segger`? Thanks :-) https://github.com/NuttX/nuttx/actions/runs/12637024742/job/35210493873#step:7:287
   > 
   > ```
   > Configuration/Tool: sim/segger
   > In file included from segger/SystemView/SEGGER/SEGGER_SYSVIEW.h:65,
   >                  from segger/SystemView/SEGGER/SEGGER_SYSVIEW_Int.h:64,
   >                  from segger/SystemView/SEGGER/SEGGER_SYSVIEW.c:146:
   > segger/SystemView/SEGGER/SEGGER_SYSVIEW.c: In function '_VPrintHost':
   > Error: segger/SystemView/SEGGER/SEGGER_SYSVIEW_ConfDefaults.h:557:51: error: implicit declaration of function 'SEGGER_RTT_LOCK'; did you mean 'SEGGER_RTT_Read'? [-Werror=implicit-function-declaration]
   >   557 |   #define SEGGER_SYSVIEW_LOCK()                   SEGGER_RTT_LOCK()
   >       |                                                   ^~~~~~~~~~~~~~~
   > segger/SystemView/SEGGER/SEGGER_SYSVIEW.c:387:35: note: in expansion of macro 'SEGGER_SYSVIEW_LOCK'
   >   387 | #define RECORD_START(PacketSize)  SEGGER_SYSVIEW_LOCK();                            \
   >       |                                   ^~~~~~~~~~~~~~~~~~~
   > segger/SystemView/SEGGER/SEGGER_SYSVIEW.c:1005:5: note: in expansion of macro 'RECORD_START'
   >  1005 |     RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + SEGGER_SYSVIEW_MAX_STRING_LEN + 2 * SEGGER_SYSVIEW_QUANTA_U32 + SEGGER_SYSVIEW_MAX_ARGUMENTS * SEGGER_SYSVIEW_QUANTA_U32);
   >       |     ^~~~~~~~~~~~
   > Error: segger/SystemView/SEGGER/SEGGER_SYSVIEW_ConfDefaults.h:570:51: error: implicit declaration of function 'SEGGER_RTT_UNLOCK'; did you mean 'SEGGER_RTT_Init'? [-Werror=implicit-function-declaration]
   >   570 |   #define SEGGER_SYSVIEW_UNLOCK()                 SEGGER_RTT_UNLOCK()
   >       |                                                   ^~~~~~~~~~~~~~~~~
   > segger/SystemView/SEGGER/SEGGER_SYSVIEW.c:390:35: note: in expansion of macro 'SEGGER_SYSVIEW_UNLOCK'
   >   390 | #define RECORD_END()              SEGGER_SYSVIEW_UNLOCK()
   >       |                                   ^~~~~~~~~~~~~~~~~~~~~
   > segger/SystemView/SEGGER/SEGGER_SYSVIEW.c:1015:5: note: in expansion of macro 'RECORD_END'
   >  1015 |     RECORD_END();
   >       |     ^~~~~~~~~~
   > ```
   > 
   > Also for `at32f437-mini:systemview` https://github.com/NuttX/nuttx/actions/runs/12637024742/job/35210486919#step:7:128
   > 
   > ```
   > Configuration/Tool: at32f437-mini/systemview,CONFIG_ARM_TOOLCHAIN_GNU_EABI
   > In file included from segger/SystemView/SEGGER/SEGGER_SYSVIEW.h:65,
   >                  from segger/SystemView/SEGGER/SEGGER_SYSVIEW_Int.h:64,
   >                  from segger/SystemView/SEGGER/SEGGER_SYSVIEW.c:146:
   > segger/SystemView/SEGGER/SEGGER_SYSVIEW.c: In function '_VPrintHost':
   > Error: segger/SystemView/SEGGER/SEGGER_SYSVIEW_ConfDefaults.h:557:51: error: implicit declaration of function 'SEGGER_RTT_LOCK'; did you mean 'SEGGER_RTT_Read'? [-Werror=implicit-function-declaration]
   >   557 |   #define SEGGER_SYSVIEW_LOCK()                   SEGGER_RTT_LOCK()
   >       |                                                   ^~~~~~~~~~~~~~~
   > ```
   
   ok


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org