@@ -78,42 +78,62 @@ static unsigned int cec_log_addr2dev(const struct cec_adapter *adap, u8 log_addr
7878 * Queue a new event for this filehandle. If ts == 0, then set it
7979 * to the current time.
8080 *
81- * The two events that are currently defined do not need to keep track
82- * of intermediate events, so no actual queue of events is needed,
83- * instead just store the latest state and the total number of lost
84- * messages.
85- *
86- * Should new events be added in the future that require intermediate
87- * results to be queued as well, then a proper queue data structure is
88- * required. But until then, just keep it simple.
81+ * We keep a queue of at most max_event events where max_event differs
82+ * per event. If the queue becomes full, then drop the oldest event and
83+ * keep track of how many events we've dropped.
8984 */
9085void cec_queue_event_fh (struct cec_fh * fh ,
9186 const struct cec_event * new_ev , u64 ts )
9287{
93- struct cec_event * ev = & fh -> events [new_ev -> event - 1 ];
88+ static const u8 max_events [CEC_NUM_EVENTS ] = {
89+ 1 , 1 , 64 , 64 ,
90+ };
91+ struct cec_event_entry * entry ;
92+ unsigned int ev_idx = new_ev -> event - 1 ;
93+
94+ if (WARN_ON (ev_idx >= ARRAY_SIZE (fh -> events )))
95+ return ;
9496
9597 if (ts == 0 )
9698 ts = ktime_get_ns ();
9799
98100 mutex_lock (& fh -> lock );
99- if (new_ev -> event == CEC_EVENT_LOST_MSGS &&
100- fh -> pending_events & (1 << new_ev -> event )) {
101- /*
102- * If there is already a lost_msgs event, then just
103- * update the lost_msgs count. This effectively
104- * merges the old and new events into one.
105- */
106- ev -> lost_msgs .lost_msgs += new_ev -> lost_msgs .lost_msgs ;
107- goto unlock ;
108- }
101+ if (ev_idx < CEC_NUM_CORE_EVENTS )
102+ entry = & fh -> core_events [ev_idx ];
103+ else
104+ entry = kmalloc (sizeof (* entry ), GFP_KERNEL );
105+ if (entry ) {
106+ if (new_ev -> event == CEC_EVENT_LOST_MSGS &&
107+ fh -> queued_events [ev_idx ]) {
108+ entry -> ev .lost_msgs .lost_msgs +=
109+ new_ev -> lost_msgs .lost_msgs ;
110+ goto unlock ;
111+ }
112+ entry -> ev = * new_ev ;
113+ entry -> ev .ts = ts ;
114+
115+ if (fh -> queued_events [ev_idx ] < max_events [ev_idx ]) {
116+ /* Add new msg at the end of the queue */
117+ list_add_tail (& entry -> list , & fh -> events [ev_idx ]);
118+ fh -> queued_events [ev_idx ]++ ;
119+ fh -> total_queued_events ++ ;
120+ goto unlock ;
121+ }
109122
110- /*
111- * Intermediate states are not interesting, so just
112- * overwrite any older event.
113- */
114- * ev = * new_ev ;
115- ev -> ts = ts ;
116- fh -> pending_events |= 1 << new_ev -> event ;
123+ if (ev_idx >= CEC_NUM_CORE_EVENTS ) {
124+ list_add_tail (& entry -> list , & fh -> events [ev_idx ]);
125+ /* drop the oldest event */
126+ entry = list_first_entry (& fh -> events [ev_idx ],
127+ struct cec_event_entry , list );
128+ list_del (& entry -> list );
129+ kfree (entry );
130+ }
131+ }
132+ /* Mark that events were lost */
133+ entry = list_first_entry_or_null (& fh -> events [ev_idx ],
134+ struct cec_event_entry , list );
135+ if (entry )
136+ entry -> ev .flags |= CEC_EVENT_FL_DROPPED_EVENTS ;
117137
118138unlock :
119139 mutex_unlock (& fh -> lock );
@@ -134,46 +154,50 @@ static void cec_queue_event(struct cec_adapter *adap,
134154}
135155
136156/*
137- * Queue a new message for this filehandle. If there is no more room
138- * in the queue, then send the LOST_MSGS event instead.
157+ * Queue a new message for this filehandle.
158+ *
159+ * We keep a queue of at most CEC_MAX_MSG_RX_QUEUE_SZ messages. If the
160+ * queue becomes full, then drop the oldest message and keep track
161+ * of how many messages we've dropped.
139162 */
140163static void cec_queue_msg_fh (struct cec_fh * fh , const struct cec_msg * msg )
141164{
142- static const struct cec_event ev_lost_msg = {
143- .ts = 0 ,
165+ static const struct cec_event ev_lost_msgs = {
144166 .event = CEC_EVENT_LOST_MSGS ,
145- .flags = 0 ,
146- {
147- .lost_msgs .lost_msgs = 1 ,
148- },
167+ .lost_msgs .lost_msgs = 1 ,
149168 };
150169 struct cec_msg_entry * entry ;
151170
152171 mutex_lock (& fh -> lock );
153172 entry = kmalloc (sizeof (* entry ), GFP_KERNEL );
154- if (!entry )
155- goto lost_msgs ;
156-
157- entry -> msg = * msg ;
158- /* Add new msg at the end of the queue */
159- list_add_tail (& entry -> list , & fh -> msgs );
173+ if (entry ) {
174+ entry -> msg = * msg ;
175+ /* Add new msg at the end of the queue */
176+ list_add_tail (& entry -> list , & fh -> msgs );
177+
178+ if (fh -> queued_msgs < CEC_MAX_MSG_RX_QUEUE_SZ ) {
179+ /* All is fine if there is enough room */
180+ fh -> queued_msgs ++ ;
181+ mutex_unlock (& fh -> lock );
182+ wake_up_interruptible (& fh -> wait );
183+ return ;
184+ }
160185
161- /*
162- * if the queue now has more than CEC_MAX_MSG_RX_QUEUE_SZ
163- * messages, drop the oldest one and send a lost message event.
164- */
165- if ( fh -> queued_msgs == CEC_MAX_MSG_RX_QUEUE_SZ ) {
186+ /*
187+ * if the message queue is full, then drop the oldest one and
188+ * send a lost message event.
189+ */
190+ entry = list_first_entry ( & fh -> msgs , struct cec_msg_entry , list );
166191 list_del (& entry -> list );
167- goto lost_msgs ;
192+ kfree ( entry ) ;
168193 }
169- fh -> queued_msgs ++ ;
170194 mutex_unlock (& fh -> lock );
171- wake_up_interruptible (& fh -> wait );
172- return ;
173195
174- lost_msgs :
175- mutex_unlock (& fh -> lock );
176- cec_queue_event_fh (fh , & ev_lost_msg , 0 );
196+ /*
197+ * We lost a message, either because kmalloc failed or the queue
198+ * was full.
199+ */
200+ cec_queue_event_fh (fh , & ev_lost_msgs , ktime_get_ns ());
177201}
178202
179203/*
0 commit comments