Revision a8950e49bd241dc9ad90c24e92e23e95dbe9f018 authored by Romain Perier on 19 April 2016, 10:17:32 UTC, committed by Ley Foon Tan on 27 April 2016, 08:35:55 UTC
Depending on the size of the area to be memset'ed, the nios2 memset implementation
either uses a naive loop (for buffers smaller or equal than 8 bytes) or a more optimized
implementation (for buffers larger than 8 bytes). This implementation does 4-byte stores
rather than 1-byte stores to speed up memset.

However, we discovered that on our nios2 platform, memset() was not properly setting the
buffer to the expected value. A memset of 0xff would not set the entire buffer to 0xff, but to:

0xff 0x00 0xff 0x00 0xff 0x00 0xff 0x00 ...

Which is obviously incorrect. Our investigation has revealed that the problem lies in the
incorrect constraints used in the inline assembly.

The following piece of assembly, from the nios2 memset implementation, is supposed to
create a 4-byte value that repeats 4 times the 1-byte pattern passed as memset argument:

/* fill8 %3, %5 (c & 0xff) */
"       slli    %4, %5, 8\n"
"       or      %4, %4, %5\n"
"       slli    %3, %4, 16\n"
"       or      %3, %3, %4\n"

However, depending on the compiler and optimization level, this code might be compiled as:

34:	280a923a 	slli	r5,r5,8
38:	294ab03a 	or	r5,r5,r5
3c:	2808943a 	slli	r4,r5,16
40:	2148b03a 	or	r4,r4,r5

This is wrong because r5 gets used both for %5 and %4, which leads to the final pattern
stored in r4 to be 0xff00ff00 rather than the expected 0xffffffff.

%4 is defined with the "=r" constraint, i.e as an output operand. However, as explained in
http://www.ethernut.de/en/documents/arm-inline-asm.html, this does not prevent gcc from
using the same register for an output operand (%4) and input operand (%5). By using the
constraint modifier '&', we indicate that the register should be used for output only. With this
change, we get the following assembly output:

34:	2810923a 	slli	r8,r5,8
38:	4150b03a 	or	r8,r8,r5
3c:	400e943a 	slli	r7,r8,16
40:	3a0eb03a 	or	r7,r7,r8

Which correctly produces the 0xffffffff pattern when 0xff is passed as the memset() pattern.

It is worth mentioning the observed consequence of this bug: we were hitting the kernel
BUG() in mm/bootmem.c:__free() that verifies when marking a page as free that it was
previously marked as occupied (i.e that the bit was set to 1). The entire bootmem bitmap is
set to 0xff bit via a memset() during the bootmem initialization. The bootmem_free() call right
after the initialization was finding some bits to be set to 0, which didn't make sense since the
bitmap has just been memset'ed to 0xff. Except that due to the bug explained above, the
bitmap was in fact initialized to 0xff00ff00.

Thanks to Marek Vasut for his help and feedback.

