
This merges up to the 5.10.209 LTS release into the android12-5.10 branch. included in here are the following commits: *f5f23fd864
ANDROID: GKI: db845c: Update symbols list and ABI *717f23122d
Merge "Merge branch 'android12-5.10' into branch 'android12-5.10-lts'" into android12-5.10-lts |\ | *10896fff34
Merge branch 'android12-5.10' into branch 'android12-5.10-lts' * |6f5d6d6de5
UPSTREAM: drm/msm/dsi: Enable runtime PM * |5a2e61dcbd
UPSTREAM: PM: runtime: Have devm_pm_runtime_enable() handle pm_runtime_dont_use_autosuspend() * |cb06375abb
UPSTREAM: PM: runtime: add devm_pm_runtime_enable helper |/ *7edcfba384
Revert "clk: fixed-rate: add devm_clk_hw_register_fixed_rate" *a8a9ceb11a
Revert "clk: fixed-rate: fix clk_hw_register_fixed_rate_with_accuracy_parent_hw" *7e6944b050
Merge 5.10.209 into android12-5.10-lts |\ | *16ad71c250
Linux 5.10.209 | *66e4f4a847
arm64: dts: armada-3720-turris-mox: set irq type for RTC | *2e23761beb
i2c: s3c24xx: fix transferring more than one message in polling mode | *da60686bab
i2c: s3c24xx: fix read transfers in polling mode | *8ba74e9016
selftests: mlxsw: qos_pfc: Adjust the test to support 8 lanes | *3e1ca80659
selftests: mlxsw: qos_pfc: Convert to iproute2 dcb | *56750ea5d1
mlxsw: spectrum_acl_tcam: Fix stack corruption | *a9b233b287
mlxsw: spectrum_acl_tcam: Reorder functions to avoid forward declarations | *d3669ebc4c
mlxsw: spectrum_acl_tcam: Make fini symmetric to init | *a557dbda64
mlxsw: spectrum_acl_tcam: Add missing mutex_destroy() | *936d06d699
mlxsw: spectrum: Use 'bitmap_zalloc()' when applicable | *e8bfdf6301
mlxsw: spectrum_acl_erp: Fix error flow of pool allocation failure | *f90b1cebc4
ethtool: netlink: Add missing ethnl_ops_begin/complete | *b44e1aec80
kdb: Fix a potential buffer overflow in kdb_local() | *977c2cf563
ipvs: avoid stat macros calls from preemptible context | *00a86f81c8
netfilter: nf_tables: reject NFT_SET_CONCAT with not field length description | *9a4d25267d
netfilter: nf_tables: skip dead set elements in netlink dump | *2d4c0798a1
netfilter: nf_tables: do not allow mismatch field size and set key length | *b099b495e1
net: dsa: vsc73xx: Add null pointer check to vsc73xx_gpio_probe | *6c8a5bbca6
net: ravb: Fix dma_addr_t truncation in error case | *ba77c8b4e1
net: phy: micrel: populate .soft_reset for KSZ9131 | *1658d7a7a2
net: ethernet: ti: am65-cpsw: Fix max mtu to fit ethernet frames | *2295c22348
net: qualcomm: rmnet: fix global oob in rmnet_policy | *b55808b96e
s390/pci: fix max size calculation in zpci_memcpy_toio() | *178b437344
PCI: keystone: Fix race condition when initializing PHYs | *0de2e62067
nvmet-tcp: Fix the H2C expected PDU len calculation | *172276460a
serial: imx: Correct clock error message in function probe() | *5ff00408e5
apparmor: avoid crash when parsed profile name is empty | *866d32bff0
perf env: Avoid recursively taking env->bpf_progs.lock | *39669fae69
nvmet-tcp: fix a crash in nvmet_req_complete() | *f775f2621c
nvmet-tcp: Fix a kernel panic when host sends an invalid H2C PDU length | *1550b870ae
usb: cdc-acm: return correct error code on unsupported break | *d7c74b010a
tty: use 'if' in send_break() instead of 'goto' | *3483ca0390
tty: don't check for signal_pending() in send_break() | *2cf81d3440
tty: early return from send_break() on TTY_DRIVER_HARDWARE_BREAK | *4e76dbd7c3
tty: change tty_write_lock()'s ndelay parameter to bool | *abcc25a237
perf genelf: Set ELF program header addresses properly | *1e17feb4ad
iio: adc: ad9467: fix scale setting | *a3167e5ab5
iio: adc: ad9467: don't ignore error codes | *00e916d998
iio: adc: ad9467: fix reset gpio handling | *8083d68401
iio: adc: ad9467: Benefit from devm_clk_get_enabled() to simplify | *6e04a9d305
serial: imx: fix tx statemachine deadlock | *6746f3e893
software node: Let args be NULL in software_node_get_reference_args | *381bea33a8
acpi: property: Let args be NULL in __acpi_node_get_property_reference | *7166e8e913
libapi: Add missing linux/types.h header to get the __u64 type on io.h | *bc57f3ef8a
serial: 8250: omap: Don't skip resource freeing if pm_runtime_resume_and_get() failed | *89b97e6b19
power: supply: cw2015: correct time_to_empty units in sysfs | *5e3995ec39
MIPS: Alchemy: Fix an out-of-bound access in db1550_dev_setup() | *c3ed63f6a1
MIPS: Alchemy: Fix an out-of-bound access in db1200_dev_setup() | *9995dee8b9
mips: Fix incorrect max_low_pfn adjustment | *5cdda6239c
mips: dmi: Fix early remap on MIPS32 | *b152868773
leds: aw2013: Select missing dependency REGMAP_I2C | *927626a207
mfd: syscon: Fix null pointer dereference in of_syscon_register() | *c964a0597b
HID: wacom: Correct behavior when processing some confidence == false touches | *c7883c9f83
iio: adc: ad7091r: Pass iio_dev to event handler | *ba7be66674
KVM: arm64: vgic-its: Avoid potential UAF in LPI translation cache | *9b5a278594
KVM: arm64: vgic-v4: Restore pending state on host userspace write | *7521ea8f62
x86/kvm: Do not try to disable kvmclock if it was not enabled | *322c5fe40f
wifi: mwifiex: configure BSSID consistently when starting AP | *ba8a4fba62
wifi: rtlwifi: Convert LNKCTL change to PCIe cap RMW accessors | *3dc3122b0b
wifi: rtlwifi: Remove bogus and dangerous ASPM disable/enable code | *5e9142b6a2
iommu/arm-smmu-qcom: Add missing GMU entry to match table | *394c6c0b6d
Bluetooth: Fix atomicity violation in {min,max}_key_size_set | *3b8d7a1b85
rootfs: Fix support for rootfstype= when root= is given | *8c0b563e9b
io_uring/rw: ensure io->bytes_done is always initialized | *daa61bacd3
pwm: jz4740: Don't use dev_err_probe() in .request() | *a7d4ec5672
fbdev: flush deferred work in fb_deferred_io_fsync() | *ff2d54d9cc
ALSA: hda/relatek: Enable Mute LED on HP Laptop 15s-fq2xxx | *b5c2a32621
ALSA: oxygen: Fix right channel of capture volume mixer | *57f34589e9
serial: imx: Ensure that imx_uart_rs485_config() is called with enabled clock | *b7747ca58e
usb: mon: Fix atomicity violation in mon_bin_vma_fault | *5e5ab50eff
usb: typec: class: fix typec_altmode_put_partner to put plugs | *baaa6d8e3f
Revert "usb: typec: class: fix typec_altmode_put_partner to put plugs" | *f5c09e1d6e
usb: chipidea: wait controller resume finished for wakeup irq | *084de4c659
Revert "usb: dwc3: don't reset device side if dwc3 was configured as host-only" | *1af0423587
Revert "usb: dwc3: Soft reset phy on probe for host" | *3253888660
usb: dwc: ep0: Update request status in dwc3_ep0_stall_restart | *e70b17282a
usb: phy: mxs: remove CONFIG_USB_OTG condition for mxs_phy_is_otg_host() | *d65cade544
tick-sched: Fix idle and iowait sleeptime accounting vs CPU hotplug | *7e7a0d8654
binder: fix race between mmput() and do_exit() | *cce8ba6fa4
xen-netback: don't produce zero-size SKB frags | *d1e68ad279
net: ethernet: mtk_eth_soc: remove duplicate if statements | *8a29463915
kprobes: Fix to handle forcibly unoptimized kprobes on freeing_list | *c02cdc2c5e
Revert "ASoC: atmel: Remove system clock tree configuration for at91sam9g20ek" | *79026a2d0a
virtio-crypto: fix memory leak in virtio_crypto_alg_skcipher_close_session() | *dfdb686d1b
virtio-crypto: fix memory-leak | *c3b3389ad0
dma-mapping: Fix build error unused-value | *0dfe57f1f2
Input: atkbd - use ab83 as id when skipping the getid command | *c8c1158ffb
binder: fix use-after-free in shinker's callback | *abbb909287
binder: fix unused alloc->free_async_space | *fa62c9050e
binder: fix async space check for 0-sized buffers | *4533e7623e
keys, dns: Fix size check of V1 server-list header | *9e8a31c1aa
of: unittest: Fix of_count_phandle_with_args() expected value message | *d5f490343c
of: Fix double free in of_parse_phandle_with_args_map | *c9ee325bae
IB/iser: Prevent invalidating wrong MR | *bedecbb5a5
mmc: sdhci_omap: Fix TI SoC dependencies | *613af7d576
mmc: sdhci_am654: Fix TI SoC dependencies | *8da3a51f74
pwm: stm32: Fix enable count for clk in .probe() | *e33e1bdbe6
pwm: stm32: Use hweight32 in stm32_pwm_detect_channels | *5af4ce2a54
pwm: stm32: Use regmap_clear_bits and regmap_set_bits where applicable | *bed5ec2a59
clk: fixed-rate: fix clk_hw_register_fixed_rate_with_accuracy_parent_hw | *764b78b927
clk: fixed-rate: add devm_clk_hw_register_fixed_rate | *e5236e58cc
clk: si5341: fix an error code problem in si5341_output_clk_set_rate | *428381fbcb
watchdog: rti_wdt: Drop runtime pm reference count when watchdog is unused | *d83662bb90
watchdog: bcm2835_wdt: Fix WDIOC_SETTIMEOUT handling | *6c93290da9
watchdog/hpwdt: Only claim UNKNOWN NMI if from iLO | *cbc15095d1
watchdog: set cdev owner before adding | *e4f4a2cb74
drivers: clk: zynqmp: update divider round rate logic | *af0b86199b
clk: zynqmp: Add a check for NULL pointer | *45b4ea38c9
clk: zynqmp: make bestdiv unsigned | *7fdcd873c0
drivers: clk: zynqmp: calculate closest mux rate | *6cccbfafc0
clk: qcom: videocc-sm8150: Add missing PLL config property | *84ee04572f
clk: qcom: videocc-sm8150: Update the videocc resets | *74bbdacf1b
dt-bindings: clock: Update the videocc resets for sm8150 | *0c8ba6937a
gpu/drm/radeon: fix two memleaks in radeon_vm_init | *520e213a0b
drivers/amd/pm: fix a use-after-free in kv_parse_power_table | *aeed2b4e4a
drm/amd/pm: fix a double-free in si_dpm_init | *d53fee9e34
drm/amdgpu/debugfs: fix error code when smc register accessors are NULL | *56a79c68b5
media: dvb-frontends: m88ds3103: Fix a memory leak in an error handling path of m88ds3103_probe() | *b5ebb9b4c1
media: dvbdev: drop refcount on error path in dvb_device_open() | *9284f409e4
f2fs: fix to update iostat correctly in f2fs_filemap_fault() | *40d36882c7
f2fs: fix to check compress file in f2fs_move_file_range() | *b864287581
media: rkisp1: Disable runtime PM in probe error path | *962b35733b
clk: qcom: gpucc-sm8150: Update the gpu_cc_pll1 config | *21a30b5969
media: cx231xx: fix a memleak in cx231xx_init_isoc | *abd50cebf9
drm/bridge: tc358767: Fix return value on error case | *3f6932b5ba
drm/radeon/trinity_dpm: fix a memleak in trinity_parse_power_table | *e646308eda
drm/radeon/dpm: fix a memleak in sumo_parse_power_table | *57ca798480
drm/radeon: check the alloc_workqueue return value in radeon_crtc_init() | *c20a6aa0a3
drm/drv: propagate errors from drm_modeset_register_all() | *da5e0feb12
drm/msm/dsi: Use pm_runtime_resume_and_get to prevent refcnt leaks | *b704eabe87
drm/msm/mdp4: flush vblank event on disable | *96f5856867
ASoC: cs35l34: Fix GPIO name and drop legacy include | *defe0327f5
ASoC: cs35l33: Fix GPIO name and drop legacy include | *1421b06618
drm/radeon: check return value of radeon_ring_lock() | *9924469212
drm/radeon/r100: Fix integer overflow issues in r100_cs_track_check() | *ca267f79a5
drm/radeon/r600_cs: Fix possible int overflows in r600_cs_check_reg() | *6f866885e1
f2fs: fix to avoid dirent corruption | *13ea8af957
drm/bridge: Fix typo in post_disable() description | *3233d8bf78
media: pvrusb2: fix use after free on context disconnection | *53926e2a39
drm/bridge: tpd12s015: Drop buggy __exit annotation for remove function | *1eb7ceae48
drm/nouveau/fence:: fix warning directly dereferencing a rcu pointer | *b1a07165be
rcu: Create an unrcu_pointer() to remove __rcu from a pointer | *1d1d5b90ea
drm/panel-elida-kd35t133: hold panel in reset for unprepare | *7794c14812
RDMA/usnic: Silence uninitialized symbol smatch warnings | *d807f4ef22
ARM: davinci: always select CONFIG_CPU_ARM926T | *da23bd709b
ip6_tunnel: fix NEXTHDR_FRAGMENT handling in ip6_tnl_parse_tlv_enc_lim() | *4e09df9b24
Bluetooth: btmtkuart: fix recv_buf() return value | *fd54d16613
Bluetooth: Fix bogus check for re-auth no supported with non-ssp | *15be96e1a6
netfilter: nf_tables: mark newset as dead on transaction abort | *6f39bea662
wifi: rtlwifi: rtl8192se: using calculate_bit_shift() | *c713826653
wifi: rtlwifi: rtl8192ee: using calculate_bit_shift() | *99f56c3f7c
wifi: rtlwifi: rtl8192de: using calculate_bit_shift() | *b75b68dde5
rtlwifi: rtl8192de: make arrays static const, makes object smaller | *df14e43219
wifi: rtlwifi: rtl8192ce: using calculate_bit_shift() | *016781c16e
wifi: rtlwifi: rtl8192cu: using calculate_bit_shift() | *bf277a76d3
wifi: rtlwifi: rtl8192c: using calculate_bit_shift() | *483c975e40
wifi: rtlwifi: rtl8188ee: phy: using calculate_bit_shift() | *31b651a7a1
wifi: rtlwifi: add calculate_bit_shift() | *64299791d0
dma-mapping: clear dev->dma_mem to NULL after freeing it | *a6dd109564
dma-mapping: Add dma_release_coherent_memory to DMA API | *ad43344ab4
virtio/vsock: fix logic which reduces credit update messages | *30ae0c6631
selftests/net: fix grep checking for fib_nexthop_multiprefix | *7a0f8295e7
scsi: hisi_sas: Replace with standard error code return value | *afea95d319
bpf: Fix verification of indirect var-off stack access | *419ab8f74a
arm64: dts: qcom: sdm845-db845c: correct LED panic indicator | *812cebdc5f
arm64: dts: qcom: qrb5165-rb5: correct LED panic indicator | *ba31bb08c1
scsi: fnic: Return error if vmalloc() failed | *2757f17972
bpf: fix check for attempt to corrupt spilled pointer | *7e98bbeb07
arm64: dts: ti: k3-am65-main: Fix DSS irq trigger type | *d49863ed28
wifi: rtlwifi: rtl8821ae: phy: fix an undefined bitwise shift behavior | *2799324d92
firmware: meson_sm: populate platform devices from sm device tree data | *3959dbb375
firmware: ti_sci: Fix an off-by-one in ti_sci_debugfs_create() | *01f9feb5ed
net/ncsi: Fix netlink major/minor version numbers | *7bcddd12a9
ncsi: internal.h: Fix a spello | *ae98b7f7bb
ARM: dts: qcom: apq8064: correct XOADC register address | *4675cacd6b
wifi: libertas: stop selecting wext | *24d8aef5d9
wifi: ath11k: Defer on rproc_get failure | *91f3111558
bpf: Add crosstask check to __bpf_get_stack | *d5d181df8d
bpf, lpm: Fix check prefixlen before walking trie | *eb0eac5736
wifi: rtw88: fix RX filter in FIF_ALLMULTI flag | *d8caf15ab1
NFSv4.1/pnfs: Ensure we handle the error NFS4ERR_RETURNCONFLICT | *722c700dd8
blocklayoutdriver: Fix reference leak of pnfs_device_node | *4518dc468c
crypto: scomp - fix req->dst buffer overflow | *1798c8fd00
crypto: sahara - do not resize req->src when doing hash operations | *67ae336f3b
crypto: sahara - fix processing hash requests with req->nbytes < sg->length | *b3287c8a7e
crypto: sahara - improve error handling in sahara_sha_process() | *98985edab6
crypto: sahara - fix wait_for_completion_timeout() error handling | *69451bf97c
crypto: sahara - fix ahash reqsize | *d8d9580831
crypto: sahara - handle zero-length aes requests | *b839648d05
crypto: sahara - avoid skcipher fallback code duplication | *14f57a013a
crypto: virtio - Wait for tasklet to complete on device remove | *5c28478af3
gfs2: Fix kernel NULL pointer dereference in gfs2_rgrp_dump | *57c7b331f0
gfs2: Also reflect single-block allocations in rgd->rd_extfail_pt | *22f63f9bc8
Revert "gfs2: Don't reject a supposedly full bitmap if we have blocks reserved" | *cd48d2a8e6
fs: indicate request originates from old mount API | *acd413da3e
pstore: ram_core: fix possible overflow in persistent_ram_init_ecc() | *c60fd7a663
crypto: sahara - fix error handling in sahara_hw_descriptor_create() | *25b7ca747b
crypto: sahara - fix processing requests with cryptlen < sg->length | *fc91d32c7d
crypto: sahara - fix ahash selftest failure | *4f4786b818
crypto: sahara - fix cbc selftest failure | *ccdb86c339
crypto: sahara - remove FLAGS_NEW_KEY logic | *7f807dc073
crypto: af_alg - Disallow multiple in-flight AIO requests | *97f9d0455b
crypto: ccp - fix memleak in ccp_init_dm_workarea | *95586bb74b
crypto: sa2ul - Return crypto_aead_setkey to transfer the error | *0eb69890e8
crypto: virtio - Handle dataq logic with tasklet | *0dee72f9b7
virtio-crypto: wait ctrl queue instead of busy polling | *4ee475e76b
virtio-crypto: use private buffer for control request | *7d386768ef
virtio-crypto: change code style | *1ff5742889
virtio-crypto: implement RSA algorithm | *f32dfee5a6
virtio-crypto: introduce akcipher service | *b2092cdcda
virtio_crypto: Introduce VIRTIO_CRYPTO_NOSPC | *1bc7a682ed
selinux: Fix error priority for bind with AF_UNSPEC on PF_INET6 socket | *001a3f59d8
mtd: Fix gluebi NULL pointer dereference caused by ftl notifier | *1d7b39c842
ACPI: extlog: Clear Extended Error Log status when RAS_CEC handled the error | *46e3dc02a4
spi: sh-msiof: Enforce fixed DTDL for R-Car H3 | *94c742324e
efivarfs: force RO when remounting if SetVariable is not supported | *44a88650ba
calipso: fix memory leak in netlbl_calipso_add_pass() | *5ac84b01a0
netlabel: remove unused parameter in netlbl_netlink_auditinfo() | *47210a5754
net: netlabel: Fix kerneldoc warnings | *d5ce66bdf6
cpufreq: scmi: process the result of devm_of_clk_add_hw_provider() | *fda1309205
cpufreq: Use of_property_present() for testing DT property presence | *ba7c7e3530
of: Add of_property_present() helper | *ded221bf4c
of: property: define of_property_read_u{8,16,32,64}_array() unconditionally | *f39c3d578c
ACPI: LPIT: Avoid u32 multiplication overflow | *c4e1a0ef0b
ACPI: video: check for error while searching for backlight device parent | *11ac297aba
mtd: rawnand: Increment IFC_TIMEOUT_MSECS for nand controller response | *f8df7c9886
spi: spi-zynqmp-gqspi: fix driver kconfig dependencies | *5a669f3511
powerpc/imc-pmu: Add a null pointer check in update_events_in_group() | *a67a04ad05
powerpc/powernv: Add a null pointer check in opal_powercap_init() | *e6ad05e3ae
powerpc/powernv: Add a null pointer check in opal_event_init() | *1eefa93faf
powerpc/powernv: Add a null pointer check to scom_debug_init_one() | *428ab6a9dd
selftests/powerpc: Fix error handling in FPU/VMX preemption tests | *b582aa1f66
powerpc/pseries/memhp: Fix access beyond end of drmem array | *f2ec41874b
powerpc/pseries/memhotplug: Quieten some DLPAR operations | *81dce186f1
powerpc/44x: select I2C for CURRITUCK | *d67339e9c8
powerpc: Remove in_kernel_text() | *ecbbd90e70
powerpc: add crtsavres.o to always-y instead of extra-y | *6aa7865ba7
EDAC/thunderx: Fix possible out-of-bounds string access | *36dbbfff28
x86/lib: Fix overflow when counting digits | *83da4fc5aa
coresight: etm4x: Fix width of CCITMIN field | *67d3d17e31
PCI: Add ACS quirk for more Zhaoxin Root Ports | *e5457b54ad
parport: parport_serial: Add Brainboxes device IDs and geometry | *937293ff64
parport: parport_serial: Add Brainboxes BAR details | *5e0be1229a
uio: Fix use-after-free in uio_open | *214aac202d
binder: fix comment on binder_alloc_new_buf() return value | *689f13128f
binder: fix trivial typo of binder_free_buf_locked() | *e18d60757b
binder: use EPOLLERR from eventpoll.h | *19d949b37e
ACPI: resource: Add another DMI match for the TongFang GMxXGxx | *f138fb6e64
drm/crtc: fix uninitialized variable use | *7ba78e0823
ARM: sun9i: smp: fix return code check of of_property_match_string | *01d8918415
net: qrtr: ns: Return 0 if server port is not present | *dbf8b0d938
ida: Fix crash in ida_free when the bitmap is empty | *d8a07ba130
i2c: rk3x: fix potential spinlock recursion on poll | *a57c59c85c
Input: xpad - add Razer Wolverine V2 support | *86e4e2eea4
ARC: fix spare error | *9700ff5a4f
s390/scm: fix virtual vs physical address confusion | *6e17155869
Input: i8042 - add nomux quirk for Acer P459-G2-M | *b0e82ef611
Input: atkbd - skip ATKBD_CMD_GETID in translated mode | *6f1614080f
reset: hisilicon: hi6220: fix Wvoid-pointer-to-enum-cast warning | *8d6913d050
ring-buffer: Do not record in NMI if the arch does not support cmpxchg in NMI | *439f3bbf75
tracing: Add size check when printing trace_marker output | *bc6619c9aa
tracing: Have large events show up as '[LINE TOO BIG]' instead of nothing | *c0f1db7380
jbd2: fix soft lockup in journal_finish_inode_data_buffers() | *cd94f81f77
neighbour: Don't let neigh_forced_gc() disable preemption for long | *3887ba7198
drm/crtc: Fix uninit-value bug in drm_mode_setcrtc | *956b740f3e
jbd2: correct the printing of write_flags in jbd2_write_superblock() | *656d684109
clk: rockchip: rk3128: Fix HCLK_OTG gate register | *05644e6365
drm/exynos: fix a wrong error checking | *0fc35b0d9f
drm/exynos: fix a potential error pointer dereference | *fb2f34d939
nvme: introduce helper function to get ctrl state | *a8b1ddeeac
ASoC: da7219: Support low DC impedance headset | *1059aa41c5
net/tg3: fix race condition in tg3_reset_task() | *44ad1b9eb3
nouveau/tu102: flush all pdbs on vmm flush | *ffe13302b8
ASoC: rt5650: add mutex to avoid the jack detection failure | *8b50b177bf
ASoC: cs43130: Fix incorrect frame delay configuration | *921ff9f2b1
ASoC: cs43130: Fix the position of const qualifier | *304529d564
ASoC: Intel: Skylake: mem leak in skl register function | *1e31b47b2e
ASoC: nau8822: Fix incorrect type in assignment and cast to restricted __be16 | *9c89777c7d
ASoC: Intel: Skylake: Fix mem leak in few functions | *c78083013b
ASoC: wm8974: Correct boost mixer inputs | *06a33eec1d
nvme-core: check for too small lba shift | *974f127fcf
drm/amdgpu: Fix cat debugfs amdgpu_regs_didt causes kernel null pointer | *52a33dbeac
debugfs: fix automount d_fsdata usage | *65bde47aad
mptcp: fix uninit-value in mptcp_incoming_options | *6b00598b64
ALSA: hda - Fix speaker and headset mic pin config for CHUWI CoreBook XPro | *94e192054f
pinctrl: lochnagar: Don't build on MIPS | *3e47740091
f2fs: explicitly null-terminate the xattr list * |62f62d810d
Revert "ipv6: remove max_size check inline with ipv4" * |08731a14db
Merge "Merge 5.10.208 into android12-5.10-lts" into android12-5.10-lts |\ \ | * |680475ab46
Merge 5.10.208 into android12-5.10-lts | |\| | | *3fee45ee55
Linux 5.10.208 | | *929ba86476
Revert "nvme: use command_id instead of req->tag in trace_nvme_complete_rq()" | | *8a10841c1a
PCI: Disable ATS for specific Intel IPU E2000 devices | | *b74a0c4ddf
PCI: Extract ATS disabling to a helper function | | *25d1e7be85
netfilter: nf_tables: Reject tables of unsupported family | | *03585b18b7
drm/qxl: fix UAF on handle creation | | *dd56c5790d
ipv6: remove max_size check inline with ipv4 | | *c6b2a6b827
net: tls, update curr on splice as well | | *d36b6b152f
powerpc: update ppc_save_regs to save current r1 in pt_regs | | *ae64985e0e
mmc: sdhci-sprd: Fix eMMC init failure after hw reset | | *f7796d76bd
mmc: core: Cancel delayed work before releasing host | | *c29da60e1f
mmc: rpmb: fixes pause retune on all RPMB partitions. | | *c82efcaad2
mmc: meson-mx-sdhc: Fix initialization frozen issue | | *73704c6b35
mm: fix unmap_mapping_range high bits shift bug | | *4aca0af447
i2c: core: Fix atomic xfer check for non-preempt config | | *0ba8c7ef19
x86/kprobes: fix incorrect return address calculation in kprobe_emulate_call_indirect | | *23c006a760
firewire: ohci: suppress unexpected system reboot in AMD Ryzen machines and ASM108x/VT630x PCIe cards | | *70168fdc74
mm/memory-failure: check the mapcount of the precise page | | *18203c4484
net: Implement missing SO_TIMESTAMPING_NEW cmsg support | | *701b03fc14
bnxt_en: Remove mis-applied code from bnxt_cfg_ntp_filters() | | *cfbf618e9a
asix: Add check for usbnet_get_endpoints | | *0fc5fe6e41
net/qla3xxx: fix potential memleak in ql_alloc_buffer_queues | | *c492f9c7d3
net/qla3xxx: switch from 'pci_' to 'dma_' API | | *53e92564c5
i40e: Restore VF MSI-X state during PCI reset | | *6d3465c3dd
ASoC: meson: g12a-tohdmitx: Fix event generation for S/PDIF mux | | *e08399e812
ASoC: meson: g12a-toacodec: Fix event generation | | *51e88b2cef
ASoC: meson: g12a-tohdmitx: Validate written enum values | | *85f8d007de
ASoC: meson: g12a-toacodec: Validate written enum values | | *21ecce2456
i40e: fix use-after-free in i40e_aqc_add_filters() | | *f8c03fd826
net: Save and restore msg_namelen in sock_sendmsg | | *06ce3b8ec4
netfilter: nft_immediate: drop chain reference counter on error | | *cf3c516dec
netfilter: nftables: add loop check helper function | | *4366b7e1f5
net: bcmgenet: Fix FCS generation for fragmented skbuffs | | *d1eb795385
sfc: fix a double-free bug in efx_probe_filters | | *cb69cad457
ARM: sun9i: smp: Fix array-index-out-of-bounds read in sunxi_mc_smp_init | | *8abb7ab7cf
net: sched: em_text: fix possible memory leak in em_text_destroy() | | *ecdfb0970c
i40e: Fix filter input checks to prevent config with invalid values | | *8d4ae760c7
drm/i915/dp: Fix passing the correct DPCD_REV for drm_dp_set_phy_test_pattern | | *3ff482518b
octeontx2-af: Fix marking couple of structure as __packed | | *6adeb15cb6
nfc: llcp_core: Hold a ref to llcp_local->dev when holding a ref to llcp_local | | *74c9135d16
ALSA: hda/realtek: Fix mute and mic-mute LEDs for HP ProBook 440 G6 | | *132ba71b4e
block: Don't invalidate pagecache for invalid falloc modes | | *ef1d1d7c10
keys, dns: Fix missing size check of V1 server-list header * | |274aa12c42
ANDROID: db845c: Enable device tree overlay support |/ / * |c925f18af7
Merge 5.10.207 into android12-5.10-lts |\| | *03a0e87f70
Linux 5.10.207 | *a7fd5c7ba4
scsi: core: Always send batch on reset or error handling command | *e30419672e
Revert "scsi: core: Add scsi_prot_ref_tag() helper" | *f60f60e1de
Revert "scsi: core: Introduce scsi_get_sector()" | *a5edb40702
Revert "scsi: core: Make scsi_get_lba() return the LBA" | *2129297760
Revert "scsi: core: Use scsi_cmd_to_rq() instead of scsi_cmnd.request" | *cea19678bf
Revert "scsi: core: Use a structure member to track the SCSI command submitter" | *6963d049a4
Revert "scsi: core: Always send batch on reset or error handling command" * |3d0828af52
Revert "ANDROID: GKI: Fix abi break in struct scsi_cmd" * |ffc061ba7d
ANDROID: GKI: Fix abi break in struct scsi_cmd * |8a9d593fd6
Merge 5.10.206 into android12-5.10-lts |/ *cf13ba74e8
Linux 5.10.206 *2df1e1887c
spi: atmel: Fix PDC transfer setup bug *2a0a658ed6
Bluetooth: SMP: Fix crash when receiving new connection when debug is enabled *ecd50f820d
Revert "MIPS: Loongson64: Enable DMA noncoherent support" *9175341bd8
dm-integrity: don't modify bio's immutable bio_vec in integrity_metadata() *73117ea033
netfilter: nf_tables: skip set commit for deleted/destroyed sets *8bf79dec73
tracing: Fix blocked reader of snapshot buffer *0afe420228
ring-buffer: Fix wake ups when buffer_percent is set to 100 *9db5239d75
scsi: core: Always send batch on reset or error handling command *f2d30198c0
scsi: core: Use a structure member to track the SCSI command submitter *df83ca8e98
scsi: core: Use scsi_cmd_to_rq() instead of scsi_cmnd.request *d054858a9c
scsi: core: Make scsi_get_lba() return the LBA *f230e6d424
scsi: core: Introduce scsi_get_sector() *294d66c35a
scsi: core: Add scsi_prot_ref_tag() helper *929f475eba
spi: atmel: Fix CS and initialization bug *23d9267c54
spi: atmel: Switch to transfer_one transfer method *db1b14eec8
Bluetooth: af_bluetooth: Fix Use-After-Free in bt_sock_recvmsg *0c54b79d1d
smb: client: fix OOB in smbCalcSize() *203a412e52
smb: client: fix OOB in SMB2_query_info_init() *79e158ddc3
usb: fotg210-hcd: delete an incorrect bounds test *da448f145f
Bluetooth: MGMT/SMP: Fix address type when using SMP over BREDR/LE *4bc912140b
Bluetooth: use inclusive language in SMP *e219c3110a
Bluetooth: SMP: Convert BT_ERR/BT_DBG to bt_dev_err/bt_dev_dbg *cdbc4a1115
ARM: dts: Fix occasional boot hang for am3 usb *1e2db0124c
9p/net: fix possible memory leak in p9_check_errors() *c4a22227f7
x86/alternatives: Sync core before enabling interrupts *7d407ef183
lib/vsprintf: Fix %pfwf when current node refcount == 0 *565fadc3ea
bus: ti-sysc: Flush posted write only after srst_udelay *e50cfb5447
tracing / synthetic: Disable events after testing in synth_event_gen_test_init() *cd6e41593e
dt-bindings: nvmem: mxs-ocotp: Document fsl,ocotp *786788bb13
net: ks8851: Fix TX stall caused by TX buffer overrun *391c1019a0
net: rfkill: gpio: set GPIO direction *6d7b8e5a6d
net: 9p: avoid freeing uninit memory in p9pdu_vreadf *45b63f09ba
Input: soc_button_array - add mapping for airplane mode button *2aa744ad0e
Bluetooth: L2CAP: Send reject on command corrupted request *25a6fdd26d
Bluetooth: hci_event: Fix not checking if HCI_OP_INQUIRY has been sent *71e1c76540
USB: serial: option: add Quectel RM500Q R13 firmware support *d521896bcc
USB: serial: option: add Foxconn T99W265 with new baseline *d0cf8a4bee
USB: serial: option: add Quectel EG912Y module support *f41f44cea9
USB: serial: ftdi_sio: update Actisense PIDs constant names *20d84a1946
wifi: cfg80211: fix certs build to not depend on file order *7a0a5cbfea
wifi: cfg80211: Add my certificate *9dcf50da59
iio: adc: ti_am335x_adc: Fix return value check of tiadc_request_dma() *abbebddb19
iio: common: ms_sensors: ms_sensors_i2c: fix humidity conversion time table *c40db29812
scsi: bnx2fc: Fix skb double free in bnx2fc_rcv() *e3749f85fd
Input: ipaq-micro-keys - add error handling for devm_kmemdup *b5f67cea27
iio: imu: inv_mpu6050: fix an error code problem in inv_mpu6050_read_raw *505df1c0ab
interconnect: Treat xlate() returning NULL node as an error *cc5eec86a4
btrfs: do not allow non subvolume root targets for snapshot *bd267af18f
smb: client: fix NULL deref in asn1_ber_decoder() *41350e813a
ALSA: hda/hdmi: add force-connect quirk for NUC5CPYB *a4692c38cd
ALSA: hda/hdmi: Add quirk to force pin connectivity on NUC10 *e032ddb0e3
pinctrl: at91-pio4: use dedicated lock class for IRQ *0b85149a9d
i2c: aspeed: Handle the coalesced stop conditions with the start conditions. *3dce7a52b2
afs: Fix overwriting of result of DNS query *97be1e865e
keys, dns: Allow key types (eg. DNS) to be reclaimed immediately on expiry *9e0d18f946
net: check dev->gso_max_size in gso_features_check() *59dc16ce09
net: warn if gso_type isn't set for a GSO SKB *63ad66d484
afs: Fix dynamic root lookup DNS check *65d2c287fc
afs: Fix the dynamic root's d_delete to always delete unused dentries *a3218319ee
net: check vlan filter feature in vlan_vids_add_by_dev() and vlan_vids_del_by_dev() *12e5a4719c
net/rose: fix races in rose_kill_by_device() *be0988c9b0
ethernet: atheros: fix a memleak in atl1e_setup_ring_resources *8b6f8bfe3a
net: sched: ife: fix potential use-after-free *f245312e9f
net/mlx5e: Correct snprintf truncation handling for fw_version buffer used by representors *e8ba688a64
net/mlx5: Fix fw tracer first block check *fc4c53f8e9
net/mlx5e: Fix slab-out-of-bounds in mlx5_query_nic_vport_mac_list() *50aa92e699
Revert "net/mlx5e: fix double free of encap_header" *b851889e91
wifi: mac80211: mesh_plink: fix matches_local logic *717f08fb51
s390/vx: fix save/restore of fpu kernel context *c48219fad1
reset: Fix crash when freeing non-existent optional resets *c999682ce8
ARM: OMAP2+: Fix null pointer dereference and memory leak in omap_soc_device_init *dbc8edb80f
smb: client: fix OOB in smb2_query_reparse_point() *bc3c57493b
ksmbd: fix wrong name of SMB2_CREATE_ALLOCATION_SIZE Updates the .xml file to track the new symbol that is required: Leaf changes summary: 1 artifact changed Changed leaf types summary: 0 leaf type changed Removed/Changed/Added functions summary: 0 Removed, 0 Changed, 1 Added function Removed/Changed/Added variables summary: 0 Removed, 0 Changed, 0 Added variable 1 Added function: [A] 'function int devm_pm_runtime_enable(device*)' Change-Id: I68d1c499f716926e1e84a98895170b9d192019e8 Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
5968 lines
162 KiB
C
5968 lines
162 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* linux/mm/memory.c
|
|
*
|
|
* Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
|
|
*/
|
|
|
|
/*
|
|
* demand-loading started 01.12.91 - seems it is high on the list of
|
|
* things wanted, and it should be easy to implement. - Linus
|
|
*/
|
|
|
|
/*
|
|
* Ok, demand-loading was easy, shared pages a little bit tricker. Shared
|
|
* pages started 02.12.91, seems to work. - Linus.
|
|
*
|
|
* Tested sharing by executing about 30 /bin/sh: under the old kernel it
|
|
* would have taken more than the 6M I have free, but it worked well as
|
|
* far as I could see.
|
|
*
|
|
* Also corrected some "invalidate()"s - I wasn't doing enough of them.
|
|
*/
|
|
|
|
/*
|
|
* Real VM (paging to/from disk) started 18.12.91. Much more work and
|
|
* thought has to go into this. Oh, well..
|
|
* 19.12.91 - works, somewhat. Sometimes I get faults, don't know why.
|
|
* Found it. Everything seems to work now.
|
|
* 20.12.91 - Ok, making the swap-device changeable like the root.
|
|
*/
|
|
|
|
/*
|
|
* 05.04.94 - Multi-page memory management added for v1.1.
|
|
* Idea by Alex Bligh (alex@cconcepts.co.uk)
|
|
*
|
|
* 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG
|
|
* (Gerhard.Wichert@pdb.siemens.de)
|
|
*
|
|
* Aug/Sep 2004 Changed to four level page tables (Andi Kleen)
|
|
*/
|
|
|
|
#include <linux/kernel_stat.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/sched/mm.h>
|
|
#include <linux/sched/coredump.h>
|
|
#include <linux/sched/numa_balancing.h>
|
|
#include <linux/sched/task.h>
|
|
#include <linux/hugetlb.h>
|
|
#include <linux/mman.h>
|
|
#include <linux/swap.h>
|
|
#include <linux/highmem.h>
|
|
#include <linux/pagemap.h>
|
|
#include <linux/memremap.h>
|
|
#include <linux/ksm.h>
|
|
#include <linux/rmap.h>
|
|
#include <linux/export.h>
|
|
#include <linux/delayacct.h>
|
|
#include <linux/init.h>
|
|
#include <linux/pfn_t.h>
|
|
#include <linux/writeback.h>
|
|
#include <linux/memcontrol.h>
|
|
#include <linux/mmu_notifier.h>
|
|
#include <linux/swapops.h>
|
|
#include <linux/elf.h>
|
|
#include <linux/gfp.h>
|
|
#include <linux/migrate.h>
|
|
#include <linux/string.h>
|
|
#include <linux/debugfs.h>
|
|
#include <linux/userfaultfd_k.h>
|
|
#include <linux/dax.h>
|
|
#include <linux/oom.h>
|
|
#include <linux/numa.h>
|
|
#include <linux/perf_event.h>
|
|
#include <linux/ptrace.h>
|
|
#include <linux/vmalloc.h>
|
|
#include <trace/hooks/mm.h>
|
|
|
|
#include <trace/events/kmem.h>
|
|
|
|
#include <asm/io.h>
|
|
#include <asm/mmu_context.h>
|
|
#include <asm/pgalloc.h>
|
|
#include <linux/uaccess.h>
|
|
#include <asm/tlb.h>
|
|
#include <asm/tlbflush.h>
|
|
|
|
#include "pgalloc-track.h"
|
|
#include "internal.h"
|
|
#include <trace/hooks/mm.h>
|
|
|
|
#define CREATE_TRACE_POINTS
|
|
#include <trace/events/pagefault.h>
|
|
|
|
#if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST)
|
|
#warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
|
|
#endif
|
|
|
|
#ifndef CONFIG_NEED_MULTIPLE_NODES
|
|
/* use the per-pgdat data instead for discontigmem - mbligh */
|
|
unsigned long max_mapnr;
|
|
EXPORT_SYMBOL(max_mapnr);
|
|
|
|
struct page *mem_map;
|
|
EXPORT_SYMBOL(mem_map);
|
|
#endif
|
|
|
|
/*
|
|
* A number of key systems in x86 including ioremap() rely on the assumption
|
|
* that high_memory defines the upper bound on direct map memory, then end
|
|
* of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
|
|
* highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
|
|
* and ZONE_HIGHMEM.
|
|
*/
|
|
void *high_memory;
|
|
EXPORT_SYMBOL(high_memory);
|
|
|
|
/*
|
|
* Randomize the address space (stacks, mmaps, brk, etc.).
|
|
*
|
|
* ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
|
|
* as ancient (libc5 based) binaries can segfault. )
|
|
*/
|
|
int randomize_va_space __read_mostly =
|
|
#ifdef CONFIG_COMPAT_BRK
|
|
1;
|
|
#else
|
|
2;
|
|
#endif
|
|
|
|
#ifndef arch_faults_on_old_pte
|
|
static inline bool arch_faults_on_old_pte(void)
|
|
{
|
|
/*
|
|
* Those arches which don't have hw access flag feature need to
|
|
* implement their own helper. By default, "true" means pagefault
|
|
* will be hit on old pte.
|
|
*/
|
|
return true;
|
|
}
|
|
#endif
|
|
|
|
#ifndef arch_wants_old_prefaulted_pte
|
|
static inline bool arch_wants_old_prefaulted_pte(void)
|
|
{
|
|
/*
|
|
* Transitioning a PTE from 'old' to 'young' can be expensive on
|
|
* some architectures, even if it's performed in hardware. By
|
|
* default, "false" means prefaulted entries will be 'young'.
|
|
*/
|
|
return false;
|
|
}
|
|
#endif
|
|
|
|
static int __init disable_randmaps(char *s)
|
|
{
|
|
randomize_va_space = 0;
|
|
return 1;
|
|
}
|
|
__setup("norandmaps", disable_randmaps);
|
|
|
|
unsigned long zero_pfn __read_mostly;
|
|
EXPORT_SYMBOL(zero_pfn);
|
|
|
|
unsigned long highest_memmap_pfn __read_mostly;
|
|
|
|
/*
|
|
* CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
|
|
*/
|
|
static int __init init_zero_pfn(void)
|
|
{
|
|
zero_pfn = page_to_pfn(ZERO_PAGE(0));
|
|
return 0;
|
|
}
|
|
early_initcall(init_zero_pfn);
|
|
|
|
/*
|
|
* Only trace rss_stat when there is a 512kb cross over.
|
|
* Smaller changes may be lost unless every small change is
|
|
* crossing into or returning to a 512kb boundary.
|
|
*/
|
|
#define TRACE_MM_COUNTER_THRESHOLD 128
|
|
|
|
void mm_trace_rss_stat(struct mm_struct *mm, int member, long count,
|
|
long value)
|
|
{
|
|
long thresh_mask = ~(TRACE_MM_COUNTER_THRESHOLD - 1);
|
|
|
|
/* Threshold roll-over, trace it */
|
|
if ((count & thresh_mask) != ((count - value) & thresh_mask))
|
|
trace_rss_stat(mm, member, count);
|
|
}
|
|
EXPORT_SYMBOL_GPL(mm_trace_rss_stat);
|
|
|
|
#if defined(SPLIT_RSS_COUNTING)
|
|
|
|
void sync_mm_rss(struct mm_struct *mm)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < NR_MM_COUNTERS; i++) {
|
|
if (current->rss_stat.count[i]) {
|
|
add_mm_counter(mm, i, current->rss_stat.count[i]);
|
|
current->rss_stat.count[i] = 0;
|
|
}
|
|
}
|
|
current->rss_stat.events = 0;
|
|
}
|
|
|
|
static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
|
|
{
|
|
struct task_struct *task = current;
|
|
|
|
if (likely(task->mm == mm))
|
|
task->rss_stat.count[member] += val;
|
|
else
|
|
add_mm_counter(mm, member, val);
|
|
}
|
|
#define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
|
|
#define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
|
|
|
|
/* sync counter once per 64 page faults */
|
|
#define TASK_RSS_EVENTS_THRESH (64)
|
|
static void check_sync_rss_stat(struct task_struct *task)
|
|
{
|
|
if (unlikely(task != current))
|
|
return;
|
|
if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
|
|
sync_mm_rss(task->mm);
|
|
}
|
|
#else /* SPLIT_RSS_COUNTING */
|
|
|
|
#define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
|
|
#define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
|
|
|
|
static void check_sync_rss_stat(struct task_struct *task)
|
|
{
|
|
}
|
|
|
|
#endif /* SPLIT_RSS_COUNTING */
|
|
|
|
/*
|
|
* Note: this doesn't free the actual pages themselves. That
|
|
* has been handled earlier when unmapping all the memory regions.
|
|
*/
|
|
static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
|
|
unsigned long addr)
|
|
{
|
|
pgtable_t token = pmd_pgtable(*pmd);
|
|
#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
|
|
/*
|
|
* Ensure page table destruction is blocked if __pte_map_lock managed
|
|
* to take this lock. Without this barrier tlb_remove_table_rcu can
|
|
* destroy ptl after __pte_map_lock locked it and during unlock would
|
|
* cause a use-after-free.
|
|
*/
|
|
spinlock_t *ptl = pmd_lock(tlb->mm, pmd);
|
|
spin_unlock(ptl);
|
|
#endif
|
|
pmd_clear(pmd);
|
|
pte_free_tlb(tlb, token, addr);
|
|
mm_dec_nr_ptes(tlb->mm);
|
|
}
|
|
|
|
static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
|
|
unsigned long addr, unsigned long end,
|
|
unsigned long floor, unsigned long ceiling)
|
|
{
|
|
pmd_t *pmd;
|
|
unsigned long next;
|
|
unsigned long start;
|
|
|
|
start = addr;
|
|
pmd = pmd_offset(pud, addr);
|
|
do {
|
|
next = pmd_addr_end(addr, end);
|
|
if (pmd_none_or_clear_bad(pmd))
|
|
continue;
|
|
free_pte_range(tlb, pmd, addr);
|
|
} while (pmd++, addr = next, addr != end);
|
|
|
|
start &= PUD_MASK;
|
|
if (start < floor)
|
|
return;
|
|
if (ceiling) {
|
|
ceiling &= PUD_MASK;
|
|
if (!ceiling)
|
|
return;
|
|
}
|
|
if (end - 1 > ceiling - 1)
|
|
return;
|
|
|
|
pmd = pmd_offset(pud, start);
|
|
pud_clear(pud);
|
|
pmd_free_tlb(tlb, pmd, start);
|
|
mm_dec_nr_pmds(tlb->mm);
|
|
}
|
|
|
|
static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d,
|
|
unsigned long addr, unsigned long end,
|
|
unsigned long floor, unsigned long ceiling)
|
|
{
|
|
pud_t *pud;
|
|
unsigned long next;
|
|
unsigned long start;
|
|
|
|
start = addr;
|
|
pud = pud_offset(p4d, addr);
|
|
do {
|
|
next = pud_addr_end(addr, end);
|
|
if (pud_none_or_clear_bad(pud))
|
|
continue;
|
|
free_pmd_range(tlb, pud, addr, next, floor, ceiling);
|
|
} while (pud++, addr = next, addr != end);
|
|
|
|
start &= P4D_MASK;
|
|
if (start < floor)
|
|
return;
|
|
if (ceiling) {
|
|
ceiling &= P4D_MASK;
|
|
if (!ceiling)
|
|
return;
|
|
}
|
|
if (end - 1 > ceiling - 1)
|
|
return;
|
|
|
|
pud = pud_offset(p4d, start);
|
|
p4d_clear(p4d);
|
|
pud_free_tlb(tlb, pud, start);
|
|
mm_dec_nr_puds(tlb->mm);
|
|
}
|
|
|
|
static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd,
|
|
unsigned long addr, unsigned long end,
|
|
unsigned long floor, unsigned long ceiling)
|
|
{
|
|
p4d_t *p4d;
|
|
unsigned long next;
|
|
unsigned long start;
|
|
|
|
start = addr;
|
|
p4d = p4d_offset(pgd, addr);
|
|
do {
|
|
next = p4d_addr_end(addr, end);
|
|
if (p4d_none_or_clear_bad(p4d))
|
|
continue;
|
|
free_pud_range(tlb, p4d, addr, next, floor, ceiling);
|
|
} while (p4d++, addr = next, addr != end);
|
|
|
|
start &= PGDIR_MASK;
|
|
if (start < floor)
|
|
return;
|
|
if (ceiling) {
|
|
ceiling &= PGDIR_MASK;
|
|
if (!ceiling)
|
|
return;
|
|
}
|
|
if (end - 1 > ceiling - 1)
|
|
return;
|
|
|
|
p4d = p4d_offset(pgd, start);
|
|
pgd_clear(pgd);
|
|
p4d_free_tlb(tlb, p4d, start);
|
|
}
|
|
|
|
/*
|
|
* This function frees user-level page tables of a process.
|
|
*/
|
|
void free_pgd_range(struct mmu_gather *tlb,
|
|
unsigned long addr, unsigned long end,
|
|
unsigned long floor, unsigned long ceiling)
|
|
{
|
|
pgd_t *pgd;
|
|
unsigned long next;
|
|
|
|
/*
|
|
* The next few lines have given us lots of grief...
|
|
*
|
|
* Why are we testing PMD* at this top level? Because often
|
|
* there will be no work to do at all, and we'd prefer not to
|
|
* go all the way down to the bottom just to discover that.
|
|
*
|
|
* Why all these "- 1"s? Because 0 represents both the bottom
|
|
* of the address space and the top of it (using -1 for the
|
|
* top wouldn't help much: the masks would do the wrong thing).
|
|
* The rule is that addr 0 and floor 0 refer to the bottom of
|
|
* the address space, but end 0 and ceiling 0 refer to the top
|
|
* Comparisons need to use "end - 1" and "ceiling - 1" (though
|
|
* that end 0 case should be mythical).
|
|
*
|
|
* Wherever addr is brought up or ceiling brought down, we must
|
|
* be careful to reject "the opposite 0" before it confuses the
|
|
* subsequent tests. But what about where end is brought down
|
|
* by PMD_SIZE below? no, end can't go down to 0 there.
|
|
*
|
|
* Whereas we round start (addr) and ceiling down, by different
|
|
* masks at different levels, in order to test whether a table
|
|
* now has no other vmas using it, so can be freed, we don't
|
|
* bother to round floor or end up - the tests don't need that.
|
|
*/
|
|
|
|
addr &= PMD_MASK;
|
|
if (addr < floor) {
|
|
addr += PMD_SIZE;
|
|
if (!addr)
|
|
return;
|
|
}
|
|
if (ceiling) {
|
|
ceiling &= PMD_MASK;
|
|
if (!ceiling)
|
|
return;
|
|
}
|
|
if (end - 1 > ceiling - 1)
|
|
end -= PMD_SIZE;
|
|
if (addr > end - 1)
|
|
return;
|
|
/*
|
|
* We add page table cache pages with PAGE_SIZE,
|
|
* (see pte_free_tlb()), flush the tlb if we need
|
|
*/
|
|
tlb_change_page_size(tlb, PAGE_SIZE);
|
|
pgd = pgd_offset(tlb->mm, addr);
|
|
do {
|
|
next = pgd_addr_end(addr, end);
|
|
if (pgd_none_or_clear_bad(pgd))
|
|
continue;
|
|
free_p4d_range(tlb, pgd, addr, next, floor, ceiling);
|
|
} while (pgd++, addr = next, addr != end);
|
|
}
|
|
|
|
void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
|
|
unsigned long floor, unsigned long ceiling)
|
|
{
|
|
while (vma) {
|
|
struct vm_area_struct *next = vma->vm_next;
|
|
unsigned long addr = vma->vm_start;
|
|
|
|
/*
|
|
* Hide vma from rmap and truncate_pagecache before freeing
|
|
* pgtables
|
|
*/
|
|
vm_write_begin(vma);
|
|
unlink_anon_vmas(vma);
|
|
vm_write_end(vma);
|
|
unlink_file_vma(vma);
|
|
|
|
if (is_vm_hugetlb_page(vma)) {
|
|
hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
|
|
floor, next ? next->vm_start : ceiling);
|
|
} else {
|
|
/*
|
|
* Optimization: gather nearby vmas into one call down
|
|
*/
|
|
while (next && next->vm_start <= vma->vm_end + PMD_SIZE
|
|
&& !is_vm_hugetlb_page(next)) {
|
|
vma = next;
|
|
next = vma->vm_next;
|
|
vm_write_begin(vma);
|
|
unlink_anon_vmas(vma);
|
|
vm_write_end(vma);
|
|
unlink_file_vma(vma);
|
|
}
|
|
free_pgd_range(tlb, addr, vma->vm_end,
|
|
floor, next ? next->vm_start : ceiling);
|
|
}
|
|
vma = next;
|
|
}
|
|
}
|
|
|
|
int __pte_alloc(struct mm_struct *mm, pmd_t *pmd)
|
|
{
|
|
spinlock_t *ptl;
|
|
pgtable_t new = pte_alloc_one(mm);
|
|
if (!new)
|
|
return -ENOMEM;
|
|
|
|
/*
|
|
* Ensure all pte setup (eg. pte page lock and page clearing) are
|
|
* visible before the pte is made visible to other CPUs by being
|
|
* put into page tables.
|
|
*
|
|
* The other side of the story is the pointer chasing in the page
|
|
* table walking code (when walking the page table without locking;
|
|
* ie. most of the time). Fortunately, these data accesses consist
|
|
* of a chain of data-dependent loads, meaning most CPUs (alpha
|
|
* being the notable exception) will already guarantee loads are
|
|
* seen in-order. See the alpha page table accessors for the
|
|
* smp_rmb() barriers in page table walking code.
|
|
*/
|
|
smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
|
|
|
|
ptl = pmd_lock(mm, pmd);
|
|
if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
|
|
mm_inc_nr_ptes(mm);
|
|
pmd_populate(mm, pmd, new);
|
|
new = NULL;
|
|
}
|
|
spin_unlock(ptl);
|
|
if (new)
|
|
pte_free(mm, new);
|
|
return 0;
|
|
}
|
|
|
|
int __pte_alloc_kernel(pmd_t *pmd)
|
|
{
|
|
pte_t *new = pte_alloc_one_kernel(&init_mm);
|
|
if (!new)
|
|
return -ENOMEM;
|
|
|
|
smp_wmb(); /* See comment in __pte_alloc */
|
|
|
|
spin_lock(&init_mm.page_table_lock);
|
|
if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
|
|
pmd_populate_kernel(&init_mm, pmd, new);
|
|
new = NULL;
|
|
}
|
|
spin_unlock(&init_mm.page_table_lock);
|
|
if (new)
|
|
pte_free_kernel(&init_mm, new);
|
|
return 0;
|
|
}
|
|
|
|
static inline void init_rss_vec(int *rss)
|
|
{
|
|
memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
|
|
}
|
|
|
|
static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
|
|
{
|
|
int i;
|
|
|
|
if (current->mm == mm)
|
|
sync_mm_rss(mm);
|
|
for (i = 0; i < NR_MM_COUNTERS; i++)
|
|
if (rss[i])
|
|
add_mm_counter(mm, i, rss[i]);
|
|
}
|
|
|
|
/*
|
|
* This function is called to print an error when a bad pte
|
|
* is found. For example, we might have a PFN-mapped pte in
|
|
* a region that doesn't allow it.
|
|
*
|
|
* The calling function must still handle the error.
|
|
*/
|
|
static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
|
|
pte_t pte, struct page *page)
|
|
{
|
|
pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
|
|
p4d_t *p4d = p4d_offset(pgd, addr);
|
|
pud_t *pud = pud_offset(p4d, addr);
|
|
pmd_t *pmd = pmd_offset(pud, addr);
|
|
struct address_space *mapping;
|
|
pgoff_t index;
|
|
static unsigned long resume;
|
|
static unsigned long nr_shown;
|
|
static unsigned long nr_unshown;
|
|
|
|
/*
|
|
* Allow a burst of 60 reports, then keep quiet for that minute;
|
|
* or allow a steady drip of one report per second.
|
|
*/
|
|
if (nr_shown == 60) {
|
|
if (time_before(jiffies, resume)) {
|
|
nr_unshown++;
|
|
return;
|
|
}
|
|
if (nr_unshown) {
|
|
pr_alert("BUG: Bad page map: %lu messages suppressed\n",
|
|
nr_unshown);
|
|
nr_unshown = 0;
|
|
}
|
|
nr_shown = 0;
|
|
}
|
|
if (nr_shown++ == 0)
|
|
resume = jiffies + 60 * HZ;
|
|
|
|
mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
|
|
index = linear_page_index(vma, addr);
|
|
|
|
pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n",
|
|
current->comm,
|
|
(long long)pte_val(pte), (long long)pmd_val(*pmd));
|
|
if (page)
|
|
dump_page(page, "bad pte");
|
|
pr_alert("addr:%px vm_flags:%08lx anon_vma:%px mapping:%px index:%lx\n",
|
|
(void *)addr, READ_ONCE(vma->vm_flags), vma->anon_vma, mapping, index);
|
|
pr_alert("file:%pD fault:%ps mmap:%ps readpage:%ps\n",
|
|
vma->vm_file,
|
|
vma->vm_ops ? vma->vm_ops->fault : NULL,
|
|
vma->vm_file ? vma->vm_file->f_op->mmap : NULL,
|
|
mapping ? mapping->a_ops->readpage : NULL);
|
|
dump_stack();
|
|
add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
|
|
}
|
|
|
|
/*
|
|
* __vm_normal_page -- This function gets the "struct page" associated with
|
|
* a pte.
|
|
*
|
|
* "Special" mappings do not wish to be associated with a "struct page" (either
|
|
* it doesn't exist, or it exists but they don't want to touch it). In this
|
|
* case, NULL is returned here. "Normal" mappings do have a struct page.
|
|
*
|
|
* There are 2 broad cases. Firstly, an architecture may define a pte_special()
|
|
* pte bit, in which case this function is trivial. Secondly, an architecture
|
|
* may not have a spare pte bit, which requires a more complicated scheme,
|
|
* described below.
|
|
*
|
|
* A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
|
|
* special mapping (even if there are underlying and valid "struct pages").
|
|
* COWed pages of a VM_PFNMAP are always normal.
|
|
*
|
|
* The way we recognize COWed pages within VM_PFNMAP mappings is through the
|
|
* rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
|
|
* set, and the vm_pgoff will point to the first PFN mapped: thus every special
|
|
* mapping will always honor the rule
|
|
*
|
|
* pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
|
|
*
|
|
* And for normal mappings this is false.
|
|
*
|
|
* This restricts such mappings to be a linear translation from virtual address
|
|
* to pfn. To get around this restriction, we allow arbitrary mappings so long
|
|
* as the vma is not a COW mapping; in that case, we know that all ptes are
|
|
* special (because none can have been COWed).
|
|
*
|
|
*
|
|
* In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
|
|
*
|
|
* VM_MIXEDMAP mappings can likewise contain memory with or without "struct
|
|
* page" backing, however the difference is that _all_ pages with a struct
|
|
* page (that is, those where pfn_valid is true) are refcounted and considered
|
|
* normal pages by the VM. The disadvantage is that pages are refcounted
|
|
* (which can be slower and simply not an option for some PFNMAP users). The
|
|
* advantage is that we don't have to follow the strict linearity rule of
|
|
* PFNMAP mappings in order to support COWable mappings.
|
|
*
|
|
*/
|
|
struct page *_vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
|
|
pte_t pte, unsigned long vma_flags)
|
|
{
|
|
unsigned long pfn = pte_pfn(pte);
|
|
|
|
if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) {
|
|
if (likely(!pte_special(pte)))
|
|
goto check_pfn;
|
|
if (vma->vm_ops && vma->vm_ops->find_special_page)
|
|
return vma->vm_ops->find_special_page(vma, addr);
|
|
if (vma_flags & (VM_PFNMAP | VM_MIXEDMAP))
|
|
return NULL;
|
|
if (is_zero_pfn(pfn))
|
|
return NULL;
|
|
if (pte_devmap(pte))
|
|
return NULL;
|
|
|
|
print_bad_pte(vma, addr, pte, NULL);
|
|
return NULL;
|
|
}
|
|
|
|
/* !CONFIG_ARCH_HAS_PTE_SPECIAL case follows: */
|
|
/*
|
|
* This part should never get called when CONFIG_SPECULATIVE_PAGE_FAULT
|
|
* is set. This is mainly because we can't rely on vm_start.
|
|
*/
|
|
|
|
if (unlikely(vma_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
|
|
if (vma_flags & VM_MIXEDMAP) {
|
|
if (!pfn_valid(pfn))
|
|
return NULL;
|
|
goto out;
|
|
} else {
|
|
unsigned long off;
|
|
off = (addr - vma->vm_start) >> PAGE_SHIFT;
|
|
if (pfn == vma->vm_pgoff + off)
|
|
return NULL;
|
|
if (!is_cow_mapping(vma_flags))
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
if (is_zero_pfn(pfn))
|
|
return NULL;
|
|
|
|
check_pfn:
|
|
if (unlikely(pfn > highest_memmap_pfn)) {
|
|
print_bad_pte(vma, addr, pte, NULL);
|
|
return NULL;
|
|
}
|
|
|
|
/*
|
|
* NOTE! We still have PageReserved() pages in the page tables.
|
|
* eg. VDSO mappings can cause them to exist.
|
|
*/
|
|
out:
|
|
return pfn_to_page(pfn);
|
|
}
|
|
|
|
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
|
|
struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
|
|
pmd_t pmd)
|
|
{
|
|
unsigned long pfn = pmd_pfn(pmd);
|
|
|
|
/*
|
|
* There is no pmd_special() but there may be special pmds, e.g.
|
|
* in a direct-access (dax) mapping, so let's just replicate the
|
|
* !CONFIG_ARCH_HAS_PTE_SPECIAL case from vm_normal_page() here.
|
|
*/
|
|
if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
|
|
if (vma->vm_flags & VM_MIXEDMAP) {
|
|
if (!pfn_valid(pfn))
|
|
return NULL;
|
|
goto out;
|
|
} else {
|
|
unsigned long off;
|
|
off = (addr - vma->vm_start) >> PAGE_SHIFT;
|
|
if (pfn == vma->vm_pgoff + off)
|
|
return NULL;
|
|
if (!is_cow_mapping(vma->vm_flags))
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
if (pmd_devmap(pmd))
|
|
return NULL;
|
|
if (is_huge_zero_pmd(pmd))
|
|
return NULL;
|
|
if (unlikely(pfn > highest_memmap_pfn))
|
|
return NULL;
|
|
|
|
/*
|
|
* NOTE! We still have PageReserved() pages in the page tables.
|
|
* eg. VDSO mappings can cause them to exist.
|
|
*/
|
|
out:
|
|
return pfn_to_page(pfn);
|
|
}
|
|
#endif
|
|
|
|
/*
|
|
* copy one vm_area from one task to the other. Assumes the page tables
|
|
* already present in the new task to be cleared in the whole range
|
|
* covered by this vma.
|
|
*/
|
|
|
|
static unsigned long
|
|
copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
|
|
pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *dst_vma,
|
|
struct vm_area_struct *src_vma, unsigned long addr, int *rss)
|
|
{
|
|
unsigned long vm_flags = dst_vma->vm_flags;
|
|
pte_t pte = *src_pte;
|
|
struct page *page;
|
|
swp_entry_t entry = pte_to_swp_entry(pte);
|
|
|
|
if (likely(!non_swap_entry(entry))) {
|
|
if (swap_duplicate(entry) < 0)
|
|
return entry.val;
|
|
|
|
/* make sure dst_mm is on swapoff's mmlist. */
|
|
if (unlikely(list_empty(&dst_mm->mmlist))) {
|
|
spin_lock(&mmlist_lock);
|
|
if (list_empty(&dst_mm->mmlist))
|
|
list_add(&dst_mm->mmlist,
|
|
&src_mm->mmlist);
|
|
spin_unlock(&mmlist_lock);
|
|
}
|
|
rss[MM_SWAPENTS]++;
|
|
} else if (is_migration_entry(entry)) {
|
|
page = migration_entry_to_page(entry);
|
|
|
|
rss[mm_counter(page)]++;
|
|
|
|
if (is_write_migration_entry(entry) &&
|
|
is_cow_mapping(vm_flags)) {
|
|
/*
|
|
* COW mappings require pages in both
|
|
* parent and child to be set to read.
|
|
*/
|
|
make_migration_entry_read(&entry);
|
|
pte = swp_entry_to_pte(entry);
|
|
if (pte_swp_soft_dirty(*src_pte))
|
|
pte = pte_swp_mksoft_dirty(pte);
|
|
if (pte_swp_uffd_wp(*src_pte))
|
|
pte = pte_swp_mkuffd_wp(pte);
|
|
set_pte_at(src_mm, addr, src_pte, pte);
|
|
}
|
|
} else if (is_device_private_entry(entry)) {
|
|
page = device_private_entry_to_page(entry);
|
|
|
|
/*
|
|
* Update rss count even for unaddressable pages, as
|
|
* they should treated just like normal pages in this
|
|
* respect.
|
|
*
|
|
* We will likely want to have some new rss counters
|
|
* for unaddressable pages, at some point. But for now
|
|
* keep things as they are.
|
|
*/
|
|
get_page(page);
|
|
rss[mm_counter(page)]++;
|
|
page_dup_rmap(page, false);
|
|
|
|
/*
|
|
* We do not preserve soft-dirty information, because so
|
|
* far, checkpoint/restore is the only feature that
|
|
* requires that. And checkpoint/restore does not work
|
|
* when a device driver is involved (you cannot easily
|
|
* save and restore device driver state).
|
|
*/
|
|
if (is_write_device_private_entry(entry) &&
|
|
is_cow_mapping(vm_flags)) {
|
|
make_device_private_entry_read(&entry);
|
|
pte = swp_entry_to_pte(entry);
|
|
if (pte_swp_uffd_wp(*src_pte))
|
|
pte = pte_swp_mkuffd_wp(pte);
|
|
set_pte_at(src_mm, addr, src_pte, pte);
|
|
}
|
|
}
|
|
if (!userfaultfd_wp(dst_vma))
|
|
pte = pte_swp_clear_uffd_wp(pte);
|
|
set_pte_at(dst_mm, addr, dst_pte, pte);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Copy a present and normal page if necessary.
|
|
*
|
|
* NOTE! The usual case is that this doesn't need to do
|
|
* anything, and can just return a positive value. That
|
|
* will let the caller know that it can just increase
|
|
* the page refcount and re-use the pte the traditional
|
|
* way.
|
|
*
|
|
* But _if_ we need to copy it because it needs to be
|
|
* pinned in the parent (and the child should get its own
|
|
* copy rather than just a reference to the same page),
|
|
* we'll do that here and return zero to let the caller
|
|
* know we're done.
|
|
*
|
|
* And if we need a pre-allocated page but don't yet have
|
|
* one, return a negative error to let the preallocation
|
|
* code know so that it can do so outside the page table
|
|
* lock.
|
|
*/
|
|
static inline int
|
|
copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
|
|
pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss,
|
|
struct page **prealloc, pte_t pte, struct page *page)
|
|
{
|
|
struct mm_struct *src_mm = src_vma->vm_mm;
|
|
struct page *new_page;
|
|
|
|
if (!is_cow_mapping(src_vma->vm_flags))
|
|
return 1;
|
|
|
|
/*
|
|
* What we want to do is to check whether this page may
|
|
* have been pinned by the parent process. If so,
|
|
* instead of wrprotect the pte on both sides, we copy
|
|
* the page immediately so that we'll always guarantee
|
|
* the pinned page won't be randomly replaced in the
|
|
* future.
|
|
*
|
|
* The page pinning checks are just "has this mm ever
|
|
* seen pinning", along with the (inexact) check of
|
|
* the page count. That might give false positives for
|
|
* for pinning, but it will work correctly.
|
|
*/
|
|
if (likely(!atomic_read(&src_mm->has_pinned)))
|
|
return 1;
|
|
if (likely(!page_maybe_dma_pinned(page)))
|
|
return 1;
|
|
|
|
/*
|
|
* The vma->anon_vma of the child process may be NULL
|
|
* because the entire vma does not contain anonymous pages.
|
|
* A BUG will occur when the copy_present_page() passes
|
|
* a copy of a non-anonymous page of that vma to the
|
|
* page_add_new_anon_rmap() to set up new anonymous rmap.
|
|
* Return 1 if the page is not an anonymous page.
|
|
*/
|
|
if (!PageAnon(page))
|
|
return 1;
|
|
|
|
new_page = *prealloc;
|
|
if (!new_page)
|
|
return -EAGAIN;
|
|
|
|
/*
|
|
* We have a prealloc page, all good! Take it
|
|
* over and copy the page & arm it.
|
|
*/
|
|
*prealloc = NULL;
|
|
copy_user_highpage(new_page, page, addr, src_vma);
|
|
__SetPageUptodate(new_page);
|
|
page_add_new_anon_rmap(new_page, dst_vma, addr, false);
|
|
lru_cache_add_inactive_or_unevictable(new_page, dst_vma);
|
|
rss[mm_counter(new_page)]++;
|
|
|
|
/* All done, just insert the new page copy in the child */
|
|
pte = mk_pte(new_page, dst_vma->vm_page_prot);
|
|
pte = maybe_mkwrite(pte_mkdirty(pte), dst_vma->vm_flags);
|
|
if (userfaultfd_pte_wp(dst_vma, *src_pte))
|
|
/* Uffd-wp needs to be delivered to dest pte as well */
|
|
pte = pte_wrprotect(pte_mkuffd_wp(pte));
|
|
set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Copy one pte. Returns 0 if succeeded, or -EAGAIN if one preallocated page
|
|
* is required to copy this pte.
|
|
*/
|
|
static inline int
|
|
copy_present_pte(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
|
|
pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss,
|
|
struct page **prealloc)
|
|
{
|
|
struct mm_struct *src_mm = src_vma->vm_mm;
|
|
unsigned long vm_flags = src_vma->vm_flags;
|
|
pte_t pte = *src_pte;
|
|
struct page *page;
|
|
|
|
page = vm_normal_page(src_vma, addr, pte);
|
|
if (page) {
|
|
int retval;
|
|
|
|
retval = copy_present_page(dst_vma, src_vma, dst_pte, src_pte,
|
|
addr, rss, prealloc, pte, page);
|
|
if (retval <= 0)
|
|
return retval;
|
|
|
|
get_page(page);
|
|
page_dup_rmap(page, false);
|
|
rss[mm_counter(page)]++;
|
|
}
|
|
|
|
/*
|
|
* If it's a COW mapping, write protect it both
|
|
* in the parent and the child
|
|
*/
|
|
if (is_cow_mapping(vm_flags) && pte_write(pte)) {
|
|
ptep_set_wrprotect(src_mm, addr, src_pte);
|
|
pte = pte_wrprotect(pte);
|
|
}
|
|
|
|
/*
|
|
* If it's a shared mapping, mark it clean in
|
|
* the child
|
|
*/
|
|
if (vm_flags & VM_SHARED)
|
|
pte = pte_mkclean(pte);
|
|
pte = pte_mkold(pte);
|
|
|
|
if (!userfaultfd_wp(dst_vma))
|
|
pte = pte_clear_uffd_wp(pte);
|
|
|
|
set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte);
|
|
return 0;
|
|
}
|
|
|
|
static inline struct page *
|
|
page_copy_prealloc(struct mm_struct *src_mm, struct vm_area_struct *vma,
|
|
unsigned long addr)
|
|
{
|
|
struct page *new_page;
|
|
|
|
new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, addr);
|
|
if (!new_page)
|
|
return NULL;
|
|
|
|
if (mem_cgroup_charge(new_page, src_mm, GFP_KERNEL)) {
|
|
put_page(new_page);
|
|
return NULL;
|
|
}
|
|
cgroup_throttle_swaprate(new_page, GFP_KERNEL);
|
|
|
|
return new_page;
|
|
}
|
|
|
|
static int
|
|
copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
|
|
pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
|
|
unsigned long end)
|
|
{
|
|
struct mm_struct *dst_mm = dst_vma->vm_mm;
|
|
struct mm_struct *src_mm = src_vma->vm_mm;
|
|
pte_t *orig_src_pte, *orig_dst_pte;
|
|
pte_t *src_pte, *dst_pte;
|
|
spinlock_t *src_ptl, *dst_ptl;
|
|
int progress, ret = 0;
|
|
int rss[NR_MM_COUNTERS];
|
|
swp_entry_t entry = (swp_entry_t){0};
|
|
struct page *prealloc = NULL;
|
|
|
|
again:
|
|
progress = 0;
|
|
init_rss_vec(rss);
|
|
|
|
dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
|
|
if (!dst_pte) {
|
|
ret = -ENOMEM;
|
|
goto out;
|
|
}
|
|
src_pte = pte_offset_map(src_pmd, addr);
|
|
src_ptl = pte_lockptr(src_mm, src_pmd);
|
|
spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
|
|
orig_src_pte = src_pte;
|
|
orig_dst_pte = dst_pte;
|
|
arch_enter_lazy_mmu_mode();
|
|
|
|
do {
|
|
/*
|
|
* We are holding two locks at this point - either of them
|
|
* could generate latencies in another task on another CPU.
|
|
*/
|
|
if (progress >= 32) {
|
|
progress = 0;
|
|
if (need_resched() ||
|
|
spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
|
|
break;
|
|
}
|
|
if (pte_none(*src_pte)) {
|
|
progress++;
|
|
continue;
|
|
}
|
|
if (unlikely(!pte_present(*src_pte))) {
|
|
entry.val = copy_nonpresent_pte(dst_mm, src_mm,
|
|
dst_pte, src_pte,
|
|
dst_vma, src_vma,
|
|
addr, rss);
|
|
if (entry.val)
|
|
break;
|
|
progress += 8;
|
|
continue;
|
|
}
|
|
/* copy_present_pte() will clear `*prealloc' if consumed */
|
|
ret = copy_present_pte(dst_vma, src_vma, dst_pte, src_pte,
|
|
addr, rss, &prealloc);
|
|
/*
|
|
* If we need a pre-allocated page for this pte, drop the
|
|
* locks, allocate, and try again.
|
|
*/
|
|
if (unlikely(ret == -EAGAIN))
|
|
break;
|
|
if (unlikely(prealloc)) {
|
|
/*
|
|
* pre-alloc page cannot be reused by next time so as
|
|
* to strictly follow mempolicy (e.g., alloc_page_vma()
|
|
* will allocate page according to address). This
|
|
* could only happen if one pinned pte changed.
|
|
*/
|
|
put_page(prealloc);
|
|
prealloc = NULL;
|
|
}
|
|
progress += 8;
|
|
} while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
|
|
|
|
arch_leave_lazy_mmu_mode();
|
|
spin_unlock(src_ptl);
|
|
pte_unmap(orig_src_pte);
|
|
add_mm_rss_vec(dst_mm, rss);
|
|
pte_unmap_unlock(orig_dst_pte, dst_ptl);
|
|
cond_resched();
|
|
|
|
if (entry.val) {
|
|
if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) {
|
|
ret = -ENOMEM;
|
|
goto out;
|
|
}
|
|
entry.val = 0;
|
|
} else if (ret) {
|
|
WARN_ON_ONCE(ret != -EAGAIN);
|
|
prealloc = page_copy_prealloc(src_mm, src_vma, addr);
|
|
if (!prealloc)
|
|
return -ENOMEM;
|
|
/* We've captured and resolved the error. Reset, try again. */
|
|
ret = 0;
|
|
}
|
|
if (addr != end)
|
|
goto again;
|
|
out:
|
|
if (unlikely(prealloc))
|
|
put_page(prealloc);
|
|
return ret;
|
|
}
|
|
|
|
static inline int
|
|
copy_pmd_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
|
|
pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
|
|
unsigned long end)
|
|
{
|
|
struct mm_struct *dst_mm = dst_vma->vm_mm;
|
|
struct mm_struct *src_mm = src_vma->vm_mm;
|
|
pmd_t *src_pmd, *dst_pmd;
|
|
unsigned long next;
|
|
|
|
dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
|
|
if (!dst_pmd)
|
|
return -ENOMEM;
|
|
src_pmd = pmd_offset(src_pud, addr);
|
|
do {
|
|
next = pmd_addr_end(addr, end);
|
|
if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd)
|
|
|| pmd_devmap(*src_pmd)) {
|
|
int err;
|
|
VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, src_vma);
|
|
err = copy_huge_pmd(dst_mm, src_mm, dst_pmd, src_pmd,
|
|
addr, dst_vma, src_vma);
|
|
if (err == -ENOMEM)
|
|
return -ENOMEM;
|
|
if (!err)
|
|
continue;
|
|
/* fall through */
|
|
}
|
|
if (pmd_none_or_clear_bad(src_pmd))
|
|
continue;
|
|
if (copy_pte_range(dst_vma, src_vma, dst_pmd, src_pmd,
|
|
addr, next))
|
|
return -ENOMEM;
|
|
} while (dst_pmd++, src_pmd++, addr = next, addr != end);
|
|
return 0;
|
|
}
|
|
|
|
static inline int
|
|
copy_pud_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
|
|
p4d_t *dst_p4d, p4d_t *src_p4d, unsigned long addr,
|
|
unsigned long end)
|
|
{
|
|
struct mm_struct *dst_mm = dst_vma->vm_mm;
|
|
struct mm_struct *src_mm = src_vma->vm_mm;
|
|
pud_t *src_pud, *dst_pud;
|
|
unsigned long next;
|
|
|
|
dst_pud = pud_alloc(dst_mm, dst_p4d, addr);
|
|
if (!dst_pud)
|
|
return -ENOMEM;
|
|
src_pud = pud_offset(src_p4d, addr);
|
|
do {
|
|
next = pud_addr_end(addr, end);
|
|
if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) {
|
|
int err;
|
|
|
|
VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, src_vma);
|
|
err = copy_huge_pud(dst_mm, src_mm,
|
|
dst_pud, src_pud, addr, src_vma);
|
|
if (err == -ENOMEM)
|
|
return -ENOMEM;
|
|
if (!err)
|
|
continue;
|
|
/* fall through */
|
|
}
|
|
if (pud_none_or_clear_bad(src_pud))
|
|
continue;
|
|
if (copy_pmd_range(dst_vma, src_vma, dst_pud, src_pud,
|
|
addr, next))
|
|
return -ENOMEM;
|
|
} while (dst_pud++, src_pud++, addr = next, addr != end);
|
|
return 0;
|
|
}
|
|
|
|
static inline int
|
|
copy_p4d_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
|
|
pgd_t *dst_pgd, pgd_t *src_pgd, unsigned long addr,
|
|
unsigned long end)
|
|
{
|
|
struct mm_struct *dst_mm = dst_vma->vm_mm;
|
|
p4d_t *src_p4d, *dst_p4d;
|
|
unsigned long next;
|
|
|
|
dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr);
|
|
if (!dst_p4d)
|
|
return -ENOMEM;
|
|
src_p4d = p4d_offset(src_pgd, addr);
|
|
do {
|
|
next = p4d_addr_end(addr, end);
|
|
if (p4d_none_or_clear_bad(src_p4d))
|
|
continue;
|
|
if (copy_pud_range(dst_vma, src_vma, dst_p4d, src_p4d,
|
|
addr, next))
|
|
return -ENOMEM;
|
|
} while (dst_p4d++, src_p4d++, addr = next, addr != end);
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)
|
|
{
|
|
pgd_t *src_pgd, *dst_pgd;
|
|
unsigned long next;
|
|
unsigned long addr = src_vma->vm_start;
|
|
unsigned long end = src_vma->vm_end;
|
|
struct mm_struct *dst_mm = dst_vma->vm_mm;
|
|
struct mm_struct *src_mm = src_vma->vm_mm;
|
|
struct mmu_notifier_range range;
|
|
bool is_cow;
|
|
int ret;
|
|
|
|
/*
|
|
* Don't copy ptes where a page fault will fill them correctly.
|
|
* Fork becomes much lighter when there are big shared or private
|
|
* readonly mappings. The tradeoff is that copy_page_range is more
|
|
* efficient than faulting.
|
|
*/
|
|
if (!(src_vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) &&
|
|
!src_vma->anon_vma)
|
|
return 0;
|
|
|
|
if (is_vm_hugetlb_page(src_vma))
|
|
return copy_hugetlb_page_range(dst_mm, src_mm, src_vma);
|
|
|
|
if (unlikely(src_vma->vm_flags & VM_PFNMAP)) {
|
|
/*
|
|
* We do not free on error cases below as remove_vma
|
|
* gets called on error from higher level routine
|
|
*/
|
|
ret = track_pfn_copy(src_vma);
|
|
if (ret)
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* We need to invalidate the secondary MMU mappings only when
|
|
* there could be a permission downgrade on the ptes of the
|
|
* parent mm. And a permission downgrade will only happen if
|
|
* is_cow_mapping() returns true.
|
|
*/
|
|
is_cow = is_cow_mapping(src_vma->vm_flags);
|
|
|
|
if (is_cow) {
|
|
mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE,
|
|
0, src_vma, src_mm, addr, end);
|
|
mmu_notifier_invalidate_range_start(&range);
|
|
/*
|
|
* Disabling preemption is not needed for the write side, as
|
|
* the read side doesn't spin, but goes to the mmap_lock.
|
|
*
|
|
* Use the raw variant of the seqcount_t write API to avoid
|
|
* lockdep complaining about preemptibility.
|
|
*/
|
|
mmap_assert_write_locked(src_mm);
|
|
raw_write_seqcount_begin(&src_mm->write_protect_seq);
|
|
}
|
|
|
|
ret = 0;
|
|
dst_pgd = pgd_offset(dst_mm, addr);
|
|
src_pgd = pgd_offset(src_mm, addr);
|
|
do {
|
|
next = pgd_addr_end(addr, end);
|
|
if (pgd_none_or_clear_bad(src_pgd))
|
|
continue;
|
|
if (unlikely(copy_p4d_range(dst_vma, src_vma, dst_pgd, src_pgd,
|
|
addr, next))) {
|
|
ret = -ENOMEM;
|
|
break;
|
|
}
|
|
} while (dst_pgd++, src_pgd++, addr = next, addr != end);
|
|
|
|
if (is_cow) {
|
|
raw_write_seqcount_end(&src_mm->write_protect_seq);
|
|
mmu_notifier_invalidate_range_end(&range);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
/* Whether we should zap all COWed (private) pages too */
|
|
static inline bool should_zap_cows(struct zap_details *details)
|
|
{
|
|
/* By default, zap all pages */
|
|
if (!details)
|
|
return true;
|
|
|
|
/* Or, we zap COWed pages only if the caller wants to */
|
|
return !details->check_mapping;
|
|
}
|
|
|
|
static unsigned long zap_pte_range(struct mmu_gather *tlb,
|
|
struct vm_area_struct *vma, pmd_t *pmd,
|
|
unsigned long addr, unsigned long end,
|
|
struct zap_details *details)
|
|
{
|
|
struct mm_struct *mm = tlb->mm;
|
|
int force_flush = 0;
|
|
int rss[NR_MM_COUNTERS];
|
|
spinlock_t *ptl;
|
|
pte_t *start_pte;
|
|
pte_t *pte;
|
|
swp_entry_t entry;
|
|
|
|
tlb_change_page_size(tlb, PAGE_SIZE);
|
|
again:
|
|
init_rss_vec(rss);
|
|
start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
|
|
pte = start_pte;
|
|
flush_tlb_batched_pending(mm);
|
|
arch_enter_lazy_mmu_mode();
|
|
do {
|
|
pte_t ptent = *pte;
|
|
if (pte_none(ptent))
|
|
continue;
|
|
|
|
if (need_resched())
|
|
break;
|
|
|
|
if (pte_present(ptent)) {
|
|
struct page *page;
|
|
|
|
page = vm_normal_page(vma, addr, ptent);
|
|
if (unlikely(details) && page) {
|
|
/*
|
|
* unmap_shared_mapping_pages() wants to
|
|
* invalidate cache without truncating:
|
|
* unmap shared but keep private pages.
|
|
*/
|
|
if (details->check_mapping &&
|
|
details->check_mapping != page_rmapping(page))
|
|
continue;
|
|
}
|
|
ptent = ptep_get_and_clear_full(mm, addr, pte,
|
|
tlb->fullmm);
|
|
tlb_remove_tlb_entry(tlb, pte, addr);
|
|
if (unlikely(!page))
|
|
continue;
|
|
|
|
if (!PageAnon(page)) {
|
|
if (pte_dirty(ptent)) {
|
|
force_flush = 1;
|
|
set_page_dirty(page);
|
|
}
|
|
if (pte_young(ptent) &&
|
|
likely(!(vma->vm_flags & VM_SEQ_READ)))
|
|
mark_page_accessed(page);
|
|
}
|
|
rss[mm_counter(page)]--;
|
|
page_remove_rmap(page, false);
|
|
if (unlikely(page_mapcount(page) < 0))
|
|
print_bad_pte(vma, addr, ptent, page);
|
|
if (unlikely(__tlb_remove_page(tlb, page)) ||
|
|
lru_cache_disabled()) {
|
|
force_flush = 1;
|
|
addr += PAGE_SIZE;
|
|
break;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
entry = pte_to_swp_entry(ptent);
|
|
if (is_device_private_entry(entry)) {
|
|
struct page *page = device_private_entry_to_page(entry);
|
|
|
|
if (unlikely(details && details->check_mapping)) {
|
|
/*
|
|
* unmap_shared_mapping_pages() wants to
|
|
* invalidate cache without truncating:
|
|
* unmap shared but keep private pages.
|
|
*/
|
|
if (details->check_mapping !=
|
|
page_rmapping(page))
|
|
continue;
|
|
}
|
|
|
|
pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
|
|
rss[mm_counter(page)]--;
|
|
page_remove_rmap(page, false);
|
|
put_page(page);
|
|
continue;
|
|
}
|
|
|
|
if (!non_swap_entry(entry)) {
|
|
/* Genuine swap entry, hence a private anon page */
|
|
if (!should_zap_cows(details))
|
|
continue;
|
|
rss[MM_SWAPENTS]--;
|
|
} else if (is_migration_entry(entry)) {
|
|
struct page *page;
|
|
|
|
page = migration_entry_to_page(entry);
|
|
if (details && details->check_mapping &&
|
|
details->check_mapping != page_rmapping(page))
|
|
continue;
|
|
rss[mm_counter(page)]--;
|
|
}
|
|
if (unlikely(!free_swap_and_cache(entry)))
|
|
print_bad_pte(vma, addr, ptent, NULL);
|
|
pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
|
|
} while (pte++, addr += PAGE_SIZE, addr != end);
|
|
|
|
add_mm_rss_vec(mm, rss);
|
|
arch_leave_lazy_mmu_mode();
|
|
|
|
/* Do the actual TLB flush before dropping ptl */
|
|
if (force_flush)
|
|
tlb_flush_mmu_tlbonly(tlb);
|
|
pte_unmap_unlock(start_pte, ptl);
|
|
|
|
/*
|
|
* If we forced a TLB flush (either due to running out of
|
|
* batch buffers or because we needed to flush dirty TLB
|
|
* entries before releasing the ptl), free the batched
|
|
* memory too. Restart if we didn't do everything.
|
|
*/
|
|
if (force_flush) {
|
|
force_flush = 0;
|
|
tlb_flush_mmu(tlb);
|
|
}
|
|
|
|
if (addr != end) {
|
|
cond_resched();
|
|
goto again;
|
|
}
|
|
|
|
return addr;
|
|
}
|
|
|
|
static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
|
|
struct vm_area_struct *vma, pud_t *pud,
|
|
unsigned long addr, unsigned long end,
|
|
struct zap_details *details)
|
|
{
|
|
pmd_t *pmd;
|
|
unsigned long next;
|
|
|
|
pmd = pmd_offset(pud, addr);
|
|
do {
|
|
next = pmd_addr_end(addr, end);
|
|
if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
|
|
if (next - addr != HPAGE_PMD_SIZE)
|
|
__split_huge_pmd(vma, pmd, addr, false, NULL);
|
|
else if (zap_huge_pmd(tlb, vma, pmd, addr))
|
|
goto next;
|
|
/* fall through */
|
|
} else if (details && details->single_page &&
|
|
PageTransCompound(details->single_page) &&
|
|
next - addr == HPAGE_PMD_SIZE && pmd_none(*pmd)) {
|
|
spinlock_t *ptl = pmd_lock(tlb->mm, pmd);
|
|
/*
|
|
* Take and drop THP pmd lock so that we cannot return
|
|
* prematurely, while zap_huge_pmd() has cleared *pmd,
|
|
* but not yet decremented compound_mapcount().
|
|
*/
|
|
spin_unlock(ptl);
|
|
}
|
|
|
|
/*
|
|
* Here there can be other concurrent MADV_DONTNEED or
|
|
* trans huge page faults running, and if the pmd is
|
|
* none or trans huge it can change under us. This is
|
|
* because MADV_DONTNEED holds the mmap_lock in read
|
|
* mode.
|
|
*/
|
|
if (pmd_none_or_trans_huge_or_clear_bad(pmd))
|
|
goto next;
|
|
next = zap_pte_range(tlb, vma, pmd, addr, next, details);
|
|
next:
|
|
cond_resched();
|
|
} while (pmd++, addr = next, addr != end);
|
|
|
|
return addr;
|
|
}
|
|
|
|
static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
|
|
struct vm_area_struct *vma, p4d_t *p4d,
|
|
unsigned long addr, unsigned long end,
|
|
struct zap_details *details)
|
|
{
|
|
pud_t *pud;
|
|
unsigned long next;
|
|
|
|
pud = pud_offset(p4d, addr);
|
|
do {
|
|
next = pud_addr_end(addr, end);
|
|
if (pud_trans_huge(*pud) || pud_devmap(*pud)) {
|
|
if (next - addr != HPAGE_PUD_SIZE) {
|
|
mmap_assert_locked(tlb->mm);
|
|
split_huge_pud(vma, pud, addr);
|
|
} else if (zap_huge_pud(tlb, vma, pud, addr))
|
|
goto next;
|
|
/* fall through */
|
|
}
|
|
if (pud_none_or_clear_bad(pud))
|
|
continue;
|
|
next = zap_pmd_range(tlb, vma, pud, addr, next, details);
|
|
next:
|
|
cond_resched();
|
|
} while (pud++, addr = next, addr != end);
|
|
|
|
return addr;
|
|
}
|
|
|
|
static inline unsigned long zap_p4d_range(struct mmu_gather *tlb,
|
|
struct vm_area_struct *vma, pgd_t *pgd,
|
|
unsigned long addr, unsigned long end,
|
|
struct zap_details *details)
|
|
{
|
|
p4d_t *p4d;
|
|
unsigned long next;
|
|
|
|
p4d = p4d_offset(pgd, addr);
|
|
do {
|
|
next = p4d_addr_end(addr, end);
|
|
if (p4d_none_or_clear_bad(p4d))
|
|
continue;
|
|
next = zap_pud_range(tlb, vma, p4d, addr, next, details);
|
|
} while (p4d++, addr = next, addr != end);
|
|
|
|
return addr;
|
|
}
|
|
|
|
void unmap_page_range(struct mmu_gather *tlb,
|
|
struct vm_area_struct *vma,
|
|
unsigned long addr, unsigned long end,
|
|
struct zap_details *details)
|
|
{
|
|
pgd_t *pgd;
|
|
unsigned long next;
|
|
|
|
BUG_ON(addr >= end);
|
|
tlb_start_vma(tlb, vma);
|
|
pgd = pgd_offset(vma->vm_mm, addr);
|
|
do {
|
|
next = pgd_addr_end(addr, end);
|
|
if (pgd_none_or_clear_bad(pgd))
|
|
continue;
|
|
next = zap_p4d_range(tlb, vma, pgd, addr, next, details);
|
|
} while (pgd++, addr = next, addr != end);
|
|
tlb_end_vma(tlb, vma);
|
|
}
|
|
|
|
|
|
static void unmap_single_vma(struct mmu_gather *tlb,
|
|
struct vm_area_struct *vma, unsigned long start_addr,
|
|
unsigned long end_addr,
|
|
struct zap_details *details)
|
|
{
|
|
unsigned long start = max(vma->vm_start, start_addr);
|
|
unsigned long end;
|
|
|
|
if (start >= vma->vm_end)
|
|
return;
|
|
end = min(vma->vm_end, end_addr);
|
|
if (end <= vma->vm_start)
|
|
return;
|
|
|
|
if (vma->vm_file)
|
|
uprobe_munmap(vma, start, end);
|
|
|
|
if (unlikely(vma->vm_flags & VM_PFNMAP))
|
|
untrack_pfn(vma, 0, 0);
|
|
|
|
if (start != end) {
|
|
if (unlikely(is_vm_hugetlb_page(vma))) {
|
|
/*
|
|
* It is undesirable to test vma->vm_file as it
|
|
* should be non-null for valid hugetlb area.
|
|
* However, vm_file will be NULL in the error
|
|
* cleanup path of mmap_region. When
|
|
* hugetlbfs ->mmap method fails,
|
|
* mmap_region() nullifies vma->vm_file
|
|
* before calling this function to clean up.
|
|
* Since no pte has actually been setup, it is
|
|
* safe to do nothing in this case.
|
|
*/
|
|
if (vma->vm_file) {
|
|
i_mmap_lock_write(vma->vm_file->f_mapping);
|
|
__unmap_hugepage_range_final(tlb, vma, start, end, NULL);
|
|
i_mmap_unlock_write(vma->vm_file->f_mapping);
|
|
}
|
|
} else
|
|
unmap_page_range(tlb, vma, start, end, details);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* unmap_vmas - unmap a range of memory covered by a list of vma's
|
|
* @tlb: address of the caller's struct mmu_gather
|
|
* @vma: the starting vma
|
|
* @start_addr: virtual address at which to start unmapping
|
|
* @end_addr: virtual address at which to end unmapping
|
|
*
|
|
* Unmap all pages in the vma list.
|
|
*
|
|
* Only addresses between `start' and `end' will be unmapped.
|
|
*
|
|
* The VMA list must be sorted in ascending virtual address order.
|
|
*
|
|
* unmap_vmas() assumes that the caller will flush the whole unmapped address
|
|
* range after unmap_vmas() returns. So the only responsibility here is to
|
|
* ensure that any thus-far unmapped pages are flushed before unmap_vmas()
|
|
* drops the lock and schedules.
|
|
*/
|
|
void unmap_vmas(struct mmu_gather *tlb,
|
|
struct vm_area_struct *vma, unsigned long start_addr,
|
|
unsigned long end_addr)
|
|
{
|
|
struct mmu_notifier_range range;
|
|
|
|
mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, vma->vm_mm,
|
|
start_addr, end_addr);
|
|
mmu_notifier_invalidate_range_start(&range);
|
|
for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
|
|
unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
|
|
mmu_notifier_invalidate_range_end(&range);
|
|
}
|
|
|
|
/**
|
|
* zap_page_range - remove user pages in a given range
|
|
* @vma: vm_area_struct holding the applicable pages
|
|
* @start: starting address of pages to zap
|
|
* @size: number of bytes to zap
|
|
*
|
|
* Caller must protect the VMA list
|
|
*/
|
|
void zap_page_range(struct vm_area_struct *vma, unsigned long start,
|
|
unsigned long size)
|
|
{
|
|
struct mmu_notifier_range range;
|
|
struct mmu_gather tlb;
|
|
|
|
lru_add_drain();
|
|
mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
|
|
start, start + size);
|
|
tlb_gather_mmu(&tlb, vma->vm_mm, start, range.end);
|
|
update_hiwater_rss(vma->vm_mm);
|
|
mmu_notifier_invalidate_range_start(&range);
|
|
for ( ; vma && vma->vm_start < range.end; vma = vma->vm_next)
|
|
unmap_single_vma(&tlb, vma, start, range.end, NULL);
|
|
mmu_notifier_invalidate_range_end(&range);
|
|
tlb_finish_mmu(&tlb, start, range.end);
|
|
}
|
|
|
|
/**
|
|
* zap_page_range_single - remove user pages in a given range
|
|
* @vma: vm_area_struct holding the applicable pages
|
|
* @address: starting address of pages to zap
|
|
* @size: number of bytes to zap
|
|
* @details: details of shared cache invalidation
|
|
*
|
|
* The range must fit into one VMA.
|
|
*/
|
|
static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
|
|
unsigned long size, struct zap_details *details)
|
|
{
|
|
struct mmu_notifier_range range;
|
|
struct mmu_gather tlb;
|
|
|
|
lru_add_drain();
|
|
mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
|
|
address, address + size);
|
|
tlb_gather_mmu(&tlb, vma->vm_mm, address, range.end);
|
|
update_hiwater_rss(vma->vm_mm);
|
|
mmu_notifier_invalidate_range_start(&range);
|
|
unmap_single_vma(&tlb, vma, address, range.end, details);
|
|
mmu_notifier_invalidate_range_end(&range);
|
|
tlb_finish_mmu(&tlb, address, range.end);
|
|
}
|
|
|
|
/**
|
|
* zap_vma_ptes - remove ptes mapping the vma
|
|
* @vma: vm_area_struct holding ptes to be zapped
|
|
* @address: starting address of pages to zap
|
|
* @size: number of bytes to zap
|
|
*
|
|
* This function only unmaps ptes assigned to VM_PFNMAP vmas.
|
|
*
|
|
* The entire address range must be fully contained within the vma.
|
|
*
|
|
*/
|
|
void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
|
|
unsigned long size)
|
|
{
|
|
if (address < vma->vm_start || address + size > vma->vm_end ||
|
|
!(vma->vm_flags & VM_PFNMAP))
|
|
return;
|
|
|
|
zap_page_range_single(vma, address, size, NULL);
|
|
}
|
|
EXPORT_SYMBOL_GPL(zap_vma_ptes);
|
|
|
|
static pmd_t *walk_to_pmd(struct mm_struct *mm, unsigned long addr)
|
|
{
|
|
pgd_t *pgd;
|
|
p4d_t *p4d;
|
|
pud_t *pud;
|
|
pmd_t *pmd;
|
|
|
|
pgd = pgd_offset(mm, addr);
|
|
p4d = p4d_alloc(mm, pgd, addr);
|
|
if (!p4d)
|
|
return NULL;
|
|
pud = pud_alloc(mm, p4d, addr);
|
|
if (!pud)
|
|
return NULL;
|
|
pmd = pmd_alloc(mm, pud, addr);
|
|
if (!pmd)
|
|
return NULL;
|
|
|
|
VM_BUG_ON(pmd_trans_huge(*pmd));
|
|
return pmd;
|
|
}
|
|
|
|
pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
|
|
spinlock_t **ptl)
|
|
{
|
|
pmd_t *pmd = walk_to_pmd(mm, addr);
|
|
|
|
if (!pmd)
|
|
return NULL;
|
|
return pte_alloc_map_lock(mm, pmd, addr, ptl);
|
|
}
|
|
|
|
static int validate_page_before_insert(struct page *page)
|
|
{
|
|
if (PageAnon(page) || PageSlab(page) || page_has_type(page))
|
|
return -EINVAL;
|
|
flush_dcache_page(page);
|
|
return 0;
|
|
}
|
|
|
|
static int insert_page_into_pte_locked(struct mm_struct *mm, pte_t *pte,
|
|
unsigned long addr, struct page *page, pgprot_t prot)
|
|
{
|
|
if (!pte_none(*pte))
|
|
return -EBUSY;
|
|
/* Ok, finally just insert the thing.. */
|
|
get_page(page);
|
|
inc_mm_counter_fast(mm, mm_counter_file(page));
|
|
page_add_file_rmap(page, false);
|
|
set_pte_at(mm, addr, pte, mk_pte(page, prot));
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* This is the old fallback for page remapping.
|
|
*
|
|
* For historical reasons, it only allows reserved pages. Only
|
|
* old drivers should use this, and they needed to mark their
|
|
* pages reserved for the old functions anyway.
|
|
*/
|
|
static int insert_page(struct vm_area_struct *vma, unsigned long addr,
|
|
struct page *page, pgprot_t prot)
|
|
{
|
|
struct mm_struct *mm = vma->vm_mm;
|
|
int retval;
|
|
pte_t *pte;
|
|
spinlock_t *ptl;
|
|
|
|
retval = validate_page_before_insert(page);
|
|
if (retval)
|
|
goto out;
|
|
retval = -ENOMEM;
|
|
pte = get_locked_pte(mm, addr, &ptl);
|
|
if (!pte)
|
|
goto out;
|
|
retval = insert_page_into_pte_locked(mm, pte, addr, page, prot);
|
|
pte_unmap_unlock(pte, ptl);
|
|
out:
|
|
return retval;
|
|
}
|
|
|
|
#ifdef pte_index
|
|
static int insert_page_in_batch_locked(struct mm_struct *mm, pte_t *pte,
|
|
unsigned long addr, struct page *page, pgprot_t prot)
|
|
{
|
|
int err;
|
|
|
|
if (!page_count(page))
|
|
return -EINVAL;
|
|
err = validate_page_before_insert(page);
|
|
if (err)
|
|
return err;
|
|
return insert_page_into_pte_locked(mm, pte, addr, page, prot);
|
|
}
|
|
|
|
/* insert_pages() amortizes the cost of spinlock operations
|
|
* when inserting pages in a loop. Arch *must* define pte_index.
|
|
*/
|
|
static int insert_pages(struct vm_area_struct *vma, unsigned long addr,
|
|
struct page **pages, unsigned long *num, pgprot_t prot)
|
|
{
|
|
pmd_t *pmd = NULL;
|
|
pte_t *start_pte, *pte;
|
|
spinlock_t *pte_lock;
|
|
struct mm_struct *const mm = vma->vm_mm;
|
|
unsigned long curr_page_idx = 0;
|
|
unsigned long remaining_pages_total = *num;
|
|
unsigned long pages_to_write_in_pmd;
|
|
int ret;
|
|
more:
|
|
ret = -EFAULT;
|
|
pmd = walk_to_pmd(mm, addr);
|
|
if (!pmd)
|
|
goto out;
|
|
|
|
pages_to_write_in_pmd = min_t(unsigned long,
|
|
remaining_pages_total, PTRS_PER_PTE - pte_index(addr));
|
|
|
|
/* Allocate the PTE if necessary; takes PMD lock once only. */
|
|
ret = -ENOMEM;
|
|
if (pte_alloc(mm, pmd))
|
|
goto out;
|
|
|
|
while (pages_to_write_in_pmd) {
|
|
int pte_idx = 0;
|
|
const int batch_size = min_t(int, pages_to_write_in_pmd, 8);
|
|
|
|
start_pte = pte_offset_map_lock(mm, pmd, addr, &pte_lock);
|
|
for (pte = start_pte; pte_idx < batch_size; ++pte, ++pte_idx) {
|
|
int err = insert_page_in_batch_locked(mm, pte,
|
|
addr, pages[curr_page_idx], prot);
|
|
if (unlikely(err)) {
|
|
pte_unmap_unlock(start_pte, pte_lock);
|
|
ret = err;
|
|
remaining_pages_total -= pte_idx;
|
|
goto out;
|
|
}
|
|
addr += PAGE_SIZE;
|
|
++curr_page_idx;
|
|
}
|
|
pte_unmap_unlock(start_pte, pte_lock);
|
|
pages_to_write_in_pmd -= batch_size;
|
|
remaining_pages_total -= batch_size;
|
|
}
|
|
if (remaining_pages_total)
|
|
goto more;
|
|
ret = 0;
|
|
out:
|
|
*num = remaining_pages_total;
|
|
return ret;
|
|
}
|
|
#endif /* ifdef pte_index */
|
|
|
|
/**
|
|
* vm_insert_pages - insert multiple pages into user vma, batching the pmd lock.
|
|
* @vma: user vma to map to
|
|
* @addr: target start user address of these pages
|
|
* @pages: source kernel pages
|
|
* @num: in: number of pages to map. out: number of pages that were *not*
|
|
* mapped. (0 means all pages were successfully mapped).
|
|
*
|
|
* Preferred over vm_insert_page() when inserting multiple pages.
|
|
*
|
|
* In case of error, we may have mapped a subset of the provided
|
|
* pages. It is the caller's responsibility to account for this case.
|
|
*
|
|
* The same restrictions apply as in vm_insert_page().
|
|
*/
|
|
int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr,
|
|
struct page **pages, unsigned long *num)
|
|
{
|
|
#ifdef pte_index
|
|
const unsigned long end_addr = addr + (*num * PAGE_SIZE) - 1;
|
|
|
|
if (addr < vma->vm_start || end_addr >= vma->vm_end)
|
|
return -EFAULT;
|
|
if (!(vma->vm_flags & VM_MIXEDMAP)) {
|
|
BUG_ON(mmap_read_trylock(vma->vm_mm));
|
|
BUG_ON(vma->vm_flags & VM_PFNMAP);
|
|
vma->vm_flags |= VM_MIXEDMAP;
|
|
}
|
|
/* Defer page refcount checking till we're about to map that page. */
|
|
return insert_pages(vma, addr, pages, num, vma->vm_page_prot);
|
|
#else
|
|
unsigned long idx = 0, pgcount = *num;
|
|
int err = -EINVAL;
|
|
|
|
for (; idx < pgcount; ++idx) {
|
|
err = vm_insert_page(vma, addr + (PAGE_SIZE * idx), pages[idx]);
|
|
if (err)
|
|
break;
|
|
}
|
|
*num = pgcount - idx;
|
|
return err;
|
|
#endif /* ifdef pte_index */
|
|
}
|
|
EXPORT_SYMBOL(vm_insert_pages);
|
|
|
|
/**
|
|
* vm_insert_page - insert single page into user vma
|
|
* @vma: user vma to map to
|
|
* @addr: target user address of this page
|
|
* @page: source kernel page
|
|
*
|
|
* This allows drivers to insert individual pages they've allocated
|
|
* into a user vma.
|
|
*
|
|
* The page has to be a nice clean _individual_ kernel allocation.
|
|
* If you allocate a compound page, you need to have marked it as
|
|
* such (__GFP_COMP), or manually just split the page up yourself
|
|
* (see split_page()).
|
|
*
|
|
* NOTE! Traditionally this was done with "remap_pfn_range()" which
|
|
* took an arbitrary page protection parameter. This doesn't allow
|
|
* that. Your vma protection will have to be set up correctly, which
|
|
* means that if you want a shared writable mapping, you'd better
|
|
* ask for a shared writable mapping!
|
|
*
|
|
* The page does not need to be reserved.
|
|
*
|
|
* Usually this function is called from f_op->mmap() handler
|
|
* under mm->mmap_lock write-lock, so it can change vma->vm_flags.
|
|
* Caller must set VM_MIXEDMAP on vma if it wants to call this
|
|
* function from other places, for example from page-fault handler.
|
|
*
|
|
* Return: %0 on success, negative error code otherwise.
|
|
*/
|
|
int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
|
|
struct page *page)
|
|
{
|
|
if (addr < vma->vm_start || addr >= vma->vm_end)
|
|
return -EFAULT;
|
|
if (!page_count(page))
|
|
return -EINVAL;
|
|
if (!(vma->vm_flags & VM_MIXEDMAP)) {
|
|
BUG_ON(mmap_read_trylock(vma->vm_mm));
|
|
BUG_ON(vma->vm_flags & VM_PFNMAP);
|
|
vma->vm_flags |= VM_MIXEDMAP;
|
|
}
|
|
return insert_page(vma, addr, page, vma->vm_page_prot);
|
|
}
|
|
EXPORT_SYMBOL(vm_insert_page);
|
|
|
|
/*
|
|
* __vm_map_pages - maps range of kernel pages into user vma
|
|
* @vma: user vma to map to
|
|
* @pages: pointer to array of source kernel pages
|
|
* @num: number of pages in page array
|
|
* @offset: user's requested vm_pgoff
|
|
*
|
|
* This allows drivers to map range of kernel pages into a user vma.
|
|
*
|
|
* Return: 0 on success and error code otherwise.
|
|
*/
|
|
static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages,
|
|
unsigned long num, unsigned long offset)
|
|
{
|
|
unsigned long count = vma_pages(vma);
|
|
unsigned long uaddr = vma->vm_start;
|
|
int ret, i;
|
|
|
|
/* Fail if the user requested offset is beyond the end of the object */
|
|
if (offset >= num)
|
|
return -ENXIO;
|
|
|
|
/* Fail if the user requested size exceeds available object size */
|
|
if (count > num - offset)
|
|
return -ENXIO;
|
|
|
|
for (i = 0; i < count; i++) {
|
|
ret = vm_insert_page(vma, uaddr, pages[offset + i]);
|
|
if (ret < 0)
|
|
return ret;
|
|
uaddr += PAGE_SIZE;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* vm_map_pages - maps range of kernel pages starts with non zero offset
|
|
* @vma: user vma to map to
|
|
* @pages: pointer to array of source kernel pages
|
|
* @num: number of pages in page array
|
|
*
|
|
* Maps an object consisting of @num pages, catering for the user's
|
|
* requested vm_pgoff
|
|
*
|
|
* If we fail to insert any page into the vma, the function will return
|
|
* immediately leaving any previously inserted pages present. Callers
|
|
* from the mmap handler may immediately return the error as their caller
|
|
* will destroy the vma, removing any successfully inserted pages. Other
|
|
* callers should make their own arrangements for calling unmap_region().
|
|
*
|
|
* Context: Process context. Called by mmap handlers.
|
|
* Return: 0 on success and error code otherwise.
|
|
*/
|
|
int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
|
|
unsigned long num)
|
|
{
|
|
return __vm_map_pages(vma, pages, num, vma->vm_pgoff);
|
|
}
|
|
EXPORT_SYMBOL(vm_map_pages);
|
|
|
|
/**
|
|
* vm_map_pages_zero - map range of kernel pages starts with zero offset
|
|
* @vma: user vma to map to
|
|
* @pages: pointer to array of source kernel pages
|
|
* @num: number of pages in page array
|
|
*
|
|
* Similar to vm_map_pages(), except that it explicitly sets the offset
|
|
* to 0. This function is intended for the drivers that did not consider
|
|
* vm_pgoff.
|
|
*
|
|
* Context: Process context. Called by mmap handlers.
|
|
* Return: 0 on success and error code otherwise.
|
|
*/
|
|
int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
|
|
unsigned long num)
|
|
{
|
|
return __vm_map_pages(vma, pages, num, 0);
|
|
}
|
|
EXPORT_SYMBOL(vm_map_pages_zero);
|
|
|
|
static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr,
|
|
pfn_t pfn, pgprot_t prot, bool mkwrite)
|
|
{
|
|
struct mm_struct *mm = vma->vm_mm;
|
|
pte_t *pte, entry;
|
|
spinlock_t *ptl;
|
|
|
|
pte = get_locked_pte(mm, addr, &ptl);
|
|
if (!pte)
|
|
return VM_FAULT_OOM;
|
|
if (!pte_none(*pte)) {
|
|
if (mkwrite) {
|
|
/*
|
|
* For read faults on private mappings the PFN passed
|
|
* in may not match the PFN we have mapped if the
|
|
* mapped PFN is a writeable COW page. In the mkwrite
|
|
* case we are creating a writable PTE for a shared
|
|
* mapping and we expect the PFNs to match. If they
|
|
* don't match, we are likely racing with block
|
|
* allocation and mapping invalidation so just skip the
|
|
* update.
|
|
*/
|
|
if (pte_pfn(*pte) != pfn_t_to_pfn(pfn)) {
|
|
WARN_ON_ONCE(!is_zero_pfn(pte_pfn(*pte)));
|
|
goto out_unlock;
|
|
}
|
|
entry = pte_mkyoung(*pte);
|
|
entry = maybe_mkwrite(pte_mkdirty(entry),
|
|
vma->vm_flags);
|
|
if (ptep_set_access_flags(vma, addr, pte, entry, 1))
|
|
update_mmu_cache(vma, addr, pte);
|
|
}
|
|
goto out_unlock;
|
|
}
|
|
|
|
/* Ok, finally just insert the thing.. */
|
|
if (pfn_t_devmap(pfn))
|
|
entry = pte_mkdevmap(pfn_t_pte(pfn, prot));
|
|
else
|
|
entry = pte_mkspecial(pfn_t_pte(pfn, prot));
|
|
|
|
if (mkwrite) {
|
|
entry = pte_mkyoung(entry);
|
|
entry = maybe_mkwrite(pte_mkdirty(entry), vma->vm_flags);
|
|
}
|
|
|
|
set_pte_at(mm, addr, pte, entry);
|
|
update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
|
|
|
|
out_unlock:
|
|
pte_unmap_unlock(pte, ptl);
|
|
return VM_FAULT_NOPAGE;
|
|
}
|
|
|
|
/**
|
|
* vmf_insert_pfn_prot - insert single pfn into user vma with specified pgprot
|
|
* @vma: user vma to map to
|
|
* @addr: target user address of this page
|
|
* @pfn: source kernel pfn
|
|
* @pgprot: pgprot flags for the inserted page
|
|
*
|
|
* This is exactly like vmf_insert_pfn(), except that it allows drivers
|
|
* to override pgprot on a per-page basis.
|
|
*
|
|
* This only makes sense for IO mappings, and it makes no sense for
|
|
* COW mappings. In general, using multiple vmas is preferable;
|
|
* vmf_insert_pfn_prot should only be used if using multiple VMAs is
|
|
* impractical.
|
|
*
|
|
* See vmf_insert_mixed_prot() for a discussion of the implication of using
|
|
* a value of @pgprot different from that of @vma->vm_page_prot.
|
|
*
|
|
* Context: Process context. May allocate using %GFP_KERNEL.
|
|
* Return: vm_fault_t value.
|
|
*/
|
|
vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
|
|
unsigned long pfn, pgprot_t pgprot)
|
|
{
|
|
/*
|
|
* Technically, architectures with pte_special can avoid all these
|
|
* restrictions (same for remap_pfn_range). However we would like
|
|
* consistency in testing and feature parity among all, so we should
|
|
* try to keep these invariants in place for everybody.
|
|
*/
|
|
BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
|
|
BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
|
|
(VM_PFNMAP|VM_MIXEDMAP));
|
|
BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
|
|
BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
|
|
|
|
if (addr < vma->vm_start || addr >= vma->vm_end)
|
|
return VM_FAULT_SIGBUS;
|
|
|
|
if (!pfn_modify_allowed(pfn, pgprot))
|
|
return VM_FAULT_SIGBUS;
|
|
|
|
track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV));
|
|
|
|
return insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot,
|
|
false);
|
|
}
|
|
EXPORT_SYMBOL(vmf_insert_pfn_prot);
|
|
|
|
/**
|
|
* vmf_insert_pfn - insert single pfn into user vma
|
|
* @vma: user vma to map to
|
|
* @addr: target user address of this page
|
|
* @pfn: source kernel pfn
|
|
*
|
|
* Similar to vm_insert_page, this allows drivers to insert individual pages
|
|
* they've allocated into a user vma. Same comments apply.
|
|
*
|
|
* This function should only be called from a vm_ops->fault handler, and
|
|
* in that case the handler should return the result of this function.
|
|
*
|
|
* vma cannot be a COW mapping.
|
|
*
|
|
* As this is called only for pages that do not currently exist, we
|
|
* do not need to flush old virtual caches or the TLB.
|
|
*
|
|
* Context: Process context. May allocate using %GFP_KERNEL.
|
|
* Return: vm_fault_t value.
|
|
*/
|
|
vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
|
|
unsigned long pfn)
|
|
{
|
|
return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot);
|
|
}
|
|
EXPORT_SYMBOL(vmf_insert_pfn);
|
|
|
|
static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn)
|
|
{
|
|
/* these checks mirror the abort conditions in vm_normal_page */
|
|
if (vma->vm_flags & VM_MIXEDMAP)
|
|
return true;
|
|
if (pfn_t_devmap(pfn))
|
|
return true;
|
|
if (pfn_t_special(pfn))
|
|
return true;
|
|
if (is_zero_pfn(pfn_t_to_pfn(pfn)))
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma,
|
|
unsigned long addr, pfn_t pfn, pgprot_t pgprot,
|
|
bool mkwrite)
|
|
{
|
|
int err;
|
|
|
|
BUG_ON(!vm_mixed_ok(vma, pfn));
|
|
|
|
if (addr < vma->vm_start || addr >= vma->vm_end)
|
|
return VM_FAULT_SIGBUS;
|
|
|
|
track_pfn_insert(vma, &pgprot, pfn);
|
|
|
|
if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot))
|
|
return VM_FAULT_SIGBUS;
|
|
|
|
/*
|
|
* If we don't have pte special, then we have to use the pfn_valid()
|
|
* based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
|
|
* refcount the page if pfn_valid is true (hence insert_page rather
|
|
* than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP
|
|
* without pte special, it would there be refcounted as a normal page.
|
|
*/
|
|
if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) &&
|
|
!pfn_t_devmap(pfn) && pfn_t_valid(pfn)) {
|
|
struct page *page;
|
|
|
|
/*
|
|
* At this point we are committed to insert_page()
|
|
* regardless of whether the caller specified flags that
|
|
* result in pfn_t_has_page() == false.
|
|
*/
|
|
page = pfn_to_page(pfn_t_to_pfn(pfn));
|
|
err = insert_page(vma, addr, page, pgprot);
|
|
} else {
|
|
return insert_pfn(vma, addr, pfn, pgprot, mkwrite);
|
|
}
|
|
|
|
if (err == -ENOMEM)
|
|
return VM_FAULT_OOM;
|
|
if (err < 0 && err != -EBUSY)
|
|
return VM_FAULT_SIGBUS;
|
|
|
|
return VM_FAULT_NOPAGE;
|
|
}
|
|
|
|
/**
|
|
* vmf_insert_mixed_prot - insert single pfn into user vma with specified pgprot
|
|
* @vma: user vma to map to
|
|
* @addr: target user address of this page
|
|
* @pfn: source kernel pfn
|
|
* @pgprot: pgprot flags for the inserted page
|
|
*
|
|
* This is exactly like vmf_insert_mixed(), except that it allows drivers
|
|
* to override pgprot on a per-page basis.
|
|
*
|
|
* Typically this function should be used by drivers to set caching- and
|
|
* encryption bits different than those of @vma->vm_page_prot, because
|
|
* the caching- or encryption mode may not be known at mmap() time.
|
|
* This is ok as long as @vma->vm_page_prot is not used by the core vm
|
|
* to set caching and encryption bits for those vmas (except for COW pages).
|
|
* This is ensured by core vm only modifying these page table entries using
|
|
* functions that don't touch caching- or encryption bits, using pte_modify()
|
|
* if needed. (See for example mprotect()).
|
|
* Also when new page-table entries are created, this is only done using the
|
|
* fault() callback, and never using the value of vma->vm_page_prot,
|
|
* except for page-table entries that point to anonymous pages as the result
|
|
* of COW.
|
|
*
|
|
* Context: Process context. May allocate using %GFP_KERNEL.
|
|
* Return: vm_fault_t value.
|
|
*/
|
|
vm_fault_t vmf_insert_mixed_prot(struct vm_area_struct *vma, unsigned long addr,
|
|
pfn_t pfn, pgprot_t pgprot)
|
|
{
|
|
return __vm_insert_mixed(vma, addr, pfn, pgprot, false);
|
|
}
|
|
EXPORT_SYMBOL(vmf_insert_mixed_prot);
|
|
|
|
vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
|
|
pfn_t pfn)
|
|
{
|
|
return __vm_insert_mixed(vma, addr, pfn, vma->vm_page_prot, false);
|
|
}
|
|
EXPORT_SYMBOL(vmf_insert_mixed);
|
|
|
|
/*
|
|
* If the insertion of PTE failed because someone else already added a
|
|
* different entry in the mean time, we treat that as success as we assume
|
|
* the same entry was actually inserted.
|
|
*/
|
|
vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma,
|
|
unsigned long addr, pfn_t pfn)
|
|
{
|
|
return __vm_insert_mixed(vma, addr, pfn, vma->vm_page_prot, true);
|
|
}
|
|
EXPORT_SYMBOL(vmf_insert_mixed_mkwrite);
|
|
|
|
/*
|
|
* maps a range of physical memory into the requested pages. the old
|
|
* mappings are removed. any references to nonexistent pages results
|
|
* in null mappings (currently treated as "copy-on-access")
|
|
*/
|
|
static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
|
|
unsigned long addr, unsigned long end,
|
|
unsigned long pfn, pgprot_t prot)
|
|
{
|
|
pte_t *pte, *mapped_pte;
|
|
spinlock_t *ptl;
|
|
int err = 0;
|
|
|
|
mapped_pte = pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
|
|
if (!pte)
|
|
return -ENOMEM;
|
|
arch_enter_lazy_mmu_mode();
|
|
do {
|
|
BUG_ON(!pte_none(*pte));
|
|
if (!pfn_modify_allowed(pfn, prot)) {
|
|
err = -EACCES;
|
|
break;
|
|
}
|
|
set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
|
|
pfn++;
|
|
} while (pte++, addr += PAGE_SIZE, addr != end);
|
|
arch_leave_lazy_mmu_mode();
|
|
pte_unmap_unlock(mapped_pte, ptl);
|
|
return err;
|
|
}
|
|
|
|
static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
|
|
unsigned long addr, unsigned long end,
|
|
unsigned long pfn, pgprot_t prot)
|
|
{
|
|
pmd_t *pmd;
|
|
unsigned long next;
|
|
int err;
|
|
|
|
pfn -= addr >> PAGE_SHIFT;
|
|
pmd = pmd_alloc(mm, pud, addr);
|
|
if (!pmd)
|
|
return -ENOMEM;
|
|
VM_BUG_ON(pmd_trans_huge(*pmd));
|
|
do {
|
|
next = pmd_addr_end(addr, end);
|
|
err = remap_pte_range(mm, pmd, addr, next,
|
|
pfn + (addr >> PAGE_SHIFT), prot);
|
|
if (err)
|
|
return err;
|
|
} while (pmd++, addr = next, addr != end);
|
|
return 0;
|
|
}
|
|
|
|
static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d,
|
|
unsigned long addr, unsigned long end,
|
|
unsigned long pfn, pgprot_t prot)
|
|
{
|
|
pud_t *pud;
|
|
unsigned long next;
|
|
int err;
|
|
|
|
pfn -= addr >> PAGE_SHIFT;
|
|
pud = pud_alloc(mm, p4d, addr);
|
|
if (!pud)
|
|
return -ENOMEM;
|
|
do {
|
|
next = pud_addr_end(addr, end);
|
|
err = remap_pmd_range(mm, pud, addr, next,
|
|
pfn + (addr >> PAGE_SHIFT), prot);
|
|
if (err)
|
|
return err;
|
|
} while (pud++, addr = next, addr != end);
|
|
return 0;
|
|
}
|
|
|
|
static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd,
|
|
unsigned long addr, unsigned long end,
|
|
unsigned long pfn, pgprot_t prot)
|
|
{
|
|
p4d_t *p4d;
|
|
unsigned long next;
|
|
int err;
|
|
|
|
pfn -= addr >> PAGE_SHIFT;
|
|
p4d = p4d_alloc(mm, pgd, addr);
|
|
if (!p4d)
|
|
return -ENOMEM;
|
|
do {
|
|
next = p4d_addr_end(addr, end);
|
|
err = remap_pud_range(mm, p4d, addr, next,
|
|
pfn + (addr >> PAGE_SHIFT), prot);
|
|
if (err)
|
|
return err;
|
|
} while (p4d++, addr = next, addr != end);
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* remap_pfn_range - remap kernel memory to userspace
|
|
* @vma: user vma to map to
|
|
* @addr: target page aligned user address to start at
|
|
* @pfn: page frame number of kernel physical memory address
|
|
* @size: size of mapping area
|
|
* @prot: page protection flags for this mapping
|
|
*
|
|
* Note: this is only safe if the mm semaphore is held when called.
|
|
*
|
|
* Return: %0 on success, negative error code otherwise.
|
|
*/
|
|
int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
|
|
unsigned long pfn, unsigned long size, pgprot_t prot)
|
|
{
|
|
pgd_t *pgd;
|
|
unsigned long next;
|
|
unsigned long end = addr + PAGE_ALIGN(size);
|
|
struct mm_struct *mm = vma->vm_mm;
|
|
unsigned long remap_pfn = pfn;
|
|
int err;
|
|
|
|
if (WARN_ON_ONCE(!PAGE_ALIGNED(addr)))
|
|
return -EINVAL;
|
|
|
|
/*
|
|
* Physically remapped pages are special. Tell the
|
|
* rest of the world about it:
|
|
* VM_IO tells people not to look at these pages
|
|
* (accesses can have side effects).
|
|
* VM_PFNMAP tells the core MM that the base pages are just
|
|
* raw PFN mappings, and do not have a "struct page" associated
|
|
* with them.
|
|
* VM_DONTEXPAND
|
|
* Disable vma merging and expanding with mremap().
|
|
* VM_DONTDUMP
|
|
* Omit vma from core dump, even when VM_IO turned off.
|
|
*
|
|
* There's a horrible special case to handle copy-on-write
|
|
* behaviour that some programs depend on. We mark the "original"
|
|
* un-COW'ed pages by matching them up with "vma->vm_pgoff".
|
|
* See vm_normal_page() for details.
|
|
*/
|
|
if (is_cow_mapping(vma->vm_flags)) {
|
|
if (addr != vma->vm_start || end != vma->vm_end)
|
|
return -EINVAL;
|
|
vma->vm_pgoff = pfn;
|
|
}
|
|
|
|
err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size));
|
|
if (err)
|
|
return -EINVAL;
|
|
|
|
vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
|
|
|
|
BUG_ON(addr >= end);
|
|
pfn -= addr >> PAGE_SHIFT;
|
|
pgd = pgd_offset(mm, addr);
|
|
flush_cache_range(vma, addr, end);
|
|
do {
|
|
next = pgd_addr_end(addr, end);
|
|
err = remap_p4d_range(mm, pgd, addr, next,
|
|
pfn + (addr >> PAGE_SHIFT), prot);
|
|
if (err)
|
|
break;
|
|
} while (pgd++, addr = next, addr != end);
|
|
|
|
if (err)
|
|
untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size));
|
|
|
|
return err;
|
|
}
|
|
EXPORT_SYMBOL(remap_pfn_range);
|
|
|
|
/**
|
|
* vm_iomap_memory - remap memory to userspace
|
|
* @vma: user vma to map to
|
|
* @start: start of the physical memory to be mapped
|
|
* @len: size of area
|
|
*
|
|
* This is a simplified io_remap_pfn_range() for common driver use. The
|
|
* driver just needs to give us the physical memory range to be mapped,
|
|
* we'll figure out the rest from the vma information.
|
|
*
|
|
* NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
|
|
* whatever write-combining details or similar.
|
|
*
|
|
* Return: %0 on success, negative error code otherwise.
|
|
*/
|
|
int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
|
|
{
|
|
unsigned long vm_len, pfn, pages;
|
|
|
|
/* Check that the physical memory area passed in looks valid */
|
|
if (start + len < start)
|
|
return -EINVAL;
|
|
/*
|
|
* You *really* shouldn't map things that aren't page-aligned,
|
|
* but we've historically allowed it because IO memory might
|
|
* just have smaller alignment.
|
|
*/
|
|
len += start & ~PAGE_MASK;
|
|
pfn = start >> PAGE_SHIFT;
|
|
pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
|
|
if (pfn + pages < pfn)
|
|
return -EINVAL;
|
|
|
|
/* We start the mapping 'vm_pgoff' pages into the area */
|
|
if (vma->vm_pgoff > pages)
|
|
return -EINVAL;
|
|
pfn += vma->vm_pgoff;
|
|
pages -= vma->vm_pgoff;
|
|
|
|
/* Can we fit all of the mapping? */
|
|
vm_len = vma->vm_end - vma->vm_start;
|
|
if (vm_len >> PAGE_SHIFT > pages)
|
|
return -EINVAL;
|
|
|
|
/* Ok, let it rip */
|
|
return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
|
|
}
|
|
EXPORT_SYMBOL(vm_iomap_memory);
|
|
|
|
static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
|
|
unsigned long addr, unsigned long end,
|
|
pte_fn_t fn, void *data, bool create,
|
|
pgtbl_mod_mask *mask)
|
|
{
|
|
pte_t *pte;
|
|
int err = 0;
|
|
spinlock_t *ptl;
|
|
|
|
if (create) {
|
|
pte = (mm == &init_mm) ?
|
|
pte_alloc_kernel_track(pmd, addr, mask) :
|
|
pte_alloc_map_lock(mm, pmd, addr, &ptl);
|
|
if (!pte)
|
|
return -ENOMEM;
|
|
} else {
|
|
pte = (mm == &init_mm) ?
|
|
pte_offset_kernel(pmd, addr) :
|
|
pte_offset_map_lock(mm, pmd, addr, &ptl);
|
|
}
|
|
|
|
BUG_ON(pmd_huge(*pmd));
|
|
|
|
arch_enter_lazy_mmu_mode();
|
|
|
|
if (fn) {
|
|
do {
|
|
if (create || !pte_none(*pte)) {
|
|
err = fn(pte++, addr, data);
|
|
if (err)
|
|
break;
|
|
}
|
|
} while (addr += PAGE_SIZE, addr != end);
|
|
}
|
|
*mask |= PGTBL_PTE_MODIFIED;
|
|
|
|
arch_leave_lazy_mmu_mode();
|
|
|
|
if (mm != &init_mm)
|
|
pte_unmap_unlock(pte-1, ptl);
|
|
return err;
|
|
}
|
|
|
|
static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
|
|
unsigned long addr, unsigned long end,
|
|
pte_fn_t fn, void *data, bool create,
|
|
pgtbl_mod_mask *mask)
|
|
{
|
|
pmd_t *pmd;
|
|
unsigned long next;
|
|
int err = 0;
|
|
|
|
BUG_ON(pud_huge(*pud));
|
|
|
|
if (create) {
|
|
pmd = pmd_alloc_track(mm, pud, addr, mask);
|
|
if (!pmd)
|
|
return -ENOMEM;
|
|
} else {
|
|
pmd = pmd_offset(pud, addr);
|
|
}
|
|
do {
|
|
next = pmd_addr_end(addr, end);
|
|
if (create || !pmd_none_or_clear_bad(pmd)) {
|
|
err = apply_to_pte_range(mm, pmd, addr, next, fn, data,
|
|
create, mask);
|
|
if (err)
|
|
break;
|
|
}
|
|
} while (pmd++, addr = next, addr != end);
|
|
return err;
|
|
}
|
|
|
|
static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d,
|
|
unsigned long addr, unsigned long end,
|
|
pte_fn_t fn, void *data, bool create,
|
|
pgtbl_mod_mask *mask)
|
|
{
|
|
pud_t *pud;
|
|
unsigned long next;
|
|
int err = 0;
|
|
|
|
if (create) {
|
|
pud = pud_alloc_track(mm, p4d, addr, mask);
|
|
if (!pud)
|
|
return -ENOMEM;
|
|
} else {
|
|
pud = pud_offset(p4d, addr);
|
|
}
|
|
do {
|
|
next = pud_addr_end(addr, end);
|
|
if (create || !pud_none_or_clear_bad(pud)) {
|
|
err = apply_to_pmd_range(mm, pud, addr, next, fn, data,
|
|
create, mask);
|
|
if (err)
|
|
break;
|
|
}
|
|
} while (pud++, addr = next, addr != end);
|
|
return err;
|
|
}
|
|
|
|
static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd,
|
|
unsigned long addr, unsigned long end,
|
|
pte_fn_t fn, void *data, bool create,
|
|
pgtbl_mod_mask *mask)
|
|
{
|
|
p4d_t *p4d;
|
|
unsigned long next;
|
|
int err = 0;
|
|
|
|
if (create) {
|
|
p4d = p4d_alloc_track(mm, pgd, addr, mask);
|
|
if (!p4d)
|
|
return -ENOMEM;
|
|
} else {
|
|
p4d = p4d_offset(pgd, addr);
|
|
}
|
|
do {
|
|
next = p4d_addr_end(addr, end);
|
|
if (create || !p4d_none_or_clear_bad(p4d)) {
|
|
err = apply_to_pud_range(mm, p4d, addr, next, fn, data,
|
|
create, mask);
|
|
if (err)
|
|
break;
|
|
}
|
|
} while (p4d++, addr = next, addr != end);
|
|
return err;
|
|
}
|
|
|
|
static int __apply_to_page_range(struct mm_struct *mm, unsigned long addr,
|
|
unsigned long size, pte_fn_t fn,
|
|
void *data, bool create)
|
|
{
|
|
pgd_t *pgd;
|
|
unsigned long start = addr, next;
|
|
unsigned long end = addr + size;
|
|
pgtbl_mod_mask mask = 0;
|
|
int err = 0;
|
|
|
|
if (WARN_ON(addr >= end))
|
|
return -EINVAL;
|
|
|
|
pgd = pgd_offset(mm, addr);
|
|
do {
|
|
next = pgd_addr_end(addr, end);
|
|
if (!create && pgd_none_or_clear_bad(pgd))
|
|
continue;
|
|
err = apply_to_p4d_range(mm, pgd, addr, next, fn, data, create, &mask);
|
|
if (err)
|
|
break;
|
|
} while (pgd++, addr = next, addr != end);
|
|
|
|
if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
|
|
arch_sync_kernel_mappings(start, start + size);
|
|
|
|
return err;
|
|
}
|
|
|
|
/*
|
|
* Scan a region of virtual memory, filling in page tables as necessary
|
|
* and calling a provided function on each leaf page table.
|
|
*/
|
|
int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
|
|
unsigned long size, pte_fn_t fn, void *data)
|
|
{
|
|
return __apply_to_page_range(mm, addr, size, fn, data, true);
|
|
}
|
|
EXPORT_SYMBOL_GPL(apply_to_page_range);
|
|
|
|
#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
|
|
static bool pte_spinlock(struct vm_fault *vmf)
|
|
{
|
|
bool ret = false;
|
|
pmd_t pmdval;
|
|
|
|
/* Check if vma is still valid */
|
|
if (!(vmf->flags & FAULT_FLAG_SPECULATIVE)) {
|
|
vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
|
|
spin_lock(vmf->ptl);
|
|
return true;
|
|
}
|
|
|
|
local_irq_disable();
|
|
if (vma_has_changed(vmf)) {
|
|
trace_spf_vma_changed(_RET_IP_, vmf->vma, vmf->address);
|
|
goto out;
|
|
}
|
|
|
|
/*
|
|
* We check if the pmd value is still the same to ensure that there
|
|
* is not a huge collapse operation in progress in our back.
|
|
* It also ensures that pmd was not cleared by pmd_clear in
|
|
* free_pte_range and ptl is still valid.
|
|
*/
|
|
pmdval = READ_ONCE(*vmf->pmd);
|
|
if (!pmd_same(pmdval, vmf->orig_pmd)) {
|
|
trace_spf_pmd_changed(_RET_IP_, vmf->vma, vmf->address);
|
|
goto out;
|
|
}
|
|
|
|
vmf->ptl = pte_lockptr(vmf->vma->vm_mm, &pmdval);
|
|
if (unlikely(!spin_trylock(vmf->ptl))) {
|
|
trace_spf_pte_lock(_RET_IP_, vmf->vma, vmf->address);
|
|
goto out;
|
|
}
|
|
|
|
/*
|
|
* The check below will fail if pte_spinlock passed its ptl barrier
|
|
* before we took the ptl lock.
|
|
*/
|
|
if (vma_has_changed(vmf)) {
|
|
spin_unlock(vmf->ptl);
|
|
trace_spf_vma_changed(_RET_IP_, vmf->vma, vmf->address);
|
|
goto out;
|
|
}
|
|
|
|
ret = true;
|
|
out:
|
|
local_irq_enable();
|
|
return ret;
|
|
}
|
|
|
|
static bool __pte_map_lock_speculative(struct vm_fault *vmf, unsigned long addr)
|
|
{
|
|
bool ret = false;
|
|
pte_t *pte;
|
|
spinlock_t *ptl;
|
|
pmd_t pmdval;
|
|
|
|
/*
|
|
* The first vma_has_changed() guarantees the page-tables are still
|
|
* valid, having IRQs disabled ensures they stay around, hence the
|
|
* second vma_has_changed() to make sure they are still valid once
|
|
* we've got the lock. After that a concurrent zap_pte_range() will
|
|
* block on the PTL and thus we're safe.
|
|
*/
|
|
local_irq_disable();
|
|
if (vma_has_changed(vmf)) {
|
|
trace_spf_vma_changed(_RET_IP_, vmf->vma, addr);
|
|
goto out;
|
|
}
|
|
|
|
/*
|
|
* We check if the pmd value is still the same to ensure that there
|
|
* is not a huge collapse operation in progress in our back.
|
|
*/
|
|
pmdval = READ_ONCE(*vmf->pmd);
|
|
if (!pmd_same(pmdval, vmf->orig_pmd)) {
|
|
trace_spf_pmd_changed(_RET_IP_, vmf->vma, addr);
|
|
goto out;
|
|
}
|
|
|
|
/*
|
|
* Same as pte_offset_map_lock() except that we call
|
|
* spin_trylock() in place of spin_lock() to avoid race with
|
|
* unmap path which may have the lock and wait for this CPU
|
|
* to invalidate TLB but this CPU has irq disabled.
|
|
* Since we are in a speculative patch, accept it could fail
|
|
*/
|
|
ptl = pte_lockptr(vmf->vma->vm_mm, &pmdval);
|
|
pte = pte_offset_map(&pmdval, addr);
|
|
if (unlikely(!spin_trylock(ptl))) {
|
|
pte_unmap(pte);
|
|
trace_spf_pte_lock(_RET_IP_, vmf->vma, addr);
|
|
goto out;
|
|
}
|
|
|
|
/*
|
|
* The check below will fail if __pte_map_lock_speculative passed its ptl
|
|
* barrier before we took the ptl lock.
|
|
*/
|
|
if (vma_has_changed(vmf)) {
|
|
pte_unmap_unlock(pte, ptl);
|
|
trace_spf_vma_changed(_RET_IP_, vmf->vma, addr);
|
|
goto out;
|
|
}
|
|
|
|
vmf->pte = pte;
|
|
vmf->ptl = ptl;
|
|
ret = true;
|
|
out:
|
|
local_irq_enable();
|
|
return ret;
|
|
}
|
|
|
|
static bool pte_map_lock(struct vm_fault *vmf)
|
|
{
|
|
if (!(vmf->flags & FAULT_FLAG_SPECULATIVE)) {
|
|
vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd,
|
|
vmf->address, &vmf->ptl);
|
|
return true;
|
|
}
|
|
|
|
return __pte_map_lock_speculative(vmf, vmf->address);
|
|
}
|
|
|
|
bool pte_map_lock_addr(struct vm_fault *vmf, unsigned long addr)
|
|
{
|
|
if (!(vmf->flags & FAULT_FLAG_SPECULATIVE)) {
|
|
vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd,
|
|
addr, &vmf->ptl);
|
|
return true;
|
|
}
|
|
|
|
return __pte_map_lock_speculative(vmf, addr);
|
|
}
|
|
|
|
static bool __read_mostly allow_file_spec_access;
|
|
static int __init allow_file_spec_access_setup(char *str)
|
|
{
|
|
allow_file_spec_access = true;
|
|
return 1;
|
|
}
|
|
__setup("allow_file_spec_access", allow_file_spec_access_setup);
|
|
|
|
static bool vmf_allows_speculation(struct vm_fault *vmf)
|
|
{
|
|
if (vma_is_anonymous(vmf->vma)) {
|
|
/*
|
|
* __anon_vma_prepare() requires the mmap_sem to be held
|
|
* because vm_next and vm_prev must be safe. This can't be
|
|
* guaranteed in the speculative path.
|
|
*/
|
|
if (!vmf->vma->anon_vma) {
|
|
trace_spf_vma_notsup(_RET_IP_, vmf->vma, vmf->address);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
if (!allow_file_spec_access) {
|
|
/*
|
|
* Can't call vm_ops service has we don't know what they would
|
|
* do with the VMA.
|
|
* This include huge page from hugetlbfs.
|
|
*/
|
|
trace_spf_vma_notsup(_RET_IP_, vmf->vma, vmf->address);
|
|
return false;
|
|
}
|
|
|
|
if (!(vmf->vma->vm_flags & VM_SHARED) &&
|
|
(vmf->flags & FAULT_FLAG_WRITE) &&
|
|
!vmf->vma->anon_vma) {
|
|
/*
|
|
* non-anonymous private COW without anon_vma.
|
|
* See above.
|
|
*/
|
|
trace_spf_vma_notsup(_RET_IP_, vmf->vma, vmf->address);
|
|
return false;
|
|
}
|
|
|
|
if (vmf->vma->vm_ops->allow_speculation &&
|
|
vmf->vma->vm_ops->allow_speculation()) {
|
|
return true;
|
|
}
|
|
|
|
trace_spf_vma_notsup(_RET_IP_, vmf->vma, vmf->address);
|
|
return false;
|
|
}
|
|
|
|
#else
|
|
static inline bool pte_spinlock(struct vm_fault *vmf)
|
|
{
|
|
vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
|
|
spin_lock(vmf->ptl);
|
|
return true;
|
|
}
|
|
|
|
static inline bool pte_map_lock(struct vm_fault *vmf)
|
|
{
|
|
vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd,
|
|
vmf->address, &vmf->ptl);
|
|
return true;
|
|
}
|
|
|
|
inline bool pte_map_lock_addr(struct vm_fault *vmf, unsigned long addr)
|
|
{
|
|
vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd,
|
|
addr, &vmf->ptl);
|
|
return true;
|
|
}
|
|
|
|
static inline bool vmf_allows_speculation(struct vm_fault *vmf)
|
|
{
|
|
return false;
|
|
}
|
|
#endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
|
|
|
|
/*
|
|
* Scan a region of virtual memory, calling a provided function on
|
|
* each leaf page table where it exists.
|
|
*
|
|
* Unlike apply_to_page_range, this does _not_ fill in page tables
|
|
* where they are absent.
|
|
*/
|
|
int apply_to_existing_page_range(struct mm_struct *mm, unsigned long addr,
|
|
unsigned long size, pte_fn_t fn, void *data)
|
|
{
|
|
return __apply_to_page_range(mm, addr, size, fn, data, false);
|
|
}
|
|
EXPORT_SYMBOL_GPL(apply_to_existing_page_range);
|
|
|
|
/*
|
|
* handle_pte_fault chooses page fault handler according to an entry which was
|
|
* read non-atomically. Before making any commitment, on those architectures
|
|
* or configurations (e.g. i386 with PAE) which might give a mix of unmatched
|
|
* parts, do_swap_page must check under lock before unmapping the pte and
|
|
* proceeding (but do_wp_page is only called after already making such a check;
|
|
* and do_anonymous_page can safely check later on).
|
|
*
|
|
* pte_unmap_same() returns:
|
|
* 0 if the PTE are the same
|
|
* VM_FAULT_PTNOTSAME if the PTE are different
|
|
* VM_FAULT_RETRY if the VMA has changed in our back during
|
|
* a speculative page fault handling.
|
|
*/
|
|
static inline int pte_unmap_same(struct vm_fault *vmf)
|
|
{
|
|
int ret = 0;
|
|
|
|
#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
|
|
if (sizeof(pte_t) > sizeof(unsigned long)) {
|
|
if (pte_spinlock(vmf)) {
|
|
if (!pte_same(*vmf->pte, vmf->orig_pte))
|
|
ret = VM_FAULT_PTNOTSAME;
|
|
spin_unlock(vmf->ptl);
|
|
} else
|
|
ret = VM_FAULT_RETRY;
|
|
}
|
|
#endif
|
|
pte_unmap(vmf->pte);
|
|
return ret;
|
|
}
|
|
|
|
static inline bool cow_user_page(struct page *dst, struct page *src,
|
|
struct vm_fault *vmf)
|
|
{
|
|
bool ret;
|
|
void *kaddr;
|
|
void __user *uaddr;
|
|
bool locked = false;
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
struct mm_struct *mm = vma->vm_mm;
|
|
unsigned long addr = vmf->address;
|
|
|
|
if (likely(src)) {
|
|
copy_user_highpage(dst, src, addr, vma);
|
|
return true;
|
|
}
|
|
|
|
/*
|
|
* If the source page was a PFN mapping, we don't have
|
|
* a "struct page" for it. We do a best-effort copy by
|
|
* just copying from the original user address. If that
|
|
* fails, we just zero-fill it. Live with it.
|
|
*/
|
|
kaddr = kmap_atomic(dst);
|
|
uaddr = (void __user *)(addr & PAGE_MASK);
|
|
|
|
/*
|
|
* On architectures with software "accessed" bits, we would
|
|
* take a double page fault, so mark it accessed here.
|
|
*/
|
|
if (arch_faults_on_old_pte() && !pte_young(vmf->orig_pte)) {
|
|
pte_t entry;
|
|
|
|
vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl);
|
|
locked = true;
|
|
if (!likely(pte_same(*vmf->pte, vmf->orig_pte))) {
|
|
/*
|
|
* Other thread has already handled the fault
|
|
* and update local tlb only
|
|
*/
|
|
update_mmu_tlb(vma, addr, vmf->pte);
|
|
ret = false;
|
|
goto pte_unlock;
|
|
}
|
|
|
|
entry = pte_mkyoung(vmf->orig_pte);
|
|
if (ptep_set_access_flags(vma, addr, vmf->pte, entry, 0))
|
|
update_mmu_cache(vma, addr, vmf->pte);
|
|
}
|
|
|
|
/*
|
|
* This really shouldn't fail, because the page is there
|
|
* in the page tables. But it might just be unreadable,
|
|
* in which case we just give up and fill the result with
|
|
* zeroes.
|
|
*/
|
|
if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) {
|
|
if (locked)
|
|
goto warn;
|
|
|
|
/* Re-validate under PTL if the page is still mapped */
|
|
vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl);
|
|
locked = true;
|
|
if (!likely(pte_same(*vmf->pte, vmf->orig_pte))) {
|
|
/* The PTE changed under us, update local tlb */
|
|
update_mmu_tlb(vma, addr, vmf->pte);
|
|
ret = false;
|
|
goto pte_unlock;
|
|
}
|
|
|
|
/*
|
|
* The same page can be mapped back since last copy attempt.
|
|
* Try to copy again under PTL.
|
|
*/
|
|
if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) {
|
|
/*
|
|
* Give a warn in case there can be some obscure
|
|
* use-case
|
|
*/
|
|
warn:
|
|
WARN_ON_ONCE(1);
|
|
clear_page(kaddr);
|
|
}
|
|
}
|
|
|
|
ret = true;
|
|
|
|
pte_unlock:
|
|
if (locked)
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
kunmap_atomic(kaddr);
|
|
flush_dcache_page(dst);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma)
|
|
{
|
|
struct file *vm_file = vma->vm_file;
|
|
|
|
if (vm_file)
|
|
return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO;
|
|
|
|
/*
|
|
* Special mappings (e.g. VDSO) do not have any file so fake
|
|
* a default GFP_KERNEL for them.
|
|
*/
|
|
return GFP_KERNEL;
|
|
}
|
|
|
|
/*
|
|
* Notify the address space that the page is about to become writable so that
|
|
* it can prohibit this or wait for the page to get into an appropriate state.
|
|
*
|
|
* We do this without the lock held, so that it can sleep if it needs to.
|
|
*/
|
|
static vm_fault_t do_page_mkwrite(struct vm_fault *vmf)
|
|
{
|
|
vm_fault_t ret;
|
|
struct page *page = vmf->page;
|
|
unsigned int old_flags = vmf->flags;
|
|
|
|
vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
|
|
|
|
if (vmf->vma->vm_file &&
|
|
IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host))
|
|
return VM_FAULT_SIGBUS;
|
|
|
|
ret = vmf->vma->vm_ops->page_mkwrite(vmf);
|
|
/* Restore original flags so that caller is not surprised */
|
|
vmf->flags = old_flags;
|
|
if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
|
|
return ret;
|
|
if (unlikely(!(ret & VM_FAULT_LOCKED))) {
|
|
lock_page(page);
|
|
if (!page->mapping) {
|
|
unlock_page(page);
|
|
return 0; /* retry */
|
|
}
|
|
ret |= VM_FAULT_LOCKED;
|
|
} else
|
|
VM_BUG_ON_PAGE(!PageLocked(page), page);
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* Handle dirtying of a page in shared file mapping on a write fault.
|
|
*
|
|
* The function expects the page to be locked and unlocks it.
|
|
*/
|
|
static vm_fault_t fault_dirty_shared_page(struct vm_fault *vmf)
|
|
{
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
struct address_space *mapping;
|
|
struct page *page = vmf->page;
|
|
bool dirtied;
|
|
bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite;
|
|
|
|
dirtied = set_page_dirty(page);
|
|
VM_BUG_ON_PAGE(PageAnon(page), page);
|
|
/*
|
|
* Take a local copy of the address_space - page.mapping may be zeroed
|
|
* by truncate after unlock_page(). The address_space itself remains
|
|
* pinned by vma->vm_file's reference. We rely on unlock_page()'s
|
|
* release semantics to prevent the compiler from undoing this copying.
|
|
*/
|
|
mapping = page_rmapping(page);
|
|
unlock_page(page);
|
|
|
|
if (!page_mkwrite)
|
|
file_update_time(vma->vm_file);
|
|
|
|
/*
|
|
* Throttle page dirtying rate down to writeback speed.
|
|
*
|
|
* mapping may be NULL here because some device drivers do not
|
|
* set page.mapping but still dirty their pages
|
|
*
|
|
* Drop the mmap_lock before waiting on IO, if we can. The file
|
|
* is pinning the mapping, as per above.
|
|
*/
|
|
if ((dirtied || page_mkwrite) && mapping) {
|
|
struct file *fpin;
|
|
|
|
fpin = maybe_unlock_mmap_for_io(vmf, NULL);
|
|
balance_dirty_pages_ratelimited(mapping);
|
|
if (fpin) {
|
|
fput(fpin);
|
|
return VM_FAULT_RETRY;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Handle write page faults for pages that can be reused in the current vma
|
|
*
|
|
* This can happen either due to the mapping being with the VM_SHARED flag,
|
|
* or due to us being the last reference standing to the page. In either
|
|
* case, all we need to do here is to mark the page as writable and update
|
|
* any related book-keeping.
|
|
*/
|
|
static inline void wp_page_reuse(struct vm_fault *vmf)
|
|
__releases(vmf->ptl)
|
|
{
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
struct page *page = vmf->page;
|
|
pte_t entry;
|
|
/*
|
|
* Clear the pages cpupid information as the existing
|
|
* information potentially belongs to a now completely
|
|
* unrelated process.
|
|
*/
|
|
if (page)
|
|
page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1);
|
|
|
|
flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
|
|
entry = pte_mkyoung(vmf->orig_pte);
|
|
entry = maybe_mkwrite(pte_mkdirty(entry), vmf->vma_flags);
|
|
if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1))
|
|
update_mmu_cache(vma, vmf->address, vmf->pte);
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
count_vm_event(PGREUSE);
|
|
}
|
|
|
|
/*
|
|
* Handle the case of a page which we actually need to copy to a new page.
|
|
*
|
|
* Called with mmap_lock locked and the old page referenced, but
|
|
* without the ptl held.
|
|
*
|
|
* High level logic flow:
|
|
*
|
|
* - Allocate a page, copy the content of the old page to the new one.
|
|
* - Handle book keeping and accounting - cgroups, mmu-notifiers, etc.
|
|
* - Take the PTL. If the pte changed, bail out and release the allocated page
|
|
* - If the pte is still the way we remember it, update the page table and all
|
|
* relevant references. This includes dropping the reference the page-table
|
|
* held to the old page, as well as updating the rmap.
|
|
* - In any case, unlock the PTL and drop the reference we took to the old page.
|
|
*/
|
|
static vm_fault_t wp_page_copy(struct vm_fault *vmf)
|
|
{
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
struct mm_struct *mm = vma->vm_mm;
|
|
struct page *old_page = vmf->page;
|
|
struct page *new_page = NULL;
|
|
pte_t entry;
|
|
int page_copied = 0;
|
|
struct mmu_notifier_range range;
|
|
vm_fault_t ret = VM_FAULT_OOM;
|
|
|
|
if (unlikely(anon_vma_prepare(vma)))
|
|
goto out;
|
|
|
|
if (is_zero_pfn(pte_pfn(vmf->orig_pte))) {
|
|
new_page = alloc_zeroed_user_highpage_movable(vma,
|
|
vmf->address);
|
|
if (!new_page)
|
|
goto out;
|
|
} else {
|
|
new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
|
|
vmf->address);
|
|
if (!new_page)
|
|
goto out;
|
|
|
|
if (!cow_user_page(new_page, old_page, vmf)) {
|
|
/*
|
|
* COW failed, if the fault was solved by other,
|
|
* it's fine. If not, userspace would re-fault on
|
|
* the same address and we will handle the fault
|
|
* from the second attempt.
|
|
*/
|
|
put_page(new_page);
|
|
if (old_page)
|
|
put_page(old_page);
|
|
return 0;
|
|
}
|
|
trace_android_vh_cow_user_page(vmf, new_page);
|
|
}
|
|
|
|
if (mem_cgroup_charge(new_page, mm, GFP_KERNEL))
|
|
goto out_free_new;
|
|
cgroup_throttle_swaprate(new_page, GFP_KERNEL);
|
|
|
|
__SetPageUptodate(new_page);
|
|
|
|
mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
|
|
vmf->address & PAGE_MASK,
|
|
(vmf->address & PAGE_MASK) + PAGE_SIZE);
|
|
mmu_notifier_invalidate_range_start(&range);
|
|
|
|
/*
|
|
* Re-check the pte - we dropped the lock
|
|
*/
|
|
if (!pte_map_lock(vmf)) {
|
|
ret = VM_FAULT_RETRY;
|
|
goto out_invalidate_end;
|
|
}
|
|
if (likely(pte_same(*vmf->pte, vmf->orig_pte))) {
|
|
if (old_page) {
|
|
if (!PageAnon(old_page)) {
|
|
dec_mm_counter_fast(mm,
|
|
mm_counter_file(old_page));
|
|
inc_mm_counter_fast(mm, MM_ANONPAGES);
|
|
}
|
|
} else {
|
|
inc_mm_counter_fast(mm, MM_ANONPAGES);
|
|
}
|
|
flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
|
|
entry = mk_pte(new_page, vmf->vma_page_prot);
|
|
entry = pte_sw_mkyoung(entry);
|
|
entry = maybe_mkwrite(pte_mkdirty(entry), vmf->vma_flags);
|
|
/*
|
|
* Clear the pte entry and flush it first, before updating the
|
|
* pte with the new entry. This will avoid a race condition
|
|
* seen in the presence of one thread doing SMC and another
|
|
* thread doing COW.
|
|
*/
|
|
ptep_clear_flush_notify(vma, vmf->address, vmf->pte);
|
|
__page_add_new_anon_rmap(new_page, vma, vmf->address, false);
|
|
__lru_cache_add_inactive_or_unevictable(new_page, vmf->vma_flags);
|
|
/*
|
|
* We call the notify macro here because, when using secondary
|
|
* mmu page tables (such as kvm shadow page tables), we want the
|
|
* new page to be mapped directly into the secondary page table.
|
|
*/
|
|
set_pte_at_notify(mm, vmf->address, vmf->pte, entry);
|
|
update_mmu_cache(vma, vmf->address, vmf->pte);
|
|
if (old_page) {
|
|
/*
|
|
* Only after switching the pte to the new page may
|
|
* we remove the mapcount here. Otherwise another
|
|
* process may come and find the rmap count decremented
|
|
* before the pte is switched to the new page, and
|
|
* "reuse" the old page writing into it while our pte
|
|
* here still points into it and can be read by other
|
|
* threads.
|
|
*
|
|
* The critical issue is to order this
|
|
* page_remove_rmap with the ptp_clear_flush above.
|
|
* Those stores are ordered by (if nothing else,)
|
|
* the barrier present in the atomic_add_negative
|
|
* in page_remove_rmap.
|
|
*
|
|
* Then the TLB flush in ptep_clear_flush ensures that
|
|
* no process can access the old page before the
|
|
* decremented mapcount is visible. And the old page
|
|
* cannot be reused until after the decremented
|
|
* mapcount is visible. So transitively, TLBs to
|
|
* old page will be flushed before it can be reused.
|
|
*/
|
|
page_remove_rmap(old_page, false);
|
|
}
|
|
|
|
/* Free the old page.. */
|
|
new_page = old_page;
|
|
page_copied = 1;
|
|
} else {
|
|
update_mmu_tlb(vma, vmf->address, vmf->pte);
|
|
}
|
|
|
|
if (new_page)
|
|
put_page(new_page);
|
|
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
/*
|
|
* No need to double call mmu_notifier->invalidate_range() callback as
|
|
* the above ptep_clear_flush_notify() did already call it.
|
|
*/
|
|
mmu_notifier_invalidate_range_only_end(&range);
|
|
if (old_page) {
|
|
/*
|
|
* Don't let another task, with possibly unlocked vma,
|
|
* keep the mlocked page.
|
|
*/
|
|
if (page_copied && (vmf->vma_flags & VM_LOCKED)) {
|
|
lock_page(old_page); /* LRU manipulation */
|
|
if (PageMlocked(old_page))
|
|
munlock_vma_page(old_page);
|
|
unlock_page(old_page);
|
|
}
|
|
put_page(old_page);
|
|
}
|
|
return page_copied ? VM_FAULT_WRITE : 0;
|
|
out_invalidate_end:
|
|
mmu_notifier_invalidate_range_only_end(&range);
|
|
out_free_new:
|
|
put_page(new_page);
|
|
out:
|
|
if (old_page)
|
|
put_page(old_page);
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* finish_mkwrite_fault - finish page fault for a shared mapping, making PTE
|
|
* writeable once the page is prepared
|
|
*
|
|
* @vmf: structure describing the fault
|
|
*
|
|
* This function handles all that is needed to finish a write page fault in a
|
|
* shared mapping due to PTE being read-only once the mapped page is prepared.
|
|
* It handles locking of PTE and modifying it.
|
|
*
|
|
* The function expects the page to be locked or other protection against
|
|
* concurrent faults / writeback (such as DAX radix tree locks).
|
|
*
|
|
* Return: %VM_FAULT_WRITE on success, %0 when PTE got changed before
|
|
* we acquired PTE lock.
|
|
*/
|
|
vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf)
|
|
{
|
|
WARN_ON_ONCE(!(vmf->vma_flags & VM_SHARED));
|
|
if (!pte_map_lock(vmf))
|
|
return VM_FAULT_RETRY;
|
|
/*
|
|
* We might have raced with another page fault while we released the
|
|
* pte_offset_map_lock.
|
|
*/
|
|
if (!pte_same(*vmf->pte, vmf->orig_pte)) {
|
|
update_mmu_tlb(vmf->vma, vmf->address, vmf->pte);
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
return VM_FAULT_NOPAGE;
|
|
}
|
|
wp_page_reuse(vmf);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED
|
|
* mapping
|
|
*/
|
|
static vm_fault_t wp_pfn_shared(struct vm_fault *vmf)
|
|
{
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
|
|
if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) {
|
|
vm_fault_t ret;
|
|
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
vmf->flags |= FAULT_FLAG_MKWRITE;
|
|
ret = vma->vm_ops->pfn_mkwrite(vmf);
|
|
if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))
|
|
return ret;
|
|
return finish_mkwrite_fault(vmf);
|
|
}
|
|
wp_page_reuse(vmf);
|
|
return VM_FAULT_WRITE;
|
|
}
|
|
|
|
static vm_fault_t wp_page_shared(struct vm_fault *vmf)
|
|
__releases(vmf->ptl)
|
|
{
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
vm_fault_t ret = VM_FAULT_WRITE;
|
|
|
|
get_page(vmf->page);
|
|
|
|
if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
|
|
vm_fault_t tmp;
|
|
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
tmp = do_page_mkwrite(vmf);
|
|
if (unlikely(!tmp || (tmp &
|
|
(VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
|
|
put_page(vmf->page);
|
|
return tmp;
|
|
}
|
|
tmp = finish_mkwrite_fault(vmf);
|
|
if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
|
|
unlock_page(vmf->page);
|
|
put_page(vmf->page);
|
|
return tmp;
|
|
}
|
|
} else {
|
|
wp_page_reuse(vmf);
|
|
lock_page(vmf->page);
|
|
}
|
|
ret |= fault_dirty_shared_page(vmf);
|
|
put_page(vmf->page);
|
|
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* This routine handles present pages, when users try to write
|
|
* to a shared page. It is done by copying the page to a new address
|
|
* and decrementing the shared-page counter for the old page.
|
|
*
|
|
* Note that this routine assumes that the protection checks have been
|
|
* done by the caller (the low-level page fault routine in most cases).
|
|
* Thus we can safely just mark it writable once we've done any necessary
|
|
* COW.
|
|
*
|
|
* We also mark the page dirty at this point even though the page will
|
|
* change only once the write actually happens. This avoids a few races,
|
|
* and potentially makes it more efficient.
|
|
*
|
|
* We enter with non-exclusive mmap_lock (to exclude vma changes,
|
|
* but allow concurrent faults), with pte both mapped and locked.
|
|
* We return with mmap_lock still held, but pte unmapped and unlocked.
|
|
*/
|
|
static vm_fault_t do_wp_page(struct vm_fault *vmf)
|
|
__releases(vmf->ptl)
|
|
{
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
|
|
if (userfaultfd_pte_wp(vma, *vmf->pte)) {
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
if (vmf->flags & FAULT_FLAG_SPECULATIVE)
|
|
return VM_FAULT_RETRY;
|
|
return handle_userfault(vmf, VM_UFFD_WP);
|
|
}
|
|
|
|
/*
|
|
* Userfaultfd write-protect can defer flushes. Ensure the TLB
|
|
* is flushed in this case before copying.
|
|
*/
|
|
if (unlikely(userfaultfd_wp(vmf->vma) &&
|
|
mm_tlb_flush_pending(vmf->vma->vm_mm)))
|
|
flush_tlb_page(vmf->vma, vmf->address);
|
|
|
|
vmf->page = _vm_normal_page(vma, vmf->address, vmf->orig_pte,
|
|
vmf->vma_flags);
|
|
if (!vmf->page) {
|
|
/*
|
|
* VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
|
|
* VM_PFNMAP VMA.
|
|
*
|
|
* We should not cow pages in a shared writeable mapping.
|
|
* Just mark the pages writable and/or call ops->pfn_mkwrite.
|
|
*/
|
|
if ((vmf->vma_flags & (VM_WRITE|VM_SHARED)) ==
|
|
(VM_WRITE|VM_SHARED))
|
|
return wp_pfn_shared(vmf);
|
|
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
return wp_page_copy(vmf);
|
|
}
|
|
|
|
/*
|
|
* Take out anonymous pages first, anonymous shared vmas are
|
|
* not dirty accountable.
|
|
*/
|
|
if (PageAnon(vmf->page)) {
|
|
struct page *page = vmf->page;
|
|
|
|
/* PageKsm() doesn't necessarily raise the page refcount */
|
|
if (PageKsm(page) || page_count(page) != 1)
|
|
goto copy;
|
|
if (!trylock_page(page))
|
|
goto copy;
|
|
if (PageKsm(page) || page_mapcount(page) != 1 || page_count(page) != 1) {
|
|
unlock_page(page);
|
|
goto copy;
|
|
}
|
|
/*
|
|
* Ok, we've got the only map reference, and the only
|
|
* page count reference, and the page is locked,
|
|
* it's dark out, and we're wearing sunglasses. Hit it.
|
|
*/
|
|
unlock_page(page);
|
|
wp_page_reuse(vmf);
|
|
return VM_FAULT_WRITE;
|
|
} else if (unlikely((vmf->vma_flags & (VM_WRITE|VM_SHARED)) ==
|
|
(VM_WRITE|VM_SHARED))) {
|
|
return wp_page_shared(vmf);
|
|
}
|
|
copy:
|
|
/*
|
|
* Ok, we need to copy. Oh, well..
|
|
*/
|
|
get_page(vmf->page);
|
|
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
return wp_page_copy(vmf);
|
|
}
|
|
|
|
static void unmap_mapping_range_vma(struct vm_area_struct *vma,
|
|
unsigned long start_addr, unsigned long end_addr,
|
|
struct zap_details *details)
|
|
{
|
|
zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
|
|
}
|
|
|
|
static inline void unmap_mapping_range_tree(struct rb_root_cached *root,
|
|
struct zap_details *details)
|
|
{
|
|
struct vm_area_struct *vma;
|
|
pgoff_t vba, vea, zba, zea;
|
|
|
|
vma_interval_tree_foreach(vma, root,
|
|
details->first_index, details->last_index) {
|
|
|
|
vba = vma->vm_pgoff;
|
|
vea = vba + vma_pages(vma) - 1;
|
|
zba = details->first_index;
|
|
if (zba < vba)
|
|
zba = vba;
|
|
zea = details->last_index;
|
|
if (zea > vea)
|
|
zea = vea;
|
|
|
|
unmap_mapping_range_vma(vma,
|
|
((zba - vba) << PAGE_SHIFT) + vma->vm_start,
|
|
((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
|
|
details);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* unmap_mapping_page() - Unmap single page from processes.
|
|
* @page: The locked page to be unmapped.
|
|
*
|
|
* Unmap this page from any userspace process which still has it mmaped.
|
|
* Typically, for efficiency, the range of nearby pages has already been
|
|
* unmapped by unmap_mapping_pages() or unmap_mapping_range(). But once
|
|
* truncation or invalidation holds the lock on a page, it may find that
|
|
* the page has been remapped again: and then uses unmap_mapping_page()
|
|
* to unmap it finally.
|
|
*/
|
|
void unmap_mapping_page(struct page *page)
|
|
{
|
|
struct address_space *mapping = page->mapping;
|
|
struct zap_details details = { };
|
|
|
|
VM_BUG_ON(!PageLocked(page));
|
|
VM_BUG_ON(PageTail(page));
|
|
|
|
details.check_mapping = mapping;
|
|
details.first_index = page->index;
|
|
details.last_index = page->index + thp_nr_pages(page) - 1;
|
|
details.single_page = page;
|
|
|
|
i_mmap_lock_write(mapping);
|
|
if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)))
|
|
unmap_mapping_range_tree(&mapping->i_mmap, &details);
|
|
i_mmap_unlock_write(mapping);
|
|
}
|
|
|
|
/**
|
|
* unmap_mapping_pages() - Unmap pages from processes.
|
|
* @mapping: The address space containing pages to be unmapped.
|
|
* @start: Index of first page to be unmapped.
|
|
* @nr: Number of pages to be unmapped. 0 to unmap to end of file.
|
|
* @even_cows: Whether to unmap even private COWed pages.
|
|
*
|
|
* Unmap the pages in this address space from any userspace process which
|
|
* has them mmaped. Generally, you want to remove COWed pages as well when
|
|
* a file is being truncated, but not when invalidating pages from the page
|
|
* cache.
|
|
*/
|
|
void unmap_mapping_pages(struct address_space *mapping, pgoff_t start,
|
|
pgoff_t nr, bool even_cows)
|
|
{
|
|
struct zap_details details = { };
|
|
|
|
details.check_mapping = even_cows ? NULL : mapping;
|
|
details.first_index = start;
|
|
details.last_index = start + nr - 1;
|
|
if (details.last_index < details.first_index)
|
|
details.last_index = ULONG_MAX;
|
|
|
|
i_mmap_lock_write(mapping);
|
|
if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)))
|
|
unmap_mapping_range_tree(&mapping->i_mmap, &details);
|
|
i_mmap_unlock_write(mapping);
|
|
}
|
|
|
|
/**
|
|
* unmap_mapping_range - unmap the portion of all mmaps in the specified
|
|
* address_space corresponding to the specified byte range in the underlying
|
|
* file.
|
|
*
|
|
* @mapping: the address space containing mmaps to be unmapped.
|
|
* @holebegin: byte in first page to unmap, relative to the start of
|
|
* the underlying file. This will be rounded down to a PAGE_SIZE
|
|
* boundary. Note that this is different from truncate_pagecache(), which
|
|
* must keep the partial page. In contrast, we must get rid of
|
|
* partial pages.
|
|
* @holelen: size of prospective hole in bytes. This will be rounded
|
|
* up to a PAGE_SIZE boundary. A holelen of zero truncates to the
|
|
* end of the file.
|
|
* @even_cows: 1 when truncating a file, unmap even private COWed pages;
|
|
* but 0 when invalidating pagecache, don't throw away private data.
|
|
*/
|
|
void unmap_mapping_range(struct address_space *mapping,
|
|
loff_t const holebegin, loff_t const holelen, int even_cows)
|
|
{
|
|
pgoff_t hba = (pgoff_t)(holebegin) >> PAGE_SHIFT;
|
|
pgoff_t hlen = ((pgoff_t)(holelen) + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
|
|
|
/* Check for overflow. */
|
|
if (sizeof(holelen) > sizeof(hlen)) {
|
|
long long holeend =
|
|
(holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
|
if (holeend & ~(long long)ULONG_MAX)
|
|
hlen = ULONG_MAX - hba + 1;
|
|
}
|
|
|
|
unmap_mapping_pages(mapping, hba, hlen, even_cows);
|
|
}
|
|
EXPORT_SYMBOL(unmap_mapping_range);
|
|
|
|
/*
|
|
* We enter with non-exclusive mmap_lock (to exclude vma changes,
|
|
* but allow concurrent faults), and pte mapped but not yet locked.
|
|
* We return with pte unmapped and unlocked.
|
|
*
|
|
* We return with the mmap_lock locked or unlocked in the same cases
|
|
* as does filemap_fault().
|
|
*/
|
|
vm_fault_t do_swap_page(struct vm_fault *vmf)
|
|
{
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
struct page *page = NULL, *swapcache;
|
|
swp_entry_t entry;
|
|
pte_t pte;
|
|
int locked;
|
|
int exclusive = 0;
|
|
vm_fault_t ret;
|
|
void *shadow = NULL;
|
|
|
|
if (vmf->flags & FAULT_FLAG_SPECULATIVE) {
|
|
bool allow_swap_spf = false;
|
|
|
|
/* ksm_might_need_to_copy() needs a stable VMA, spf can't be used */
|
|
#ifndef CONFIG_KSM
|
|
trace_android_vh_do_swap_page_spf(&allow_swap_spf);
|
|
#endif
|
|
if (!allow_swap_spf) {
|
|
pte_unmap(vmf->pte);
|
|
return VM_FAULT_RETRY;
|
|
}
|
|
}
|
|
|
|
ret = pte_unmap_same(vmf);
|
|
if (ret) {
|
|
/*
|
|
* If pte != orig_pte, this means another thread did the
|
|
* swap operation in our back.
|
|
* So nothing else to do.
|
|
*/
|
|
if (ret == VM_FAULT_PTNOTSAME)
|
|
ret = 0;
|
|
goto out;
|
|
}
|
|
|
|
entry = pte_to_swp_entry(vmf->orig_pte);
|
|
if (unlikely(non_swap_entry(entry))) {
|
|
if (vmf->flags & FAULT_FLAG_SPECULATIVE) {
|
|
ret = VM_FAULT_RETRY;
|
|
goto out;
|
|
}
|
|
if (is_migration_entry(entry)) {
|
|
migration_entry_wait(vma->vm_mm, vmf->pmd,
|
|
vmf->address);
|
|
} else if (is_device_private_entry(entry)) {
|
|
vmf->page = device_private_entry_to_page(entry);
|
|
ret = vmf->page->pgmap->ops->migrate_to_ram(vmf);
|
|
} else if (is_hwpoison_entry(entry)) {
|
|
ret = VM_FAULT_HWPOISON;
|
|
} else {
|
|
print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL);
|
|
ret = VM_FAULT_SIGBUS;
|
|
}
|
|
goto out;
|
|
}
|
|
|
|
|
|
delayacct_set_flag(DELAYACCT_PF_SWAPIN);
|
|
page = lookup_swap_cache(entry, vma, vmf->address);
|
|
swapcache = page;
|
|
|
|
if (!page) {
|
|
struct swap_info_struct *si = swp_swap_info(entry);
|
|
bool skip_swapcache = false;
|
|
|
|
trace_android_vh_skip_swapcache(entry, &skip_swapcache);
|
|
if ((data_race(si->flags & SWP_SYNCHRONOUS_IO) || skip_swapcache) &&
|
|
__swap_count(entry) == 1) {
|
|
/* skip swapcache */
|
|
gfp_t flags = GFP_HIGHUSER_MOVABLE;
|
|
|
|
trace_android_rvh_set_skip_swapcache_flags(&flags);
|
|
page = alloc_page_vma(flags, vma, vmf->address);
|
|
if (page) {
|
|
int err;
|
|
|
|
__SetPageLocked(page);
|
|
__SetPageSwapBacked(page);
|
|
set_page_private(page, entry.val);
|
|
|
|
/* Tell memcg to use swap ownership records */
|
|
SetPageSwapCache(page);
|
|
err = mem_cgroup_charge(page, vma->vm_mm,
|
|
GFP_KERNEL);
|
|
ClearPageSwapCache(page);
|
|
if (err) {
|
|
ret = VM_FAULT_OOM;
|
|
goto out_page;
|
|
}
|
|
|
|
shadow = get_shadow_from_swap_cache(entry);
|
|
if (shadow)
|
|
workingset_refault(page, shadow);
|
|
|
|
lru_cache_add(page);
|
|
swap_readpage(page, true);
|
|
}
|
|
} else if (vmf->flags & FAULT_FLAG_SPECULATIVE) {
|
|
/*
|
|
* Don't try readahead during a speculative page fault
|
|
* as the VMA's boundaries may change in our back.
|
|
* If the page is not in the swap cache and synchronous
|
|
* read is disabled, fall back to the regular page fault
|
|
* mechanism.
|
|
*/
|
|
delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
|
|
ret = VM_FAULT_RETRY;
|
|
goto out;
|
|
} else {
|
|
page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
|
|
vmf);
|
|
swapcache = page;
|
|
}
|
|
|
|
if (!page) {
|
|
/*
|
|
* Back out if the VMA has changed in our back during
|
|
* a speculative page fault or if somebody else
|
|
* faulted in this pte while we released the pte lock.
|
|
*/
|
|
if (!pte_map_lock(vmf)) {
|
|
delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
|
|
ret = VM_FAULT_RETRY;
|
|
goto out;
|
|
}
|
|
|
|
if (likely(pte_same(*vmf->pte, vmf->orig_pte)))
|
|
ret = VM_FAULT_OOM;
|
|
delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
|
|
goto unlock;
|
|
}
|
|
|
|
/* Had to read the page from swap area: Major fault */
|
|
ret = VM_FAULT_MAJOR;
|
|
count_vm_event(PGMAJFAULT);
|
|
count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
|
|
} else if (PageHWPoison(page)) {
|
|
/*
|
|
* hwpoisoned dirty swapcache pages are kept for killing
|
|
* owner processes (which may be unknown at hwpoison time)
|
|
*/
|
|
ret = VM_FAULT_HWPOISON;
|
|
delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
|
|
goto out_release;
|
|
}
|
|
|
|
locked = lock_page_or_retry(page, vma->vm_mm, vmf->flags);
|
|
|
|
delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
|
|
if (!locked) {
|
|
ret |= VM_FAULT_RETRY;
|
|
goto out_release;
|
|
}
|
|
|
|
/*
|
|
* Make sure try_to_free_swap or reuse_swap_page or swapoff did not
|
|
* release the swapcache from under us. The page pin, and pte_same
|
|
* test below, are not enough to exclude that. Even if it is still
|
|
* swapcache, we need to check that the page's swap has not changed.
|
|
*/
|
|
if (unlikely((!PageSwapCache(page) ||
|
|
page_private(page) != entry.val)) && swapcache)
|
|
goto out_page;
|
|
|
|
page = ksm_might_need_to_copy(page, vma, vmf->address);
|
|
if (unlikely(!page)) {
|
|
ret = VM_FAULT_OOM;
|
|
page = swapcache;
|
|
goto out_page;
|
|
}
|
|
|
|
cgroup_throttle_swaprate(page, GFP_KERNEL);
|
|
|
|
/*
|
|
* Back out if the VMA has changed in our back during a speculative
|
|
* page fault or if somebody else already faulted in this pte.
|
|
*/
|
|
if (!pte_map_lock(vmf)) {
|
|
ret = VM_FAULT_RETRY;
|
|
goto out_page;
|
|
}
|
|
if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte)))
|
|
goto out_nomap;
|
|
|
|
if (unlikely(!PageUptodate(page))) {
|
|
ret = VM_FAULT_SIGBUS;
|
|
goto out_nomap;
|
|
}
|
|
|
|
/*
|
|
* The page isn't present yet, go ahead with the fault.
|
|
*
|
|
* Be careful about the sequence of operations here.
|
|
* To get its accounting right, reuse_swap_page() must be called
|
|
* while the page is counted on swap but not yet in mapcount i.e.
|
|
* before page_add_anon_rmap() and swap_free(); try_to_free_swap()
|
|
* must be called after the swap_free(), or it will never succeed.
|
|
*/
|
|
|
|
inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
|
|
dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS);
|
|
pte = mk_pte(page, vmf->vma_page_prot);
|
|
if ((vmf->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) {
|
|
pte = maybe_mkwrite(pte_mkdirty(pte), vmf->vma_flags);
|
|
vmf->flags &= ~FAULT_FLAG_WRITE;
|
|
ret |= VM_FAULT_WRITE;
|
|
exclusive = RMAP_EXCLUSIVE;
|
|
}
|
|
flush_icache_page(vma, page);
|
|
if (pte_swp_soft_dirty(vmf->orig_pte))
|
|
pte = pte_mksoft_dirty(pte);
|
|
if (pte_swp_uffd_wp(vmf->orig_pte)) {
|
|
pte = pte_mkuffd_wp(pte);
|
|
pte = pte_wrprotect(pte);
|
|
}
|
|
set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte);
|
|
arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte);
|
|
vmf->orig_pte = pte;
|
|
|
|
/* ksm created a completely new copy */
|
|
if (unlikely(page != swapcache && swapcache)) {
|
|
__page_add_new_anon_rmap(page, vma, vmf->address, false);
|
|
__lru_cache_add_inactive_or_unevictable(page, vmf->vma_flags);
|
|
} else {
|
|
do_page_add_anon_rmap(page, vma, vmf->address, exclusive);
|
|
}
|
|
|
|
trace_android_vh_swapin_add_anon_rmap(vmf, page);
|
|
swap_free(entry);
|
|
if (mem_cgroup_swap_full(page) ||
|
|
(vmf->vma_flags & VM_LOCKED) || PageMlocked(page))
|
|
try_to_free_swap(page);
|
|
unlock_page(page);
|
|
if (page != swapcache && swapcache) {
|
|
/*
|
|
* Hold the lock to avoid the swap entry to be reused
|
|
* until we take the PT lock for the pte_same() check
|
|
* (to avoid false positives from pte_same). For
|
|
* further safety release the lock after the swap_free
|
|
* so that the swap count won't change under a
|
|
* parallel locked swapcache.
|
|
*/
|
|
unlock_page(swapcache);
|
|
put_page(swapcache);
|
|
}
|
|
|
|
if (vmf->flags & FAULT_FLAG_WRITE) {
|
|
ret |= do_wp_page(vmf);
|
|
if (ret & VM_FAULT_ERROR)
|
|
ret &= VM_FAULT_ERROR;
|
|
goto out;
|
|
}
|
|
|
|
/* No need to invalidate - it was non-present before */
|
|
update_mmu_cache(vma, vmf->address, vmf->pte);
|
|
unlock:
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
out:
|
|
return ret;
|
|
out_nomap:
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
out_page:
|
|
unlock_page(page);
|
|
out_release:
|
|
put_page(page);
|
|
if (page != swapcache && swapcache) {
|
|
unlock_page(swapcache);
|
|
put_page(swapcache);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* We enter with non-exclusive mmap_lock (to exclude vma changes,
|
|
* but allow concurrent faults), and pte mapped but not yet locked.
|
|
* We return with mmap_lock still held, but pte unmapped and unlocked.
|
|
*/
|
|
static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
|
|
{
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
struct page *page;
|
|
vm_fault_t ret = 0;
|
|
pte_t entry;
|
|
|
|
/* File mapping without ->vm_ops ? */
|
|
if (vmf->vma_flags & VM_SHARED)
|
|
return VM_FAULT_SIGBUS;
|
|
|
|
/* Do not check unstable pmd, if it's changed will retry later */
|
|
if (vmf->flags & FAULT_FLAG_SPECULATIVE)
|
|
goto skip_pmd_checks;
|
|
|
|
/*
|
|
* Use pte_alloc() instead of pte_alloc_map(). We can't run
|
|
* pte_offset_map() on pmds where a huge pmd might be created
|
|
* from a different thread.
|
|
*
|
|
* pte_alloc_map() is safe to use under mmap_write_lock(mm) or when
|
|
* parallel threads are excluded by other means.
|
|
*
|
|
* Here we only have mmap_read_lock(mm).
|
|
*/
|
|
if (pte_alloc(vma->vm_mm, vmf->pmd))
|
|
return VM_FAULT_OOM;
|
|
|
|
/* See comment in handle_pte_fault() */
|
|
if (unlikely(pmd_trans_unstable(vmf->pmd)))
|
|
return 0;
|
|
|
|
skip_pmd_checks:
|
|
/* Use the zero-page for reads */
|
|
if (!(vmf->flags & FAULT_FLAG_WRITE) &&
|
|
!mm_forbids_zeropage(vma->vm_mm)) {
|
|
entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address),
|
|
vmf->vma_page_prot));
|
|
if (!pte_map_lock(vmf))
|
|
return VM_FAULT_RETRY;
|
|
if (!pte_none(*vmf->pte)) {
|
|
update_mmu_tlb(vma, vmf->address, vmf->pte);
|
|
goto unlock;
|
|
}
|
|
ret = check_stable_address_space(vma->vm_mm);
|
|
if (ret)
|
|
goto unlock;
|
|
/*
|
|
* Don't call the userfaultfd during the speculative path.
|
|
* We already checked for the VMA to not be managed through
|
|
* userfaultfd, but it may be set in our back once we have lock
|
|
* the pte. In such a case we can ignore it this time.
|
|
*/
|
|
if (vmf->flags & FAULT_FLAG_SPECULATIVE)
|
|
goto setpte;
|
|
/* Deliver the page fault to userland, check inside PT lock */
|
|
if (userfaultfd_missing(vma)) {
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
return handle_userfault(vmf, VM_UFFD_MISSING);
|
|
}
|
|
goto setpte;
|
|
}
|
|
|
|
/* Allocate our own private page. */
|
|
if (unlikely(anon_vma_prepare(vma)))
|
|
goto oom;
|
|
page = alloc_zeroed_user_highpage_movable(vma, vmf->address);
|
|
if (!page)
|
|
goto oom;
|
|
|
|
if (mem_cgroup_charge(page, vma->vm_mm, GFP_KERNEL))
|
|
goto oom_free_page;
|
|
cgroup_throttle_swaprate(page, GFP_KERNEL);
|
|
|
|
/*
|
|
* The memory barrier inside __SetPageUptodate makes sure that
|
|
* preceding stores to the page contents become visible before
|
|
* the set_pte_at() write.
|
|
*/
|
|
__SetPageUptodate(page);
|
|
|
|
entry = mk_pte(page, vmf->vma_page_prot);
|
|
entry = pte_sw_mkyoung(entry);
|
|
if (vmf->vma_flags & VM_WRITE)
|
|
entry = pte_mkwrite(pte_mkdirty(entry));
|
|
|
|
if (!pte_map_lock(vmf)) {
|
|
ret = VM_FAULT_RETRY;
|
|
goto release;
|
|
}
|
|
|
|
if (!pte_none(*vmf->pte)) {
|
|
update_mmu_cache(vma, vmf->address, vmf->pte);
|
|
goto unlock_and_release;
|
|
}
|
|
|
|
ret = check_stable_address_space(vma->vm_mm);
|
|
if (ret)
|
|
goto unlock_and_release;
|
|
|
|
/* Deliver the page fault to userland, check inside PT lock */
|
|
if (!(vmf->flags & FAULT_FLAG_SPECULATIVE) &&
|
|
userfaultfd_missing(vma)) {
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
put_page(page);
|
|
return handle_userfault(vmf, VM_UFFD_MISSING);
|
|
}
|
|
|
|
inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
|
|
__page_add_new_anon_rmap(page, vma, vmf->address, false);
|
|
__lru_cache_add_inactive_or_unevictable(page, vmf->vma_flags);
|
|
setpte:
|
|
set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);
|
|
|
|
/* No need to invalidate - it was non-present before */
|
|
update_mmu_cache(vma, vmf->address, vmf->pte);
|
|
unlock:
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
return ret;
|
|
unlock_and_release:
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
release:
|
|
put_page(page);
|
|
return ret;
|
|
oom_free_page:
|
|
put_page(page);
|
|
oom:
|
|
return VM_FAULT_OOM;
|
|
}
|
|
|
|
/*
|
|
* The mmap_lock must have been held on entry, and may have been
|
|
* released depending on flags and vma->vm_ops->fault() return value.
|
|
* See filemap_fault() and __lock_page_retry().
|
|
*/
|
|
static vm_fault_t __do_fault(struct vm_fault *vmf)
|
|
{
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
vm_fault_t ret;
|
|
|
|
/* Do not check unstable pmd, if it's changed will retry later */
|
|
if (vmf->flags & FAULT_FLAG_SPECULATIVE)
|
|
goto skip_pmd_checks;
|
|
|
|
/*
|
|
* Preallocate pte before we take page_lock because this might lead to
|
|
* deadlocks for memcg reclaim which waits for pages under writeback:
|
|
* lock_page(A)
|
|
* SetPageWriteback(A)
|
|
* unlock_page(A)
|
|
* lock_page(B)
|
|
* lock_page(B)
|
|
* pte_alloc_one
|
|
* shrink_page_list
|
|
* wait_on_page_writeback(A)
|
|
* SetPageWriteback(B)
|
|
* unlock_page(B)
|
|
* # flush A, B to clear the writeback
|
|
*/
|
|
if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) {
|
|
vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
|
|
if (!vmf->prealloc_pte)
|
|
return VM_FAULT_OOM;
|
|
smp_wmb(); /* See comment in __pte_alloc() */
|
|
}
|
|
|
|
skip_pmd_checks:
|
|
ret = vma->vm_ops->fault(vmf);
|
|
if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY |
|
|
VM_FAULT_DONE_COW)))
|
|
return ret;
|
|
|
|
if (unlikely(PageHWPoison(vmf->page))) {
|
|
struct page *page = vmf->page;
|
|
vm_fault_t poisonret = VM_FAULT_HWPOISON;
|
|
if (ret & VM_FAULT_LOCKED) {
|
|
if (page_mapped(page))
|
|
unmap_mapping_pages(page_mapping(page),
|
|
page->index, 1, false);
|
|
/* Retry if a clean page was removed from the cache. */
|
|
if (invalidate_inode_page(page))
|
|
poisonret = VM_FAULT_NOPAGE;
|
|
unlock_page(page);
|
|
}
|
|
put_page(page);
|
|
vmf->page = NULL;
|
|
return poisonret;
|
|
}
|
|
|
|
if (unlikely(!(ret & VM_FAULT_LOCKED)))
|
|
lock_page(vmf->page);
|
|
else
|
|
VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page);
|
|
|
|
return ret;
|
|
}
|
|
|
|
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
|
|
static void deposit_prealloc_pte(struct vm_fault *vmf)
|
|
{
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
|
|
pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte);
|
|
/*
|
|
* We are going to consume the prealloc table,
|
|
* count that as nr_ptes.
|
|
*/
|
|
mm_inc_nr_ptes(vma->vm_mm);
|
|
vmf->prealloc_pte = NULL;
|
|
}
|
|
|
|
vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
|
|
{
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
bool write = vmf->flags & FAULT_FLAG_WRITE;
|
|
unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
|
|
pmd_t entry;
|
|
int i;
|
|
vm_fault_t ret = VM_FAULT_FALLBACK;
|
|
|
|
if (!transhuge_vma_suitable(vma, haddr))
|
|
return ret;
|
|
|
|
page = compound_head(page);
|
|
if (compound_order(page) != HPAGE_PMD_ORDER)
|
|
return ret;
|
|
|
|
/*
|
|
* Archs like ppc64 need additonal space to store information
|
|
* related to pte entry. Use the preallocated table for that.
|
|
*/
|
|
if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) {
|
|
vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
|
|
if (!vmf->prealloc_pte)
|
|
return VM_FAULT_OOM;
|
|
smp_wmb(); /* See comment in __pte_alloc() */
|
|
}
|
|
|
|
vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
|
|
if (unlikely(!pmd_none(*vmf->pmd)))
|
|
goto out;
|
|
|
|
for (i = 0; i < HPAGE_PMD_NR; i++)
|
|
flush_icache_page(vma, page + i);
|
|
|
|
entry = mk_huge_pmd(page, vmf->vma_page_prot);
|
|
if (write)
|
|
entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
|
|
|
|
add_mm_counter(vma->vm_mm, mm_counter_file(page), HPAGE_PMD_NR);
|
|
page_add_file_rmap(page, true);
|
|
/*
|
|
* deposit and withdraw with pmd lock held
|
|
*/
|
|
if (arch_needs_pgtable_deposit())
|
|
deposit_prealloc_pte(vmf);
|
|
|
|
set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry);
|
|
|
|
update_mmu_cache_pmd(vma, haddr, vmf->pmd);
|
|
|
|
/* fault is handled */
|
|
ret = 0;
|
|
count_vm_event(THP_FILE_MAPPED);
|
|
out:
|
|
spin_unlock(vmf->ptl);
|
|
return ret;
|
|
}
|
|
#else
|
|
vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
|
|
{
|
|
return VM_FAULT_FALLBACK;
|
|
}
|
|
#endif
|
|
|
|
void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr)
|
|
{
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
bool write = vmf->flags & FAULT_FLAG_WRITE;
|
|
bool prefault = vmf->address != addr;
|
|
pte_t entry;
|
|
|
|
flush_icache_page(vma, page);
|
|
entry = mk_pte(page, vmf->vma_page_prot);
|
|
|
|
if (prefault && arch_wants_old_prefaulted_pte())
|
|
entry = pte_mkold(entry);
|
|
else
|
|
entry = pte_sw_mkyoung(entry);
|
|
|
|
if (write)
|
|
entry = maybe_mkwrite(pte_mkdirty(entry), vmf->vma_flags);
|
|
/* copy-on-write page */
|
|
if (write && !(vmf->vma_flags & VM_SHARED)) {
|
|
inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
|
|
__page_add_new_anon_rmap(page, vma, addr, false);
|
|
__lru_cache_add_inactive_or_unevictable(page, vmf->vma_flags);
|
|
} else {
|
|
inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
|
|
page_add_file_rmap(page, false);
|
|
}
|
|
set_pte_at(vma->vm_mm, addr, vmf->pte, entry);
|
|
}
|
|
|
|
/**
|
|
* finish_fault - finish page fault once we have prepared the page to fault
|
|
*
|
|
* @vmf: structure describing the fault
|
|
*
|
|
* This function handles all that is needed to finish a page fault once the
|
|
* page to fault in is prepared. It handles locking of PTEs, inserts PTE for
|
|
* given page, adds reverse page mapping, handles memcg charges and LRU
|
|
* addition.
|
|
*
|
|
* The function expects the page to be locked and on success it consumes a
|
|
* reference of a page being mapped (for the PTE which maps it).
|
|
*
|
|
* Return: %0 on success, %VM_FAULT_ code in case of error.
|
|
*/
|
|
vm_fault_t finish_fault(struct vm_fault *vmf)
|
|
{
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
struct page *page;
|
|
vm_fault_t ret;
|
|
|
|
/* Did we COW the page? */
|
|
if ((vmf->flags & FAULT_FLAG_WRITE) &&
|
|
!(vmf->vma_flags & VM_SHARED))
|
|
page = vmf->cow_page;
|
|
else
|
|
page = vmf->page;
|
|
|
|
/*
|
|
* check even for read faults because we might have lost our CoWed
|
|
* page
|
|
*/
|
|
if (!(vma->vm_flags & VM_SHARED)) {
|
|
ret = check_stable_address_space(vma->vm_mm);
|
|
if (ret)
|
|
return ret;
|
|
}
|
|
|
|
/* Do not check unstable pmd, if it's changed will retry later */
|
|
if (vmf->flags & FAULT_FLAG_SPECULATIVE)
|
|
goto skip_pmd_checks;
|
|
|
|
if (pmd_none(*vmf->pmd)) {
|
|
if (PageTransCompound(page)) {
|
|
ret = do_set_pmd(vmf, page);
|
|
if (ret != VM_FAULT_FALLBACK)
|
|
return ret;
|
|
}
|
|
|
|
if (vmf->prealloc_pte) {
|
|
vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
|
|
if (likely(pmd_none(*vmf->pmd))) {
|
|
mm_inc_nr_ptes(vma->vm_mm);
|
|
pmd_populate(vma->vm_mm, vmf->pmd, vmf->prealloc_pte);
|
|
vmf->prealloc_pte = NULL;
|
|
}
|
|
spin_unlock(vmf->ptl);
|
|
} else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd))) {
|
|
return VM_FAULT_OOM;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* See comment in handle_pte_fault() for how this scenario happens, we
|
|
* need to return NOPAGE so that we drop this page.
|
|
*/
|
|
if (pmd_devmap_trans_unstable(vmf->pmd))
|
|
return VM_FAULT_NOPAGE;
|
|
|
|
skip_pmd_checks:
|
|
if (!pte_map_lock(vmf))
|
|
return VM_FAULT_RETRY;
|
|
|
|
ret = 0;
|
|
/* Re-check under ptl */
|
|
if (likely(pte_none(*vmf->pte)))
|
|
do_set_pte(vmf, page, vmf->address);
|
|
else
|
|
ret = VM_FAULT_NOPAGE;
|
|
|
|
update_mmu_tlb(vma, vmf->address, vmf->pte);
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
return ret;
|
|
}
|
|
|
|
static unsigned long fault_around_bytes __read_mostly =
|
|
rounddown_pow_of_two(65536);
|
|
|
|
#ifdef CONFIG_DEBUG_FS
|
|
static int fault_around_bytes_get(void *data, u64 *val)
|
|
{
|
|
*val = fault_around_bytes;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* fault_around_bytes must be rounded down to the nearest page order as it's
|
|
* what do_fault_around() expects to see.
|
|
*/
|
|
static int fault_around_bytes_set(void *data, u64 val)
|
|
{
|
|
if (val / PAGE_SIZE > PTRS_PER_PTE)
|
|
return -EINVAL;
|
|
if (val > PAGE_SIZE)
|
|
fault_around_bytes = rounddown_pow_of_two(val);
|
|
else
|
|
fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */
|
|
return 0;
|
|
}
|
|
DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops,
|
|
fault_around_bytes_get, fault_around_bytes_set, "%llu\n");
|
|
|
|
static int __init fault_around_debugfs(void)
|
|
{
|
|
debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL,
|
|
&fault_around_bytes_fops);
|
|
return 0;
|
|
}
|
|
late_initcall(fault_around_debugfs);
|
|
#endif
|
|
|
|
/*
|
|
* do_fault_around() tries to map few pages around the fault address. The hope
|
|
* is that the pages will be needed soon and this will lower the number of
|
|
* faults to handle.
|
|
*
|
|
* It uses vm_ops->map_pages() to map the pages, which skips the page if it's
|
|
* not ready to be mapped: not up-to-date, locked, etc.
|
|
*
|
|
* This function is called with the page table lock taken. In the split ptlock
|
|
* case the page table lock only protects only those entries which belong to
|
|
* the page table corresponding to the fault address.
|
|
*
|
|
* This function doesn't cross the VMA boundaries, in order to call map_pages()
|
|
* only once.
|
|
*
|
|
* fault_around_bytes defines how many bytes we'll try to map.
|
|
* do_fault_around() expects it to be set to a power of two less than or equal
|
|
* to PTRS_PER_PTE.
|
|
*
|
|
* The virtual address of the area that we map is naturally aligned to
|
|
* fault_around_bytes rounded down to the machine page size
|
|
* (and therefore to page order). This way it's easier to guarantee
|
|
* that we don't cross page table boundaries.
|
|
*/
|
|
static vm_fault_t do_fault_around(struct vm_fault *vmf)
|
|
{
|
|
unsigned long address = vmf->address, nr_pages, mask;
|
|
pgoff_t start_pgoff = vmf->pgoff;
|
|
pgoff_t end_pgoff;
|
|
int off;
|
|
|
|
nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT;
|
|
mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
|
|
|
|
address = max(address & mask, vmf->vma->vm_start);
|
|
off = ((vmf->address - address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
|
|
start_pgoff -= off;
|
|
|
|
/*
|
|
* end_pgoff is either the end of the page table, the end of
|
|
* the vma or nr_pages from start_pgoff, depending what is nearest.
|
|
*/
|
|
end_pgoff = start_pgoff -
|
|
((address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
|
|
PTRS_PER_PTE - 1;
|
|
end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1,
|
|
start_pgoff + nr_pages - 1);
|
|
|
|
if (!(vmf->flags & FAULT_FLAG_SPECULATIVE) &&
|
|
pmd_none(*vmf->pmd)) {
|
|
vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm);
|
|
if (!vmf->prealloc_pte)
|
|
return VM_FAULT_OOM;
|
|
smp_wmb(); /* See comment in __pte_alloc() */
|
|
}
|
|
|
|
return vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff);
|
|
}
|
|
|
|
static vm_fault_t do_read_fault(struct vm_fault *vmf)
|
|
{
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
vm_fault_t ret = 0;
|
|
|
|
/*
|
|
* Let's call ->map_pages() first and use ->fault() as fallback
|
|
* if page by the offset is not ready to be mapped (cold cache or
|
|
* something).
|
|
*/
|
|
if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) {
|
|
if (likely(!userfaultfd_minor(vmf->vma))) {
|
|
ret = do_fault_around(vmf);
|
|
if (ret)
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
ret = __do_fault(vmf);
|
|
if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
|
|
return ret;
|
|
|
|
ret |= finish_fault(vmf);
|
|
unlock_page(vmf->page);
|
|
if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
|
|
put_page(vmf->page);
|
|
return ret;
|
|
}
|
|
|
|
static vm_fault_t do_cow_fault(struct vm_fault *vmf)
|
|
{
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
vm_fault_t ret;
|
|
|
|
if (unlikely(anon_vma_prepare(vma)))
|
|
return VM_FAULT_OOM;
|
|
|
|
vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address);
|
|
if (!vmf->cow_page)
|
|
return VM_FAULT_OOM;
|
|
|
|
if (mem_cgroup_charge(vmf->cow_page, vma->vm_mm, GFP_KERNEL)) {
|
|
put_page(vmf->cow_page);
|
|
return VM_FAULT_OOM;
|
|
}
|
|
cgroup_throttle_swaprate(vmf->cow_page, GFP_KERNEL);
|
|
|
|
ret = __do_fault(vmf);
|
|
if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
|
|
goto uncharge_out;
|
|
if (ret & VM_FAULT_DONE_COW)
|
|
return ret;
|
|
|
|
copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma);
|
|
__SetPageUptodate(vmf->cow_page);
|
|
|
|
ret |= finish_fault(vmf);
|
|
unlock_page(vmf->page);
|
|
put_page(vmf->page);
|
|
if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
|
|
goto uncharge_out;
|
|
return ret;
|
|
uncharge_out:
|
|
put_page(vmf->cow_page);
|
|
return ret;
|
|
}
|
|
|
|
static vm_fault_t do_shared_fault(struct vm_fault *vmf)
|
|
{
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
vm_fault_t ret, tmp;
|
|
|
|
ret = __do_fault(vmf);
|
|
if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
|
|
return ret;
|
|
|
|
/*
|
|
* Check if the backing address space wants to know that the page is
|
|
* about to become writable
|
|
*/
|
|
if (vma->vm_ops->page_mkwrite) {
|
|
unlock_page(vmf->page);
|
|
tmp = do_page_mkwrite(vmf);
|
|
if (unlikely(!tmp ||
|
|
(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
|
|
put_page(vmf->page);
|
|
return tmp;
|
|
}
|
|
}
|
|
|
|
ret |= finish_fault(vmf);
|
|
if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
|
|
VM_FAULT_RETRY))) {
|
|
unlock_page(vmf->page);
|
|
put_page(vmf->page);
|
|
return ret;
|
|
}
|
|
|
|
ret |= fault_dirty_shared_page(vmf);
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* We enter with non-exclusive mmap_lock (to exclude vma changes,
|
|
* but allow concurrent faults).
|
|
* The mmap_lock may have been released depending on flags and our
|
|
* return value. See filemap_fault() and __lock_page_or_retry().
|
|
* If mmap_lock is released, vma may become invalid (for example
|
|
* by other thread calling munmap()).
|
|
*/
|
|
static vm_fault_t do_fault(struct vm_fault *vmf)
|
|
{
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
struct mm_struct *vm_mm = vma->vm_mm;
|
|
vm_fault_t ret;
|
|
|
|
/*
|
|
* The VMA was not fully populated on mmap() or missing VM_DONTEXPAND
|
|
*/
|
|
if (!vma->vm_ops->fault) {
|
|
/*
|
|
* If we find a migration pmd entry or a none pmd entry, which
|
|
* should never happen, return SIGBUS
|
|
*/
|
|
if (unlikely(!pmd_present(*vmf->pmd)))
|
|
ret = VM_FAULT_SIGBUS;
|
|
else {
|
|
vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm,
|
|
vmf->pmd,
|
|
vmf->address,
|
|
&vmf->ptl);
|
|
/*
|
|
* Make sure this is not a temporary clearing of pte
|
|
* by holding ptl and checking again. A R/M/W update
|
|
* of pte involves: take ptl, clearing the pte so that
|
|
* we don't have concurrent modification by hardware
|
|
* followed by an update.
|
|
*/
|
|
if (unlikely(pte_none(*vmf->pte)))
|
|
ret = VM_FAULT_SIGBUS;
|
|
else
|
|
ret = VM_FAULT_NOPAGE;
|
|
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
}
|
|
} else if (!(vmf->flags & FAULT_FLAG_WRITE))
|
|
ret = do_read_fault(vmf);
|
|
else if (!(vmf->vma_flags & VM_SHARED))
|
|
ret = do_cow_fault(vmf);
|
|
else
|
|
ret = do_shared_fault(vmf);
|
|
|
|
/* preallocated pagetable is unused: free it */
|
|
if (vmf->prealloc_pte) {
|
|
pte_free(vm_mm, vmf->prealloc_pte);
|
|
vmf->prealloc_pte = NULL;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
|
|
unsigned long addr, int page_nid,
|
|
int *flags)
|
|
{
|
|
get_page(page);
|
|
|
|
count_vm_numa_event(NUMA_HINT_FAULTS);
|
|
if (page_nid == numa_node_id()) {
|
|
count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
|
|
*flags |= TNF_FAULT_LOCAL;
|
|
}
|
|
|
|
return mpol_misplaced(page, vma, addr);
|
|
}
|
|
|
|
static vm_fault_t do_numa_page(struct vm_fault *vmf)
|
|
{
|
|
struct vm_area_struct *vma = vmf->vma;
|
|
struct page *page = NULL;
|
|
int page_nid = NUMA_NO_NODE;
|
|
int last_cpupid;
|
|
int target_nid;
|
|
bool migrated = false;
|
|
pte_t pte, old_pte;
|
|
bool was_writable = pte_savedwrite(vmf->orig_pte);
|
|
int flags = 0;
|
|
|
|
/*
|
|
* The "pte" at this point cannot be used safely without
|
|
* validation through pte_unmap_same(). It's of NUMA type but
|
|
* the pfn may be screwed if the read is non atomic.
|
|
*/
|
|
if (!pte_spinlock(vmf))
|
|
return VM_FAULT_RETRY;
|
|
if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) {
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
goto out;
|
|
}
|
|
|
|
/*
|
|
* Make it present again, Depending on how arch implementes non
|
|
* accessible ptes, some can allow access by kernel mode.
|
|
*/
|
|
old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte);
|
|
pte = pte_modify(old_pte, vmf->vma_page_prot);
|
|
pte = pte_mkyoung(pte);
|
|
if (was_writable)
|
|
pte = pte_mkwrite(pte);
|
|
ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte);
|
|
update_mmu_cache(vma, vmf->address, vmf->pte);
|
|
|
|
page = _vm_normal_page(vma, vmf->address, pte, vmf->vma_flags);
|
|
if (!page) {
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
return 0;
|
|
}
|
|
|
|
/* TODO: handle PTE-mapped THP */
|
|
if (PageCompound(page)) {
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Avoid grouping on RO pages in general. RO pages shouldn't hurt as
|
|
* much anyway since they can be in shared cache state. This misses
|
|
* the case where a mapping is writable but the process never writes
|
|
* to it but pte_write gets cleared during protection updates and
|
|
* pte_dirty has unpredictable behaviour between PTE scan updates,
|
|
* background writeback, dirty balancing and application behaviour.
|
|
*/
|
|
if (!pte_write(pte))
|
|
flags |= TNF_NO_GROUP;
|
|
|
|
/*
|
|
* Flag if the page is shared between multiple address spaces. This
|
|
* is later used when determining whether to group tasks together
|
|
*/
|
|
if (page_mapcount(page) > 1 && (vmf->vma_flags & VM_SHARED))
|
|
flags |= TNF_SHARED;
|
|
|
|
last_cpupid = page_cpupid_last(page);
|
|
page_nid = page_to_nid(page);
|
|
target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid,
|
|
&flags);
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
if (target_nid == NUMA_NO_NODE) {
|
|
put_page(page);
|
|
goto out;
|
|
}
|
|
|
|
/* Migrate to the requested node */
|
|
migrated = migrate_misplaced_page(page, vmf, target_nid);
|
|
if (migrated) {
|
|
page_nid = target_nid;
|
|
flags |= TNF_MIGRATED;
|
|
} else
|
|
flags |= TNF_MIGRATE_FAIL;
|
|
|
|
out:
|
|
if (page_nid != NUMA_NO_NODE)
|
|
task_numa_fault(last_cpupid, page_nid, 1, flags);
|
|
return 0;
|
|
}
|
|
|
|
static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf)
|
|
{
|
|
if (vma_is_anonymous(vmf->vma))
|
|
return do_huge_pmd_anonymous_page(vmf);
|
|
if (vmf->vma->vm_ops->huge_fault)
|
|
return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);
|
|
return VM_FAULT_FALLBACK;
|
|
}
|
|
|
|
/* `inline' is required to avoid gcc 4.1.2 build error */
|
|
static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd)
|
|
{
|
|
if (vma_is_anonymous(vmf->vma)) {
|
|
if (userfaultfd_huge_pmd_wp(vmf->vma, orig_pmd))
|
|
return handle_userfault(vmf, VM_UFFD_WP);
|
|
return do_huge_pmd_wp_page(vmf, orig_pmd);
|
|
}
|
|
if (vmf->vma->vm_ops->huge_fault) {
|
|
vm_fault_t ret = vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);
|
|
|
|
if (!(ret & VM_FAULT_FALLBACK))
|
|
return ret;
|
|
}
|
|
|
|
/* COW or write-notify handled on pte level: split pmd. */
|
|
__split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL);
|
|
|
|
return VM_FAULT_FALLBACK;
|
|
}
|
|
|
|
static vm_fault_t create_huge_pud(struct vm_fault *vmf)
|
|
{
|
|
#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \
|
|
defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
|
|
/* No support for anonymous transparent PUD pages yet */
|
|
if (vma_is_anonymous(vmf->vma))
|
|
return VM_FAULT_FALLBACK;
|
|
if (vmf->vma->vm_ops->huge_fault)
|
|
return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD);
|
|
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
|
|
return VM_FAULT_FALLBACK;
|
|
}
|
|
|
|
static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud)
|
|
{
|
|
#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \
|
|
defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
|
|
/* No support for anonymous transparent PUD pages yet */
|
|
if (vma_is_anonymous(vmf->vma))
|
|
goto split;
|
|
if (vmf->vma->vm_ops->huge_fault) {
|
|
vm_fault_t ret = vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD);
|
|
|
|
if (!(ret & VM_FAULT_FALLBACK))
|
|
return ret;
|
|
}
|
|
split:
|
|
/* COW or write-notify not handled on PUD level: split pud.*/
|
|
__split_huge_pud(vmf->vma, vmf->pud, vmf->address);
|
|
#endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
|
|
return VM_FAULT_FALLBACK;
|
|
}
|
|
|
|
/*
|
|
* These routines also need to handle stuff like marking pages dirty
|
|
* and/or accessed for architectures that don't do it in hardware (most
|
|
* RISC architectures). The early dirtying is also good on the i386.
|
|
*
|
|
* There is also a hook called "update_mmu_cache()" that architectures
|
|
* with external mmu caches can use to update those (ie the Sparc or
|
|
* PowerPC hashed page tables that act as extended TLBs).
|
|
*
|
|
* We enter with non-exclusive mmap_lock (to exclude vma changes, but allow
|
|
* concurrent faults).
|
|
*
|
|
* The mmap_lock may have been released depending on flags and our return value.
|
|
* See filemap_fault() and __lock_page_or_retry().
|
|
*/
|
|
static vm_fault_t handle_pte_fault(struct vm_fault *vmf)
|
|
{
|
|
pte_t entry;
|
|
vm_fault_t ret = 0;
|
|
|
|
/* Do not check unstable pmd, if it's changed will retry later */
|
|
if (vmf->flags & FAULT_FLAG_SPECULATIVE)
|
|
goto skip_pmd_checks;
|
|
|
|
if (unlikely(pmd_none(*vmf->pmd))) {
|
|
/*
|
|
* Leave __pte_alloc() until later: because vm_ops->fault may
|
|
* want to allocate huge page, and if we expose page table
|
|
* for an instant, it will be difficult to retract from
|
|
* concurrent faults and from rmap lookups.
|
|
*/
|
|
vmf->pte = NULL;
|
|
} else {
|
|
/*
|
|
* If a huge pmd materialized under us just retry later. Use
|
|
* pmd_trans_unstable() via pmd_devmap_trans_unstable() instead
|
|
* of pmd_trans_huge() to ensure the pmd didn't become
|
|
* pmd_trans_huge under us and then back to pmd_none, as a
|
|
* result of MADV_DONTNEED running immediately after a huge pmd
|
|
* fault in a different thread of this mm, in turn leading to a
|
|
* misleading pmd_trans_huge() retval. All we have to ensure is
|
|
* that it is a regular pmd that we can walk with
|
|
* pte_offset_map() and we can do that through an atomic read
|
|
* in C, which is what pmd_trans_unstable() provides.
|
|
*/
|
|
if (pmd_devmap_trans_unstable(vmf->pmd))
|
|
return 0;
|
|
/*
|
|
* A regular pmd is established and it can't morph into a huge
|
|
* pmd from under us anymore at this point because we hold the
|
|
* mmap_lock read mode and khugepaged takes it in write mode.
|
|
* So now it's safe to run pte_offset_map().
|
|
* This is not applicable to the speculative page fault handler
|
|
* but in that case, the pte is fetched earlier in
|
|
* handle_speculative_fault().
|
|
*/
|
|
vmf->pte = pte_offset_map(vmf->pmd, vmf->address);
|
|
vmf->orig_pte = *vmf->pte;
|
|
|
|
/*
|
|
* some architectures can have larger ptes than wordsize,
|
|
* e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and
|
|
* CONFIG_32BIT=y, so READ_ONCE cannot guarantee atomic
|
|
* accesses. The code below just needs a consistent view
|
|
* for the ifs and we later double check anyway with the
|
|
* ptl lock held. So here a barrier will do.
|
|
*/
|
|
barrier();
|
|
if (pte_none(vmf->orig_pte)) {
|
|
pte_unmap(vmf->pte);
|
|
vmf->pte = NULL;
|
|
}
|
|
}
|
|
|
|
skip_pmd_checks:
|
|
if (!vmf->pte) {
|
|
if (vma_is_anonymous(vmf->vma))
|
|
return do_anonymous_page(vmf);
|
|
else if ((vmf->flags & FAULT_FLAG_SPECULATIVE) &&
|
|
!vmf_allows_speculation(vmf))
|
|
return VM_FAULT_RETRY;
|
|
else
|
|
return do_fault(vmf);
|
|
}
|
|
|
|
if (!pte_present(vmf->orig_pte))
|
|
return do_swap_page(vmf);
|
|
|
|
if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma))
|
|
return do_numa_page(vmf);
|
|
|
|
if (!pte_spinlock(vmf))
|
|
return VM_FAULT_RETRY;
|
|
entry = vmf->orig_pte;
|
|
if (unlikely(!pte_same(*vmf->pte, entry))) {
|
|
update_mmu_tlb(vmf->vma, vmf->address, vmf->pte);
|
|
goto unlock;
|
|
}
|
|
if (vmf->flags & FAULT_FLAG_WRITE) {
|
|
if (!pte_write(entry)) {
|
|
if (!(vmf->flags & FAULT_FLAG_SPECULATIVE))
|
|
return do_wp_page(vmf);
|
|
|
|
if (!mmu_notifier_trylock(vmf->vma->vm_mm)) {
|
|
ret = VM_FAULT_RETRY;
|
|
goto unlock;
|
|
}
|
|
|
|
ret = do_wp_page(vmf);
|
|
mmu_notifier_unlock(vmf->vma->vm_mm);
|
|
return ret;
|
|
}
|
|
entry = pte_mkdirty(entry);
|
|
}
|
|
entry = pte_mkyoung(entry);
|
|
if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry,
|
|
vmf->flags & FAULT_FLAG_WRITE)) {
|
|
update_mmu_cache(vmf->vma, vmf->address, vmf->pte);
|
|
} else {
|
|
/* Skip spurious TLB flush for retried page fault */
|
|
if (vmf->flags & FAULT_FLAG_TRIED)
|
|
goto unlock;
|
|
if (vmf->flags & FAULT_FLAG_SPECULATIVE)
|
|
ret = VM_FAULT_RETRY;
|
|
/*
|
|
* This is needed only for protection faults but the arch code
|
|
* is not yet telling us if this is a protection fault or not.
|
|
* This still avoids useless tlb flushes for .text page faults
|
|
* with threads.
|
|
*/
|
|
if (vmf->flags & FAULT_FLAG_WRITE)
|
|
flush_tlb_fix_spurious_fault(vmf->vma, vmf->address);
|
|
}
|
|
trace_android_rvh_handle_pte_fault_end(vmf, highest_memmap_pfn);
|
|
trace_android_vh_handle_pte_fault_end(vmf, highest_memmap_pfn);
|
|
unlock:
|
|
pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* By the time we get here, we already hold the mm semaphore
|
|
*
|
|
* The mmap_lock may have been released depending on flags and our
|
|
* return value. See filemap_fault() and __lock_page_or_retry().
|
|
*/
|
|
static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
|
|
unsigned long address, unsigned int flags)
|
|
{
|
|
struct vm_fault vmf = {
|
|
.vma = vma,
|
|
.address = address & PAGE_MASK,
|
|
.flags = flags,
|
|
.pgoff = linear_page_index(vma, address),
|
|
.gfp_mask = __get_fault_gfp_mask(vma),
|
|
.vma_flags = vma->vm_flags,
|
|
.vma_page_prot = vma->vm_page_prot,
|
|
};
|
|
unsigned int dirty = flags & FAULT_FLAG_WRITE;
|
|
struct mm_struct *mm = vma->vm_mm;
|
|
pgd_t *pgd;
|
|
p4d_t *p4d;
|
|
vm_fault_t ret;
|
|
|
|
pgd = pgd_offset(mm, address);
|
|
p4d = p4d_alloc(mm, pgd, address);
|
|
if (!p4d)
|
|
return VM_FAULT_OOM;
|
|
|
|
vmf.pud = pud_alloc(mm, p4d, address);
|
|
if (!vmf.pud)
|
|
return VM_FAULT_OOM;
|
|
retry_pud:
|
|
if (pud_none(*vmf.pud) && __transparent_hugepage_enabled(vma)) {
|
|
ret = create_huge_pud(&vmf);
|
|
if (!(ret & VM_FAULT_FALLBACK))
|
|
return ret;
|
|
} else {
|
|
pud_t orig_pud = *vmf.pud;
|
|
|
|
barrier();
|
|
if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) {
|
|
|
|
/* NUMA case for anonymous PUDs would go here */
|
|
|
|
if (dirty && !pud_write(orig_pud)) {
|
|
ret = wp_huge_pud(&vmf, orig_pud);
|
|
if (!(ret & VM_FAULT_FALLBACK))
|
|
return ret;
|
|
} else {
|
|
huge_pud_set_accessed(&vmf, orig_pud);
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
vmf.pmd = pmd_alloc(mm, vmf.pud, address);
|
|
if (!vmf.pmd)
|
|
return VM_FAULT_OOM;
|
|
|
|
/* Huge pud page fault raced with pmd_alloc? */
|
|
if (pud_trans_unstable(vmf.pud))
|
|
goto retry_pud;
|
|
|
|
#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
|
|
vmf.sequence = raw_read_seqcount(&vma->vm_sequence);
|
|
#endif
|
|
if (pmd_none(*vmf.pmd) && __transparent_hugepage_enabled(vma)) {
|
|
ret = create_huge_pmd(&vmf);
|
|
if (!(ret & VM_FAULT_FALLBACK))
|
|
return ret;
|
|
} else {
|
|
pmd_t orig_pmd = *vmf.pmd;
|
|
|
|
barrier();
|
|
if (unlikely(is_swap_pmd(orig_pmd))) {
|
|
VM_BUG_ON(thp_migration_supported() &&
|
|
!is_pmd_migration_entry(orig_pmd));
|
|
if (is_pmd_migration_entry(orig_pmd))
|
|
pmd_migration_entry_wait(mm, vmf.pmd);
|
|
return 0;
|
|
}
|
|
if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) {
|
|
if (pmd_protnone(orig_pmd) && vma_is_accessible(vma))
|
|
return do_huge_pmd_numa_page(&vmf, orig_pmd);
|
|
|
|
if (dirty && !pmd_write(orig_pmd)) {
|
|
ret = wp_huge_pmd(&vmf, orig_pmd);
|
|
if (!(ret & VM_FAULT_FALLBACK))
|
|
return ret;
|
|
} else {
|
|
huge_pmd_set_accessed(&vmf, orig_pmd);
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
return handle_pte_fault(&vmf);
|
|
}
|
|
|
|
/**
|
|
* mm_account_fault - Do page fault accountings
|
|
*
|
|
* @regs: the pt_regs struct pointer. When set to NULL, will skip accounting
|
|
* of perf event counters, but we'll still do the per-task accounting to
|
|
* the task who triggered this page fault.
|
|
* @address: the faulted address.
|
|
* @flags: the fault flags.
|
|
* @ret: the fault retcode.
|
|
*
|
|
* This will take care of most of the page fault accountings. Meanwhile, it
|
|
* will also include the PERF_COUNT_SW_PAGE_FAULTS_[MAJ|MIN] perf counter
|
|
* updates. However note that the handling of PERF_COUNT_SW_PAGE_FAULTS should
|
|
* still be in per-arch page fault handlers at the entry of page fault.
|
|
*/
|
|
static inline void mm_account_fault(struct pt_regs *regs,
|
|
unsigned long address, unsigned int flags,
|
|
vm_fault_t ret)
|
|
{
|
|
bool major;
|
|
|
|
/*
|
|
* We don't do accounting for some specific faults:
|
|
*
|
|
* - Unsuccessful faults (e.g. when the address wasn't valid). That
|
|
* includes arch_vma_access_permitted() failing before reaching here.
|
|
* So this is not a "this many hardware page faults" counter. We
|
|
* should use the hw profiling for that.
|
|
*
|
|
* - Incomplete faults (VM_FAULT_RETRY). They will only be counted
|
|
* once they're completed.
|
|
*/
|
|
if (ret & (VM_FAULT_ERROR | VM_FAULT_RETRY))
|
|
return;
|
|
|
|
/*
|
|
* We define the fault as a major fault when the final successful fault
|
|
* is VM_FAULT_MAJOR, or if it retried (which implies that we couldn't
|
|
* handle it immediately previously).
|
|
*/
|
|
major = (ret & VM_FAULT_MAJOR) || (flags & FAULT_FLAG_TRIED);
|
|
|
|
if (major)
|
|
current->maj_flt++;
|
|
else
|
|
current->min_flt++;
|
|
|
|
/*
|
|
* If the fault is done for GUP, regs will be NULL. We only do the
|
|
* accounting for the per thread fault counters who triggered the
|
|
* fault, and we skip the perf event updates.
|
|
*/
|
|
if (!regs)
|
|
return;
|
|
|
|
if (major)
|
|
perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
|
|
else
|
|
perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
|
|
}
|
|
#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
|
|
|
|
#ifndef CONFIG_ARCH_HAS_PTE_SPECIAL
|
|
/* This is required by vm_normal_page() */
|
|
#error "Speculative page fault handler requires CONFIG_ARCH_HAS_PTE_SPECIAL"
|
|
#endif
|
|
/*
|
|
* vm_normal_page() adds some processing which should be done while
|
|
* hodling the mmap_sem.
|
|
*/
|
|
|
|
/*
|
|
* Tries to handle the page fault in a speculative way, without grabbing the
|
|
* mmap_sem.
|
|
* When VM_FAULT_RETRY is returned, the vma pointer is valid and this vma must
|
|
* be checked later when the mmap_sem has been grabbed by calling
|
|
* can_reuse_spf_vma().
|
|
* This is needed as the returned vma is kept in memory until the call to
|
|
* can_reuse_spf_vma() is made.
|
|
*/
|
|
static vm_fault_t ___handle_speculative_fault(struct mm_struct *mm,
|
|
unsigned long address, unsigned int flags,
|
|
struct vm_area_struct *vma)
|
|
{
|
|
struct vm_fault vmf = {
|
|
.address = address,
|
|
.pgoff = linear_page_index(vma, address),
|
|
.vma = vma,
|
|
.gfp_mask = __get_fault_gfp_mask(vma),
|
|
.flags = flags,
|
|
};
|
|
#ifdef CONFIG_NUMA
|
|
struct mempolicy *pol;
|
|
#endif
|
|
pgd_t *pgd, pgdval;
|
|
p4d_t *p4d, p4dval;
|
|
pud_t pudval;
|
|
int seq;
|
|
vm_fault_t ret;
|
|
bool uffd_missing_sigbus = false;
|
|
|
|
/* Clear flags that may lead to release the mmap_sem to retry */
|
|
flags &= ~(FAULT_FLAG_ALLOW_RETRY|FAULT_FLAG_KILLABLE);
|
|
flags |= FAULT_FLAG_SPECULATIVE;
|
|
|
|
/* rmb <-> seqlock,vma_rb_erase() */
|
|
seq = raw_read_seqcount(&vmf.vma->vm_sequence);
|
|
if (seq & 1) {
|
|
trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
|
|
return VM_FAULT_RETRY;
|
|
}
|
|
|
|
vmf.vma_flags = READ_ONCE(vmf.vma->vm_flags);
|
|
vmf.vma_page_prot = READ_ONCE(vmf.vma->vm_page_prot);
|
|
|
|
#ifdef CONFIG_USERFAULTFD
|
|
/*
|
|
* Only support SPF for SIGBUS+MISSING userfaults in private anonymous
|
|
* VMAs. Rest all should be retried with mmap_lock.
|
|
*/
|
|
if (unlikely(vmf.vma_flags & __VM_UFFD_FLAGS)) {
|
|
uffd_missing_sigbus = vma_is_anonymous(vmf.vma) &&
|
|
(vmf.vma_flags & VM_UFFD_MISSING) &&
|
|
userfaultfd_using_sigbus(vmf.vma);
|
|
if (!uffd_missing_sigbus) {
|
|
trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
|
|
return VM_FAULT_RETRY;
|
|
}
|
|
/* Not having anon_vma implies that the PTE is missing */
|
|
if (!vmf.vma->anon_vma)
|
|
return VM_FAULT_SIGBUS;
|
|
}
|
|
#endif
|
|
|
|
if (!vmf_allows_speculation(&vmf))
|
|
return VM_FAULT_RETRY;
|
|
|
|
if (vmf.vma_flags & VM_GROWSDOWN || vmf.vma_flags & VM_GROWSUP) {
|
|
/*
|
|
* This could be detected by the check address against VMA's
|
|
* boundaries but we want to trace it as not supported instead
|
|
* of changed.
|
|
*/
|
|
trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
|
|
return VM_FAULT_RETRY;
|
|
}
|
|
|
|
if (address < READ_ONCE(vmf.vma->vm_start)
|
|
|| READ_ONCE(vmf.vma->vm_end) <= address) {
|
|
trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
|
|
return VM_FAULT_RETRY;
|
|
}
|
|
|
|
if (!arch_vma_access_permitted(vmf.vma, flags & FAULT_FLAG_WRITE,
|
|
flags & FAULT_FLAG_INSTRUCTION,
|
|
flags & FAULT_FLAG_REMOTE))
|
|
goto out_segv;
|
|
|
|
/* This is one is required to check that the VMA has write access set */
|
|
if (flags & FAULT_FLAG_WRITE) {
|
|
if (unlikely(!(vmf.vma_flags & VM_WRITE)))
|
|
goto out_segv;
|
|
} else if (unlikely(!(vmf.vma_flags & (VM_READ|VM_EXEC|VM_WRITE))))
|
|
goto out_segv;
|
|
|
|
#ifdef CONFIG_NUMA
|
|
/*
|
|
* MPOL_INTERLEAVE implies additional checks in
|
|
* mpol_misplaced() which are not compatible with the
|
|
*speculative page fault processing.
|
|
*/
|
|
pol = __get_vma_policy(vmf.vma, address);
|
|
if (!pol)
|
|
pol = get_task_policy(current);
|
|
if (pol && pol->mode == MPOL_INTERLEAVE) {
|
|
trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
|
|
return VM_FAULT_RETRY;
|
|
}
|
|
#endif
|
|
|
|
/*
|
|
* Do a speculative lookup of the PTE entry.
|
|
*/
|
|
local_irq_disable();
|
|
pgd = pgd_offset(mm, address);
|
|
pgdval = READ_ONCE(*pgd);
|
|
if (pgd_none(pgdval) || unlikely(pgd_bad(pgdval)))
|
|
goto out_walk;
|
|
|
|
p4d = p4d_offset(pgd, address);
|
|
if (pgd_val(READ_ONCE(*pgd)) != pgd_val(pgdval))
|
|
goto out_walk;
|
|
p4dval = READ_ONCE(*p4d);
|
|
if (p4d_none(p4dval) || unlikely(p4d_bad(p4dval)))
|
|
goto out_walk;
|
|
|
|
vmf.pud = pud_offset(p4d, address);
|
|
if (p4d_val(READ_ONCE(*p4d)) != p4d_val(p4dval))
|
|
goto out_walk;
|
|
pudval = READ_ONCE(*vmf.pud);
|
|
if (pud_none(pudval) || unlikely(pud_bad(pudval)))
|
|
goto out_walk;
|
|
|
|
/* Huge pages at PUD level are not supported. */
|
|
if (unlikely(pud_trans_huge(pudval)))
|
|
goto out_walk;
|
|
|
|
vmf.pmd = pmd_offset(vmf.pud, address);
|
|
if (pud_val(READ_ONCE(*vmf.pud)) != pud_val(pudval))
|
|
goto out_walk;
|
|
vmf.orig_pmd = READ_ONCE(*vmf.pmd);
|
|
/*
|
|
* pmd_none could mean that a hugepage collapse is in progress
|
|
* in our back as collapse_huge_page() mark it before
|
|
* invalidating the pte (which is done once the IPI is catched
|
|
* by all CPU and we have interrupt disabled).
|
|
* For this reason we cannot handle THP in a speculative way since we
|
|
* can't safely indentify an in progress collapse operation done in our
|
|
* back on that PMD.
|
|
* Regarding the order of the following checks, see comment in
|
|
* pmd_devmap_trans_unstable()
|
|
*/
|
|
if (unlikely(pmd_devmap(vmf.orig_pmd) ||
|
|
pmd_none(vmf.orig_pmd) || pmd_trans_huge(vmf.orig_pmd) ||
|
|
is_swap_pmd(vmf.orig_pmd)))
|
|
goto out_walk;
|
|
|
|
/*
|
|
* The above does not allocate/instantiate page-tables because doing so
|
|
* would lead to the possibility of instantiating page-tables after
|
|
* free_pgtables() -- and consequently leaking them.
|
|
*
|
|
* The result is that we take at least one !speculative fault per PMD
|
|
* in order to instantiate it.
|
|
*/
|
|
|
|
vmf.pte = pte_offset_map(vmf.pmd, address);
|
|
if (pmd_val(READ_ONCE(*vmf.pmd)) != pmd_val(vmf.orig_pmd)) {
|
|
pte_unmap(vmf.pte);
|
|
vmf.pte = NULL;
|
|
goto out_walk;
|
|
}
|
|
vmf.orig_pte = READ_ONCE(*vmf.pte);
|
|
barrier(); /* See comment in handle_pte_fault() */
|
|
if (pte_none(vmf.orig_pte)) {
|
|
pte_unmap(vmf.pte);
|
|
vmf.pte = NULL;
|
|
}
|
|
|
|
vmf.sequence = seq;
|
|
vmf.flags = flags;
|
|
|
|
local_irq_enable();
|
|
|
|
if (!vmf.pte && uffd_missing_sigbus)
|
|
return VM_FAULT_SIGBUS;
|
|
|
|
/*
|
|
* We need to re-validate the VMA after checking the bounds, otherwise
|
|
* we might have a false positive on the bounds.
|
|
*/
|
|
if (read_seqcount_retry(&vmf.vma->vm_sequence, seq)) {
|
|
trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
|
|
return VM_FAULT_RETRY;
|
|
}
|
|
|
|
mem_cgroup_enter_user_fault();
|
|
ret = handle_pte_fault(&vmf);
|
|
mem_cgroup_exit_user_fault();
|
|
|
|
if (ret != VM_FAULT_RETRY) {
|
|
if (vma_is_anonymous(vmf.vma))
|
|
count_vm_event(SPECULATIVE_PGFAULT_ANON);
|
|
else
|
|
count_vm_event(SPECULATIVE_PGFAULT_FILE);
|
|
}
|
|
|
|
/*
|
|
* The task may have entered a memcg OOM situation but
|
|
* if the allocation error was handled gracefully (no
|
|
* VM_FAULT_OOM), there is no need to kill anything.
|
|
* Just clean up the OOM state peacefully.
|
|
*/
|
|
if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
|
|
mem_cgroup_oom_synchronize(false);
|
|
return ret;
|
|
|
|
out_walk:
|
|
trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
|
|
local_irq_enable();
|
|
/*
|
|
* Failing page-table walk is similar to page-missing so give an
|
|
* opportunity to SIGBUS+MISSING userfault to handle it before retrying
|
|
* with mmap_lock
|
|
*/
|
|
return uffd_missing_sigbus ? VM_FAULT_SIGBUS : VM_FAULT_RETRY;
|
|
|
|
out_segv:
|
|
trace_spf_vma_access(_RET_IP_, vmf.vma, address);
|
|
return VM_FAULT_SIGSEGV;
|
|
}
|
|
|
|
vm_fault_t __handle_speculative_fault(struct mm_struct *mm,
|
|
unsigned long address, unsigned int flags,
|
|
struct vm_area_struct **vma,
|
|
struct pt_regs *regs)
|
|
{
|
|
vm_fault_t ret;
|
|
|
|
check_sync_rss_stat(current);
|
|
|
|
*vma = get_vma(mm, address);
|
|
if (!*vma)
|
|
return VM_FAULT_RETRY;
|
|
|
|
ret = ___handle_speculative_fault(mm, address, flags, *vma);
|
|
|
|
/*
|
|
* If there is no need to retry, don't return the vma to the caller.
|
|
*/
|
|
if (ret != VM_FAULT_RETRY) {
|
|
put_vma(*vma);
|
|
*vma = NULL;
|
|
mm_account_fault(regs, address, flags, ret);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* This is used to know if the vma fetch in the speculative page fault handler
|
|
* is still valid when trying the regular fault path while holding the
|
|
* mmap_sem.
|
|
* The call to put_vma(vma) must be made after checking the vma's fields, as
|
|
* the vma may be freed by put_vma(). In such a case it is expected that false
|
|
* is returned.
|
|
*/
|
|
bool can_reuse_spf_vma(struct vm_area_struct *vma, unsigned long address)
|
|
{
|
|
bool ret;
|
|
|
|
ret = !RB_EMPTY_NODE(&vma->vm_rb) &&
|
|
vma->vm_start <= address && address < vma->vm_end;
|
|
put_vma(vma);
|
|
return ret;
|
|
}
|
|
#endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
|
|
|
|
/*
|
|
* By the time we get here, we already hold the mm semaphore
|
|
*
|
|
* The mmap_lock may have been released depending on flags and our
|
|
* return value. See filemap_fault() and __lock_page_or_retry().
|
|
*/
|
|
vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
|
|
unsigned int flags, struct pt_regs *regs)
|
|
{
|
|
vm_fault_t ret;
|
|
|
|
__set_current_state(TASK_RUNNING);
|
|
|
|
count_vm_event(PGFAULT);
|
|
count_memcg_event_mm(vma->vm_mm, PGFAULT);
|
|
|
|
/* do counter updates before entering really critical section. */
|
|
check_sync_rss_stat(current);
|
|
|
|
if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
|
|
flags & FAULT_FLAG_INSTRUCTION,
|
|
flags & FAULT_FLAG_REMOTE))
|
|
return VM_FAULT_SIGSEGV;
|
|
|
|
/*
|
|
* Enable the memcg OOM handling for faults triggered in user
|
|
* space. Kernel faults are handled more gracefully.
|
|
*/
|
|
if (flags & FAULT_FLAG_USER)
|
|
mem_cgroup_enter_user_fault();
|
|
|
|
if (unlikely(is_vm_hugetlb_page(vma)))
|
|
ret = hugetlb_fault(vma->vm_mm, vma, address, flags);
|
|
else
|
|
ret = __handle_mm_fault(vma, address, flags);
|
|
|
|
if (flags & FAULT_FLAG_USER) {
|
|
mem_cgroup_exit_user_fault();
|
|
/*
|
|
* The task may have entered a memcg OOM situation but
|
|
* if the allocation error was handled gracefully (no
|
|
* VM_FAULT_OOM), there is no need to kill anything.
|
|
* Just clean up the OOM state peacefully.
|
|
*/
|
|
if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
|
|
mem_cgroup_oom_synchronize(false);
|
|
}
|
|
|
|
mm_account_fault(regs, address, flags, ret);
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(handle_mm_fault);
|
|
|
|
#ifndef __PAGETABLE_P4D_FOLDED
|
|
/*
|
|
* Allocate p4d page table.
|
|
* We've already handled the fast-path in-line.
|
|
*/
|
|
int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
|
|
{
|
|
p4d_t *new = p4d_alloc_one(mm, address);
|
|
if (!new)
|
|
return -ENOMEM;
|
|
|
|
smp_wmb(); /* See comment in __pte_alloc */
|
|
|
|
spin_lock(&mm->page_table_lock);
|
|
if (pgd_present(*pgd)) /* Another has populated it */
|
|
p4d_free(mm, new);
|
|
else
|
|
pgd_populate(mm, pgd, new);
|
|
spin_unlock(&mm->page_table_lock);
|
|
return 0;
|
|
}
|
|
#endif /* __PAGETABLE_P4D_FOLDED */
|
|
|
|
#ifndef __PAGETABLE_PUD_FOLDED
|
|
/*
|
|
* Allocate page upper directory.
|
|
* We've already handled the fast-path in-line.
|
|
*/
|
|
int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address)
|
|
{
|
|
pud_t *new = pud_alloc_one(mm, address);
|
|
if (!new)
|
|
return -ENOMEM;
|
|
|
|
smp_wmb(); /* See comment in __pte_alloc */
|
|
|
|
spin_lock(&mm->page_table_lock);
|
|
if (!p4d_present(*p4d)) {
|
|
mm_inc_nr_puds(mm);
|
|
p4d_populate(mm, p4d, new);
|
|
} else /* Another has populated it */
|
|
pud_free(mm, new);
|
|
spin_unlock(&mm->page_table_lock);
|
|
return 0;
|
|
}
|
|
#endif /* __PAGETABLE_PUD_FOLDED */
|
|
|
|
#ifndef __PAGETABLE_PMD_FOLDED
|
|
/*
|
|
* Allocate page middle directory.
|
|
* We've already handled the fast-path in-line.
|
|
*/
|
|
int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
|
|
{
|
|
spinlock_t *ptl;
|
|
pmd_t *new = pmd_alloc_one(mm, address);
|
|
if (!new)
|
|
return -ENOMEM;
|
|
|
|
smp_wmb(); /* See comment in __pte_alloc */
|
|
|
|
ptl = pud_lock(mm, pud);
|
|
if (!pud_present(*pud)) {
|
|
mm_inc_nr_pmds(mm);
|
|
pud_populate(mm, pud, new);
|
|
} else /* Another has populated it */
|
|
pmd_free(mm, new);
|
|
spin_unlock(ptl);
|
|
return 0;
|
|
}
|
|
#endif /* __PAGETABLE_PMD_FOLDED */
|
|
|
|
int follow_invalidate_pte(struct mm_struct *mm, unsigned long address,
|
|
struct mmu_notifier_range *range, pte_t **ptepp,
|
|
pmd_t **pmdpp, spinlock_t **ptlp)
|
|
{
|
|
pgd_t *pgd;
|
|
p4d_t *p4d;
|
|
pud_t *pud;
|
|
pmd_t *pmd;
|
|
pte_t *ptep;
|
|
|
|
pgd = pgd_offset(mm, address);
|
|
if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
|
|
goto out;
|
|
|
|
p4d = p4d_offset(pgd, address);
|
|
if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d)))
|
|
goto out;
|
|
|
|
pud = pud_offset(p4d, address);
|
|
if (pud_none(*pud) || unlikely(pud_bad(*pud)))
|
|
goto out;
|
|
|
|
pmd = pmd_offset(pud, address);
|
|
VM_BUG_ON(pmd_trans_huge(*pmd));
|
|
|
|
if (pmd_huge(*pmd)) {
|
|
if (!pmdpp)
|
|
goto out;
|
|
|
|
if (range) {
|
|
mmu_notifier_range_init(range, MMU_NOTIFY_CLEAR, 0,
|
|
NULL, mm, address & PMD_MASK,
|
|
(address & PMD_MASK) + PMD_SIZE);
|
|
mmu_notifier_invalidate_range_start(range);
|
|
}
|
|
*ptlp = pmd_lock(mm, pmd);
|
|
if (pmd_huge(*pmd)) {
|
|
*pmdpp = pmd;
|
|
return 0;
|
|
}
|
|
spin_unlock(*ptlp);
|
|
if (range)
|
|
mmu_notifier_invalidate_range_end(range);
|
|
}
|
|
|
|
if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
|
|
goto out;
|
|
|
|
if (range) {
|
|
mmu_notifier_range_init(range, MMU_NOTIFY_CLEAR, 0, NULL, mm,
|
|
address & PAGE_MASK,
|
|
(address & PAGE_MASK) + PAGE_SIZE);
|
|
mmu_notifier_invalidate_range_start(range);
|
|
}
|
|
ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
|
|
if (!pte_present(*ptep))
|
|
goto unlock;
|
|
*ptepp = ptep;
|
|
return 0;
|
|
unlock:
|
|
pte_unmap_unlock(ptep, *ptlp);
|
|
if (range)
|
|
mmu_notifier_invalidate_range_end(range);
|
|
out:
|
|
return -EINVAL;
|
|
}
|
|
|
|
/**
|
|
* follow_pte - look up PTE at a user virtual address
|
|
* @mm: the mm_struct of the target address space
|
|
* @address: user virtual address
|
|
* @ptepp: location to store found PTE
|
|
* @ptlp: location to store the lock for the PTE
|
|
*
|
|
* On a successful return, the pointer to the PTE is stored in @ptepp;
|
|
* the corresponding lock is taken and its location is stored in @ptlp.
|
|
* The contents of the PTE are only stable until @ptlp is released;
|
|
* any further use, if any, must be protected against invalidation
|
|
* with MMU notifiers.
|
|
*
|
|
* Only IO mappings and raw PFN mappings are allowed. The mmap semaphore
|
|
* should be taken for read.
|
|
*
|
|
* KVM uses this function. While it is arguably less bad than ``follow_pfn``,
|
|
* it is not a good general-purpose API.
|
|
*
|
|
* Return: zero on success, -ve otherwise.
|
|
*/
|
|
int follow_pte(struct mm_struct *mm, unsigned long address,
|
|
pte_t **ptepp, spinlock_t **ptlp)
|
|
{
|
|
return follow_invalidate_pte(mm, address, NULL, ptepp, NULL, ptlp);
|
|
}
|
|
EXPORT_SYMBOL_GPL(follow_pte);
|
|
|
|
/**
|
|
* follow_pfn - look up PFN at a user virtual address
|
|
* @vma: memory mapping
|
|
* @address: user virtual address
|
|
* @pfn: location to store found PFN
|
|
*
|
|
* Only IO mappings and raw PFN mappings are allowed.
|
|
*
|
|
* This function does not allow the caller to read the permissions
|
|
* of the PTE. Do not use it.
|
|
*
|
|
* Return: zero and the pfn at @pfn on success, -ve otherwise.
|
|
*/
|
|
int follow_pfn(struct vm_area_struct *vma, unsigned long address,
|
|
unsigned long *pfn)
|
|
{
|
|
int ret = -EINVAL;
|
|
spinlock_t *ptl;
|
|
pte_t *ptep;
|
|
|
|
if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
|
|
return ret;
|
|
|
|
ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
|
|
if (ret)
|
|
return ret;
|
|
*pfn = pte_pfn(*ptep);
|
|
pte_unmap_unlock(ptep, ptl);
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL(follow_pfn);
|
|
|
|
#ifdef CONFIG_HAVE_IOREMAP_PROT
|
|
int follow_phys(struct vm_area_struct *vma,
|
|
unsigned long address, unsigned int flags,
|
|
unsigned long *prot, resource_size_t *phys)
|
|
{
|
|
int ret = -EINVAL;
|
|
pte_t *ptep, pte;
|
|
spinlock_t *ptl;
|
|
|
|
if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
|
|
goto out;
|
|
|
|
if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
|
|
goto out;
|
|
pte = *ptep;
|
|
|
|
if ((flags & FOLL_WRITE) && !pte_write(pte))
|
|
goto unlock;
|
|
|
|
*prot = pgprot_val(pte_pgprot(pte));
|
|
*phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
|
|
|
|
ret = 0;
|
|
unlock:
|
|
pte_unmap_unlock(ptep, ptl);
|
|
out:
|
|
return ret;
|
|
}
|
|
|
|
int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
|
|
void *buf, int len, int write)
|
|
{
|
|
resource_size_t phys_addr;
|
|
unsigned long prot = 0;
|
|
void __iomem *maddr;
|
|
int offset = addr & (PAGE_SIZE-1);
|
|
|
|
if (follow_phys(vma, addr, write, &prot, &phys_addr))
|
|
return -EINVAL;
|
|
|
|
maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot);
|
|
if (!maddr)
|
|
return -ENOMEM;
|
|
|
|
if (write)
|
|
memcpy_toio(maddr + offset, buf, len);
|
|
else
|
|
memcpy_fromio(buf, maddr + offset, len);
|
|
iounmap(maddr);
|
|
|
|
return len;
|
|
}
|
|
EXPORT_SYMBOL_GPL(generic_access_phys);
|
|
#endif
|
|
|
|
/*
|
|
* Access another process' address space as given in mm. If non-NULL, use the
|
|
* given task for page fault accounting.
|
|
*/
|
|
int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
|
|
unsigned long addr, void *buf, int len, unsigned int gup_flags)
|
|
{
|
|
struct vm_area_struct *vma;
|
|
void *old_buf = buf;
|
|
int write = gup_flags & FOLL_WRITE;
|
|
|
|
if (mmap_read_lock_killable(mm))
|
|
return 0;
|
|
|
|
/* ignore errors, just check how much was successfully transferred */
|
|
while (len) {
|
|
int bytes, ret, offset;
|
|
void *maddr;
|
|
struct page *page = NULL;
|
|
|
|
ret = get_user_pages_remote(mm, addr, 1,
|
|
gup_flags, &page, &vma, NULL);
|
|
if (ret <= 0) {
|
|
#ifndef CONFIG_HAVE_IOREMAP_PROT
|
|
break;
|
|
#else
|
|
/*
|
|
* Check if this is a VM_IO | VM_PFNMAP VMA, which
|
|
* we can access using slightly different code.
|
|
*/
|
|
vma = find_vma(mm, addr);
|
|
if (!vma || vma->vm_start > addr)
|
|
break;
|
|
if (vma->vm_ops && vma->vm_ops->access)
|
|
ret = vma->vm_ops->access(vma, addr, buf,
|
|
len, write);
|
|
if (ret <= 0)
|
|
break;
|
|
bytes = ret;
|
|
#endif
|
|
} else {
|
|
bytes = len;
|
|
offset = addr & (PAGE_SIZE-1);
|
|
if (bytes > PAGE_SIZE-offset)
|
|
bytes = PAGE_SIZE-offset;
|
|
|
|
maddr = kmap(page);
|
|
if (write) {
|
|
copy_to_user_page(vma, page, addr,
|
|
maddr + offset, buf, bytes);
|
|
set_page_dirty_lock(page);
|
|
} else {
|
|
copy_from_user_page(vma, page, addr,
|
|
buf, maddr + offset, bytes);
|
|
}
|
|
kunmap(page);
|
|
put_user_page(page);
|
|
}
|
|
len -= bytes;
|
|
buf += bytes;
|
|
addr += bytes;
|
|
}
|
|
mmap_read_unlock(mm);
|
|
|
|
return buf - old_buf;
|
|
}
|
|
|
|
/**
|
|
* access_remote_vm - access another process' address space
|
|
* @mm: the mm_struct of the target address space
|
|
* @addr: start address to access
|
|
* @buf: source or destination buffer
|
|
* @len: number of bytes to transfer
|
|
* @gup_flags: flags modifying lookup behaviour
|
|
*
|
|
* The caller must hold a reference on @mm.
|
|
*
|
|
* Return: number of bytes copied from source to destination.
|
|
*/
|
|
int access_remote_vm(struct mm_struct *mm, unsigned long addr,
|
|
void *buf, int len, unsigned int gup_flags)
|
|
{
|
|
return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags);
|
|
}
|
|
|
|
/*
|
|
* Access another process' address space.
|
|
* Source/target buffer must be kernel space,
|
|
* Do not walk the page table directly, use get_user_pages
|
|
*/
|
|
int access_process_vm(struct task_struct *tsk, unsigned long addr,
|
|
void *buf, int len, unsigned int gup_flags)
|
|
{
|
|
struct mm_struct *mm;
|
|
int ret;
|
|
|
|
mm = get_task_mm(tsk);
|
|
if (!mm)
|
|
return 0;
|
|
|
|
ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags);
|
|
|
|
mmput(mm);
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(access_process_vm);
|
|
|
|
/*
|
|
* Print the name of a VMA.
|
|
*/
|
|
void print_vma_addr(char *prefix, unsigned long ip)
|
|
{
|
|
struct mm_struct *mm = current->mm;
|
|
struct vm_area_struct *vma;
|
|
|
|
/*
|
|
* we might be running from an atomic context so we cannot sleep
|
|
*/
|
|
if (!mmap_read_trylock(mm))
|
|
return;
|
|
|
|
vma = find_vma(mm, ip);
|
|
if (vma && vma->vm_file) {
|
|
struct file *f = vma->vm_file;
|
|
char *buf = (char *)__get_free_page(GFP_NOWAIT);
|
|
if (buf) {
|
|
char *p;
|
|
|
|
p = file_path(f, buf, PAGE_SIZE);
|
|
if (IS_ERR(p))
|
|
p = "?";
|
|
printk("%s%s[%lx+%lx]", prefix, kbasename(p),
|
|
vma->vm_start,
|
|
vma->vm_end - vma->vm_start);
|
|
free_page((unsigned long)buf);
|
|
}
|
|
}
|
|
mmap_read_unlock(mm);
|
|
}
|
|
|
|
#if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
|
|
void __might_fault(const char *file, int line)
|
|
{
|
|
/*
|
|
* Some code (nfs/sunrpc) uses socket ops on kernel memory while
|
|
* holding the mmap_lock, this is safe because kernel memory doesn't
|
|
* get paged out, therefore we'll never actually fault, and the
|
|
* below annotations will generate false positives.
|
|
*/
|
|
if (uaccess_kernel())
|
|
return;
|
|
if (pagefault_disabled())
|
|
return;
|
|
__might_sleep(file, line, 0);
|
|
#if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
|
|
if (current->mm)
|
|
might_lock_read(¤t->mm->mmap_lock);
|
|
#endif
|
|
}
|
|
EXPORT_SYMBOL(__might_fault);
|
|
#endif
|
|
|
|
#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
|
|
/*
|
|
* Process all subpages of the specified huge page with the specified
|
|
* operation. The target subpage will be processed last to keep its
|
|
* cache lines hot.
|
|
*/
|
|
static inline void process_huge_page(
|
|
unsigned long addr_hint, unsigned int pages_per_huge_page,
|
|
void (*process_subpage)(unsigned long addr, int idx, void *arg),
|
|
void *arg)
|
|
{
|
|
int i, n, base, l;
|
|
unsigned long addr = addr_hint &
|
|
~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
|
|
|
|
/* Process target subpage last to keep its cache lines hot */
|
|
might_sleep();
|
|
n = (addr_hint - addr) / PAGE_SIZE;
|
|
if (2 * n <= pages_per_huge_page) {
|
|
/* If target subpage in first half of huge page */
|
|
base = 0;
|
|
l = n;
|
|
/* Process subpages at the end of huge page */
|
|
for (i = pages_per_huge_page - 1; i >= 2 * n; i--) {
|
|
cond_resched();
|
|
process_subpage(addr + i * PAGE_SIZE, i, arg);
|
|
}
|
|
} else {
|
|
/* If target subpage in second half of huge page */
|
|
base = pages_per_huge_page - 2 * (pages_per_huge_page - n);
|
|
l = pages_per_huge_page - n;
|
|
/* Process subpages at the begin of huge page */
|
|
for (i = 0; i < base; i++) {
|
|
cond_resched();
|
|
process_subpage(addr + i * PAGE_SIZE, i, arg);
|
|
}
|
|
}
|
|
/*
|
|
* Process remaining subpages in left-right-left-right pattern
|
|
* towards the target subpage
|
|
*/
|
|
for (i = 0; i < l; i++) {
|
|
int left_idx = base + i;
|
|
int right_idx = base + 2 * l - 1 - i;
|
|
|
|
cond_resched();
|
|
process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg);
|
|
cond_resched();
|
|
process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg);
|
|
}
|
|
}
|
|
|
|
static void clear_gigantic_page(struct page *page,
|
|
unsigned long addr,
|
|
unsigned int pages_per_huge_page)
|
|
{
|
|
int i;
|
|
struct page *p = page;
|
|
|
|
might_sleep();
|
|
for (i = 0; i < pages_per_huge_page;
|
|
i++, p = mem_map_next(p, page, i)) {
|
|
cond_resched();
|
|
clear_user_highpage(p, addr + i * PAGE_SIZE);
|
|
}
|
|
}
|
|
|
|
static void clear_subpage(unsigned long addr, int idx, void *arg)
|
|
{
|
|
struct page *page = arg;
|
|
|
|
clear_user_highpage(page + idx, addr);
|
|
}
|
|
|
|
void clear_huge_page(struct page *page,
|
|
unsigned long addr_hint, unsigned int pages_per_huge_page)
|
|
{
|
|
unsigned long addr = addr_hint &
|
|
~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
|
|
|
|
if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
|
|
clear_gigantic_page(page, addr, pages_per_huge_page);
|
|
return;
|
|
}
|
|
|
|
process_huge_page(addr_hint, pages_per_huge_page, clear_subpage, page);
|
|
}
|
|
|
|
static void copy_user_gigantic_page(struct page *dst, struct page *src,
|
|
unsigned long addr,
|
|
struct vm_area_struct *vma,
|
|
unsigned int pages_per_huge_page)
|
|
{
|
|
int i;
|
|
struct page *dst_base = dst;
|
|
struct page *src_base = src;
|
|
|
|
for (i = 0; i < pages_per_huge_page; ) {
|
|
cond_resched();
|
|
copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
|
|
|
|
i++;
|
|
dst = mem_map_next(dst, dst_base, i);
|
|
src = mem_map_next(src, src_base, i);
|
|
}
|
|
}
|
|
|
|
struct copy_subpage_arg {
|
|
struct page *dst;
|
|
struct page *src;
|
|
struct vm_area_struct *vma;
|
|
};
|
|
|
|
static void copy_subpage(unsigned long addr, int idx, void *arg)
|
|
{
|
|
struct copy_subpage_arg *copy_arg = arg;
|
|
|
|
copy_user_highpage(copy_arg->dst + idx, copy_arg->src + idx,
|
|
addr, copy_arg->vma);
|
|
}
|
|
|
|
void copy_user_huge_page(struct page *dst, struct page *src,
|
|
unsigned long addr_hint, struct vm_area_struct *vma,
|
|
unsigned int pages_per_huge_page)
|
|
{
|
|
unsigned long addr = addr_hint &
|
|
~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
|
|
struct copy_subpage_arg arg = {
|
|
.dst = dst,
|
|
.src = src,
|
|
.vma = vma,
|
|
};
|
|
|
|
if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
|
|
copy_user_gigantic_page(dst, src, addr, vma,
|
|
pages_per_huge_page);
|
|
return;
|
|
}
|
|
|
|
process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg);
|
|
}
|
|
|
|
long copy_huge_page_from_user(struct page *dst_page,
|
|
const void __user *usr_src,
|
|
unsigned int pages_per_huge_page,
|
|
bool allow_pagefault)
|
|
{
|
|
void *src = (void *)usr_src;
|
|
void *page_kaddr;
|
|
unsigned long i, rc = 0;
|
|
unsigned long ret_val = pages_per_huge_page * PAGE_SIZE;
|
|
struct page *subpage = dst_page;
|
|
|
|
for (i = 0; i < pages_per_huge_page;
|
|
i++, subpage = mem_map_next(subpage, dst_page, i)) {
|
|
if (allow_pagefault)
|
|
page_kaddr = kmap(subpage);
|
|
else
|
|
page_kaddr = kmap_atomic(subpage);
|
|
rc = copy_from_user(page_kaddr,
|
|
(const void __user *)(src + i * PAGE_SIZE),
|
|
PAGE_SIZE);
|
|
if (allow_pagefault)
|
|
kunmap(subpage);
|
|
else
|
|
kunmap_atomic(page_kaddr);
|
|
|
|
ret_val -= (PAGE_SIZE - rc);
|
|
if (rc)
|
|
break;
|
|
|
|
flush_dcache_page(subpage);
|
|
|
|
cond_resched();
|
|
}
|
|
return ret_val;
|
|
}
|
|
#endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
|
|
|
|
#if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS
|
|
|
|
static struct kmem_cache *page_ptl_cachep;
|
|
|
|
void __init ptlock_cache_init(void)
|
|
{
|
|
page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0,
|
|
SLAB_PANIC, NULL);
|
|
}
|
|
|
|
bool ptlock_alloc(struct page *page)
|
|
{
|
|
spinlock_t *ptl;
|
|
|
|
ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL);
|
|
if (!ptl)
|
|
return false;
|
|
page->ptl = ptl;
|
|
return true;
|
|
}
|
|
|
|
void ptlock_free(struct page *page)
|
|
{
|
|
kmem_cache_free(page_ptl_cachep, page->ptl);
|
|
}
|
|
#endif
|