Bug 12301 - Kernel boot hanging on 4.12.14 with qemux86-64
Summary: Kernel boot hanging on 4.12.14 with qemux86-64
Status: RESOLVED FIXED
Alias: None
Product: OE-Core
Classification: Build System, Metadata & Runtime
Component: kernel (show other bugs)
Version: 2.2
Hardware: x86 Multiple
: Medium+ normal
Target Milestone: 2.5 M1
Assignee: Richard Purdie
QA Contact:
URL:
Whiteboard:
: 12124 (view as bug list)
Depends on:
Blocks:
 
Reported: 2017-11-06 15:09 UTC by Richard Purdie
Modified: 2018-01-04 22:07 UTC (History)
7 users (show)

See Also:
OS type for building Yocto: ---
Type of Regression: ---
Verified:
Documentation change: No (bug/feature does not impact docs)


Attachments
Logs of the good/bad boots (150.33 KB, application/x-compressed-tar)
2017-11-06 15:10 UTC, Richard Purdie
no flags Details
fedora26 qemu boot log (6.99 KB, application/octet-stream)
2017-11-10 15:12 UTC, Richard Purdie
no flags Details
fedora26 qemu boot log #2 (6.99 KB, application/octet-stream)
2017-11-10 15:12 UTC, Richard Purdie
no flags Details
testimage task log (8.44 KB, application/octet-stream)
2017-11-10 15:13 UTC, Richard Purdie
no flags Details
testiamge task log #2 (8.83 KB, application/octet-stream)
2017-11-10 15:14 UTC, Richard Purdie
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Richard Purdie 2017-11-06 15:09:43 UTC
This occurred on tumbleweed.yocto.io:

https://autobuilder.yocto.io/builders/nightly-musl-x86-64/builds/27/steps/Running%20Sanity%20Tests/logs/stdio

NOTE: core-image-sato-sdk-1.0-r0 do_testimage: Target didn't reached login boot in 1500 seconds
NOTE: core-image-sato-sdk-1.0-r0 do_testimage: Last 25 lines of text:
[    2.525934] usbcore: registered new interface driver ftdi_sio
[    2.526923] usbserial: USB Serial support registered for FTDI USB Serial Device
[    2.528315] usbcore: registered new interface driver pl2303
[    2.529299] usbserial: USB Serial support registered for pl2303
[    2.530373] i8042: PNP: PS/2 Controller [PNP0303:KBD,PNP0f13:MOU] at 0x60,0x64 irq 1,12
[    2.532548] serio: i8042 KBD port at 0x60,0x64 irq 1
[    2.533495] serio: i8042 AUX port at 0x60,0x64 irq 12
[    2.534544] mousedev: PS/2 mouse device common for all mice
[    2.535831] input: AT Translated Set 2 keyboard as /devices/platform/i8042/serio0/input/input1
[    2.537806] rtc_cmos 00:00: RTC can wake from S4
[    2.540139] rtc_cmos 00:00: rtc core: registered rtc_cmos as rtc0
[    2.541255] rtc_cmos 00:00: alarms up to one day, y3k, 114 bytes nvram, hpet irqs
[    2.544050] device-mapper: ioctl: 4.35.0-ioctl (2016-06-23) initialised: dm-devel@redhat.com
[    2.546585] sdhci: Secure Digital Host Controller Interface driver
[    2.547539] sdhci: Copyright(c) Pierre Ossman
[    2.548306] sdhci-pltfm: SDHCI platform and OF driver helper
[    2.549343] usbcore: registered new interface driver usbhid
[    2.550245] usbhid: USB HID core driver
[    2.551028] u32 classifier
[    2.551560]     Actions configured
[    2.552489] NET: Registered protocol family 10
[    2.555734] Segment Routing with IPv6
[    2.556386] sit: IPv6, IPv4 and MPLS over IPv4 tunneling driver
[    2.557476] NET: Registered protocol family 17
[    2.558334] Key type dns_resolver registered
NOTE: core-image-sato-sdk-1.0-r0 do_testimage: Check full boot log: /home/pokybuild/yocto-autobuilder/yocto-worker/nightly-musl-x86-64/build/build/tmp/work/qemux86_64-poky-linux-musl/core-image-sato-sdk/1.0-r0/testimage/qemu_boot_log.20171106031513
WARNING: core-image-sato-sdk-1.0-r0 do_testimage: Qemu ended unexpectedly, dump data from host is in /tmp/oe-saved-tests/201711060340_qemu

At a shell on the builder, running:

pokybuild@tumbleweed:~/yocto-autobuilder/yocto-worker/nightly-musl-x86-64/build/build> DISPLAY=:1 bitbake -v core-image-minimal core-image-full-cmdline core-image-sato-sdk -c testimage

seems reproduce the hangs every 1-3 attempts. Its random which image will hang and it hangs early in kernel boot.

I'll attach the logs from the good/bad runs, I've not been able to spot any pattern.

Putting " clocksource=kvm-clock hpet=disable noapic nolapic" on the kernel commandline does seem to make it much less likely to hang, still trying to check if I can make it hang with all these specified. I have tried pruning options but if I remove any of these, it does hang, albeit less frequently.

I am also using a kernel with ftrace disabled as one of the early kernel logs had:

