Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp1021728ybp; Fri, 11 Oct 2019 07:55:36 -0700 (PDT) X-Google-Smtp-Source: APXvYqzjEhoh21pBZ7dR6RoNxGqutbzxBk1a7qKdismHJ042AanEm3BYA4R22WdXMmTDNTcuUk2C X-Received: by 2002:a50:ce06:: with SMTP id y6mr14100222edi.259.1570805736532; Fri, 11 Oct 2019 07:55:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570805736; cv=none; d=google.com; s=arc-20160816; b=HgKINgd6IKg9HNlGOou2RsQlNUcKco9uxNMN/7tSlWCUgwIv97mYK1WBStjqt/rfsG tB4GaSFu+5wEqHqlVGr4Rpi38mDUYCohoJokE3Xy9wKGDAIzUo5LmNBOsal3wXacEFDm a+/LLflSasDHRz3c9E4/Ie30bATxdY/8IOWMZT2PrTKhQ0NXRyLJ7SQuJ0YUlcarfRM3 QYUn2mGJMOBJ3lxNSvQhXRZ4toaQlPNeTXPBOwlyE5tehKzEl7OYwOZnc669swXgWEQv xaEgivGaFdJUDMq4ziueKm/6DOeFRdTEW4Jidz5qCi+Ed/2D7+aP1AZ4+9w/ce/Fiou7 CUwA== 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=LjOMlr04CDEWM+WiRuQ0QvBbFcIyJJ1BMad1vbnZ+hQ=; b=UBNAFaiYRBwYD5VVtZNqn71tXp7ejf3hrJvhrwGbRCrfHbkeSFWl6xSwJxEi4TXJ4Z 81RX8htpEkOujeMuF14sxP8o0tERu2QHPj5OZ+prp4yDbaC+2ezlH3x3wacFqf5XRAZo vcsp2SQnhNeLItiDj61iw0JevpJBNiWdt0AobJxcPadeh7gtsU/53QGrCVDKEoWjBufx t+0ZISlYWOl2evRkWdcBO2gHGWKovo6NBOYnUuxj15qDGjZ4z34dQz6KsPjxgDQBRqqp dyViEqAeuJBS0yaiBQaN2uxUM/gjmNpMPmWLti0s/QT3uE4rua8HtKfHrXXfqd4uRmUo hNLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CNOnOzJm; 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 x51si5906353edd.193.2019.10.11.07.55.13; Fri, 11 Oct 2019 07:55:36 -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=CNOnOzJm; 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 S1727646AbfJKOwX (ORCPT + 99 others); Fri, 11 Oct 2019 10:52:23 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:29760 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727587AbfJKOwW (ORCPT ); Fri, 11 Oct 2019 10:52:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1570805540; 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=LjOMlr04CDEWM+WiRuQ0QvBbFcIyJJ1BMad1vbnZ+hQ=; b=CNOnOzJmqRH5mhETW8/dcXcNZ8wjAxMVe9sSSw/z6uNlT+0K7ScTwRsDIXoJb9Jx699O/G nOD1ROJKKPVrxu5alHaNCrJAlUwimisv+KTh0TnaY9Pea8P5l1OXih0KFeOZkztT9GIg8y Q+0NFkDEilDLrPBkTAeDM1xw+KN2uNA= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-302-2PuEUBfPNq6YoOomPvurlA-1; Fri, 11 Oct 2019 10:52:16 -0400 Received: by mail-qt1-f197.google.com with SMTP id m19so9665021qtm.13 for ; Fri, 11 Oct 2019 07:52:16 -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=SZ0bfU2716rhby1DyBXCXfWn03SSsmBdmRdt0EnLS1k=; b=CnIALHlpS1HxBA6atwG/0kn5yzCn+d+Ypz+72Q9630d3BnsimQgu/tgamk4bAwfZob bek0NBW+5EsWNWsPTKLLq3YDdSdhn93jFqCGuHwx14HkBMW2rMuzX3cw+nvxvhOldx5x +xCM1I5h57KP4WvTWBgg5h80wlXwVWn0JZdAx9u/wRgvQrS9P9TCRNOKsS56J3t7zciw UOt0cvpjFz6WecT5SBuQoUI9evuH9BJz+gW+rllAtaofPyY9We0ibBdwLTjyoBfcUKvN UCYV7oqQ+HWJSsff8Bos7haGkd0BGnSbn2y+H+rt5c0DyjgM2Lr0J2XXlCyKBSG7tLJI W11Q== X-Gm-Message-State: APjAAAVbDGjUWKSb53yK8IApqMBLO/Qtijj7gemPFvAs8PUquqPsBtBq EKMK99jsrZk177svEgcvelxNL+IAcx/bUvBTUW4Dj3im3Hgi3db78LPOipExvkCYNiHWZrWnL1h WBFlt0GpIx+1V1o601fyIfVtVHLw85YdiP+tzKPOn X-Received: by 2002:ae9:f306:: with SMTP id p6mr16072324qkg.169.1570805536491; Fri, 11 Oct 2019 07:52:16 -0700 (PDT) X-Received: by 2002:ae9:f306:: with SMTP id p6mr16072285qkg.169.1570805536178; Fri, 11 Oct 2019 07:52:16 -0700 (PDT) MIME-Version: 1.0 References: <20191007051240.4410-1-andrew.smirnov@gmail.com> <20191007051240.4410-2-andrew.smirnov@gmail.com> In-Reply-To: <20191007051240.4410-2-andrew.smirnov@gmail.com> From: Benjamin Tissoires Date: Fri, 11 Oct 2019 16:52:04 +0200 Message-ID: Subject: Re: [PATCH 1/3] HID: logitech-hidpp: use devres to manage FF private data To: Andrey Smirnov , Dmitry Torokhov Cc: "open list:HID CORE LAYER" , Jiri Kosina , Henrik Rydberg , Sam Bazely , "Pierre-Loup A . Griffais" , Austin Palmer , lkml , "3.8+" X-MC-Unique: 2PuEUBfPNq6YoOomPvurlA-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 Hi Andrey, On Mon, Oct 7, 2019 at 7:13 AM Andrey Smirnov wr= ote: > > To simplify resource management in commit that follows as well 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 private 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/hid-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_ids? > + /* > + * 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. > + */ > + ff->private =3D NULL; > } > > static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index) > @@ -2090,7 +2095,7 @@ static int hidpp_ff_init(struct hidpp_device *hidpp= , u8 feature_index) > const u16 bcdDevice =3D le16_to_cpu(udesc->bcdDevice); > struct ff_device *ff; > struct hidpp_report response; > - struct hidpp_ff_private_data *data; > + struct hidpp_ff_private_data *data =3D hidpp->private_data; > int error, j, num_slots; > u8 version; > > @@ -2129,18 +2134,13 @@ static int hidpp_ff_init(struct hidpp_device *hid= pp, u8 feature_index) > return error; > } > > - data =3D kzalloc(sizeof(*data), GFP_KERNEL); > - if (!data) > - return -ENOMEM; > data->effect_ids =3D kcalloc(num_slots, sizeof(int), GFP_KERNEL); > - if (!data->effect_ids) { > - kfree(data); > + if (!data->effect_ids) > return -ENOMEM; > - } > + > data->wq =3D create_singlethread_workqueue("hidpp-ff-sendqueue"); > if (!data->wq) { > kfree(data->effect_ids); > - kfree(data); > return -ENOMEM; > } > > @@ -2199,28 +2199,15 @@ static int hidpp_ff_init(struct hidpp_device *hid= pp, u8 feature_index) > return 0; > } > > -static int hidpp_ff_deinit(struct hid_device *hid) > +static void hidpp_ff_deinit(struct hid_device *hid) > { > - struct hid_input *hidinput =3D list_entry(hid->inputs.next, struc= t hid_input, list); > - struct input_dev *dev =3D hidinput->input; > - struct hidpp_ff_private_data *data; > - > - if (!dev) { > - hid_err(hid, "Struct input_dev not found!\n"); > - return -EINVAL; > - } > + struct hidpp_device *hidpp =3D hid_get_drvdata(hid); > + struct hidpp_ff_private_data *data =3D hidpp->private_data; > > hid_info(hid, "Unloading HID++ force feedback.\n"); > - data =3D dev->ff->private; > - if (!data) { I am pretty sure we might need to keep a test on data not being null. > - hid_err(hid, "Private data not found!\n"); > - return -EINVAL; > - } > > destroy_workqueue(data->wq); > device_remove_file(&hid->dev, &dev_attr_range); > - > - return 0; > } This whole hunk seems unrelated to the devm change. Can you extract a patch that just stores hidpp_ff_private_data in hidpp->private_data and then cleans up hidpp_ff_deinit() before switching it to devm? (or maybe not, see below) After a few more thoughts, I don't think this mixing of devm and non devm is a good idea: we could say that the hidpp_ff_private_data and its effect_ids are both children of the input_dev, not the hid_device. And we would expect the whole thing to simplify with devm, but it's not, because ff is not supposed to be used with devm. I have a feeling the whole ff logic is wrong in term of where things should be cleaned up, but I can not come up with a good hint on where to start. For example, destroy_workqueue() is called in hidpp_ff_deinit() where it might be racy, and maybe we should call it in hidpp_ff_destroy()... Last, you should base this patch on top of the for-next branch, we recently merged a fix for the FF drivers in the HID subsystem: https://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git/commit/?h=3Dfor= -next&id=3Dd9d4b1e46d9543a82c23f6df03f4ad697dab361b Would it be too complex to drop this patch from the series and do a proper follow up cleanup series that might not need to go to stable? Cheers, Benjamin > > > @@ -2725,6 +2712,20 @@ static int k400_connect(struct hid_device *hdev, b= ool connected) > > #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123 > > +static int g920_allocate(struct hid_device *hdev) > +{ > + struct hidpp_device *hidpp =3D hid_get_drvdata(hdev); > + struct hidpp_ff_private_data *data; > + > + data =3D devm_kzalloc(&hdev->dev, sizeof(*data), GFP_KERNEL); > + if (!data) > + return -ENOMEM; > + > + hidpp->private_data =3D data; > + > + return 0; > +} > + > static int g920_get_config(struct hidpp_device *hidpp) > { > u8 feature_type; > @@ -3561,6 +3562,10 @@ static int hidpp_probe(struct hid_device *hdev, co= nst struct hid_device_id *id) > ret =3D k400_allocate(hdev); > if (ret) > return ret; > + } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) { > + ret =3D g920_allocate(hdev); > + if (ret) > + return ret; > } > > INIT_WORK(&hidpp->work, delayed_work_cb); > -- > 2.21.0 >