Message ID | 19512bb03eed27ced5abeb5bd03f9a8381742cb1.1675111415.git.andreyknvl@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2393629wrn; Mon, 30 Jan 2023 13:00:35 -0800 (PST) X-Google-Smtp-Source: AMrXdXt/3D+xfZ/e+SOEC2AFLuslGnoQn2BRjw44oQKABzC9PNWzG8sbgx2C/cLo/zOIDLk/VyHK X-Received: by 2002:a05:6402:5299:b0:49c:5a27:c1fa with SMTP id en25-20020a056402529900b0049c5a27c1famr54671397edb.24.1675112435219; Mon, 30 Jan 2023 13:00:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675112435; cv=none; d=google.com; s=arc-20160816; b=k0WbirKaXK6JpkQAp+kNvUumvfDXWAnEW2laI6ItNxgxvJMzcMF7wGM7AdtcIivBm0 HaEIyml0HYEFx86ZPZ2fqhVWy2W/dy60S13SeY7brWHunjjmTfPwK4q7lMDuBGpWC4Lk JNuRT3KzdLfSx//sj0fQzFQgWVAH6ypFtWKdsbowMuZnYIsqEXfmGPejt8WVMkdYSEqJ nBD6FB6sDBuM12NoKgz9ifCKioJoVQter8UH+paM+cWiwKp9ONYeTMdMkO5B/0bpG3nQ 3/1W7jQnu0F51Q+E+iM7T7zghNGWV1QGfvbIhjlKzWosFTesWUPEgSupLojvu0YLAsA9 Ee/w== 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=HatpIAXF9ywMATjIlRO5zkwkJoWVcZESn/qWFP01cUE=; b=EWcNWdAqphhUQKz4Zc6et1l8yBTtfAxQFagNCMXRbdfovZI1syYJVFpKIpv6LzRM1d M4UMlpNP399UtDvlmzn1bXCT/4ka59VwtgP96CIQllFrYqch7/Qbx7HAF9A9zXGGOEmw mF2LRzGf+3EUkkaVarwFLqy0fZzlPOScSC2LA9IQ8BOGCjOJpATxhijKSi8aGR29xc5I lToyhTw5RyJILIHBM70cl38ZOhkyWn57LsCGi1wrxrLrckCKS4edjH4/u5V630z057CJ gxvjL5f5qgIDqC0zwlE3z4ERm72teMr0d0iioyKByWJ1I5gIV/lSbToIxsNv/F260z/P qH8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=wXtDfrkY; 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 ui7-20020a170907c90700b00888a606c89esi3774805ejc.585.2023.01.30.13.00.10; Mon, 30 Jan 2023 13:00:35 -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=wXtDfrkY; 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 S230071AbjA3Uw1 (ORCPT <rfc822;maxin.john@gmail.com> + 99 others); Mon, 30 Jan 2023 15:52:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230040AbjA3UwM (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 30 Jan 2023 15:52:12 -0500 Received: from out-80.mta0.migadu.com (out-80.mta0.migadu.com [91.218.175.80]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5741F45220 for <linux-kernel@vger.kernel.org>; Mon, 30 Jan 2023 12:51:58 -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=1675111916; 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=HatpIAXF9ywMATjIlRO5zkwkJoWVcZESn/qWFP01cUE=; b=wXtDfrkYlD8IpL/lQ8l++bN1YdtjxhnwL1anSh49Js2VOzOsVnOBIPbJSklgemF2REahtq DvdvaNag4m/GqwVZu03XHDBsOWpGjVLbIuihC73epl6av328Yy8aLHk/DlbB90DLAwCqg2 4Z44QfKvcXgws9zhTONc9RpzBkBuxac= From: andrey.konovalov@linux.dev To: Marco Elver <elver@google.com>, Alexander Potapenko <glider@google.com> Cc: Andrey Konovalov <andreyknvl@gmail.com>, Vlastimil Babka <vbabka@suse.cz>, kasan-dev@googlegroups.com, Evgenii Stepanov <eugenis@google.com>, Andrew Morton <akpm@linux-foundation.org>, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov <andreyknvl@google.com> Subject: [PATCH 16/18] lib/stackdepot: annotate racy slab_index accesses Date: Mon, 30 Jan 2023 21:49:40 +0100 Message-Id: <19512bb03eed27ced5abeb5bd03f9a8381742cb1.1675111415.git.andreyknvl@google.com> In-Reply-To: <cover.1675111415.git.andreyknvl@google.com> References: <cover.1675111415.git.andreyknvl@google.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756482697313529542?= X-GMAIL-MSGID: =?utf-8?q?1756482697313529542?= |
Series |
lib/stackdepot: fixes and clean-ups
|
|
Commit Message
andrey.konovalov@linux.dev
Jan. 30, 2023, 8:49 p.m. UTC
From: Andrey Konovalov <andreyknvl@google.com> Accesses to slab_index are protected by slab_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 <andreyknvl@google.com> --- lib/stackdepot.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
Comments
On Mon, 30 Jan 2023 at 21:51, <andrey.konovalov@linux.dev> wrote: > > From: Andrey Konovalov <andreyknvl@google.com> > > Accesses to slab_index are protected by slab_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 <andreyknvl@google.com> > --- > lib/stackdepot.c | 13 +++++++++---- > 1 file changed, 9 insertions(+), 4 deletions(-) > > diff --git a/lib/stackdepot.c b/lib/stackdepot.c > index f291ad6a4e72..cc2fe8563af4 100644 > --- a/lib/stackdepot.c > +++ b/lib/stackdepot.c > @@ -269,8 +269,11 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) > return NULL; > } > > - /* Move on to the next slab. */ > - slab_index++; > + /* > + * Move on to the next slab. > + * WRITE_ONCE annotates a race with stack_depot_fetch. "Pairs with potential concurrent read in stack_depot_fetch()." would be clearer. I wouldn't say WRITE_ONCE annotates a race (race = involves 2+ accesses, but here's just 1), it just marks this access here which itself is paired with the potential racing read in the other function. > + */ > + WRITE_ONCE(slab_index, slab_index + 1); > slab_offset = 0; > /* > * smp_store_release() here pairs with smp_load_acquire() in > @@ -492,6 +495,8 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, > unsigned long **entries) > { > union handle_parts parts = { .handle = handle }; > + /* READ_ONCE annotates a race with depot_alloc_stack. */ > + int slab_index_cached = READ_ONCE(slab_index); > void *slab; > size_t offset = parts.offset << DEPOT_STACK_ALIGN; > struct stack_record *stack; > @@ -500,9 +505,9 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, > if (!handle) > return 0; > > - if (parts.slab_index > slab_index) { > + if (parts.slab_index > slab_index_cached) { > WARN(1, "slab index %d out of bounds (%d) for stack id %08x\n", > - parts.slab_index, slab_index, handle); > + parts.slab_index, slab_index_cached, handle); > return 0; > } > slab = stack_slabs[parts.slab_index]; > -- > 2.25.1 >
On Tue, Jan 31, 2023 at 9:41 AM Marco Elver <elver@google.com> wrote: > > > diff --git a/lib/stackdepot.c b/lib/stackdepot.c > > index f291ad6a4e72..cc2fe8563af4 100644 > > --- a/lib/stackdepot.c > > +++ b/lib/stackdepot.c > > @@ -269,8 +269,11 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) > > return NULL; > > } > > > > - /* Move on to the next slab. */ > > - slab_index++; > > + /* > > + * Move on to the next slab. > > + * WRITE_ONCE annotates a race with stack_depot_fetch. > > "Pairs with potential concurrent read in stack_depot_fetch()." would be clearer. > > I wouldn't say WRITE_ONCE annotates a race (race = involves 2+ > accesses, but here's just 1), it just marks this access here which > itself is paired with the potential racing read in the other function. Will do in v2. Thanks!
On Tue, 31 Jan 2023 19:57:58 +0100 Andrey Konovalov <andreyknvl@gmail.com> wrote: > On Tue, Jan 31, 2023 at 9:41 AM Marco Elver <elver@google.com> wrote: > > > > > diff --git a/lib/stackdepot.c b/lib/stackdepot.c > > > index f291ad6a4e72..cc2fe8563af4 100644 > > > --- a/lib/stackdepot.c > > > +++ b/lib/stackdepot.c > > > @@ -269,8 +269,11 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) > > > return NULL; > > > } > > > > > > - /* Move on to the next slab. */ > > > - slab_index++; > > > + /* > > > + * Move on to the next slab. > > > + * WRITE_ONCE annotates a race with stack_depot_fetch. > > > > "Pairs with potential concurrent read in stack_depot_fetch()." would be clearer. > > > > I wouldn't say WRITE_ONCE annotates a race (race = involves 2+ > > accesses, but here's just 1), it just marks this access here which > > itself is paired with the potential racing read in the other function. > > Will do in v2. Thanks! Please let's not redo an 18-patch series for a single line comment change. If there are more substantial changes then OK. I queued this as a to-be-squashed fixup against "/stackdepot: annotate racy slab_index accesses": From: Andrew Morton <akpm@linux-foundation.org> Subject: lib-stackdepot-annotate-racy-slab_index-accesses-fix Date: Tue Jan 31 01:10:50 PM PST 2023 enhance comment, per Marco Cc: Alexander Potapenko <glider@google.com> Cc: Andrey Konovalov <andreyknvl@google.com> Cc: Evgenii Stepanov <eugenis@google.com> Cc: Marco Elver <elver@google.com> Cc: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> --- lib/stackdepot.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) --- a/lib/stackdepot.c~lib-stackdepot-annotate-racy-slab_index-accesses-fix +++ a/lib/stackdepot.c @@ -271,7 +271,8 @@ depot_alloc_stack(unsigned long *entries /* * Move on to the next slab. - * WRITE_ONCE annotates a race with stack_depot_fetch. + * WRITE_ONCE pairs with potential concurrent read in + * stack_depot_fetch(). */ WRITE_ONCE(slab_index, slab_index + 1); slab_offset = 0;
diff --git a/lib/stackdepot.c b/lib/stackdepot.c index f291ad6a4e72..cc2fe8563af4 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -269,8 +269,11 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) return NULL; } - /* Move on to the next slab. */ - slab_index++; + /* + * Move on to the next slab. + * WRITE_ONCE annotates a race with stack_depot_fetch. + */ + WRITE_ONCE(slab_index, slab_index + 1); slab_offset = 0; /* * smp_store_release() here pairs with smp_load_acquire() in @@ -492,6 +495,8 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, unsigned long **entries) { union handle_parts parts = { .handle = handle }; + /* READ_ONCE annotates a race with depot_alloc_stack. */ + int slab_index_cached = READ_ONCE(slab_index); void *slab; size_t offset = parts.offset << DEPOT_STACK_ALIGN; struct stack_record *stack; @@ -500,9 +505,9 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, if (!handle) return 0; - if (parts.slab_index > slab_index) { + if (parts.slab_index > slab_index_cached) { WARN(1, "slab index %d out of bounds (%d) for stack id %08x\n", - parts.slab_index, slab_index, handle); + parts.slab_index, slab_index_cached, handle); return 0; } slab = stack_slabs[parts.slab_index];