Signed-off-by: Romain Perier <romain.perier@free-electrons.com>
Acked-by: Marek Vasut <marex@denx.de>
Acked-by: Ley Foon Tan <lftan@altera.com>
1 parent 02da2d7
History
File Mode Size
ABI
DocBook
EDID
PCI
RCU
accounting
acpi
aoe
arm
arm64
auxdisplay
backlight
blackfin
block
blockdev
bus-devices
cdrom
cgroup-v1
cma
connector
console
cpu-freq
cpuidle
cris
crypto
development-process
device-mapper
devicetree
dmaengine
driver-model
dvb
early-userspace
extcon
fault-injection
fb
features
filesystems
firmware_class
fmc
fpga
frv
gpio
hid
hwmon
i2c
ia64
ide
iio
infiniband
input
ioctl
isdn
ja_JP
kbuild
kdump
ko_KR
laptops
leds
locking
m68k
memory-devices
metag
mic
mips
misc-devices
mmc
mn10300
mtd
namespaces
netlabel
networking
nfc
nios2
nvdimm
nvmem
parisc
pcmcia
phy
platform
power
powerpc
pps
prctl
pti
ptp
rapidio
s390
scheduler
scsi
security
serial
sh
sound
spi
sysctl
target
thermal
timers
tpm
trace
usb
vDSO
video4linux
virtual
vm
w1
watchdog
wimax
x86
xtensa
zh_CN
00-INDEX -rw-r--r-- 16.8 KB
BUG-HUNTING -rw-r--r-- 8.1 KB
Changes -rw-r--r-- 11.6 KB
CodeOfConflict -rw-r--r-- 1.4 KB
CodingStyle -rw-r--r-- 34.0 KB
DMA-API-HOWTO.txt -rw-r--r-- 34.7 KB
DMA-API.txt -rw-r--r-- 27.5 KB
DMA-ISA-LPC.txt -rw-r--r-- 5.2 KB
DMA-attributes.txt -rw-r--r-- 5.7 KB
HOWTO -rw-r--r-- 26.8 KB
IPMI.txt -rw-r--r-- 29.4 KB
IRQ-affinity.txt -rw-r--r-- 2.5 KB
IRQ-domain.txt -rw-r--r-- 10.0 KB
IRQ.txt -rw-r--r-- 962 bytes
Intel-IOMMU.txt -rw-r--r-- 3.8 KB
Makefile -rw-r--r-- 177 bytes
ManagementStyle -rw-r--r-- 12.9 KB
SAK.txt -rw-r--r-- 2.8 KB
SM501.txt -rw-r--r-- 2.8 KB
SecurityBugs -rw-r--r-- 1.8 KB
SubmitChecklist -rw-r--r-- 4.4 KB
SubmittingDrivers -rw-r--r-- 6.2 KB
SubmittingPatches -rw-r--r-- 35.7 KB
VGA-softcursor.txt -rw-r--r-- 2.0 KB
adding-syscalls.txt -rw-r--r-- 23.8 KB
applying-patches.txt -rw-r--r-- 19.5 KB
assoc_array.txt -rw-r--r-- 20.0 KB
atomic_ops.txt -rw-r--r-- 22.0 KB
bad_memory.txt -rw-r--r-- 1.1 KB
basic_profiling.txt -rw-r--r-- 1.7 KB
bcache.txt -rw-r--r-- 16.4 KB
binfmt_misc.txt -rw-r--r-- 6.5 KB
braille-console.txt -rw-r--r-- 1.4 KB
bt8xxgpio.txt -rw-r--r-- 4.3 KB
btmrvl.txt -rw-r--r-- 2.9 KB
bus-virt-phys-mapping.txt -rw-r--r-- 7.9 KB
cachetlb.txt -rw-r--r-- 17.1 KB
cgroup-v2.txt -rw-r--r-- 56.7 KB
circular-buffers.txt -rw-r--r-- 8.4 KB
clk.txt -rw-r--r-- 10.3 KB
coccinelle.txt -rw-r--r-- 9.0 KB
cpu-hotplug.txt -rw-r--r-- 16.8 KB
cpu-load.txt -rw-r--r-- 3.0 KB
cputopology.txt -rw-r--r-- 4.5 KB
crc32.txt -rw-r--r-- 8.5 KB
dcdbas.txt -rw-r--r-- 3.6 KB
debugging-modules.txt -rw-r--r-- 954 bytes
debugging-via-ohci1394.txt -rw-r--r-- 7.4 KB
dell_rbu.txt -rw-r--r-- 4.9 KB
devices.txt -rw-r--r-- 116.2 KB
digsig.txt -rw-r--r-- 2.8 KB
dma-buf-sharing.txt -rw-r--r-- 22.0 KB
dontdiff -rw-r--r-- 2.5 KB
dynamic-debug-howto.txt -rw-r--r-- 12.6 KB
edac.txt -rw-r--r-- 24.7 KB
efi-stub.txt -rw-r--r-- 3.2 KB
eisa.txt -rw-r--r-- 7.1 KB
email-clients.txt -rw-r--r-- 10.9 KB
flexible-arrays.txt -rw-r--r-- 5.5 KB
futex-requeue-pi.txt -rw-r--r-- 5.0 KB
gcov.txt -rw-r--r-- 7.6 KB
gdb-kernel-debugging.txt -rw-r--r-- 5.9 KB
highuid.txt -rw-r--r-- 2.4 KB
hsi.txt -rw-r--r-- 2.9 KB
hw_random.txt -rw-r--r-- 3.5 KB
hwspinlock.txt -rw-r--r-- 12.7 KB
init.txt -rw-r--r-- 2.5 KB
initrd.txt -rw-r--r-- 14.1 KB
intel_txt.txt -rw-r--r-- 10.2 KB
io-mapping.txt -rw-r--r-- 3.2 KB
io_ordering.txt -rw-r--r-- 1.9 KB
iostats.txt -rw-r--r-- 8.0 KB
irqflags-tracing.txt -rw-r--r-- 2.3 KB
isapnp.txt -rw-r--r-- 433 bytes
java.txt -rw-r--r-- 10.9 KB
kasan.txt -rw-r--r-- 8.1 KB
kcov.txt -rw-r--r-- 3.4 KB
kernel-doc-nano-HOWTO.txt -rw-r--r-- 11.7 KB
kernel-docs.txt -rw-r--r-- 33.0 KB
kernel-parameters.txt -rw-r--r-- 152.9 KB
kernel-per-CPU-kthreads.txt -rw-r--r-- 13.2 KB
kmemcheck.txt -rw-r--r-- 29.9 KB
kmemleak.txt -rw-r--r-- 8.5 KB
kobject.txt -rw-r--r-- 18.0 KB
kprobes.txt -rw-r--r-- 30.3 KB
kref.txt -rw-r--r-- 8.4 KB
kselftest.txt -rw-r--r-- 2.4 KB
ldm.txt -rw-r--r-- 3.8 KB
local_ops.txt -rw-r--r-- 6.5 KB
lockup-watchdogs.txt -rw-r--r-- 4.1 KB
logo.gif -rw-r--r-- 16.0 KB
logo.txt -rw-r--r-- 563 bytes
lzo.txt -rw-r--r-- 7.8 KB
magic-number.txt -rw-r--r-- 8.7 KB
mailbox.txt -rw-r--r-- 4.1 KB
md-cluster.txt -rw-r--r-- 12.3 KB
md.txt -rw-r--r-- 25.3 KB
memory-barriers.txt -rw-r--r-- 112.3 KB
memory-hotplug.txt -rw-r--r-- 17.9 KB
men-chameleon-bus.txt -rw-r--r-- 6.1 KB
module-signing.txt -rw-r--r-- 10.3 KB
mono.txt -rw-r--r-- 2.5 KB
nommu-mmap.txt -rw-r--r-- 12.7 KB
ntb.txt -rw-r--r-- 6.3 KB
numastat.txt -rw-r--r-- 836 bytes
oops-tracing.txt -rw-r--r-- 12.8 KB
padata.txt -rw-r--r-- 7.3 KB
parport-lowlevel.txt -rw-r--r-- 32.2 KB
parport.txt -rw-r--r-- 8.8 KB
percpu-rw-semaphore.txt -rw-r--r-- 1.1 KB
phy.txt -rw-r--r-- 6.9 KB
pi-futex.txt -rw-r--r-- 5.7 KB
pinctrl.txt -rw-r--r-- 50.4 KB
pnp.txt -rw-r--r-- 6.8 KB
preempt-locking.txt -rw-r--r-- 5.2 KB
printk-formats.txt -rw-r--r-- 10.3 KB
pwm.txt -rw-r--r-- 4.7 KB
ramoops.txt -rw-r--r-- 5.2 KB
rbtree.txt -rw-r--r-- 13.3 KB
remoteproc.txt -rw-r--r-- 12.7 KB
rfkill.txt -rw-r--r-- 5.0 KB
robust-futex-ABI.txt -rw-r--r-- 8.7 KB
robust-futexes.txt -rw-r--r-- 9.4 KB
rpmsg.txt -rw-r--r-- 13.5 KB
rtc.txt -rw-r--r-- 10.3 KB
serial-console.txt -rw-r--r-- 4.0 KB
sgi-ioc4.txt -rw-r--r-- 2.0 KB
smsc_ece1099.txt -rw-r--r-- 2.4 KB
sparse.txt -rw-r--r-- 3.8 KB
stable_api_nonsense.txt -rw-r--r-- 9.2 KB
stable_kernel_rules.txt -rw-r--r-- 6.0 KB
static-keys.txt -rw-r--r-- 11.5 KB
svga.txt -rw-r--r-- 14.1 KB
sysfs-rules.txt -rw-r--r-- 9.0 KB
sysrq.txt -rw-r--r-- 11.8 KB
this_cpu_ops.txt -rw-r--r-- 11.1 KB
ubsan.txt -rw-r--r-- 3.1 KB
unaligned-memory-access.txt -rw-r--r-- 10.4 KB
unicode.txt -rw-r--r-- 6.5 KB
unshare.txt -rw-r--r-- 13.1 KB
vfio.txt -rw-r--r-- 21.3 KB
vgaarbiter.txt -rw-r--r-- 8.1 KB
video-output.txt -rw-r--r-- 1.1 KB
vme_api.txt -rw-r--r-- 13.4 KB
volatile-considered-harmful.txt -rw-r--r-- 5.6 KB
workqueue.txt -rw-r--r-- 14.7 KB
xillybus.txt -rw-r--r-- 17.7 KB
xz.txt -rw-r--r-- 5.7 KB
zorro.txt -rw-r--r-- 2.9 KB

back to top