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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5C39BC04A6A for ; Tue, 15 Aug 2023 12:58:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237314AbjHOM5k (ORCPT ); Tue, 15 Aug 2023 08:57:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42246 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237185AbjHOM5S (ORCPT ); Tue, 15 Aug 2023 08:57:18 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5755FE63; Tue, 15 Aug 2023 05:57:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1692104237; x=1723640237; h=date:from:to:cc:subject:message-id:references: mime-version:content-transfer-encoding:in-reply-to; bh=8zXOqYd4DMvfC6c54rZPbDXh6kGCoq7MWUmTzQG7Jmc=; b=fdi7ohbHkoB8DPJL3e4NS4RZsd7vTq9KzBE1cCTRryOe+uJ6K960jbdY HfyUHo8S9Z84iseWJkixP/x126duXSCY4LB7g/Futobcv2f08Pp1his0D vfVp1RrXkpt+aV5ScVVVo7sV0W7BwaOe+7vspBzqgfzQfCTiAf3790I7N vkwLvfUgnp86c/7zT7Aqgld2W1IfQvG0mi8sQSQ8+B6QK2dF8QENIoswo 76HPkJrhT2o5jNwe9KuZaVcl3CJ3bMiBVLLIyNhB126BguQ7Hs0gSuBjB PS2/ew1Pm0n3Gmf4IV5JDmApMIM8bDqVqvdDrYajK0FjQ46NEZVH22Kfh g==; X-IronPort-AV: E=McAfee;i="6600,9927,10803"; a="375039202" X-IronPort-AV: E=Sophos;i="6.01,174,1684825200"; d="scan'208";a="375039202" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Aug 2023 05:57:16 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10803"; a="848060285" X-IronPort-AV: E=Sophos;i="6.01,174,1684825200"; d="scan'208";a="848060285" Received: from smile.fi.intel.com ([10.237.72.54]) by fmsmga002.fm.intel.com with ESMTP; 15 Aug 2023 05:57:15 -0700 Received: from andy by smile.fi.intel.com with local (Exim 4.96) (envelope-from ) id 1qVtbl-00256d-1c; Tue, 15 Aug 2023 15:57:13 +0300 Date: Tue, 15 Aug 2023 15:57:13 +0300 From: Andy Shevchenko To: Linus Walleij Cc: Bartosz Golaszewski , Kent Gibson , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: Re: [PATCH v3] gpiolib: fix reference leaks when removing GPIO chips still in use Message-ID: References: <20230811193034.59124-1-brgl@bgdev.pl> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Aug 15, 2023 at 01:40:22PM +0200, Linus Walleij wrote: > On Tue, Aug 15, 2023 at 11:50 AM Andy Shevchenko > wrote: > > On Fri, Aug 11, 2023 at 09:30:34PM +0200, 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 == 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 ... > > > - if (desc && desc->gdev && gpiod_free_commit(desc)) { > > > > The commit message doesn't explain disappearing of gdev check. > > > > > - module_put(desc->gdev->owner); > > > - gpio_device_put(desc->gdev); > > > - } else { > > > + /* > > > + * We must not use VALIDATE_DESC_VOID() as the underlying gdev->chip > > > + * may already be NULL but we still want to put the references. > > > + */ > > > + if (!desc) > > > + return; > > > + > > > + if (!gpiod_free_commit(desc)) > > > WARN_ON(extra_checks); > > > - } > > > + > > > + module_put(desc->gdev->owner); > > > + gpio_device_put(desc->gdev); > > > } > > > > So, if gdev can be NULL, you will get an Oops with new code. > > I read it such that gdev->chip can be NULL, but not gdev, > and desc->gdev->owner is fine to reference? Basically the Q is "if desc is non-NULL, does it guarantee that gdev is non-NULL either?" -- With Best Regards, Andy Shevchenko