From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) (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 56ABC3CF02F; Fri, 13 Mar 2026 17:45:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.167.242.64 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773423921; cv=none; b=p8UuPhjzT/9PtUtfzXnXVG61DKnrxM7Pb/jLCEc6l7rPM2P7aRR0cxZAb1J1XNOVF4NExli4mGNZ+TyXIqE0aGoc94V6izOBGXNhFcqzMYFQosWqzMpxLJqwcUZsx2XwoTwsSUaz6EdCdspQ0BMu348dM6bCc0BjfaIM+w+aJ7U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773423921; c=relaxed/simple; bh=0rTfE3mYr6Mpa4MDq+zILXMHGMeM4a0Hchbu1CsUrFg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=auTnTUAgGF5SWQ0tGyrsWQDryQbdF7+cqixFe5EYdCb5fCvWLLoAQ8LPp8pLicwSyvjXWob1CZ8mdJjW1pgdnM9ln1egiuILaTpRCZqj6Hy1VTpNvNQd4+1Xsa2RaxPrnwn9BWGtIcU/f0uO/epjyqZn2wEzu3l7Hvq9pAWiT/Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ideasonboard.com; spf=pass smtp.mailfrom=ideasonboard.com; dkim=pass (1024-bit key) header.d=ideasonboard.com header.i=@ideasonboard.com header.b=MWhrVck6; arc=none smtp.client-ip=213.167.242.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ideasonboard.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ideasonboard.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="MWhrVck6" Received: from killaraus.ideasonboard.com (unknown [88.128.88.5]) by perceval.ideasonboard.com (Postfix) with UTF8SMTPSA id 00BF6E70; Fri, 13 Mar 2026 18:44:06 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1773423847; bh=0rTfE3mYr6Mpa4MDq+zILXMHGMeM4a0Hchbu1CsUrFg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=MWhrVck6orwaeC7sqH//rtBOyHCGVMpOVwZUtQLUQszHCkQGSt+CetOqFJZMbWI3p c8cz7rgYzajmiXQRfdPPYfWhrgYl1SMRPDkk4sxSx3HqE0c/Wl0ycfIR6YsglyAFbQ WI1gBzvVVZ0nN0sCX1yq8d8Dqe8xCtxpuRfNz524= Date: Fri, 13 Mar 2026 18:45:14 +0100 From: Laurent Pinchart To: Sean Anderson Cc: Hans de Goede , Ricardo Ribalda , linux-media@vger.kernel.org, Mauro Carvalho Chehab , linux-kernel@vger.kernel.org, Hans Verkuil Subject: Re: [PATCH] media: uvcvideo: Fix deadlock if uvc_status_stop is called from async_ctrl.work Message-ID: <20260313174514.GC672609@killaraus.ideasonboard.com> References: <20260310222259.1202061-1-sean.anderson@linux.dev> 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-Disposition: inline In-Reply-To: <20260310222259.1202061-1-sean.anderson@linux.dev> On Tue, Mar 10, 2026 at 06:22:59PM -0400, Sean Anderson wrote: > If a UVC camera has an asynchronous control, uvc_status_stop may be > called from async_ctrl.work: > > uvc_ctrl_status_event_work() > uvc_ctrl_status_event() > uvc_ctrl_clear_handle() > uvc_pm_put() > uvc_status_put() > uvc_status_stop() > cancel_work_sync() > > This will cause a deadlock, since cancel_work_sync will wait for > uvc_ctrl_status_event_work to complete before returning. > > Fix this by returning early from uvc_status_stop if we are currently in > the work function. flush_status now remains false until uvc_status_start > is called again, ensuring that uvc_ctrl_status_event_work won't resubmit > the URB. > > Fixes: a32d9c41bdb8 ("media: uvcvideo: Make power management granular") > Closes: https://lore.kernel.org/all/6733bdfb-3e88-479f-8956-ab09c04c433e@linux.dev/ > Signed-off-by: Sean Anderson > --- > > drivers/media/usb/uvc/uvc_status.c | 25 ++++++++++++++++--------- > 1 file changed, 16 insertions(+), 9 deletions(-) > > diff --git a/drivers/media/usb/uvc/uvc_status.c b/drivers/media/usb/uvc/uvc_status.c > index 231cfee8e7c2c..2a23606c7f4c6 100644 > --- a/drivers/media/usb/uvc/uvc_status.c > +++ b/drivers/media/usb/uvc/uvc_status.c > @@ -316,6 +316,14 @@ static int uvc_status_start(struct uvc_device *dev, gfp_t flags) > if (!dev->int_urb) > return 0; > > + /* > + * If the work called uvc_status_stop it may still be running. Wait for > + * it to finish before we submit the urb. I don't like this much. The code is becoming really convoluted and hard to follow. Would there be a way to solve the issue with a broader refactoring that would simplify the implementation ? > + */ > + cancel_work_sync(&dev->async_ctrl.work); > + > + /* Clear the flush status if we were previously stopped */ s/stopped/stopped./ > + smp_store_release(&dev->flush_status, false); Add a blank line here. > return usb_submit_urb(dev->int_urb, flags); > } > > @@ -336,6 +344,14 @@ static void uvc_status_stop(struct uvc_device *dev) > */ > smp_store_release(&dev->flush_status, true); > > + /* > + * We will deadlock if we are currently in the work function. > + * Fortunately, we know that the URB is already dead and that no The URB hasn't been killed, at has just completed and not been resubmitted. I'd write /* * If we are called from the event work function, the URB is guaranteed * to not be in flight as it has completed and has not been resubmitted. * There's no need to cancel the work (which would deadlock), or to kill * the URB. */ > + * further work can be queued, so there's nothing left for us to do. > + */ > + if (current_work() == &w->work) > + return; > + > /* > * Cancel any pending asynchronous work. If any status event was queued, > * process it synchronously. > @@ -354,15 +370,6 @@ static void uvc_status_stop(struct uvc_device *dev) > */ > if (cancel_work_sync(&w->work)) > uvc_ctrl_status_event(w->chain, w->ctrl, w->data); > - > - /* > - * From this point, there are no events on the queue and the status URB > - * is dead. No events will be queued until uvc_status_start() is called. > - * The barrier is needed to make sure that flush_status is visible to > - * uvc_ctrl_status_event_work() when uvc_status_start() will be called > - * again. > - */ > - smp_store_release(&dev->flush_status, false); > } > > int uvc_status_resume(struct uvc_device *dev) -- Regards, Laurent Pinchart