Linux Input/HID development
 help / color / mirror / Atom feed
From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Aniroop Mathur <a.mathur@samsung.com>
Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] [v9]Input: evdev: fix bug of dropping valid packet after syn_dropped event
Date: Sat, 19 Nov 2016 10:59:59 -0800	[thread overview]
Message-ID: <20161119185959.GE20446@dtor-ws> (raw)
In-Reply-To: <1475608376-3077-1-git-send-email-a.mathur@samsung.com>

Hi Anoroop,

On Wed, Oct 05, 2016 at 12:42:56AM +0530, Aniroop Mathur wrote:
> If last event dropped in the old queue was EV_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 <a.mathur@samsung.com>
> 
> Difference from v8:
> Added check for handling EVIOCG[type] ioctl for queue empty case
> ---
>  drivers/input/evdev.c | 52 ++++++++++++++++++++++++++++++++++++++-------------
>  1 file changed, 39 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/input/evdev.c b/drivers/input/evdev.c
> index e9ae3d5..69407ff 100644
> --- a/drivers/input/evdev.c
> +++ b/drivers/input/evdev.c
> @@ -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 am uneasy with this "looking back" into the queue. How can we be sure
that we are looking at the right event? As far as I can tell we do not
know if that event has been consumed or not.

>  
>  	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->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->head = (client->head - 1) & mask;
> +		client->packet_head = client->tail = client->head;
> +		__evdev_queue_syn_dropped(client);
>  
> -		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) {
> @@ -920,6 +942,7 @@ static int evdev_handle_get_val(struct evdev_client *client,
>  	int ret;
>  	unsigned long *mem;
>  	size_t len;
> +	bool is_queue_empty;
>  
>  	len = BITS_TO_LONGS(maxbit) * sizeof(unsigned long);
>  	mem = kmalloc(len, GFP_KERNEL);
> @@ -933,12 +956,15 @@ static int evdev_handle_get_val(struct evdev_client *client,
>  
>  	spin_unlock(&dev->event_lock);
>  
> +	if (client->head == client->tail)
> +		is_queue_empty = true;
> +
>  	__evdev_flush_queue(client, type);
>  
>  	spin_unlock_irq(&client->buffer_lock);
>  
>  	ret = bits_to_user(mem, maxbit, maxlen, p, compat);
> -	if (ret < 0)
> +	if (ret < 0 && !is_queue_empty)
>  		evdev_queue_syn_dropped(client);
>  
>  	kfree(mem);
> -- 
> 2.6.2
> 

Thanks.

-- 
Dmitry

  parent reply	other threads:[~2016-11-19 18:59 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-04 19:12 [PATCH] [v9]Input: evdev: fix bug of dropping valid packet after syn_dropped event Aniroop Mathur
     [not found] ` <CAOXgUe3xduK2Fhs6ooPP=6fGSmY8m=088NVaNsreNE=_pP=asg@mail.gmail.com>
2016-10-12 18:35   ` Aniroop Mathur
2016-11-04 19:57     ` Aniroop Mathur
2016-11-19 18:59 ` Dmitry Torokhov [this message]
     [not found] ` <CGME20161119190005epcas4p156a7f1ebece75cae0bfaf88500d8bf90@epcas4p1.samsung.com>
2016-11-21 16:17   ` Aniroop Mathur
2017-01-31 16:29     ` Aniroop Mathur

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161119185959.GE20446@dtor-ws \
    --to=dmitry.torokhov@gmail.com \
    --cc=a.mathur@samsung.com \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox