Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3709752pxv; Mon, 5 Jul 2021 03:58:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxef8K9Zt2nU8ly3DiJv5Ri2ahyTymT5luOXDXGEU0gdE5zk5/AOgDld5vgUiYSvOpfcZbz X-Received: by 2002:a92:c708:: with SMTP id a8mr10440342ilp.177.1625482739404; Mon, 05 Jul 2021 03:58:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625482739; cv=none; d=google.com; s=arc-20160816; b=mgz3AzomKorBLsBBW0jPjmIA4b6yX8I7hjdeWz5xwLunVrHB9mBwCHT3DGn9To8bcW xJH3jCIjelfWkee8fvWdtak8vV+G8NUaiyJYblBrRCnnQ+1Yrw4+MK+665luaknvR21T e+HjKBwonexNNwxFpRN2AZww413Ds5ZkU4ihdtdKiSkZwHrido6l3ISvDKWhy++2akVG Ij0MAvEDyCbF8tK5sjE8YWBW7e0WhWw1b09qmDpSZdhdTopw4jUJhsyCOyxpsNr8uken OUOcYKj6f7oyKqM8E4+OmGGuEJYtQn03OhY8E5Tp9U5Km6WWYtH8jz77Dl18v74d0+Od bstw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=IeeGV8hz1/HY5ePcvNujXHNx73FXZgIuQn2XIYXVS7Q=; b=Kzm+ICC8O1FOqCewsrl5OSRdlmbSA7imActhMvRVCgMvrQTDMnbLi62ZmbDZbtTNpm UCwQZGbTv7151ZAI+iRWyXYBrGXnkFwwJVitdm3MLpr1E3dybJnIwu/26F+ieegsrUBy S9G+J4dpzjkI8HSfjaakvLnoajNZb6da8wracH5Rz+iTGruxdg3fOOPsgA+b+0rkAAac ns59nWuhzYzvplG49c7dqOz3GUz8qOwLKgBQQlCNF6GBR8MItzawvaC3ptIqkZCaU5ig D8Gbfnu07ramRReTLnVISAT1hGloKgZnoh2oq/HU2T+Oc1Ni6LoLLP38hoSzK1l8MvQg VmJQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d6si14148618ilm.75.2021.07.05.03.58.48; Mon, 05 Jul 2021 03:58:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231243AbhGELAy (ORCPT + 99 others); Mon, 5 Jul 2021 07:00:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231181AbhGELAx (ORCPT ); Mon, 5 Jul 2021 07:00:53 -0400 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FF86C061760 for ; Mon, 5 Jul 2021 03:58:16 -0700 (PDT) Received: from dude.hi.pengutronix.de ([2001:67c:670:100:1d::7]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m0MIj-00083W-VX; Mon, 05 Jul 2021 12:58:09 +0200 Received: from ore by dude.hi.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1m0MIi-0001Ri-NR; Mon, 05 Jul 2021 12:58:08 +0200 Date: Mon, 5 Jul 2021 12:58:08 +0200 From: Oleksij Rempel To: Jonathan Cameron Cc: Rob Herring , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Pengutronix Kernel Team , David Jander , Robin van der Gracht , linux-iio@vger.kernel.org, Lars-Peter Clausen , Peter Meerwald-Stadler , Dmitry Torokhov Subject: Re: [PATCH v1 2/2] iio: adc: tsc2046: fix sleeping in atomic context warning and a deadlock after iio_trigger_poll() call Message-ID: <20210705105808.GA7671@pengutronix.de> References: <20210625065922.8310-1-o.rempel@pengutronix.de> <20210625065922.8310-2-o.rempel@pengutronix.de> <20210704185710.07789b8f@jic23-huawei> <20210705035440.35iualr6kkg22n56@pengutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20210705035440.35iualr6kkg22n56@pengutronix.de> X-Sent-From: Pengutronix Hildesheim X-URL: http://www.pengutronix.de/ X-IRC: #ptxdist @freenode X-Accept-Language: de,en X-Accept-Content-Type: text/plain X-Uptime: 12:44:22 up 126 days, 20:19, 125 users, load average: 26.63, 23.43, 16.00 User-Agent: Mutt/1.10.1 (2018-07-13) X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::7 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jul 05, 2021 at 05:54:40AM +0200, Oleksij Rempel wrote: > Hi Jonathan, > > On Sun, Jul 04, 2021 at 06:57:10PM +0100, Jonathan Cameron wrote: > > On Fri, 25 Jun 2021 08:59:22 +0200 > > Oleksij Rempel wrote: > > > > > If iio_trigger_poll() is called after IRQ was disabled, we will call > > > reenable_trigger() directly from hard IRQ or hrtimer context instead of > > > IRQ thread. In this case we will run in to multiple issue as sleeping in atomic > > > context and a deadlock. > > > > Hmm. This sounds like a problem that might bite us in other circumstances. > > > > So do I have the basic issue right in thinking we have a race between > > calling iio_trigger_poll() and having no devices still using that trigger? > > Thus we end up with all of trig->subirqs not being enabled. > > > > There was a previous discussion that the calls to iio_trigger_notify_done() in > > iio_trigger_poll() are only meant to decrement the counter, as the assumption > > was that the calls via threads would always happen later. Unfortunately this > > is all clearly a little bit racy and I suspect not many of the reenable() callbacks > > are safe if they are called in interrupt context. > > > > Perhaps an alternative would be to schedule the reenable() if we hit it from > > that path thus ensuring it doesn't happen in a place where we can't sleep? > > > > Would something like that solve your problem? > > Yes :) But the initial design of the driver wasn't that good, there were two variables to decide what to do and now there is a proper state machine. I see this as a cleanup, that also fixes the problem with the re-enable(). > > I'd do it by having a new function > > > > iio_trigger_notify_done_schedule() that uses a work struct to call > > trig->ops->reenable(trig) from a context that can sleep. Said that, the driver doesn't need the re-enable from sleeping context anymore. If you provide an initial patch I can test that. > > It's a rare corner case so I don't really care that in theory we might have > > a device that was safe to reenable the trigger without sleeping. That makes > > it easier to just have one path for this which allows sleeping. Sure, having always the same (i.e. sleeping context) makes it easier for the driver, especially if the non sleeping re-enable is only called during shutdown of the trigger-consumer if an interrupt comes in. Regards, Oleksij -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |