From nobody Mon Nov 25 00:23:50 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1719337015; cv=none; d=zohomail.com; s=zohoarc; b=EntrLyPR2AE30tOg9FV7C5WLj0TF6GMtFYDIKx+ps2/AEBRRj66K7bwZTwd+MYFehyP397XKufB0ZhUUQk8ZMu4LVmsazNjd3xxMY6es2M856bvNWBuOwjc7ELngwwfEMNthMFaoI+D8axJCDCbMzhGhWaKPXDqGKdjnk3TzWsc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1719337015; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=rbbUSVqqZXzZF+3uTUU0bNtjkD7QAaruiWfpJ52dpCQ=; b=XfO2afFgGVam1t5QEXrYPfyitVSAS5mRJ7mi76R36C8Gu7sF0jV1UBt9tPXsKUl6/kWS/MpNkaM+37I21F7Yd7uVvKvAtHNgZLXOISX/PtsarCZKbRC1ALmoHWF4HU8FPk4VWR4lC8E0OsJ09GZhJZOgo44O8X6FxrzW5CDsFps= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1719337015496602.7421600385989; Tue, 25 Jun 2024 10:36:55 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sM9xb-0001TE-Qc; Tue, 25 Jun 2024 13:28:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sM7Ys-0003e6-Qa for qemu-devel@nongnu.org; Tue, 25 Jun 2024 10:54:22 -0400 Received: from mail-ed1-x52d.google.com ([2a00:1450:4864:20::52d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sM7Yr-0003Gl-4O for qemu-devel@nongnu.org; Tue, 25 Jun 2024 10:54:22 -0400 Received: by mail-ed1-x52d.google.com with SMTP id 4fb4d7f45d1cf-57cbc66a0a6so399786a12.1 for ; Tue, 25 Jun 2024 07:54:20 -0700 (PDT) Received: from 41414141.home (84-82-177-210.fixed.kpn.net. [84.82.177.210]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-57d3042d421sm6092740a12.42.2024.06.25.07.54.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jun 2024 07:54:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719327259; x=1719932059; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rbbUSVqqZXzZF+3uTUU0bNtjkD7QAaruiWfpJ52dpCQ=; b=k0CWagpBtv4+5W0IJODAw9qwFKM9Z9PZAEy3SD0nHKZ5K95Je7Jttffjs7J3NZCDqp q5FWBr7/FjovZgraXAhAwVBl5E0PDw21Rtjr3jgXRS8g4Q2iJPTj7JftKcHJMHMuMrOS +r0stSr/Xhy4hSa+qWXhdmFV72Q/P+9ZKqXWbJaRu81HzG/5iRZdchSDmXztcltaYun1 c4iE/dZjMnetc40mrM5nN+1mu6bRh06ebeSChJlTCWrs5BxejjPknMIKRf/y/udCcbgW R3llabukIbOjKtl/tzkEBQABJVymlv/SSQ9BoTpEqFyEqNhdNmANixPMfBVpuLnivXmK hzog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719327259; x=1719932059; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rbbUSVqqZXzZF+3uTUU0bNtjkD7QAaruiWfpJ52dpCQ=; b=FAxIV1wp4efuX4gLTc7lADwYqV53iozV21BCTql9nDaES5nMJPD6UjvycvAgcq0UsB UABquyY8W9v8FZwW4TTLyJPG4sj0B6m+6F4C0+g3PQzIhp71NURle7M17xH0pgb1YEDJ xXW1unKzx+w25LXOXhDYZ6a2DKTvG7djEOUn3+z6e1lYdwG65kpll0/n+nRK/q/UyOhj NeUzYQqwtRXmosl1kwajEhwjnAMIDqsgA2kNH1NVqRxcVdU6VaqUKaTvBAu6fWoe5sZ+ weh0225AiukMMyDpEdFkxI6bwrxbjvRVPJdcuswX7NhZfHn6gtChup0GxIo86qnB6QvO 9Ang== X-Gm-Message-State: AOJu0Yyl+qYie8zZ5iRrXz3ZnWYwdEQn12+nhLFc6cFx7EMZZLOrR+12 CFz6Z/upwws7Dr+2YHuDIDsnvp+lgDcDycqLiRiyC+Qpmc9RGGGcaMFTzw== X-Google-Smtp-Source: AGHT+IExjcGqP/+Yp+argbQqFMrMDhox0o8HA00ZaFqhGJVKN1W1DTcm4/KzQKeeuJ+pIrMnTlt14Q== X-Received: by 2002:aa7:d6cd:0:b0:57d:22a1:25cc with SMTP id 4fb4d7f45d1cf-57d447e24f5mr6569334a12.8.1719327259154; Tue, 25 Jun 2024 07:54:19 -0700 (PDT) From: David Bouman To: qemu-devel@nongnu.org Cc: David Bouman Subject: [PATCH 2/4] hw/usb/u2f-passthru: Do not needlessly retain handle to hidraw chardev Date: Tue, 25 Jun 2024 16:53:48 +0200 Message-Id: <20240625145350.65978-3-dbouman03@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240625145350.65978-1-dbouman03@gmail.com> References: <20240625145350.65978-1-dbouman03@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::52d; envelope-from=dbouman03@gmail.com; helo=mail-ed1-x52d.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Tue, 25 Jun 2024 13:25:46 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1719337017512100006 Content-Type: text/plain; charset="utf-8" The Linux kernel presumes a hidraw device to be "active" as long as an open handle to its character device exists, and during that time will actively poll its bus for new messages. The u2f-passthru device keeps an open handle to the hidraw character device for its entire lifetime, wasting power and causing its queue to be clogged with irrelevant packets that were not initiated by the guest. To mitigate this, use the u2f `start` and `stop` callbacks to dynamically open the handle when the guest is about to use the u2f-passthru device (start callback), and close it again whenever the guest stops using it (stop callback). Signed-off-by: David Bouman Fixes: 299976b050bf (hw/usb: Add U2F key passthru mode) --- hw/usb/u2f-passthru.c | 84 +++++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 23 deletions(-) diff --git a/hw/usb/u2f-passthru.c b/hw/usb/u2f-passthru.c index b7025d303d..54062ab4d5 100644 --- a/hw/usb/u2f-passthru.c +++ b/hw/usb/u2f-passthru.c @@ -118,7 +118,10 @@ static inline uint16_t packet_init_get_bcnt( static void u2f_passthru_reset(U2FPassthruState *key) { timer_del(&key->timer); - qemu_set_fd_handler(key->hidraw_fd, NULL, NULL, key); + + if (key->hidraw_fd >=3D 0) { + qemu_set_fd_handler(key->hidraw_fd, NULL, NULL, key); + } key->last_transaction_time =3D 0; key->current_transactions_start =3D 0; key->current_transactions_end =3D 0; @@ -456,47 +459,79 @@ static int u2f_passthru_open_from_scan(void) } #endif =20 -static void u2f_passthru_unrealize(U2FKeyState *base) -{ - U2FPassthruState *key =3D PASSTHRU_U2F_KEY(base); - - u2f_passthru_reset(key); - qemu_close(key->hidraw_fd); -} =20 -static void u2f_passthru_realize(U2FKeyState *base, Error **errp) +static int u2f_passthru_open_hidraw(U2FPassthruState *key, Error** errp) { - U2FPassthruState *key =3D PASSTHRU_U2F_KEY(base); int fd; - if (key->hidraw =3D=3D NULL) { #ifdef CONFIG_LIBUDEV fd =3D u2f_passthru_open_from_scan(); - if (fd < 0) { + + if (fd < 0 && errp) { error_setg(errp, "%s: Failed to find a U2F USB device", TYPE_U2F_PASSTHRU); - return; } #else - error_setg(errp, "%s: Missing hidraw", TYPE_U2F_PASSTHRU); - return; + if (errp) { + error_setg(errp, "%s: Missing hidraw", TYPE_U2F_PASSTHRU); + } + return -1; #endif } else { fd =3D qemu_open_old(key->hidraw, O_RDWR); - if (fd < 0) { + + if (fd < 0 && errp) { error_setg(errp, "%s: Failed to open %s", TYPE_U2F_PASSTHRU, key->hidraw); - return; - } =20 - if (!u2f_passthru_is_u2f_device(fd)) { + } else if (!u2f_passthru_is_u2f_device(fd)) { qemu_close(fd); - error_setg(errp, "%s: Passed hidraw does not represent " - "a U2F HID device", TYPE_U2F_PASSTHRU); - return; + if (errp) { + error_setg(errp, "%s: Passed hidraw does not represent " + "a U2F HID device", TYPE_U2F_PASSTHRU); + } + return -1; } } - key->hidraw_fd =3D fd; + + return fd; +} + +static bool u2f_passthru_start(U2FKeyState *base) +{ + U2FPassthruState *key =3D PASSTHRU_U2F_KEY(base); + + if (key->hidraw_fd < 0) { + key->hidraw_fd =3D u2f_passthru_open_hidraw(key, NULL); + } + return key->hidraw_fd >=3D 0; +} + +static void u2f_passthru_stop(U2FKeyState *base) +{ + U2FPassthruState *key =3D PASSTHRU_U2F_KEY(base); + + u2f_passthru_reset(key); + if (key->hidraw_fd >=3D 0) { + qemu_close(key->hidraw_fd); + key->hidraw_fd =3D -1; + } +} + +static void u2f_passthru_unrealize(U2FKeyState *base) +{ + u2f_passthru_stop(base); +} +static void u2f_passthru_realize(U2FKeyState *base, Error **errp) +{ + U2FPassthruState *key =3D PASSTHRU_U2F_KEY(base); + int fd =3D u2f_passthru_open_hidraw(key, errp); + + /* we only open the fd to error at start */ + if (fd >=3D 0) { + qemu_close(fd); + } + key->hidraw_fd =3D -1; u2f_passthru_reset(key); } =20 @@ -531,6 +566,9 @@ static void u2f_passthru_class_init(ObjectClass *klass,= void *data) kc->realize =3D u2f_passthru_realize; kc->unrealize =3D u2f_passthru_unrealize; kc->recv_from_guest =3D u2f_passthru_recv_from_guest; + kc->start =3D u2f_passthru_start; + kc->stop =3D u2f_passthru_stop; + dc->desc =3D "QEMU U2F passthrough key"; dc->vmsd =3D &u2f_passthru_vmstate; device_class_set_props(dc, u2f_passthru_properties); --=20 2.34.1