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: 55593 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1181872wrn; Fri, 10 Feb 2023 13:20:18 -0800 (PST) X-Google-Smtp-Source: AK7set9Wq+xL22pELKo1MH9QmXVC1ErYZ93kWo47jAwuXCAUZVW/83v84lKtGG1Zl8af30QpcjUC X-Received: by 2002:a17:907:3f93:b0:82e:a57b:cc9b with SMTP id hr19-20020a1709073f9300b0082ea57bcc9bmr14936350ejc.24.1676064018301; Fri, 10 Feb 2023 13:20:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676064018; cv=none; d=google.com; s=arc-20160816; b=vctq0aKbgsBAgjN9fho3+ObnlakSeQa4f7Xl5iIVq+yUuaZcPoZaGdKMN7UQyZ7/wW K+FKh+6mXbNNBuoa+SiJl+6Wyuazl6qYQ/I/8Snyv2B412K9k/vetyarY8YQNZU/2SH1 36r0uwJwe6hWzcfEnoGi1s7g7HLXHaHGzzmUd+fmMNHv1wIUthMFsL1MwdaX0exmqjQO VQHh7AKdVwfosv76FFv/qztFoJaDJqSom1SiQSajCKhn7TYKZrlhM/qG4m+FMxBfIQkG pa9ih0Djrj+PN2yZjFNwNPK/eummC3QHkmmBuzUAWnqHAq2An3L301nLlZfIESHuCeK0 TbQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Qnr9u9smPMPXsKAXlYWG4fUkkGTB3Zxw4vbQvgaYcVg=; b=fUbuykygZNdMADztUCJunXH8WgR4S4HuvwFt2GKONuuPUZ4cBTpPL6UtT4zHJ8x4OX KzgQw5BEdpMbF2RzP3/giFruJM+RO3aQs0cQ3h5aRgpAmEmy3SRTtc0aDFlvq3vQU5cx Tm7YQM7M7qMGPbUz21oHdKAnbcMSsd4RQcIN6xtkL0ANLozGffQc4+Te2uQDcIPNn+oW NQUsoA+K00pP4hGw5Z9dLE8e45FZlYssSuD94OZVv3ku/qsdwftR7e8XTDzfwEJc9U4/ HJeYkyBlM7wM7byyDLz9E6RNLYgjRAE3/gppVFSfMNhgunKoOdzPf/Gv+O1tElaYkUVN DHmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=t7vDpQcZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cq21-20020a170906d51500b008aabf1f76dasi6245043ejc.583.2023.02.10.13.19.54; Fri, 10 Feb 2023 13:20:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=t7vDpQcZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233829AbjBJVTA (ORCPT + 99 others); Fri, 10 Feb 2023 16:19:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232950AbjBJVSz (ORCPT ); Fri, 10 Feb 2023 16:18:55 -0500 Received: from out-191.mta1.migadu.com (out-191.mta1.migadu.com [IPv6:2001:41d0:203:375::bf]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0F7E81CC6 for ; Fri, 10 Feb 2023 13:18:26 -0800 (PST) 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-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757480503845440351?= X-GMAIL-MSGID: =?utf-8?q?1757480503845440351?= 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;