Received: by 2002:a05:7412:798b:b0:fc:a2b0:25d7 with SMTP id fb11csp280425rdb; Thu, 22 Feb 2024 03:39:59 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXq2sYVsN6QKZnQKjGjyTsS7+RVzern4ol3P5foHDx2eYzA2qQDf7E2X4aOaVRXoqsadl5skvNp+MFoTD5epdlB5NT0UW0iLUpVDoR1VQ== X-Google-Smtp-Source: AGHT+IEjN5JKHsssNTWKL9b0srcnQutTa3M7ibDpWEZYzDYrn9OH+PNd03IB2zdgK5Ieu2V5N+CT X-Received: by 2002:a05:6a20:c886:b0:19e:a3cc:2496 with SMTP id hb6-20020a056a20c88600b0019ea3cc2496mr26573454pzb.10.1708601999057; Thu, 22 Feb 2024 03:39:59 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708601999; cv=pass; d=google.com; s=arc-20160816; b=HvQZf2a+w99P4RKYbvaFusJ77V4NFckixGctJnw8biI/uLz/JtXT4hS4ngYnLUtlv+ SBdcymYb3spb514qPmQSe4YQRek0+KibJODiznUGUsd0FiA/mkd1wstodUum2tuf8+tL rozXaW2JcQuBdCo+eStDraC16uGTAbJBQKkybYbkdM73ef/tYknNHZEVowyfX/dr59eY tR/iiqAd5MuIxDPpaxyNm6VKA4COpyY+RVQ7YJBZasqWlUOz4A89ZniaIpYRbHBkTs8O AWmWlTp+J59EzG7eP48GxfRhe/1ijRhLoVLIgHWXALtnLNyf8YcRkIo71xKMSpRRWN7O hRCg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:organization:references :in-reply-to:message-id:subject:cc:to:from:date:dkim-signature; bh=maOFycEoHJZpXjV4st8F3wfV32IoWjfu72XKDbdjfwo=; fh=+IN/Jj8g5fMKLP5Z7aRnBmk3pA1iQt04JsgDdZeWthU=; b=zutdnm/njEk8xdwI1Upc+3b2dRN11BXct/xjKUWwuL1nc/ZzUZAgSCAeJtyH4xCCBe XoFpWtZiKq9j22WdQhibT3HayvLEDVtnDtVWr6I9cZfg9n+tmzTXNiMH5MPVhL9112G6 IKMS4PbX9MY0YJ94IzD/pOo00UPrZsKn9A7LH6Cy/uzcp7Cnq/g9MCOKh77i48WcA0Vq eprmhtg8qZXcGjuYRrpXWKG7mmVDaAuqWBtqn6R9Cvuw7E/ofXwpG5/gnpN7acPYSaiH z7CBm5CZNs4LGX1yMBiBn60ESB7FPS5QVIvQ7EAFNEf34rTR4SZQi0KzLge00bOfeiWH grbA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=hVPCwmQS; arc=pass (i=1 spf=pass spfdomain=bootlin.com dkim=pass dkdomain=bootlin.com dmarc=pass fromdomain=bootlin.com); spf=pass (google.com: domain of linux-kernel+bounces-76436-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-76436-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id i12-20020aa78b4c000000b006e470d7dcb4si5983383pfd.369.2024.02.22.03.39.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 03:39:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-76436-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=hVPCwmQS; arc=pass (i=1 spf=pass spfdomain=bootlin.com dkim=pass dkdomain=bootlin.com dmarc=pass fromdomain=bootlin.com); spf=pass (google.com: domain of linux-kernel+bounces-76436-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-76436-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id C28542844EA for ; Thu, 22 Feb 2024 11:39:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 13A2D47A55; Thu, 22 Feb 2024 11:36:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="hVPCwmQS" Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 32AEB405D4; Thu, 22 Feb 2024 11:36:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.197 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708601787; cv=none; b=b9ROeWdo0Dz5NEp+mjS1xwFHL1RYGQJ2zmSZeo1hNsex3zxQ8kHnaiM2avdVico07ax9E6bbOMM+hu9PPjp9pIzIkyBbCZ5JbXMj57AtEAbKXdArV1YI+ur75iYJIGtlYGjw4Wgwor00BFy1BXCcXy80iSdphhV+/GUcOj/0Bwk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708601787; c=relaxed/simple; bh=Nq5AC/M/SyoGFJBpxXGvnqX2nW9FIGfWcanhZYMjTio=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=hYIXuPYjEw7YdlAxODYWfcy/oi/q/7SAeHy29r0u4di4L38e2uMIZkEHToK+J0fd9WP/yg2m7yyJEJ9d8tUaBjHrw1GkKNmMoQRNtLCeXPHFrH3IJ45mov8ddBa6QjHeFiTufvIeIy3coRhShLwhlR+AuWNqYnX0/c5AtoQahHk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=hVPCwmQS; arc=none smtp.client-ip=217.70.183.197 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Received: by mail.gandi.net (Postfix) with ESMTPSA id EF9471C0003; Thu, 22 Feb 2024 11:36:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1708601776; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=maOFycEoHJZpXjV4st8F3wfV32IoWjfu72XKDbdjfwo=; b=hVPCwmQSoeoUKbWUs1Ur9iVK0qZEx/otlw7f6S/vyZcezk6GhfjjgCPYREND+Z0VzugqP2 rn2BMSz4GgdJ5WOkp+bCNP8sYEpwjP+kfBdY7KCTcvzXMrw7IXetqpZJOaDVtZw97oH3mu ZAKscrCcb7PpkbbMRRnZQS/HMTdU7gNvjdbOuusrumIaUn1lkqLnT1eE8iza+8MkM0ZP+v 4T3F9mewzRPWMQ1g0UqcZ1wB9Y7ieo/Nb43z6VddHRfkGiDQR29wvTS8Qcpzz6xHqCODTt cHFwVUGXGQVCTpWzX6oZQrHG8Gs1xBsLxT76XlZma2JiaHwv6b9Ry/4kdrhcUg== Date: Thu, 22 Feb 2024 12:36:15 +0100 From: Herve Codina To: Bartosz Golaszewski Cc: Kent Gibson , Linus Walleij , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Luca Ceresoli , Thomas Petazzoni , Saravana Kannan Subject: Re: [PATCH 2/2] gpiolib: cdev: release IRQs when the gpio chip device is removed Message-ID: <20240222123615.2cbada98@bootlin.com> In-Reply-To: References: <20240220111019.133697-1-herve.codina@bootlin.com> <20240220111019.133697-3-herve.codina@bootlin.com> <20240220142959.GA244726@rigel> <20240222005744.GA3603@rigel> <20240222010530.GA11949@rigel> Organization: Bootlin X-Mailer: Claws Mail 4.2.0 (GTK 3.24.38; x86_64-redhat-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-GND-Sasl: herve.codina@bootlin.com Hi Bartosz, On Thu, 22 Feb 2024 00:31:08 -0800 Bartosz Golaszewski wrote: > On Thu, 22 Feb 2024 02:05:30 +0100, Kent Gibson said: > > On Thu, Feb 22, 2024 at 08:57:44AM +0800, Kent Gibson wrote: > >> On Tue, Feb 20, 2024 at 10:29:59PM +0800, Kent Gibson wrote: > >> > On Tue, Feb 20, 2024 at 12:10:18PM +0100, Herve Codina wrote: > >> > >> ... > >> > >> > > } > >> > > > >> > > +static int linereq_unregistered_notify(struct notifier_block *nb, > >> > > + unsigned long action, void *data) > >> > > +{ > >> > > + struct linereq *lr = container_of(nb, struct linereq, > >> > > + device_unregistered_nb); > >> > > + int i; > >> > > + > >> > > + for (i = 0; i < lr->num_lines; i++) { > >> > > + if (lr->lines[i].desc) > >> > > + edge_detector_stop(&lr->lines[i]); > >> > > + } > >> > > + > >> > > >> > Firstly, the re-ordering in the previous patch creates a race, > >> > as the NULLing of the gdev->chip serves to numb the cdev ioctls, so > >> > there is now a window between the notifier being called and that numbing, > >> > during which userspace may call linereq_set_config() and re-request > >> > the irq. > >> > > >> > There is also a race here with linereq_set_config(). That can be prevented > >> > by holding the lr->config_mutex - assuming the notifier is not being called > >> > from atomic context. > >> > > >> > >> It occurs to me that the fixed reordering in patch 1 would place > >> the notifier call AFTER the NULLing of the ioctls, so there will no longer > >> be any chance of a race with linereq_set_config() - so holding the > >> config_mutex semaphore is not necessary. > >> > > > > NULLing -> numbing > > > > The gdev->chip is NULLed, so the ioctls are numbed. > > And I need to let the coffee soak in before sending. > > > >> In which case this patch is fine - it is only patch 1 that requires > >> updating. > >> > >> Cheers, > >> Kent. > > > > The fix for the user-space issue may be more-or-less correct but the problem is > deeper and this won't fix it for in-kernel users. > > Herve: please consider the following DT snippet: > > gpio0 { > compatible = "foo"; > > gpio-controller; > #gpio-cells = <2>; > interrupt-controller; > #interrupt-cells = <1>; > ngpios = <8>; > }; > > consumer { > compatible = "bar"; > > interrupts-extended = <&gpio0 0>; > }; > > If you unbind the "gpio0" device after the consumer requested the interrupt, > you'll get the same splat. And device links will not help you here (on that > note: Saravana: is there anything we could do about it? Have you even > considered making the irqchip subsystem use the driver model in any way? Is it > even feasible?). > > I would prefer this to be fixed at a lower lever than the GPIOLIB character > device. I think this use case is covered. When the consumer device related to the consumer DT node is added, a consumer/supplier relationship is created: parse_interrupts() parses the 'interrups-extended' property https://elixir.bootlin.com/linux/v6.8-rc1/source/drivers/of/property.c#L1316 and so, of_link_to_phandle() creates the consumer/supplier link. https://elixir.bootlin.com/linux/v6.8-rc1/source/drivers/of/property.c#L1316 We that link present, if the supplier is removed, the consumer is removed before. The consumer should release the interrupt during its remove process (i.e explicit in its .remove() or explicit because of a devm_*() call). At least, it is my understanding. Best regards, Hervé