Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!

Bug 454006

Summary: sys-fs/btrfs-progs-0.9.11 causes unaligned accesses
Product: Gentoo Linux Reporter: Ben Peddell <lightspeed85>
Component: [OLD] Core systemAssignee: Mike Gilbert <floppym>
Status: RESOLVED FIXED    
Severity: normal CC: slyfox
Priority: Normal    
Version: unspecified   
Hardware: ARM   
OS: Linux   
Whiteboard:
Package list:
Runtime testing required: ---
Bug Depends on:    
Bug Blocks: 371525    

Description Ben Peddell 2013-01-25 14:52:09 UTC
The btrfs programs (and undoubtedly kernel modules) encounter unaligned accesses.

Reproducible: Always

Steps to Reproduce:
1. run mkfs.btrfs {device}
2.
3.
Actual Results:  
[836847.499603] Alignment trap: mkfs.btrfs (21448) PC=0x00025458 Instr=0xe8880018 Address=0xbeba69c5 FSR 0x801
[836847.500438] Alignment trap: mkfs.btrfs (21448) PC=0x00025458 Instr=0xe8880018 Address=0xbeba69c5 FSR 0x801
[836847.501568] Alignment trap: mkfs.btrfs (21448) PC=0x00025458 Instr=0xe8880018 Address=0xbeba697d FSR 0x801
[836847.501868] Alignment trap: mkfs.btrfs (21448) PC=0x00025458 Instr=0xe8880018 Address=0xbeba697d FSR 0x801


Expected Results:  
no alignment traps

Numerous patches have been submitted to linux-btrfs, but they appear to have been ignored by the linux-btrfs maintainer.

https://patchwork.kernel.org/patch/645671/
https://patchwork.kernel.org/patch/1168401/
https://patchwork.kernel.org/patch/1622451/

I suspect that the reason why we don't see crashes in the kernel from this on ARM is because the kernel fixes up (but does not report) unaligned accesses that occur in kernel mode.

# emerge --info
Portage 2.1.11.9 (default/linux/arm/10.0, gcc-4.5.4, glibc-2.15-r2, 3.4.2-gentoo-r1 armv5tel)
=================================================================
System uname: Linux-3.4.2-gentoo-r1-armv5tel-Feroceon_88FR131_rev_1_-v5l-with-gentoo-2.1
Timestamp of tree: Fri, 25 Jan 2013 12:45:01 +0000
app-shells/bash:          4.2_p37
dev-lang/python:          2.7.3-r2, 3.2.3
dev-util/cmake:           2.8.7-r5
dev-util/pkgconfig:       0.27.1
sys-apps/baselayout:      2.1-r1
sys-apps/openrc:          0.9.8.4
sys-apps/sandbox:         2.5
sys-devel/autoconf:       2.68
sys-devel/automake:       1.11.6
sys-devel/binutils:       2.22-r1
sys-devel/gcc:            4.2.4-r1, 4.5.4
sys-devel/gcc-config:     1.7.3
sys-devel/libtool:        2.4-r1
sys-devel/make:           3.82-r3
sys-kernel/linux-headers: 3.4-r2 (virtual/os-headers)
sys-libs/glibc:           2.15-r2
Repositories: gentoo x-local
ACCEPT_KEYWORDS="arm"
ACCEPT_LICENSE="* -@EULA"
CBUILD="armv5tel-softfloat-linux-gnueabi"
CFLAGS="-O2 -pipe"
CHOST="armv5tel-softfloat-linux-gnueabi"
CONFIG_PROTECT="/etc /var/bind"
CONFIG_PROTECT_MASK="/etc/ca-certificates.conf /etc/env.d /etc/fonts/fonts.conf /etc/gconf /etc/gentoo-release /etc/php/apache2-php5.4/ext-active/ /etc/php/cgi-php5.4/ext-active/ /etc/php/cli-php5.4/ext-active/ /etc/revdep-rebuild /etc/sandbox.d /etc/terminfo"
CXXFLAGS="-O2 -pipe"
DISTDIR="/var/lib/gentoo-distfiles"
EMERGE_DEFAULT_OPTS="--autounmask=n"
FCFLAGS="-O2"
FEATURES="assume-digests binpkg-logs buildpkg config-protect-if-modified distlocks ebuild-locks fixlafiles news parallel-fetch parse-eapi-ebuild-head protect-owned sfperms strict unknown-features-warn unmerge-logs unmerge-orphans userfetch"
FFLAGS="-O2"
GENTOO_MIRRORS="http://ftp.iinet.net.au/linux/Gentoo/           http://mirror.ihug.co.nz/gentoo/"
LDFLAGS="-Wl,-O1 -Wl,--as-needed"
PKGDIR="/var/lib/gentoo-packages"
PORTAGE_CONFIGROOT="/"
PORTAGE_RSYNC_OPTS="--recursive --links --safe-links --perms --times --compress --force --whole-file --delete --stats --human-readable --timeout=180 --exclude=/distfiles --exclude=/local --exclude=/packages"
PORTAGE_TMPDIR="/var/tmp"
PORTDIR="/usr/portage"
PORTDIR_OVERLAY="/var/lib/portage-overlays/local"
SYNC="rsync://rsync.au.gentoo.org/gentoo-portage/"
USE="acl arm berkdb bzip2 cgi cli cracklib crypt cxx exif fortran gdbm iconv modules mudflap mysql ncurses nls nptl openmp pcre php readline session snmp ssl tcpd threads unicode xml zlib" ALSA_PCM_PLUGINS="adpcm alaw asym copy dmix dshare dsnoop empty extplug file hooks iec958 ioplug ladspa lfloat linear meter mmap_emul mulaw multi null plug rate route share shm softvol" APACHE2_MODULES="authn_core authz_core socache_shmcb unixd actions alias auth_basic authn_alias authn_anon authn_dbm authn_default authn_file authz_dbm authz_default authz_groupfile authz_host authz_owner authz_user autoindex cache cgi cgid dav dav_fs dav_lock deflate dir disk_cache env expires ext_filter file_cache filter headers include info log_config logio mem_cache mime mime_magic negotiation rewrite setenvif speling status unique_id userdir usertrack vhost_alias" CALLIGRA_FEATURES="kexi words flow plan sheets stage tables krita karbon braindump" CAMERAS="ptp2" COLLECTD_PLUGINS="df interface irq load memory rrdtool swap syslog" ELIBC="glibc" GPSD_PROTOCOLS="ashtech aivdm earthmate evermore fv18 garmin garmintxt gpsclock itrax mtk3301 nmea ntrip navcom oceanserver oldstyle oncore rtcm104v2 rtcm104v3 sirf superstar2 timing tsip tripmate tnt ubx" INPUT_DEVICES="keyboard mouse evdev" KERNEL="linux" LCD_DEVICES="bayrad cfontz cfontz633 glk hd44780 lb216 lcdm001 mtxorb ncurses text" LIBREOFFICE_EXTENSIONS="presenter-console presenter-minimizer" PHP_TARGETS="php5-3" PYTHON_SINGLE_TARGET="python2_7" PYTHON_TARGETS="python2_7 python3_2" RUBY_TARGETS="ruby18 ruby19" USERLAND="GNU" VIDEO_CARDS="exynos fbdev omap omapfb dummy v4l" XTABLES_ADDONS="quota2 psd pknock lscan length2 ipv4options ipset ipp2p iface geoip fuzzy condition tee tarpit sysrq steal rawnat logmark ipmark dhcpmac delude chaos account"
Unset:  CPPFLAGS, CTARGET, INSTALL_MASK, LANG, LC_ALL, MAKEOPTS, PORTAGE_BUNZIP2_COMMAND, PORTAGE_COMPRESS, PORTAGE_COMPRESS_FLAGS, PORTAGE_RSYNC_EXTRA_OPTS, USE_PYTHON
Comment 1 Mike Gilbert gentoo-dev 2013-01-26 16:13:24 UTC
I don't plan to diverge from upstream in this package -- I'm not knowledgeable enough to know when it is safe to do so.

Please continue to submit patches upstream.
Comment 2 Mike Gilbert gentoo-dev 2013-01-26 16:19:32 UTC
Actually, it looks like one of those patches landed upstream very recently:

http://git.kernel.org/?p=linux/kernel/git/mason/btrfs-progs.git;a=commit;h=0b0a954e4383d2074c499523e2aa89527e5ac053
Comment 3 Ben Peddell 2013-01-26 19:57:34 UTC
Whoa!

Instead of 4 alignment traps when running mkfs.btrfs, I now get 3234 alignment traps when using the default '-O2 -pipe' CFLAGS.  When using the btrfs-progs default '-g -O1' CFLAGS, no alignment traps are encountered.

It looks like gcc 4.5.4 optimizes out memcpy when it is copying 4 bytes and optimization level 2 is selected.

e.g. one of the functions that got alignment errors is leaf_space_used in ctree.c

It inlines btrfs_item_end_nr(l, start);
That inlines btrfs_item_end();
That inlines btrfs_item_offset() and btrfs_item_size(), both of which use memcpy to try to avoid those alignment traps.

I'll try a newer gcc, and see if this is still the case.
Comment 4 Ben Peddell 2013-01-27 06:47:50 UTC
I have submitted a patch upstream to attempt to resolve this.

Looking at /usr/src/linux/include/linux/unaligned/memmove.h, they've known that memcpy gets optimized out by gcc for word-sized moves.  Unfortunately, it seems that gcc 4.5.4 (and probably 4.6 and 4.7) also optimize out memmove for word-sized moves.

Therefore my proposed patch uses the packed struct method of unaligned access.

https://patchwork.kernel.org/patch/2051321/
Comment 5 Piotr Szymaniak 2016-09-22 19:55:26 UTC
Looking for btrfs-progs related bugs I've found this one. It seems that this version is gone from portage and mentioned patch in last post was queued by David Sterba for integration in 2013. Marking as FIXED.