Root/m1/patches/rtems/coremsgsubmit-race.patch

Source at commit f6fd776f528905e346c7f1caec97945535c7ca43 created 8 years 4 months ago.
By Werner Almesberger, m1/patches/rtems/: Milkymist-specific patches are in upstream (update by Xiangfu)
1This issue is under review:
2https://www.rtems.org/bugzilla/show_bug.cgi?id=1961
3
4If it's permissible to call rtems_message_queue_send from an
5interrupt, then there is at least one race condition in the core
6message subsystem.
7
8This created the MIDI/mouse hang we love so much on M1.
9
10The problem is as follows: RTEMS queues use pre-allocated message
11buffers that are kept on an "inactive" (free) list. When enqueuing
12a message, a buffer is first removed from the inactive list, data
13it copied to it, and it is then added to the pending list.
14
15The reverse happens when dequeuing. Besides these two queues, there
16is also a counter called number_of_pending_messages keeping track,
17as the name suggests, of the number of pending messages. It is
18updated atomically together with changes to the pending buffers
19list.
20
21From the above it is clear that the counter will be out of sync with
22the inactive list between the beginning and the end of an enqueue or
23dequeue operation.
24
25In order to minimize interrupt latency, RTEMS disables interrupts
26only when adding and removing buffers from lists, but not throughout
27the whole enqueuing/dequeuing operation. Instead, it disables the
28scheduler during these operations, but this doesn't prevent
29interrupts.
30
31This means that the inconsistency between number_of_pending_messages
32and the inactive list can be observed from an interrupt handler if
33enqueuing or dequeuing is in progress.
34
35_CORE_message_queue_Submit checks whether there is still room in the
36queue by reading number_of_pending_messages. If there is room, it
37then calls _CORE_message_queue_Allocate_message_buffer to obtain a
38free buffer.
39
40Given that number_of_pending_messages and the list of inactive
41buffers can disagree, e.g., if _CORE_message_queue_Seize or another
42_CORE_message_queue_Submit is executing concurrently,
43_CORE_message_queue_Allocate_message_buffer may fail to obtain a
44free buffer despite the prior test.
45
46_CORE_message_queue_Allocate_message_buffer can detect a lack of
47free buffers and indicates it by returning a NULL pointer. Checking
48whether NULL has been returned instead of a buffer is optional and
49depends on RTEMS_DEBUG.
50
51If no check is performed, _CORE_message_queue_Submit will then try
52to use the buffer. In the absence of hardware detecting the
53de-referencing of NULL pointers, the wounded system will limp on a
54little further until, at least in the case of M1, it finally hangs
55somewhere.
56
57The patch below avoids the problem in the scenario described above
58by not using number_of_pending_messages as an indicator of whether
59free buffers are available, but by simply trying to get a buffer,
60and handling the result of failure.
61
62This is similar to how _CORE_message_queue_Seize works.
63
64Another possibility would be to make testing of the_message no
65longer optional. But then, there would basically be two tests for
66the same condition, which is ugly.
67
68- Werner
69
70Index: rtems/cpukit/score/src/coremsgsubmit.c
71===================================================================
72--- rtems.orig/cpukit/score/src/coremsgsubmit.c 2011-11-12 09:15:12.000000000 -0300
73+++ rtems/cpukit/score/src/coremsgsubmit.c 2011-11-12 09:15:17.000000000 -0300
74@@ -101,21 +101,9 @@
75    * No one waiting on the message queue at this time, so attempt to
76    * queue the message up for a future receive.
77    */
78- if ( the_message_queue->number_of_pending_messages <
79- the_message_queue->maximum_pending_messages ) {
80-
81- the_message =
82- _CORE_message_queue_Allocate_message_buffer( the_message_queue );
83-
84- #if defined(RTEMS_DEBUG)
85- /*
86- * NOTE: If the system is consistent, this error should never occur.
87- */
88-
89- if ( !the_message )
90- return CORE_MESSAGE_QUEUE_STATUS_UNSATISFIED;
91- #endif
92-
93+ the_message =
94+ _CORE_message_queue_Allocate_message_buffer( the_message_queue );
95+ if ( the_message ) {
96     _CORE_message_queue_Copy_buffer(
97       buffer,
98       the_message->Contents.buffer,
99

Archive Download this file

Branches:
master



interactive