Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2510064lqp; Mon, 25 Mar 2024 00:22:44 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW4qKQBjdsjZPiTSvW7pl2vaA7AIgE8HKJSnQ15Q3F4WyAuDoH2TdBOg0syLy9QTyoH+wRX3++jUqRCyhUlsgTqq2X/i7PouMdOLg7Lnw== X-Google-Smtp-Source: AGHT+IF41Ay8R1ts5fUlXvNqgOSUR6UaFXXfxje4+WRt9hkpDMIL7EiA9nTbtyHK5lDEbUIQBgh8 X-Received: by 2002:a05:6359:4ca5:b0:17f:5f96:4ab4 with SMTP id kk37-20020a0563594ca500b0017f5f964ab4mr7665478rwc.16.1711351364460; Mon, 25 Mar 2024 00:22:44 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711351364; cv=pass; d=google.com; s=arc-20160816; b=tViiSATf/XQGSLfFoDcvgWL/kXfqJFMhc7eLWdnUafUJ/Kbv89NSS/ph9FV8ixzwr0 D6hjG97gK7rD3WmzqvhvccCouAGG2DhItwsflR4lavUYs68qE0Hdcp8KW+13OyE11h4v NK8bKlaxFpAC9vNpKNx8aRt7tYK9xJ2RKUKUxLnUIgTCjlk5iBAyx2MvgjOFtR6fLhze l1DqbI2i9LfoXWeFLU5cGyhNHga5X108Oxm/cot8JVPDnThYYp082mSTquZ6q1zCoocQ aT28wIFZza9cWbcY7bsFho8HcvUn5PU+Tj/+jJcyykSvs0J05Bp4iHb09ZmDIec2sVeU mwBA== 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:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Pho5NRTqzTdeR0q/liu7H9EsUT8JMnMfJvUiR8uy42Q=; fh=EjLAqUyLwEPop6mp6Mv2Dla/zlkqni22rvia26c7r5k=; b=e+ZGaywTdbWEpxk3sMTHn+pTdo+jPkrTocRscRTxkKd307xQp6ndbPFEYOaVYJfLWI 0oVmfnfoZ6cSBDZh4wLrUepPyUQ195WAJiHB/heBryR1aEJjAtu86KzetPv4LzFlhQ7p S2R3aFx+xmzw2TMOLmw15tOs5e2wOB7z25T5wSIac6RDxCYYHWHnPIQW+8cyvtKNo0Hz gipn7lGcWb5edCQNapmFNoY6KJR8q2doguxUvO6bgKf/o6dhywAj6vimW6R6Ee4A0Jxs gJuUhZXLFLhCNzNxqvxHU2IcCRMPmTQexQbpWxEZ1MytH2ng/SveqiE6JWaPYtZ9v7QE P2Lw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=AbLjfzFx; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-114903-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-114903-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id o7-20020a656a47000000b005dc7ee3c62dsi7562959pgu.236.2024.03.25.00.22.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 00:22:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-114903-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=@kernel.org header.s=k20201202 header.b=AbLjfzFx; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-114903-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-114903-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 804DE29B9E3 for ; Mon, 25 Mar 2024 07:08:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2074A1BD5CD; Mon, 25 Mar 2024 00:55:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="AbLjfzFx" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 8ECEA178CC3; Sun, 24 Mar 2024 23:39:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711323568; cv=none; b=pLfJT7Xea9HLhZdB6Mf2Qi+Jltis257SgAKu6iRrg6BWo+RNzoLduKWibDYAyNE58LEFPj/HSHjQI74JZsF8JDW2ZFFpyC9j+4TawcCNgAHuGQoefW9d5id0iKB/QNigmDd/EQ34w0GyPh8PFw4H4NeJDIqbdjL4rXuSvO8E7YM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711323568; c=relaxed/simple; bh=eijCuQ1jqO3YPjRi/TEwrn5Nr7gd0LKFppwpqIhn5rQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hYSCA68Ks0jfjK4KJicITVzaSjYqhVhADmw+T7Kd1c/UdxvgT1xjRJq0mnkg/UFs/xcARSPnqNR4c/HbewBRiJJSpCwKZ0I+gbLZeN0V+hz3/In5NO5L9UYVLDtY299Q6idJ+4QtmSXopiksHHjIMTnlkEPX6P4tBHVgFAX+8KY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AbLjfzFx; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E499DC433F1; Sun, 24 Mar 2024 23:39:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711323567; bh=eijCuQ1jqO3YPjRi/TEwrn5Nr7gd0LKFppwpqIhn5rQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AbLjfzFxePxOwGOG0T5hpd7ikTN3mi80o5loD8+d9CgtqCzwLZ5QCSHoO/ndsKo42 hv3ZyUGv8llyKx9AQQRTV23yb0E5b5lY2dwSW2U7gC5ueRgJE7ipTVdkoBvNJ/M1HD XzjO0CCSR65L69Gsb0O6WheWUIRd57Oz0znffkvOczpSWn6//nSCVpWT7pG4xIihGC ok/BJg00jYVYvpdYORQh8sk5iBuicRYqw4j3msqw4rRuqoeslRp8pUer7O7+6h0mGG enoMjoVbU5ctBbDz/mXZyKv3qrxEcPdO0VmEQbgV43zy5g9ciDpq4r7LPaXGQL9TE6 5AdFdWPn8JmTg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Ian Abbott , Greg Kroah-Hartman Subject: [PATCH 5.15 274/317] comedi: comedi_test: Prevent timers rescheduling during deletion Date: Sun, 24 Mar 2024 19:34:14 -0400 Message-ID: <20240324233458.1352854-275-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324233458.1352854-1-sashal@kernel.org> References: <20240324233458.1352854-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit From: Ian Abbott commit f53641a6e849034a44bf80f50245a75d7a376025 upstream. 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: stable@vger.kernel.org # 4.4+ Signed-off-by: Ian Abbott Link: https://lore.kernel.org/r/20240214100747.16203-1-abbotti@mev.co.uk Signed-off-by: Greg Kroah-Hartman --- drivers/comedi/drivers/comedi_test.c | 30 ++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/drivers/comedi/drivers/comedi_test.c b/drivers/comedi/drivers/comedi_test.c index cbc225eb19918..bea9a3adf08c8 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]; }; @@ -236,8 +238,12 @@ static void waveform_ai_timer(struct timer_list *t) time_increment = devpriv->ai_convert_time - now; else time_increment = 1; - mod_timer(&devpriv->ai_timer, - jiffies + usecs_to_jiffies(time_increment)); + spin_lock(&dev->spinlock); + if (devpriv->ai_timer_enable) { + mod_timer(&devpriv->ai_timer, + jiffies + usecs_to_jiffies(time_increment)); + } + spin_unlock(&dev->spinlock); } overrun: @@ -393,9 +399,12 @@ static int waveform_ai_cmd(struct comedi_device *dev, * Seem to need an extra jiffy here, otherwise timer expires slightly * early! */ + spin_lock_bh(&dev->spinlock); + 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_bh(&dev->spinlock); return 0; } @@ -404,6 +413,9 @@ static int waveform_ai_cancel(struct comedi_device *dev, { struct waveform_private *devpriv = dev->private; + spin_lock_bh(&dev->spinlock); + devpriv->ai_timer_enable = false; + spin_unlock_bh(&dev->spinlock); if (in_softirq()) { /* Assume we were called from the timer routine itself. */ del_timer(&devpriv->ai_timer); @@ -495,8 +507,12 @@ static void waveform_ao_timer(struct timer_list *t) unsigned int time_inc = devpriv->ao_last_scan_time + devpriv->ao_scan_period - now; - mod_timer(&devpriv->ao_timer, - jiffies + usecs_to_jiffies(time_inc)); + spin_lock(&dev->spinlock); + if (devpriv->ao_timer_enable) { + mod_timer(&devpriv->ao_timer, + jiffies + usecs_to_jiffies(time_inc)); + } + spin_unlock(&dev->spinlock); } underrun: @@ -517,9 +533,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_bh(&dev->spinlock); + devpriv->ao_timer_enable = true; devpriv->ao_timer.expires = jiffies + usecs_to_jiffies(devpriv->ao_scan_period); add_timer(&devpriv->ao_timer); + spin_unlock_bh(&dev->spinlock); return 1; } @@ -604,6 +623,9 @@ static int waveform_ao_cancel(struct comedi_device *dev, struct waveform_private *devpriv = dev->private; s->async->inttrig = NULL; + spin_lock_bh(&dev->spinlock); + devpriv->ao_timer_enable = false; + spin_unlock_bh(&dev->spinlock); if (in_softirq()) { /* Assume we were called from the timer routine itself. */ del_timer(&devpriv->ao_timer); -- 2.43.0