android_kernel_xiaomi_sm8450/lib/stackdepot.c
Greg Kroah-Hartman c15c1199d6 Merge 5.10.216 into android12-5.10-lts
Changes in 5.10.216
	batman-adv: Avoid infinite loop trying to resize local TT
	Bluetooth: Fix memory leak in hci_req_sync_complete()
	media: cec: core: remove length check of Timer Status
	nouveau: fix function cast warning
	net: openvswitch: fix unwanted error log on timeout policy probing
	u64_stats: fix u64_stats_init() for lockdep when used repeatedly in one file
	xsk: validate user input for XDP_{UMEM|COMPLETION}_FILL_RING
	geneve: fix header validation in geneve[6]_xmit_skb
	octeontx2-af: Fix NIX SQ mode and BP config
	ipv6: fib: hide unused 'pn' variable
	ipv4/route: avoid unused-but-set-variable warning
	ipv6: fix race condition between ipv6_get_ifaddr and ipv6_del_addr
	Bluetooth: SCO: Fix not validating setsockopt user input
	netfilter: complete validation of user input
	net/mlx5: Properly link new fs rules into the tree
	af_unix: Do not use atomic ops for unix_sk(sk)->inflight.
	af_unix: Fix garbage collector racing against connect()
	net: ena: Fix potential sign extension issue
	net: ena: Wrong missing IO completions check order
	net: ena: Fix incorrect descriptor free behavior
	iommu/vt-d: Allocate local memory for page request queue
	mailbox: imx: fix suspend failue
	btrfs: qgroup: correctly model root qgroup rsv in convert
	drm/client: Fully protect modes[] with dev->mode_config.mutex
	vhost: Add smp_rmb() in vhost_vq_avail_empty()
	x86/cpu: Actually turn off mitigations by default for SPECULATION_MITIGATIONS=n
	selftests: timers: Fix abs() warning in posix_timers test
	x86/apic: Force native_apic_mem_read() to use the MOV instruction
	irqflags: Explicitly ignore lockdep_hrtimer_exit() argument
	btrfs: record delayed inode root in transaction
	riscv: Enable per-task stack canaries
	riscv: process: Fix kernel gp leakage
	selftests/ftrace: Limit length in subsystem-enable tests
	kprobes: Fix possible use-after-free issue on kprobe registration
	Revert "tracing/trigger: Fix to return error if failed to alloc snapshot"
	netfilter: nf_tables: Fix potential data-race in __nft_expr_type_get()
	netfilter: nft_set_pipapo: do not free live element
	tun: limit printing rate when illegal packet received by tun dev
	RDMA/rxe: Fix the problem "mutex_destroy missing"
	RDMA/cm: Print the old state when cm_destroy_id gets timeout
	RDMA/mlx5: Fix port number for counter query in multi-port configuration
	drm: nv04: Fix out of bounds access
	drm/panel: visionox-rm69299: don't unregister DSI device
	clk: Remove prepare_lock hold assertion in __clk_release()
	clk: Mark 'all_lists' as const
	clk: remove extra empty line
	clk: Print an info line before disabling unused clocks
	clk: Initialize struct clk_core kref earlier
	clk: Get runtime PM before walking tree during disable_unused
	x86/cpufeatures: Fix dependencies for GFNI, VAES, and VPCLMULQDQ
	binder: check offset alignment in binder_get_object()
	thunderbolt: Avoid notify PM core about runtime PM resume
	thunderbolt: Fix wake configurations after device unplug
	comedi: vmk80xx: fix incomplete endpoint checking
	serial/pmac_zilog: Remove flawed mitigation for rx irq flood
	USB: serial: option: add Fibocom FM135-GL variants
	USB: serial: option: add support for Fibocom FM650/FG650
	USB: serial: option: add Lonsung U8300/U9300 product
	USB: serial: option: support Quectel EM060K sub-models
	USB: serial: option: add Rolling RW101-GL and RW135-GL support
	USB: serial: option: add Telit FN920C04 rmnet compositions
	Revert "usb: cdc-wdm: close race between read and workqueue"
	usb: dwc2: host: Fix dereference issue in DDMA completion flow.
	usb: Disable USB3 LPM at shutdown
	mei: me: disable RPL-S on SPS and IGN firmwares
	speakup: Avoid crash on very long word
	fs: sysfs: Fix reference leak in sysfs_break_active_protection()
	init/main.c: Fix potential static_command_line memory overflow
	drm/amdgpu: validate the parameters of bo mapping operations more clearly
	nouveau: fix instmem race condition around ptr stores
	nilfs2: fix OOB in nilfs_set_de_type
	arm64: dts: rockchip: fix alphabetical ordering RK3399 puma
	arm64: dts: rockchip: enable internal pull-up on PCIE_WAKE# for RK3399 Puma
	arm64: dts: rockchip: Remove unsupported node from the Pinebook Pro dts
	arm64: dts: mediatek: mt7622: add support for coherent DMA
	arm64: dts: mediatek: mt7622: introduce nodes for Wireless Ethernet Dispatch
	arm64: dts: mediatek: mt7622: fix clock controllers
	arm64: dts: mediatek: mt7622: fix IR nodename
	arm64: dts: mediatek: mt7622: fix ethernet controller "compatible"
	arm64: dts: mediatek: mt7622: drop "reset-names" from thermal block
	arm64: dts: mediatek: mt2712: fix validation errors
	ARC: [plat-hsdk]: Remove misplaced interrupt-cells property
	wifi: iwlwifi: mvm: remove old PASN station when adding a new one
	vxlan: drop packets from invalid src-address
	mlxsw: core: Unregister EMAD trap using FORWARD action
	NFC: trf7970a: disable all regulators on removal
	ipv4: check for NULL idev in ip_route_use_hint()
	net: usb: ax88179_178a: stop lying about skb->truesize
	net: gtp: Fix Use-After-Free in gtp_dellink
	ipvs: Fix checksumming on GSO of SCTP packets
	net: openvswitch: Fix Use-After-Free in ovs_ct_exit
	mlxsw: spectrum_acl_tcam: Fix race during rehash delayed work
	mlxsw: spectrum_acl_tcam: Fix possible use-after-free during activity update
	mlxsw: spectrum_acl_tcam: Fix possible use-after-free during rehash
	mlxsw: spectrum_acl_tcam: Rate limit error message
	mlxsw: spectrum_acl_tcam: Fix memory leak during rehash
	mlxsw: spectrum_acl_tcam: Fix warning during rehash
	mlxsw: spectrum_acl_tcam: Fix incorrect list API usage
	mlxsw: spectrum_acl_tcam: Fix memory leak when canceling rehash work
	netfilter: nf_tables: honor table dormant flag from netdev release event path
	i40e: Do not use WQ_MEM_RECLAIM flag for workqueue
	i40e: Report MFS in decimal base instead of hex
	iavf: Fix TC config comparison with existing adapter TC config
	net: ethernet: ti: am65-cpts: Fix PTPv1 message type on TX packets
	af_unix: Suppress false-positive lockdep splat for spin_lock() in __unix_gc().
	serial: core: Provide port lock wrappers
	serial: mxs-auart: add spinlock around changing cts state
	Revert "crypto: api - Disallow identical driver names"
	net/mlx5e: Fix a race in command alloc flow
	tracing: Show size of requested perf buffer
	tracing: Increase PERF_MAX_TRACE_SIZE to handle Sentinel1 and docker together
	PM / devfreq: Fix buffer overflow in trans_stat_show
	Bluetooth: Fix type of len in {l2cap,sco}_sock_getsockopt_old()
	Bluetooth: btusb: Add Realtek RTL8852BE support ID 0x0bda:0x4853
	btrfs: fix information leak in btrfs_ioctl_logical_to_ino()
	cpu: Re-enable CPU mitigations by default for !X86 architectures
	arm64: dts: rockchip: enable internal pull-up for Q7_THRM# on RK3399 Puma
	drm/amdgpu/sdma5.2: use legacy HDP flush for SDMA2/3
	drm/amdgpu: Fix leak when GPU memory allocation fails
	irqchip/gic-v3-its: Prevent double free on error
	ethernet: Add helper for assigning packet type when dest address does not match device address
	net: b44: set pause params only when interface is up
	stackdepot: respect __GFP_NOLOCKDEP allocation flag
	mtd: diskonchip: work around ubsan link failure
	tcp: Clean up kernel listener's reqsk in inet_twsk_purge()
	tcp: Fix NEW_SYN_RECV handling in inet_twsk_purge()
	dmaengine: owl: fix register access functions
	idma64: Don't try to serve interrupts when device is powered off
	dma: xilinx_dpdma: Fix locking
	riscv: fix VMALLOC_START definition
	riscv: Fix TASK_SIZE on 64-bit NOMMU
	i2c: smbus: fix NULL function pointer dereference
	HID: i2c-hid: remove I2C_HID_READ_PENDING flag to prevent lock-up
	bounds: Use the right number of bits for power-of-two CONFIG_NR_CPUS
	udp: preserve the connected status if only UDP cmsg
	serial: core: fix kernel-doc for uart_port_unlock_irqrestore()
	riscv: Disable STACKPROTECTOR_PER_TASK if GCC_PLUGIN_RANDSTRUCT is enabled
	Linux 5.10.216

