Re: [PATCH v10 3/4] tools/mm: add page_owner_filter userspace tool
From: zhen.ni
Date: Tue Jun 23 2026 - 02:26:11 EST
在 2026/6/18 15:21, Lance Yang 写道:
On Thu, Jun 18, 2026 at 11:57:49AM +0800, Zhen Ni wrote:
[...]
+ /* Read and display filtered output */
+ ret = 0;
+ while ((ret = read(fd, buf, sizeof(buf))) > 0) {
+ size_t written = fwrite(buf, 1, ret, output);
+
+ if (written != (size_t)ret) {
+ if (errno == EPIPE) {
Hmm ... does this work without handling SIGPIPE first?
With something like:
$ ./page_owner_filter -m handle | head
The writer can be killed by SIGPIPE before fwrite() returns EPIPE no?
so this branch would usually not be reached ...
+ /* Pipe closed, treat as success */
+ ret = 0;
+ goto out;
+ }
+ perror("write output");
+ ret = -1;
+ goto out;
+ }
+ }
+
+ if (ret < 0) {
+ perror("read page_owner");
+ goto out;
+ }
+
+ if (fflush(output)) {
fflush() can also be where the broken pipe is reported, but still treats
EPIPE as an error ...
Shouldn't we ignore/handle SIGPIPE and treat EPIPE from both fwrite()
and fflush() as succes?
Cheers, Lance
+ perror("flush output");
+ ret = -1;
+ }
+
+out:
+ close(fd);
+ if (output != stdout)
+ fclose(output);
+ return ret < 0 ? 1 : 0;
+}
--
2.20.1
Thanks for the review.
The SIGPIPE handling was indeed incomplete. Without ignoring SIGPIPE first,
the process gets killed before fwrite() or fflush() can return EPIPE, so that
error branch would never be reached.
I've fixed this in v11 by:
1. Adding signal(SIGPIPE, SIG_IGN) at the start of main()
2. Handling EPIPE from both fwrite() and fflush() as success
The fix ensures that when the output pipe is closed (e.g., `| head`), the program
exits cleanly instead of being terminated by SIGPIPE.
Thanks,
Zhen