From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 286E7301471; Tue, 31 Mar 2026 17:03:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774976598; cv=none; b=sNkh1pb1qnmLeNMuel8mpBvCgSdUlZbtFkYm8FszttsjPbS6esHVgkfD/izTx2NlBstftX5ujutRJhqx3nYFqx6vFbQjbSMnG2btocZgndde+Zn0LXxBdUU/OAPTGQBoBQd1O+eQ04eEgHh3Z/HR4lb4XY3h6e+GKICsjXnURo0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774976598; c=relaxed/simple; bh=oaR8EDExvBD5TONrSceMg1+ZANHjvVrNCfBIsYOQor0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NERQ91BNhaN/HlYKebkIFKhtniNuh/v0RIp7gFW1sDkJlogfDpNxv/Ju3YXRh9EVNgF7GvHD2G2yBxDxwfKJ48MhwCXxk1W3e1oOAMpoZM+Hv/MOU5t0ajqjfzD0mucmvFwckoacpNETiL0OZD/CFAYKotd0U5P8bNFu8x/BPb0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=URYz8QTw; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="URYz8QTw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 57D94C19423; Tue, 31 Mar 2026 17:03:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1774976597; bh=oaR8EDExvBD5TONrSceMg1+ZANHjvVrNCfBIsYOQor0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=URYz8QTwR2YGkDoVIgi58uFeUOpt5q6EbyXeWu4bK0kPh2MTECPQ948Xnk5Ghaf8P Ry+E6msm9dah8J24RMsQXbHmm50iOHrMan6ULqmIVQVTE9niht8BRHVmcUlD8Wb+CB Z/8Nh2BZaTVgTBEFWlbbXqcgVJ8KuAp1fDv25To0= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Alberto Garcia , Brian Geffon , "Rafael J. Wysocki" , Sasha Levin Subject: [PATCH 6.18 154/309] PM: hibernate: Drain trailing zero pages on userspace restore Date: Tue, 31 Mar 2026 18:20:57 +0200 Message-ID: <20260331161759.138713866@linuxfoundation.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260331161753.468533260@linuxfoundation.org> References: <20260331161753.468533260@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Alberto Garcia [ Upstream commit 734eba62cd32cb9ceffa09e57cdc03d761528525 ] Commit 005e8dddd497 ("PM: hibernate: don't store zero pages in the image file") added an optimization to skip zero-filled pages in the hibernation image. On restore, zero pages are handled internally by snapshot_write_next() in a loop that processes them without returning to the caller. With the userspace restore interface, writing the last non-zero page to /dev/snapshot is followed by the SNAPSHOT_ATOMIC_RESTORE ioctl. At this point there are no more calls to snapshot_write_next() so any trailing zero pages are not processed, snapshot_image_loaded() fails because handle->cur is smaller than expected, the ioctl returns -EPERM and the image is not restored. The in-kernel restore path is not affected by this because the loop in load_image() in swap.c calls snapshot_write_next() until it returns 0. It is this final call that drains any trailing zero pages. Fixed by calling snapshot_write_next() in snapshot_write_finalize(), giving the kernel the chance to drain any trailing zero pages. Fixes: 005e8dddd497 ("PM: hibernate: don't store zero pages in the image file") Signed-off-by: Alberto Garcia Acked-by: Brian Geffon Link: https://patch.msgid.link/ef5a7c5e3e3dbd17dcb20efaa0c53a47a23498bb.1773075892.git.berto@igalia.com Signed-off-by: Rafael J. Wysocki Signed-off-by: Sasha Levin --- kernel/power/snapshot.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c index 645f42e404789..e249e5786fbcd 100644 --- a/kernel/power/snapshot.c +++ b/kernel/power/snapshot.c @@ -2856,6 +2856,17 @@ int snapshot_write_finalize(struct snapshot_handle *handle) { int error; + /* + * Call snapshot_write_next() to drain any trailing zero pages, + * but make sure we're in the data page region first. + * This function can return PAGE_SIZE if the kernel was expecting + * another copy page. Return -ENODATA in that situation. + */ + if (handle->cur > nr_meta_pages + 1) { + error = snapshot_write_next(handle); + if (error) + return error > 0 ? -ENODATA : error; + } copy_last_highmem_page(); error = hibernate_restore_protect_page(handle->buffer); /* Do that only if we have loaded the image entirely */ -- 2.53.0