[    0.000000] Memory: 239224K/261608K available (9366K kernel code, 1201K rwdata, 2872K rodata, 1440K init, 480K bss, 22384K reserved, 0K cma-reserved)
[    0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
[    0.000000] ftrace: allocating 37138 entries in 146 pages
[    0.000000] ------------[ cut here ]------------
[    0.000000] WARNING: CPU: 0 PID: 0 at /kernel-source//kernel/trace/ftrace.c:2022 ftrace_bug+0x171/0x290
[    0.000000] Modules linked in:
[    0.000000] CPU: 0 PID: 0 Comm: swapper Not tainted 4.12.14-yocto-standard #1
[    0.000000] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.10.2-0-g5f4c7b1-prebuilt.qemu-project.org 04/01/2014
[    0.000000] task: ffffffff8380e4c0 task.stack: ffffffff83800000
[    0.000000] RIP: 0010:ftrace_bug+0x171/0x290
[    0.000000] RSP: 0000:ffffffff83803e30 EFLAGS: 00010046
[    0.000000] RAX: 00000000fffffff2 RBX: 0000000000000000 RCX: 00000000fffffff2
[    0.000000] RDX: 0000000000000005 RSI: ffffa3ca0ece50b0 RDI: 00000000fffffff2
[    0.000000] RBP: ffffffff83803e48 R08: 0000000000000001 R09: 0000000000000000
[    0.000000] R10: fffff282c03b0280 R11: ffffffff8390e388 R12: ffffa3ca0ece50b0
[    0.000000] R13: ffffa3ca0ece50b0 R14: 000000000000650b R15: 0000000000000046
[    0.000000] FS:  0000000000000000(0000) GS:ffffa3ca0fc00000(0000) knlGS:0000000000000000
[    0.000000] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[    0.000000] CR2: 0000000000000000 CR3: 0000000009809000 CR4: 00000000000006b0
[    0.000000] Call Trace:
[    0.000000]  ftrace_process_locs+0x31e/0x450
[    0.000000]  ftrace_init+0x92/0x111
[    0.000000]  start_kernel+0x1f1/0x3d6
[    0.000000]  ? early_idt_handler_array+0x120/0x120
[    0.000000]  x86_64_start_reservations+0x29/0x2b
[    0.000000]  x86_64_start_kernel+0x134/0x143
[    0.000000]  secondary_startup_64+0x9f/0x9f
[    0.000000] Code: c7 05 20 0c e4 00 01 00 00 00 48 c7 c7 9e 93 5c 83 c7 05 1f 0c e4 00 00 00 00 00 48 c7 05 f4 0b e4 00 bb 12 32 83 e9 e4 fe ff ff <0f> ff c7 05 f3 0b e4 00 01 00 00 00 48 c7 c7 48 16 5c 83 c7 05 
[    0.000000] ---[ end trace 0000000000000000 ]---
[    0.000000] ftrace faulted on modifying 
[    0.000000] [<          (null)>]           (null)
[    0.000000] Initializing ftrace call sites
[    0.000000] ftrace record flags: 0
[    0.000000]  (0)  
[    0.000000]  expected tramp: ffffffff83321210
Comment 1 Richard Purdie 2017-11-06 15:10:40 UTC
Created attachment 4116 [details]
Logs of the good/bad boots
Comment 2 Richard Purdie 2017-11-06 15:21:24 UTC
Jsut to be clear, many of these tests were made with the kernel modified to set "CONFIG_FTRACE is not set" to rule out that piece. I've not yet seen a hang when all the kernel commandline options mentioned are set together.
Comment 3 Richard Purdie 2017-11-06 16:26:02 UTC
Also noticed this:

$ grep BIOS * -R | grep bug
1/bad/qemu_boot_log.20171106114453:[    0.000000] BIOS bug: APIC version is 0 for CPU 0/0x0, fixing up to 0x10
1/bad/qemu_boot_log.20171106114453:[    0.000000] BIOS bug: APIC version mismatch, boot CPU: 0, CPU 0: version 10
1/bad/qemu_boot_log.20171106141805:[    0.000000] BIOS bug: APIC version is 0 for CPU 0/0x0, fixing up to 0x10
1/bad/qemu_boot_log.20171106141805:[    0.000000] BIOS bug: APIC version mismatch, boot CPU: 0, CPU 0: version 10
1/qemu_boot_log.20171106131447:[    0.000000] BIOS bug: APIC version mismatch, boot CPU: 0, CPU 0: version 14
2/bad/qemu_boot_log.20171106130421:[    0.000000] BIOS bug: APIC version is 0 for CPU 0/0x0, fixing up to 0x10
2/bad/qemu_boot_log.20171106130421:[    0.000000] BIOS bug: APIC version mismatch, boot CPU: 0, CPU 0: version 10
2/bad/qemu_boot_log.20171106140453:[    0.000000] BIOS bug: APIC version is 0 for CPU 0/0x0, fixing up to 0x10
2/bad/qemu_boot_log.20171106140453:[    0.000000] BIOS bug: APIC version mismatch, boot CPU: 0, CPU 0: version 10
2/bad/qemu_boot_log.20171106131447:[    0.000000] BIOS bug: APIC version mismatch, boot CPU: 0, CPU 0: version 14
2/bad/qemu_boot_log.20171106112930:[    0.000000] BIOS bug: APIC version is 0 for CPU 0/0x0, fixing up to 0x10
2/bad/qemu_boot_log.20171106112930:[    0.000000] BIOS bug: APIC version mismatch, boot CPU: 0, CPU 0: version 10
2/qemu_boot_log.20171106130943:[    0.000000] BIOS bug: APIC version mismatch, boot CPU: 0, CPU 0: version 14
2/qemu_boot_log.20171106133135:[    0.000000] BIOS bug: APIC version mismatch, boot CPU: 0, CPU 0: version 14
3/bad/qemu_boot_log.20171106112930:[    0.000000] BIOS bug: APIC version is 0 for CPU 0/0x0, fixing up to 0x10
3/bad/qemu_boot_log.20171106112930:[    0.000000] BIOS bug: APIC version mismatch, boot CPU: 0, CPU 0: version 10

The bad runs have the "version is 0" message, some of the good and bad have the version mismatch.
Comment 4 Richard Purdie 2017-11-06 16:27:12 UTC
(In reply to comment #3) 
> The bad runs have the "version is 0" message, some of the good and bad have
> the version mismatch.

Note that not all bad runs have this message.
Comment 5 Juro Bystricky 2017-11-06 21:26:41 UTC
Another observation, several "bad" (7/9) logs have the last message:

[    0.022539] ..TIMER: vector=0x30 apic1=0 pin1=2 apic2=-1 pin2=-1
Comment 6 Juro Bystricky 2017-11-06 21:52:32 UTC
After this, the code goes on and fiddles with APIC bits...
/arch/x86/kernel/apic/io_apic.c line 2082 ...

I will try to reproduce the problem (need to install tumbleweed first), optionally add few messages following the line 2082...
Comment 7 Richard Purdie 2017-11-06 23:18:13 UTC
FWIW I tried to reproduce this on Ubuntu 17.04 and can't
Comment 8 Bruce Ashfield 2017-11-07 02:00:59 UTC
I also did about 60 boot sequences on my builder and haven't seen it. I tried with and without KVM.

Will continue trying here.
Comment 9 Richard Purdie 2017-11-07 10:42:53 UTC
I upgraded to Ubuntu 1710 which has a 4.13.0 kernel and this still works fine. I also copied the qemu-native binaries from the tumbleweed builder and those also work fine (once the interpreter paths are tweaked).

This leads to me suspect its the tumbleweed 4.13.10 kernel (which is preempt) compared to the Ubuntu 4.10.0 which isn't preempt.
Comment 10 Richard Purdie 2017-11-07 13:09:07 UTC
Further update - we changed the kernel on tumbleweed.yocto.io from the default to the vanilla one which means the suse patchset was removed. The hang has then disappeared. Its looking very likely that something in the suse patchset is therefore breaking the timers in qemu when using kvm.
Comment 11 Joshua Lock 2017-11-07 13:21:05 UTC
*** Bug 12124 has been marked as a duplicate of this bug. ***
Comment 12 Richard Purdie 2017-11-10 15:08:31 UTC
We've just seen two failures which look very like this on fedora26. https://autobuilder.yocto.io/builders/nightly-x86-64-lsb/builds/575 has the failures however the SIGTERM is misleading. The actual logs are attached.

I've added a separate patch which addresses the logging issue this raises (http://git.yoctoproject.org/cgit.cgi/poky/commit/?id=702f4692a25c670d7d14da106f545b86e55473ce)

Fedora 26 has a 4.13.10 kernel like tumbleweed so I suspect we're looking for a broken patch that both Fedora and SUSE apply to 4.13.10.
Comment 13 Richard Purdie 2017-11-10 15:12:27 UTC
Created attachment 4118 [details]
fedora26 qemu boot log
Comment 14 Richard Purdie 2017-11-10 15:12:45 UTC
Created attachment 4119 [details]
fedora26 qemu boot log #2
Comment 15 Richard Purdie 2017-11-10 15:13:04 UTC
Created attachment 4120 [details]
testimage task log
Comment 17 Richard Purdie 2017-11-10 15:14:47 UTC
Created attachment 4121 [details]
testiamge task log #2
Comment 18 Richard Purdie 2017-11-10 15:16:11 UTC
Also just to note that rng is running on fedora26 so I suspect this issue is not entropy related.
Comment 19 Richard Purdie 2017-11-10 15:18:33 UTC
Since affecting more than one worker, up priority.
Comment 20 Joshua Lock 2017-11-10 15:25:27 UTC
(In reply to comment #16)
> Tumbleweed kernel patches:
> https://github.com/openSUSE/kernel-source/tree/
> db36cf83181a996bfdb86b3c32dc376d7b6922f0
> 
> Fedora 26 kernel patches:
> https://src.fedoraproject.org/rpms/kernel/tree/f26

Actually, that's the 4.3.12 kernel. For 4.3.10 we want: 

https://src.fedoraproject.org/rpms/kernel/tree/0b2f4649840f7fb349bcfe10378a85bfc9e448a2
Comment 21 Richard Purdie 2017-11-10 16:16:53 UTC
Also, https://autobuilder.yocto.io/builders/nightly-oe-selftest/builds/614

which looks like the same kernel issue and on tumbleweed but with the vanilla kernel:

rpurdie@tumbleweed:~> cat /proc/version 
Linux version 4.13.10-1-vanilla (geeko@buildhost) (gcc version 7.2.1 20171020 [gcc-7-branch revision 253932] (SUSE Linux)) #1 SMP PREEMPT Fri Oct 27 09:03:09 UTC 2017 (db36cf8)

So this could be a 4.13 general issue.
Comment 22 Juro Bystricky 2017-11-10 16:35:07 UTC
I installed tumbleweed.
Then I could reproduce the bug by simply:

$ MACHINE=qemux86-64 bitbake core-image-minimal
$ runqemu kvm

The QEMU boot stopped at the TIMER mesage, this happened about 3x out of 5 attempts.
Then I installed some other packages (can't remember which, remember, this was a brand new install so some of my favorite utilities were not present). So I think something else was updated as a by product.

After that I could not reproduce the problem in about 50 attempts.
The current kernel is:
4-13-10-1-default #1 SMP PREEMPT Fri Oct 27 09:03:09 UTC 2017 (db36cf8)

I tried to boot a previous snapshot, but it only boots read-only, so I could not get the TAP working.

Anyway, I will try Fedora 26, as it is more reproducible environment (Tumbleweed has rolling updates, so occasional "instabilities" are to be expected)
Comment 23 Juro Bystricky 2017-11-13 15:07:58 UTC
Since the problem seems to be related to kernel 4.13.10, I installed and booted upstreamed 4.13.10 with Ubuntu 17.04. (I built/tested with and without CONFIG_PREEMPT), presently I have:
4.13.10 #11 SMP PREEMPT Sat Nov 11 22:20:26 PST 2017 x86_64 x86_64 x86_64 GNU/Linux

I repeatedly ran (with and without KVM, does not seem to matter) over the weekend:

$ bitbake core-image-minimal core-image-full-cmdline core-image-sato-sdk -c testimage

Although I did not reproduce the problem, I saw frequent anomalies:
Normally, I would expect:

tsc: Fast TSC calibration using PIT

But sometimes (frequently) the boot massage says:

[    0.000000] tsc: Fast TSC calibration failed
[    0.000000] tsc: Unable to calibrate against PIT
[    0.000000] tsc: No reference (HPET/PMTIMER) available
...
[    0.946000] APIC calibration not consistent with PM-Timer: 103ms instead of 100ms
[    0.946000] APIC delta adjusted to PM-Timer: 6249951 (6440338)


I also saw these messages in some log files::

[    0.000000] tsc: Unable to calibrate against PIT
[    0.000000] tsc: No reference (HPET/PMTIMER) available
...
[    0.003000] tsc: Fast TSC calibration failed
[    0.008000] tsc: Unable to calibrate against PIT
[    0.008000] tsc: using HPET reference calibration
..
[    0.803000] APIC calibration not consistent with PM-Timer: 103ms instead of 100ms
[    0.803000] APIC delta adjusted to PM-Timer: 6249972 (6499899)

I would expect the dmesg log to be always identical (with possible timestamps differences), but it seems sometimes we detect HPET/PTIMER and sometimes we don't.

BTW, I don't know if this is 4.13.10 issue only, I will check if we had these kind of inconsistent boot messages before. Since the boot/tests succeed  in both cases (with or without HPET/PTIMER detected), we may have not noticed these before.
Comment 24 Juro Bystricky 2017-11-14 00:52:36 UTC
I installed Fedora26.
I ran repeatedly:

$ bitbake core-image-minimal core-image-full-cmdline core-image-sato-sdk -c testimage

Could not reproduce the problem. But I found out my Fedora is not 4.13.10:

Linux localhost.localdomain 4.13.11-200.fc26.x86_64 #1 SMP Thu Nov 2 18:28:35 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

It does not seem to be possible to "downgrade" the kernel to 4.13.10,
as the kernel is not listed in:

$ yum list --showduplicates kernel
...
Available Packages
kernel.x86_64                                 4.11.8-300.fc26                                   @anaconda
kernel.x86_64                                 4.11.8-300.fc26                                   fedora   
kernel.x86_64                                 4.13.11-200.fc26                                  @updates 
kernel.x86_64                                 4.13.11-200.fc26                                  updates
Comment 25 Juro Bystricky 2017-11-22 17:44:28 UTC
Observed again on:
Linux version 4.13.12-1-vanilla (geeko@buildhost) (gcc version 7.2.1 20171020 [gcc-7-branch revision 253932] (SUSE Linux)) #1 SMP PREEMPT Wed Nov 8 11:21:09 UTC 2017 (9151c66)

| DEBUG: Target didn't reached login boot in 1500 seconds
| DEBUG: Last 25 lines of text:
| [    0.000000] Memory: 239224K/261608K available (9366K kernel code, 1201K rwdata, 2872K rodata, 1440K init, 480K bss, 22384K reserved, 0K cma-reserved)
| [    0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
| [    0.000000] ftrace: allocating 37138 entries in 146 pages
| [    0.001000] Preemptible hierarchical RCU implementation.
| [    0.001000] 	RCU restricting CPUs from NR_CPUS=64 to nr_cpu_ids=1.
| [    0.001000] RCU: Adjusting geometry for rcu_fanout_leaf=16, nr_cpu_ids=1
| [    0.001000] NR_IRQS:4352 nr_irqs:256 16
| [    0.001000] Console: colour VGA+ 80x25
| [    0.001000] console [tty1] enabled
| [    0.001000] console [ttyS0] enabled
| [    0.001000] clocksource: hpet: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604467 ns
| [    0.001000] tsc: Detected 2599.996 MHz processor
| [    0.001000] Calibrating delay loop (skipped) preset value.. 5199.99 BogoMIPS (lpj=2599996)
| [    0.001000] pid_max: default: 32768 minimum: 301
| [    0.001000] ACPI: Core revision 20170303
| [    0.001000] ACPI: 1 ACPI AML tables successfully acquired and loaded
| [    0.001000] Security Framework initialized
| [    0.001000] Mount-cache hash table entries: 512 (order: 0, 4096 bytes)
| [    0.001000] Mountpoint-cache hash table entries: 512 (order: 0, 4096 bytes)
| [    0.001000] mce: CPU supports 10 MCE banks
| [    0.001000] Last level iTLB entries: 4KB 0, 2MB 0, 4MB 0
| [    0.001000] Last level dTLB entries: 4KB 0, 2MB 0, 4MB 0, 1GB 0
| [    0.001000] Freeing SMP alternatives memory: 36K
| [    0.001000] smpboot: Max logical packages: 1
| [    0.001000] ..TIMER: vector=0x30 apic1=0 pin1=2 apic2=-1 pin2=-1
Comment 26 Richard Purdie 2017-11-27 17:00:55 UTC
To add some extra data to this bug report, we're seeing the issue on both fedora26 and tumbleweed autobuilders. We've seen it with 4.13.10 and 4.13.12 kernels and with the vanialla tumbleweed kernel.

When the bug occurs, the dmesg usually contains:

BIOS bug: APIC version is 0 for CPU 0/0x0, fixing up to 0x10
BIOS bug: APIC version mismatch, boot CPU: 0, CPU 0: version 10

it should have a version field of 0x14. I've checked the qemu code and that puts the 0x14 version in all the right places. I've instrumented a guest kernel and reads of the version register through mmio always reads 0.

When it sees version 0x0, it basically means the APIC is missing and the kernel then later hangs at some future point, often the ..TIMER line but sometimes elsewhere, basically as it never gets timer interrupts.

For some reason our x86-64 kernel does not enable x2apic mode. If we enable x2apic mode, the guest will switch to accessing the APIC through MSR and will then start working.

Also, if we disable KVM (or likely just irqchip), we don't see this issue.

The issue is therefore likely somewhere in the APIC virutalisation code and the mmio page memory (rather than the MSR).

I have tried taking the tumbleweed kernel+kernel modules and qemu binary (but not dependencies), copying them to my local ubuntu system and booting/using them. I was unable to reproduce the hangs locally.

I am able to reproduce the hangs on the tumbleweed autobuilder at will using my brute force script which I used for the qemuppc bug. In this case you can just test of the BIOS BUG message to know if there was a problem.
Comment 27 Richard Purdie 2017-11-27 17:59:43 UTC
From /proc/cpuinfo, flags in common:
fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm cpuid_fault epb intel_ppin tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm xsaveopt cqm_llc cqm_occup_llc dtherm ida arat pln pts 

Tubleweed autobuilder only:
x2apic

My local system only:
3dnowprefetch  intel_pt cat_l3 cdp_l3 hle rtm rdt_a rdseed adx smap cqm_mbm_total cqm_mbm_local

I was testing my local system booting with nox2apic, I also tried is intel_pstate disabled.
Comment 28 Juro Bystricky 2017-11-27 18:18:31 UTC
The code in linus kernel apic.c says:

	if (version == 0x0) {
		pr_warning("BIOS bug: APIC version is 0 for CPU %d/0x%x, fixing up to 0x10\n",
			   cpu, apicid);
		version = 0x10;
	}

	if (version != boot_cpu_apic_version) {
		pr_warning("BIOS bug: APIC version mismatch, boot CPU: %x, CPU %d: version %x\n",
			boot_cpu_apic_version, cpu, version);
	}

But it only **says** it is fixing the version. I don't actually see the version being changed. It says 0x0 (or whatever the passed "version" was), so the message is a bit misleading.
Comment 29 Juro Bystricky 2017-11-27 19:21:55 UTC
(In reply to comment #27)
> From /proc/cpuinfo, flags in common:
> fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36
> clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm
> constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid
> aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg
> fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 movbe popcnt tsc_deadline_timer
> aes xsave avx f16c rdrand lahf_lm abm cpuid_fault epb intel_ppin tpr_shadow
> vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms
> invpcid cqm xsaveopt cqm_llc cqm_occup_llc dtherm ida arat pln pts 
> 
> Tubleweed autobuilder only:
> x2apic
> 
> My local system only:
> 3dnowprefetch  intel_pt cat_l3 cdp_l3 hle rtm rdt_a rdseed adx smap
> cqm_mbm_total cqm_mbm_local
> 
> I was testing my local system booting with nox2apic, I also tried is
> intel_pstate disabled.

Speaking of x2apic, QEMU only enables (emulates) it with KVM, you can verify this with (runqemu with and without KVM):

# grep x2apic /proc/cpuinfo

So if we don't see problems without KVM, it could be pointing at problems with x2apic emulation.
Comment 30 Juro Bystricky 2017-11-27 19:50:16 UTC
(In reply to comment #26)
> To add some extra data to this bug report, we're seeing the issue on both
> fedora26 and tumbleweed autobuilders. We've seen it with 4.13.10 and 4.13.12
> kernels and with the vanialla tumbleweed kernel.
> 
> When the bug occurs, the dmesg usually contains:
> 
> BIOS bug: APIC version is 0 for CPU 0/0x0, fixing up to 0x10
> BIOS bug: APIC version mismatch, boot CPU: 0, CPU 0: version 10
> 
> it should have a version field of 0x14. I've checked the qemu code and that
> puts the 0x14 version in all the right places. I've instrumented a guest
> kernel and reads of the version register through mmio always reads 0.
> 
> When it sees version 0x0, it basically means the APIC is missing and the
> kernel then later hangs at some future point, often the ..TIMER line but
> sometimes elsewhere, basically as it never gets timer interrupts.
> 
> For some reason our x86-64 kernel does not enable x2apic mode. If we enable
> x2apic mode, the guest will switch to accessing the APIC through MSR and
> will then start working.
> 
> Also, if we disable KVM (or likely just irqchip), we don't see this issue.
> 
> The issue is therefore likely somewhere in the APIC virutalisation code and
> the mmio page memory (rather than the MSR).
> 
> I have tried taking the tumbleweed kernel+kernel modules and qemu binary
> (but not dependencies), copying them to my local ubuntu system and
> booting/using them. I was unable to reproduce the hangs locally.
> 
> I am able to reproduce the hangs on the tumbleweed autobuilder at will using
> my brute force script which I used for the qemuppc bug. In this case you can
> just test of the BIOS BUG message to know if there was a problem.

Just to clarify: are there no problems when used together -enable-kvm -no-kvm-irqchip ?
Comment 31 Richard Purdie 2017-11-28 11:27:08 UTC
(In reply to comment #30)
> Just to clarify: are there no problems when used together -enable-kvm
> -no-kvm-irqchip ?

I think there may be two bugs. With "-enable-kvm -no-kvm-irqchip", I do not see the BIOS bug message and I do not see it hang at ..TIMER but I do see it hang later in the boot around udevd or ssh key generation, in a similar way to the qemuppc lack of timer interrupts bug.

So there are two issues here and removing the emulated APIC only seems to partly solve the issue. Whether they're related, unsure.
Comment 32 Richard Purdie 2017-11-28 13:27:27 UTC
Robert reported that upgrading qemu to 2.11-rc2 resolved issues for him. I tried that and it does resolve the hangs I saw with -no-kvm-irqchip however the issues I saw without that remain.
Comment 33 Richard Purdie 2017-11-28 13:43:04 UTC
Also, passing in the kernel options "noapic nolapic hpet=disable clocksource=kvm-clock noioapic" does not avoid the hang.
Comment 34 Richard Purdie 2017-11-28 16:49:27 UTC
After some comments from Robert I rebooted tumbleweed and was no longer able to reproduce the issue.

This is therefore starting to look like some kind of bad kernel kvm state. The question is therefore becomming, how do you wedge the kernel in that state?
Comment 35 Juro Bystricky 2017-11-28 16:57:26 UTC
(In reply to comment #34)
> After some comments from Robert I rebooted tumbleweed and was no longer able
> to reproduce the issue.
> 
> This is therefore starting to look like some kind of bad kernel kvm state.
> The question is therefore becomming, how do you wedge the kernel in that
> state?

Is there a possibility the rebooted tumbleweed was not the same as before?
I believe they tend to upgrade various components rapidly, you may not necessarily notice that.
Comment 36 Juro Bystricky 2017-11-28 17:01:18 UTC
(In reply to comment #34)
> After some comments from Robert I rebooted tumbleweed and was no longer able
> to reproduce the issue.
> 
> This is therefore starting to look like some kind of bad kernel kvm state.
> The question is therefore becomming, how do you wedge the kernel in that
> state?

Please see my comment 22, same thing: I rebooted tumbleweed and could not reproduce the problem anymore
Comment 37 Richard Purdie 2017-11-28 17:02:46 UTC
Next up is to mix up qemux86 and qemux86-64 runs. Its possible bad state from 32 bit runs which could explain why qemux86 keeps working...
Comment 38 Richard Purdie 2017-11-28 18:43:21 UTC
(In reply to comment #37)
> Next up is to mix up qemux86 and qemux86-64 runs. Its possible bad state
> from 32 bit runs which could explain why qemux86 keeps working...

No luck booting all qemux86 first, then qemux86-64, or all qemux86-64, then qemux86, then qemux86-64 or intermixing qemux86 and qemx86-64 on my local autobuilder with the tumbleweed kernel+modules. With qemu 2.10, not 2.11.

The tumbleweed builder is also no longer reproducing now its been rebooted.
Comment 39 Robert Yang 2017-11-29 10:50:16 UTC
It's so strange, I can't reproduce it any more with 2.10 or 2.11 after reboot it again. I got got hang everytime with 2.10 yesterday.
Comment 40 Richard Purdie 2017-12-10 15:34:47 UTC
New data point. The build failed on tumbleweed with the hanging boot. I then could reproduce hangs with runqemu-parallel.py. I tried unloading and reloading the kvm + kvm_intel + irqbypass modules which made no difference.

After a "echo 1 > /proc/sys/vm/drop_caches" it all started working perfectly.

So it appears that its a memory fragmentation related bug.
Comment 41 Richard Purdie 2017-12-10 15:40:35 UTC
From the builder's dmesg:

[85637.442463] dropcache (1187): drop_caches: 1
[163691.861365] bash (39598): drop_caches: 1

so it was able to fragment memory in around 24 hours. The rough log of builds on that machine from reboot is:

Dec 10 12:42 nightly-ppc
Dec 10 12:42 nightly-mips-lsb 	#624 	Build successful (master)
Dec 10 12:42 nightly-x86-64-lsb 	#644 	Failed running sanity tests (master)
Dec 10 02:09 nightly-oe-selftest 	#677 	Failed running oe-selftest (morty)
Dec 10 01:53 nightly-mips-lsb 	#623 	Build successful (morty)
Dec 10 02:11 build-appliance 	#629 	Build successful (morty)
Dec 10 02:09 poky-tiny 	#620 	Build successful (morty)
Dec 10 02:07 buildtools 	#621 	Build successful (morty)
Dec 10 01:53 nightly-deb-non-deb 	#635 	Build successful (morty)
Dec 10 02:05 nightly-refkit 	#339 	Build successful (morty)
Dec 10 01:59 nightly-musl 	#644 	Build successful (morty)
Dec 10 01:54 eclipse-plugin-oxygen 	#136 	Build successful (master)
Dec  9 15:01 nightly-x86 (master)
Dec  9 14:59 nightly-mips64 (master)
Dec  9 14:53 eclipse-plugin-neon (master)
Dec  9 14:53 nightly-wic (master)
Dec  9 14:53 nightly-non-gpl3 (master)
Dec  9 11:26 nightly-mips (morty)
Dec  9 11:24 nightly-world-lsb (morty)
Dec  9 06:27 nightly-arm64 (rocko)
Dec  9 04:53 nightly-x86-64 (rocko)
Dec  8 18:25 nightly-qa-extras (morty)
Dec  8 18:15 nightly-uclibc (morty)
Dec  8 18:15 nightly-musl-x86-64 (morty)
<reboot>
Comment 42 Juro Bystricky 2017-12-10 17:34:06 UTC
(In reply to comment #40)
> New data point. The build failed on tumbleweed with the hanging boot. I then
> could reproduce hangs with runqemu-parallel.py. I tried unloading and
> reloading the kvm + kvm_intel + irqbypass modules which made no difference.
> 
> After a "echo 1 > /proc/sys/vm/drop_caches" it all started working perfectly.
> 
> So it appears that its a memory fragmentation related bug.

Interesting, I assume after you drop the caches you need to wait until the memory becomes fragmented before you can reproduce the problem again. Is it possible to get the dmesg log after the problem occurs? Any memory failures should be logged in there, they may contain hints.
Comment 43 Richard Purdie 2017-12-10 23:31:29 UTC
(In reply to comment #42)
> (In reply to comment #40)
> > New data point. The build failed on tumbleweed with the hanging boot. I then
> > could reproduce hangs with runqemu-parallel.py. I tried unloading and
> > reloading the kvm + kvm_intel + irqbypass modules which made no difference.
> > 
> > After a "echo 1 > /proc/sys/vm/drop_caches" it all started working perfectly.
> > 
> > So it appears that its a memory fragmentation related bug.
> 
> Interesting, I assume after you drop the caches you need to wait until the
> memory becomes fragmented before you can reproduce the problem again.

I would assume so, yes.

> Is it
> possible to get the dmesg log after the problem occurs? Any memory failures
> should be logged in there, they may contain hints.

There is nothing in the logs that appears related...
Comment 44 Richard Purdie 2017-12-11 11:50:41 UTC
I'm finding:

https://elixir.free-electrons.com/linux/latest/source/arch/x86/kvm/lapic.c#L177

an interesting piece of code.

Its a kvzalloc() call so would use kalloc, falling back to vmalloc so it may or may not be contiguous. If it fails, as far as I can tell the system is in trouble...
Comment 45 Juro Bystricky 2017-12-11 17:17:56 UTC
(In reply to comment #44)
> I'm finding:
> 
> https://elixir.free-electrons.com/linux/latest/source/arch/x86/kvm/lapic.
> c#L177
> 
> an interesting piece of code.
> 
> Its a kvzalloc() call so would use kalloc, falling back to vmalloc so it may
> or may not be contiguous. If it fails, as far as I can tell the system is in
> trouble...

This is by (relatively new) design, so you can work in fragmented memory. Basically, most of kmallocs were converted to kvmallocs. I am pretty sure I tried replacing kvmallocs with vmallocs at some point to see if something breaks. There was only one allocation (kvm_arch_init) that actually complained (BUG_ON triggerd). Having said that, maybe there were allocations that I somehow did not test.
Comment 46 Richard Purdie 2017-12-11 17:20:18 UTC
I did some further digging on tumbleweed as I was once again able to reproduce the hangs.

I ran: 

$ trace-cmd record -b 20000 -T -e kmem

whilst reproducing the hang. This generated a 19GB log.

$ trace-cmd report | grep kmalloc.*ptr=[^0]

the listed six kmalloc failures. Since this is on a 56 core system the logs were unreadable to associate the right stack trace with the right kmalloc. I therefore took the cpu number and did:

$ trace-cmd report --cpu 27 | grep kmalloc.*ptr=[^0] 

which shows the key piece:

           <...>-8213  [027] 248759.935967: kmalloc_node:         (kvmalloc_node+0x3e) call_site=ffffffffad1ce5ae ptr=(nil) bytes_req=522240 bytes_alloc=524288 gfp_flags=GFP_KERNEL|__GFP_NOWARN|__GFP_NORETRY|__GFP_ZERO node=-1
           <...>-8213  [027] 248759.935968: kernel_stack:         <stack trace>
=> kvm_arch_create_memslot (ffffffffc0d43a45)
=> __kvm_set_memory_region (ffffffffc0d29065)
=> kvm_set_memory_region (ffffffffc0d29356)
=> kvm_vm_ioctl (ffffffffc0d297e0)
=> do_vfs_ioctl (ffffffffad253cbd)
=> SyS_ioctl (ffffffffad254274)
=> entry_SYSCALL_64_fastpath (ffffffffad78ac3b)

so the call is coming from kvm_arch_create_memslot() which calls kvzalloc():

https://elixir.free-electrons.com/linux/latest/source/arch/x86/kvm/x86.c#L8286

I therefore suspect than when this call site falls back to vmalloc, the apic breaks.
Comment 47 Richard Purdie 2017-12-11 17:22:48 UTC
(In reply to comment #45)
> (In reply to comment #44)
> > I'm finding:
> > 
> > https://elixir.free-electrons.com/linux/latest/source/arch/x86/kvm/lapic.
> > c#L177
> > 
> > an interesting piece of code.
> > 
> > Its a kvzalloc() call so would use kalloc, falling back to vmalloc so it may
> > or may not be contiguous. If it fails, as far as I can tell the system is in
> > trouble...
> 
> This is by (relatively new) design, so you can work in fragmented memory.
> Basically, most of kmallocs were converted to kvmallocs. I am pretty sure I
> tried replacing kvmallocs with vmallocs at some point to see if something
> breaks. There was only one allocation (kvm_arch_init) that actually
> complained (BUG_ON triggerd). Having said that, maybe there were allocations
> that I somehow did not test.

A straight replacement with vmalloc might not work as it might allocate contigous memory that happens to work. You'd have to call a vmalloc which deliberately reordered the pages...
Comment 48 Juro Bystricky 2017-12-11 17:32:15 UTC
(In reply to comment #47)
> (In reply to comment #45)
> > (In reply to comment #44)
> > > I'm finding:
> > > 
> > > https://elixir.free-electrons.com/linux/latest/source/arch/x86/kvm/lapic.
> > > c#L177
> > > 
> > > an interesting piece of code.
> > > 
> > > Its a kvzalloc() call so would use kalloc, falling back to vmalloc so it may
> > > or may not be contiguous. If it fails, as far as I can tell the system is in
> > > trouble...
> > 
> > This is by (relatively new) design, so you can work in fragmented memory.
> > Basically, most of kmallocs were converted to kvmallocs. I am pretty sure I
> > tried replacing kvmallocs with vmallocs at some point to see if something
> > breaks. There was only one allocation (kvm_arch_init) that actually
> > complained (BUG_ON triggerd). Having said that, maybe there were allocations
> > that I somehow did not test.
> 
> A straight replacement with vmalloc might not work as it might allocate
> contigous memory that happens to work. You'd have to call a vmalloc which
> deliberately reordered the pages...

Good point. At some point I asked the KVM maintainers if there is a need for contiguous memory, the answer was not really.  But, this can be easily verified by 
a. Replacing kvmallocs with kmallocs. We could get memory allocatione errors but not crashes. 
b. Adding printouts to kvmalloc when "switching" to to vmalloc.

My main problem is I cannot reproduce the problem.
Comment 49 Randy MacLeod 2017-12-11 18:39:42 UTC
Juro, Have you tried manually filling up the page cache:
  https://serverfault.com/questions/534770/how-to-fill-up-linux-cache

That may not be sufficient.
In my previous job, I had a somewhat similar problem and I found a useful tool to fragment all memory so that there was only page-sized fragments available and all larger alocations would fail. It's a kernel module so that we didn't have to wait days to be able to reproduce the problem. The code is still online but it's for 2.4 only! Perhaps the idea is useful and the code may be a good starting point:
  http://opensource.sys-con.com/node/284266
  https://oss.oracle.com/projects/codefragments/src/trunk/fragment-slab/README

I tried a bunch of simple core-image-minimal boots of qemu on Fedora 27  and they all worked. I am trying to get the testimage to build now... in my spare time.
Comment 50 Juro Bystricky 2017-12-11 19:12:51 UTC
(In reply to comment #49)
> Juro, Have you tried manually filling up the page cache:
>   https://serverfault.com/questions/534770/how-to-fill-up-linux-cache
> 
> That may not be sufficient.
> In my previous job, I had a somewhat similar problem and I found a useful
> tool to fragment all memory so that there was only page-sized fragments
> available and all larger alocations would fail. It's a kernel module so that
> we didn't have to wait days to be able to reproduce the problem. The code is
> still online but it's for 2.4 only! Perhaps the idea is useful and the code
> may be a good starting point:
>   http://opensource.sys-con.com/node/284266
>  
> https://oss.oracle.com/projects/codefragments/src/trunk/fragment-slab/README
> 
> I tried a bunch of simple core-image-minimal boots of qemu on Fedora 27  and
> they all worked. I am trying to get the testimage to build now... in my
> spare time.

A kernel module that fragments memory quickly would be ** very ** useful.
I'll check the links you provided.
Comment 51 Richard Purdie 2017-12-13 09:55:03 UTC
Some further details:

I was able to clean up that fragmentation module and get it building with 4.13-4.15 kernels.

With the module I was able to reproduce qemu hangs on my local machine. The BIOS bug shows up some of the time but not always.

I tried a number of tweaks with my 4.13.12 kernel including:
* only using kzalloc (no vzalloc in kvm)
* forcing different sections of memory to different NUMA nodes
* forcing use of vzalloc within kvm

Only fragmentation appears to cause the problem.

I was able to reproduce the problem with 4.14.0 and 4.14.5.

I was not able to reproduce the problem with 4.15-rc3.

I'm therefore bisecting between 4.14.0 and 4.15-rc3 to see if I can find out which kernel change fixes this.
Comment 52 Richard Purdie 2017-12-13 12:17:34 UTC
I believe we have a winning commit: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b1394e745b9453dcb5b0671c205b770e87dedb87

Found by inspection before I reached it with bisect. Confirmed that applying it to 4.14.0 fixes the hangs which otherwise occurred.

Now the question is how to fix the fedora and tumbleweed builders...
Comment 53 Richard Purdie 2017-12-13 12:30:58 UTC
As a quick fix for the autobuilders I'm going to propose we enable x2apic in qemux86-64 which will mean it accesses the apic via msrs and avoids the issue...
Comment 54 Randy MacLeod 2017-12-14 16:34:39 UTC
For anyone reading along, Richard has added some debugging notes, including a link to a minimally working memory-fragmenting kernel module, here:
https://wiki.yoctoproject.org/wiki/TipsAndTricks/DebuggingHardQemuFailures