Commit Graph

1062795 Commits

Author SHA1 Message Date
Suren Baghdasaryan
5844c8e7aa ANDROID: mm: disable speculative page faults for CONFIG_NUMA
do_numa_page() uses pte_offset_map() directly and needs to implement
additional mechanisms to ensure the mempolicy object used in
numa_migrate_prep() is not destroyed from under it when speculating.
Rather than fixing this, just disable speculation for CONFIG_NUMA
for now and fix it if it's ever needed in Android.

Bug: 257443051
Change-Id: Ib5750b9809979a69a42ebfa6c130e123f416f1aa
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2022-11-23 10:25:44 -08:00
Suren Baghdasaryan
4ea18cd059 ANDROID: mm: fix speculative walk which is unsafe under RCU
Speculative page fault handling expects MMU_GATHER_RCU_TABLE_FREE to
guarantee that page tables are stable, however tlb_remove_table() has
a slow-path fall-back case when __get_free_page() returns NULL and
tlb_remove_table_one() gets called. The way synchronization is
implemented in that function is not RCU-safe and require IRQs to be
disabled (see the comment in tlb_remove_table_sync_one()).
Fix the invalid assumption to disable IRQs even when
MMU_GATHER_RCU_TABLE_FREE=y.

Bug: 257443051
Change-Id: I227f351607cf73022cb31f6f7a232cab41cf6a5a
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2022-11-23 10:25:28 -08:00
Suren Baghdasaryan
ca96bd7bf1 ANDROID: mm: avoid using vmacache in lockless vma search
When searching vma under RCU protection vmcache should be avoided because
a race with munmap() might result in finding a vma and placing it into
vmcache after munmap() removed that vma and called vmcache_invalidate.
Once that vma is freed, vmcache will be left with an invalid vma pointer.

Bug: 257443051
Change-Id: I62438305fcf5139974f4f7d3bae5b22c74084a59
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2022-11-23 10:25:27 -08:00
Suren Baghdasaryan
533a88fed7 ANDROID: disable page table moves when speculative page faults are enabled
move_page_tables() can move entire pmd or pud without locking individual
ptes. This is problematic for speculative page faults which do not take
mmap_lock because they rely on ptl lock when writing new pte value. To
avoid possible race, disable move_page_tables() optimization when
CONFIG_SPECULATIVE_PAGE_FAULT is enabled.

Bug: 257443051
Change-Id: Ib48dda08ecad1abc60d08fc089a6566a63393c13
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2022-11-23 10:25:27 -08:00
Suren Baghdasaryan
a1f65b39ba ANDROID: mm: skip pte_alloc during speculative page fault
Speculative page fault checks pmd to be valid before starting to handle
the page fault and pte_alloc() should do nothing if pmd stays valid.
If pmd gets changed during speculative page fault, we will detect the
change later and retry with mmap_lock. Therefore pte_alloc() can be
safely skipped and this prevents the racy pmd_lock() call which can
access pmd->ptl after pmd was cleared.

Bug: 257443051
Change-Id: Iec57df5530dba6e0e0bdf9f7500f910851c3d3fd
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2022-11-23 10:25:27 -08:00
Suren Baghdasaryan
3f311327f9 ANDROID: mm: introduce vma refcounting to protect vma during SPF
Current mechanism to stabilize a vma during speculative page fault
handling makes a copy of the faulting vma under RCU protection. This
makes it hard to protect elements which do not belong to the vma but
are used by the page fault handler like vma->vm_file.
The problems is that a copy of the vma can't be used to safely
protect the file attached to the original vma unless the file is
also released after RCU grace period (which is how SPF was designed
originally but that caused performance regression and had to be
changed).
To avoid these complications, introduce vma refcounting to stabilize
and operate on the original vma during page fault handling. Page
fault handler finds the vma and increases its refcount under RCU
protection, vma is freed after RCU grace period, vma->vm_file is
released only after refcount indicates no users. This mechanism
guarantees that once get_vma returns a vma, both the vma itself and
vma->vm_file are stable.
Additional benefits of this patch are: we don't need to copy the vma
and no additional logic is needed to stabilize vma->vm_file.

Bug: 257443051
Change-Id: I59d373926d687fcbd56847a8c3500c43bf1844c8
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2022-11-23 10:25:27 -08:00
Suren Baghdasaryan
50567620db ANDROID: reimplement vm_file protection during speculative page fault
Use vma->vm_file refcounting to protect the file during speculative page
fault handling.

Bug: 258731892
Change-Id: I222c23785391bea7d95c4506d70d6f68029ec45f
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2022-11-23 10:25:27 -08:00
Suren Baghdasaryan
c11ef6356b Revert "ANDROID: add vma->file_ref_count to synchronize vma->vm_file destruction"
This reverts commit a3fe25d92303739a0515c92cb1febb46a920d4d9.

