Skip to content

Commit cc82b3d

Browse files
cyyzeroakpm00
authored andcommitted
tools/accounting: handle truncated taskstats netlink messages
procacct and getdelays use a fixed receive buffer for taskstats generic netlink messages. A multi-threaded process exit can emit a single PID+TGID notification large enough to exceed that buffer on newer kernels. Switch to recvmsg() so MSG_TRUNC is detected explicitly, increase the message buffer size, and report truncated datagrams clearly instead of misparsing them as fatal netlink errors. Also print the taskstats version in debug output to make version mismatches easier to diagnose while inspecting taskstats traffic. Link: https://lkml.kernel.org/r/520308bb4cbbaf8dc2c7296b5f60f11e12fb30a5.1774810498.git.cyyzero16@gmail.com Signed-off-by: Yiyang Chen <cyyzero16@gmail.com> Cc: Balbir Singh <bsingharora@gmail.com> Cc: Dr. Thomas Orgis <thomas.orgis@uni-hamburg.de> Cc: Fan Yu <fan.yu9@zte.com.cn> Cc: Wang Yaxin <wang.yaxin@zte.com.cn> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 16c4f02 commit cc82b3d

2 files changed

Lines changed: 73 additions & 8 deletions

File tree

tools/accounting/getdelays.c

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ int print_task_context_switch_counts;
6060
}
6161

6262
/* Maximum size of response requested or message sent */
63-
#define MAX_MSG_SIZE 1024
63+
#define MAX_MSG_SIZE 2048
6464
/* Maximum number of cpus expected to be specified in a cpumask */
6565
#define MAX_CPUS 32
6666

@@ -115,6 +115,32 @@ static int create_nl_socket(int protocol)
115115
return -1;
116116
}
117117

118+
static int recv_taskstats_msg(int sd, struct msgtemplate *msg)
119+
{
120+
struct sockaddr_nl nladdr;
121+
struct iovec iov = {
122+
.iov_base = msg,
123+
.iov_len = sizeof(*msg),
124+
};
125+
struct msghdr hdr = {
126+
.msg_name = &nladdr,
127+
.msg_namelen = sizeof(nladdr),
128+
.msg_iov = &iov,
129+
.msg_iovlen = 1,
130+
};
131+
int ret;
132+
133+
ret = recvmsg(sd, &hdr, 0);
134+
if (ret < 0)
135+
return -1;
136+
if (hdr.msg_flags & MSG_TRUNC) {
137+
errno = EMSGSIZE;
138+
return -1;
139+
}
140+
141+
return ret;
142+
}
143+
118144

119145
static int send_cmd(int sd, __u16 nlmsg_type, __u32 nlmsg_pid,
120146
__u8 genl_cmd, __u16 nla_type,
@@ -633,12 +659,16 @@ int main(int argc, char *argv[])
633659
}
634660

635661
do {
636-
rep_len = recv(nl_sd, &msg, sizeof(msg), 0);
662+
rep_len = recv_taskstats_msg(nl_sd, &msg);
637663
PRINTF("received %d bytes\n", rep_len);
638664

639665
if (rep_len < 0) {
640-
fprintf(stderr, "nonfatal reply error: errno %d\n",
641-
errno);
666+
if (errno == EMSGSIZE)
667+
fprintf(stderr,
668+
"dropped truncated taskstats netlink message, please increase MAX_MSG_SIZE\n");
669+
else
670+
fprintf(stderr, "nonfatal reply error: errno %d\n",
671+
errno);
642672
continue;
643673
}
644674
if (msg.n.nlmsg_type == NLMSG_ERROR ||
@@ -680,6 +710,9 @@ int main(int argc, char *argv[])
680710
printf("TGID\t%d\n", rtid);
681711
break;
682712
case TASKSTATS_TYPE_STATS:
713+
PRINTF("version %u\n",
714+
((struct taskstats *)
715+
NLA_DATA(na))->version);
683716
if (print_delays)
684717
print_delayacct((struct taskstats *) NLA_DATA(na));
685718
if (print_io_accounting)

tools/accounting/procacct.c

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ int print_task_context_switch_counts;
7171
}
7272

7373
/* Maximum size of response requested or message sent */
74-
#define MAX_MSG_SIZE 1024
74+
#define MAX_MSG_SIZE 2048
7575
/* Maximum number of cpus expected to be specified in a cpumask */
7676
#define MAX_CPUS 32
7777

@@ -121,6 +121,32 @@ static int create_nl_socket(int protocol)
121121
return -1;
122122
}
123123

124+
static int recv_taskstats_msg(int sd, struct msgtemplate *msg)
125+
{
126+
struct sockaddr_nl nladdr;
127+
struct iovec iov = {
128+
.iov_base = msg,
129+
.iov_len = sizeof(*msg),
130+
};
131+
struct msghdr hdr = {
132+
.msg_name = &nladdr,
133+
.msg_namelen = sizeof(nladdr),
134+
.msg_iov = &iov,
135+
.msg_iovlen = 1,
136+
};
137+
int ret;
138+
139+
ret = recvmsg(sd, &hdr, 0);
140+
if (ret < 0)
141+
return -1;
142+
if (hdr.msg_flags & MSG_TRUNC) {
143+
errno = EMSGSIZE;
144+
return -1;
145+
}
146+
147+
return ret;
148+
}
149+
124150

125151
static int send_cmd(int sd, __u16 nlmsg_type, __u32 nlmsg_pid,
126152
__u8 genl_cmd, __u16 nla_type,
@@ -239,6 +265,8 @@ void handle_aggr(int mother, struct nlattr *na, int fd)
239265
PRINTF("TGID\t%d\n", rtid);
240266
break;
241267
case TASKSTATS_TYPE_STATS:
268+
PRINTF("version %u\n",
269+
((struct taskstats *)NLA_DATA(na))->version);
242270
if (mother == TASKSTATS_TYPE_AGGR_PID)
243271
print_procacct((struct taskstats *) NLA_DATA(na));
244272
if (fd) {
@@ -347,12 +375,16 @@ int main(int argc, char *argv[])
347375
}
348376

349377
do {
350-
rep_len = recv(nl_sd, &msg, sizeof(msg), 0);
378+
rep_len = recv_taskstats_msg(nl_sd, &msg);
351379
PRINTF("received %d bytes\n", rep_len);
352380

353381
if (rep_len < 0) {
354-
fprintf(stderr, "nonfatal reply error: errno %d\n",
355-
errno);
382+
if (errno == EMSGSIZE)
383+
fprintf(stderr,
384+
"dropped truncated taskstats netlink message, please increase MAX_MSG_SIZE\n");
385+
else
386+
fprintf(stderr, "nonfatal reply error: errno %d\n",
387+
errno);
356388
continue;
357389
}
358390
if (msg.n.nlmsg_type == NLMSG_ERROR ||

0 commit comments

Comments
 (0)