linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] arch: tile: kernel: kgdb.c: Use memcpy() instead of pointer copy one by one
@ 2014-11-01 13:17 Chen Gang
  2014-11-12  2:11 ` Chen Gang
  0 siblings, 1 reply; 7+ messages in thread
From: Chen Gang @ 2014-11-01 13:17 UTC (permalink / raw)
  To: metcalf; +Cc: linux-kernel@vger.kernel.org

Not only memcpy() is faster than pointer copy, but also let code more
clearer and simple, which can avoid compiling warning (the original
implementation copy registers by exceeding member array border).

The related warning (with allmodconfig under tile):

    CC      arch/tile/kernel/kgdb.o
  arch/tile/kernel/kgdb.c: In function 'sleeping_thread_to_gdb_regs':
  arch/tile/kernel/kgdb.c:140:31: warning: iteration 53u invokes undefined behavior [-Waggressive-loop-optimizations]
     *(ptr++) = thread_regs->regs[reg];
                                 ^
  arch/tile/kernel/kgdb.c:139:2: note: containing loop
    for (reg = 0; reg <= TREG_LAST_GPR; reg++)
    ^

Signed-off-by: Chen Gang <gang.chen.5i5j@gmail.com>
---
 arch/tile/kernel/kgdb.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/arch/tile/kernel/kgdb.c b/arch/tile/kernel/kgdb.c
index 4cd8838..ff5335a 100644
--- a/arch/tile/kernel/kgdb.c
+++ b/arch/tile/kernel/kgdb.c
@@ -125,9 +125,7 @@ int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
 void
 sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
 {
-	int reg;
 	struct pt_regs *thread_regs;
-	unsigned long *ptr = gdb_regs;
 
 	if (task == NULL)
 		return;
@@ -136,9 +134,7 @@ sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
 	memset(gdb_regs, 0, NUMREGBYTES);
 
 	thread_regs = task_pt_regs(task);
-	for (reg = 0; reg <= TREG_LAST_GPR; reg++)
-		*(ptr++) = thread_regs->regs[reg];
-
+	memcpy(gdb_regs, thread_regs, TREG_LAST_GPR * sizeof(unsigned long));
 	gdb_regs[TILEGX_PC_REGNUM] = thread_regs->pc;
 	gdb_regs[TILEGX_FAULTNUM_REGNUM] = thread_regs->faultnum;
 }
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 7+ messages in thread
* Re: [PATCH] arch: tile: kernel: kgdb.c: Use memcpy() instead of pointer copy one by one
@ 2014-11-13  0:08 Chen Gang
  0 siblings, 0 replies; 7+ messages in thread
From: Chen Gang @ 2014-11-13  0:08 UTC (permalink / raw)
  To: Jeff Epler; +Cc: cmetcalf,  linux-kernel@vger.kernel.org

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=utf-8, Size: 1791 bytes --]

Thank you for your detail information, and what you said sounds
reasonable to me.

Send from Lenovo A788t.

Jeff Epler <jepler@unpythonic.net> wrote:

>On Wed, Nov 12, 2014 at 11:43:08PM +0800, Chen Gang wrote:
>> >                                                       (I assume the
>> > compiler could do things like replace an intended load from memory with
>> > a constant load or even no load at all)
>> > 
>> 
>> Excuse me, my English is not quite well, I can not understand what you
>> said above. (If necessary, please help provide more details for it).
>
>I am concerned that writing regs[TREG_TP] is "undefined behavior"
>according to the C standard.
>
>This expression is equivalent to *(regs + TREG_TP).  The expression
>(regs + TREG_TP) does not result in a pointer to any element of regs[],
>so dereferencing it is undefined behavior.  (Source: C99 draft standard
>WG14/N1256, annex J.2, "[The behavior is undefined if t]he operand of
>the unary * operator has an invalid value")
>
>That is why the compiler showed the original diagnostic, but the same
>logic that made the loop's behavior undefined also makes the expression
>regs[TREG_TP] undefined whereever it appears.
>
>None of this is a specific problem with your proposed patch.  Rather, it
>is a suggestion that the whole structure's design needs to be revisited
>in light of compilers beginning to notice that regs[TREG_TP] is
>undefined behavior and change their generated code as a result.
>
>Unfortunately it looks like this header is also a part of the userspace
>API, so it can't simply be changed just in case all in-kernel uses are
>changed.
>
>Jeff
ÿôèº{.nÇ+‰·Ÿ®‰­†+%ŠËÿ±éݶ\x17¥Šwÿº{.nÇ+‰·¥Š{±þG«éÿŠ{ayº\x1dʇڙë,j\a­¢f£¢·hšïêÿ‘êçz_è®\x03(­éšŽŠÝ¢j"ú\x1a¶^[m§ÿÿ¾\a«þG«éÿ¢¸?™¨è­Ú&£ø§~á¶iO•æ¬z·švØ^\x14\x04\x1a¶^[m§ÿÿÃ\fÿ¶ìÿ¢¸?–I¥

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-11-13  0:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-11-01 13:17 [PATCH] arch: tile: kernel: kgdb.c: Use memcpy() instead of pointer copy one by one Chen Gang
2014-11-12  2:11 ` Chen Gang
2014-11-12 13:27   ` Jeff Epler
2014-11-12 15:43     ` Chen Gang
2014-11-12 19:29       ` Jeff Epler
2014-11-12 20:15   ` Chris Metcalf
  -- strict thread matches above, loose matches on Subject: below --
2014-11-13  0:08 Chen Gang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).