Skip to contentSkip to conversation

workqueue: Fix alignment calculation in alloc_cwqs()

PATCH In review David Howells 2 participants 4 messages Oct 25, 2010 Workqueue

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 workqueue: Fix alignment calculation in alloc_cwqs()

In the MN10300 arch, we occasionally see an assertion being tripped in alloc_cwqs() at the following line:

	/* just in case, make sure it's actually aligned */
  --->	BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
	return wq->cpu_wq.v ? 0 : -ENOMEM;

The values are:

	wa->cpu_wq.v => 0x902776e0
	align => 0x100

and align is calculated by the following:

	const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
				   __alignof__(unsigned long long));

which is wrong. __alignof__() returns its value in bytes, but:

1 << WORK_STRUCT_FLAG_BITS

returns the value in bits. It needs dividing by the number of bits in a byte.

---

 kernel/workqueue.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
kernel/workqueue.c +3 -2
index 30acdb7..e29ebd4 100644
@@ -2766,8 +2766,9 @@ static int alloc_cwqs(struct workqueue_struct *wq)
 	 * unsigned long long.
 	 */
 	const size_t size = sizeof(struct cpu_workqueue_struct);
-	const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
-				   __alignof__(unsigned long long));
+	const size_t align =
+		max_t(size_t, 1 << (WORK_STRUCT_FLAG_BITS - BITS_PER_BYTE),
+		      __alignof__(unsigned long long));
 #ifdef CONFIG_SMP
 	bool percpu = !(wq->flags & WQ_UNBOUND);
 #else
#1 lore raw

kernel/workqueue.c static int alloc_cwqs(struct workqueue_struct *wq) · line 2771

Quoted 3 lines · David Howells
kernel/workqueue.c +3 -0
+	const size_t align =
+		max_t(size_t, 1 << (WORK_STRUCT_FLAG_BITS - BITS_PER_BYTE),
+		      __alignof__(unsigned long long));

Actually, no, that's not right either. Need to subtract log2(BITS_PER_BYTE).

David

#2 lore raw
In reply to David Howells
Quoted 14 lines · David Howells

In the MN10300 arch, we occasionally see an assertion being tripped in alloc_cwqs() at the following line:

       /* just in case, make sure it's actually aligned */ --->  BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align)); return wq->cpu_wq.v ? 0 : -ENOMEM;

The values are:

       wa->cpu_wq.v => 0x902776e0
       align => 0x100

and align is calculated by the following:

Quoted 8 lines

       const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS, __alignof__(unsigned long long));

which is wrong.  __alignof__() returns its value in bytes, but:

       1 << WORK_STRUCT_FLAG_BITS

returns the value in bits.  It needs dividing by the number of bits in a byte.

No it doesn't. Those bits really require that many bytes of alignment.

Think about it: if the low 8 bits of the pointer are used for flags, then the actual pointer itself needs to be aligned to a 256-byte boundary.

So the code is right. If needs to ask for "1 << WORK_STRUCT_FLAG_BITS" alignment, and if it doesn't get it (because the allocator is somehow broken - percpu allocator issues?), things will break.

Linus

#3 lore raw
Quoted 9 lines · Linus Torvalds

No it doesn't. Those bits really require that many bytes of alignment.

Think about it: if the low 8 bits of the pointer are used for flags, then the actual pointer itself needs to be aligned to a 256-byte boundary.

So the code is right. If needs to ask for "1 << WORK_STRUCT_FLAG_BITS" alignment, and if it doesn't get it (because the allocator is somehow broken - percpu allocator issues?), things will break.

That's a good point.

David