From nobody Wed Apr 1 11:18:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 B306C342144 for ; Mon, 30 Mar 2026 15:16:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774883768; cv=none; b=s6J5aMqtGbo8CHSLSmA0gzqee0sYtorWfVS8bBP42itZhFjFadIPX6y4EPPhjYfYOeXhpvWA1JepEZcESHuKCUo07GmaemVefaEuuR0/hXe89lP2JhHtcSrDgnYNDQTjuM8G/np19qPVnjgvnfV0ayvqMxg8oMOKFsiZZ9uRKP0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774883768; c=relaxed/simple; bh=BrnkHbz+Dft8woX9qGSY6DNldonjJr0lsH0nr+uo33g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Xne1Eo4U+QYN/5qlfR1jMZ/GScE6c7U0+MUkajkAB4NNnmS9bgWcQYvUUCHx9lAFJ2LScPDCUW4FPxuF00IqMqZrvru4M/7PEqF5pwY6PAAdcP8/yiXWM3xoU06dLh+eUFthpW/fl5ndfvKh6JBjReSz28wS2Hvr+Go9BAh1odQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=IDyFGer3; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="IDyFGer3" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1774883766; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=l1tErw3apD+S1FQUsZ/S4dLljpIAMgHXSr6sgIoc4Ns=; b=IDyFGer3sLosGhIt+gXoTU/Sv0Jqsuo33OwI4G/M6leNuAmlA/77Hjm1cwZ+NTjwwkn8Ql kRgk6+B8Sc4WAKGEQpOof2oiaoHu7BD4KUPa10H0lWZiUCT5+GsULH6Nxa8l7ZoDw9N7sU XPNwfmIISgSmH5mj8qsPceopDJ/mvEA= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-351-D_PY-4v-O-iwOZ2SptaFdw-1; Mon, 30 Mar 2026 11:16:03 -0400 X-MC-Unique: D_PY-4v-O-iwOZ2SptaFdw-1 X-Mimecast-MFC-AGG-ID: D_PY-4v-O-iwOZ2SptaFdw_1774883760 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8442419560A7; Mon, 30 Mar 2026 15:16:00 +0000 (UTC) Received: from dell-per7425-02.rhts.eng.pek2.redhat.com (dell-per7425-02.rhts.eng.pek2.redhat.com [10.73.116.18]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8F8C419560AB; Mon, 30 Mar 2026 15:15:52 +0000 (UTC) From: Chunyu Hu To: akpm@linux-foundation.org, david@kernel.org, shuah@kernel.org, linux-mm@kvack.org, ljs@kernel.org Cc: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, lorenzo.stoakes@oracle.com, Liam.Howlett@oracle.com, vbabka@suse.cz, rppt@kernel.org, surenb@google.com, mhocko@suse.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, npache@redhat.com, ryan.roberts@arm.com, dev.jain@arm.com, baohua@kernel.org, lance.yang@linux.dev, chuhu@redhat.com Subject: [PATCH v7 5/7] selftests/mm/vm_util: robust write_file() Date: Mon, 30 Mar 2026 23:15:01 +0800 Message-ID: <20260330151503.670415-6-chuhu@redhat.com> In-Reply-To: <20260330151503.670415-1-chuhu@redhat.com> References: <20260330151503.670415-1-chuhu@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" Add three more checks for buflen and numwritten. The buflen should be at least two, that means at least one char and the null-end. The error case check is added by checking numwriten < 0 instead of numwritten < 1. And the truncate case is checked. The test will exit if any of these conditions aren't met. Additionally, add more print information when a write failure occurs or a truncated write happens, providing clearer diagnostics. Signed-off-by: Chunyu Hu --- Changes in v7: - add an empty line before the 'buflen < 1' line. And change to 'buflen < 2 to limit the buflen at least to be 2, so at least one char is allowed. - handle the numwriten < 0 case, which is the error path. - use ksft_exit_fail_perror in error exit path. - use %.*s to print the buf when error happened Changes in v6: - save/restore errno before and after the close(), so the log could show the correct error info on failure. Suggested by AI. Chagnes in v5: - new patch for making improve on write_file. Add more safety checks and diagnostics info in log --- tools/testing/selftests/mm/vm_util.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests= /mm/vm_util.c index ad96d19d1b85..00aaf33387f5 100644 --- a/tools/testing/selftests/mm/vm_util.c +++ b/tools/testing/selftests/mm/vm_util.c @@ -767,15 +767,24 @@ int unpoison_memory(unsigned long pfn) =20 void write_file(const char *path, const char *buf, size_t buflen) { - int fd; + int fd, saved_errno; ssize_t numwritten; =20 + if (buflen < 2) + ksft_exit_fail_msg("Incorrect buffer len: %zu\n", buflen); + fd =3D open(path, O_WRONLY); if (fd =3D=3D -1) - ksft_exit_fail_msg("%s open failed: %s\n", path, strerror(errno)); + ksft_exit_fail_perror("%s open failed", path); =20 numwritten =3D write(fd, buf, buflen - 1); + saved_errno =3D errno; close(fd); - if (numwritten < 1) - ksft_exit_fail_msg("Write failed\n"); + errno =3D saved_errno; + if (numwritten < 0) + ksft_exit_fail_perror("%s write(%.*s) failed", path, (int)(buflen - 1), + buf); + if (numwritten !=3D buflen - 1) + ksft_exit_fail_msg("%s write(%.*s) is truncated, expected %zu bytes, got= %zd bytes\n", + path, (int)(buflen - 1), buf, buflen - 1, numwritten); } --=20 2.53.0