From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (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 69085306752 for ; Sun, 31 May 2026 23:42:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780270943; cv=none; b=frtxambwdTD3hj/HK/0/fmBFzwfoWNCQj//4rhgf5ZYphuPLLt+QZWS74TI0ywrDG/PDts+ZK4rE7ds2OgFtZm9fy1pU3R9u3xmuYDNg8r5qRtppoSR6cfTlos0cKUhkohBAPBHrj9vgt+Hjv4RS+jGJiebr0VTCtQeer0ji5/4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780270943; c=relaxed/simple; bh=IcvenFvDSEeitw94OIsc1t/NptBefJ6gkPOKtVOU97M=; h=Date:From:To:Subject:Message-ID:Content-Type:MIME-Version; b=Xdfh2Ek2nrxTNor9z90eb1XUvqCXHR3Fda5zGZuich+vLyKUP2Dr1bpxZ4QNbheAguLzyx15b/LT2OCQHPXbYZESfRd3BFK3F0MZyUVpvu7Xz0B+lpSsP3yO7I/FyLxBFA6Cp0qJj395XBcUa3vb0mbqT9u/Qs1vOFXSp8Qimso= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Gf6jTFE+; arc=none smtp.client-ip=209.85.128.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Gf6jTFE+" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-490ac357c55so330445e9.1 for ; Sun, 31 May 2026 16:42:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780270941; x=1780875741; darn=vger.kernel.org; h=mime-version:content-transfer-encoding:message-id:subject:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=4TX5yQalO1hwVJTd6bVf4E6cp8wz9iOD9xEZdK1VlIg=; b=Gf6jTFE+SBN80lcTnz/Mpslzx2kdXAiSUa4edznkZ4TmaR/Qe+B+jTcnXtjaqpBaWK wsOiscIbo1OSeOtNO83DVdbuqEvTmtRu7qlSrGACuIhRjMaXbVokmRGj6seEKz+Ty2fz pFWvj7uzVyMwdHOwTs2dgYFz96xsoTKJyE65KkcBl2gqHou+Bc8GHQQUp30tSX6NvwNH 4YszT/fdUJiL8vxTnL76xkg8Lmzu7Zk/ePMasRdgz7f5pg3T+YkYEMQwZLsdeBGKzhhR gUdJAH/tbgFgzny3Auo9m0wJGIuuNJ9Q6Z17kmLmf54u6zdQgTn9a6TnMRRSMC3a6ia4 Q8fA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780270941; x=1780875741; h=mime-version:content-transfer-encoding:message-id:subject:to:from :date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4TX5yQalO1hwVJTd6bVf4E6cp8wz9iOD9xEZdK1VlIg=; b=BqWTlFUXYFVfaSHs5hbVT+ns6zr3Bf3hhldkfh5BHUYRYlxY4JhyDSCIIbI8oDynd1 3vRqBD2FgLatvYslq3l5FOil4lwJlfGmDrYI0x9bWD8TncHt7BujrvFWKjLOucHUVzLI YzB9skDlFN5xwGegD89r7VDu+uFZ/qa1Js/LUEyTZT9UA2oWlLXnKQxtYDwwFs6BgCz/ JUCQ7rnxTs2ALzZOt0KTNsDQQH/CXuKwARxD73Sdvb2TVsIJxmjC2EMeFYj/oetZmkMP 921adPEaWbaAHliewVP5UI6+2ZbhV0J3Zd7FlIiJZWutNI39b7KsYB1Z1FaikR3f8hNn 4Thg== X-Forwarded-Encrypted: i=1; AFNElJ/h2PfgztqsT6u986de5uw01Cgwzw3B5aEnJhn7Wa415f+CCD3DLLlEqXvT6c6GoxNg7hIDwD2ZNrg1fvg=@vger.kernel.org X-Gm-Message-State: AOJu0YwPlgM2ZVv/HJz2J22lo0Q+0/LBxZVdH/Mmts4CvWDXE/vXFgJA 7IxPc0FuD9oWs2X8uGo+BD3pmnAKxLW+1VC5Ni2sGL6XypHhNPvIWEMT X-Gm-Gg: Acq92OGf4Xw3FzkD39V7xYIzkEY6WKi3aKNt+D2LEbk0kfR1FeT33EN5sI33XcCJP8f ngQ+pf15mzAZsKjyxgQGwX3pyfPEAiUSpr3ilfNSOF74BZfgKYl5lF8JCXF3T7kPL+NLwUuFgCR CDhuP9AZYD6PVwVvredpfRVfCC6W4AQ6u/gUAlwoq/8PvgZQZPueJzmLd0EYUuQbI9SIewJojxt FYMo1A0cAJrI3DC4Me4uVoPqdETe3603PzG4dFy8+JfPi2IMC9Xd0bP5B0P5ZN8VhfotmGrm2F6 mxRjt20c2g9CaqUoZibzKcXy73SeAqTKS9Zik4wT74fUdttsSaXmNS2buB0MecHrtbe/J1stVuq XhyGOQTPvMiOvBlgp4mUEHP5ux5GkB4862lXRbOaaqjcGqLOBpbMsZ8AXw5myHpNdJ8Q5yEKp3U LH7BBQ1AviBTxyeSb7QX7rHS1XkVPeg9QPsS/n2+rH84GmElFLTBfmB2kP X-Received: by 2002:a05:600c:1d29:b0:490:a646:9d77 with SMTP id 5b1f17b1804b1-490a6469e58mr133187285e9.6.1780270940707; Sun, 31 May 2026 16:42:20 -0700 (PDT) Received: from [127.0.0.1] ([141.255.129.1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-45ef354cf0dsm21196831f8f.17.2026.05.31.16.42.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 May 2026 16:42:20 -0700 (PDT) Date: Sun, 31 May 2026 16:42:20 -0700 (PDT) From: Charles Pellegrini To: akpm@linux-foundation.org, egorenar-dev@posteo.net, robert.jarzmik@free.fr, t-pratham@ti.com, linux-kernel@vger.kernel.org Subject: [PATCH 0/2] lib: scatterlist: fix sg_split() partial-coverage geometry + add KUnit tests Message-ID: <178027099087.72481.1976843064458686851@gmail.com> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 sg_calculate_split() can decrement its nb_splits counter once too often and miss the loop-termination check, corrupting the last output split's geometry. This series fixes that and adds a KUnit suite. Patch 1 is a one-line fix: the termination check tests `!nb_splits`, but on partial coverage that ends mid-entry of a non-last input entry the counter overshoots to -1, so the loop runs one iteration too many and folds a trailing input entry into the last split (it ends up covering more bytes than the caller requested). Widening the check to `nb_splits <= 0` catches the overshoot. Patch 2 adds a KUnit suite (16 cases) over the (input shape, skip, split sizes) matrix: full / edge-aligned / mid-entry partial coverage, skip variants, and multi-split runs. The mid-entry cases fail without patch 1 and pass with it. Prior art / attribution. I hit this independently via property-based testing and only afterwards found that Alexander Egorenkov had reported it in 2021 [1], with an identical root-cause analysis. The independent rediscovery and the matching diagnosis are good evidence this is a real defect rather than a misreading of the partial-coverage contract. That 2021 posting went only to linux-kernel@vger, received no review, and was never applied -- this file has no MAINTAINERS entry, which is likely why, so I'm addressing the lib/ maintainer directly this time. Egorenkov's proposed fix moved the termination check rather than widening it, which fixes the mid-entry case but regresses edge-aligned partial coverage (a split ending exactly on an input-entry boundary with further entries trailing) -- a case the current code handles correctly. Test sg_split_t10_edge_first_two_trailing in patch 2 fails his fix and passes the one here, which is the concrete reason for choosing `nb_splits <= 0` over the reorder. He is kept as Reported-by; the diagnosis is his. Impact. No in-tree caller triggers this today. Four of the five callers (SEC, pxa_camera, spi-omap2-mcspi, and sa2ul via its DMA-mapped re-split) request full coverage; the one caller that requests partial coverage (DTHEv2 AEAD) bounds its consumer by cryptlen, masking the wrong geometry. The fix matters for callers that legitimately request partial coverage, which sg_split() has documented as supported since it was added ("the union of spans of all resulting scatter lists is a subrange of the span of the original scatter list"). Testing. Verified against the 16-case KUnit suite: 9 pass / 7 fail on mainline, 16/16 with patch 1 applied. The seven failures are exactly the mid-entry partial-coverage cases. The same 16 cases were first developed and run as a standalone userspace harness against an extracted copy of lib/sg_split.c, with identical results. [1] https://lore.kernel.org/all/20210418143425.22944-1-egorenar-dev@posteo.net/ Charles Pellegrini (2): lib: scatterlist: fix sg_calculate_split() nb_splits overshoot on partial coverage lib: scatterlist: add KUnit tests for sg_split() lib/Kconfig | 13 ++ lib/Makefile | 1 + lib/sg_split.c | 2 +- lib/test_sg_split_kunit.c | 371 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 386 insertions(+), 1 deletion(-) create mode 100644 lib/test_sg_split_kunit.c -- 2.47.3