Linux Kernelのソースコードを読む - ソースコードの準備 -

# この記事を書くモチベーション
Linuxの勉強をしたくなったので、まずはLinux Kernelのソースコードリーディング環境を整えます。
# 環境
手元にあったLinuxを使います。AWSのAmazon Linuxです。
カーネルのバージョンは、4.9.119-44.140.amzn1.x86_64 でした。
# やったこと
カーネルソースのダウンロードと展開

```
$ rpm -qa | grep kernel
kernel-4.9.119-44.140.amzn1.x86_64
kernel-devel-4.9.93-41.60.amzn1.x86_64
kernel-4.9.76-3.78.amzn1.x86_64
kernel-tools-4.9.119-44.140.amzn1.x86_64
kernel-devel-4.9.119-44.140.amzn1.x86_64
kernel-4.9.93-41.60.amzn1.x86_64
kernel-headers-4.9.119-44.140.amzn1.x86_64
$
$ uname -a
Linux ip-172-21-0-185 4.9.119-44.140.amzn1.x86_64 #1 SMP Fri Aug 10 19:17:29 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
```
kernel-4.9.119-44.140.amzn1.x86_64 のソースをダウンロードすることにします。Amazon Linuxはyumが使えるので、yumdownloaderを使用します。

```
$ sudo yumdownloader --source kernel kernel-4.9.119-44.140.amzn1
```

ダウンロードした kernel-4.9.119-44.140.amzn1.src.rpm は、rpm2cpio でcpioにしてから展開、または、rpmコマンドでインストールします。今回はrpm2cpioにしました。

```
$ sudo sh -c "rpm2cpio kernel-4.9.119-44.140.amzn1.src.rpm | cpio -id"
```

展開したらこんな感じです。

