From nobody Sat Nov 23 20:37:45 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0CEC158558 for ; Mon, 11 Nov 2024 13:12:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731330773; cv=none; b=tq8Szm4GmHMeAczURRDzQO5NX7IRVsXw+suwPAGjkvP30VGNYBfE7Q72aH8H3ynziIvb0OrJKluenbXv5r4LBL0LrHCiY6pRw+PFS5oG/Z8PGUrMWRKylGtPe8+w34APnoRFhaUZG1Pe/DWB7w6IDKDdXNSk+N8l3Y0sXm1kLDA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731330773; c=relaxed/simple; bh=hlCgYj4KqFoNje7LPqbjN114QT5GqWux5iHnY488pNA=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=jx4AYpi2t5yquOA7Mr2FhgFtEZBAxNl5b9q46MGuvxpVG7OrHrwQEBmgTcrZptIAtvRjJLI4peAPGTxJSjNRecUSxN3w0gtZ9aDh26gr/z5Pi+602Hcm5iSGJxnnKA6ssIWP6bFYCjAQytvsYna/dICFBNtrplG5gTRlY3gpdrA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=M/PKMMw0; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="M/PKMMw0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731330770; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=FyfkMBNSykWaaIeAogOFuYzRo8X+mQaFAtl/EmHfBwc=; b=M/PKMMw0WKGcpzJ4ioJMRmO095V4tR1L6BVcWaY7vqAZk+z3eQMe7UfyEYEENe7ajcoA6o CoUcUzte224kglS3Awbp25B38rtz+MzyGpf5ZtPXHdeh6RMhx2KpoK+HwUtlYibihvNVI0 lEUDucOYqOBJSdYNXB2iJ38zpr3Fv/Q= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-216-7XekgXwUNGWoRRtlMyqelg-1; Mon, 11 Nov 2024 08:12:47 -0500 X-MC-Unique: 7XekgXwUNGWoRRtlMyqelg-1 X-Mimecast-MFC-AGG-ID: 7XekgXwUNGWoRRtlMyqelg Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 81DEA1953940; Mon, 11 Nov 2024 13:12:45 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.225.127]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A03D21956086; Mon, 11 Nov 2024 13:12:41 +0000 (UTC) From: Vitaly Kuznetsov To: linux-hyperv@vger.kernel.org, linux-input@vger.kernel.org Cc: Saurabh Singh Sengar , mhklinux@outlook.com, "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Jiri Kosina , Benjamin Tissoires , Dmitry Torokhov , linux-kernel@vger.kernel.org Subject: [PATCH v2] HID: hyperv: streamline driver probe to avoid devres issues Date: Mon, 11 Nov 2024 14:12:40 +0100 Message-ID: <20241111131240.35158-1-vkuznets@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Content-Type: text/plain; charset="utf-8" It was found that unloading 'hid_hyperv' module results in a devres complaint: ... hv_vmbus: unregistering driver hid_hyperv ------------[ cut here ]------------ WARNING: CPU: 2 PID: 3983 at drivers/base/devres.c:691 devres_release_grou= p+0x1f2/0x2c0 ... Call Trace: ? devres_release_group+0x1f2/0x2c0 ? __warn+0xd1/0x1c0 ? devres_release_group+0x1f2/0x2c0 ? report_bug+0x32a/0x3c0 ? handle_bug+0x53/0xa0 ? exc_invalid_op+0x18/0x50 ? asm_exc_invalid_op+0x1a/0x20 ? devres_release_group+0x1f2/0x2c0 ? devres_release_group+0x90/0x2c0 ? rcu_is_watching+0x15/0xb0 ? __pfx_devres_release_group+0x10/0x10 hid_device_remove+0xf5/0x220 device_release_driver_internal+0x371/0x540 ? klist_put+0xf3/0x170 bus_remove_device+0x1f1/0x3f0 device_del+0x33f/0x8c0 ? __pfx_device_del+0x10/0x10 ? cleanup_srcu_struct+0x337/0x500 hid_destroy_device+0xc8/0x130 mousevsc_remove+0xd2/0x1d0 [hid_hyperv] device_release_driver_internal+0x371/0x540 driver_detach+0xc5/0x180 bus_remove_driver+0x11e/0x2a0 ? __mutex_unlock_slowpath+0x160/0x5e0 vmbus_driver_unregister+0x62/0x2b0 [hv_vmbus] ... And the issue seems to be that the corresponding devres group is not allocated. Normally, devres_open_group() is called from __hid_device_probe() but Hyper-V HID driver overrides 'hid_dev->driver' with 'mousevsc_hid_driver' stub and basically re-implements __hid_device_probe() by calling hid_parse() and hid_hw_start() but not devres_open_group(). hid_device_probe() does not call __hid_device_probe() for it. Later, when the driver is removed, hid_device_remove() calls devres_release_group() as it doesn't check whether hdev->driver was initially overridden or not. The issue seems to be related to the commit 62c68e7cee33 ("HID: ensure timely release of driver-allocated resources") but the commit itself seems to be correct. Fix the issue by dropping the 'hid_dev->driver' override and using hid_register_driver()/hid_unregister_driver() instead. Alternatively, it would have been possible to rely on the default handling but HID_CONNECT_DEFAULT implies HID_CONNECT_HIDRAW and it doesn't seem to work for mousevsc as-is. Fixes: 62c68e7cee33 ("HID: ensure timely release of driver-allocated resour= ces") Suggested-by: Michael Kelley Signed-off-by: Vitaly Kuznetsov --- Changes since v1: - Keep custom HID driver for mousevsc but do it properly [Michael Kelley]. - Add 'Fixes:' tag [Saurabh Singh Sengar]. --- drivers/hid/hid-hyperv.c | 58 ++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/drivers/hid/hid-hyperv.c b/drivers/hid/hid-hyperv.c index f33485d83d24..0fb210e40a41 100644 --- a/drivers/hid/hid-hyperv.c +++ b/drivers/hid/hid-hyperv.c @@ -422,6 +422,25 @@ static int mousevsc_hid_raw_request(struct hid_device = *hid, return 0; } =20 +static int mousevsc_hid_probe(struct hid_device *hid_dev, const struct hid= _device_id *id) +{ + int ret; + + ret =3D hid_parse(hid_dev); + if (ret) { + hid_err(hid_dev, "parse failed\n"); + return ret; + } + + ret =3D hid_hw_start(hid_dev, HID_CONNECT_HIDINPUT | HID_CONNECT_HIDDEV); + if (ret) { + hid_err(hid_dev, "hw start failed\n"); + return ret; + } + + return 0; +} + static const struct hid_ll_driver mousevsc_ll_driver =3D { .parse =3D mousevsc_hid_parse, .open =3D mousevsc_hid_open, @@ -431,7 +450,16 @@ static const struct hid_ll_driver mousevsc_ll_driver = =3D { .raw_request =3D mousevsc_hid_raw_request, }; =20 -static struct hid_driver mousevsc_hid_driver; +static const struct hid_device_id mousevsc_devices[] =3D { + { HID_DEVICE(BUS_VIRTUAL, HID_GROUP_ANY, 0x045E, 0x0621) }, + { } +}; + +static struct hid_driver mousevsc_hid_driver =3D { + .name =3D "hid-hyperv", + .id_table =3D mousevsc_devices, + .probe =3D mousevsc_hid_probe, +}; =20 static int mousevsc_probe(struct hv_device *device, const struct hv_vmbus_device_id *dev_id) @@ -473,7 +501,6 @@ static int mousevsc_probe(struct hv_device *device, } =20 hid_dev->ll_driver =3D &mousevsc_ll_driver; - hid_dev->driver =3D &mousevsc_hid_driver; hid_dev->bus =3D BUS_VIRTUAL; hid_dev->vendor =3D input_dev->hid_dev_info.vendor; hid_dev->product =3D input_dev->hid_dev_info.product; @@ -488,20 +515,6 @@ static int mousevsc_probe(struct hv_device *device, if (ret) goto probe_err2; =20 - - ret =3D hid_parse(hid_dev); - if (ret) { - hid_err(hid_dev, "parse failed\n"); - goto probe_err2; - } - - ret =3D hid_hw_start(hid_dev, HID_CONNECT_HIDINPUT | HID_CONNECT_HIDDEV); - - if (ret) { - hid_err(hid_dev, "hw start failed\n"); - goto probe_err2; - } - device_init_wakeup(&device->device, true); =20 input_dev->connected =3D true; @@ -579,12 +592,23 @@ static struct hv_driver mousevsc_drv =3D { =20 static int __init mousevsc_init(void) { - return vmbus_driver_register(&mousevsc_drv); + int ret; + + ret =3D hid_register_driver(&mousevsc_hid_driver); + if (ret) + return ret; + + ret =3D vmbus_driver_register(&mousevsc_drv); + if (ret) + hid_unregister_driver(&mousevsc_hid_driver); + + return ret; } =20 static void __exit mousevsc_exit(void) { vmbus_driver_unregister(&mousevsc_drv); + hid_unregister_driver(&mousevsc_hid_driver); } =20 MODULE_LICENSE("GPL"); --=20 2.47.0