Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp804430ybh; Thu, 12 Mar 2020 11:26:02 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtVk963aJI+7k5yssppp4rQZ64bB7VX2SjNJVYhsfD56UZgLIiv0ka3/56BkZqLoeFRRl/0 X-Received: by 2002:a9d:5e04:: with SMTP id d4mr7316385oti.36.1584037562768; Thu, 12 Mar 2020 11:26:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584037562; cv=none; d=google.com; s=arc-20160816; b=qMvapQMWVef/Ktf1374dSVVNLJsA0orVa644q7YBFIwvtAj9a2tbnrcUyXLN5ef7aI o1P/ORhh+OIwEaKT3nXG+5ksRaCt3TXLr3+Nuu92aHYrT0F54rNu7EqesiFFVoDnPGPa bAl6Zr1ZGy4DvF0FZ4ljYo4zThaDvhhy8Se+sKFom90/mcjIE6OW0DqMRp3X+oUuNKaQ cij6BLA4U1Yu99eBAXh0PdiKrbGRwWhaZZwy90b1FxLbRkCa+NKAE1UX5rpJi5kI/pQw 5MpSXNnGL+FtEN2Kyh+mwa3dyL1VH8aS0pYx6r+MEd0HWVjuZLEovgCre1kIym38gFvv yZag== 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=Jf7CCJlaBzBWuwy+AxN+gg+jjj5mPJjuMgQyXl2F5To=; b=UCyDNi0rZJOD0JBCKRBXS9kjej1gbL8iHeTRQPvsXMDZ/rCSKnUbsmmDP60tG7LKyB Zgke5jUxKeXuubBBRNpk/R9HMQqKRO9h8fSsMgJ8hIYVEPkLnpe9WcilrHd2KwxgOGlL 18FRVX8WY/f54npKqb1y8uQab6C8iIy8azMsuUm6ztMqzRgaF2DP+abt4fH13K26MTWU D1m2/SfxXnE7FGQKV42qqb132TFt1sek/Y2RC6vU6Te3ua3pusJOoBwht0sndJlW5d21 /3ci4zX1T/4hI3Sw9n1duB8Bv0zKrI10xRAUYcBRRti5ueiK47zcCesJaVIvsvejZ2GW lwkg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=dMld6cRI; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i8si3231772oib.81.2020.03.12.11.25.49; Thu, 12 Mar 2020 11:26:02 -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=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=dMld6cRI; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726752AbgCLSZK (ORCPT + 99 others); Thu, 12 Mar 2020 14:25:10 -0400 Received: from mail-qt1-f195.google.com ([209.85.160.195]:42833 "EHLO mail-qt1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726569AbgCLSZJ (ORCPT ); Thu, 12 Mar 2020 14:25:09 -0400 Received: by mail-qt1-f195.google.com with SMTP id g16so5179525qtp.9 for ; Thu, 12 Mar 2020 11:25:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=Jf7CCJlaBzBWuwy+AxN+gg+jjj5mPJjuMgQyXl2F5To=; b=dMld6cRIv553ZXuXvnx1/9oXxRUJQVhYQt01RJrga16eD9ADF5ho5x7vzM4Db/aHdG +DmeShswgIdAanewpRonn6j3yBEUUSoTOlfx4iMa3nFX8Prgqh15+5c/CDgKro1CFjZ3 Aj+Nm1GSCTqfhSJKnlpdPUBzKbhgzr+h3YbEEYjiY3y26sbAQqPxfp1Z2wjmNVyFvwsC jyxDjn/wZ1pGXSVD7ByXmgAkhFdX3NdyskFUfArocC35/y+7OMb2J0O6fiamvjFnGUms cQ/DJ2LHDmuuG5YNQ1Q3lRIGIBddWrY+pL7rv+drHVDkU5yfhF/CPbw66l54yhxNL2OG LvZQ== 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=Jf7CCJlaBzBWuwy+AxN+gg+jjj5mPJjuMgQyXl2F5To=; b=TeaKgKLRcgrKN2dyRHFJVuvay4XOHEgmnrfis+DmOaT96dqyzKVSVX0fwgwW5mInAz InB2oLA7WInXh29rPOG4/+GRisHDPwDUJsgllETS0hDTSCXtTcF9LWlwzAmK3qJIiu61 dekvDOhVw5n7dr7H+1H+djEYd5RpGP8q38YQkwC3y/HEfMmLOhhnu2PST4rP77Dtc5ef 9OwJCHZxM3KHeODkQ8tuT3JcS0OGB8FkF6BBQK/fkUg5q951Byr/T0w/aGqyAeZAGcYE QmYMbicG2XsQn7Hvu/u0bZ4C8HdAEgkolIVvyp4JciG86JjEIVcIh9YczOLKuzYf1rCy V3rg== X-Gm-Message-State: ANhLgQ06fXciTGivyA+q3JKVLESDMTQpOOhwfwIeggwBhxTcRVsz4Hyu UCeJYqdpQ60Xt7XBCyTuRCsaXcx4dyNDAKlAnAF9Yw== X-Received: by 2002:ac8:5208:: with SMTP id r8mr8806874qtn.131.1584037508177; Thu, 12 Mar 2020 11:25:08 -0700 (PDT) MIME-Version: 1.0 References: <20200224094158.28761-1-brgl@bgdev.pl> <20200224094158.28761-3-brgl@bgdev.pl> In-Reply-To: From: Bartosz Golaszewski Date: Thu, 12 Mar 2020 19:24:54 +0100 Message-ID: Subject: Re: [PATCH 2/3] gpiolib: use kref in gpio_desc To: Linus Walleij Cc: Bartosz Golaszewski , "open list:GPIO SUBSYSTEM" , Linux Kernel Mailing List , Srinivas Kandagatla , Geert Uytterhoeven , Khouloud Touil 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 czw., 12 mar 2020 o 11:11 Linus Walleij napisa= =C5=82(a): > > On Thu, Mar 5, 2020 at 5:49 PM Bartosz Golaszewski wrote: > > > I refreshed my memory on device links and reference counting. I think > > that device links are not the right tool for the problem I'm trying to > > solve. > > OK, just check the below though so we are doing reference > counting in the right place. > > > You're right on the other hand about the need for reference > > counting of gpiochip devices. Right now if we remove the chip with > > GPIOs still requested the only thing that happens is a big splat: > > "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED". > > > > We should probably have a kref on the gpiochip structure which would > > be set to 1 when registering the chip, increased and decreased on > > every operation such as requesting and releasing a GPIO respectively > > and decreased by gpiochip_remove() too. That way if we call > > gpiochip_remove() while some users are still holding GPIO descriptors > > then the only thing that happens is: the reference count for this > > gpiochip is decreased. Once the final consumer calls the appropriate > > release routine and the reference count goes to 0, we'd call the > > actual gpiochip release code. This is similar to what the clock > > framework does IIRC. > > I don't think that is consistent with the device model: there is already > a struct device inside struct gpio_device which is what gets > created when the gpio_chip is registered. > > The struct device inside struct gpio_device contains a > struct kobject. > > The struct kobject contains a struct kref. > > This kref is increased and decreased with get_device() and > put_device(). This is why in the gpiolib you have a bunch of > this: > get_device(&gdev->dev); > put_device(&gdev->dev); > > This is used when creating any descriptor handle with > [devm_]gpiod_request(), linehandles or lineevents. > > So it is already reference counted and there is no need to > introduce another reference counter on gpio_chips. > I think there's one significant detail missing here. While it's true that the life-time of a device object is controlled by its reference count, its registration with the driver model is not ie. device_add/del() are called once per device as opposed to get/put_device(). > The reason why gpiochip_remove() right now > enforces removal and only prints a warning if you remove > a gpio_chip with requested GPIOs on it, is historical. > Given the above I think that it wouldn't be possible to add reference counting to gpio devices without a new kref if the task of the release callback would be to call device_del() once the provider called its unregister function and all consumers released requested resources. > When I created the proper device and char device, the gpiolib > was really just a library (hence the name) not a driver framework. > Thus there was no real reference counting of anything > going on, and it was (as I perceived it) pretty common that misc > platforms just pulled out the GPIO chip underneath the drivers > using the GPIO lines. > > If we would just block that, say refuse to perform the .remove > action on the module or platform (boardfile) code implementing > GPIO I was worried that we could cause serious regressions. > > But I do not think this is a big problem? > > Most drivers these days are using devm_gpiochip_add_data() > and that will not release the gpiochip until exactly this same > kref inside struct device inside gpio_device goes down to > zero. > I believe this is not correct. The resources managed by devres are released when the device is detached from a driver, not when the device's reference count goes to 0. When the latter happens, the device's specific (or its device_type's) release callback is called - for gpiolib this is gpiodevice_release(). The kref inside struct device will not go down to zero until you call device_del() (if you previously called device_add() that is which increases the reference count by a couple points). But what I'm thinking about is making the call to device_del() depend not on the call to gpiochip_remove() but on the kref on the gpio device going down to zero. As for the protection against module removal - this should be handled by module_get/put(). I may be wrong of course but when looking at the code, this is what I understand. Please let me know what you think. Best regards Bartosz Golaszewski