From patchwork Mon May 6 21:10:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 13656004 Received: from mail-ot1-f44.google.com (mail-ot1-f44.google.com [209.85.210.44]) (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 1DB3B15B55F for ; Mon, 6 May 2024 21:10:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715029831; cv=none; b=dggaHpAhoGo/gz7o6++R653vQq4JZrUQkW9fGBBvug83DAwvRVcPA17yu0IsErL0FS4+H2t6uwkyGuZDuiC9lACkjNJqDmr1ZEHXtzDGgkasM/5lV/c5wCtqsuKAWDdgMpWZrPYDRaNI+mZ/+OuV7Ew8FVsrXvdFwerC5JdQexA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715029831; c=relaxed/simple; bh=onS8B0M5Jv8Adbn3ETYy2u3f71LcIIwxaG7egh3GMOA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=f6qc27BL9SQX/w0v+PIAJO2cd8kZ1HKDDXdrt1hKZ6Z2Q0qE2WQwa7yuWB6Ht39CDrK1s4tfo/hi1LMGtAR+jZwUOd8wmi4eVAkdb3BKF4+DN34YWW1Hy0SYt4nynVtxmoMFmPTstx7AhewPimX53D358Yt7WDhmCA5LH/EgDdQ= 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=Pj5biK3H; arc=none smtp.client-ip=209.85.210.44 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Pj5biK3H" Received: by mail-ot1-f44.google.com with SMTP id 46e09a7af769-6f0585edd8bso779536a34.2 for ; Mon, 06 May 2024 14:10:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715029829; x=1715634629; 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=jtUlP6VM7qrXki+A4GKytcg5ATq7Ztpreg61VfyOW+M=; b=Pj5biK3Hq8ko7p5nG3kzeJ2i7QPZ2N2EZ3zrPitj3cDSf4XcQa2TvVhV99Zii1amFV NWN/7Bljo7C06yUa6J6n3XM2cFk/M6D3bYcHimByMxAnvYA6RVgC3RsnffmGEtwxpasN KLFC2hPVApiza4s0QPJmEs6j9D2ataalmC1LM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715029829; x=1715634629; 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=jtUlP6VM7qrXki+A4GKytcg5ATq7Ztpreg61VfyOW+M=; b=LW76qtcZw+ZerYaERKJ5LKUe9BNQNf+BHUgJhBzpca7zRYW+hz6mVPY2L+3QYxS0Ig JW0vtO9GMkFgHsIBMak+eeL+ZMib2l8HxCCk1sYiQi/PpFAIQQVzAXRBUNi7PUztraat f0malYNVkRkx3/LK9HfVksMOVnlbCAqlIh+rqeXy/Me2gqzvz8A2SKTW9qRYiA0Bko9o P+ylKCEt6QNAY4p0mlrs30SpchKkXCUoc0UJMneJyKWf538qca3jHwQN3GUy07y+sw4e JwlzOw1GdiDumTTZRYAEHuiMPUn6nxKTkzwZSXF2eBM/5daiqpBpDtJmObu8yPkWjglD fGSQ== X-Forwarded-Encrypted: i=1; AJvYcCUweyfoPW2Rq/Jj61DgRAa+xajqYrvF/7fFzKBuYltTIA1OTucAHn+PCZeGmZgX+iwnhFhzEJ9hBQ48/8e2cNXP+B5TI1Gfa9ld/tQ= X-Gm-Message-State: AOJu0YzzoULqYLXfsiNx2e3gFcFnYPeuCVPWvS9MlNxxCkrai3jvnYm8 s4QolOyvfEtAVBWaibbD6jfIkwXulfX/NL+/ORr7mFuPqoYRnQzewcQnQevPQA== X-Google-Smtp-Source: AGHT+IF8Sz8E4E8aQminjkh9wNuTyPXbJEfh1IRvEaK9iGF6PZ9aIZftMbIJIZYtGTFlMIVQc4MDfA== X-Received: by 2002:a9d:6943:0:b0:6f0:30b8:4f06 with SMTP id p3-20020a9d6943000000b006f030b84f06mr5974830oto.2.1715029829149; Mon, 06 May 2024 14:10:29 -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.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 14:10:28 -0700 (PDT) From: Ricardo Ribalda Date: Mon, 06 May 2024 21:10:26 +0000 Subject: [PATCH 1/5] media: ivtv: Factor out schedule functions Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240506-cocci-locks-v1-1-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 Cocci is very confused by unlock-lock a mutex in the middle of a function. Factor the schedules out, avoid code duplication and make cocci a bit happier. Fix the following cocci warnings: drivers/media/pci/ivtv/ivtv-fileops.c:223:4-10: preceding lock on line 267 drivers/media/pci/ivtv/ivtv-fileops.c:230:3-9: preceding lock on line 267 drivers/media/pci/ivtv/ivtv-fileops.c:236:4-10: preceding lock on line 267 drivers/media/pci/ivtv/ivtv-fileops.c:245:3-9: preceding lock on line 267 drivers/media/pci/ivtv/ivtv-fileops.c:251:3-9: preceding lock on line 267 drivers/media/pci/ivtv/ivtv-fileops.c:257:3-9: preceding lock on line 267 drivers/media/pci/ivtv/ivtv-fileops.c:272:3-9: preceding lock on line 267 drivers/media/pci/ivtv/ivtv-fileops.c:598:4-10: preceding lock on line 627 drivers/media/pci/ivtv/ivtv-fileops.c:598:4-10: preceding lock on line 689 drivers/media/pci/ivtv/ivtv-fileops.c:606:3-9: preceding lock on line 627 drivers/media/pci/ivtv/ivtv-fileops.c:606:3-9: preceding lock on line 689 drivers/media/pci/ivtv/ivtv-fileops.c:648:3-9: preceding lock on line 627 drivers/media/pci/ivtv/ivtv-fileops.c:648:3-9: preceding lock on line 689 drivers/media/pci/ivtv/ivtv-fileops.c:692:4-10: preceding lock on line 689 Signed-off-by: Ricardo Ribalda --- drivers/media/pci/ivtv/ivtv-fileops.c | 66 +++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/drivers/media/pci/ivtv/ivtv-fileops.c b/drivers/media/pci/ivtv/ivtv-fileops.c index 4202c3a47d33..cfa28d035586 100644 --- a/drivers/media/pci/ivtv/ivtv-fileops.c +++ b/drivers/media/pci/ivtv/ivtv-fileops.c @@ -21,6 +21,7 @@ #include "ivtv-ioctl.h" #include "ivtv-cards.h" #include "ivtv-firmware.h" +#include #include #include @@ -190,12 +191,27 @@ static void ivtv_update_pgm_info(struct ivtv *itv) itv->pgm_info_write_idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num; } +static void ivtv_schedule(struct ivtv_stream *s) +{ + struct ivtv *itv = s->itv; + DEFINE_WAIT(wait); + + lockdep_assert_held(&itv->serialize_lock); + + mutex_unlock(&itv->serialize_lock); + prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE); + /* New buffers might have become free before we were added to the waitqueue */ + if (!s->q_free.buffers) + schedule(); + finish_wait(&s->waitq, &wait); + mutex_lock(&itv->serialize_lock); +} + static struct ivtv_buffer *ivtv_get_buffer(struct ivtv_stream *s, int non_block, int *err) { struct ivtv *itv = s->itv; struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI]; struct ivtv_buffer *buf; - DEFINE_WAIT(wait); *err = 0; while (1) { @@ -258,13 +274,7 @@ static struct ivtv_buffer *ivtv_get_buffer(struct ivtv_stream *s, int non_block, } /* wait for more data to arrive */ - mutex_unlock(&itv->serialize_lock); - prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE); - /* New buffers might have become available before we were added to the waitqueue */ - if (!s->q_full.buffers) - schedule(); - finish_wait(&s->waitq, &wait); - mutex_lock(&itv->serialize_lock); + ivtv_schedule(s); if (signal_pending(current)) { /* return if a signal was received */ IVTV_DEBUG_INFO("User stopped %s\n", s->name); @@ -533,6 +543,25 @@ int ivtv_start_decoding(struct ivtv_open_id *id, int speed) return 0; } +static int ivtv_schedule_dma(struct ivtv_stream *s) +{ + struct ivtv *itv = s->itv; + int got_sig; + DEFINE_WAIT(wait); + + lockdep_assert_held(&itv->serialize_lock); + + mutex_unlock(&itv->serialize_lock); + prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE); + while (!(got_sig = signal_pending(current)) && + test_bit(IVTV_F_S_DMA_PENDING, &s->s_flags)) + schedule(); + finish_wait(&itv->dma_waitq, &wait); + mutex_lock(&itv->serialize_lock); + + return got_sig; +} + static ssize_t ivtv_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos) { struct ivtv_open_id *id = fh2id(filp->private_data); @@ -544,7 +573,6 @@ static ssize_t ivtv_write(struct file *filp, const char __user *user_buf, size_t int bytes_written = 0; int mode; int rc; - DEFINE_WAIT(wait); IVTV_DEBUG_HI_FILE("write %zd bytes to %s\n", count, s->name); @@ -618,13 +646,7 @@ static ssize_t ivtv_write(struct file *filp, const char __user *user_buf, size_t break; if (filp->f_flags & O_NONBLOCK) return -EAGAIN; - mutex_unlock(&itv->serialize_lock); - prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE); - /* New buffers might have become free before we were added to the waitqueue */ - if (!s->q_free.buffers) - schedule(); - finish_wait(&s->waitq, &wait); - mutex_lock(&itv->serialize_lock); + ivtv_schedule(s); if (signal_pending(current)) { IVTV_DEBUG_INFO("User stopped %s\n", s->name); return -EINTR; @@ -674,20 +696,10 @@ static ssize_t ivtv_write(struct file *filp, const char __user *user_buf, size_t if (test_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags)) { if (s->q_full.length >= itv->dma_data_req_size) { - int got_sig; - if (mode == OUT_YUV) ivtv_yuv_setup_stream_frame(itv); - mutex_unlock(&itv->serialize_lock); - prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE); - while (!(got_sig = signal_pending(current)) && - test_bit(IVTV_F_S_DMA_PENDING, &s->s_flags)) { - schedule(); - } - finish_wait(&itv->dma_waitq, &wait); - mutex_lock(&itv->serialize_lock); - if (got_sig) { + if (ivtv_schedule_dma(s)) { IVTV_DEBUG_INFO("User interrupted %s\n", s->name); return -EINTR; } From patchwork Mon May 6 21:10:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 13656006 Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.171]) (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 358CD15B57B for ; Mon, 6 May 2024 21:10:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715029832; cv=none; b=iszVA7zZ6TDT9bpYsnTtgS+J0V2rjXZGbAqGASGTIHcf1RtceABwGQ7QCgz4UZfkfn6RwR+A/Dl7c6x+TWCEsvyQfqNbgLp3Sv1YjHlM/N8Nc++xGn+fkhzOdGESJ3V4rNcAbet0+6ShjsY30tASxYBmdOAC8D5SgYFGpGpW+NM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715029832; c=relaxed/simple; bh=rmMHfZ07Kd9lTZW6PwC3rqA96g9FbrjW5lK3extywms=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=YMpFMKD8vOQTMskrJHrzH0iGBU29gnB/2YCl+t4grMgh6+D8y0AbVN7sb+pYKT5j7wp1yaRKWBVza4JVy0xwKZduKGmmJJ+oowS6y8Fb+IGpISqTcHyJUmEmgY3cCFijSFCT2ay2qdL9slxQk+YpZYd6TDC1f8ZlDiE321LlmMY= 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=AD8v238k; arc=none smtp.client-ip=209.85.222.171 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="AD8v238k" Received: by mail-qk1-f171.google.com with SMTP id af79cd13be357-7928c54e945so208431585a.2 for ; Mon, 06 May 2024 14:10:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715029830; x=1715634630; 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=GChtxSEHiZ0rsnEnXBszudDorgEfMXDvam+C+CQ8ysU=; b=AD8v238kG/nRmGskPklFvxXewBA+3TI+A2uSOWBNUYHNym1YT14DlD/dUc0YpjsH+d ZiyY+x2FX2Zx6tatsr7ebbwclChoXX6QC6WQb0fUSQQzuT9aToTXu4jrT1bPQ2qUnN2n Y6vGYnqE+O706qslQb5NRtbF4W3TtkKtiTsls= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715029830; x=1715634630; 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=GChtxSEHiZ0rsnEnXBszudDorgEfMXDvam+C+CQ8ysU=; b=te1xbt4E3udy+Mwwf8/ojv8t6se3jqD5NavuNcsHHJg9WKn4MM1i3JPPi/VDKF0HKq MdSJ9y7coMQOgq6/O22HruKUyYBTTwu6awb6LYJuVUjPhMlSALTovPDxTySjL9dV9BRt 5DIkElvXFSznvOS6m+fXjyU6XvzMxYyxS39kMBynt8e44rH5b0Ig21rn7rXUsC7ah98A ECDx0jbRP/ZMVmjZppAP3m5klNmw1cExzOxBYugFisIdMP/7OQNgZBSPFVLOhb9mp/A/ 6VWLzLXR5SMhMYeZRxhq9sKg6TrycoFBWAS7mS054cviGEWtFCg6EEAKUsMlIL1izVUV ORMg== X-Forwarded-Encrypted: i=1; AJvYcCVvKEhN/4nvptQZpcc6gdbULBlRvXhPqwcsnXHkPRN+jvqXy6kWPdzrpH8l8gMCyvc25Cll7QTsdKGRyJRd5oW7v2vPDhHp1lJRCug= X-Gm-Message-State: AOJu0Yz+iZA9AZyNU9hIueItvLTXnLqU0wh8LLfI+1F1bK7gAd5YGeMN A1VrgbK08DpFbshx2vxRlNy9SJpjWdwf0Y9ZE5Nd0GUakKD1VQgN+j1VD3KVeA== X-Google-Smtp-Source: AGHT+IHCIfstPtI+qCxYBl/8IeI1JeUuGRWhMdpvvDoh1G+j87TAujBrzH7bpSdDG6tDvHp9Uo2iFg== X-Received: by 2002:a05:620a:89e:b0:792:93ed:2e7c with SMTP id b30-20020a05620a089e00b0079293ed2e7cmr4684450qka.76.1715029830157; Mon, 06 May 2024 14:10:30 -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.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 14:10:29 -0700 (PDT) From: Ricardo Ribalda Date: Mon, 06 May 2024 21:10:27 +0000 Subject: [PATCH 2/5] media: imon: Fix race getting ictx->lock Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240506-cocci-locks-v1-2-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 Lets fix a race between mutex_is_lock() and mutex_lock(). <-mutex is not locked if (!mutex_is_locked(&ictx->lock)) { unlock = true; <- mutex is locked externaly mutex_lock(&ictx->lock); } Let's use mutex_trylock() that does mutex_is_lock() and mutex_lock() atomically. Fix the following cocci warning: drivers/media/rc/imon.c:1167:1-7: preceding lock on line 1153 Fixes: 23ef710e1a6c ("[media] imon: add conditional locking in change_protocol") Signed-off-by: Ricardo Ribalda --- drivers/media/rc/imon.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/drivers/media/rc/imon.c b/drivers/media/rc/imon.c index 0b55314a8082..8f1361bcce3a 100644 --- a/drivers/media/rc/imon.c +++ b/drivers/media/rc/imon.c @@ -1148,10 +1148,7 @@ static int imon_ir_change_protocol(struct rc_dev *rc, u64 *rc_proto) memcpy(ictx->usb_tx_buf, &ir_proto_packet, sizeof(ir_proto_packet)); - if (!mutex_is_locked(&ictx->lock)) { - unlock = true; - mutex_lock(&ictx->lock); - } + unlock = mutex_trylock(&ictx->lock); retval = send_packet(ictx); if (retval) From patchwork Mon May 6 21:10:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 13656007 Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174]) (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 051BB15B96C for ; Mon, 6 May 2024 21:10:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715029833; cv=none; b=eWfLqBaSglONHqRnCohHHzQcz3lB+IKRIANhpJK/tJzh1i+xEQnLiV9L24y9yhiV29WRI/QsxqOCmngdY+8+3rWo8LH0nAX89G+sE2SBczwns+f1g95NI5oeIB8iyDLRWPHRwyDiSapYv8gBa4sR+OKKYj+MRzGYgqBNDdqikuE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715029833; c=relaxed/simple; bh=13OxCCXFD0dQ5I30NApvqTwlwhaZmASYM9bf5Kr18JA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Zm7wcSShJYGG7/QfMGDiBlV30vxAiu5FBZwmHU1wdes9H7wsstYVuL37If4jnVohtIyjqZSA1yJXKBnBtHUtGYZH/O6P95tE0leaq3sCf5gMns1ghRVdkKIdz46sYyl/zch7E/fjYjtgTaLooAX9Mvsw83+Sl7lMH/jGlGIdmdg= 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=XwKYLAWH; arc=none smtp.client-ip=209.85.222.174 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="XwKYLAWH" Received: by mail-qk1-f174.google.com with SMTP id af79cd13be357-78ecd752a7cso194504385a.0 for ; Mon, 06 May 2024 14:10:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715029831; x=1715634631; 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=OkJF7t/M3Ojeyh5RuyGpe29BYnbPsSXvxhXAxpUcSgA=; b=XwKYLAWHKeesNbtDBek7g9IS4gFMiv9eI9W2VBr5egHx5PloHV7HSKt4OlzddnYsH+ QBZeHbgd3jXjI7ickZ4evj85OGAlPcfdzHoNL7sBJoAjJ4SBBGUpEK7BMIMjZrP1A7Gd x8GnPM9FZaD0w/uVfEWgbMbcgZvuNDCI25M8M= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715029831; x=1715634631; 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=OkJF7t/M3Ojeyh5RuyGpe29BYnbPsSXvxhXAxpUcSgA=; b=HFFOCFRglgAEL3J16hj6t4jh1e9qomHpY6TGhtwQu1UuuZT5yGAPu5XhYtadt2nfXe 0CznIwgTbv5sFrKtzOnowplBgNS7mbVU6//FInxv9KJsz+neZzdfancgQCS7UxI/cVmC xIRDPw1wNEAh2m8v12SWRVbY6LuTHwm6Qjue3R79j4LiG8kF9yRxYH/jhU3yCltuTIYs qeOmzLGIeMSSLUhkt5Rkdqsocp1ndKP+htm0oQ472rPFRZC1ljDiV8mTdK0a7SP0FVRC kOBoHpzVFtI9vm7yv3w0y82h2xhw7rl3yOlTwi5JozMZHqKrY6v9AvXSks56F70he/tB WvfA== X-Forwarded-Encrypted: i=1; AJvYcCWUm9Nsn3bqKOeDwIKv+BFsW9RbIvVLNoLfBsgBAiQU7Ufa1RzGQ3LkagPWU8mMPTurnbEfTAB3u8QiP+UpmKRmAYThNZLM7SoNK38= X-Gm-Message-State: AOJu0YwwJr1GvZhk/EANmKqKh+smCXYPeYPDbDmfnL6FtYENTm/rT2a+ Jw5xmfvXAE9A/o8cfO1X76ddQaqNRhNgLm2z7GegTmsfO3T2EpEfFdWDQRGJ7w== X-Google-Smtp-Source: AGHT+IEHqq8p4xvadiUkZkjltTHuOSmpKAULLj6lKhTtpITVnQwDNjpo5F7If+QRhvKAuXMvz0wKiQ== X-Received: by 2002:a05:620a:2481:b0:792:8c4c:375c with SMTP id i1-20020a05620a248100b007928c4c375cmr9980404qkn.11.1715029830998; Mon, 06 May 2024 14:10:30 -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.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 14:10:30 -0700 (PDT) From: Ricardo Ribalda Date: Mon, 06 May 2024 21:10:28 +0000 Subject: [PATCH 3/5] media: dvb-frontends/stv090x: Refactor tuner_i2c_lock Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240506-cocci-locks-v1-3-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 lock logic to it's own function. There is less code duplication and cocci is much happier. Fix the following cocci warning: drivers/media/dvb-frontends/stv090x.c:799:1-7: preceding lock on line 768 Signed-off-by: Ricardo Ribalda --- drivers/media/dvb-frontends/stv090x.c | 37 ++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/drivers/media/dvb-frontends/stv090x.c b/drivers/media/dvb-frontends/stv090x.c index cc45139057ba..3b02d504941f 100644 --- a/drivers/media/dvb-frontends/stv090x.c +++ b/drivers/media/dvb-frontends/stv090x.c @@ -748,6 +748,22 @@ static int stv090x_write_reg(struct stv090x_state *state, unsigned int reg, u8 d return stv090x_write_regs(state, reg, &tmp, 1); } +static inline void stv090x_tuner_i2c_lock(struct stv090x_state *state) +{ + if (state->config->tuner_i2c_lock) + state->config->tuner_i2c_lock(&state->frontend, 1); + else + mutex_lock(&state->internal->tuner_lock); +} + +static inline void stv090x_tuner_i2c_unlock(struct stv090x_state *state) +{ + if (state->config->tuner_i2c_lock) + state->config->tuner_i2c_lock(&state->frontend, 0); + else + mutex_unlock(&state->internal->tuner_lock); +} + static int stv090x_i2c_gate_ctrl(struct stv090x_state *state, int enable) { u32 reg; @@ -761,12 +777,8 @@ static int stv090x_i2c_gate_ctrl(struct stv090x_state *state, int enable) * In case of any error, the lock is unlocked and exit within the * relevant operations themselves. */ - if (enable) { - if (state->config->tuner_i2c_lock) - state->config->tuner_i2c_lock(&state->frontend, 1); - else - mutex_lock(&state->internal->tuner_lock); - } + if (enable) + stv090x_tuner_i2c_lock(state); reg = STV090x_READ_DEMOD(state, I2CRPT); if (enable) { @@ -782,20 +794,13 @@ static int stv090x_i2c_gate_ctrl(struct stv090x_state *state, int enable) goto err; } - if (!enable) { - if (state->config->tuner_i2c_lock) - state->config->tuner_i2c_lock(&state->frontend, 0); - else - mutex_unlock(&state->internal->tuner_lock); - } + if (!enable) + stv090x_tuner_i2c_unlock(state); return 0; err: dprintk(FE_ERROR, 1, "I/O error"); - if (state->config->tuner_i2c_lock) - state->config->tuner_i2c_lock(&state->frontend, 0); - else - mutex_unlock(&state->internal->tuner_lock); + stv090x_tuner_i2c_unlock(state); return -1; } From patchwork Mon May 6 21:10:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 13656008 Received: from mail-oi1-f175.google.com (mail-oi1-f175.google.com [209.85.167.175]) (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 A262115CD40 for ; Mon, 6 May 2024 21:10:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715029834; cv=none; b=dMcqyIEPKEke5Mw0SvdUOgp54zqu9iqivR+f0d+xE0bIanqp91f8sMRWUJhvxea9fNteNr/0ke5Fxg/JaJKUFP9svHBjZm89e+Htp2Gz2eDo4zs7EG6X+4LdErTyNuTAtEMiu1ABK2P3rAt5hCLX4z+aOU7DyPLll6K/ywYg6u4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715029834; c=relaxed/simple; bh=7MOAwd4bBULwL5xOTaUt6yrPZS3n8PS3P9PINQKXayg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=OkTpoa7WoE+/ambP2jOdx0Z3JfKc0y3i66cqU2t8QH8wyEcX/hK7TSMZzAzmiqhcul/HhaU7IHALRuPZhx8n7lOUcXcsbpXhTgA5MEaMicfgKFBkGTmEfEG2ZBBcN8+80P9uQ6VyAvn8Zosfskx3PFDByZ2w8LAbtkGKzJOFNwk= 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=ACUBShd9; arc=none smtp.client-ip=209.85.167.175 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="ACUBShd9" Received: by mail-oi1-f175.google.com with SMTP id 5614622812f47-3c70d8bb618so1165343b6e.2 for ; Mon, 06 May 2024 14:10:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715029832; x=1715634632; 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=Sqvg3kz5s9OxeYpuWk0/MXqOrMxTZPD2syeSEOk+5RQ=; b=ACUBShd9mbIa0Ya5TH/mHs1ksWYaiB+sy6sebrjYnuUTAxOXIP5UU0viwRGzD9rkeB ByMgehQ2J/uHpvrIdr6VaXgOVLli5o+Sd0yVXVdSxLW4Rt60MWemqk10rloGE0BJaT2F 8JPJwXszS85PTNOT5oIQj7C8JSNgwteMjAxzw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715029832; x=1715634632; 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=Sqvg3kz5s9OxeYpuWk0/MXqOrMxTZPD2syeSEOk+5RQ=; b=hzZkIGvMcsJqy+cIhvIDqysetjqmpUCb5RXCLd74NpZr7w/ZY35QZeSIh8gyBooOtJ ATLpUMiKYhYWCjaab1+6dOx5qrf+8uTvvHLpiBvx6b051khiiDbMokWp3Ni6sr6+uIQn EQ7wKRhOLnM3t8LxTGuODnpAqLI39Dd4pSeJ9cCmLOAwR1ogH5VJbPibo94W9Q06aTlQ kyhWhGjCPMq4pgp04mdF7tRL+L7J/h1U5f5JxhpA2qmWs1a4QDbWN4IalFX8NcDW8x0x njfFZsAtJTCBR7Xkm9AlJVDp1luft7+EWA6XiyyFqBdoWD111n6DxyXYxG62T49Jp5w9 00Bw== X-Forwarded-Encrypted: i=1; AJvYcCXdix+9RgYk6r45xCzYTfCmOf7F3bpmI0OInuJpIPWqwI35KNfFRLfUXIYt9yFlaSJtxNP0rrU4TTr1On64rFVR/y+ZlcAetdZx+ek= X-Gm-Message-State: AOJu0Yy8lAb8ClXtXAFzYaZpvNKua3/M49sB8V5p0ByAsBP4BUdgXa53 fJKxWHECyri5ef8r6qEkqrNlDzQ5e5Ozg5XmWikWepf8jm3mppYKVJtTZ7YGMA== X-Google-Smtp-Source: AGHT+IEAFuuBQrD8KQbGry8SYQjhcxEUI+G4myA4NMTrdT2JnKJdlengkUCK51yDD5mFW/bTmbxyNQ== X-Received: by 2002:a05:6871:341b:b0:22e:bcfd:debc with SMTP id nh27-20020a056871341b00b0022ebcfddebcmr16281376oac.13.1715029831841; Mon, 06 May 2024 14:10:31 -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:31 -0700 (PDT) From: Ricardo Ribalda Date: Mon, 06 May 2024 21:10:29 +0000 Subject: [PATCH 4/5] media: go7007: Refactor Adlink PCI-MPG24 i2c mutex Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240506-cocci-locks-v1-4-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 lock/unlock to its own function. It makes the code cleaner and cocci happier. Fix the following cocci warning: drivers/media/usb/go7007/go7007-i2c.c:125:1-7: preceding lock on line 61 Signed-off-by: Ricardo Ribalda --- drivers/media/usb/go7007/go7007-i2c.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/drivers/media/usb/go7007/go7007-i2c.c b/drivers/media/usb/go7007/go7007-i2c.c index 2880370e45c8..f6ce28a4a768 100644 --- a/drivers/media/usb/go7007/go7007-i2c.c +++ b/drivers/media/usb/go7007/go7007-i2c.c @@ -33,7 +33,21 @@ /* There is only one I2C port on the TW2804 that feeds all four GO7007 VIPs * on the Adlink PCI-MPG24, so access is shared between all of them. */ -static DEFINE_MUTEX(adlink_mpg24_i2c_lock); +static DEFINE_MUTEX(adlink_mpg24_i2c_mutex); + +static inline void adlink_mpg24_i2c_lock(struct go7007 *go) +{ + /* Bridge the I2C port on this GO7007 to the shared bus */ + mutex_lock(&adlink_mpg24_i2c_mutex); + go7007_write_addr(go, 0x3c82, 0x0020); +} + +static inline void adlink_mpg24_i2c_unlock(struct go7007 *go) +{ + /* Isolate the I2C port on this GO7007 from the shared bus */ + go7007_write_addr(go, 0x3c82, 0x0000); + mutex_unlock(&adlink_mpg24_i2c_mutex); +} static int go7007_i2c_xfer(struct go7007 *go, u16 addr, int read, u16 command, int flags, u8 *data) @@ -56,11 +70,8 @@ static int go7007_i2c_xfer(struct go7007 *go, u16 addr, int read, mutex_lock(&go->hw_lock); - if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) { - /* Bridge the I2C port on this GO7007 to the shared bus */ - mutex_lock(&adlink_mpg24_i2c_lock); - go7007_write_addr(go, 0x3c82, 0x0020); - } + if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) + adlink_mpg24_i2c_lock(go); /* Wait for I2C adapter to be ready */ for (i = 0; i < 10; ++i) { @@ -116,11 +127,8 @@ static int go7007_i2c_xfer(struct go7007 *go, u16 addr, int read, ret = 0; i2c_done: - if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) { - /* Isolate the I2C port on this GO7007 from the shared bus */ - go7007_write_addr(go, 0x3c82, 0x0000); - mutex_unlock(&adlink_mpg24_i2c_lock); - } + if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) + adlink_mpg24_i2c_unlock(go); mutex_unlock(&go->hw_lock); return ret; } From patchwork Mon May 6 21:10:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 13656009 Received: from mail-oa1-f42.google.com (mail-oa1-f42.google.com [209.85.160.42]) (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 2AD2715D5B3 for ; Mon, 6 May 2024 21:10:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715029835; cv=none; b=nbY3jjoVFIax46j4cieWK86DL+8+DSKqF3lsIKKfmgYzGluze0htJgAAiDaEqRrH0F9gbzBESDxDSJ43vph0mq6KspnCTNTQ/u66/2vFzGxVNu5aDH5r9wwv1syhEonHnXQQHhTeWUYJQsJ6EnKfd+AeUfaYQYNNA5zQ+Qs/bCE= 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.42 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="crnIXjPE" Received: by mail-oa1-f42.google.com with SMTP id 586e51a60fabf-22ed075a629so1467928fac.3 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=A//AHhRuJeLrHMY0p5NtpJUi1xwMPrMNVW/Deboes0LfZbhGjnwkwfki/5m5ZvrfAD K+lIpdV30osSPltOdKmG1JpYMDijkZqaFmwV0AnM6+xRXQdqH3XcfECTapuJ34jaeKy/ kXzWFyID+zEnvdGL28Hj5DjT9wO8UqByBQz6/65BWYnUyeGKlMU/v+N+djasiX6tBmcY pqhXRxCHzHsZqCFKDxtAFyJgozWGJV/6HPrRNQNHnOdK1MY9sDrYmRGT6LfN20dEkgyo N5bGXe3vZ+zSQNwYHeqIac+oD8KjOPaZMY4/kZEktQtGuSlAycJodCb3s0ZBcuwMyEIK TFVA== X-Forwarded-Encrypted: i=1; AJvYcCUCosBZGcY9Woe1Z1VyOIWRbKfAsD77dZP9CFbxE21N7ja9eP2tJnU8ulhfY8hpaRS4s7P8KDweadU/ImZcZ3tYColatjuDPe3KtgI= X-Gm-Message-State: AOJu0YyMy5BQ0pNPMde/JlH2MWpG+sHY0Yevhg9Qjt4i2y45DUD/NTyz tPmjtSfJ9DjpfXBc0Ezk+oGzuUDvJsCmuen9y/lv78rQRo+BJGoV9S3061cLqA== X-Google-Smtp-Source: AGHT+IFYjs3nRF6SaoGCfVOA1vP4x0YulqSRg/VGAqs+ro65Kw9a73H+Z1IGlXP/lC5je220W3Ok6A== 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-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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;