File refcounting implemented in this patch is broken and needs to be
redone.
The change in include/linux/mm_types.h which adds file_ref_count into
vm_area_struct is left untouched to keep ABI intact.

Bug: 258731892
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Change-Id: I37984eb2f0981a989f74bcaaa6be42040a2f241e
2022-11-23 10:25:26 -08:00
Suren Baghdasaryan
9fe88266f2 Revert "ANDROID: arm64/mm: protect vm_file during speculative page fault handling"
This reverts commit 0f4ea1e59394908a0c1c7619c7a24fd7f790586f.

File refcounting implemented in this patch is broken and needs to be
redone.

Bug: 258731892
Change-Id: I3ae5a78b871edaf655d1c9a7868c8543e27f39e5
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2022-11-23 10:25:26 -08:00
Suren Baghdasaryan
218d6f9d77 Revert "ANDROID: x86/mm: protect vm_file during speculative page fault handling"
This reverts commit 4fc18576ca94ca9620bd03e0fc7a64467c1ea0c2.

File refcounting implemented in this patch is broken and needs to be
redone.

Bug: 258731892
Change-Id: Ibcefaf6aa72c60c9627d0ea7d473a3ec806535f4
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2022-11-23 10:25:26 -08:00
Suren Baghdasaryan
d4a5296efa Revert "ANDROID: powerpc/mm: protect vm_file during speculative page fault handling"
This reverts commit 6551a55c4dc5492dcae3dc340c376ed160ab9928.

File refcounting implemented in this patch is broken and needs to be
redone.

Bug: 258731892
Change-Id: I425517a07d1fdcf5cd1842733a4c6c70ef0608b4
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2022-11-23 10:25:26 -08:00
Vincent Donnefort
e45f4b3c5a ANDROID: KVM: arm64: Add protected_shared_mem statistic
When using nVHE in protected mode, protected memory can be between
host and a guest. Tracking this value is interesting from a debug
perspective, to identify potential leaks.

Keeping the count of memory sharing is easy, each share/unshare will return
to the host where the accounting will take place.

Bug: 222044477
Change-Id: I43dcd258789f79dbfe489e5bf721e606c5e6e022
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
2022-11-23 17:11:25 +00:00
Vincent Donnefort
9c9e41a043 ANDROID: KVM: arm64: count KVM s2 mmu usage in nVHE protected mode
When using the nVHE protected mode, the stage-2 page tables are handled by
the hypervisor, but are backed by memory donated by the host. That memory
is accounted during the donation (add to the vCPUs hyp_memcache) under
secondary pagetable stats.

On VM teardown, those pages are mixed with others in the teardown_mc, so use
a separated teardown_stage2_mc to deduct them from accounting after
reclaim.

Bug: 222044477
Change-Id: I2a45ce65c5ce9cf96aabd1b66d6f83ffe4808a0c
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
2022-11-23 17:11:25 +00:00
Vincent Donnefort
36b536a5a5 ANDROID: KVM: arm64: Add protected_hyp_mem VM statistic
When using nVHE in protected mode, the host allocates memory for the
hypervisor to store shadow structures and the stage-2 page tables. This has
been proven to be an interesting value to follow, for debug and health
purpose. Account for those allocations in bytes, in a newly created VM
statistic "protected_hyp_mem".

It is expected, on VM teardown to reclaim all that memory. Raise a warning
if not all the donations are recovered.

Bug: 222044477
Change-Id: I18657d275f2ced67ceb6d0e4bd5ce41cf1d41dc8
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
2022-11-23 17:11:25 +00:00
Yosry Ahmed
da27463ad6 UPSTREAM: KVM: arm64/mmu: count KVM s2 mmu usage in secondary pagetable stats
Count the pages used by KVM in arm64 for stage2 mmu in memory stats
under secondary pagetable stats (e.g. "SecPageTables" in /proc/meminfo)
to give better visibility into the memory consumption of KVM mmu in a
similar way to how normal user page tables are accounted.

Signed-off-by: Yosry Ahmed <yosryahmed@google.com>
Reviewed-by: Oliver Upton <oliver.upton@linux.dev>
Reviewed-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20220823004639.2387269-5-yosryahmed@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>

Bug: 222044477
(cherry picked from commit d38ba8ccd9)
Change-Id: I042d6804dd542bb0f25c7f1b040f5b1e5260c0e6
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
2022-11-23 17:11:25 +00:00
Yosry Ahmed
2af25795b7 BACKPORT: KVM: x86/mmu: count KVM mmu usage in secondary pagetable stats.
Count the pages used by KVM mmu on x86 in memory stats under secondary
pagetable stats (e.g. "SecPageTables" in /proc/meminfo) to give better
visibility into the memory consumption of KVM mmu in a similar way to
how normal user page tables are accounted.

Add the inner helper in common KVM, ARM will also use it to count stats
in a future commit.

