Received: by 2002:a89:288:0:b0:1f7:eeee:6653 with SMTP id j8csp145100lqh; Mon, 6 May 2024 14:14:16 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUbm6CG80AfIrFFU+X8NwrxE1CfyuQMewGSPnl+OyYYp+N+hWC2P9hM4GfOHzFeM9Cz3ssqlEAY85Gx3mEd6tEi0KQgONXX6SAx7lM+VA== X-Google-Smtp-Source: AGHT+IE7TXkrtHB3drUoGVvegMuAsiHinn+qI/+lRNZZuMZw1Fb6nrjZ2oxJGZ6RSPAE3bFoGY5r X-Received: by 2002:a05:6a21:78a1:b0:1a3:df1d:deba with SMTP id bf33-20020a056a2178a100b001a3df1ddebamr11962878pzc.31.1715030056395; Mon, 06 May 2024 14:14:16 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715030056; cv=pass; d=google.com; s=arc-20160816; b=dUmS2sdlhoJgMj7EoFicVqJjc/WWa/db0trcVmKvOYdWFB8J6RZZfRux71+O1tgd97 /g4BxbeNxy+WCGS6gxyKc6Or2+u3ZL0TZiu7sui6yG4bHmJnimzwIt0YVODGNn/7oGr6 01C3Jj5U+rzVDEhb6o4TyPk1xOtPYvaHbjMZnQXTy1uqgycHfqaYAwWk3kBZpWn3CT2N Lu0TIxjk3RJZ3zDaw8SSzgzs8dLQy6n1KzmRjy0UEtKZdF/TRWC9EMx2VzJ/uZhuZxP/ 1LF+JEdsT3fhwj3uGXfUucNKlsWW3rrfC0ejwjykpgvsQiQ6kOwzWa8mKof9dwjUNXwC Uz2A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=B9QNMPvGLAYBnNPSn0ftFG2L7lyXZe/71ldtmrt+Bjs=; fh=VZ7ByeJYtMFnSCzEsUFhfbRkLPdB5fbPBlmePbH0DK4=; b=kokHaPbgh9c7VvDwS/LzrcYJ7gUHpVKkndklH351Y6bYmjo7mlXt4T4VMoW8NPLpos PXvCZ5akHUcU0EoIxYqtqoPQlB50bUcG2UFhenJb7hK7UNAyGu2Rh5qb5HYbP5qLnZJs ZCixPtsIFXV3RcVUhSOnP5Ov8F5VDFNWmeTB2ecLRuyj1h28/qRMMd3FUmA/VjLbYpAw KAKDJsdALxzc5ATTqXVy9fj9eK37i0w0fp1ZfaU24NeZkCxhRYmz+purkRtmpmOoprJr YCtPyBm5Ji6G2KcaZ7ANTF+coOm8fJHrn0+nh3/MEHV3n4EdxsjivBaxto5yK219JG1S CN2Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=crnIXjPE; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-170427-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-170427-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id ld1-20020a056a004f8100b006e8f7f4dd60si5363087pfb.253.2024.05.06.14.14.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 14:14:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-170427-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=crnIXjPE; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-170427-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-170427-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 951CEB23507 for ; Mon, 6 May 2024 21:11:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D6B8315E5A5; Mon, 6 May 2024 21:10:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="crnIXjPE" Received: from mail-oa1-f49.google.com (mail-oa1-f49.google.com [209.85.160.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3EC2915D5BD for ; Mon, 6 May 2024 21:10:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715029835; cv=none; b=FhBaJsXJP6wNTroqQEDRCVDEI7PV2uKakAaeslT/RAPRKT7H9c1qGYLhUMgw+KPdFCXxxi2JLk1ZFkLdOOq1M9boLE2epgu11w9/ZcpsyjxikW4sL2LFAKdt8pmI+q08BDPkb8oKWIjFjBadPO8PssRwhfnP2ev42RoUt+8Ooo8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715029835; c=relaxed/simple; bh=E2Y7TzdEbh5vcibAf3xlTX0c6fqY+SvR327dj3nMUZY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jCBydrJ6pxdxsEdksFDw8x8eKP2JLEAgiLxVrJT07hKBJKNrXiBWBA2a96/x6RsOF69aV7oV7VJqsrkwMJ/0MFNivvWilHH1THCKyNmUd/3zagYGFmTTAmcRhmP1ntJxqIvB63BsiJGDEmtULGLM1bVaFQj0RNOur0be5WQNUH0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=crnIXjPE; arc=none smtp.client-ip=209.85.160.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-oa1-f49.google.com with SMTP id 586e51a60fabf-23f0d7d2ce6so1356260fac.2 for ; Mon, 06 May 2024 14:10:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715029833; x=1715634633; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=B9QNMPvGLAYBnNPSn0ftFG2L7lyXZe/71ldtmrt+Bjs=; b=crnIXjPEFYnavZcwoQrYP+vp+BmvG3hM2cC/Ep6tWEwY9ttDlIpZq5UwExUamvriuS rJIom+3YzKN6oGcak4w/qCWRWVtlTXwl5qUVqmcF1D3IOxb0LkUMATiRd9tTwX6TJXok 9COPGp8OSZh2/FyJr/IYaOmXd4158oNvBn0Z8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715029833; x=1715634633; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=B9QNMPvGLAYBnNPSn0ftFG2L7lyXZe/71ldtmrt+Bjs=; b=obfx0KZw40wJOMj0M9uCQiy5cXmtCmCQo1r9nZD2g06V6vFhAI6qXsgtLwOLdLbHjS p3aicXQ8FsLBeJocvHo2BFoxxuRvB6NTvjKgBsYVtYanmTZLgyiBKH1IqeRQ4zkXMO8N ZYmtIJrmddSqlqKorP4XRzalgxaQRsIoUFh/Y9iklR0MXieGJuA4c/sKsxlt8PxnasCC WusvoHKfVtWyZVsdpMpbkBH079c6rjMay7pg0JcuuHyvWgGQEtN1Nxt8w5+nZ6A8H5cJ WTQgr4N/5+XWmf/nAlzuDoeIuilQo17OxKDuaoQnKQ6oyv59YcRkxAS8t6TVrBoUKHWX 2QpA== X-Forwarded-Encrypted: i=1; AJvYcCWuQrV2inDZhvf/wNnPFRDU09tBEPR3Jze6zRqQJeSNshId5WFHayO8hN7mVCDS0XdOuAEDvkRDwpKF2cDQ8sbiJdm9HDaWErThbybk X-Gm-Message-State: AOJu0Yw/xh8HnfNKR88XKiRfF9aKFbgtz95RX3mW6132QlaNLg1YdbUA 6zqFge3yTenj4Am/0H6fG5sqSGyRcoJVGsNmRvLASOXOMUn7uuqub1EZKU32Xw== X-Received: by 2002:a05:6870:2007:b0:23d:41b7:6f10 with SMTP id o7-20020a056870200700b0023d41b76f10mr14015836oab.31.1715029833192; Mon, 06 May 2024 14:10:33 -0700 (PDT) Received: from denia.c.googlers.com (114.152.245.35.bc.googleusercontent.com. [35.245.152.114]) by smtp.gmail.com with ESMTPSA id pa20-20020a05620a831400b0078f13e59dc9sm4224921qkn.102.2024.05.06.14.10.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 14:10:32 -0700 (PDT) From: Ricardo Ribalda Date: Mon, 06 May 2024 21:10:30 +0000 Subject: [PATCH 5/5] media: drivers/media/dvb-core: Refactor dvb_frontend_open 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-Transfer-Encoding: 7bit Message-Id: <20240506-cocci-locks-v1-5-a67952fe5d19@chromium.org> References: <20240506-cocci-locks-v1-0-a67952fe5d19@chromium.org> In-Reply-To: <20240506-cocci-locks-v1-0-a67952fe5d19@chromium.org> To: Andy Walls , Mauro Carvalho Chehab , Sean Young , Jarod Wilson Cc: Hans Verkuil , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Ricardo Ribalda X-Mailer: b4 0.12.4 Move the shared frontend logic to its own function. This allows using guard() to handle the mfe_lock mutex, and using lockdep asserts. There is no intended change of behavior in this code. This fixes the following cocci warnings: drivers/media/dvb-core/dvb_frontend.c:2897:1-7: preceding lock on line 2776 drivers/media/dvb-core/dvb_frontend.c:2897:1-7: preceding lock on line 2786 drivers/media/dvb-core/dvb_frontend.c:2897:1-7: preceding lock on line 2809 Signed-off-by: Ricardo Ribalda --- drivers/media/dvb-core/dvb_frontend.c | 116 ++++++++++++++++++++-------------- 1 file changed, 69 insertions(+), 47 deletions(-) diff --git a/drivers/media/dvb-core/dvb_frontend.c b/drivers/media/dvb-core/dvb_frontend.c index 4f78f30b3646..d04a6092f1ba 100644 --- a/drivers/media/dvb-core/dvb_frontend.c +++ b/drivers/media/dvb-core/dvb_frontend.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -2760,66 +2761,70 @@ static __poll_t dvb_frontend_poll(struct file *file, struct poll_table_struct *w return 0; } -static int dvb_frontend_open(struct inode *inode, struct file *file) +static int dvb_get_shared_frontend(struct dvb_adapter *adapter, + struct dvb_device *dvbdev, + struct file *file) { - struct dvb_device *dvbdev = file->private_data; - struct dvb_frontend *fe = dvbdev->priv; - struct dvb_frontend_private *fepriv = fe->frontend_priv; - struct dvb_adapter *adapter = fe->dvb; - int ret; + struct dvb_device *mfedev; + struct dvb_frontend *mfe; + struct dvb_frontend_private *mfepriv; + int mferetry; - dev_dbg(fe->dvb->device, "%s:\n", __func__); - if (fe->exit == DVB_FE_DEVICE_REMOVED) - return -ENODEV; + lockdep_assert_held(&adapter->mfe_lock); if (adapter->mfe_shared == 2) { - mutex_lock(&adapter->mfe_lock); if ((file->f_flags & O_ACCMODE) != O_RDONLY) { if (adapter->mfe_dvbdev && !adapter->mfe_dvbdev->writers) { - mutex_unlock(&adapter->mfe_lock); return -EBUSY; } adapter->mfe_dvbdev = dvbdev; } - } else if (adapter->mfe_shared) { - mutex_lock(&adapter->mfe_lock); + return 0; + } - if (!adapter->mfe_dvbdev) - adapter->mfe_dvbdev = dvbdev; + if (!adapter->mfe_dvbdev) { + adapter->mfe_dvbdev = dvbdev; + return 0; + } - else if (adapter->mfe_dvbdev != dvbdev) { - struct dvb_device - *mfedev = adapter->mfe_dvbdev; - struct dvb_frontend - *mfe = mfedev->priv; - struct dvb_frontend_private - *mfepriv = mfe->frontend_priv; - int mferetry = (dvb_mfe_wait_time << 1); - - mutex_unlock(&adapter->mfe_lock); - while (mferetry-- && (mfedev->users != -1 || - mfepriv->thread)) { - if (msleep_interruptible(500)) { - if (signal_pending(current)) - return -EINTR; - } - } + if (adapter->mfe_dvbdev == dvbdev) + return 0; - mutex_lock(&adapter->mfe_lock); - if (adapter->mfe_dvbdev != dvbdev) { - mfedev = adapter->mfe_dvbdev; - mfe = mfedev->priv; - mfepriv = mfe->frontend_priv; - if (mfedev->users != -1 || - mfepriv->thread) { - mutex_unlock(&adapter->mfe_lock); - return -EBUSY; - } - adapter->mfe_dvbdev = dvbdev; + mfedev = adapter->mfe_dvbdev; + mfe = mfedev->priv; + mfepriv = mfe->frontend_priv; + mferetry = (dvb_mfe_wait_time << 1); + + mutex_unlock(&adapter->mfe_lock); + while (mferetry-- && (mfedev->users != -1 || mfepriv->thread)) { + if (msleep_interruptible(500)) { + if (signal_pending(current)) { + mutex_lock(&adapter->mfe_lock); + return -EINTR; } } } + mutex_lock(&adapter->mfe_lock); + + if (adapter->mfe_dvbdev != dvbdev) { + mfedev = adapter->mfe_dvbdev; + mfe = mfedev->priv; + mfepriv = mfe->frontend_priv; + if (mfedev->users != -1 || mfepriv->thread) + return -EBUSY; + adapter->mfe_dvbdev = dvbdev; + } + + return 0; +} + +static int __dvb_frontend_open(struct inode *inode, struct file *file) +{ + struct dvb_device *dvbdev = file->private_data; + struct dvb_frontend *fe = dvbdev->priv; + struct dvb_frontend_private *fepriv = fe->frontend_priv; + int ret; if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) { if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0) @@ -2871,8 +2876,6 @@ static int dvb_frontend_open(struct inode *inode, struct file *file) dvb_frontend_get(fe); - if (adapter->mfe_shared) - mutex_unlock(&adapter->mfe_lock); return ret; err3: @@ -2892,11 +2895,30 @@ static int dvb_frontend_open(struct inode *inode, struct file *file) if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) fe->ops.ts_bus_ctrl(fe, 0); err0: - if (adapter->mfe_shared) - mutex_unlock(&adapter->mfe_lock); return ret; } +static int dvb_frontend_open(struct inode *inode, struct file *file) +{ + struct dvb_device *dvbdev = file->private_data; + struct dvb_frontend *fe = dvbdev->priv; + struct dvb_adapter *adapter = fe->dvb; + int ret; + + dev_dbg(fe->dvb->device, "%s:\n", __func__); + if (fe->exit == DVB_FE_DEVICE_REMOVED) + return -ENODEV; + + if (!adapter->mfe_shared) + return __dvb_frontend_open(inode, file); + + guard(mutex)(&adapter->mfe_lock); + ret = dvb_get_shared_frontend(adapter, dvbdev, file); + if (ret) + return ret; + return __dvb_frontend_open(inode, file); +} + static int dvb_frontend_release(struct inode *inode, struct file *file) { struct dvb_device *dvbdev = file->private_data; -- 2.45.0.rc1.225.g2a3ae87e7f-goog