<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>nrf_log_frontend_dequeue must be atomically protected against re-entry from interrupt context</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/39188/nrf_log_frontend_dequeue-must-be-atomically-protected-against-re-entry-from-interrupt-context</link><description>This issue exists in NRF SDK5 15.2.0 and earlier. NRF log module has entered &amp;quot;production&amp;quot; level as of SDK5 15.2. 
 
 While implementing an additional logging backend, I discovered a race condition in the nrf_log_frontend_dequeue function. 
 
 The bug</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 20 Feb 2020 16:41:16 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/39188/nrf_log_frontend_dequeue-must-be-atomically-protected-against-re-entry-from-interrupt-context" /><item><title>RE: nrf_log_frontend_dequeue must be atomically protected against re-entry from interrupt context</title><link>https://devzone.nordicsemi.com/thread/235557?ContentTypeID=1</link><pubDate>Thu, 20 Feb 2020 16:41:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b4888ffc-4bd7-4b7d-8871-5826eae64ddd</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;Ah, quite so - thanks!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_log_frontend_dequeue must be atomically protected against re-entry from interrupt context</title><link>https://devzone.nordicsemi.com/thread/235554?ContentTypeID=1</link><pubDate>Thu, 20 Feb 2020 16:33:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b8e24f59-500c-41b2-ba57-53d408d14e3d</guid><dc:creator>Anthony Ambuehl</dc:creator><description>&lt;p&gt;the return type for&amp;nbsp;nrf_log_frontend_dequeue is bool so in your busy check should look like this:&lt;/p&gt;
