Received: by 2002:a17:90a:1609:0:0:0:0 with SMTP id n9csp2356459pja; Thu, 26 Mar 2020 13:51:29 -0700 (PDT) X-Google-Smtp-Source: ADFU+vt2YgkNjtsmZHJcSuiBxeYK+2HwailONg2TOv3TVIiLZvIp8s79dY9moHm60DKSDZSxjCVV X-Received: by 2002:a05:6808:aaa:: with SMTP id r10mr1613234oij.111.1585255889831; Thu, 26 Mar 2020 13:51:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585255889; cv=none; d=google.com; s=arc-20160816; b=cQTI4uibM1La1iJSrQo0HY75UxjWv3b7wo1FwfA/BEG35DVLGghKLl0paritRkysfV 5ClnA44H2hK4X2QBdxQ/0l+IjLjwkaGswepOex7FrRCSOgb5lhSN7zn7DNbuO476ceGJ mMueEY8vJ0ZNdbbh/AlXQb/c11HI3MOCXmEoQudGYQJyfBxlyDp5wzgc289ebu9MSWD4 4VkHSh0jXsPbuI8fKC7QmQj3Gp4BualhCEN6x9RH39AFujpv21KgqiuRK5DYlp5IzOTS jOGP3l4fQ3SCaA3I0vqitgLFyJaWluFqPo1Con7da/dagthoSXaP/g9Vs6symQL8TyFi g+AQ== 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=7rAItXMRvBZm31ysm2z8WUbF9TsTJLp451lXgVpORtI=; b=tDtwW2IaY+/UyrnXqMCSMi+8r5JCDQOKXoEkxe5juCuzJwvGy5u416q68MNxoEj60M AUsAKbtrtl+iR60l3jJ8ZdmKcTTXgttPwIgY156mWvq9T2zZQW7hY7aRVxomPQZUQAob p5gIqUle+Y9PReDrXjoUXtP9wmKzs3YfeJXKly+O5nvZIRcgf7lF2js70QwDhYY4T7Em Euvxg0naii+Uv6EbltACLuYLR6wcxAqHrPRqgd8V84ImA6m3c5JgL/Dsu7fsT8I6I6/2 p1ApF5W3c1iWwbtazRVRWZD7SdhtyApoUbvcS9d0Iu12JSjTLIHVhYhEqunfUi7oWP9+ UV2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=jSpc3zgt; 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=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j206si1559868oia.158.2020.03.26.13.51.16; Thu, 26 Mar 2020 13:51:29 -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=@linaro.org header.s=google header.b=jSpc3zgt; 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=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727443AbgCZUu6 (ORCPT + 99 others); Thu, 26 Mar 2020 16:50:58 -0400 Received: from mail-lj1-f195.google.com ([209.85.208.195]:43313 "EHLO mail-lj1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726422AbgCZUu6 (ORCPT ); Thu, 26 Mar 2020 16:50:58 -0400 Received: by mail-lj1-f195.google.com with SMTP id g27so7908396ljn.10 for ; Thu, 26 Mar 2020 13:50:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=7rAItXMRvBZm31ysm2z8WUbF9TsTJLp451lXgVpORtI=; b=jSpc3zgtOsuGZiH4Vx5+f4B1qjsyI9zsctUsWSP69CzTAJxMCxojEJdHl61huawuZT 7z16LNUHLvoN0FK3avAUgWI5PRQafFJXvfOXbnfDeTTBt7YxG9nDBsLZ0Je97PwUzZFe q+UUd4mACIn/XJ88T4iqtJwWmwjg/T5/gjhk1Lp52aD0BUtRA5rlgNbU9JhhjjVwGsgR uTZDlwx1b5GTRDOXGcGvvqYISqZkALfdd5C0FVARQQ2mUksAQjgvQqLLNwemVXt5S5Jp 9JAjY/XpcPOKVllugYZ1I+t/N02KizInAewdW4zyZ92DhuaO/5fYhGjJsPUTJYIGtg0n UxFw== 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:content-transfer-encoding; bh=7rAItXMRvBZm31ysm2z8WUbF9TsTJLp451lXgVpORtI=; b=KFViI3AWPeVSuUACI5YBCcSzTN061EOulZBDb8hZQQmYvrNryd5OIyKoUOYp4DQTUv e0riUeAqv8/YxhW+cej3jdZEjl2TA5J/BfS2jMu0MqaiHk/W930t+k/iBYaJOlUoRYDw FKPUt7AwQtNDSvX+UjUJRRUpf0OaE3gYyvXt6osgC73rP3y6dMFuBcRntlJxkvMW0qAO pttayMEVGyXIOl2WZg6XZGwtWdYIMl9PtZ/xcwbF4fUHMz6680tm+atVKT8g5oWsPmrZ GOOCzLEt1BHZldcKEvv6ObO8Z6VVYc3fjxQ8MLsGyi+M7hQ+Uxh+xpOtHLXs50S+XBjL rn6A== X-Gm-Message-State: AGi0PuaC3Gf52cq697QZ357ZFCTV0H4ohQC9YluLbXDWdSi4gBIqwPzV OIQfbwbwhqjEAlufbUh7F3UAfpkhi1Q8teHoB4YeNw== X-Received: by 2002:a2e:9c48:: with SMTP id t8mr496874ljj.168.1585255856054; Thu, 26 Mar 2020 13:50:56 -0700 (PDT) MIME-Version: 1.0 References: <20200224094158.28761-1-brgl@bgdev.pl> <20200224094158.28761-3-brgl@bgdev.pl> In-Reply-To: From: Linus Walleij Date: Thu, 26 Mar 2020 21:50:44 +0100 Message-ID: Subject: Re: [PATCH 2/3] gpiolib: use kref in gpio_desc To: Bartosz Golaszewski Cc: Srinivas Kandagatla , Khouloud Touil , Geert Uytterhoeven , "open list:GPIO SUBSYSTEM" , "linux-kernel@vger.kernel.org" , Bartosz Golaszewski 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, Mar 13, 2020 at 3:47 PM Bartosz Golaszewski wrote: > czw., 12 mar 2020 o 11:35 Linus Walleij napisa= =C5=82(a): > In this case I was thinking about a situation where we pass a > requested descriptor to some other framework (nvmem in this case) > which internally doesn't know anything about who manages this resource > externally. Now we can of course simply not do anything about it and > expect the user (who passed us the descriptor) to handle the resources > correctly. But what happens if the user releases the descriptor not on > driver detach but somewhere else for whatever reason while nvmem > doesn't know about it? It may try to use the descriptor which will now > be invalid. Reference counting in this case would help IMHO. I'm so confused because I keep believing it is reference counted elsewhere. struct gpio_desc *d always comes from the corresponding struct gpio_device *descs array. This: struct gpio_device { int id; struct device dev; (...) struct gpio_desc *descs; (...) This array is allocated in gpiochip_add_data_with_key() like this: gdev->descs =3D kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KE= RNEL); Then it gets free:d in gpiodevice_release(): static void gpiodevice_release(struct device *dev) { struct gpio_device *gdev =3D dev_get_drvdata(dev); (...) kfree(gdev->descs); kfree(gdev); } This is the .release function for the gdev->dev, the device inside struct gpio_device, i.e. the same device that contains the descs in the first place. So it is just living and dying with the struct gpio_device. struct gpio_device does *NOT* die in the devm_* destructor that gets called when someone has e.g. added a gpiochip using devm_gpiochip_add_data(). I think the above observation is crucial: the lifetime of struct gpio_chip = and struct gpio_device are decoupled. When the struct gpio_chip dies, that just "numbs" all gpio descriptors but they stay around along with the struct gpio_device that contain them until the last user is gone. The struct gpio_device reference counted with the call to get_device(&gdev-= >dev) in gpiod_request() which is on the path of gpiod_get_[index](). If a consumer gets a gpio_desc using any gpiod_get* API this gets increased and it gets decreased at every gpiod_put() or by the managed resources. So should you not rather exploit this fact and just add something like: void gpiod_reference(struct gpio_desc *desc) { struct gpio_device *gdev; VALIDATE_DESC(desc); gdev =3D desc->gdev; get_device(&gdev->dev); } void gpiod_unreference(struct gpio_desc *desc) { struct gpio_device *gdev; VALIDATE_DESC(desc); gdev =3D desc->gdev; put_device(&gdev->dev); } This should make sure the desc and the backing gpio_device stays around until all references are gone. NB: We also issue try_module_get() on the module that drives the GPIO, which will make it impossible to unload that module while it has active GPIOs. I think maybe the whole logic inside gpiod_request() is needed to properly add an extra reference to a gpiod else someone can (theoretically) pull out the module from below. Yours, Linus Walleij