Change-Id: Ia2bf3ba6ed3f36a56f71543442427eb770a2400b
Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
2024-06-13 16:02:17 +00:00

367 lines
10 KiB
C

// SPDX-License-Identifier: GPL-2.0-only
/*
* Generic stack depot for storing stack traces.
*
* Some debugging tools need to save stack traces of certain events which can
* be later presented to the user. For example, KASAN needs to safe alloc and
* free stacks for each object, but storing two stack traces per object
* requires too much memory (e.g. SLUB_DEBUG needs 256 bytes per object for
* that).
*
* Instead, stack depot maintains a hashtable of unique stacktraces. Since alloc
* and free stacks repeat a lot, we save about 100x space.
* Stacks are never removed from depot, so we store them contiguously one after
* another in a contiguos memory allocation.
*
* Author: Alexander Potapenko <glider@google.com>
* Copyright (C) 2016 Google, Inc.
*
* Based on code by Dmitry Chernenkov.
*/
#include <linux/gfp.h>
#include <linux/interrupt.h>
#include <linux/jhash.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/percpu.h>
#include <linux/printk.h>
#include <linux/slab.h>
#include <linux/stacktrace.h>
#include <linux/stackdepot.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/memblock.h>
#define DEPOT_STACK_BITS (sizeof(depot_stack_handle_t) * 8)
#define STACK_ALLOC_NULL_PROTECTION_BITS 1
#define STACK_ALLOC_ORDER 2 /* 'Slab' size order for stack depot, 4 pages */
#define STACK_ALLOC_SIZE (1LL << (PAGE_SHIFT + STACK_ALLOC_ORDER))
#define STACK_ALLOC_ALIGN 4
#define STACK_ALLOC_OFFSET_BITS (STACK_ALLOC_ORDER + PAGE_SHIFT - \
STACK_ALLOC_ALIGN)
#define STACK_ALLOC_INDEX_BITS (DEPOT_STACK_BITS - \
STACK_ALLOC_NULL_PROTECTION_BITS - STACK_ALLOC_OFFSET_BITS)
#define STACK_ALLOC_SLABS_CAP 8192
#define STACK_ALLOC_MAX_SLABS \
(((1LL << (STACK_ALLOC_INDEX_BITS)) < STACK_ALLOC_SLABS_CAP) ? \
(1LL << (STACK_ALLOC_INDEX_BITS)) : STACK_ALLOC_SLABS_CAP)
/* The compact structure to store the reference to stacks. */
union handle_parts {
depot_stack_handle_t handle;
struct {
u32 slabindex : STACK_ALLOC_INDEX_BITS;
u32 offset : STACK_ALLOC_OFFSET_BITS;
u32 valid : STACK_ALLOC_NULL_PROTECTION_BITS;
};
};
struct stack_record {
struct stack_record *next; /* Link in the hashtable */
u32 hash; /* Hash in the hastable */
u32 size; /* Number of frames in the stack */
union handle_parts handle;
unsigned long entries[]; /* Variable-sized array of entries. */
};
static void *stack_slabs[STACK_ALLOC_MAX_SLABS];
static int depot_index;
static int next_slab_inited;
static size_t depot_offset;
static DEFINE_RAW_SPINLOCK(depot_lock);
static bool init_stack_slab(void **prealloc)
{
if (!*prealloc)
return false;
/*
* This smp_load_acquire() pairs with smp_store_release() to
* |next_slab_inited| below and in depot_alloc_stack().
*/
if (smp_load_acquire(&next_slab_inited))
return true;
if (stack_slabs[depot_index] == NULL) {
stack_slabs[depot_index] = *prealloc;
*prealloc = NULL;
} else {
/* If this is the last depot slab, do not touch the next one. */
if (depot_index + 1 < STACK_ALLOC_MAX_SLABS) {
stack_slabs[depot_index + 1] = *prealloc;
*prealloc = NULL;
}
/*
* This smp_store_release pairs with smp_load_acquire() from
* |next_slab_inited| above and in stack_depot_save().
*/
smp_store_release(&next_slab_inited, 1);
}
return true;
}
/* Allocation of a new stack in raw storage */
static struct stack_record *depot_alloc_stack(unsigned long *entries, int size,
u32 hash, void **prealloc, gfp_t alloc_flags)
{
struct stack_record *stack;
size_t required_size = struct_size(stack, entries, size);
required_size = ALIGN(required_size, 1 << STACK_ALLOC_ALIGN);
if (unlikely(depot_offset + required_size > STACK_ALLOC_SIZE)) {
if (unlikely(depot_index + 1 >= STACK_ALLOC_MAX_SLABS)) {
WARN_ONCE(1, "Stack depot reached limit capacity");
return NULL;
}
depot_index++;
depot_offset = 0;
/*
* smp_store_release() here pairs with smp_load_acquire() from
* |next_slab_inited| in stack_depot_save() and
* init_stack_slab().
*/
if (depot_index + 1 < STACK_ALLOC_MAX_SLABS)
smp_store_release(&next_slab_inited, 0);
}
init_stack_slab(prealloc);
if (stack_slabs[depot_index] == NULL)
return NULL;
stack = stack_slabs[depot_index] + depot_offset;
stack->hash = hash;
stack->size = size;
stack->handle.slabindex = depot_index;
stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
stack->handle.valid = 1;
memcpy(stack->entries, entries, flex_array_size(stack, entries, size));
depot_offset += required_size;
return stack;
}
#define STACK_HASH_SIZE (1L << CONFIG_STACK_HASH_ORDER)
#define STACK_HASH_MASK (STACK_HASH_SIZE - 1)
#define STACK_HASH_SEED 0x9747b28c
static bool stack_depot_disable;
static struct stack_record **stack_table;
static int __init is_stack_depot_disabled(char *str)
{
int ret;
ret = kstrtobool(str, &stack_depot_disable);
if (!ret && stack_depot_disable) {
pr_info("Stack Depot is disabled\n");
stack_table = NULL;
}
return 0;
}
early_param("stack_depot_disable", is_stack_depot_disabled);
int __init stack_depot_init(void)
{
if (!stack_depot_disable) {
size_t size = (STACK_HASH_SIZE * sizeof(struct stack_record *));
int i;
stack_table = memblock_alloc(size, size);
for (i = 0; i < STACK_HASH_SIZE; i++)
stack_table[i] = NULL;
}
return 0;
}
/* Calculate hash for a stack */
static inline u32 hash_stack(unsigned long *entries, unsigned int size)
{
return jhash2((u32 *)entries,
array_size(size, sizeof(*entries)) / sizeof(u32),
STACK_HASH_SEED);
}
/* Use our own, non-instrumented version of memcmp().
*
* We actually don't care about the order, just the equality.
*/
static inline
int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2,
unsigned int n)
{
for ( ; n-- ; u1++, u2++) {
if (*u1 != *u2)
return 1;
}
return 0;
}
/* Find a stack that is equal to the one stored in entries in the hash */
static inline struct stack_record *find_stack(struct stack_record *bucket,
unsigned long *entries, int size,
u32 hash)
{
struct stack_record *found;
for (found = bucket; found; found = found->next) {
if (found->hash == hash &&
found->size == size &&
!stackdepot_memcmp(entries, found->entries, size))
return found;
}
return NULL;
}
/**
* stack_depot_fetch - Fetch stack entries from a depot
*
* @handle: Stack depot handle which was returned from
* stack_depot_save().
* @entries: Pointer to store the entries address
*
* Return: The number of trace entries for this depot.
*/
unsigned int stack_depot_fetch(depot_stack_handle_t handle,
unsigned long **entries)
{
union handle_parts parts = { .handle = handle };
void *slab;
size_t offset = parts.offset << STACK_ALLOC_ALIGN;
struct stack_record *stack;
*entries = NULL;
if (parts.slabindex > depot_index) {
WARN(1, "slab index %d out of bounds (%d) for stack id %08x\n",
parts.slabindex, depot_index, handle);
return 0;
}
slab = stack_slabs[parts.slabindex];
if (!slab)
return 0;
stack = slab + offset;
*entries = stack->entries;
return stack->size;
}
EXPORT_SYMBOL_GPL(stack_depot_fetch);
/**
* stack_depot_save - Save a stack trace from an array
*
* @entries: Pointer to storage array
* @nr_entries: Size of the storage array
* @alloc_flags: Allocation gfp flags
*
* Return: The handle of the stack struct stored in depot
*/
depot_stack_handle_t stack_depot_save(unsigned long *entries,
unsigned int nr_entries,
gfp_t alloc_flags)
{
struct stack_record *found = NULL, **bucket;
depot_stack_handle_t retval = 0;
struct page *page = NULL;
void *prealloc = NULL;
unsigned long flags;
u32 hash;
if (unlikely(nr_entries == 0) || stack_depot_disable)
goto fast_exit;
hash = hash_stack(entries, nr_entries);
bucket = &stack_table[hash & STACK_HASH_MASK];
/*
* Fast path: look the stack trace up without locking.
* The smp_load_acquire() here pairs with smp_store_release() to
* |bucket| below.
*/
found = find_stack(smp_load_acquire(bucket), entries,
nr_entries, hash);
if (found)
goto exit;
/*
* Check if the current or the next stack slab need to be initialized.
* If so, allocate the memory - we won't be able to do that under the
* lock.
*
* The smp_load_acquire() here pairs with smp_store_release() to
* |next_slab_inited| in depot_alloc_stack() and init_stack_slab().
*/
if (unlikely(!smp_load_acquire(&next_slab_inited))) {
/*
* Zero out zone modifiers, as we don't have specific zone
* requirements. Keep the flags related to allocation in atomic
* contexts, I/O, nolockdep.
*/
alloc_flags &= ~GFP_ZONEMASK;
alloc_flags &= (GFP_ATOMIC | GFP_KERNEL | __GFP_NOLOCKDEP);
alloc_flags |= __GFP_NOWARN;
page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);
if (page)
prealloc = page_address(page);
}
raw_spin_lock_irqsave(&depot_lock, flags);
found = find_stack(*bucket, entries, nr_entries, hash);
if (!found) {
struct stack_record *new =
depot_alloc_stack(entries, nr_entries,
hash, &prealloc, alloc_flags);
if (new) {
new->next = *bucket;
/*
* This smp_store_release() pairs with
* smp_load_acquire() from |bucket| above.
*/
smp_store_release(bucket, new);
found = new;
}
} else if (prealloc) {
/*
* We didn't need to store this stack trace, but let's keep
* the preallocated memory for the future.
*/
WARN_ON(!init_stack_slab(&prealloc));
}
raw_spin_unlock_irqrestore(&depot_lock, flags);
exit:
if (prealloc) {
/* Nobody used this memory, ok to free it. */
free_pages((unsigned long)prealloc, STACK_ALLOC_ORDER);
}
if (found)
retval = found->handle.handle;
fast_exit:
return retval;
}
EXPORT_SYMBOL_GPL(stack_depot_save);
static inline int in_irqentry_text(unsigned long ptr)
{
return (ptr >= (unsigned long)&__irqentry_text_start &&
ptr < (unsigned long)&__irqentry_text_end) ||
(ptr >= (unsigned long)&__softirqentry_text_start &&
ptr < (unsigned long)&__softirqentry_text_end);
}
unsigned int filter_irq_stacks(unsigned long *entries,
unsigned int nr_entries)
{
unsigned int i;
for (i = 0; i < nr_entries; i++) {
if (in_irqentry_text(entries[i])) {
/* Include the irqentry function into the stack. */
return i + 1;
}
}
return nr_entries;
}
EXPORT_SYMBOL_GPL(filter_irq_stacks);