Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp2546464rwr; Fri, 28 Apr 2023 11:56:23 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6yQUUUY+AhVkB+0oIR6tP8WLBkmKoxzSx5DlIq4JMPEFYuz6yVs0oNoTFO+pqEqA6c4PAq X-Received: by 2002:a17:903:32ce:b0:1a6:6f09:6736 with SMTP id i14-20020a17090332ce00b001a66f096736mr7936524plr.20.1682708183025; Fri, 28 Apr 2023 11:56:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682708183; cv=none; d=google.com; s=arc-20160816; b=FKki1a6OEeowosm3EwzjniZVd94yWmiqOgChRDF2sXsXRfV7hew1D44xNUCF+UAIoX hjHRh82WMhcOdDppqEc6rR7PCH9PHyiFh6f2macTJ51jfp6SbKk4e21WpyCgOwTetgKm 0+yrvAx7K3yCZDibFgS047J6GKF7o1a8NrI+rp8Wq+vkAxxxAnIKF8wuNTm4nsklJ5+Y 8n7+Yzo5O0m4vnconxsFKLj9FJMoaNIp7Huey0ZNBX1h8XlIoxXWUUJh8Zsf71RFWgKs wcbAlEwJfjiRJgNku2e+Ykk162uJUISXW9T2aMEXyBS1vpeB3x89n3Hha/Ixz6pPDE+S hAjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=6b+Htt6CLQb89zu+TCed4Uelo19IvsWFLMs9XiUa7MQ=; b=exTl6xVT4dNy3xRwJq2u4zMGgYImmdwWGyCAa4i7XF6NsV72O+GAnPi2+KHH8mwxDv toNx7q8FtdvCh2nz0hRXieOEFrnPwJ3rrvJv8oKh2UluGD7vkojD3Yq1gZkG8JfHvjUv MtZt/bPAL50EvB1gLOfRxW/lv3jCp3DrbbEZLEVlPTUYQRFuIwUBmkZLzHuWbkGVKLqR UcvMH85/JvjstK1Ohiq4yBSaEIZFPmatqw/KZ70P8RCRJXIu3I3YR47KfP5TUl1re1W4 8ZT/yUi5mv5uY/TJ37zYjeFyrh2GqbvZFbpREiCcrd++Yrb2Ds2Y+1+g8V4Xbe8LQdCO 0Img== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=fu45N3qU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z2-20020a170902ccc200b001a6f3949171si22377506ple.24.2023.04.28.11.56.08; Fri, 28 Apr 2023 11:56:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=fu45N3qU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346493AbjD1Siu (ORCPT + 99 others); Fri, 28 Apr 2023 14:38:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229611AbjD1Sit (ORCPT ); Fri, 28 Apr 2023 14:38:49 -0400 Received: from mail-oo1-xc32.google.com (mail-oo1-xc32.google.com [IPv6:2607:f8b0:4864:20::c32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 537171BEB for ; Fri, 28 Apr 2023 11:38:48 -0700 (PDT) Received: by mail-oo1-xc32.google.com with SMTP id 006d021491bc7-546de76c23eso130319eaf.0 for ; Fri, 28 Apr 2023 11:38:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682707127; x=1685299127; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=6b+Htt6CLQb89zu+TCed4Uelo19IvsWFLMs9XiUa7MQ=; b=fu45N3qU9YtD8VGGP9MFC2YqwcPCE/WQUJN6O/kveVsVfV0kr5P5qeU3r9o8tK58a9 Jr76FCQqdD1bG2PUjPeqWeIFKfpGInorn/CuL5ryX3COAgbmpWvkv64p/S1r7SHO5M+U 4ZNYCgp4NI4urSCgEzbDreZl1WSR+C5YVLkBlO2oxqgKsNPAb4L0LsNDE9oM8YcgnRBs eqFBSBZW7pXXHQT6tgVo1vKKUJISevpmveyibFTx4LbYuuou/D6t/IL/oHtQgung/b4j 9iKl9lpc9yZJuLiQn+vUgzMRSTW/9gX5u6/UDyAeuVPoNKsjhU4SKFIRyK5G1/Yz31I6 FPLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682707127; x=1685299127; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6b+Htt6CLQb89zu+TCed4Uelo19IvsWFLMs9XiUa7MQ=; b=lfxl8ytufXeJWU2UdckU3EA7is94yW0E8GpjfvxMWCpEouQPNZUTXZU008on10ndZJ g60ORkTSLNiR687KizqGnuzGB9m/zW1LpS1nze9aTYUjxVu23zbHY8Xzl7AnfvEkLDQi eI1lkiDeh0v0NjTp1BrcNeGPzp7egK+avORV1IOkYDCF01rUGKS3DpRiYSjcUyZr7uWa LEFHoJ5GvOKNO8wPpEHXjkLDpJuX8V8i4iTKe9DPvya+8nPh/zhS0+9bj6TR1Z66Q2I/ iLl8xx/OvsCWVT7TmNAUfMoZGteZ9I0qlupnXKC2cQ+spoIrPd5qolXjU6R+V1sBlxm/ jkJQ== X-Gm-Message-State: AC+VfDwt8V/OqleWjYESJBM7em9zZZ3uO7ok0mFX9oFrJeBW7sxE0tKZ G92XslF8VPwU7YkgT1O8jd+2qYbE6ajS9Qytqik3QQ== X-Received: by 2002:a05:6820:1b15:b0:547:8545:acaf with SMTP id bv21-20020a0568201b1500b005478545acafmr3192957oob.7.1682707127442; Fri, 28 Apr 2023 11:38:47 -0700 (PDT) MIME-Version: 1.0 References: <20230427000152.1407471-1-biswarupp@google.com> In-Reply-To: <20230427000152.1407471-1-biswarupp@google.com> From: Siarhei Vishniakou Date: Fri, 28 Apr 2023 11:38:36 -0700 Message-ID: Subject: Re: [PATCH] Call input_set_timestamp for events injected using uinput To: Biswarup Pal Cc: Dmitry Torokhov , kernel-team@android.com, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Thanks Biswarup! Just to add a bit of context: we were concerned with breaking the existing uinput usages where the caller might be setting an incorrect value (since previously, that had no effect). So the 10 second guard was added to fall back to the default behaviour for those devices. Reviewed-by: Siarhei Vishniakou On Wed, Apr 26, 2023 at 5:03=E2=80=AFPM Biswarup Pal = wrote: > > Currently, uinput doesn't use the input_set_timestamp API, so any > event injected using uinput is not accurately timestamped in terms of > measuring when the actual event happened. Hence, call the > input_set_timestamp API from uinput in order to provide a more > accurate sense of time for the event. Propagate only the timestamps > which are a) positive, b) within a pre-defined offset (10 secs) from > the current time, and c) not in the future. > > Signed-off-by: Biswarup Pal > --- > drivers/input/misc/uinput.c | 34 ++++++++++++++++++++++++++++++++++ > 1 file changed, 34 insertions(+) > > diff --git a/drivers/input/misc/uinput.c b/drivers/input/misc/uinput.c > index f2593133e524..d98212d55108 100644 > --- a/drivers/input/misc/uinput.c > +++ b/drivers/input/misc/uinput.c > @@ -33,6 +33,7 @@ > #define UINPUT_NAME "uinput" > #define UINPUT_BUFFER_SIZE 16 > #define UINPUT_NUM_REQUESTS 16 > +#define UINPUT_TIMESTAMP_ALLOWED_OFFSET_SECS 10 > > enum uinput_state { UIST_NEW_DEVICE, UIST_SETUP_COMPLETE, UIST_CREATED }= ; > > @@ -569,11 +570,40 @@ static int uinput_setup_device_legacy(struct uinput= _device *udev, > return retval; > } > > +/* > + * Returns true if the given timestamp is valid (i.e., if all the follow= ing > + * conditions are satisfied), false otherwise. > + * 1) given timestamp is positive > + * 2) it's within the allowed offset before the current time > + * 3) it's not in the future > + */ > +static bool is_valid_timestamp(const ktime_t timestamp) > +{ > + ktime_t zero_time; > + ktime_t current_time; > + ktime_t min_time; > + ktime_t offset; > + > + zero_time =3D ktime_set(0, 0); > + if (ktime_compare(zero_time, timestamp) >=3D 0) > + return false; > + > + current_time =3D ktime_get(); > + offset =3D ktime_set(UINPUT_TIMESTAMP_ALLOWED_OFFSET_SECS, 0); > + min_time =3D ktime_sub(current_time, offset); > + > + if (ktime_after(min_time, timestamp) || ktime_after(timestamp, cu= rrent_time)) > + return false; > + > + return true; > +} > + > static ssize_t uinput_inject_events(struct uinput_device *udev, > const char __user *buffer, size_t cou= nt) > { > struct input_event ev; > size_t bytes =3D 0; > + ktime_t timestamp; > > if (count !=3D 0 && count < input_event_size()) > return -EINVAL; > @@ -588,6 +618,10 @@ static ssize_t uinput_inject_events(struct uinput_de= vice *udev, > if (input_event_from_user(buffer + bytes, &ev)) > return -EFAULT; > > + timestamp =3D ktime_set(ev.input_event_sec, ev.input_even= t_usec * NSEC_PER_USEC); > + if (is_valid_timestamp(timestamp)) > + input_set_timestamp(udev->dev, timestamp); > + > input_event(udev->dev, ev.type, ev.code, ev.value); > bytes +=3D input_event_size(); > cond_resched(); > -- > 2.40.1.495.gc816e09b53d-goog >