Signed-off-by: Yosry Ahmed <yosryahmed@google.com>
Reviewed-by: Sean Christopherson <seanjc@google.com>
Acked-by: Marc Zyngier <maz@kernel.org> # generic KVM changes
Link: https://lore.kernel.org/r/20220823004639.2387269-3-yosryahmed@google.com
Link: https://lore.kernel.org/r/20220823004639.2387269-4-yosryahmed@google.com
[sean: squash x86 usage to workaround modpost issues]
Signed-off-by: Sean Christopherson <seanjc@google.com>

Bug: 222044477
(cherry picked from commit 43a063cab3)
[vdonnefort@: Fix conflicts in mmu.c and tdp_mmu.c]
Change-Id: I9b81155758e513504a87ea2d634f341652ed0630
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
2022-11-23 17:11:25 +00:00
Yosry Ahmed
4445b043d4 BACKPORT: mm: add NR_SECONDARY_PAGETABLE to count secondary page table uses.
We keep track of several kernel memory stats (total kernel memory, page
tables, stack, vmalloc, etc) on multiple levels (global, per-node,
per-memcg, etc). These stats give insights to users to how much memory
is used by the kernel and for what purposes.

Currently, memory used by KVM mmu is not accounted in any of those
kernel memory stats. This patch series accounts the memory pages
used by KVM for page tables in those stats in a new
NR_SECONDARY_PAGETABLE stat. This stat can be later extended to account
for other types of secondary pages tables (e.g. iommu page tables).

KVM has a decent number of large allocations that aren't for page
tables, but for most of them, the number/size of those allocations
scales linearly with either the number of vCPUs or the amount of memory
assigned to the VM. KVM's secondary page table allocations do not scale
linearly, especially when nested virtualization is in use.

