From: Jens Axboe <jens.axboe@oracle.com>
To: Linux Kernel <linux-kernel@vger.kernel.org>
Cc: lizf@cn.fujitsu.com, Alan.Brunelle@hp.com, Ingo Molnar <mingo@elte.hu>
Subject: [PATCH] Fix blktrace unaligned memory access
Date: Fri, 10 Jul 2009 09:53:40 +0200 [thread overview]
Message-ID: <20090710075339.GX23611@kernel.dk> (raw)
Hi,
It seems that relay_reserve() (or the ring_buffer_event_data(), that one
still needs some love) can return unaligned memory, there's no way
around that when you have pdu lengths that aren't a nice size. This can
cause unaligned access warnings on platforms that care about alignment.
This is an RFC, perhaps we can fix this in some other way. This one takes
the simple approach, use an on-stack copy and memcpy() that to the
destination.
diff --git a/kernel/trace/blktrace.c b/kernel/trace/blktrace.c
index 39af8af..9aba4ec 100644
--- a/kernel/trace/blktrace.c
+++ b/kernel/trace/blktrace.c
@@ -62,7 +62,7 @@ static void blk_unregister_tracepoints(void);
static void trace_note(struct blk_trace *bt, pid_t pid, int action,
const void *data, size_t len)
{
- struct blk_io_trace *t;
+ struct blk_io_trace *ptr, __t, *t = &__t;
struct ring_buffer_event *event = NULL;
int pc = 0;
int cpu = smp_processor_id();
@@ -75,15 +75,15 @@ static void trace_note(struct blk_trace *bt, pid_t pid, int action,
0, pc);
if (!event)
return;
- t = ring_buffer_event_data(event);
+ ptr = t = ring_buffer_event_data(event);
goto record_it;
}
if (!bt->rchan)
return;
- t = relay_reserve(bt->rchan, sizeof(*t) + len);
- if (t) {
+ ptr = relay_reserve(bt->rchan, sizeof(*t) + len);
+ if (ptr) {
t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
t->time = ktime_to_ns(ktime_get());
record_it:
@@ -92,7 +92,9 @@ record_it:
t->pid = pid;
t->cpu = cpu;
t->pdu_len = len;
- memcpy((void *) t + sizeof(*t), data, len);
+ if (t == &__t)
+ memcpy(ptr, t, sizeof(*t));
+ memcpy((void *) ptr + sizeof(*ptr), data, len);
if (blk_tracer)
trace_buffer_unlock_commit(blk_tr, event, 0, pc);
@@ -178,7 +180,7 @@ static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
{
struct task_struct *tsk = current;
struct ring_buffer_event *event = NULL;
- struct blk_io_trace *t;
+ struct blk_io_trace *ptr, __t, *t = &__t;
unsigned long flags = 0;
unsigned long *sequence;
pid_t pid;
@@ -209,7 +211,7 @@ static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
0, pc);
if (!event)
return;
- t = ring_buffer_event_data(event);
+ ptr = t = ring_buffer_event_data(event);
goto record_it;
}
@@ -223,8 +225,8 @@ static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
if (unlikely(tsk->btrace_seq != blktrace_seq))
trace_note_tsk(bt, tsk);
- t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
- if (t) {
+ ptr = relay_reserve(bt->rchan, sizeof(*ptr) + pdu_len);
+ if (ptr) {
sequence = per_cpu_ptr(bt->sequence, cpu);
t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
@@ -247,8 +249,10 @@ record_it:
t->error = error;
t->pdu_len = pdu_len;
+ if (t == &__t)
+ memcpy(ptr, t, sizeof(*t));
if (pdu_len)
- memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
+ memcpy((void *) ptr + sizeof(*ptr), pdu_data, pdu_len);
if (blk_tracer) {
trace_buffer_unlock_commit(blk_tr, event, 0, pc);
--
Jens Axboe
next reply other threads:[~2009-07-10 7:53 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-07-10 7:53 Jens Axboe [this message]
2009-07-10 9:07 ` [PATCH] Fix blktrace unaligned memory access Li Zefan
2009-07-10 9:13 ` Jens Axboe
2009-07-10 9:30 ` Li Zefan
2009-07-10 9:36 ` Jens Axboe
2009-07-14 17:35 ` Steven Rostedt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20090710075339.GX23611@kernel.dk \
--to=jens.axboe@oracle.com \
--cc=Alan.Brunelle@hp.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lizf@cn.fujitsu.com \
--cc=mingo@elte.hu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox