Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp1523688ybp; Fri, 11 Oct 2019 15:56:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqwcGd9EhzuxZ8MwijRQJHO5DtPJrbP9XUtyR29te/7vFyAqOax9ROz+AB+DudWlU8BXj6yM X-Received: by 2002:aa7:d6cd:: with SMTP id x13mr16099583edr.272.1570834592587; Fri, 11 Oct 2019 15:56:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570834592; cv=none; d=google.com; s=arc-20160816; b=ng55ksh4Cp/ivxMstvMZFJi41XSm6V5dljl9M192LMOoZWuetoZ0sAYx1ZdHaTHz2J CmKuy9FGl2Y4TMNbJNNAYGav3FXIS+3Z+CRjWsOPrgBgp3MtwIeWTsPetKnW7z8aYPZY RXPU3pY7bZZzsOyM9b8ZGYav5Ra1+38X76maRMoIILTx8EzYRofxpFx9IC86/op3Uv05 VhqZhJcKPNjXVpvExZXhbeometeBvpoWTWePlJ52SOQmBbJcT9t7PXxcy32TAUCN0aig EAmm5MBXPixwGjnRn5b+6PYX3dPNtS/xLQl7+1a0MnwVda8j4nsIrdo/nYnDM0ptqVXx N0Wg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=7RgpO7SkaYhR0wNjaRJqhAx6MtC43nqgY8XsTUlipHM=; b=q+aAY+OlcrZubWmBKOWeldzuJXF5n9TpEkj2tvJwf8fhUJPcweefcIu4s7TO7gb5ya u0+70ps0zy1qhwT2rMxDAY4Wo0mV7BPN2xHp0W8uRHJF/SYY+1KfbfVE2yNdgINSlH8z 3QhoQhNwrxtte/1xKJ6v+2Xd9/mELZxQxrIiiYRxBQdt94I5m0pZonkO2NUx+Vg2BYTo 22P4XfUAB8pWOpar0Fe2tEGprE5qNxyge1yrhLlojTIVWBgZPVJ68IdSBbV6nCEJyNkl 3v3HO+TjAAtRWrr2G10rVZqINco389gZ741tRhU1MGc/TmpzM2o7VmZtkVRGuUKNFUp5 TtPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=afAbI0So; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l19si6514618eds.389.2019.10.11.15.56.09; Fri, 11 Oct 2019 15:56:32 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=afAbI0So; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727973AbfJKWs7 (ORCPT + 99 others); Fri, 11 Oct 2019 18:48:59 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:42002 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727908AbfJKWs7 (ORCPT ); Fri, 11 Oct 2019 18:48:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1570834136; 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: in-reply-to:in-reply-to:references:references; bh=7RgpO7SkaYhR0wNjaRJqhAx6MtC43nqgY8XsTUlipHM=; b=afAbI0SoKPtHKqm3EeJMjp/mwwoy2tEVFANnhUavaK3gVzSM++2bbBNHJ2MXuiPRMSkHAe woFGQ/ND6LtT44f8MxK1wWQ7wIVjbnPL3OrhalgWChe9KjJMq8Vhow9BdbyoYJkTR0fdVR zpvP197YydJZ6IJeJN6HciLnxyEdkl8= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-30-3TLZE6X7PbG7pcl3aptwcA-1; Fri, 11 Oct 2019 18:48:55 -0400 Received: by mail-qt1-f200.google.com with SMTP id h20so10965466qto.7 for ; Fri, 11 Oct 2019 15:48:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Sldpp1aOhl8UuH/6Wbt+hAC3+th/BbJ+jr5E9+Cj7TI=; b=lRFxQv017adpXROZhXWZOQ8KNZgokLA8uz57d4Twf+IqBCWDwDnBnK1VTHt2s79ctw HxKH+x8Nw71m2oWSeTsMzSxvYhPkYkTmAwIggjssVFgkTlN/h0vIe2cGSkdPRIz3gTeh HchNEmQwxcQJlNXSK11C9l2BCvoQMGit1NKqjKaeG/BJ8/NS9F+M9qKwRSawdPQL1mO+ tnWtxnkCo6UYqcXhk+W22VA/Xm4uihcFdDaLLzkCwEeHIIvvHyxI0SP7hPJYp31YZ9/l DxI/JoJlUc0Px8a7cYuh+h/ZX2nMjTwFfCU8u1d2BoPnqFk9QgLkqp0tt+/34q1dd3x2 1YrA== X-Gm-Message-State: APjAAAWVd1EcIcLCyItUu2VSKwK+75gE+hBc0ZnpzJw7S46i4wLmX4Sf uNnG3ogL9+/SmhVrBv9fv9mIRGLG24n6SO/0UeKtTQzVPG86hIQ5W2nh6oySUIJ+QbFREv2RmvB RgGeqjZMU+G1aFFY7TsV5vrY7wfVFdHYFU4JWHxVS X-Received: by 2002:a05:620a:13d9:: with SMTP id g25mr18415092qkl.230.1570834134402; Fri, 11 Oct 2019 15:48:54 -0700 (PDT) X-Received: by 2002:a05:620a:13d9:: with SMTP id g25mr18415070qkl.230.1570834134123; Fri, 11 Oct 2019 15:48:54 -0700 (PDT) MIME-Version: 1.0 References: <20191007051240.4410-1-andrew.smirnov@gmail.com> <20191007051240.4410-2-andrew.smirnov@gmail.com> <20191011182617.GE229325@dtor-ws> <20191011203303.GF229325@dtor-ws> <20191011203509.GG229325@dtor-ws> <20191011213349.GJ229325@dtor-ws> In-Reply-To: <20191011213349.GJ229325@dtor-ws> From: Benjamin Tissoires Date: Sat, 12 Oct 2019 00:48:42 +0200 Message-ID: Subject: Re: [PATCH 1/3] HID: logitech-hidpp: use devres to manage FF private data To: Dmitry Torokhov Cc: Andrey Smirnov , "open list:HID CORE LAYER" , Jiri Kosina , Henrik Rydberg , Sam Bazely , "Pierre-Loup A . Griffais" , Austin Palmer , lkml , "3.8+" X-MC-Unique: 3TLZE6X7PbG7pcl3aptwcA-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Oct 11, 2019 at 11:34 PM Dmitry Torokhov wrote: > > On Fri, Oct 11, 2019 at 01:35:09PM -0700, Dmitry Torokhov wrote: > > On Fri, Oct 11, 2019 at 01:33:03PM -0700, Dmitry Torokhov wrote: > > > On Fri, Oct 11, 2019 at 09:25:52PM +0200, Benjamin Tissoires wrote: > > > > On Fri, Oct 11, 2019 at 8:26 PM Dmitry Torokhov > > > > wrote: > > > > > > > > > > On Fri, Oct 11, 2019 at 04:52:04PM +0200, Benjamin Tissoires wrot= e: > > > > > > Hi Andrey, > > > > > > > > > > > > On Mon, Oct 7, 2019 at 7:13 AM Andrey Smirnov wrote: > > > > > > > > > > > > > > To simplify resource management in commit that follows as wel= l as to > > > > > > > save a couple of extra kfree()s and simplify hidpp_ff_deinit(= ) switch > > > > > > > driver code to use devres to manage the life-cycle of FF priv= ate data. > > > > > > > > > > > > > > Signed-off-by: Andrey Smirnov > > > > > > > Cc: Jiri Kosina > > > > > > > Cc: Benjamin Tissoires > > > > > > > Cc: Henrik Rydberg > > > > > > > Cc: Sam Bazely > > > > > > > Cc: Pierre-Loup A. Griffais > > > > > > > Cc: Austin Palmer > > > > > > > Cc: linux-input@vger.kernel.org > > > > > > > Cc: linux-kernel@vger.kernel.org > > > > > > > Cc: stable@vger.kernel.org > > > > > > > > > > > > This patch doesn't seem to fix any error, is there a reason to = send it > > > > > > to stable? (besides as a dependency of the rest of the series). > > > > > > > > > > > > > --- > > > > > > > drivers/hid/hid-logitech-hidpp.c | 53 +++++++++++++++++-----= ---------- > > > > > > > 1 file changed, 29 insertions(+), 24 deletions(-) > > > > > > > > > > > > > > diff --git a/drivers/hid/hid-logitech-hidpp.c b/drivers/hid/h= id-logitech-hidpp.c > > > > > > > index 0179f7ed77e5..58eb928224e5 100644 > > > > > > > --- a/drivers/hid/hid-logitech-hidpp.c > > > > > > > +++ b/drivers/hid/hid-logitech-hidpp.c > > > > > > > @@ -2079,6 +2079,11 @@ static void hidpp_ff_destroy(struct ff= _device *ff) > > > > > > > struct hidpp_ff_private_data *data =3D ff->private; > > > > > > > > > > > > > > kfree(data->effect_ids); > > > > > > > > > > > > Is there any reasons we can not also devm alloc data->effect_id= s? > > > > > > > > > > > > > + /* > > > > > > > + * Set private to NULL to prevent input_ff_destroy() = from > > > > > > > + * freeing our devres allocated memory > > > > > > > > > > > > Ouch. There is something wrong here: input_ff_destroy() calls > > > > > > kfree(ff->private), when the data has not been allocated by > > > > > > input_ff_create(). This seems to lack a little bit of symmetry. > > > > > > > > > > Yeah, ff and ff-memless essentially take over the private data as= signed > > > > > to them. They were done before devm and the lifetime of the "priv= ate" > > > > > data pieces was tied to the lifetime of the input device to simpl= ify > > > > > error handling and teardown. > > > > > > > > Yeah, that stealing of the pointer is not good :) > > > > But OTOH, it helps > > > > > > > > > > > > > > Maybe we should clean it up a bit... I'm open to suggestions. > > > > > > > > The problem I had when doing the review was that there is no easy w= ay > > > > to have a "devm_input_ff_create_()", because the way it's built is > > > > already "devres-compatible": the destroy gets called by input core. > > > > > > I do not think we want devm_input_ff_create() explicitly, I think the > > > fact that you can "build up" an input device by allocating it, then > > > adding slots, poller, ff support, etc, and input core cleans it up is > > > all good. It is just the ownership if the driver-private data block i= s > > > not very obvious and is not compatible with allocating via devm. > > > > > > > > > > > So I don't have a good answer to simplify in a transparent manner > > > > without breaking the API. > > > > > > > > > > > > > > In this case maybe best way is to get rid of hidpp_ff_destroy() a= nd not > > > > > set ff->private and rely on devm to free the buffers. One can get= to > > > > > device private data from ff methods via input_get_drvdata() since= they > > > > > all (except destroy) are passed input device pointer. > > > > > > > > Sounds like a good idea. However, it seems there might be a race wh= en > > > > removing the workqueue: > > > > the workqueue gets deleted in hidpp_remove, when the input node wil= l > > > > be freed by devres, so after the call of hidpp_remove. > > > > > > Yeah, well, that is a common issue with mixing devm and normal resour= ces > > > (and workqueue here is that "normal" resource), and we should either: > > > > > > - not use devm > > > - use devm_add_action_or_reset() to work in custom actions that work > > > freeing of non-managed resources into devm flow. > > > > Actually, there is a door #3: use system workqueue. After all the work > > that Tejun done on workqueues it is very rare that one actually needs > > a dedicated workqueue (as works usually execute on one if the system > > worker threads that are shared with other workqueues anyway). > > And additional note about devm: > > I think all HID input drivers that are using devm in probe, but do not > have proper remove() function (and maybe even some with remove) are > broken: hid_device_remove() calls hid_hw_stop() which potentially will > shut off the transport. This happens before devm starts unwinding, so > we still can be trying to communicate with the device in question, but > the transport is gone. Well, that is by design. A driver is supposed to call hid_hw_start() at the very end of its .probe(). And the supposed rule is that in the specific .remove(), you are to call first hid_hw_stop() to stop the transport layer underneath. That also means that in the HID subsystem, at least, you are not supposed to talk to the device during the devm teardown of the allocated data. If you really need to communicate with the device during tear down, then you are supposed to write your own .remove, in which you control where the hid_hw_stop() happens. We might have overlooked one or two, but I think we are on a good basis for= now. > > io_started/driver_input_lock is broken on removal as well as we release > the lock when driver may very well be still talking to the device in > devm teardown actions. Again, this is not supposed to happen. Once hid_hw_stop() is called, we do not have access to the transport, so drivers can't talk to the device. So releasing/clearing the locks is supposed to be safe now. > > I think we have similar kind of issues in other buses as well (i2c, spi, > etc). For example, in i2c we remove the device from power domain before > we actually complete devm unwinding. > I agree that this looks bad. I would need to have a better look at it on Monday. Time to go on week end (this jet lag doesn't help me to go to sleep...) Cheers, Benjamin