&lt;p&gt;if (nrf_atomic_flag_set_fetch(&amp;amp;m_log_data.log_is_busy)==1)&lt;br /&gt; {&lt;br /&gt; return false;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;also you should use the atomic library to clear the variable:&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; UNUSED_RETURN_VALUE(nrf_atomic_flag_clear(&amp;amp;m_log_data.log_is_busy));&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_log_frontend_dequeue must be atomically protected against re-entry from interrupt context</title><link>https://devzone.nordicsemi.com/thread/235551?ContentTypeID=1</link><pubDate>Thu, 20 Feb 2020 16:28:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:aadd3193-1f36-460f-9c0b-1d42ec5c1e21</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;Good find.&amp;nbsp; I have the same issue, and following you suggestion have implemented the following, which if you have time to review I would be most grateful.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;typedef struct
{
    uint32_t                  wr_idx;          // Current write index (never reset)
    uint32_t                  rd_idx;          // Current read index  (never_reset)
    uint32_t                  mask;            // Size of buffer (must be power of 2) presented as mask
    uint32_t                  buffer[NRF_LOG_BUF_WORDS];
    nrf_log_timestamp_func_t  timestamp_func;  // A pointer to function that returns timestamp
    nrf_log_backend_t const * p_backend_head;
    nrf_atomic_flag_t         log_skipping;
    nrf_atomic_flag_t         log_skipped;
    nrf_atomic_flag_t         log_is_busy;     // This flag replaces the blocking use of critical region
    nrf_atomic_u32_t          log_dropped_cnt;
    bool                      autoflush;
} log_data_t;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;ret_code_t nrf_log_init(nrf_log_timestamp_func_t timestamp_func, uint32_t timestamp_freq)
{
    (void)NRF_LOG_ITEM_DATA_CONST(app);

    if (NRF_LOG_USES_TIMESTAMP &amp;amp;&amp;amp; (timestamp_func == NULL))
    {
        return NRF_ERROR_INVALID_PARAM;
    }

    m_log_data.mask         = NRF_LOG_BUF_WORDS - 1;
    m_log_data.wr_idx       = 0;
    m_log_data.rd_idx       = 0;
    m_log_data.log_skipped  = 0;
    m_log_data.log_skipping = 0;
    m_log_data.log_is_busy  = 0;
    m_log_data.autoflush    = NRF_LOG_DEFERRED ? false : true;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;bool nrf_log_frontend_dequeue(void)
{
    if (buffer_is_empty())
    {
        return false;
    }
    // Note also add atomic flag set before this __DSB() and after this function exits
    // See https://devzone.nordicsemi.com/f/nordic-q-a/39188/nrf_log_frontend_dequeue-must-be-atomically-protected-against-re-entry-from-interrupt-context
    if (nrf_atomic_flag_set_fetch(&amp;amp;m_log_data.log_is_busy))
    {
        return NRF_ERROR_BUSY;
    }
...
    // Skip any in progress packets.
    do {
        if (invalid_packets_omit(p_header, &amp;amp;rd_idx) &amp;amp;&amp;amp; (m_log_data.log_skipped == 0))
        {
            //Check if end of data is not reached.
            if (rd_idx &amp;gt;= m_log_data.wr_idx)
            {
                m_log_data.rd_idx     = m_log_data.wr_idx;
                // Ensure that next invocation occurs after this flag is cleared.
                m_log_data.log_is_busy = 0;
                return false;
            }
...
    // Ensure that next invocation occurs after this flag is cleared.
    m_log_data.log_is_busy = 0;
    return buffer_is_empty() ? false : true;
}
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_log_frontend_dequeue must be atomically protected against re-entry from interrupt context</title><link>https://devzone.nordicsemi.com/thread/231224?ContentTypeID=1</link><pubDate>Mon, 27 Jan 2020 15:30:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9f0b089b-562b-4bd4-8a1c-b1929eeefb72</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;I have updated the internal jira with your suggestions. Hopefully it the meantime others may find his case and your suggestion.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_log_frontend_dequeue must be atomically protected against re-entry from interrupt context</title><link>https://devzone.nordicsemi.com/thread/227770?ContentTypeID=1</link><pubDate>Mon, 06 Jan 2020 22:10:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6a631b04-2144-4777-bd29-22751ef344f2</guid><dc:creator>Anthony Ambuehl</dc:creator><description>&lt;p&gt;I noticed what appears as an attempt at fixing this issue in the nrf_log_frontend.c.&amp;nbsp; The change in recent SDK adds a CRITICAL_REGION around the NRF_LOG_FLUSH() called by autoflush.&amp;nbsp; This may address the issue however it will block interrupts for an extended time while waiting for logs to transfer.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I recommend not using this workaround.&amp;nbsp; Instead, I recommend adding an atomic flag set_fetch before the DSB call in&amp;nbsp;nrf_log_frontend_dequeue() and atomic flag clear before returning.&amp;nbsp; This change would allow ISR&amp;#39;s to continue normal operation.&lt;/p&gt;
&lt;p&gt;I have been using this fix for over a year and it has not shown any issues.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_log_frontend_dequeue must be atomically protected against re-entry from interrupt context</title><link>https://devzone.nordicsemi.com/thread/152403?ContentTypeID=1</link><pubDate>Wed, 10 Oct 2018 16:54:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cf7fc397-973f-4e1f-bd9f-99502085d385</guid><dc:creator>awneil</dc:creator><description>&lt;p&gt;Thanks.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_log_frontend_dequeue must be atomically protected against re-entry from interrupt context</title><link>https://devzone.nordicsemi.com/thread/152401?ContentTypeID=1</link><pubDate>Wed, 10 Oct 2018 16:42:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:625b999c-45c0-419c-9eb9-fe2cf4012d85</guid><dc:creator>Anthony Ambuehl</dc:creator><description>&lt;p&gt;The symptom is log messages that are sometimes repeated.&amp;nbsp; They may be a single log repeated or a series of logs that are repeated in a sequence.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;for example&lt;/p&gt;
&lt;p&gt;log 1&lt;/p&gt;
&lt;p&gt;log 2&lt;/p&gt;
&lt;p&gt;log 2&lt;/p&gt;
&lt;p&gt;log 3&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;p&gt;log 1&lt;/p&gt;
&lt;p&gt;log 2&lt;/p&gt;
&lt;p&gt;log 3&lt;/p&gt;
&lt;p&gt;log 1&lt;/p&gt;
&lt;p&gt;log 2&lt;/p&gt;
&lt;p&gt;log 3&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_log_frontend_dequeue must be atomically protected against re-entry from interrupt context</title><link>https://devzone.nordicsemi.com/thread/152020?ContentTypeID=1</link><pubDate>Mon, 08 Oct 2018 11:20:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:922335d3-978c-47bf-96f4-8928d41bb6d5</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;I&amp;#39;ll notify the SDK team. It might be some time before we get a reply.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_log_frontend_dequeue must be atomically protected against re-entry from interrupt context</title><link>https://devzone.nordicsemi.com/thread/151730?ContentTypeID=1</link><pubDate>Thu, 04 Oct 2018 20:01:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9e7485ed-179b-4758-9f83-97d4589a01ff</guid><dc:creator>awneil</dc:creator><description>&lt;p&gt;Hmmm - what would be the symptoms of this going wrong?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>