Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3597032pxb; Mon, 24 Jan 2022 13:08:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJwIwK2QThLGLoq77vKY3TN0av1gJPSyL0INXZBhFGXDcGW0kiHBxV9UTi54UXAuryeHyOh4 X-Received: by 2002:a17:90b:1e07:: with SMTP id pg7mr110224pjb.97.1643058529083; Mon, 24 Jan 2022 13:08:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643058529; cv=none; d=google.com; s=arc-20160816; b=cvWU6/YcSIb+NPCkOR1A3+DEuLQg5D8Yv3pmrKRFFJIkBoHPFyh2cxJKlc86eRB7ho LMN68wYFMg/HEV+CZfNOWnt+rXqvThip6HTSDr/MK98Cb2fvRYV/wnLBzTY6XNfByvKs yaJzpvIGcpXquSrZs7EWtdjz+9gp+5nmmCn0N/uxNKmCk7rhlexHIfgNaz4edH4ZIGCT PvTNvR2IGL0zDdl6sxDNV7Rr1luC9rDYgwJguhXwEz16Au1cMnx+EQEZZ9gyDZ27piye idSzrPbKKwh8JfcG6B5xGzRFgyEUOpC6I+b3B1m6WeQsqpY33e4cf8trUAolWHL1te6h N4FA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=NDV8xfEvRSw97WMpg58Cj9fo7fqi22+qkgRP3norm20=; b=MUAgQG01lOT5ZkRHREJ2moqIVsewPJGUeVgG8iBjKzhzkBWPV3iuoKqeIyqwdpqtDA xuT2H48tLpth8WtCzKhbvi0//GQR/fYoxbGfVqYeCiV4ZtJVyk38vihDyQ0w+vqAGgEC EZC7ro53Wv08OR/F4ycv/t1+clrWf/TSg8AY3AXCjv0IOsX1XNph/jRlMJD2N1W2zYgP SGL0qZHxntmN7LB3NaongHdcN3LEe+WvOcLAWglgIO7fdmc2gLnl4qMBGCSdCcVyk71A 9lq4YCnG0RY4HAZoakxSqbF+9yNsnFglaKHs6LIW1ynzYzXnV4ji2BRKy2qQWfk4/DaG XP7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=sQ2unch9; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q28si17146527pgu.468.2022.01.24.13.08.28; Mon, 24 Jan 2022 13:08:49 -0800 (PST) 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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=sQ2unch9; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1385418AbiAXUdY (ORCPT + 99 others); Mon, 24 Jan 2022 15:33:24 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:39412 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379764AbiAXUMZ (ORCPT ); Mon, 24 Jan 2022 15:12:25 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id DB57A6091A; Mon, 24 Jan 2022 20:12:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B94A1C340E5; Mon, 24 Jan 2022 20:12:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643055144; bh=l+gtYq5zMqIJC9T7FFg/WoG6gd4YYnKZA2mw7RhH/1s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sQ2unch9TiZcLbJoE1EFFSRp/D8blHkfBJ+OgQa5BEJZf41ykkWWE0KEaFDYhmwm3 AY7JBY8bL6DLNuRwSh+e5ukqIFrIVr0YezjR+QCZl8fiID6CgteoBcJsDbhh7Eyhyt HMbPncvnf7EsUYs48e1uXa60Ucbc1VpOmbAasUOU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jonathan Cameron , Pengutronix Kernel Team , Dmitry Torokhov , Oleksij Rempel , Stable@vger.kernel.org Subject: [PATCH 5.15 056/846] iio: trigger: Fix a scheduling whilst atomic issue seen on tsc2046 Date: Mon, 24 Jan 2022 19:32:53 +0100 Message-Id: <20220124184102.903148761@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184100.867127425@linuxfoundation.org> References: <20220124184100.867127425@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jonathan Cameron commit 9020ef659885f2622cfb386cc229b6d618362895 upstream. IIO triggers are software IRQ chips that split an incoming IRQ into separate IRQs routed to all devices using the trigger. When all consumers are done then a trigger callback reenable() is called. There are a few circumstances under which this can happen in atomic context. 1) A single user of the trigger that calls the iio_trigger_done() function from interrupt context. 2) A race between disconnecting the last device from a trigger and the trigger itself sucessfully being disabled. To avoid a resulting scheduling whilst atomic, close this second corner by using schedule_work() to ensure the reenable is not done in atomic context. Note that drivers must be careful to manage the interaction of set_state() and reenable() callbacks to ensure appropriate reference counting if they are relying on the same hardware controls. Deliberately taking this the slow path rather than via a fixes tree because the error has hard to hit and I would like it to soak for a while before hitting a release kernel. Signed-off-by: Jonathan Cameron Cc: Pengutronix Kernel Team Cc: Dmitry Torokhov Tested-by: Oleksij Rempel Cc: Link: https://lore.kernel.org/r/20211017172209.112387-1-jic23@kernel.org Signed-off-by: Greg Kroah-Hartman --- drivers/iio/industrialio-trigger.c | 36 +++++++++++++++++++++++++++++++++++- include/linux/iio/trigger.h | 2 ++ 2 files changed, 37 insertions(+), 1 deletion(-) --- a/drivers/iio/industrialio-trigger.c +++ b/drivers/iio/industrialio-trigger.c @@ -162,6 +162,39 @@ static struct iio_trigger *iio_trigger_a return trig; } +static void iio_reenable_work_fn(struct work_struct *work) +{ + struct iio_trigger *trig = container_of(work, struct iio_trigger, + reenable_work); + + /* + * This 'might' occur after the trigger state is set to disabled - + * in that case the driver should skip reenabling. + */ + trig->ops->reenable(trig); +} + +/* + * In general, reenable callbacks may need to sleep and this path is + * not performance sensitive, so just queue up a work item + * to reneable the trigger for us. + * + * Races that can cause this. + * 1) A handler occurs entirely in interrupt context so the counter + * the final decrement is still in this interrupt. + * 2) The trigger has been removed, but one last interrupt gets through. + * + * For (1) we must call reenable, but not in atomic context. + * For (2) it should be safe to call reenanble, if drivers never blindly + * reenable after state is off. + */ +static void iio_trigger_notify_done_atomic(struct iio_trigger *trig) +{ + if (atomic_dec_and_test(&trig->use_count) && trig->ops && + trig->ops->reenable) + schedule_work(&trig->reenable_work); +} + void iio_trigger_poll(struct iio_trigger *trig) { int i; @@ -173,7 +206,7 @@ void iio_trigger_poll(struct iio_trigger if (trig->subirqs[i].enabled) generic_handle_irq(trig->subirq_base + i); else - iio_trigger_notify_done(trig); + iio_trigger_notify_done_atomic(trig); } } } @@ -535,6 +568,7 @@ struct iio_trigger *viio_trigger_alloc(s trig->dev.type = &iio_trig_type; trig->dev.bus = &iio_bus_type; device_initialize(&trig->dev); + INIT_WORK(&trig->reenable_work, iio_reenable_work_fn); mutex_init(&trig->pool_lock); trig->subirq_base = irq_alloc_descs(-1, 0, --- a/include/linux/iio/trigger.h +++ b/include/linux/iio/trigger.h @@ -55,6 +55,7 @@ struct iio_trigger_ops { * @attached_own_device:[INTERN] if we are using our own device as trigger, * i.e. if we registered a poll function to the same * device as the one providing the trigger. + * @reenable_work: [INTERN] work item used to ensure reenable can sleep. **/ struct iio_trigger { const struct iio_trigger_ops *ops; @@ -74,6 +75,7 @@ struct iio_trigger { unsigned long pool[BITS_TO_LONGS(CONFIG_IIO_CONSUMERS_PER_TRIGGER)]; struct mutex pool_lock; bool attached_own_device; + struct work_struct reenable_work; };