Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1535114rwr; Wed, 26 Apr 2023 17:10:52 -0700 (PDT) X-Google-Smtp-Source: AKy350YEgHeZN1p8/1DMw24D8ATVhQSL42v3nPeV/TOS9aVBW1+edNSCzJeageL+Psy8O31eCqTM X-Received: by 2002:a05:6a20:4298:b0:f3:e0fc:a654 with SMTP id o24-20020a056a20429800b000f3e0fca654mr17228584pzj.7.1682554252252; Wed, 26 Apr 2023 17:10:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682554252; cv=none; d=google.com; s=arc-20160816; b=yEAo4atZPfZw/hmr08LrrJ/cTalP+oTx4wC1qmc+Yhr9aCBcH/y+lDRIqjTBHo3qFp qq/XaevHYXgTy6SDW1zBsuCJTL6FxMQWejI+H8fvFN5pQ3LvqTDMJQb5eiPmELqyE0ju 52ERV1aaxAYaWM6Lx4+HlBV4UbFhH6EiKRYec31N6UYxHUlRqhd06mFxywQduJvf9xnL Kq0YCBrFwByFtlI4wmzzoNBF9Q1955E3qsxuIBIr2YEK/tTZEpZTG3iDd90FUBLViMJs r9UKj8sxktLU5C45eVIwgbX7fWplnqmtD8LOly/qCej8GNKftasnMGayqdg6DTemgjNm 1+/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=PVGkNH8SAKHyU4ewQ+OfZZlAvgFTi0J5Tx9tCjECJi0=; b=tJSoIrBvb4lHtcuaw/MR0SpGAjvG0SRWSGyYJiLttQummIMXG/uNxdmCTBDwhvrx0H a2NEYvSagI1s8ckIMR12B12ZSrX0Wlu7Ke56hqUsSXDhmHPU2Wy6KnQIHzps6cr+DsRp UyQR7Bg+rWzLwJPQiNc7yMJhAgGI8cp8KR10L9RsHpO24tW+umkTv7Gl1+pVXrDh4bK6 hGuEsc7XjzGU/yJEqiRxPZzDSENdo6i1iF+4ycK+2zGNiYaoe+Uw74c6huMPlvK+io0l 4vc6vfFzKNBFCHi/MRGY+iGzIAaVO/CdYRvgBmnMLuIHA2e57EUvlk/nnTirLG60ApTf Ef3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=qq6MyjDB; 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 9-20020a630209000000b005289dd0ef00si244431pgc.568.2023.04.26.17.10.16; Wed, 26 Apr 2023 17:10:52 -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=qq6MyjDB; 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 S242688AbjD0ADn (ORCPT + 99 others); Wed, 26 Apr 2023 20:03:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242613AbjD0ADl (ORCPT ); Wed, 26 Apr 2023 20:03:41 -0400 Received: from mail-wr1-x44a.google.com (mail-wr1-x44a.google.com [IPv6:2a00:1450:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1EFF73AAA for ; Wed, 26 Apr 2023 17:03:37 -0700 (PDT) Received: by mail-wr1-x44a.google.com with SMTP id ffacd0b85a97d-2f831f6e175so4289526f8f.2 for ; Wed, 26 Apr 2023 17:03:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682553815; x=1685145815; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=PVGkNH8SAKHyU4ewQ+OfZZlAvgFTi0J5Tx9tCjECJi0=; b=qq6MyjDBvDrT46K2IB42/jAKgB5xhC8HvE5DZGK6hjyPBDDtneGzvHyIYbiwGccGv5 fJyFSWaUyl+mcymLHIZXLyZ9O00lQ5+oShA7w3pNNGC+wVICXFtDNPp6cphy0PNc82Ud 6u2f51op1AZXjuqdYzWF7LRo7djpIUC9DZ4O24zazkV5r3oJH5NVUIy9jsFgolizBHf7 FXIh2ietB3cwNX3Nsyp/2/V3hSeRjVqxjEUrwrVy7b1myoeVkTcEQI6E+dVdV4Qlqhs1 zPSzQK1zZHyALmBO8HKeNzgTPwiEFlTuZeT2F89+mNY6ikG2mUkJzZFozRaufOG/Oz6v J2zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682553815; x=1685145815; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=PVGkNH8SAKHyU4ewQ+OfZZlAvgFTi0J5Tx9tCjECJi0=; b=BqSJKgYAt8HOPX66k0rCOXcRyL4g0h4j6J+gRYNM5WHqvM1o7wChtuswp/hP2+k/J7 fjP9uws7+TubpfMeaxyYLSU1mOZ0xe5i76GmStsZWYQ+g39isWXA4lHPp5JmJLCT9Xyw yLxL9l5VTuzrd0xe6fJzZeuMOLm0SOIZjv0kQaXzBklLuSLfuTFc1BowteYjcLhV9LCU SZZouipJCgszMExLns7MvsST1wNkfUW8Ntu2OcsVMHHMUkXYI9D1S7ObkFRbYBP0uU/K m7+DKEh8/rpDYE/GBCstHHYYpfWe1V2ccO0TVA1a8xK3l9OTsBFcLs7xcAtOSbGiDTVk 480Q== X-Gm-Message-State: AAQBX9cw2d4COJlrZu+qbXt/gBoKpgj32V+sZns0TV68W1lS9mUgSEbW 1JPu2arTf7a/nHChPDASeqjM5BoEk0Y+ZXM= X-Received: from palandroid.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:350f]) (user=biswarupp job=sendgmr) by 2002:a5d:4fcd:0:b0:2f8:5130:522e with SMTP id h13-20020a5d4fcd000000b002f85130522emr2823320wrw.8.1682553815546; Wed, 26 Apr 2023 17:03:35 -0700 (PDT) Date: Thu, 27 Apr 2023 00:01:51 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230427000152.1407471-1-biswarupp@google.com> Subject: [PATCH] Call input_set_timestamp for events injected using uinput From: Biswarup Pal To: Dmitry Torokhov Cc: Biswarup Pal , kernel-team@android.com, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable 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 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 following + * 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 = ktime_set(0, 0); + if (ktime_compare(zero_time, timestamp) >= 0) + return false; + + current_time = ktime_get(); + offset = ktime_set(UINPUT_TIMESTAMP_ALLOWED_OFFSET_SECS, 0); + min_time = ktime_sub(current_time, offset); + + if (ktime_after(min_time, timestamp) || ktime_after(timestamp, current_time)) + return false; + + return true; +} + static ssize_t uinput_inject_events(struct uinput_device *udev, const char __user *buffer, size_t count) { struct input_event ev; size_t bytes = 0; + ktime_t timestamp; if (count != 0 && count < input_event_size()) return -EINVAL; @@ -588,6 +618,10 @@ static ssize_t uinput_inject_events(struct uinput_device *udev, if (input_event_from_user(buffer + bytes, &ev)) return -EFAULT; + timestamp = ktime_set(ev.input_event_sec, ev.input_event_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 += input_event_size(); cond_resched(); -- 2.40.1.495.gc816e09b53d-goog