From patchwork Fri Feb 10 21:16:04 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: 13136465 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 482B6C6379F for ; Fri, 10 Feb 2023 21:18:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 527096B019E; Fri, 10 Feb 2023 16:18:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 488146B01A0; Fri, 10 Feb 2023 16:18:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 303D06B01A1; Fri, 10 Feb 2023 16:18:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 1EADC6B019E for ; Fri, 10 Feb 2023 16:18:23 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id F20FD807E3 for ; Fri, 10 Feb 2023 21:18:22 +0000 (UTC) X-FDA: 80452645644.07.B4D80BA Received: from out-33.mta1.migadu.com (out-33.mta1.migadu.com [95.215.58.33]) by imf06.hostedemail.com (Postfix) with ESMTP id 2286B180009 for ; Fri, 10 Feb 2023 21:18:20 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=t7vDpQcZ; spf=pass (imf06.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.33 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676063901; 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=Qnr9u9smPMPXsKAXlYWG4fUkkGTB3Zxw4vbQvgaYcVg=; b=4/ABSra0oaVMfwgUmnNpOCz2sljyHvf8+LDTCmf4DtEk0vDylN068tLjREN7M3lrje4Gia +ua9NGR8FvIiy1uZU+2vUzfgvutCIJ2N+WP39MUAwkN3u9q+6n21UNtU2QA/ndNR82t8yp 6/UgQEJrUkC270AXf2lRwgGaDxUi+SE= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=t7vDpQcZ; spf=pass (imf06.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.33 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676063901; a=rsa-sha256; cv=none; b=bblhBegu4BizfBDUnWHNGXWu/EIvNoKTVF8jIIuk9rSxo0PprkztLqDbCfnz4B5QTTm0da QQ8FLLNr7eXNoTF6RtR1pV2YxIJK6GtykK2bA7H36avSxTCN4V6SiNP4JpRIZ6llnCBuxK 0oQbKHVG4zEjADm5bvQCE8LQ3DpW8CE= 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=1676063899; 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=Qnr9u9smPMPXsKAXlYWG4fUkkGTB3Zxw4vbQvgaYcVg=; b=t7vDpQcZUJdsleERJ1PgO38Pvi8xFhTNn74zLonJYcITtx61e02XeD2vPBg9c//+BpeG1V s7T+qicncVYyTp2KtBAyctdyaGSJ7zdRMIgR7n9TfK44wSuf3yHrV92DaF9e4tXpgOOkwx I+Gi5V7tNQW76e1Nfyz/DJq/+nVFLGI= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 16/18] lib/stackdepot: annotate racy pool_index accesses Date: Fri, 10 Feb 2023 22:16:04 +0100 Message-Id: <359ac9c13cd0869c56740fb2029f505e41593830.1676063693.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Stat-Signature: e4huhf54figtuma9sz8sg5goe78t6zpi X-Rspam-User: X-Rspamd-Queue-Id: 2286B180009 X-Rspamd-Server: rspam06 X-HE-Tag: 1676063900-36848 X-HE-Meta: U2FsdGVkX1+ybCgC1YgIyA9D4nIIy4CcQza960XAwT0Hp8iG22eiX6gVcdvL+nwJ0zBVB9cDKgR5VXuDFk2nZUSDo1Kv7Buo2m9WD17PzgiG491FfjC4uLnd8hRz9/a5W0iTyWyO90Bkv3YS3+wCh0VrGEIN7pTCuLmOeYqHjiKWuobH3fQ5YAEzl7KjrHVcgqPJ+WM1SZPeVqasq4n9AK+RVlpbhRmws17TAfXRJ1TMXNaVhb+Z6EiewkWTuMZwOBlStm49bjjEwG8gZHET0QANqUo3Ek0POfpyzV+txkuEqyXEWJD5C1863ZhpAgxQPRM802YZ1Dt5NchQDMq9vZExXPodfT7eEY/jFfQjzcJkPTku+qCeTGrYDoSoC9L1Dj8TPFlDfU5YGl9vf4EB9n9O19lPpUnHabPLrD2vqrPO7CbAqtdMINtk8GZq3t7x2JedvhLX/jDbAhhncFc0oS46rnX8e88h0ZJgRC8LJMqgVo5305JnJzcRMdb1nObbVwbmggIiJZJMKWHutJZfQ/Z/MpGnJIwQvRBY8IlhPy/9Z/2Ag64s4rJudJewI6sp7nBbb5V7axK2Lcd0h/YBZzSRRz3oyCSTUZt4n+hqLNRXQIJltg+T8qWOwHGZyCwcLWYdExCuqI53pSuK4HAFZTCcuNwoeKFfrRMqH/7AodbJSMof8yJcYUpALk6u648a8R4LFJQTtkFsgMP479Nu1Hnanb9Aq88pKsvr2I/nInvT2XJsAR5R9atV0ofHgRyI9Z3FHOnV6J7q00KivRche4uz1nHkdHe15AM0f0PoYLAeS6FEPHT8oip98i7bA5q/3ER76zfzPD29mhBr689oF+8RproBzGqImZVVww0xu5LD8GxFg3QEKyA2MyBgGqGFr5hzV6B41C/1kiNHYWZfUrsUDu+gUew8prYpv7a81aCPTJoJgUvLaOIlmKUkA6UDJGYm4D2xALqB8b6xdr+ veqjGOGS mbLX7Z1RV9tebUWkZOIh2JfrwiDpAKSGJPYjtGZMFyXzsm0V+InVpB5xr9VpazjzRmNqSrLr7I2Lmko4WdoWI7+4yy8S4HosmW7pJP0+Aj0vNpYzX7cSmtqyJrNggyuPWYLwv0VTsVoyp0czZknb1GZwWpQ== 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: From: Andrey Konovalov Accesses to pool_index are protected by pool_lock everywhere except in a sanity check in stack_depot_fetch. The read access there can race with the write access in depot_alloc_stack. Use WRITE/READ_ONCE() to annotate the racy accesses. As the sanity check is only used to print a warning in case of a violation of the stack depot interface usage, it does not make a lot of sense to use proper synchronization. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- Changes v1->v2: - Improve comments as suggested by Marco. --- lib/stackdepot.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 8c6e4e9cb535..684c2168bed9 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -278,8 +278,12 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) return NULL; } - /* Move on to the next pool. */ - pool_index++; + /* + * Move on to the next pool. + * WRITE_ONCE pairs with potential concurrent read in + * stack_depot_fetch(). + */ + WRITE_ONCE(pool_index, pool_index + 1); pool_offset = 0; /* * If the maximum number of pools is not reached, take note @@ -502,6 +506,11 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, unsigned long **entries) { union handle_parts parts = { .handle = handle }; + /* + * READ_ONCE pairs with potential concurrent write in + * depot_alloc_stack. + */ + int pool_index_cached = READ_ONCE(pool_index); void *pool; size_t offset = parts.offset << DEPOT_STACK_ALIGN; struct stack_record *stack; @@ -510,7 +519,7 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, if (!handle) return 0; - if (parts.pool_index > pool_index) { + if (parts.pool_index > pool_index_cached) { WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n", parts.pool_index, pool_index, handle); return 0;