Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp1001819rdg; Fri, 11 Aug 2023 06:58:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFZZYeWrCXgEYB2oHGKl8sGQFYn7IkJDhbE1AIlD8MnycBlCNiXu/TzdWgkwR2os5Cw5Z+8 X-Received: by 2002:a2e:809a:0:b0:2b4:6f0c:4760 with SMTP id i26-20020a2e809a000000b002b46f0c4760mr1626246ljg.11.1691762337394; Fri, 11 Aug 2023 06:58:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691762337; cv=none; d=google.com; s=arc-20160816; b=oyLL2b/Ouos5xQ8OUg1HdBOFpGhXOejDlmA5qXk34sPo0Lf14KNZJGff49EJZS6nV4 pni3Wfo9Fkj5CPvmyoM3x4Tw4t/rDSKOC01be1cpkyc9RZoXb9+uq8eWzZrqYu08p7ZQ ipet9pkAIawtOWS0dwZhwH++lBH9S18QYdz5JD5O/tRb+Oo0WoVEkSF9dc6ToErEAUwj nzQMndC/I3W76z8iA3TA5mjoNTHnEtnmZdRS0la+C1wUhORJGLB+T/Bd5KHW2JOZojD3 xVa8Yq+R9UTzqUSeyGuJBzV28nDGNrq6Xft3Pwm9OhmiZNDLu/leUxlfSXfyVdU3eHO7 0UlQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=Fwd/GQaC5Var2VRAIYUUNIOAcVCAPS6jdx6HEHhIjcY=; fh=rCqKS5Wgxyvu9nhWEkn7CU+OTiPpAXAWtEe1ogm7zOc=; b=nSnsrqEvzY3pv0XQ7g/HbnJMjfNnFkdxyd2acESSLEFTV5EcaiDATaz3qoylO9r+pG I9/lAKTR4OCYjnQXdvJoteEXulsH0u2W/4pYt74PuAh3ey5V6MANrFSYbQKoDQFaxh9O KkuZBSecHBVbWAvzc/a2B/lYBs9nsry0Ui9GBSMOOqv34riy5WLDx6vij7O8ZGYZZyTQ 8o1Hhdpf5SRDYsBhquBUWLPu9R1WKkfkvbYtdoIfjAga3ogFAU4B3daZHr9gAfEDm+Dg c2VZMEjfW1Gc6JlvtP+OHXzyHJNZUFBiV1I6PiakEAd+yv7Jj9vkWRlO1tbSlFQObTL3 PSfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20221208.gappssmtp.com header.s=20221208 header.b=Brd8oVXG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gl5-20020a170906e0c500b00997c7157a03si3550143ejb.642.2023.08.11.06.58.32; Fri, 11 Aug 2023 06:58:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20221208.gappssmtp.com header.s=20221208 header.b=Brd8oVXG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236260AbjHKL5V (ORCPT + 99 others); Fri, 11 Aug 2023 07:57:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235934AbjHKL5R (ORCPT ); Fri, 11 Aug 2023 07:57:17 -0400 Received: from mail-vs1-xe34.google.com (mail-vs1-xe34.google.com [IPv6:2607:f8b0:4864:20::e34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 67D8330FF for ; Fri, 11 Aug 2023 04:57:15 -0700 (PDT) Received: by mail-vs1-xe34.google.com with SMTP id ada2fe7eead31-447805165e9so827973137.1 for ; Fri, 11 Aug 2023 04:57:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20221208.gappssmtp.com; s=20221208; t=1691755034; x=1692359834; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Fwd/GQaC5Var2VRAIYUUNIOAcVCAPS6jdx6HEHhIjcY=; b=Brd8oVXG1yHwigSlT4kv+EMSbguhXvboqzLE7ncaOnEpxbR9cJqg2EZSkA/GvDdHLj MidfJcuJvgGDQKyg6fx4MTw3zj09XzSaTjZk/fwODibCS3/AFpjgaAyR+yCVx4x/mHrp hSHRVNbom1ncvwC0reF3Y6U6L4nuWiKxEGHPIUrlaX+xfTeCmVImxopmz/f3uwOnvuNz lCXndhDA4ciTxvLZpoc7nlq4uH/TPweQxPDVWKn2gM37YGPoBJ4yUYO60cn8UktLoYgq n55AeNTMgIXRdF09L4ZPaUfs9MahczMYwENwtBC41Wt80JRTKlDhghh4AmspgS5gI3BO +TKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691755034; x=1692359834; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Fwd/GQaC5Var2VRAIYUUNIOAcVCAPS6jdx6HEHhIjcY=; b=XMBW2UZjoCWm5bH+lWHyEJPYVagjhCodggvUKECjfIbeJMyNdV2+oR5srQtPBoV7LY JbZZtDivE8uwBHKL+P8bXg95WYlUMWHhqLsIuagXC1zerc1V4jYheZphzTLKxKiGPj0e Gz+zc/VqhoHJc+z1HDPYCVY9t+KiRvjIujpMN7cSpMZv9a6gynKLpUwd2P8pINWVdP9J 72caxByupOXGIANUElknehdSBukJ6lNpP0Ie26Us84O2tR4DSzVpNQy5i5cdgYGAwgH5 av0dIP0SrQBSbzvDVukc9lPk/uPYWJQ9atX+/aDcXX2JQpdwRyQKjP6qyJsTnarxZyBf ltKg== X-Gm-Message-State: AOJu0YyUxiJFeGFNcWiDrKs1KEm5kSKxRSTM0wttR/KW/64QhZy205pg LkGXHW+y0KA9ca1qgEfoE1g0BHwrVpgFksuMuEnaow== X-Received: by 2002:a67:f753:0:b0:443:6457:101 with SMTP id w19-20020a67f753000000b0044364570101mr1311370vso.7.1691755034555; Fri, 11 Aug 2023 04:57:14 -0700 (PDT) MIME-Version: 1.0 References: <20230810184846.22144-1-brgl@bgdev.pl> In-Reply-To: <20230810184846.22144-1-brgl@bgdev.pl> From: Bartosz Golaszewski Date: Fri, 11 Aug 2023 13:57:03 +0200 Message-ID: Subject: Re: [PATCH v2] gpiolib: fix reference leaks when removing GPIO chips still in use To: Linus Walleij , Andy Shevchenko , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Aug 10, 2023 at 8:48=E2=80=AFPM Bartosz Golaszewski = wrote: > > From: Bartosz Golaszewski > > After we remove a GPIO chip that still has some requested descriptors, > gpiod_free_commit() will fail and we will never put the references to the > GPIO device and the owning module in gpiod_free(). > > Rework this function to: > - not warn on desc =3D=3D NULL as this is a use-case on which most free > functions silently return > - put the references to desc->gdev and desc->gdev->owner unconditionally > so that the release callback actually gets called when the remaining > references are dropped by external GPIO users > > Signed-off-by: Bartosz Golaszewski > --- > v1 -> v2: > - add a comment about why we can't use VALIDATE_DESC_VOID() > > drivers/gpio/gpiolib.c | 16 +++++++++++----- > 1 file changed, 11 insertions(+), 5 deletions(-) > > diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c > index 251c875b5c34..2158067c4fd2 100644 > --- a/drivers/gpio/gpiolib.c > +++ b/drivers/gpio/gpiolib.c > @@ -2167,12 +2167,18 @@ static bool gpiod_free_commit(struct gpio_desc *d= esc) > > void gpiod_free(struct gpio_desc *desc) > { > - if (desc && desc->gdev && gpiod_free_commit(desc)) { > - module_put(desc->gdev->owner); > - gpio_device_put(desc->gdev); > - } else { > + /* > + * We must not use VALIDATE_DESC_VOID() as the underlying gdev->c= hip > + * may already be NULL but we still want to put the references. > + */ > + if (!desc) > + return; > + > + if (!gpiod_free_commit(desc)) > WARN_ON(extra_checks); > - } > + > + gpio_device_put(desc->gdev); > + module_put(desc->gdev->owner); > } > > /** > -- > 2.39.2 > Queued for fixes. Bartosz