```
$ ls
0001-kbuild-AFTER_LINK.patch                                     0078-x86-cpu-common-Provide-detect_ht_early.patch
0002-lib-cpumask-Make-CPUMASK_OFFSTACK-usable-without-deb.patch  0079-x86-cpu-topology-Provide-detect_extended_topology_ea.patch
0003-die-floppy-die.patch                                        0080-x86-cpu-intel-Evaluate-smp_num_siblings-early.patch
0004-no-pcspkr-modalias.patch                                    0081-x86-CPU-AMD-Do-not-check-CPUID-max-ext-level-before-.patch
0005-Kbuild-Add-an-option-to-enable-GCC-VTA.patch                0082-x86-cpu-AMD-Evaluate-smp_num_siblings-early.patch
0006-crash-driver.patch                                          0083-x86-apic-Ignore-secondary-threads-if-nosmt-force.patch
0007-watchdog-Disable-watchdog-on-virtual-machines.patch         0084-x86-speculation-l1tf-Extend-64bit-swap-file-size-lim.patch
0008-scsi-sd_revalidate_disk-prevent-NULL-ptr-deref.patch        0085-x86-cpufeatures-Add-detection-of-L1D-cache-flush-sup.patch
0009-bump-the-default-TTL-to-255.patch                           0086-x86-CPU-AMD-Move-TOPOEXT-reenablement-before-reading.patch
0010-bump-default-tcp_wmem-from-16KB-to-20KB.patch               0087-x86-speculation-l1tf-Protect-PAE-swap-entries-agains.patch
0011-force-perf-to-use-usr-bin-python-instead-of-usr-bin-.patch  0088-x86-speculation-l1tf-Fix-up-pte-pfn-conversion-for-P.patch
0012-drivers-introduce-AMAZON_DRIVER_UPDATES.patch               0089-Revert-x86-apic-Ignore-secondary-threads-if-nosmt-fo.patch
0013-drivers-amazon-add-network-device-drivers-support.patch     0090-cpu-hotplug-Boot-HT-siblings-at-least-once.patch
0014-drivers-amazon-introduce-AMAZON_IXGBEVF.patch               0091-x86-KVM-Warn-user-if-KVM-is-loaded-SMT-and-L1TF-CPU-.patch
0015-drivers-amazon-import-out-of-tree-ixgbevf-4.0.3.patch       0092-x86-KVM-VMX-Add-module-argument-for-L1TF-mitigation.patch
0016-drivers-amazon-ixgbevf-update-Makefile.patch                0093-x86-KVM-VMX-Add-L1D-flush-algorithm.patch
0017-drivers-amazon-introduce-AMAZON_ENA_ETHERNET.patch          0094-x86-KVM-VMX-Add-L1D-MSR-based-flush.patch
0018-drivers-amazon-import-out-of-tree-ENA-driver-1.1.3.patch    0095-x86-KVM-VMX-Add-L1D-flush-logic.patch
0019-drivers-amazon-ena-update-Makefile.patch                    0096-kvm-nVMX-Update-MSR-load-counts-on-a-VMCS-switch.patch
0020-drivers-amazon-add-block-device-drivers-support.patch       0097-x86-KVM-VMX-Split-the-VMX-MSR-LOAD-structures-to-hav.patch
0021-drivers-amazon-introduce-AMAZON_XEN_BLKDEV_FRONTEND.patch   0098-x86-KVM-VMX-Add-find_msr-helper-function.patch
0022-drivers-amazon-import-xen-blkfront-from-the-tree.patch      0099-x86-KVM-VMX-Separate-the-VMX-AUTOLOAD-guest-host-num.patch
0023-drivers-amazon-xen-blkfront-add-persistent_grants-pa.patch  0100-x86-KVM-VMX-Extend-add_atomic_switch_msr-to-allow-VM.patch
0024-drivers-amazon-xen-blkfront-resurrect-request-based-.patch  0101-x86-KVM-VMX-Use-MSR-save-list-for-IA32_FLUSH_CMD-if-.patch
0025-xen-pvhvm-unplug-block-deivces-driven-by-out-of-tree.patch  0102-cpu-hotplug-Online-siblings-when-SMT-control-is-turn.patch
0026-drivers-amazon-introduce-AMAZON_GPU_MODULES.patch           0103-x86-litf-Introduce-vmx-status-variable.patch
0027-drivers-amazon-xen-blkfront-add-uevent-for-size-chan.patch  0104-x86-kvm-Drop-L1TF-MSR-list-approach.patch
0028-block-relax-check-on-sg-gap.patch                           0105-x86-l1tf-Handle-EPT-disabled-state-proper.patch
0029-block-fix-bio_will_gap-for-first-bvec-with-offset.patch     0106-x86-kvm-Move-l1tf-setup-function.patch
0030-drivers-amazon-ena-update-to-1.2.0.patch                    0107-x86-kvm-Add-static-key-for-flush-always.patch
0031-drivers-amazon-xen-blkfront-introduce-macro-to-check.patch  0108-x86-kvm-Serialize-L1D-flush-parameter-setter.patch
0032-drivers-amazon-xen-blkfront-convert-to-use-blkfront_.patch  0109-x86-kvm-Allow-runtime-control-of-L1D-flush.patch
0033-drivers-amazon-xen-blkfront-resurrect-per-device-loc.patch  0110-cpu-hotplug-Expose-SMT-control-init-function.patch
0034-drivers-amazon-xen-blkfront-empty-the-request-queue-.patch  0111-cpu-hotplug-Set-CPU_SMT_NOT_SUPPORTED-early.patch
0035-drivers-amazon-xen-blkfront-use-a-right-index-when-c.patch  0112-x86-bugs-kvm-Introduce-boot-time-control-of-L1TF-mit.patch
0036-xen-manage-keep-track-of-the-on-going-suspend-mode.patch    0113-Documentation-Add-section-about-CPU-vulnerabilities.patch
0037-xen-manage-introduce-helper-function-to-know-the-on-.patch  0114-x86-KVM-VMX-Initialize-the-vmx_l1d_flush_pages-conte.patch
0038-xenbus-add-freeze-thaw-restore-callbacks-support.patch      0115-Documentation-l1tf-Fix-typos.patch
0039-x86-xen-decouple-shared_info-mapping-from-xen_hvm_in.patch  0116-cpu-hotplug-detect-SMT-disabled-by-BIOS.patch
0040-x86-xen-add-system-core-suspend-and-resume-callbacks.patch  0117-x86-KVM-VMX-Don-t-set-l1tf_flush_l1d-to-true-from-vm.patch
0041-drivers-amazon-xen-blkfront-add-callbacks-for-PM-sus.patch  0118-x86-KVM-VMX-Replace-vmx_l1d_flush_always-with-vmx_l1.patch
0042-xen-netfront-add-callbacks-for-PM-suspend-and-hibern.patch  0119-x86-KVM-VMX-Move-the-l1tf_flush_l1d-test-to-vmx_l1d_.patch
0043-drivers-amazon-ixgbevf-use-pci-drvdata-correctly-in-.patch  0120-x86-irq-Demote-irq_cpustat_t-__softirq_pending-to-u1.patch
0044-xen-time-introduce-xen_-save-restore-_steal_clock.patch     0121-x86-KVM-VMX-Introduce-per-host-cpu-analogue-of-l1tf_.patch
0045-x86-xen-save-and-restore-steal-clock.patch                  0122-x86-Don-t-include-linux-irq.h-from-asm-hardirq.h.patch
0046-xen-events-add-xen_shutdown_pirqs-helper-function.patch     0123-x86-irq-Let-interrupt-handlers-set-kvm_cpu_l1tf_flus.patch
0047-x86-xen-close-event-channels-for-PIRQs-in-system-cor.patch  0124-x86-KVM-VMX-Don-t-set-l1tf_flush_l1d-from-vmx_handle.patch
0048-drivers-amazon-ena-update-to-1.3.0.patch                    0125-Documentation-l1tf-Remove-Yonah-processors-from-not-.patch
0049-nvme-update-timeout-module-parameter-type.patch             0126-KVM-x86-Add-a-framework-for-supporting-MSR-based-fea.patch
0050-drivers-amazon-xen-blkfront-ensure-no-reqs-rsps-in-r.patch  0127-KVM-SVM-Add-MSR-based-feature-support-for-serializin.patch
0051-xen-netfront-add-longer-default-freeze-timeout-as-a-.patch  0128-KVM-X86-Introduce-kvm_get_msr_feature.patch
0052-drivers-amazon-ena-update-to-1.4.0.patch                    0129-KVM-X86-Allow-userspace-to-define-the-microcode-vers.patch
0053-PM-hibernate-update-the-resume-offset-on-SNAPSHOT_SE.patch  0130-KVM-VMX-support-MSR_IA32_ARCH_CAPABILITIES-as-a-feat.patch
0054-Not-for-upstream-PM-hibernate-Speed-up-hibernation-b.patch  0131-x86-speculation-Simplify-sysfs-report-of-VMX-L1TF-vu.patch
0055-ACPI-SPCR-Make-SPCR-available-to-x86.patch                  0132-x86-speculation-Use-ARCH_CAPABILITIES-to-skip-L1D-fl.patch
0056-locking-paravirt-Use-new-static-key-for-controlling-.patch  0133-KVM-VMX-Tell-the-nested-hypervisor-to-skip-L1D-flush.patch
0057-KVM-Introduce-paravirtualization-hints-and-KVM_HINTS.patch  0134-cpu-hotplug-Fix-SMT-supported-evaluation.patch
0058-KVM-X86-Choose-qspinlock-when-dedicated-physical-CPU.patch  0135-x86-speculation-l1tf-Invert-all-not-present-mappings.patch
0059-x86-paravirt-Set-up-the-virt_spin_lock_key-after-sta.patch  0136-x86-speculation-l1tf-Make-pmd-pud_mknotpresent-inver.patch
0060-KVM-X86-Fix-setup-the-virt_spin_lock_key-before-stat.patch  0137-x86-mm-pat-Make-set_memory_np-L1TF-safe.patch
0061-x86-speculation-l1tf-Increase-32bit-PAE-__PHYSICAL_P.patch  0138-x86-mm-kmmio-Make-the-tracer-robust-against-L1TF.patch
0062-mm-x86-move-_PAGE_SWP_SOFT_DIRTY-from-bit-7-to-bit-1.patch  0139-tools-headers-Synchronise-x86-cpufeatures.h-for-L1TF.patch
0063-x86-speculation-l1tf-Change-order-of-offset-type-in-.patch  config-generic
0064-x86-speculation-l1tf-Protect-swap-entries-against-L1.patch  config-x86_32-generic
0065-x86-speculation-l1tf-Protect-PROT_NONE-PTEs-against-.patch  config-x86_64-generic
0066-x86-speculation-l1tf-Make-sure-the-first-page-is-alw.patch  cpupower.config
0067-x86-speculation-l1tf-Add-sysfs-reporting-for-l1tf.patch     cpupower.init
0068-x86-speculation-l1tf-Disallow-non-privileged-high-MM.patch  kconfig.py
0069-x86-speculation-l1tf-Limit-swap-file-size-to-MAX_PA-.patch  kernel.spec
0070-x86-bugs-Move-the-l1tf-function-and-define-pr_fmt-pr.patch  linux-4.9.119
0071-x86-smp-Provide-topology_is_primary_thread.patch            linux-4.9.119-patches.tar
0072-x86-topology-Provide-topology_smt_supported.patch           linux-4.9.119.tar
0073-cpu-hotplug-Make-bringup-teardown-of-smp-threads-sym.patch  Makefile.config
0074-cpu-hotplug-Split-do_cpu_down.patch                         mod-extra.list
0075-cpu-hotplug-Provide-knobs-to-control-SMT.patch              mod-extra.sh
0076-x86-cpu-Remove-the-pointless-CPU-printout.patch             mod-extra-sign.sh
0077-x86-cpu-AMD-Remove-the-pointless-detect_ht-call.patch       x509.genkey
```