From a KVM perspective, NR_SECONDARY_PAGETABLE will scale with KVM's
per-VM pages_{4k,2m,1g} stats unless the guest is doing something
bizarre (e.g. accessing only 4kb chunks of 2mb pages so that KVM is
forced to allocate a large number of page tables even though the guest
isn't accessing that much memory). However, someone would need to either
understand how KVM works to make that connection, or know (or be told) to
go look at KVM's stats if they're running VMs to better decipher the stats.

Furthermore, having NR_PAGETABLE side-by-side with NR_SECONDARY_PAGETABLE
is informative. For example, when backing a VM with THP vs. HugeTLB,
NR_SECONDARY_PAGETABLE is roughly the same, but NR_PAGETABLE is an order
of magnitude higher with THP. So having this stat will at the very least
prove to be useful for understanding tradeoffs between VM backing types,
and likely even steer folks towards potential optimizations.

The original discussion with more details about the rationale:
https://lore.kernel.org/all/87ilqoi77b.wl-maz@kernel.org

This stat will be used by subsequent patches to count KVM mmu
memory usage.

Signed-off-by: Yosry Ahmed <yosryahmed@google.com>
Acked-by: Shakeel Butt <shakeelb@google.com>
Acked-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20220823004639.2387269-2-yosryahmed@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>

Bug: 222044477
(cherry picked from commit ebc97a52b5)
[vdonnefort@: Fix trivial documentation conflict]
Change-Id: I16976e21d2e68ebbcd49e9f1275055e81ec82881
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
2022-11-23 17:11:25 +00:00
Keir Fraser
b1b649a291 Revert "ANDROID: virtio_balloon: New module parameter "pkvm""
This reverts commit 87bcd3edf3.

Reason for revert: Memory reclaim capability will be checked by the
host before configuring the virtio_balloon device.

Bug: 240239989
Change-Id: I03e7c39ec6d671babeace4040138b416c7e201cf
Signed-off-by: Keir Fraser <keirf@google.com>
2022-11-23 14:42:18 +00:00
Dan Vacura
034f49ebf6 ANDROID: gki_defconfig: enable CONFIG_USB_CONFIGFS_F_UVC
Enable the UVC function driver to allow USB gadgets
to connect as a standard video device to a host.

Bug: 200712777
Bug: 242344221
Signed-off-by: Dan Vacura <w36195@motorola.com>
Change-Id: Ia037f8560664f9e98f28f3fede609764d5d5699d
(cherry picked from commit 8d5dd0a5a4)
(cherry picked from commit 885f16fab68e456b9dc9856641b706ce17551456)
2022-11-23 07:33:30 +00:00
Yifan Hong
3d7c9fdef1 ANDROID: Remove virtgpu_trace.h from DDK unsafe headers.
With the following change merged, the unsafe header
is no longer necessary.

3b72a6405c0f301ed787d899077748f84c8bcafc
("kleaf: enable DDK for virtual devices")

Bug: 254735056

Change-Id: I2e89f5c921d641a486d4c06628d59551f61ba2ba
Signed-off-by: Yifan Hong <elsk@google.com>
2022-11-23 01:23:48 +00:00
Yifan Hong
b5b9d443ba ANDROID: Add ddk_headers for arm architecture.
Similar to aarch64 and x86_64, we also add ddk_headers
for arm so we can build DDK (Driver development kit) modules
for the arm architecture for virtual devices.

Test: Treehugger
Bug: 254735056
Change-Id: I7ade4a6053e59d84b825285fbc6162b6e642682e
Signed-off-by: Yifan Hong <elsk@google.com>
2022-11-23 01:23:48 +00:00
Yifan Hong
023b893955 Revert "ANDROID: kleaf: convert rockpi4 to mixed build."
This reverts commit 6100c90ef5.

Reason for revert: rockpi4 has DEVTMPFS enabled and GKI doesn't

Bug: 258841346
Change-Id: Icefb1bb4cf39004234513d307e385b04cb76e51d
2022-11-22 23:44:09 +00:00
Yifan Hong
6100c90ef5 ANDROID: kleaf: convert rockpi4 to mixed build.
Build the GKI //common:kernel_aarch64, then
build rockpi4 modules on top of it.

As a side effect of this change, rockpi4 will no longer
be able to be built with build.sh because it won't produce
vmlinux, etc..

Test: TH
Test: bazel run //common:rockpi4_dist
Bug: 258841346
Change-Id: I88989a265d0a90daddc85dd45a8736f942350522
Signed-off-by: Yifan Hong <elsk@google.com>
2022-11-22 23:42:33 +00:00
Will Deacon
ed40663592 ANDROID: KVM: arm64: Relax SMCCC version check during FF-A proxy init
Although FF-A claims to require version v1.2 of SMCCC, in reality the
current set of calls work just fine with v1.1 and some devices ship with
EL3 firmware that advertises this configuration.

Allow pKVM to proxy FF-A calls for these devices by relaxing our SMCCC
version check to permit SMCCC v1.1+

Reported-by: Alan Stokes <alanstokes@google.com>
Bug: 222663556
Signed-off-by: Will Deacon <willdeacon@google.com>
Change-Id: I41e9ff35f169df3609acee7bbc67999c1d11c9d1
Signed-off-by: Quentin Perret <qperret@google.com>
2022-11-22 17:50:08 +00:00
Quentin Perret
c91cd1264a ANDROID: KVM: arm64: Increase size of FF-A buffer
As it turns out, the kernel's DMA code doesn't enforce the
SG_MAX_SEGMENTS limit on the number of elements in an sglist, which can
confuse the pKVM FF-A proxy which has a buffer sized to contain a
descriptor of at most SG_MAX_SEGMENTS constituents.

As the number of elements in an sglist doesn't seem to have an actual
upper bound, let's paper over the issue for now by increasing the size
of the pKVM buffer based on empirical 'measurements'. Longer term we
might need to make this value configurable on the kernel's cmdline, or
to rework the FF-A proxy to sanely handle large descriptors, although
this is not clear how at the time of writing.

Bug: 221256863
Signed-off-by: Quentin Perret <qperret@google.com>
Change-Id: If252f01bec8ae71c0fe1f7007a3ca7b037924c84
2022-11-22 17:50:08 +00:00
Quentin Perret
d5e4e2b75f BACKPORT: FROMLIST: KVM: arm64: pkvm: Add support for fragmented FF-A descriptors
FF-A memory descriptors may need to be sent in fragments when they don't
fit in the mailboxes. Doing so involves using the FRAG_TX and FRAG_RX
primitives defined in the FF-A protocol.

Add support in the pKVM FF-A relayer for fragmented descriptors by
monitoring outgoing FRAG_TX transactions and by buffering large
descriptors on the reclaim path.

[ qperret: BACKPORT because I removed the erroneous ANDROID tag from the
  patch title posted upstream ]

Bug: 254811097
Co-developed-by: Andrew Walbran <qwandor@google.com>
Change-Id: I701f279cd4820abb0b6d7c2572ee28e0f943edad
Signed-off-by: Andrew Walbran <qwandor@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-13-qperret@google.com
2022-11-22 17:50:08 +00:00
Will Deacon
6c417d4b04 FROMLIST: KVM: arm64: Handle FFA_MEM_LEND calls from the host
Handle FFA_MEM_LEND calls from the host by treating them identically to
FFA_MEM_SHARE calls for the purposes of the host stage-2 page-table, but
forwarding on the original request to EL3.

Bug: 254811097
Change-Id: I8f53bca6f0865fabd9938eefd8427fa0e78016ed
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-12-qperret@google.com
2022-11-22 17:50:08 +00:00
Will Deacon
8c2dae8b16 FROMLIST: KVM: arm64: Handle FFA_MEM_RECLAIM calls from the host
Intecept FFA_MEM_RECLAIM calls from the host and transition the host
stage-2 page-table entries from the SHARED_OWNED state back to the OWNED
state once EL3 has confirmed that the secure mapping has been reclaimed.

Bug: 254811097
Change-Id: I58365e1b3fafa47f290a292fe57f6d2ed7f9091b
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-11-qperret@google.com
2022-11-22 17:50:08 +00:00
Will Deacon
d6ebc184fc FROMLIST: KVM: arm64: Handle FFA_MEM_SHARE calls from the host
Intercept FFA_MEM_SHARE/FFA_FN64_MEM_SHARE calls from the host and
transition the host stage-2 page-table entries from the OWNED state to
the SHARED_OWNED state prior to forwarding the call onto EL3.

Bug: 254811097
Co-developed-by: Andrew Walbran <qwandor@google.com>
Change-Id: Ic75a943c67e6cb96794c250dccf2a59362bf857e
Signed-off-by: Andrew Walbran <qwandor@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-10-qperret@google.com
2022-11-22 17:50:08 +00:00
Will Deacon
ffa0e8607e BACKPORT: FROMLIST: KVM: arm64: Add FF-A helpers to share/unshare memory with secure world
Extend pKVM's memory protection code so that we can update the host's
stage-2 page-table to track pages shared with secure world by the host
using FF-A and prevent those pages from being mapped into a guest.

[ qperret: BACKPORT due to context conflicts in mem_protect.c caused by
  the presense of guest-related memory transition in the android kernel
  (host_donate_guest and friends) ]

Bug: 254811097
Co-developed-by: Andrew Walbran <qwandor@google.com>
Change-Id: Ib4d404cd1d4fa11d7bf8c1d0b8ec00838a8038a0
Signed-off-by: Andrew Walbran <qwandor@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-9-qperret@google.com
2022-11-22 17:50:08 +00:00
Will Deacon
847f7e0189 FROMLIST: KVM: arm64: Handle FFA_RXTX_MAP and FFA_RXTX_UNMAP calls from the host
Handle FFA_RXTX_MAP and FFA_RXTX_UNMAP calls from the host by sharing
the host's mailbox memory with the hypervisor and establishing a
separate pair of mailboxes between the hypervisor and the SPMD at EL3.

Bug: 254811097
Co-developed-by: Andrew Walbran <qwandor@google.com>
Change-Id: Ib5fa89e9b01aa20f7c1b5b41df79d66e98d07f55
Signed-off-by: Andrew Walbran <qwandor@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-8-qperret@google.com
2022-11-22 17:50:08 +00:00
Will Deacon
0b1291b733 FROMLIST: KVM: arm64: Allocate pages for hypervisor FF-A mailboxes
The FF-A proxy code needs to allocate its own buffer pair for
communication with EL3 and for forwarding calls from the host at EL1.

Reserve a couple of pages for this purpose and use them to initialise
the hypervisor's FF-A buffer structure.

Bug: 254811097
Co-developed-by: Andrew Walbran <qwandor@google.com>
Change-Id: Id72cd7f59be20eb6d1faa6f1c5e64ecc8debf929
Signed-off-by: Andrew Walbran <qwandor@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-7-qperret@google.com
2022-11-22 17:50:08 +00:00
Fuad Tabba
c1f54ec477 FROMLIST: KVM: arm64: Handle FFA_FEATURES call from the host
Filter out advertising unsupported features, and only advertise
features and properties that are supported by the hypervisor proxy.

Bug: 254811097
Change-Id: I071766d6d241f4bdd00b8f80e6b237c184a1e59a
Signed-off-by: Fuad Tabba <tabba@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-6-qperret@google.com
2022-11-22 17:50:08 +00:00
Will Deacon
95971bdd60 BACKPORT: FROMLIST: KVM: arm64: Probe FF-A version and host/hyp partition ID during init
Probe FF-A during pKVM initialisation so that we can detect any
inconsistencies in the version or partition ID early on.

[ qperret: BACKPORT due to trivial conflict with header includes in
  setup.c ]

Bug: 254811097
Change-Id: I7def4c2c497017ba86621bc98298bc65ffdeefae
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-5-qperret@google.com
2022-11-22 17:50:08 +00:00
Will Deacon
a46b5c0ea5 FROMLIST: KVM: arm64: Block unsafe FF-A calls from the host
When KVM is initialised in protected mode, we must take care to filter
certain FFA calls from the host kernel so that the integrity of guest
and hypervisor memory is maintained and is not made available to the
secure world.

As a first step, intercept and block all memory-related FF-A SMC calls
from the host to EL3. This puts the framework in place for handling them
properly.

Bug: 254811097
Co-developed-by: Andrew Walbran <qwandor@google.com>
Change-Id: I5279bce56956c590862a68e8c4803dd2205e3f81
Signed-off-by: Andrew Walbran <qwandor@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-4-qperret@google.com
2022-11-22 17:50:08 +00:00
Will Deacon
96a7ea3aa8 FROMLIST: firmware: arm_ffa: Move comment before the field it is documenting
This is consistent with the other comments in the struct.

Bug: 254811097
Co-developed-by: Andrew Walbran <qwandor@google.com>
Change-Id: I10e9014a0d505fe5e132fb1cd6105b95a3f5f2bf
Signed-off-by: Andrew Walbran <qwandor@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-3-qperret@google.com
2022-11-22 17:50:08 +00:00
Will Deacon
1cafec0ee6 FROMLIST: firmware: arm_ffa: Move constants to header file
FF-A function IDs and error codes will be needed in the hypervisor too,
so move to them to the header file where they can be shared. Rename the
version constants with an "FFA_" prefix so that they are less likely
to clash with other code in the tree.

Bug: 254811097
Co-developed-by: Andrew Walbran <qwandor@google.com>
Change-Id: I00ed487279fdfb61ea34ae99140c6fac8ee89187
Signed-off-by: Andrew Walbran <qwandor@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20221116170335.2341003-2-qperret@google.com
2022-11-22 17:50:08 +00:00
Fuad Tabba
6a2536ac98 ANDROID: KVM: arm64: Fix sparse __percpu warning
Force the cast to silence the warning.

We don't have a proper way to dynamically allocate memory at EL2,
and hence no proper way to dynamically allocate percpu fields.
Instead, we rely on memory donated from the host and index it by
hyp_smp_processor_id().

Bug: 258616809
Reported-by: Todd Kjos <tkjos@google.com>
Change-Id: I3f7efd4a41294a6696992ce0a49270c4f9468bb5
Signed-off-by: Fuad Tabba <tabba@google.com>
2022-11-22 11:48:24 +00:00
Yongqin Liu
a8dd10b422 ANDROID: BUILD.bazel: db845c: integrate gki can.ko libarc4.ko rfkill.ko
as they are required by the db845c vendor modules.
Otherwise error like the following will be reported:
    [    1.850121][    T1] init: Loading module /lib/modules/cfg80211.ko with args ''
    [    1.865624][    T1] cfg80211: Unknown symbol rfkill_alloc (err -2)
    [    1.871916][    T1] cfg80211: Unknown symbol rfkill_register (err -2)
    [    1.878426][    T1] cfg80211: Unknown symbol rfkill_destroy (err -2)
    [    1.884845][    T1] cfg80211: Unknown symbol rfkill_resume_polling (err -2)
    [    1.891889][    T1] cfg80211: Unknown symbol rfkill_unregister (err -2)
    [    1.898621][    T1] cfg80211: Unknown symbol rfkill_set_hw_state_reason (err -2)
    [    1.906236][    T1] cfg80211: Unknown symbol rfkill_blocked (err -2)
    [    1.942752][    T1] init: Failed to insmod '/lib/modules/cfg80211.ko' with args '': No such file or directory
    [    1.952811][    T1] init: LoadWithAliases was unable to load /lib/modules/cfg80211.ko

Fixes: 6a0f9b1a5a ("ANDROID: GKI: Convert RF switch subsys as module")
Fixes: 6647fadd21 ("ANDROID: GKI: Convert CAN Bus Subsystem as module")
Fixes: 01d9b23ec3 ("ANDROID: GKI: Convert Net PPP Protocol as module")

Test: boot the db845c build to homescreen

Change-Id: Ia2a072a9d422922a0400495a2107ea0967493ba7
Signed-off-by: Yongqin Liu <yongqin.liu@linaro.org>
2022-11-22 03:27:11 +00:00
davidchao
7b9c0e11b4 ANDROID: thermal: Add vendor thermal genl check
Add vendor enable_thermal_genl_check logic.
Filter on-die tz genl event.

To avoid thermal-hal being woken up all the time by thermal genl events,
only the selected thermal_zone and cooling_device can send events from
kernel.

Bug: 170682696
Test: boot and thermal-hal can receive thermal genl events from kernel
Change-Id: Idb3f4b07a2a2740c01d8785910878bfe6edc832d
Signed-off-by: davidchao <davidchao@google.com>
2022-11-21 22:41:57 +00:00
Yifan Hong
ce3c550c15 ANDROID: kleaf: Add build rules for allmodconfig.
These corresponds to the build.config.allmodconfig.*
build configs.

Unlike build.sh, the Kleaf variant deliberately does not copy
any outputs to the distribution directory. There's not
even a dist target.
This is because allmodconfig is a build test only.

Test: bazel build --allow_undeclared_mdoules //common:allmodconfig_{aarch64,arm,x86_64}
Bug: 258259749
Change-Id: Iefe8f69149f6391938696e90243939d42d4fda84
Signed-off-by: Yifan Hong <elsk@google.com>
2022-11-21 21:15:22 +00:00
T.J. Mercier
7216484575 ANDROID: dma-buf: Return EOPNOTSUPP in dma_buf_begin_cpu_access_partial
Currently callers of dma_buf_begin_cpu_access_partial and
dma_buf_end_cpu_access_partial cannot distinguish between an exporter
not providing {begin,end}_cpu_access_partial functions, and
those functions completing successfully. For cases where an exporter
provides no begin_cpu_access_partial or end_cpu_access_partial function,
we now return EOPNOTSUPP. Callers are then free to fallback to
dma_buf_{begin,end}_cpu_access or fail entirely.

Bug: 259320043
Change-Id: I8d4db5246907362f22b6fbc30338aec7d9d1be58
Signed-off-by: T.J. Mercier <tjmercier@google.com>
2022-11-19 00:49:02 +00:00
Daniel Rosenberg
9bc75534b3 ANDROID: fuse-bpf v1.1
This is a squash of these changes cherry-picked from common-android13-5.10

ANDROID: fuse-bpf: Make compile and pass test
ANDROID: fuse-bpf: set error_in to ENOENT in negative lookup
ANDROID: fuse-bpf: Add ability to run ranges of tests to fuse_test
ANDROID: fuse-bpf: Add test for lookup postfilter
ANDROID: fuse-bpf: readddir postfilter fixes
ANDROID: fix kernelci error in fs/fuse/dir.c
ANDROID: fuse-bpf: Fix RCU/reference issue
ANDROID: fuse-bpf: Always call revalidate for backing
ANDROID: fuse-bpf: Adjust backing handle funcs
ANDROID: fuse-bpf: Fix revalidate error path and backing handling
ANDROID: fuse-bpf: Fix use of get_fuse_inode
ANDROID: fuse: Don't use readdirplus w/ nodeid 0
ANDROID: fuse-bpf: Introduce readdirplus test case for fuse bpf
ANDROID: fuse-bpf: Make sure force_again flag is false by default
ANDROID: fuse-bpf: Make inodes with backing_fd reachable for regular FUSE fuse_iget
Revert "ANDROID: fuse-bpf: use target instead of parent inode to execute backing revalidate"
ANDROID: fuse-bpf: use target instead of parent inode to execute backing revalidate
ANDROID: fuse-bpf: Fix misuse of args.out_args
ANDROID: fuse-bpf: Fix non-fusebpf build
ANDROID: fuse-bpf: Use fuse_bpf_args in uapi
ANDROID: fuse-bpf: Fix read_iter
ANDROID: fuse-bpf: Use cache and refcount
ANDROID: fuse-bpf: Rename iocb_fuse to iocb_orig
ANDROID: fuse-bpf: Fix fixattr in rename
ANDROID: fuse-bpf: Fix readdir
ANDROID: fuse-bpf: Fix lseek return value for offset 0
ANDROID: fuse-bpf: fix read_iter and write_iter
ANDROID: fuse-bpf: fix special devices
ANDROID: fuse-bpf: support FUSE_LSEEK
ANDROID: fuse-bpf: Add support for FUSE_COPY_FILE_RANGE
ANDROID: fuse-bpf: Report errors to finalize
ANDROID: fuse-bpf: Avoid reusing uint64_t for file
ANDROID: fuse-bpf: Fix CONFIG_FUSE_BPF typo in FUSE_FSYNCDIR
ANDROID: fuse-bpf: Move fd operations to be synchronous
ANDROID: fuse-bpf: Invalidate if lower is unhashed
ANDROID: fuse-bpf: Move bpf earlier in fuse_permission
ANDROID: fuse-bpf: Update attributes on file write
ANDROID: fuse: allow mounting with no userspace daemon
ANDROID: fuse-bpf: Support FUSE_STATFS
ANDROID: fuse-bpf: Fix filldir
ANDROID: fuse-bpf: fix fuse_create_open_finalize
ANDROID: fuse: add bpf support for removexattr
ANDROID: fuse-bpf: Fix truncate
ANDROID: fuse-bpf: Support inotify
ANDROID: fuse-bpf: Make compile with CONFIG_FUSE but no CONFIG_FUSE_BPF
ANDROID: fuse-bpf: Fix perms on readdir
ANDROID: fuse: Fix umasking in backing
ANDROID: fs/fuse: Backing move returns EXDEV if TO not backed
ANDROID: bpf-fuse: Fix Setattr
ANDROID: fuse-bpf: Check if mkdir dentry setup
ANDROID: fuse-bpf: Close backing fds in fuse_dentry_revalidate
ANDROID: fuse-bpf: Close backing-fd on both paths
ANDROID: fuse-bpf: Partial fix for mmap'd files
ANDROID: fuse-bpf: Restore a missing const
ANDROID: Add fuse-bpf self tests
ANDROID: Add FUSE_BPF to gki_defconfig
ANDROID: fuse-bpf v1
ANDROID: fuse: Move functions in preparation for fuse-bpf

Bug: 202785178
Test: test_fuse passes on linux.
      On cuttlefish,
      atest android.scopedstorage.cts.host.ScopedStorageHostTest
      passes with fuse-bpf enabled and disabled
Change-Id: Idb099c281f9b39ff2c46fa3ebc63e508758416ee
Signed-off-by: Paul Lawrence <paullawrence@google.com>
Signed-off-by: Daniel Rosenberg <drosen@google.com>
2022-11-18 22:29:39 +00:00
Quentin Perret
877e4e60f1 ANDROID: KVM: arm64: Issue CMOs when tearing down shadow pages
On the guest teardown path, pKVM will zero the pages used to back the
guest shadow data structures before returning them to the host as they
may contain secrets (e.g. in the vCPU registers). However, the zeroing
is done using a cacheable alias, and CMOs are missing, hence giving the
host a potential opportunity to read the original content of the shadow
structs from memory.

Fix this by issuing CMOs after zeroing the pages.

[ qperret@: moved the CMOs to __unmap_donated_memory() to cover all
  callers, including the __pkvm_init_vm() error path ]

Bug: 259551298
Change-Id: Id696d47d16e4c3fd870cb70b792eeb7f2282fc78
Signed-off-by: Quentin Perret <qperret@google.com>
2022-11-18 14:01:09 +00:00
Will Deacon
9bb80c1ede ANDROID: KVM: arm64: Use PSCI MEM_PROTECT to zap guest pages on reset
If a malicious/compromised host issues a PSCI SYSTEM_RESET call in the
presence of guest-owned pages then the contents of those pages may be
susceptible to cold-reboot attacks.

Use the PSCI MEM_PROTECT call to ensure that volatile memory is wiped by
the firmware if a SYSTEM_RESET occurs while unpoisoned guest pages exist
in the system. Since this call does not offer protection for a "warm"
reset initiated by SYSTEM_RESET2, detect this case in the PSCI relay and
repaint the call to a standard SYSTEM_RESET instead.

Signed-off-by: Will Deacon <will@kernel.org>
Bug: 254821051
Change-Id: I5c3dd93bc83ebcd0b6cea2ec734f6e3a77f0064e
Signed-off-by: Will Deacon <willdeacon@google.com>
2022-11-18 10:17:42 +00:00
Will Deacon
c342235242 ANDROID: KVM: arm64: Check IPA range for pvmfw during guest donation
When donating pages to the guest, we only check the first IPA in the
range against the pvmfw loading range. Although this is fine for the
page-at-a-time faulting path, it doesn't fit with the rest of the mem
protection logic, which deals with the possibility of an arbitrarily
sized contiguous address range.

Rework the logic so that we check the whole IPA range during guest
donation and trigger the pvmfw loading path if any of the pages
intersect with the pvmfw region.

Signed-off-by: Will Deacon <will@kernel.org>
Bug: 254819795
Change-Id: I6fef9f1898e65a95cab7f6a0ffa8aa422a8d5a91
Signed-off-by: Will Deacon <willdeacon@google.com>
2022-11-18 10:17:42 +00:00
Will Deacon
0543b83737 ANDROID: KVM: arm64: Use fixmap when poisoning pvmfw pages
When poisoning the pvmfw pages during system reset at EL2, ensure that we
use a writable fixmap mapping rather than the persistent read-only mapping
of the region.

Signed-off-by: Will Deacon <will@kernel.org>
Bug: 254819795
Change-Id: I4c8be092d3c822695afd7d03d0d64163664a9f64
Signed-off-by: Will Deacon <willdeacon@google.com>
2022-11-18 10:17:42 +00:00
Will Deacon
8b48bb1593 ANDROID: KVM: arm64: Rename pkvm_clear_pvmfw_pages()
pkvm_clear_pvmfw_pages() is used to poison the pvmfw pages during reset,
so rename it to pkvm_poison_pvmfw_pages() instead.

Signed-off-by: Will Deacon <will@kernel.org>
Bug: 254819795
Change-Id: Ie5b9c90f0707fa81d9099425cff35383bfb0d009
Signed-off-by: Will Deacon <willdeacon@google.com>
2022-11-18 10:17:42 +00:00
Will Deacon
27dede52b4 ANDROID: KVM: arm64: Rename hyp_zero_page() and make available as helper
hyp_zero_page() is used for poisoning memory, so rename it to
hyp_poison_page() to avoid confusing with the concept of a "zero page"
and make it available outside of mem_protect.c as it will be used to
poison the pvmfw memory in a subsequent patch.

Signed-off-by: Will Deacon <will@kernel.org>
Bug: 254819795
Change-Id: Ia4aec46437db3ffe466ae09bd180392fa06c0b46
Signed-off-by: Will Deacon <willdeacon@google.com>
2022-11-18 10:17:42 +00:00
Will Deacon
54e6cbeedd ANDROID: KVM: arm64: Don't check for hyp_fixmap_map() returning NULL
hyp_fixmap_map() never returns NULL, so remove the redundant checks for
it and simplify the error handling in the callers.

Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Will Deacon <willdeacon@google.com>
Bug: 254819795
Change-Id: Ie73a97cc3d9bded3750abe6e243003827393ee5e
2022-11-18 10:17:42 +00:00