Skip to content

Commit 4be2c95

Browse files
jeffmahoneytorvalds
authored andcommitted
taskstats: pad taskstats netlink response for aligment issues on ia64
The taskstats structure is internally aligned on 8 byte boundaries but the layout of the aggregrate reply, with two NLA headers and the pid (each 4 bytes), actually force the entire structure to be unaligned. This causes the kernel to issue unaligned access warnings on some architectures like ia64. Unfortunately, some software out there doesn't properly unroll the NLA packet and assumes that the start of the taskstats structure will always be 20 bytes from the start of the netlink payload. Aligning the start of the taskstats structure breaks this software, which we don't want. So, for now the alignment only happens on architectures that require it and those users will have to update to fixed versions of those packages. Space is reserved in the packet only when needed. This ifdef should be removed in several years e.g. 2012 once we can be confident that fixed versions are installed on most systems. We add the padding before the aggregate since the aggregate is already a defined type. Commit 8589312 ("delayacct: align to 8 byte boundary on 64-bit systems") previously addressed the alignment issues by padding out the pid field. This was supposed to be a compatible change but the circumstances described above mean that it wasn't. This patch backs out that change, since it was a hack, and introduces a new NULL attribute type to provide the padding. Padding the response with 4 bytes avoids allocating an aligned taskstats structure and copying it back. Since the structure weighs in at 328 bytes, it's too big to do it on the stack. Signed-off-by: Jeff Mahoney <jeffm@suse.com> Reported-by: Brian Rogers <brian@xyzw.org> Cc: Jeff Mahoney <jeffm@suse.com> Cc: Guillaume Chazarain <guichaz@gmail.com> Cc: Balbir Singh <balbir@in.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 4e06fd1 commit 4be2c95

3 files changed

Lines changed: 47 additions & 14 deletions

File tree

‎Documentation/accounting/getdelays.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ int main(int argc, char *argv[])
516516
default:
517517
fprintf(stderr, "Unknown nla_type %d\n",
518518
na->nla_type);
519+
case TASKSTATS_TYPE_NULL:
519520
break;
520521
}
521522
na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);

‎include/linux/taskstats.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*/
3434

3535

36-
#define TASKSTATS_VERSION 7
36+
#define TASKSTATS_VERSION 8
3737
#define TS_COMM_LEN 32 /* should be >= TASK_COMM_LEN
3838
* in linux/sched.h */
3939

@@ -188,6 +188,7 @@ enum {
188188
TASKSTATS_TYPE_STATS, /* taskstats structure */
189189
TASKSTATS_TYPE_AGGR_PID, /* contains pid + stats */
190190
TASKSTATS_TYPE_AGGR_TGID, /* contains tgid + stats */
191+
TASKSTATS_TYPE_NULL, /* contains nothing */
191192
__TASKSTATS_TYPE_MAX,
192193
};
193194

‎kernel/taskstats.c‎

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -349,25 +349,47 @@ static int parse(struct nlattr *na, struct cpumask *mask)
349349
return ret;
350350
}
351351

352+
#ifdef CONFIG_IA64
353+
#define TASKSTATS_NEEDS_PADDING 1
354+
#endif
355+
352356
static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid)
353357
{
354358
struct nlattr *na, *ret;
355359
int aggr;
356360

357-
/* If we don't pad, we end up with alignment on a 4 byte boundary.
358-
* This causes lots of runtime warnings on systems requiring 8 byte
359-
* alignment */
360-
u32 pids[2] = { pid, 0 };
361-
int pid_size = ALIGN(sizeof(pid), sizeof(long));
362-
363361
aggr = (type == TASKSTATS_TYPE_PID)
364362
? TASKSTATS_TYPE_AGGR_PID
365363
: TASKSTATS_TYPE_AGGR_TGID;
366364

365+
/*
366+
* The taskstats structure is internally aligned on 8 byte
367+
* boundaries but the layout of the aggregrate reply, with
368+
* two NLA headers and the pid (each 4 bytes), actually
369+
* force the entire structure to be unaligned. This causes
370+
* the kernel to issue unaligned access warnings on some
371+
* architectures like ia64. Unfortunately, some software out there
372+
* doesn't properly unroll the NLA packet and assumes that the start
373+
* of the taskstats structure will always be 20 bytes from the start
374+
* of the netlink payload. Aligning the start of the taskstats
375+
* structure breaks this software, which we don't want. So, for now
376+
* the alignment only happens on architectures that require it
377+
* and those users will have to update to fixed versions of those
378+
* packages. Space is reserved in the packet only when needed.
379+
* This ifdef should be removed in several years e.g. 2012 once
380+
* we can be confident that fixed versions are installed on most
381+
* systems. We add the padding before the aggregate since the
382+
* aggregate is already a defined type.
383+
*/
384+
#ifdef TASKSTATS_NEEDS_PADDING
385+
if (nla_put(skb, TASKSTATS_TYPE_NULL, 0, NULL) < 0)
386+
goto err;
387+
#endif
367388
na = nla_nest_start(skb, aggr);
368389
if (!na)
369390
goto err;
370-
if (nla_put(skb, type, pid_size, pids) < 0)
391+
392+
if (nla_put(skb, type, sizeof(pid), &pid) < 0)
371393
goto err;
372394
ret = nla_reserve(skb, TASKSTATS_TYPE_STATS, sizeof(struct taskstats));
373395
if (!ret)
@@ -456,6 +478,18 @@ static int cmd_attr_deregister_cpumask(struct genl_info *info)
456478
return rc;
457479
}
458480

481+
static size_t taskstats_packet_size(void)
482+
{
483+
size_t size;
484+
485+
size = nla_total_size(sizeof(u32)) +
486+
nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
487+
#ifdef TASKSTATS_NEEDS_PADDING
488+
size += nla_total_size(0); /* Padding for alignment */
489+
#endif
490+
return size;
491+
}
492+
459493
static int cmd_attr_pid(struct genl_info *info)
460494
{
461495
struct taskstats *stats;
@@ -464,8 +498,7 @@ static int cmd_attr_pid(struct genl_info *info)
464498
u32 pid;
465499
int rc;
466500

467-
size = nla_total_size(sizeof(u32)) +
468-
nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
501+
size = taskstats_packet_size();
469502

470503
rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
471504
if (rc < 0)
@@ -494,8 +527,7 @@ static int cmd_attr_tgid(struct genl_info *info)
494527
u32 tgid;
495528
int rc;
496529

497-
size = nla_total_size(sizeof(u32)) +
498-
nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
530+
size = taskstats_packet_size();
499531

500532
rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
501533
if (rc < 0)
@@ -570,8 +602,7 @@ void taskstats_exit(struct task_struct *tsk, int group_dead)
570602
/*
571603
* Size includes space for nested attributes
572604
*/
573-
size = nla_total_size(sizeof(u32)) +
574-
nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
605+
size = taskstats_packet_size();
575606

576607
is_thread_group = !!taskstats_tgid_alloc(tsk);
577608
if (is_thread_group) {

0 commit comments

Comments
 (0)