Received: by 2002:a05:7412:2a91:b0:fc:a2b0:25d7 with SMTP id u17csp89002rdh; Tue, 13 Feb 2024 10:10:51 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVsPSmisc/UItOQuf66KR0PfVEshZe8981hZegAJyIZxTNFxTgJiitC+vZUvwx4ifn+S33nZ9AqrRMG9/QhOleoRUY3avuxpt17hiisig== X-Google-Smtp-Source: AGHT+IGB4NVmdFFheiUDNcU2l+UH4SztjUf+5rfTwOncnVktKX4opa6wnD0BTLSQw09xgNIAD/kO X-Received: by 2002:a17:906:718:b0:a39:d272:3715 with SMTP id y24-20020a170906071800b00a39d2723715mr156172ejb.37.1707847851138; Tue, 13 Feb 2024 10:10:51 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707847851; cv=pass; d=google.com; s=arc-20160816; b=OX38AWu473LsJe1QgBX0f7RpVMSu2o5miF/REjhWx4hdgyPP4+TNMq/8kH76vzmNUd gJEbguoggjM2DAxidY0t+Rj0o5OotaCyDd6vxkQnJVmAIafZ+YWj+aOCO8v0rxGBnDvn uWSWELl0l7o9y8KkWIKZFOmDb4VqwyqMgUvyn0swUh0sppDQUeEyS4QSTUF4YORle7MB QWcm/syAcuo7CR2wqHXlloEIH3JUpYqbNWZwPgS44nqcGfL2zkUgRCdEY756fEz4f7a7 lJ6zE56iMU/XPOUgJcHDavj9pYJ+GuJceDj3RJLisBdlB0j3MKugy77bsZM8dt3QY4fY VpKg== 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:message-id:date:subject:cc:to :from:dkim-signature; bh=hAxom2fpaOzsVKiYwPOFiIHf5ir7JuqKcxjXsAGRotE=; fh=F0kjFRxnElBVdvL7CGinGXdxpU59IY0DuWsmb12cLEU=; b=tQ1ZyOfb39NiEQyk15oLlmjDutJHwhAtb0qZsLC9lSxQAPwXaYwz3NBVUX5+YWPrKQ a80551j+r5tpJ/euOsolbz2USbCiZI6u4xP+kKNgtJ5XLREQpQiBbns6o5xcCP/EHPQ5 lXEW9GIoP3xx+Dg57iKbenxNxHVbgwM2ZvVWXc+lxFqwxWz2NZQN+IKV8QnuCehkgMTj vlYi0qf6HSWc6+PMaEFNDgphMS1qNvKJHYhZx2dmnisLka44GUDDPG9A0iHjZShDz/1C RQ2ItG+GXhcRzqwN0hYtnvP3SdoGuSx85SUvR5g4GnGaYP7RYEWlQc3tgNm8sVvUNNta QFMA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@mev.co.uk header.s=20221208-6x11dpa4 header.b=SHOGLMNv; arc=pass (i=1 spf=pass spfdomain=mev.co.uk dkim=pass dkdomain=mev.co.uk dmarc=pass fromdomain=mev.co.uk); spf=pass (google.com: domain of linux-kernel+bounces-64061-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64061-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=mev.co.uk X-Forwarded-Encrypted: i=2; AJvYcCU/Wj994R1orl0rjY7mR6V1uVahgIsrUPrTkcNsth5YPD8WwsvNixBnDSNHlJbHw37xa5WoZKiBIO+Jo+KRdvIAnOvtO5XZC0PWxQJ9bg== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id n16-20020a170906689000b00a3bfccab348si1430613ejr.380.2024.02.13.10.10.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 10:10:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64061-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@mev.co.uk header.s=20221208-6x11dpa4 header.b=SHOGLMNv; arc=pass (i=1 spf=pass spfdomain=mev.co.uk dkim=pass dkdomain=mev.co.uk dmarc=pass fromdomain=mev.co.uk); spf=pass (google.com: domain of linux-kernel+bounces-64061-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64061-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=mev.co.uk 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 am.mirrors.kernel.org (Postfix) with ESMTPS id AB3CF1F24C96 for ; Tue, 13 Feb 2024 18:10:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DEAE2605C5; Tue, 13 Feb 2024 18:10:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=mev.co.uk header.i=@mev.co.uk header.b="SHOGLMNv" Received: from smtp102.iad3a.emailsrvr.com (smtp102.iad3a.emailsrvr.com [173.203.187.102]) (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 BEF96605B1 for ; Tue, 13 Feb 2024 18:10:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=173.203.187.102 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707847827; cv=none; b=oofjfHTMs6NX8QclPJHMr5oZWwVr++xDJmUl+OBx3da3PT5i7xW7xbE0fmmNImmZu3xycPb3zK66et33TmmlOZphIPo5dkmhtWyK2lfNsVnh4q6a9MksMg5iIeFnt6+dHiWTIZUpOzuNQfe9yvUfLisu8GdSQBIZJp4ntOC9mrA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707847827; c=relaxed/simple; bh=pjbuxl8oRQ0+R+g36gg/gBcvAmBulWkgzcftiFFzEzc=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=dm6aXamRHG+rU3texM2N93VpMgzmVuNVabt+v+einzTdhr1uvUbL7XqQC1WJDikSv15777kZ7awAbOAc/ylCw4DVa9dbHsth7IN71AYSamtbVnKXllTdVsRySocXUdmNi8ww7bvQH6GlFSKc7U1txyaTWrd/ymq1GRiveHvyNy0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=mev.co.uk; spf=pass smtp.mailfrom=mev.co.uk; dkim=pass (1024-bit key) header.d=mev.co.uk header.i=@mev.co.uk header.b=SHOGLMNv; arc=none smtp.client-ip=173.203.187.102 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=mev.co.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mev.co.uk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mev.co.uk; s=20221208-6x11dpa4; t=1707847819; bh=pjbuxl8oRQ0+R+g36gg/gBcvAmBulWkgzcftiFFzEzc=; h=From:To:Subject:Date:From; b=SHOGLMNv4peTulwzUKMCjCrkHdXPCQtzjvUY+5UrWYuajT9QGy2WRexnZM/yWArAs 21aO1AbTAWmhXg5Lg/qSCLLVS0pIOi7DZqaQAvQ0Djju3ofC7QTy8OqCeqGP9QpdHn aeoxWzuP+LDqrC8kd8TdHGRMBD6rl2Xi28slCsXQ= X-Auth-ID: abbotti@mev.co.uk Received: by smtp29.relay.iad3a.emailsrvr.com (Authenticated sender: abbotti-AT-mev.co.uk) with ESMTPSA id 4D98124EB0; Tue, 13 Feb 2024 13:10:18 -0500 (EST) From: Ian Abbott To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , Ian Abbott , H Hartley Sweeten , stable@vger.kernel.org Subject: [PATCH] comedi: comedi_test: Prevent timers rescheduling during deletion Date: Tue, 13 Feb 2024 18:10:04 +0000 Message-ID: <20240213181004.105072-1-abbotti@mev.co.uk> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Classification-ID: 969d28ca-18e8-4879-92d9-b2ffe5639bd8-1-1 The comedi_test devices have a couple of timers (ai_timer and ao_timer) that can be started to simulate hardware interrupts. Their expiry functions normally reschedule the timer. The driver code calls either del_timer_sync() or del_timer() to delete the timers from the queue, but does not currently prevent the timers from rescheduling themselves so synchronized deletion may be ineffective. Add a couple of boolean members (one for each timer: ai_timer_enable and ao_timer_enable) to the device private data structure to indicate whether the timers are allowed to reschedule themselves. Set the member to true when adding the timer to the queue, and to false when deleting the timer from the queue in the waveform_ai_cancel() and waveform_ao_cancel() functions. The del_timer_sync() function is also called from the waveform_detach() function, but the timer enable members will already be set to false when that function is called, so no change is needed there. Fixes: 403fe7f34e33 ("staging: comedi: comedi_test: fix timer race conditions") Cc: # 4.4+ Signed-off-by: Ian Abbott --- drivers/comedi/drivers/comedi_test.c | 37 +++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/drivers/comedi/drivers/comedi_test.c b/drivers/comedi/drivers/comedi_test.c index 30ea8b53ebf8..7fefe0de0bcc 100644 --- a/drivers/comedi/drivers/comedi_test.c +++ b/drivers/comedi/drivers/comedi_test.c @@ -87,6 +87,8 @@ struct waveform_private { struct comedi_device *dev; /* parent comedi device */ u64 ao_last_scan_time; /* time of previous AO scan in usec */ unsigned int ao_scan_period; /* AO scan period in usec */ + bool ai_timer_enable:1; /* should AI timer be running? */ + bool ao_timer_enable:1; /* should AO timer be running? */ unsigned short ao_loopbacks[N_CHANS]; }; @@ -232,12 +234,18 @@ static void waveform_ai_timer(struct timer_list *t) if (cmd->stop_src == TRIG_COUNT && async->scans_done >= cmd->stop_arg) { async->events |= COMEDI_CB_EOA; } else { + unsigned long flags; + if (devpriv->ai_convert_time > now) time_increment = devpriv->ai_convert_time - now; else time_increment = 1; - mod_timer(&devpriv->ai_timer, - jiffies + usecs_to_jiffies(time_increment)); + spin_lock_irqsave(&dev->spinlock, flags); + if (devpriv->ai_timer_enable) { + mod_timer(&devpriv->ai_timer, + jiffies + usecs_to_jiffies(time_increment)); + } + spin_unlock_irqrestore(&dev->spinlock, flags); } overrun: @@ -352,6 +360,7 @@ static int waveform_ai_cmd(struct comedi_device *dev, struct comedi_cmd *cmd = &s->async->cmd; unsigned int first_convert_time; u64 wf_current; + unsigned long flags; if (cmd->flags & CMDF_PRIORITY) { dev_err(dev->class_dev, @@ -393,9 +402,12 @@ static int waveform_ai_cmd(struct comedi_device *dev, * Seem to need an extra jiffy here, otherwise timer expires slightly * early! */ + spin_lock_irqsave(&dev->spinlock, flags); + devpriv->ai_timer_enable = true; devpriv->ai_timer.expires = jiffies + usecs_to_jiffies(devpriv->ai_convert_period) + 1; add_timer(&devpriv->ai_timer); + spin_unlock_irqrestore(&dev->spinlock, flags); return 0; } @@ -403,7 +415,11 @@ static int waveform_ai_cancel(struct comedi_device *dev, struct comedi_subdevice *s) { struct waveform_private *devpriv = dev->private; + unsigned long flags; + spin_lock_irqsave(&dev->spinlock, flags); + devpriv->ai_timer_enable = false; + spin_unlock_irqrestore(&dev->spinlock, flags); if (in_softirq()) { /* Assume we were called from the timer routine itself. */ del_timer(&devpriv->ai_timer); @@ -494,9 +510,14 @@ static void waveform_ao_timer(struct timer_list *t) } else { unsigned int time_inc = devpriv->ao_last_scan_time + devpriv->ao_scan_period - now; + unsigned long flags; - mod_timer(&devpriv->ao_timer, - jiffies + usecs_to_jiffies(time_inc)); + spin_lock_irqsave(&dev->spinlock, flags); + if (devpriv->ao_timer_enable) { + mod_timer(&devpriv->ao_timer, + jiffies + usecs_to_jiffies(time_inc)); + } + spin_unlock_irqrestore(&dev->spinlock, flags); } underrun: @@ -510,6 +531,7 @@ static int waveform_ao_inttrig_start(struct comedi_device *dev, struct waveform_private *devpriv = dev->private; struct comedi_async *async = s->async; struct comedi_cmd *cmd = &async->cmd; + unsigned long flags; if (trig_num != cmd->start_arg) return -EINVAL; @@ -517,9 +539,12 @@ static int waveform_ao_inttrig_start(struct comedi_device *dev, async->inttrig = NULL; devpriv->ao_last_scan_time = ktime_to_us(ktime_get()); + spin_lock_irqsave(&dev->spinlock, flags); + devpriv->ao_timer_enable = true; devpriv->ao_timer.expires = jiffies + usecs_to_jiffies(devpriv->ao_scan_period); add_timer(&devpriv->ao_timer); + spin_unlock_irqrestore(&dev->spinlock, flags); return 1; } @@ -602,8 +627,12 @@ static int waveform_ao_cancel(struct comedi_device *dev, struct comedi_subdevice *s) { struct waveform_private *devpriv = dev->private; + unsigned long flags; s->async->inttrig = NULL; + spin_lock_irqsave(&dev->spinlock, flags); + devpriv->ao_timer_enable = false; + spin_unlock_irqrestore(&dev->spinlock, flags); if (in_softirq()) { /* Assume we were called from the timer routine itself. */ del_timer(&devpriv->ao_timer); -- 2.43.0