From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_2 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 69983C3A589 for ; Tue, 20 Aug 2019 14:34:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 46F5122DBF for ; Tue, 20 Aug 2019 14:34:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730395AbfHTOe1 (ORCPT ); Tue, 20 Aug 2019 10:34:27 -0400 Received: from mx2.suse.de ([195.135.220.15]:51790 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730390AbfHTOe1 (ORCPT ); Tue, 20 Aug 2019 10:34:27 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 2EC87AE84; Tue, 20 Aug 2019 14:34:26 +0000 (UTC) Message-ID: <1566311665.11678.22.camel@suse.com> Subject: Re: Duplicated code in hiddev_open() From: Oliver Neukum To: Alan Stern Cc: Jiri Kosina , USB list Date: Tue, 20 Aug 2019 16:34:25 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.26.6 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org Am Montag, den 19.08.2019, 10:17 -0400 schrieb Alan Stern: > On Mon, 19 Aug 2019, Oliver Neukum wrote: > > > Am Freitag, den 16.08.2019, 13:10 -0400 schrieb Alan Stern: > > > Oliver and Jiri: > > > > > > Why is there duplicated code in > > > drivers/hid/usbhid/hiddev.c:hiddev_open()? > > > > > > Line 267: > > > /* > > > * no need for locking because the USB major number > > > * is shared which usbcore guards against disconnect > > > */ > > > if (list->hiddev->exist) { > > > if (!list->hiddev->open++) { > > > res = hid_hw_open(hiddev->hid); > > > if (res < 0) > > > goto bail; > > > } > > > } else { > > > res = -ENODEV; > > > goto bail; > > > } > > > > > > Line 286: > > > mutex_lock(&hiddev->existancelock); > > > if (!list->hiddev->open++) > > > if (list->hiddev->exist) { > > > struct hid_device *hid = hiddev->hid; > > > res = hid_hw_power(hid, PM_HINT_FULLON); > > > if (res < 0) > > > goto bail_unlock; > > > res = hid_hw_open(hid); > > > if (res < 0) > > > goto bail_normal_power; > > > } > > > mutex_unlock(&hiddev->existancelock); > > > > > > The second part can never execute, because the first part ensures that > > > list->hiddev->open > 0 by the time the second part runs. > > > > > > Even more disturbing, why is one of these code sections protected by a > > > mutex and the other not? > > > > I suppose the comment I made back then: > > > > 079034073faf9 drivers/hid/usbhid/hiddev.c (Oliver Neukum 2008-12-16 10:55:15 +0100 268) * no need for locking because the USB major number > > 079034073faf9 drivers/hid/usbhid/hiddev.c (Oliver Neukum 2008-12-16 10:55:15 +0100 269) * is shared which usbcore guards against disconnect > > > > has ceased to be true, but the section was not removed, as the check > > for existance was duplicated. > > > > > Note: The second section was added in commit 0361a28d3f9a ("HID: > > > autosuspend support for USB HID") over ten years ago! > > > > Yes and I remember how frustrating keyboards were in testing, but > > no further details. > > Indeed. But more importantly for now, how should this be fixed? This > may be the culprit in some of the syzbot bug reports (those involving > hiddev). I doubt it. This looks like it would cause a resource leak, not the other way round. But I'd say all operations need to be done under lock. Regards oliver