From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 95FED330B29 for ; Wed, 17 Jun 2026 17:21:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781716869; cv=none; b=raTkpTD5DFpZ/IVLNEJZVBIFS/WSuAcmjLUY8YUH/xLsYiJGKugqI7DiM3Fu+z6Dsr1bFG3+iw91JwouBfaXYldioxHHD/MG/V5Xcly8PFE+MmWePrReLhJl3JEPTvdLcQKz8BZ/ylc1OW2n7mN5ISxZ3QuP0D5lK1VzWWHFOPA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781716869; c=relaxed/simple; bh=Jzodu++ffHxmP/q02T+KOXGBTZRsCsqgPHX87KDMyVM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=s+nIkTb3p6Qw9WunIeuCugd+8qX2e/3icNpANBTIGsDTgCoEfYJQq6ZnHx6na9x3xMe/1Eg+JYziD1IXT5Ttl97BUAb2RrfqervoBuhsQoyPOxepouzjOMFev7FpjqpHtzaNBy04JF6PgJ8VCHgq65UI8kppLrXtdemGBSh83yE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FD1kk15r; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FD1kk15r" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A88021F00ACA; Wed, 17 Jun 2026 17:21:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1781716865; bh=X3LnKzloJ62UsW2+8JEBcwYLDvkOFwmb7H+dvI/596Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=FD1kk15r+B+qQHnfVqPcDsa5hacHQ8qh1ipylJ7SCP4TLx8mqrIsU8xLHvLDJ2LpD JaRZGsGpdqZOEwMKoY1EvxcwGdTpgR/A4B6xRkvpXTbWbo80Ta2uIDzZdkRra4u60s 8d9EdNoAtDieBmp+Nsj6tgFVfKSjRW8127N7O9x4fdkCT7ulw54TOrtXaowAK8fnsk Qd0mP5bZKgcusGlNagZwEmfPlA1spS3Nht6vlpM9Cq0XPICM1uOZ3LMhqpjQqUc7Mo aFtrxrA4Gjl7xHtYgkYob328+OdpxuCtnhUmVsoowDvJJiNUGMO3ebzBaEzowk21tC szaG3V3zve/7Q== From: Sasha Levin To: stable@vger.kernel.org Cc: Tudor Ambarus , Krzysztof Kozlowski , Sasha Levin Subject: [PATCH 6.18.y 6/6] firmware: samsung: acpm: Fix missing LKMM barriers in sequence allocator Date: Wed, 17 Jun 2026 13:20:58 -0400 Message-ID: <20260617172058.253463-6-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260617172058.253463-1-sashal@kernel.org> References: <2026061553-curly-gigahertz-3ad1@gregkh> <20260617172058.253463-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Tudor Ambarus [ Upstream commit bf296f83a3ddab1ab875edc4e8862cb10553064f ] Sashiko identified memory ordering races in [1]. The ACPM driver uses a globally shared 'bitmap_seqnum' to track available sequence numbers. Even though threads now strictly free their own sequence numbers, the allocation and freeing of these bits across concurrent threads are effectively lockless operations and require explicit LKMM memory barriers. Previously, the driver used plain bitwise operators (test_bit, set_bit, clear_bit), which lack ordering guarantees. This creates two race conditions on weakly ordered architectures like ARM64: 1. Polling Release Violation: The polling thread copies its payload and calls clear_bit(). Without a release barrier, the CPU can reorder the memory operations, making the cleared bit globally visible before the payload reads have fully completed. 2. TX Acquire Violation: The TX thread loops on test_bit(), calls set_bit(), and then wipes the payload buffer via memset(). Without an acquire barrier, the CPU can speculatively execute the memset() before the bit is safely and formally claimed. If these reorderings overlap, a new TX thread can claim the sequence number and overwrite the buffer while the original polling thread is still actively reading from it. Fix this by upgrading the bitwise operators. Wrap the TX allocation in test_and_set_bit_lock() to establish formal LKMM Acquire semantics, and pair it with clear_bit_unlock() in the polling path to enforce Release semantics. Cc: stable@vger.kernel.org Fixes: a88927b534ba ("firmware: add Exynos ACPM protocol driver") Closes: https://sashiko.dev/#/patchset/20260423-acpm-fixes-sashiko-reports-v1-0-2217b790925e%40linaro.org [1] Signed-off-by: Tudor Ambarus Link: https://patch.msgid.link/20260505-acpm-fixes-sashiko-reports-v5-6-43b5ee7f1674@linaro.org Signed-off-by: Krzysztof Kozlowski Signed-off-by: Sasha Levin --- drivers/firmware/samsung/exynos-acpm.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/drivers/firmware/samsung/exynos-acpm.c b/drivers/firmware/samsung/exynos-acpm.c index eaa52aeede3772..f3873d2edcb3ad 100644 --- a/drivers/firmware/samsung/exynos-acpm.c +++ b/drivers/firmware/samsung/exynos-acpm.c @@ -7,7 +7,7 @@ #include #include -#include +#include #include #include #include @@ -337,7 +337,7 @@ static int acpm_dequeue_by_polling(struct acpm_chan *achan, acpm_get_saved_rx(achan, xfer, seqnum); /* Relinquish ownership of the sequence slot */ - clear_bit(seqnum - 1, achan->bitmap_seqnum); + clear_bit_unlock(seqnum - 1, achan->bitmap_seqnum); return 0; } @@ -394,11 +394,18 @@ static void acpm_prepare_xfer(struct acpm_chan *achan, struct acpm_rx_data *rx_data; u32 *txd = (u32 *)xfer->txd; - /* Prevent chan->seqnum from being re-used */ + /* + * Prevent chan->seqnum from being re-used. + * test_and_set_bit_lock() provides formal LKMM Acquire semantics. + * It pairs with the RX thread's clear_bit_unlock() to ensure the CPU + * does not speculatively execute the rx_data buffer wipe (memset) + * before the sequence number is safely claimed. + */ do { if (++achan->seqnum == ACPM_SEQNUM_MAX) achan->seqnum = 1; - } while (test_bit(achan->seqnum - 1, achan->bitmap_seqnum)); + /* Flag the index based on seqnum. (seqnum: 1~63, bitmap: 0~62) */ + } while (test_and_set_bit_lock(achan->seqnum - 1, achan->bitmap_seqnum)); txd[0] |= FIELD_PREP(ACPM_PROTOCOL_SEQNUM, achan->seqnum); @@ -408,9 +415,6 @@ static void acpm_prepare_xfer(struct acpm_chan *achan, memset(rx_data->cmd, 0, sizeof(*rx_data->cmd) * rx_data->n_cmd); /* zero means no response expected */ rx_data->rxcnt = xfer->rxcnt; - - /* Flag the index based on seqnum. (seqnum: 1~63, bitmap: 0~62) */ - set_bit(achan->seqnum - 1, achan->bitmap_seqnum); } /** -- 2.53.0