<p>tsaitgaist has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/c/simtrace2/+/16097">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">free USB buffer when allocation failed<br><br>when the reader sends APDU headers (e.g. after multiple reset),<br>messages are queued for USB transmission.<br>but if no host software is connected to SIMtrace in card emulation<br>mode, the USB message queue is not emptied, leading to the memory<br>getting full and preventing allocation for newer messages (e.g.<br>more recent APDU).<br>in this case the oldest queued message is now dropped to free some<br>memory.<br><br>Change-Id: Ie9ebdd2ff966f67c9afd1ed760f106558f0091ad<br>---<br>M firmware/libcommon/source/card_emu.c<br>1 file changed, 27 insertions(+), 4 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/simtrace2 refs/changes/97/16097/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/firmware/libcommon/source/card_emu.c b/firmware/libcommon/source/card_emu.c</span><br><span>index 5298ffc..497b963 100644</span><br><span>--- a/firmware/libcommon/source/card_emu.c</span><br><span>+++ b/firmware/libcommon/source/card_emu.c</span><br><span>@@ -254,12 +254,35 @@</span><br><span> /* Allocate USB buffer and push + initialize simtrace_msg_hdr */</span><br><span> struct msgb *usb_buf_alloc_st(uint8_t ep, uint8_t msg_class, uint8_t msg_type)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-    struct msgb *msg;</span><br><span style="color: hsl(120, 100%, 40%);">+     struct msgb *msg = NULL;</span><br><span>     struct simtrace_msg_hdr *sh;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-        msg = usb_buf_alloc(ep);</span><br><span style="color: hsl(0, 100%, 40%);">-        if (!msg)</span><br><span style="color: hsl(0, 100%, 40%);">-               return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  while (!msg) {</span><br><span style="color: hsl(120, 100%, 40%);">+                msg = usb_buf_alloc(ep); // try to allocate some memory</span><br><span style="color: hsl(120, 100%, 40%);">+               if (!msg) { // allocation failed, we might be out of memory</span><br><span style="color: hsl(120, 100%, 40%);">+                   struct llist_head *queue = usb_get_queue(ep);</span><br><span style="color: hsl(120, 100%, 40%);">+                 if (!queue) {</span><br><span style="color: hsl(120, 100%, 40%);">+                         TRACE_ERROR("ep %u: %s queue does not exist\n\r",</span><br><span style="color: hsl(120, 100%, 40%);">+                                       ep, __func__);</span><br><span style="color: hsl(120, 100%, 40%);">+                            return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+                  }</span><br><span style="color: hsl(120, 100%, 40%);">+                     if (llist_empty(queue)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                             TRACE_ERROR("ep %u: %s EOMEM (queue already empty)\n\r",</span><br><span style="color: hsl(120, 100%, 40%);">+                                        ep, __func__);</span><br><span style="color: hsl(120, 100%, 40%);">+                            return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+                  }</span><br><span style="color: hsl(120, 100%, 40%);">+                     msg = msgb_dequeue(queue);</span><br><span style="color: hsl(120, 100%, 40%);">+                    if (!msg) {</span><br><span style="color: hsl(120, 100%, 40%);">+                           TRACE_ERROR("ep %u: %s no msg in non-empty queue\n\r",</span><br><span style="color: hsl(120, 100%, 40%);">+                                          ep, __func__);</span><br><span style="color: hsl(120, 100%, 40%);">+                            return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+                  }</span><br><span style="color: hsl(120, 100%, 40%);">+                     usb_buf_free(msg);</span><br><span style="color: hsl(120, 100%, 40%);">+                    msg = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+                   TRACE_DEBUG("ep %u: %s queue msg dropped\n\r",</span><br><span style="color: hsl(120, 100%, 40%);">+                                  ep, __func__);</span><br><span style="color: hsl(120, 100%, 40%);">+            }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span> </span><br><span>        msg->l1h = msgb_put(msg, sizeof(*sh));</span><br><span>    sh = (struct simtrace_msg_hdr *) msg->l1h;</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/simtrace2/+/16097">change 16097</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/simtrace2/+/16097"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: simtrace2 </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: Ie9ebdd2ff966f67c9afd1ed760f106558f0091ad </div>
<div style="display:none"> Gerrit-Change-Number: 16097 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: tsaitgaist <kredon@sysmocom.de> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>