From patchwork Mon Oct 23 16:22:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 13433191 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BF68FC001E0 for ; Mon, 23 Oct 2023 16:24:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E0F756B0104; Mon, 23 Oct 2023 12:24:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CF8096B0105; Mon, 23 Oct 2023 12:24:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B22336B0106; Mon, 23 Oct 2023 12:24:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 97DBB6B0104 for ; Mon, 23 Oct 2023 12:24:09 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 73992B5C10 for ; Mon, 23 Oct 2023 16:24:09 +0000 (UTC) X-FDA: 81377248218.12.B712B88 Received: from out-191.mta1.migadu.com (out-191.mta1.migadu.com [95.215.58.191]) by imf21.hostedemail.com (Postfix) with ESMTP id 7AE9E1C001C for ; Mon, 23 Oct 2023 16:24:07 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=d8BAggel; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.191 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698078247; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=p8Hgl8kIxqRiPg7P/caKlN6EZe6EuOrMJBszDGSf5JA=; b=gqlrqWSL2vWA4pwBnBljds3NI/c0BRNz+wmsIEoHNvPMstRpLm1G3nzqqXvB7dH3Ilwk/P 2K9wCP2bWkUnsJlfNlzCVTQGwuUN/6b7YroQXMwrw6ED3Pbv9nwMhu0mUeqHd2+F71bDSG h+XUJdavTRAXjLCYcreAjXMMpo6Ivdg= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=d8BAggel; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.191 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078247; a=rsa-sha256; cv=none; b=rj4FiEy+J9IHJXTAwJm/KdvAK9dmDklnRA71cjxc8VZ00VEB9jH0i1jrlG/EPbiOpJpnMN hmfMl44pePyxucyzFInJbexaCihjlO6MFpI3OYfxnDlsgfIhDbb9lW+2i1wV/zT7kGXX6p +U5dh3G5S/b+zircpOMgdBMM8UU9eC8= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1698078246; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=p8Hgl8kIxqRiPg7P/caKlN6EZe6EuOrMJBszDGSf5JA=; b=d8BAggels5u67sU3a1DsLYB3mzDLPzqPZYgCDFI/IUfM7R5EyUYByQkE3kU+k9PyxTsmM6 GNUYqd5pUyWDQggTtDoeOwneQ3CQxbmr4n6O3nWwZpqYNBDV2DNnDg12CvGqgMggwxUqrA 379T75KzzDunwidWZHcC0GAmFd/27C8= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v3 11/19] lib/stackdepot: use read/write lock Date: Mon, 23 Oct 2023 18:22:42 +0200 Message-Id: <68b90916541c09fd64de22dd4666b53172f7e618.1698077459.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 7AE9E1C001C X-Stat-Signature: j1da1qsrty15y3qfs5sqh1fguzhxtz5d X-HE-Tag: 1698078247-220751 X-HE-Meta: U2FsdGVkX18RHHADvA6KGWPq13O4QSj1ypMXIn71IECu2/sJLYS9x1duoE0hL7L0RXgn+kZEaqQQcwySo+w20Gw4TqSluooky/yNE8NkH2lQygdX70T1SFA2Lw4dJgExK9JGqxV8/x85QQL7kJ/o8T7RAeJOKccqFk9svcp97+An++gCICsoHY1Fsy06Voqti7iLUp0anhEadEOcnnHddx2lu4oppbzUkPMZgUVioYL4H1s2RaI+GHXu9lxHy40Otm5kALyo3PVf+EDmETsOVUcS/Ygr7p8aM5b62cu13Mu5lfUaEiRDJMyzaDuk7VN4HgEoUTTwMqRUiw9v2vMeiAYe0Dh7P7BZ5+kgFHSBEzbhYkQeNM6sfvVgyjFnd8d/54T/8o4TYy+KQyfteDaxFcnXzQNvJ8iTzUq2NRce7fCj2P5IKU9Q1KaEGI+x+HV1/NAnnDxrAf9KPaByh61vfrXvfws8C14T5tTZZjCHY9sC6dHtNIO3LOu0bm7J6VBX/JyzTj7zq9PtiKTPKgu+fxpAetbQlw+qhQG4SSWgy1M9tug0UF+iJMMIGoBqji8NwW3xz8EHHv+BUNhjCU1rQ+trdvlRTX+92T7wkB8IXAyO3xjuNmcpH+9yPsnCRSmkZ1tjQwi9y9qjfBwQq8wzi9qK6gJHTyvaJDLxK3Y4iTjzjNMZ7s45I9Hyrqg4DtRYSJgoBRpgGoSd3dgbBDNHs1YiKdJYykIOD2FYfwV0q63p7Fu9TMYEijX/fbktEh4xSVlEAP74jaGfDqjNQHaq9G68bPgyF3QPWshP7OIKlKRhzEo/vUUK8oB8KgmTwZswMTOQCl2vh+m/NewVhEeMBtWZp2zs9zcmZiOAW5PbS1GqZZGI9flWw2+Db9eYWOfEKy5svkrkQS/hnfCZj6Zz8YDujL8uVBv8iV9iZI0U2W3b27Pp6zzWH3nZYp6N2doP1s2cJ+MzpwpTZQOHvB9 Kk5IRW/e dzFeEgosuARbrGnL0J3OokgrFzK+cK8R4fJubpG7ZX3e9C+BmA5u9IqsgFpM+L0nkFzoI/ILuKSjC9YjFF8f4rwR+Bkc+L0SRNS8zemUqnKwxEqc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Currently, stack depot uses the following locking scheme: 1. Lock-free accesses when looking up a stack record, which allows to have multiple users to look up records in parallel; 2. Spinlock for protecting the stack depot pools and the hash table when adding a new record. For implementing the eviction of stack traces from stack depot, the lock-free approach is not going to work anymore, as we will need to be able to also remove records from the hash table. Convert the spinlock into a read/write lock, and drop the atomic accesses, as they are no longer required. Looking up stack traces is now protected by the read lock and adding new records - by the write lock. One of the following patches will add a new function for evicting stack records, which will be protected by the write lock as well. With this change, multiple users can still look up records in parallel. This is preparatory patch for implementing the eviction of stack records from the stack depot. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changed v2->v3: - Use lockdep_assert_held_read annotation in depot_fetch_stack. Changes v1->v2: - Add lockdep_assert annotations. --- lib/stackdepot.c | 87 +++++++++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 41 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 1067c072a0f8..b1ceade0acc9 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -91,15 +92,15 @@ static void *new_pool; static int pools_num; /* Next stack in the freelist of stack records within stack_pools. */ static struct stack_record *next_stack; -/* Lock that protects the variables above. */ -static DEFINE_RAW_SPINLOCK(pool_lock); /* * Stack depot tries to keep an extra pool allocated even before it runs out * of space in the currently used pool. This flag marks whether this extra pool * needs to be allocated. It has the value 0 when either an extra pool is not * yet allocated or if the limit on the number of pools is reached. */ -static int new_pool_required = 1; +static bool new_pool_required = true; +/* Lock that protects the variables above. */ +static DEFINE_RWLOCK(pool_rwlock); static int __init disable_stack_depot(char *str) { @@ -226,6 +227,8 @@ static void depot_init_pool(void *pool) const int records_in_pool = DEPOT_POOL_SIZE / DEPOT_STACK_RECORD_SIZE; int i, offset; + lockdep_assert_held_write(&pool_rwlock); + /* Initialize handles and link stack records to each other. */ for (i = 0, offset = 0; offset <= DEPOT_POOL_SIZE - DEPOT_STACK_RECORD_SIZE; @@ -248,22 +251,17 @@ static void depot_init_pool(void *pool) /* Save reference to the pool to be used by depot_fetch_stack(). */ stack_pools[pools_num] = pool; - - /* - * WRITE_ONCE() pairs with potential concurrent read in - * depot_fetch_stack(). - */ - WRITE_ONCE(pools_num, pools_num + 1); + pools_num++; } /* Keeps the preallocated memory to be used for a new stack depot pool. */ static void depot_keep_new_pool(void **prealloc) { + lockdep_assert_held_write(&pool_rwlock); + /* * If a new pool is already saved or the maximum number of * pools is reached, do not use the preallocated memory. - * Access new_pool_required non-atomically, as there are no concurrent - * write accesses to this variable. */ if (!new_pool_required) return; @@ -281,15 +279,15 @@ static void depot_keep_new_pool(void **prealloc) * At this point, either a new pool is kept or the maximum * number of pools is reached. In either case, take note that * keeping another pool is not required. - * smp_store_release() pairs with smp_load_acquire() in - * stack_depot_save(). */ - smp_store_release(&new_pool_required, 0); + new_pool_required = false; } /* Updates refences to the current and the next stack depot pools. */ static bool depot_update_pools(void **prealloc) { + lockdep_assert_held_write(&pool_rwlock); + /* Check if we still have objects in the freelist. */ if (next_stack) goto out_keep_prealloc; @@ -301,7 +299,7 @@ static bool depot_update_pools(void **prealloc) /* Take note that we might need a new new_pool. */ if (pools_num < DEPOT_MAX_POOLS) - smp_store_release(&new_pool_required, 1); + new_pool_required = true; /* Try keeping the preallocated memory for new_pool. */ goto out_keep_prealloc; @@ -335,6 +333,8 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) { struct stack_record *stack; + lockdep_assert_held_write(&pool_rwlock); + /* Update current and new pools if required and possible. */ if (!depot_update_pools(prealloc)) return NULL; @@ -370,18 +370,15 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) static struct stack_record *depot_fetch_stack(depot_stack_handle_t handle) { union handle_parts parts = { .handle = handle }; - /* - * READ_ONCE() pairs with potential concurrent write in - * depot_init_pool(). - */ - int pools_num_cached = READ_ONCE(pools_num); void *pool; size_t offset = parts.offset << DEPOT_STACK_ALIGN; struct stack_record *stack; - if (parts.pool_index > pools_num_cached) { + lockdep_assert_held_read(&pool_rwlock); + + if (parts.pool_index > pools_num) { WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n", - parts.pool_index, pools_num_cached, handle); + parts.pool_index, pools_num, handle); return NULL; } @@ -423,6 +420,8 @@ static inline struct stack_record *find_stack(struct stack_record *bucket, { struct stack_record *found; + lockdep_assert_held(&pool_rwlock); + for (found = bucket; found; found = found->next) { if (found->hash == hash && found->size == size && @@ -440,6 +439,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, depot_stack_handle_t handle = 0; struct page *page = NULL; void *prealloc = NULL; + bool need_alloc = false; unsigned long flags; u32 hash; @@ -459,22 +459,26 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, hash = hash_stack(entries, nr_entries); bucket = &stack_table[hash & stack_hash_mask]; - /* - * Fast path: look the stack trace up without locking. - * smp_load_acquire() pairs with smp_store_release() to |bucket| below. - */ - found = find_stack(smp_load_acquire(bucket), entries, nr_entries, hash); - if (found) + read_lock_irqsave(&pool_rwlock, flags); + + /* Fast path: look the stack trace up without full locking. */ + found = find_stack(*bucket, entries, nr_entries, hash); + if (found) { + read_unlock_irqrestore(&pool_rwlock, flags); goto exit; + } + + /* Take note if another stack pool needs to be allocated. */ + if (new_pool_required) + need_alloc = true; + + read_unlock_irqrestore(&pool_rwlock, flags); /* - * Check if another stack pool needs to be allocated. If so, allocate - * the memory now: we won't be able to do that under the lock. - * - * smp_load_acquire() pairs with smp_store_release() in - * depot_update_pools() and depot_keep_new_pool(). + * Allocate memory for a new pool if required now: + * we won't be able to do that under the lock. */ - if (unlikely(can_alloc && smp_load_acquire(&new_pool_required))) { + if (unlikely(can_alloc && need_alloc)) { /* * Zero out zone modifiers, as we don't have specific zone * requirements. Keep the flags related to allocation in atomic @@ -488,7 +492,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, prealloc = page_address(page); } - raw_spin_lock_irqsave(&pool_lock, flags); + write_lock_irqsave(&pool_rwlock, flags); found = find_stack(*bucket, entries, nr_entries, hash); if (!found) { @@ -497,11 +501,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, if (new) { new->next = *bucket; - /* - * smp_store_release() pairs with smp_load_acquire() - * from |bucket| above. - */ - smp_store_release(bucket, new); + *bucket = new; found = new; } } else if (prealloc) { @@ -512,7 +512,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, depot_keep_new_pool(&prealloc); } - raw_spin_unlock_irqrestore(&pool_lock, flags); + write_unlock_irqrestore(&pool_rwlock, flags); exit: if (prealloc) { /* Stack depot didn't use this memory, free it. */ @@ -536,6 +536,7 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, unsigned long **entries) { struct stack_record *stack; + unsigned long flags; *entries = NULL; /* @@ -547,8 +548,12 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, if (!handle || stack_depot_disabled) return 0; + read_lock_irqsave(&pool_rwlock, flags); + stack = depot_fetch_stack(handle); + read_unlock_irqrestore(&pool_rwlock, flags); + *entries = stack->entries; return stack->size; }