Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp3481438ybb; Mon, 23 Mar 2020 01:46:32 -0700 (PDT) X-Google-Smtp-Source: ADFU+vvNpBt+GL2tgozTUm7HW8T+43leHazy59Xa3FZhLEnBI64aInYAKMAlMQVNqkBBfdFqA112 X-Received: by 2002:aca:47c8:: with SMTP id u191mr16954722oia.170.1584953192289; Mon, 23 Mar 2020 01:46:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584953192; cv=none; d=google.com; s=arc-20160816; b=Vx6oOro8TJrbhFFEepy8Jp/kFQjVZJDa42mlKWSapHlnYF5pu9tgt25sLVNg6428a8 ebsMEoHBVrJif1TbFkoeSB8bnNXtNCHCGvaPyKQJi5xvXAdUpYHMYKswxmCbwAgf1yJj Q3aorMYyNj+MFbJDijezTi+VfKOKGDhTWhBClE/xUTnHlXnvli+YD8TIhwg8AfJDuDRL iPrOyQ4ezZqA7MMfzo2UNnJGi4Y8vuB2NZJb5bze9DzlT0P/Prxsb201Qyaw/PdqW++a 84cCRu94s0AhQedIIRPpvsypQFORoko/zUsXDxNtvy2IcARB7tEirI3HShh+WGWsr+8j zEtQ== 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=8zPLvGcYQo1o0IQb+SkUkD1MBg65ZhQ3XSlCwhyWfpI=; b=CnOOptxrb7rC6NdStwMImXEn1EBZO36Ufw7Gl08Ex//Sbz6/3slIkBDf2IIebMn6iS IMZbO1yl0ahZjBo2VvdrkmO9ANSdaV9UGfzGji/BqNq5TRO/ywN4IJWc++J4+ysCyxQY oSsEfP+YJVKJH5aIRHSG046JU3YUEqX/7YW8b+eK8nMNGJ402UTGERfzUxfTEQWlAkQX X+udzDq4iRtOEh0+GiRB26fRSzNB6yzj0aWdX1k76PZXyqXeMtwiImtSCVSGuK4XxGlp kPouqcooALgDD/KHckVRLvHQJC5shN/wlVzn8c/oXejv5lO0LyB5W2DrSX/CvBh6nIXd mx7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=Ua25DunD; 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 m81si7125257oig.190.2020.03.23.01.46.19; Mon, 23 Mar 2020 01:46:32 -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=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=Ua25DunD; 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 S1727597AbgCWIo6 (ORCPT + 99 others); Mon, 23 Mar 2020 04:44:58 -0400 Received: from mail-io1-f68.google.com ([209.85.166.68]:42117 "EHLO mail-io1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727569AbgCWIo6 (ORCPT ); Mon, 23 Mar 2020 04:44:58 -0400 Received: by mail-io1-f68.google.com with SMTP id q128so13228124iof.9 for ; Mon, 23 Mar 2020 01:44:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=8zPLvGcYQo1o0IQb+SkUkD1MBg65ZhQ3XSlCwhyWfpI=; b=Ua25DunDi0MeBVuRXcr6VxR75Zm8tN3mOIgQLv7SVFyOMiQTGlLwB2NhjD5hHMyGh0 jgbJY8Kv2wMMvllY/0rD5aG71MJobqW61gI5zuPRyiYBq6RN9RhoFIEInxws/vuFougT m/FLLRIBW1noWeJQ+8SFmGYFFLbrveNGrvdotyOtaM840t/6npJwkZvNMkqWP6Eo12Q4 P7lpEQYGPmYNJisG/Rb/DbJraeU/vv11KnJDPi+LCNrWQiUE9eEr56RXwk27WCP2e5V1 QI1E06abQhbNtKpYYxUUwilUbd9UGJyPYuxk4wDHfg5Llgh8rTn7ZE4Ft/eMN5uLy3oV nuKg== 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=8zPLvGcYQo1o0IQb+SkUkD1MBg65ZhQ3XSlCwhyWfpI=; b=BsfOKHQR7FJbyAa3COpRsetkFHuxGqgCPL9JtTMevnu3qLF5PbetmDv14kb9jeiTlI m06wFB06/4jQAMfwZosKpJCetozXLJwgYFqz208pV2kAENWTZTtM+IVUeJdfJtt4xmMY PuiEq/TRP3sZgTb4P6UzuEyM+3uGGpTjkdaW1NDtjf3p3Fj64RDIsKRoIRaDtjISgOgk Q9vvhRi4NBFLXtggL0RlDXOT9jBAsa5jBJsuyEPtXD6CHlBqaVWu3CjwpH+vrSnATtJf KT4koznPnYBX2T4r1Kf+CNaHE2/roFumHLJXOV8aIGLqpIB6k+53HxBX9fbtevV6E2aU NEhg== X-Gm-Message-State: ANhLgQ2eGjC6uKM6IATUo0mitTVFoMJ+xw9R85qpZf6oqlfM4S948Bbb RtGCRWxXaeIxNIBu2Xy6Ie8aE7elq3e28SIO15NWTQ== X-Received: by 2002:a5e:8204:: with SMTP id l4mr18406002iom.31.1584953095899; Mon, 23 Mar 2020 01:44:55 -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: Mon, 23 Mar 2020 09:44:45 +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 pt., 13 mar 2020 o 16:04 Bartosz Golaszewski napisa=C5=82(a= ): > > pt., 13 mar 2020 o 09:44 Linus Walleij napisa= =C5=82(a): > > > > On Thu, Mar 12, 2020 at 7:25 PM Bartosz Golaszewski > > wrote: > > > > > 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(). > > > > Yeah you're right, I even point that out in my second letter :/ > > > > It's a bit of confusion for everyone (or it's just me). > > > > No, it is confusing and I only recently understood it while trying to > fix a memory leak in nvmem. > > > > 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(). > > > > Right. At the end of gpiochip_remove(): > > > > cdev_device_del(&gdev->chrdev, &gdev->dev); > > put_device(&gdev->dev); > > > > That last put_device() should in best case bring the refcount > > to zero. > > > > So the actual way we lifecycle GPIO chips is managed > > resources using only devm_* but the reference count does work > > too: reference count should normally land at zero since the > > gpiochip_remove() call is ended with a call to > > put_device() and that should (ideally) bring it to zero. > > > > It's just that this doesn't really trigger anything. > > > > Not necessarily - if the new kref for GPIO device would be detached > from device reference counting and what it would trigger at release is > this: > > cdev_device_del(&gdev->chrdev, &gdev->dev); > put_device(&gdev->dev); > > Or to be even more clear: "getting" the gpiodevice would not be the > same as "getting" a device - in fact only when the gpiodevice kref > goes down to 0 do we put the reference to the device object. > > > I think there is no way out of the fact that we have to > > forcefully remove the gpio_chip when devm_* destructors > > kicks in: the driver is indeed getting removed at that > > point. > > > > There does seem to be a way around that though: the clock framework > does it by creating a clock "core" object which is reference counted > and if the provider is removed while consumers still hold references > to it, then it does a couple things to "numb" the provider (as you > nicely put it) like replacing all ops callbacks with NULL pointers but > keeps the structure alive until the consumers also give up all their > references. > > That being said: I'm not saying this is necessary or even useful. I > started the discussion because I was under the impression I wasn't > clear enough when writing about reference counting for descriptors. If > nobody complains about the current implementation then let's not fix > something that's not broken. > > Bartosz > > > In gpiochip_remove() we "numb" the chip so that any > > gpio_desc:s currently in use will just fail silently and not crash, > > since they are not backed by a driver any more. The descs > > stay around until the consumer releases them, but if we probe the > > same GPIO device again they will certainly not re-attach or > > something. > > > > Arguably it is a bit of policy. Would it make more sense to > > have rmmod fail if the kref inside gdev->dev->kobj->kref > > is !=3D 1? I suppose that is what things like storage > > drivers pretty much have to do. > > > > The problem with that is that as soon as you have a consumer > > that is compiled into the kernel it makes it impossible to > > remove the gpio driver with rmmod. > > > > I really needed to refresh this a bit, so the above is maybe > > a bit therapeutic. > > > > I don't really see how we could do things differently without > > creating some other problem though. > > > > Yours, > > Linus Walleij Hi Linus, what is your decision on this? Because if we don't merge this, then we need to make sure nvmem doesn't call gpiod_put() for descriptors it didn't obtain itself and we should probably fix it this week. Bart