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 2075419C542; Fri, 27 Mar 2026 12:32:11 +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=1774614731; cv=none; b=ifRqYLK3YhtI/s9MrbYejC1MSLdYyfpJxPPtlN9t/K0SbIXNJoTiduXLWky1oBbz3pAzFiW68lrPzmuHnK9OyEMFvhifSgvXccTkZv8qz9Qx4fXmwDCAiyeFvqDMVvMV4yzazw/Drc3YPM/3BRd6ID55ckGaE74U0IkTiS97D78= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774614731; c=relaxed/simple; bh=Pl9DVulQW4wTm72CZ7Z0DlDl11UMwY/qQTHqQXd4fo0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=A+p+G7han6wgu35PsKuCSSjPyLhdaJZpzav1KWWo6PXcYSO92CwxC9k+YEO6D8in3cJR0REycnAz/lqWPqCjgBgj9g+8nHh02eKUbS+BwiDb2XQGWQhLpKgEZVpgE27cSLyMEzgwJMxqs0vOi8nDIXCP+i7+Tyk20XQSO53rN2Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gG3JNHS2; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gG3JNHS2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9583DC19423; Fri, 27 Mar 2026 12:32:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774614730; bh=Pl9DVulQW4wTm72CZ7Z0DlDl11UMwY/qQTHqQXd4fo0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=gG3JNHS2r8tw4BAPgiCvkVDhREBhFMB72+fGe4fR9spd9APmORjZYd8Xb69ygI8JO 4vEKHENnLe/BZ+47FfQXurrgYhUzorJ2IXP2e2n77JPaOB+ncMOcOZce0c3Zaxe7NF 9VnkLGAdrbhEpLs5EwhyR5t2zLu7lNnKR1rbOEnAsrb6SVP6ZKWBBy3xy1ubR9xIF+ rSUUUPvqW05hB3S2125JudhUuiWmvna3jyeSasYilQmyLoSoVAxy8kxroqq6g9u/Ls nFVz+dcgo1JHgg7NhZ9OvZ0A2tD0Nb383uOlmdgyXfg2UuOejuCQ/knrq6K5KTRx+k 0yF06X7oAWONg== Date: Fri, 27 Mar 2026 15:32:01 +0300 From: Mike Rapoport To: Chunyu Hu Cc: akpm@linux-foundation.org, david@kernel.org, shuah@kernel.org, linux-mm@kvack.org, ljs@kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, lorenzo.stoakes@oracle.com, Liam.Howlett@oracle.com, vbabka@suse.cz, 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 Subject: Re: [PATCH v6 4/6] selftests/mm/vm_util: robust write_file() Message-ID: References: <20260324013316.2590422-1-chuhu@redhat.com> <20260324013316.2590422-5-chuhu@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260324013316.2590422-5-chuhu@redhat.com> On Tue, Mar 24, 2026 at 09:33:14AM +0800, Chunyu Hu wrote: > Add two more checks for buflen and numwritten. The buflen should be at > least one, otherwise the 'buflen - 1' could underflow and cause trouble. > The numwritten should be equal to 'buflen - 1'. 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 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 | 12 ++++++++++-- > 1 file changed, 10 insertions(+), 2 deletions(-) > > diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c > index ad96d19d1b85..572ccb99de8e 100644 > --- a/tools/testing/selftests/mm/vm_util.c > +++ b/tools/testing/selftests/mm/vm_util.c > @@ -767,15 +767,23 @@ int unpoison_memory(unsigned long pfn) > > void write_file(const char *path, const char *buf, size_t buflen) > { > - int fd; > + int fd, saved_errno; > ssize_t numwritten; > + if (buflen < 1) > + ksft_exit_fail_msg("Incorrect buffer len: %zu\n", buflen); > > fd = open(path, O_WRONLY); > if (fd == -1) > ksft_exit_fail_msg("%s open failed: %s\n", path, strerror(errno)); We have ksft_exit_fail_perror(), fits great here > > numwritten = write(fd, buf, buflen - 1); > + saved_errno = errno; > close(fd); > + errno = saved_errno; > if (numwritten < 1) > - ksft_exit_fail_msg("Write failed\n"); > + ksft_exit_fail_msg("%s write(%s) failed: %s\n", path, buf, > + strerror(errno)); and here. > + if (numwritten != buflen - 1) > + ksft_exit_fail_msg("%s write(%s) is truncated, expected %zu bytes, got %zd bytes\n", > + path, buf, buflen - 1, numwritten); > } > -- > 2.53.0 > -- Sincerely yours, Mike.