Input: evdev: fix bug of dropping valid packet after syn_dropped event
Subsystems (2)
PATCH v8 1/1 Input: evdev: fix bug of dropping valid packet after syn_dropped event
If last event dropped in the old queue was EVi_SYN/SYN_REPORT, then lets generate EV_SYN/SYN_REPORT immediately after queing EV_SYN/SYN_DROPPED so that clients would not ignore next valid full packet events.
--- drivers/input/evdev.c | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-)
drivers/input/evdev.c +34 -12
@@ -156,7 +156,12 @@ static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)static void __evdev_queue_syn_dropped (struct evdev_client *client) {struct input_event ev; +struct input_event *last_ev;ktime_t time; +unsigned int mask = client->bufsize -1 ; + +/* capture last event stored in the buffer */ + last_ev = &client->buffer[(client->head -1 ) & mask]; time = client->clk_type ==EV_CLK_REAL ? ktime_get_real() :
@@ -170,13 +175,28 @@ static void __evdev_queue_syn_dropped(struct evdev_client *client) ev.value =0 ; client->buffer[client->head++] = ev; - client->head &= client->bufsize -1 ; + client->head &= mask;if (unlikely (client->head == client->tail)) {/* drop queue but keep our SYN_DROPPED event */ - client->tail = (client->head -1 ) & (client->bufsize -1 ); + client->tail = (client->head -1 ) & mask; client->packet_head = client->tail; } + +/* +* If last packet was completely stored, then queue SYN_REPORT +* so that clients would not ignore next valid full packet +*/ +if (last_ev->type ==EV_SYN && last_ev->code ==SYN_REPORT ) { + last_ev->time = ev.time; + client->buffer[client->head++] = *last_ev; + client->head &= mask; + client->packet_head = client->head; + +/* drop queue but keep our SYN_DROPPED & SYN_REPORT event */ +if (unlikely (client->head == client->tail)) + client->tail = (client->head -2 ) & mask; + } }static void evdev_queue_syn_dropped (struct evdev_client *client)
@@ -218,7 +238,7 @@ static int evdev_set_clk_type(struct evdev_client *client, unsigned int clkid)spin_lock_irqsave (&client->buffer_lock, flags);if (client->head != client->tail) { - client->packet_head = client->head = client->tail; + client->packet_head = client->tail = client->head;__evdev_queue_syn_dropped (client); }
@@ -231,22 +251,24 @@ static int evdev_set_clk_type(struct evdev_client *client, unsigned int clkid)static void __pass_event (struct evdev_client *client,const struct input_event *event) { +unsigned int mask = client->bufsize -1 ; + client->buffer[client->head++] = *event; - client->head &= client->bufsize -1 ; + client->head &= mask;if (unlikely (client->head == client->tail)) {/* * This effectively "drops" all unconsumed events, leaving -* EV_SYN/SYN_DROPPED plus the newest event in the queue. +* EV_SYN/SYN_DROPPED, EV_SYN/SYN_REPORT (if required) and +* newest event in the queue. */ - client->tail = (client->head -2 ) & (client->bufsize -1 ); + client->head = (client->head -1 ) & mask; + client->packet_head = client->tail = client->head; +__evdev_queue_syn_dropped (client); - client->buffer[client->tail].time = event->time; - client->buffer[client->tail].type =EV_SYN ; - client->buffer[client->tail].code =SYN_DROPPED ; - client->buffer[client->tail].value =0 ; - - client->packet_head = client->tail; + client->buffer[client->head++] = *event; + client->head &= mask; +/* No need to check for buffer overflow as it just occurred */ }if (event->type ==EV_SYN && event->code ==SYN_REPORT ) {
Signature · 1 line
2.6.2
Dmitry Torokhov
maintainerHi Anoroop,
Quoted 14 lines · Aniroop Mathur
If last event dropped in the old queue was EVi_SYN/SYN_REPORT, then lets generate EV_SYN/SYN_REPORT immediately after queing EV_SYN/SYN_DROPPED so that clients would not ignore next valid full packet events.
Signed-off-by Aniroop Mathur <[email protected]>--- drivers/input/evdev.c | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-)drivers/input/evdev.c +0
-0
drivers/input/evdev.c static void __evdev_flush_queue(struct evdev_client *client, unsigned int type) · line 164
Quoted 9 lines
drivers/input/evdev.c +5
-0@@ -156,7 +156,12 @@ static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)static void __evdev_queue_syn_dropped (struct evdev_client *client) {struct input_event ev; +struct input_event *last_ev;ktime_t time; +unsigned int mask = client->bufsize -1 ; + +/* capture last event stored in the buffer */ + last_ev = &client->buffer[(client->head -1 ) & mask];
I have still the same comment. How do you know that event at last_ev position is in fact valid and unconsumed yet event. Also, you need to figure out not only if queue contains last SYN event, but also to handle the case where the queue is empty and client has consumed either full or partial packet at the time you are queueing the drop.
Also please enumerate what changes you done between version n and n+1 so I do not have to compare them line by line trying to figure it out myself.
Thanks.
Signature · 1 line
Dmitry
Hi Mr. Torokhov,
Quoted 24 lines · Dmitry Torokhov
Hi Anoroop,
Quoted 21 lines · Aniroop Mathur
If last event dropped in the old queue was EVi_SYN/SYN_REPORT, then lets generate EV_SYN/SYN_REPORT immediately after queing EV_SYN/SYN_DROPPED so that clients would not ignore next valid full packet events.
Signed-off-by Aniroop Mathur <[email protected]>--- drivers/input/evdev.c | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-)drivers/input/evdev.c +3
-0@@ -156,7 +156,12 @@ static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)static void __evdev_queue_syn_dropped (struct evdev_client *client) {struct input_event ev; +struct input_event *last_ev;ktime_t time; +unsigned int mask = client->bufsize -1 ; +
Quoted 9 lines
drivers/input/evdev.c static void __evdev_flush_queue(struct evdev_client *client, unsigned int type) · line 164
Quoted 2 lines
drivers/input/evdev.c +2
-0+/* capture last event stored in the buffer */ + last_ev = &client->buffer[(client->head -1 ) & mask];I have still the same comment. How do you know that event at
last_evposition is in fact valid and unconsumed yet event. Also, you need to figure out not only if queue contains last SYN event, but also to handle the case where the queue is empty and client has consumed either full or partial packet at the time you are queueing the drop.
Could you please explain what you mean exactly so that I could answer it properly?
Quoted 1 line
From what I understood, it seems to me that there is no problem related to
validity, unconsumption, empty queue, full/partial packet.
I would like to explain for clock change request case so that you can know my understanding for your question.
Clock change request case:
1.1 About last event being valid and unconsumed:
We flush buffer and queue syn_dropped only when buffer is not empty. So there will be always be atleast one event in buffer that is not consumed and is ofcourse valid.
1.2 About queue is empty
If not empty, we do not flush or add syn_dropped at all.
1.3 About consumption of full or partial packet
If client has consumed full packet, then buffer will look like, ... X Y S(consumed) ... X Y S
As we always store packets keeping buffer lock and not single events, so there will always be syn_report in the end.
If syn_dropped is queued here, then queing syn_report is fine.
If client has consumed partial packet, then buffer will look like, ... X(consumed) Y S ... S
If syn_dropped is queued here, then it is fine to queue syn_report because now new X Y will be reported by driver, and so client will consume new X and Y and that old X will be replaced with new X and this new packet will be sent by client to application. Obviously, client never sends partial events to application and send data packet by packet.
So I do not see any trouble here related to partial or full packet.
Quoted 4 lines
Also please enumerate what changes you done between version n and n+1 so I do not have to compare them line by line trying to figure it out myself.
Difference from v5:
Made a mistake about head index in v5.
Corrected in v8.
evdev_set_clk_type:
- client->packet_head = client->head = client->tail;
+ client->packet_head = client->tail = client->head;
__pass_event:
- client->tail = (client->head - 2) & (client->bufsize - 1);
+ client->head = (client->head - 1) & mask;
+ client->packet_head = client->tail = client->head;
+ __evdev_queue_syn_dropped(client);
v6:
Made change only for clock change and pass event function. Do not included change for evdev_flush_queue as I was not sure. But now I think it is okay for evdev_flush_queue also. As I am not 100% sure, so sent v6 for that.
v7:
please ignore it.
Thanks,
Aniroop Mathur
Quoted 4 lines
Thanks.
--
Dmitry
Dmitry Torokhov
maintainerQuoted 48 lines · Aniroop Mathur
Hi Mr. Torokhov,
Quoted 24 lines · Dmitry Torokhov
Hi Anoroop,
Quoted 21 lines · Aniroop Mathur
If last event dropped in the old queue was EVi_SYN/SYN_REPORT, then lets generate EV_SYN/SYN_REPORT immediately after queing EV_SYN/SYN_DROPPED so that clients would not ignore next valid full packet events.
Signed-off-by Aniroop Mathur <[email protected]>--- drivers/input/evdev.c | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-)drivers/input/evdev.c +3
-0@@ -156,7 +156,12 @@ static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)static void __evdev_queue_syn_dropped (struct evdev_client *client) {struct input_event ev; +struct input_event *last_ev;ktime_t time; +unsigned int mask = client->bufsize -1 ; +Quoted 9 lines
drivers/input/evdev.c static void __evdev_flush_queue(struct evdev_client *client, unsigned int type) · line 164
Quoted 2 lines
drivers/input/evdev.c +2
-0+/* capture last event stored in the buffer */ + last_ev = &client->buffer[(client->head -1 ) & mask];I have still the same comment. How do you know that event at
last_evposition is in fact valid and unconsumed yet event. Also, you need to figure out not only if queue contains last SYN event, but also to handle the case where the queue is empty and client has consumed either full or partial packet at the time you are queueing the drop.Could you please explain what you mean exactly so that I could answer it properly?
From what I understood, it seems to me that there is no problem related to validity, unconsumption, empty queue, full/partial packet.
I would like to explain for clock change request case so that you can know my understanding for your question.Clock change request case:
Quoted 7 lines
1.1 About last event being valid and unconsumed:
We flush buffer and queuesyn_droppedonly when buffer is not empty. So there will be always be atleast one event in buffer that is not consumed and is ofcourse valid.1.2 About queue is empty
If not empty, we do not flush or addsyn_droppedat all.
Clock type change is not the only time we queue SYN_DROP, the other time is when we fail to handle EVIOCG[type] (during which we remove some events from the queue). Queue may be empty when these ioctls are issued.
Quoted 6 lines
1.3 About consumption of full or partial packet
If client has consumed full packet, then buffer will look like, ... X Y S(consumed) ... X Y S
As we always store packets keeping buffer lock and not single events, so there will always besyn_reportin the end.
We try to pass full packets to clients, but there is no guarantee. We only estimate number of events in device's packet, not guarantee that it is correct size.
Quoted 15 lines
If
syn_droppedis queued here, then queingsyn_reportis fine.
If client has consumed partial packet, then buffer will look like, ... X(consumed) Y S ... S
Ifsyn_droppedis queued here, then it is fine to queuesyn_reportbecause now new X Y will be reported by driver, and so client will consume new X and Y and that old X will be replaced with new X and this new packet will be sent by client to application. Obviously, client never sends partial events to application and send data packet by packet.
So I do not see any trouble here related to partial or full packet.Quoted 4 lines
Also please enumerate what changes you done between version n and n+1 so I do not have to compare them line by line trying to figure it out myself.
Quoted 7 lines
Difference from v5:
Made a mistake about head index in v5.
Corrected in v8.
evdev_set_clk_type:
-client->packet_head=client->head=client->tail;
+client->packet_head=client->tail=client->head;
Why does this matter?
Thanks.
Signature · 1 line
Dmitry
Quoted 54 lines · Dmitry Torokhov
Quoted 53 lines · Aniroop Mathur
Hi Mr. Torokhov,
Quoted 24 lines · Dmitry Torokhov
Hi Anoroop,
Quoted 21 lines · Aniroop Mathur
If last event dropped in the old queue was EVi_SYN/SYN_REPORT, then lets generate EV_SYN/SYN_REPORT immediately after queing EV_SYN/SYN_DROPPED so that clients would not ignore next valid full packet events.
Signed-off-by Aniroop Mathur <[email protected]>--- drivers/input/evdev.c | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-)drivers/input/evdev.c +3
-0@@ -156,7 +156,12 @@ static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)static void __evdev_queue_syn_dropped (struct evdev_client *client) {struct input_event ev; +struct input_event *last_ev;ktime_t time; +unsigned int mask = client->bufsize -1 ; +Quoted 9 lines
drivers/input/evdev.c static void __evdev_flush_queue(struct evdev_client *client, unsigned int type) · line 164
Quoted 2 lines
drivers/input/evdev.c +2
-0+/* capture last event stored in the buffer */ + last_ev = &client->buffer[(client->head -1 ) & mask];I have still the same comment. How do you know that event at
last_evposition is in fact valid and unconsumed yet event. Also, you need to figure out not only if queue contains last SYN event, but also to handle the case where the queue is empty and client has consumed either full or partial packet at the time you are queueing the drop.Could you please explain what you mean exactly so that I could answer it properly?
From what I understood, it seems to me that there is no problem related to validity, unconsumption, empty queue, full/partial packet.
I would like to explain for clock change request case so that you can know my understanding for your question.Clock change request case:
1.1 About last event being valid and unconsumed:
We flush buffer and queuesyn_droppedonly when buffer is not empty. So there will be always be atleast one event in buffer that is not consumed and is ofcourse valid.
Quoted 7 lines
Quoted 2 lines
1.2 About queue is empty
If not empty, we do not flush or addsyn_droppedat all.Clock type change is not the only time we queue
SYN_DROP, the other time is when we fail to handle EVIOCG[type] (during which we remove some events from the queue). Queue may be empty when these ioctls are issued.
yeah, ideally it should be changed to:
ret = bits_to_user(mem, maxbit, maxlen, p, compat); if (ret < 0)
+ if (client->head != client->tail)
- evdev_queue_syn_dropped(client);
+ evdev_queue_syn_dropped(client);
Firstly, there is a need to follow protocol of SYN_REPORT as mentioned in next section.
Unless syn_report does not denote end of a packet,
it is okay without this change too because last event stored would be syn_report and after flushing, syn_report will still be at the end and with empty queue too if syn_dropped is queued then we have added syn_report to not ignore upcoming valid packet.
Quoted 11 lines
Quoted 6 lines
1.3 About consumption of full or partial packet
If client has consumed full packet, then buffer will look like, ... X Y S(consumed) ... X Y S
As we always store packets keeping buffer lock and not single events, so there will always besyn_reportin the end.We try to pass full packets to clients, but there is no guarantee. We only estimate number of events in device's packet, not guarantee that it is correct size.
As per documentation, SYN_REPORT should be used to separate packets.
* SYN_REPORT:
- Used to synchronize and separate events into packets of input data changes occurring at the same moment in time. For example, motion of a mouse may set the REL_X and REL_Y values for one motion, then emit a SYN_REPORT. The next motion will emit more REL_X and REL_Y values and send another SYN_REPORT.
So I think, there is a need of below change:
file: input.c
function: input_handle_event:
} else if (dev->num_vals >= dev->max_vals - 2) {
- dev->vals[dev->num_vals++] = input_value_sync;input_pass_values(dev, dev->vals, dev->num_vals);
When the packet is really completed
We have sufficient space in buffer to store more than 1 packet even when actual packet size is more than max_vals so there seems no need to add syn_report event here by self. So whenever driver sends syn_report, then only it will be considered as end of a packet and on exceding max_vals, we can simply pass to handlers to store those partial events in buffer. And anyways unless the packet is really completed then only client will send it to application.
Without following this protocol, we would not be able to find the end of a packet because if max_vals comes out to 3 but actual packet size is 15, then in the buffer, there will be many syn_reports within a single packet. So with both current code and with patch code, there will be trouble in handling syn_dropped because after one syn_report comes, client will stop ignoring the events.
Quoted 19 lines
Quoted 19 lines
If
syn_droppedis queued here, then queingsyn_reportis fine.
If client has consumed partial packet, then buffer will look like, ... X(consumed) Y S ... S
Ifsyn_droppedis queued here, then it is fine to queuesyn_reportbecause now new X Y will be reported by driver, and so client will consume new X and Y and that old X will be replaced with new X and this new packet will be sent by client to application. Obviously, client never sends partial events to application and send data packet by packet.
So I do not see any trouble here related to partial or full packet.Quoted 4 lines
Also please enumerate what changes you done between version n and n+1 so I do not have to compare them line by line trying to figure it out myself.
Difference from v5:
Made a mistake about head index in v5.
Corrected in v8.
Quoted 6 lines
Quoted 3 lines
evdev_set_clk_type:
-client->packet_head=client->head=client->tail;
+client->packet_head=client->tail=client->head;Why does this matter?
We must not change the head index. So we need to make packet_head and tail index same as head index and not same as tail index. After this setting, we call evdev_queue_syn_dropped where we capture last event as the event present at (head - 1) index;
Thanks,
Aniroop Mathur
Quoted 4 lines
Thanks.
--
Dmitry
Quoted 71 lines · Aniroop Mathur
Quoted 54 lines · Dmitry Torokhov
Quoted 53 lines · Aniroop Mathur
Hi Mr. Torokhov,
Quoted 24 lines · Dmitry Torokhov
Hi Anoroop,
Quoted 21 lines · Aniroop Mathur
If last event dropped in the old queue was EVi_SYN/SYN_REPORT, then lets generate EV_SYN/SYN_REPORT immediately after queing EV_SYN/SYN_DROPPED so that clients would not ignore next valid full packet events.
Signed-off-by Aniroop Mathur <[email protected]>--- drivers/input/evdev.c | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-)drivers/input/evdev.c +3
-0@@ -156,7 +156,12 @@ static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)static void __evdev_queue_syn_dropped (struct evdev_client *client) {struct input_event ev; +struct input_event *last_ev;ktime_t time; +unsigned int mask = client->bufsize -1 ; +Quoted 9 lines
drivers/input/evdev.c static void __evdev_flush_queue(struct evdev_client *client, unsigned int type) · line 164
Quoted 2 lines
drivers/input/evdev.c +2
-0+/* capture last event stored in the buffer */ + last_ev = &client->buffer[(client->head -1 ) & mask];I have still the same comment. How do you know that event at
last_evposition is in fact valid and unconsumed yet event. Also, you need to figure out not only if queue contains last SYN event, but also to handle the case where the queue is empty and client has consumed either full or partial packet at the time you are queueing the drop.Could you please explain what you mean exactly so that I could answer it properly?
From what I understood, it seems to me that there is no problem related to validity, unconsumption, empty queue, full/partial packet.
I would like to explain for clock change request case so that you can know my understanding for your question.Clock change request case:
1.1 About last event being valid and unconsumed:
We flush buffer and queuesyn_droppedonly when buffer is not empty. So there will be always be atleast one event in buffer that is not consumed and is ofcourse valid.Quoted 7 lines
Quoted 2 lines
1.2 About queue is empty
If not empty, we do not flush or addsyn_droppedat all.Clock type change is not the only time we queue
SYN_DROP, the other time is when we fail to handle EVIOCG[type] (during which we remove some events from the queue). Queue may be empty when these ioctls are issued.yeah, ideally it should be changed to:
ret =bits_to_user(mem, maxbit, maxlen, p, compat); if (ret < 0)
+ if (client->head!=client->tail)
-evdev_queue_syn_dropped(client);
+evdev_queue_syn_dropped(client);
Quoted 3 lines
Firstly, there is a need to follow protocol of
SYN_REPORTas mentioned in next section.
and yes, there is need to make many more changes to have correct last_event for this case. But it seems really complex.
Quoted 40 lines
Unless
syn_reportdoes not denote end of a packet,
it is okay without this change too because last event stored would besyn_reportand after flushing,syn_reportwill still be at the end and with empty queue too ifsyn_droppedis queued then we have addedsyn_reportto not ignore upcoming valid packet.Quoted 11 lines
Quoted 6 lines
1.3 About consumption of full or partial packet
If client has consumed full packet, then buffer will look like, ... X Y S(consumed) ... X Y S
As we always store packets keeping buffer lock and not single events, so there will always besyn_reportin the end.We try to pass full packets to clients, but there is no guarantee. We only estimate number of events in device's packet, not guarantee that it is correct size.
As per documentation,
SYN_REPORTshould be used to separate packets.
*SYN_REPORT:
- Used to synchronize and separate events into packets of input data changes occurring at the same moment in time. For example, motion of a mouse may set theREL_XandREL_Yvalues for one motion, then emit aSYN_REPORT. The next motion will emit moreREL_XandREL_Yvalues and send anotherSYN_REPORT.So I think, there is a need of below change:
file:input.c
function:input_handle_event:
} else if (dev->num_vals>=dev->max_vals- 2) {
- dev->vals[dev->num_vals++] =input_value_sync;input_pass_values(dev, dev->vals, dev->num_vals);We have sufficient space in buffer to store more than 1 packet even when actual packet size is more than
max_valsso there seems no need to addsyn_reportevent here by self. So whenever driver sendssyn_report, then only it will be considered as end of a packet and on excedingmax_vals, we can simply pass to handlers to store those partial events in buffer. And anyways unless the packet is really completed then only client will send it to application.
Quoted 7 lines
Without following this protocol, we would not be able to find the end of a packet because if
max_valscomes out to 3 but actual packet size is 15, then in the buffer, there will be manysyn_reportswithin a single packet. So with both current code and with patch code, there will be trouble in handlingsyn_droppedbecause after onesyn_reportcomes, client will stop ignoring the events.
How about above change and do you want me send separate patch for it?
Quoted 38 lines
Quoted 19 lines
Quoted 19 lines
If
syn_droppedis queued here, then queingsyn_reportis fine.
If client has consumed partial packet, then buffer will look like, ... X(consumed) Y S ... S
Ifsyn_droppedis queued here, then it is fine to queuesyn_reportbecause now new X Y will be reported by driver, and so client will consume new X and Y and that old X will be replaced with new X and this new packet will be sent by client to application. Obviously, client never sends partial events to application and send data packet by packet.
So I do not see any trouble here related to partial or full packet.Quoted 4 lines
Also please enumerate what changes you done between version n and n+1 so I do not have to compare them line by line trying to figure it out myself.
Difference from v5:
Made a mistake about head index in v5.
Corrected in v8.Quoted 6 lines
Quoted 3 lines
evdev_set_clk_type:
-client->packet_head=client->head=client->tail;
+client->packet_head=client->tail=client->head;Why does this matter?
We must not change the head index. So we need to make
packet_headand tail index same as head index and not same as tail index. After this setting, we callevdev_queue_syn_droppedwhere we capture last event as the event present at (head - 1) index;Thanks,
Aniroop MathurQuoted 4 lines
Thanks.
--
Dmitry
Hello Mr. Torokhov,
Quoted 151 lines · Aniroop Mathur
Quoted 82 lines · Aniroop Mathur
Quoted 65 lines · Dmitry Torokhov
Quoted 64 lines · Aniroop Mathur
Hi Mr. Torokhov,
Quoted 31 lines · Dmitry Torokhov
Hi Anoroop,
Quoted 17 lines · Aniroop Mathur
If last event dropped in the old queue was EVi_SYN/SYN_REPORT, then lets
generate EV_SYN/SYN_REPORT immediately after queing EV_SYN/SYN_DROPPED
so that clients would not ignore next valid full packet events.Signed-off-by Aniroop Mathur <[email protected]>--- drivers/input/evdev.c | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-)drivers/input/evdev.c +0
-0drivers/input/evdev.c static void __evdev_flush_queue(struct · line 165
Quoted 10 lines
drivers/input/evdev.c +5
-0@@ -156,7 +156,12 @@ static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)static void __evdev_queue_syn_dropped (struct evdev_client *client) {struct input_event ev; +struct input_event *last_ev;ktime_t time; +unsigned int mask = client->bufsize -1 ; + +/* capture last event stored in the buffer */ + last_ev = &client->buffer[(client->head -1 ) & mask];Quoted 8 lines
I have still the same comment. How do you know that event at
last_evposition is in fact valid and unconsumed yet event. Also, you need to figure out not only if queue contains last SYN event, but also to handle
the case where the queue is empty and client has consumed either full or
partial packet at the time you are queueing the drop.Could you please explain what you mean exactly so that I could answer it
properly?From what I understood, it seems to me that there is no problem related to
validity, unconsumption, empty queue, full/partial packet.
I would like to explain for clock change request case so that you can know
my understanding for your question.Clock change request case:
1.1 About last event being valid and unconsumed:
We flush buffer and queuesyn_droppedonly when buffer is not empty. So there
will be always be atleast one event in buffer that is not consumed and is
ofcourse valid.Quoted 7 lines
Quoted 2 lines
1.2 About queue is empty
If not empty, we do not flush or addsyn_droppedat all.Clock type change is not the only time we queue
SYN_DROP, the other time is when we fail to handle EVIOCG[type] (during which we remove some events from the queue). Queue may be empty when these ioctls are issued.yeah, ideally it should be changed to:
ret =bits_to_user(mem, maxbit, maxlen, p, compat); if (ret < 0)
+ if (client->head!=client->tail)
-evdev_queue_syn_dropped(client);
+evdev_queue_syn_dropped(client);Quoted 3 lines
Firstly, there is a need to follow protocol of
SYN_REPORTas mentioned in next section.and yes, there is need to make many more changes to have correct
last_eventfor this case. But it seems really complex.Quoted 48 lines
Unless
syn_reportdoes not denote end of a packet,
it is okay without this change too because last event stored would besyn_reportand after flushing,syn_reportwill still be at the end and with empty queue too ifsyn_droppedis queued then we have addedsyn_report
to not ignore upcoming valid packet.Quoted 8 lines
Quoted 7 lines
1.3 About consumption of full or partial packet
If client has consumed full packet, then buffer will look like, ... X Y S(consumed) ... X Y S
As we always store packets keeping buffer lock and not single events, so there
will always besyn_reportin the end.Quoted 4 lines
We try to pass full packets to clients, but there is no guarantee. We only estimate number of events in device's packet, not guarantee that it is correct size.
As per documentation,
SYN_REPORTshould be used to separate packets.
*SYN_REPORT:
- Used to synchronize and separate events into packets of input data
changes
occurring at the same moment in time. For example, motion of a mouse
may set
theREL_XandREL_Yvalues for one motion, then emit aSYN_REPORT. The
next
motion will emit moreREL_XandREL_Yvalues and send anotherSYN_REPORT.So I think, there is a need of below change:
file:input.c
function:input_handle_event:
} else if (dev->num_vals>=dev->max_vals- 2) {
- dev->vals[dev->num_vals++] =input_value_sync;input_pass_values(dev, dev->vals, dev->num_vals);We have sufficient space in buffer to store more than 1 packet even when actual packet size is more than
max_valsso there seems no need to addsyn_reportevent here by self. So whenever driver sendssyn_report, then only
it will be considered as end of a packet and on excedingmax_vals, we can simply pass to handlers to store those partial events in buffer. And anyways
unless the packet is really completed then only client will send it to application.Quoted 11 lines
Without following this protocol, we would not be able to find the end of a
packet because ifmax_valscomes out to 3 but actual packet size is 15, then
in the buffer, there will be manysyn_reportswithin a single packet. So with
both current code and with patch code, there will be trouble in handlingsyn_droppedbecause after onesyn_reportcomes, client will stop ignoring the
events.
Quoted 2 lines
How about above change and do you want me send separate patch for it?
Could you please update about above change as based on this change further changes can be decided.
Thank you,
Aniroop Mathur
Quoted 44 lines
Quoted 43 lines
Quoted 23 lines
Quoted 23 lines
If
syn_droppedis queued here, then queingsyn_reportis fine. If client has consumed partial packet, then buffer will look like, ... X(consumed) Y S ... S
Ifsyn_droppedis queued here, then it is fine to queuesyn_reportbecause
now new X Y will be reported by driver, and so client will consume new X and Y
and that old X will be replaced with new X and this new packet will be sent by
client to application. Obviously, client never sends partial events to application and send data packet by packet.
So I do not see any trouble here related to partial or full packet.Quoted 5 lines
Also please enumerate what changes you done between version n and n+1 so
I do not have to compare them line by line trying to figure it out myself.Difference from v5:
Made a mistake about head index in v5.
Corrected in v8.Quoted 6 lines
Quoted 3 lines
evdev_set_clk_type:
-client->packet_head=client->head=client->tail;
+client->packet_head=client->tail=client->head;Why does this matter?
We must not change the head index. So we need to make
packet_headand tail
index same as head index and not same as tail index. After this setting, we callevdev_queue_syn_droppedwhere we capture last event as the event present at (head - 1) index;Thanks,
Aniroop MathurQuoted 4 lines
Thanks.
--
Dmitry
Dear Mr. Torokhov,
Greetings of the day!!
Somehow, it has been a while since this patch is further discussed. I am top posting as to finish off the remaining two points.
Quoted 1 line
From the earlier conversations, I could deduce that this patch needs to be
further checked for 2 problems as follows:
Problem 1: Handle EVIOCG[type] for queue empty case
--> For this, I will send you the the new patch version in a while. Queue empty condition needs to be added before calling evdev_queue_syn_dropped. Since there is a rare chance that new packet gets inserted between buffer_lock mutex unlocking and copying events to user space, so I will check whether queue is empty or not before __evdev_flush_queue and then use it before invoking evdev_queue_syn_dropped.
And if queue is not empty then there is no problem as after flushing some events we always have atleast one SYN_REPORT event left in queue.
Problem 2: We try to pass full packets to clients, but there is no guarantee. --> From my earlier patch discussion regarding dropping syn_report in between a single packet it was deduced that since no driver is currently using that code so there is no impact. Hence for this problem too, we are good to go.
Do you have any more comments?
Best Regards,
Aniroop Mathur
Quoted 160 lines · Aniroop Mathur
Quoted 54 lines · Dmitry Torokhov
Quoted 53 lines · Aniroop Mathur
Hi Mr. Torokhov,
Quoted 24 lines · Dmitry Torokhov
Hi Anoroop,
Quoted 21 lines · Aniroop Mathur
If last event dropped in the old queue was EVi_SYN/SYN_REPORT, then lets generate EV_SYN/SYN_REPORT immediately after queing EV_SYN/SYN_DROPPED so that clients would not ignore next valid full packet events.
Signed-off-by Aniroop Mathur <[email protected]>--- drivers/input/evdev.c | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-)drivers/input/evdev.c +3
-0@@ -156,7 +156,12 @@ static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)static void __evdev_queue_syn_dropped (struct evdev_client *client) {struct input_event ev; +struct input_event *last_ev;ktime_t time; +unsigned int mask = client->bufsize -1 ; +Quoted 9 lines
drivers/input/evdev.c static void __evdev_flush_queue(struct evdev_client *client, unsigned int type) · line 164
Quoted 2 lines
drivers/input/evdev.c +2
-0+/* capture last event stored in the buffer */ + last_ev = &client->buffer[(client->head -1 ) & mask];I have still the same comment. How do you know that event at
last_evposition is in fact valid and unconsumed yet event. Also, you need to figure out not only if queue contains last SYN event, but also to handle the case where the queue is empty and client has consumed either full or partial packet at the time you are queueing the drop.Could you please explain what you mean exactly so that I could answer it properly?
From what I understood, it seems to me that there is no problem related to validity, unconsumption, empty queue, full/partial packet.
I would like to explain for clock change request case so that you can know my understanding for your question.Clock change request case:
1.1 About last event being valid and unconsumed:
We flush buffer and queuesyn_droppedonly when buffer is not empty. So there will be always be atleast one event in buffer that is not consumed and is ofcourse valid.Quoted 7 lines
Quoted 2 lines
1.2 About queue is empty
If not empty, we do not flush or addsyn_droppedat all.Clock type change is not the only time we queue
SYN_DROP, the other time is when we fail to handle EVIOCG[type] (during which we remove some events from the queue). Queue may be empty when these ioctls are issued.yeah, ideally it should be changed to:
ret =bits_to_user(mem, maxbit, maxlen, p, compat); if (ret < 0)
+ if (client->head!=client->tail)
-evdev_queue_syn_dropped(client);
+evdev_queue_syn_dropped(client);Firstly, there is a need to follow protocol of
SYN_REPORTas mentioned in next section.Unless
syn_reportdoes not denote end of a packet,
it is okay without this change too because last event stored would besyn_reportand after flushing,syn_reportwill still be at the end and with empty queue too ifsyn_droppedis queued then we have addedsyn_reportto not ignore upcoming valid packet.Quoted 11 lines
Quoted 6 lines
1.3 About consumption of full or partial packet
If client has consumed full packet, then buffer will look like, ... X Y S(consumed) ... X Y S
As we always store packets keeping buffer lock and not single events, so there will always besyn_reportin the end.We try to pass full packets to clients, but there is no guarantee. We only estimate number of events in device's packet, not guarantee that it is correct size.
As per documentation,
SYN_REPORTshould be used to separate packets.
*SYN_REPORT:
- Used to synchronize and separate events into packets of input data changes occurring at the same moment in time. For example, motion of a mouse may set theREL_XandREL_Yvalues for one motion, then emit aSYN_REPORT. The next motion will emit moreREL_XandREL_Yvalues and send anotherSYN_REPORT.So I think, there is a need of below change:
file:input.c
function:input_handle_event:
} else if (dev->num_vals>=dev->max_vals- 2) {
- dev->vals[dev->num_vals++] =input_value_sync;input_pass_values(dev, dev->vals, dev->num_vals);When the packet is really completed
We have sufficient space in buffer to store more than 1 packet even when actual packet size is more thanmax_valsso there seems no need to addsyn_reportevent here by self. So whenever driver sendssyn_report, then only it will be considered as end of a packet and on excedingmax_vals, we can simply pass to handlers to store those partial events in buffer. And anyways unless the packet is really completed then only client will send it to application.Without following this protocol, we would not be able to find the end of a packet because if
max_valscomes out to 3 but actual packet size is 15, then in the buffer, there will be manysyn_reportswithin a single packet. So with both current code and with patch code, there will be trouble in handlingsyn_droppedbecause after onesyn_reportcomes, client will stop ignoring the events.Quoted 19 lines
Quoted 19 lines
If
syn_droppedis queued here, then queingsyn_reportis fine.
If client has consumed partial packet, then buffer will look like, ... X(consumed) Y S ... S
Ifsyn_droppedis queued here, then it is fine to queuesyn_reportbecause now new X Y will be reported by driver, and so client will consume new X and Y and that old X will be replaced with new X and this new packet will be sent by client to application. Obviously, client never sends partial events to application and send data packet by packet.
So I do not see any trouble here related to partial or full packet.Quoted 4 lines
Also please enumerate what changes you done between version n and n+1 so I do not have to compare them line by line trying to figure it out myself.
Difference from v5:
Made a mistake about head index in v5.
Corrected in v8.Quoted 6 lines
Quoted 3 lines
evdev_set_clk_type:
-client->packet_head=client->head=client->tail;
+client->packet_head=client->tail=client->head;Why does this matter?
We must not change the head index. So we need to make
packet_headand tail index same as head index and not same as tail index. After this setting, we callevdev_queue_syn_droppedwhere we capture last event as the event present at (head - 1) index;Thanks,
Aniroop MathurQuoted 4 lines
Thanks.
--
Dmitry