From nobody Wed Nov 29 16:38:03 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4SgQ3v64gjz52wXF; Wed, 29 Nov 2023 16:38:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4SgQ3v3J2Mz3HBv; Wed, 29 Nov 2023 16:38:03 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1701275883; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=IuSqTQnn1rjx39erw8yzVxEyZPIGCzJlmg192IKnHJM=; b=Hiz+YCAXt0KKDolMyoJdUZjoXaPnSlLzb6PV0FNfK0qAkyWh3raMH2VmHWiQ/iN+b5J2K6 7fT4P+NVN7Ts3DhX9+waJgoWZSUmo2MElOSytaACKyXmcxx/k35u2UKELmhcou6r+DGwKh tNWKeDLNcuhWc9pU1HchbRbPGsjrFiTD0gdYURGAktZh3WZiBvYSJ2+BbMqsRApDRVBkqD doMJUMkZfju8bDmYGEzF997opgVikaVKg4pJugpyZ79hzCbtzGX5X/WCTk2dj/Uo/w6FGd aKLhmRBwJ0DFjOIzxvTLoPJvT0XREIVyyr84uxnqYNOCoc0xv5Fyzbsa2YtJnw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1701275883; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=IuSqTQnn1rjx39erw8yzVxEyZPIGCzJlmg192IKnHJM=; b=hkui4G+tECQGPCS3TwcK13zXGYtXB7GnawXrIooxaVL4t3oRBpvOPL2SrzXmikI+fLDYkq 3FAnWQpHQh87i5t23qFrKhv8ntxfuZLeB3sebso5sWdEIioSIC1WzHi6AZ7ls5BbmiUwHy cFqiAkBJ/JtlrXTofeRtqF0ZqeSYjpbLt/7itodun7MfGBE6n0TUjvRwWo9hWtzTBsjt4o 6EDh931WH71b70WJR0N5yWVi1S/FXrWqMG2DlGSjrlWBhthqF6IlPNmBpfnlzvM4ILXn49 1cA/AEaVfcC+7//Of+9eak2sJkg9anaMI+KaE34/xf4x65Q8adD3kQmvhI9Sdw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1701275883; a=rsa-sha256; cv=none; b=X51W/QOsRH8vFnIkz6s/OnmF2E9ZP7hQUWXm5Z+FMAIiGUolvXOOUpdcno9h4qnmxi0eVX Jtjw4T156jq+ZFidaMDawiZRO9mRgw9On1gyqgnXJgKBiStPFD6+viF5KsR4QGNYG3cBB5 9H1IAkN4XWoVj3BqcKqUCU44uM5rZzipOxMDgMl8C5OEuXlwelXd+mT3tdhTc/LT0GPjwn rpvtkh9C1GcGA5u2d2QaLiW5lN1+UsAC4tYW5plN1kImFaPbqdtUfTGpxHHeT3yHPwsu1N /8Vg3+BZxzGaajVX6LbvkSX0U19QrPDXfJbBc6Tl5LqoyM+ghHEGVlbUro9uUA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4SgQ3v2632z1CZw; Wed, 29 Nov 2023 16:38:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3ATGc3KN044815; Wed, 29 Nov 2023 16:38:03 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3ATGc3TP044812; Wed, 29 Nov 2023 16:38:03 GMT (envelope-from git) Date: Wed, 29 Nov 2023 16:38:03 GMT Message-Id: <202311291638.3ATGc3TP044812@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: "Bjoern A. Zeeb" Subject: git: 934b2489b951 - stable/13 - linuxkpi: Fix __sg_alloc_table_from_pages loop List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bz X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 934b2489b951698adc5d4c3421fa66fb80be7052 Auto-Submitted: auto-generated The branch stable/13 has been updated by bz: URL: https://cgit.FreeBSD.org/src/commit/?id=934b2489b951698adc5d4c3421fa66fb80be7052 commit 934b2489b951698adc5d4c3421fa66fb80be7052 Author: Austin Shafer AuthorDate: 2023-04-21 07:56:50 +0000 Commit: Bjoern A. Zeeb CommitDate: 2023-11-29 16:36:05 +0000 linuxkpi: Fix __sg_alloc_table_from_pages loop Commit 3e0856b63fe0e375a0951e05c2ef98bb2ebd9421 updated __sg_alloc_table_from_pages to use the same API as linux, but modified the loop condition when going over the pages in a sg list. Part of the change included moving the sg_next call out of the for loop and into the body, which causes an off by one error when traversing the list. Since sg_next is called before the loop body it will skip the first element and read one past the last element. This caused panics when running PRIME with nvidia-drm as the off-by-one issue causes a NULL dereference. (cherry picked from commit 3f686532c9b4fd603d4ad2d3392232cff24188f4) Commit 3f686532c9b4 tried to fix an issue with not properly starting at the first page in the sg list to prevent a panic. This worked but with the side effect of incrementing "s" during the final iteration causing it to be NULL since the list had ended. In cases non-DEBUG kernels this causes a panic with drm-5.15, since "s" is NULL when we later pass it to sg_mark_end(). This change decouples the iteration sg from the return value so that it is never incremented past the final page in the chain. (cherry picked from commit 09b0401e91a92bcb58ea1873857b42f8211f660f) --- sys/compat/linuxkpi/common/include/linux/scatterlist.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/sys/compat/linuxkpi/common/include/linux/scatterlist.h b/sys/compat/linuxkpi/common/include/linux/scatterlist.h index 17928f9c50e5..e462d5c649f1 100644 --- a/sys/compat/linuxkpi/common/include/linux/scatterlist.h +++ b/sys/compat/linuxkpi/common/include/linux/scatterlist.h @@ -343,7 +343,7 @@ __sg_alloc_table_from_pages(struct sg_table *sgt, { unsigned int i, segs, cur, len; int rc; - struct scatterlist *s; + struct scatterlist *s, *sg_iter; #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 51300 if (prv != NULL) { @@ -377,11 +377,17 @@ __sg_alloc_table_from_pages(struct sg_table *sgt, #endif cur = 0; - for (i = 0, s = sgt->sgl; i < sgt->orig_nents; i++) { + for_each_sg(sgt->sgl, sg_iter, sgt->orig_nents, i) { unsigned long seg_size; unsigned int j; - s = sg_next(s); + /* + * We need to make sure that when we exit this loop "s" has the + * last sg in the chain so we can call sg_mark_end() on it. + * Only set this inside the loop since sg_iter will be iterated + * until it is NULL. + */ + s = sg_iter; len = 0; for (j = cur + 1; j < count; ++j) {