Skip to content

Commit 51bb38c

Browse files
Al Viroguoren83
authored andcommitted
csky: Fixup raw_copy_from_user()
If raw_copy_from_user(to, from, N) returns K, callers expect the first N - K bytes starting at to to have been replaced with the contents of corresponding area starting at from and the last K bytes of destination *left* *unmodified*. What arch/sky/lib/usercopy.c is doing is broken - it can lead to e.g. data corruption on write(2). raw_copy_to_user() is inaccurate about return value, which is a bug, but consequences are less drastic than for raw_copy_from_user(). And just what are those access_ok() doing in there? I mean, look into linux/uaccess.h; that's where we do that check (as well as zero tail on failure in the callers that need zeroing). AFAICS, all of that shouldn't be hard to fix; something like a patch below might make a useful starting point. I would suggest moving these macros into usercopy.c (they are never used anywhere else) and possibly expanding them there; if you leave them alive, please at least rename __copy_user_zeroing(). Again, it must not zero anything on failed read. Said that, I'm not sure we won't be better off simply turning usercopy.c into usercopy.S - all that is left there is a couple of functions, each consisting only of inline asm. Guo Ren reply: Yes, raw_copy_from_user is wrong, it's no need zeroing code. unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n) { unsigned long res = n; might_fault(); if (likely(access_ok(from, n))) { kasan_check_write(to, n); res = raw_copy_from_user(to, from, n); } if (unlikely(res)) memset(to + (n - res), 0, res); return res; } EXPORT_SYMBOL(_copy_from_user); You are right and access_ok() should be removed. but, how about: do { ... "2: stw %3, (%1, 0) \n" \ + " subi %0, 4 \n" \ "9: stw %4, (%1, 4) \n" \ + " subi %0, 4 \n" \ "10: stw %5, (%1, 8) \n" \ + " subi %0, 4 \n" \ "11: stw %6, (%1, 12) \n" \ + " subi %0, 4 \n" \ " addi %2, 16 \n" \ " addi %1, 16 \n" \ Don't expand __ex_table AI Viro reply: Hey, I've no idea about the instruction scheduling on csky - if that doesn't slow the things down, all the better. It's just that copy_to_user() and friends are on fairly hot codepaths, and in quite a few situations they will dominate the speed of e.g. read(2). So I tried to keep the fast path unchanged. Up to the architecture maintainers, obviously. Which would be you... As for the fixups size increase (__ex_table size is unchanged)... You have each of those macros expanded exactly once. So the size is not a serious argument, IMO - useless complexity would be, if it is, in fact, useless; the size... not really, especially since those extra subi will at least offset it. Again, up to you - asm optimizations of (essentially) memcpy()-style loops are tricky and can depend upon the fairly subtle details of architecture. So even on something I know reasonably well I would resort to direct experiments if I can't pass the buck to architecture maintainers. It *is* worth optimizing - this is where read() from a file that is already in page cache spends most of the time, etc. Guo Ren reply: Thx, after fixup some typo “sub %0, 4”, apply the patch. TODO: - user copy/from codes are still need optimizing. Signed-off-by: Al Viro <[email protected]> Signed-off-by: Guo Ren <[email protected]>
1 parent 6700281 commit 51bb38c

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

