linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Tuo Li <islituo@gmail.com>
To: s.nawrocki@samsung.com, mchehab@kernel.org,
	krzysztof.kozlowski@linaro.org, alim.akhtar@samsung.com
Cc: linux-media@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-samsung-soc@vger.kernel.org, linux-kernel@vger.kernel.org,
	baijiaju1990@gmail.com, Tuo Li <islituo@gmail.com>,
	stable@vger.kernel.org, BassCheck <bass@buaa.edu.cn>
Subject: [PATCH] media:fimc-capture: Fix a possible data inconsistency due to a data race in fimc_subdev_set_fmt()
Date: Sun, 24 Dec 2023 00:43:51 +0800	[thread overview]
Message-ID: <20231223164351.3521588-1-islituo@gmail.com> (raw)

Accesses to ctx->s_frame.width and ctx->s_frame.height should be protected
by the lock fimc->lock to guarantee that width and height are consistent.
Here is an example in fimc_subdev_get_fmt():

  struct fimc_frame *ff = &ctx->s_frame; // Alias
  mutex_lock(&fimc->lock);
  mf->width = ff->width;
  mf->height = ff->height;

However, ctx->s_frame.width and ctx->s_frame.height are accessed without 
holding the lock fimc->lock in fimc_subdev_set_fmt():

  mf->width = ctx->s_frame.width;
  mf->height = ctx->s_frame.height;

And thus a harmful data race can occur, which can make ctx->s_frame.width
inconsistent with ctx->s_frame.height, if ctx->s_frame.height is updated 
right after ctx->s_frame.width is accessed by another thread.

This possible bug is found by an experimental static analysis tool
developed by our team, BassCheck[1]. This tool analyzes the locking APIs
to extract function pairs that can be concurrently executed, and then
analyzes the instructions in the paired functions to identify possible
concurrency bugs including data races and atomicity violations. The above
possible bug is reported when our tool analyzes the source code of
Linux 6.2.

To fix this possible data race, the lock operation mutex_lock(&fimc->lock)
is moved to the front of the accesses to these two variables. With this 
patch applied, our tool no longer reports the bug, with the kernel 
configuration allyesconfig for x86_64. Due to the lack of associated 
hardware, we cannot test the patch in runtime testing, and just verify it 
according to the code logic.

[1] https://sites.google.com/view/basscheck/

Fixes: 88fa8311ee36 ("[media] s5p-fimc: Add support for ISP Writeback ...")
Signed-off-by: Tuo Li <islituo@gmail.com>
Cc: stable@vger.kernel.org
Reported-by: BassCheck <bass@buaa.edu.cn>
---
 drivers/media/platform/samsung/exynos4-is/fimc-capture.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/media/platform/samsung/exynos4-is/fimc-capture.c b/drivers/media/platform/samsung/exynos4-is/fimc-capture.c
index a0d43bf892e6..5c8b67f92c65 100644
--- a/drivers/media/platform/samsung/exynos4-is/fimc-capture.c
+++ b/drivers/media/platform/samsung/exynos4-is/fimc-capture.c
@@ -1546,6 +1546,7 @@ static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
 	fimc_alpha_ctrl_update(ctx);
 
 	fimc_capture_mark_jpeg_xfer(ctx, ffmt->color);
+	mutex_lock(&fimc->lock);
 	if (fmt->pad == FIMC_SD_PAD_SOURCE) {
 		ff = &ctx->d_frame;
 		/* Sink pads crop rectangle size */
@@ -1555,7 +1556,6 @@ static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
 		ff = &ctx->s_frame;
 	}
 
-	mutex_lock(&fimc->lock);
 	set_frame_bounds(ff, mf->width, mf->height);
 
 	if (fmt->pad == FIMC_SD_PAD_SINK_FIFO)
-- 
2.34.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

             reply	other threads:[~2023-12-23 16:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-23 16:43 Tuo Li [this message]
2023-12-24 10:11 ` [PATCH] media:fimc-capture: Fix a possible data inconsistency due to a data race in fimc_subdev_set_fmt() Krzysztof Kozlowski
2023-12-25 14:57   ` Li Tuo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231223164351.3521588-1-islituo@gmail.com \
    --to=islituo@gmail.com \
    --cc=alim.akhtar@samsung.com \
    --cc=baijiaju1990@gmail.com \
    --cc=bass@buaa.edu.cn \
    --cc=krzysztof.kozlowski@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-samsung-soc@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=s.nawrocki@samsung.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).