arch/csky/include/asm/uaccess.h

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ do { \
253253

254254
extern int __get_user_bad(void);
255255

256-
#define __copy_user(to, from, n) \
256+
#define ___copy_to_user(to, from, n) \
257257
do { \
258258
int w0, w1, w2, w3; \
259259
asm volatile( \
@@ -288,31 +288,34 @@ do { \
288288
" subi %0, 4 \n" \
289289
" br 3b \n" \
290290
"5: cmpnei %0, 0 \n" /* 1B */ \
291-
" bf 8f \n" \
291+
" bf 13f \n" \
292292
" ldb %3, (%2, 0) \n" \
293293
"6: stb %3, (%1, 0) \n" \
294294
" addi %2, 1 \n" \
295295
" addi %1, 1 \n" \
296296
" subi %0, 1 \n" \
297297
" br 5b \n" \
298-
"7: br 8f \n" \
298+
"7: subi %0, 4 \n" \
299+
"8: subi %0, 4 \n" \
300+
"12: subi %0, 4 \n" \
301+
" br 13f \n" \
299302
".section __ex_table, \"a\" \n" \
300303
".align 2 \n" \
301-
".long 2b, 7b \n" \
302-
".long 9b, 7b \n" \
303-
".long 10b, 7b \n" \
304+
".long 2b, 13f \n" \
305+
".long 4b, 13f \n" \
306+
".long 6b, 13f \n" \
307+
".long 9b, 12b \n" \
308+
".long 10b, 8b \n" \
304309
".long 11b, 7b \n" \
305-
".long 4b, 7b \n" \
306-
".long 6b, 7b \n" \
307310
".previous \n" \
308-
"8: \n" \
311+
"13: \n" \
309312
: "=r"(n), "=r"(to), "=r"(from), "=r"(w0), \
310313
"=r"(w1), "=r"(w2), "=r"(w3) \
311314
: "0"(n), "1"(to), "2"(from) \
312315
: "memory"); \
313316
} while (0)
314317

315-
#define __copy_user_zeroing(to, from, n) \
318+
#define ___copy_from_user(to, from, n) \
316319
do { \
317320
int tmp; \
318321
int nsave; \
@@ -355,22 +358,22 @@ do { \
355358
" addi %1, 1 \n" \
356359
" subi %0, 1 \n" \
357360
" br 5b \n" \
358-
"8: mov %3, %0 \n" \
359-
" movi %4, 0 \n" \
360-
"9: stb %4, (%1, 0) \n" \
361-
" addi %1, 1 \n" \
362-
" subi %3, 1 \n" \
363-
" cmpnei %3, 0 \n" \
364-
" bt 9b \n" \
365-
" br 7f \n" \
361+
"8: stw %3, (%1, 0) \n" \
362+
" subi %0, 4 \n" \
363+
" bf 7f \n" \
364+
"9: subi %0, 8 \n" \
365+
" bf 7f \n" \
366+
"13: stw %3, (%1, 8) \n" \
367+
" subi %0, 12 \n" \
368+
" bf 7f \n" \
366369
".section __ex_table, \"a\" \n" \
367370
".align 2 \n" \
368-
".long 2b, 8b \n" \
371+
".long 2b, 7f \n" \
372+
".long 4b, 7f \n" \
373+
".long 6b, 7f \n" \
369374
".long 10b, 8b \n" \
370-
".long 11b, 8b \n" \
371-
".long 12b, 8b \n" \
372-
".long 4b, 8b \n" \
373-
".long 6b, 8b \n" \
375+
".long 11b, 9b \n" \
376+
".long 12b,13b \n" \
374377
".previous \n" \
375378
"7: \n" \
376379
: "=r"(n), "=r"(to), "=r"(from), "=r"(nsave), \

arch/csky/lib/usercopy.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,15 @@
77
unsigned long raw_copy_from_user(void *to, const void *from,
88
unsigned long n)
99
{
10-
if (access_ok(from, n))
11-
__copy_user_zeroing(to, from, n);
12-
else
13-
memset(to, 0, n);
10+
___copy_from_user(to, from, n);
1411
return n;
1512
}
1613
EXPORT_SYMBOL(raw_copy_from_user);
1714

1815
unsigned long raw_copy_to_user(void *to, const void *from,
1916
unsigned long n)
2017
{
21-
if (access_ok(to, n))
22-
__copy_user(to, from, n);
18+
___copy_to_user(to, from, n);
2319
return n;
2420
}
2521
EXPORT_SYMBOL(raw_copy_to_user);

0 commit comments

Comments
 (0)