Message ID | 9fbb4d2bf9b2676a29b120980b5ffbda8e2304ee.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 s9csp2395188wrn; Mon, 30 Jan 2023 13:03:19 -0800 (PST) X-Google-Smtp-Source: AK7set/CgzCjDA5nOlf1tnVp0F+KuzwegQ1rt7R099hvNrSX7ZuAj/Fxj8h70k77JZZPYVLWsiTM X-Received: by 2002:a17:906:44d:b0:878:816f:8691 with SMTP id e13-20020a170906044d00b00878816f8691mr14929067eja.71.1675112598923; Mon, 30 Jan 2023 13:03:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675112598; cv=none; d=google.com; s=arc-20160816; b=cJ1aG2/7BbsopprEobNwT4EoIeK2H/s2Vyx61OwaZZBt6gUK24zYMbLRRH2XXRHOzY gQRMu7T+q91odaV8sVeuBDFe4VtwPJIZCRs39c2XMMFsRuJo19OTaESsSVZAUIpXMdgt RGos0B3YOVt6NB+QfqQwCjob48o6qV89ziLG2sxBm/LsW3N6BBrK5NqTNmp0krEN8sUh 8hC3JwLiOP1phHcf6RRKnaM0sokH8zBBr4j0SpvDLyLBBSprEORYJFydIGjfGtsmbJSu 6h/5N+jWk5CnjnZFeT88v5UVId7vd2DngiXP4NZHgjqq4WpaKEgCnT28D1lRqh7KQGLn lYVw== 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=vpL8/NdYX1/weZXFrNrkU0jpgSm404fZwOGAfIO2ueo=; b=aL/SU+DFR/GGN2TFi+Jf2P00xbD1ewdpdWAhQhg2ouHdriH0Y6/LYJMKjMxSOTSKj/ TUpb6vm+DjAbLJwCxEKhxJ5Q73pad237fQgKrfxnGleM2k8PX3qcge3n6GO3wZMIBNES nFRGqVEJMF8odhFhloRJqYDxR/DWxdEel3gKOUP6ZAMYEltSCoL7+DjpKC13NBu7PSok 8puY1303eNmF8l3LrQW9HDFfmCwpHKiwRHih3DHh0MSam78ztahrQ/hiOVcO6M7KQudy Rye/LGITHRDGBy8x9yUwPwGVg6VQvDmDh7c1v8gruxgzYarMHM50+xa14QD551m1V3VY ZcCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="l/E56Ec3"; 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 gk4-20020a17090790c400b00886971a7fc7si6384927ejb.732.2023.01.30.13.02.54; Mon, 30 Jan 2023 13:03: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="l/E56Ec3"; 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 S229836AbjA3U4M (ORCPT <rfc822;maxin.john@gmail.com> + 99 others); Mon, 30 Jan 2023 15:56:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229546AbjA3U4L (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 30 Jan 2023 15:56:11 -0500 X-Greylist: delayed 382 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Mon, 30 Jan 2023 12:56:10 PST Received: from out-195.mta1.migadu.com (out-195.mta1.migadu.com [95.215.58.195]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A3182A15A for <linux-kernel@vger.kernel.org>; Mon, 30 Jan 2023 12:56:10 -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=1675111786; 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=vpL8/NdYX1/weZXFrNrkU0jpgSm404fZwOGAfIO2ueo=; b=l/E56Ec3WqQvEhqdZjl8IUp453ZZ1+XVexohawe9WXRwZLVX9lAj0fJNkLKpd1L0XbJ/cZ r7uaM/Wl4isiyHyMF6tbOKjX8DK6rY8u5FVwTS72y6me8Wdk0ledgHuQWb2LrJtfpWi2lC Ire9k++4IxKra40cnB4YNO9o09pIkPQ= 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 01/18] lib/stackdepot: fix setting next_slab_inited in init_stack_slab Date: Mon, 30 Jan 2023 21:49:25 +0100 Message-Id: <9fbb4d2bf9b2676a29b120980b5ffbda8e2304ee.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?1756482868511311815?= X-GMAIL-MSGID: =?utf-8?q?1756482868511311815?= |
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> In commit 305e519ce48e ("lib/stackdepot.c: fix global out-of-bounds in stack_slabs"), init_stack_slab was changed to only use preallocated memory for the next slab if the slab number limit is not reached. However, setting next_slab_inited was not moved together with updating stack_slabs. Set next_slab_inited only if the preallocated memory was used for the next slab. Fixes: 305e519ce48e ("lib/stackdepot.c: fix global out-of-bounds in stack_slabs") Signed-off-by: Andrey Konovalov <andreyknvl@google.com> --- lib/stackdepot.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
Comments
On Mon, 30 Jan 2023 21:49:25 +0100 andrey.konovalov@linux.dev wrote: > In commit 305e519ce48e ("lib/stackdepot.c: fix global out-of-bounds in > stack_slabs"), init_stack_slab was changed to only use preallocated > memory for the next slab if the slab number limit is not reached. > However, setting next_slab_inited was not moved together with updating > stack_slabs. > > Set next_slab_inited only if the preallocated memory was used for the > next slab. Please provide a full description of the user-visible runtime effects of the bug (always always). I'll add the cc:stable (per your comments in the [0/N] cover letter), but it's more reliable to add it to the changelog yourself. As to when I upstream this: don't know - that depends on the user-visible-effects thing.
On Mon, Jan 30, 2023 at 9:49 PM <andrey.konovalov@linux.dev> wrote: > > From: Andrey Konovalov <andreyknvl@google.com> > > In commit 305e519ce48e ("lib/stackdepot.c: fix global out-of-bounds in > stack_slabs"), init_stack_slab was changed to only use preallocated > memory for the next slab if the slab number limit is not reached. > However, setting next_slab_inited was not moved together with updating > stack_slabs. > > Set next_slab_inited only if the preallocated memory was used for the > next slab. > > Fixes: 305e519ce48e ("lib/stackdepot.c: fix global out-of-bounds in stack_slabs") > Signed-off-by: Andrey Konovalov <andreyknvl@google.com> Reviewed-by: Alexander Potapenko <glider@google.com>
On Mon, Jan 30, 2023 at 9:49 PM <andrey.konovalov@linux.dev> wrote: > > From: Andrey Konovalov <andreyknvl@google.com> > > In commit 305e519ce48e ("lib/stackdepot.c: fix global out-of-bounds in > stack_slabs"), init_stack_slab was changed to only use preallocated > memory for the next slab if the slab number limit is not reached. > However, setting next_slab_inited was not moved together with updating > stack_slabs. > > Set next_slab_inited only if the preallocated memory was used for the > next slab. > > Fixes: 305e519ce48e ("lib/stackdepot.c: fix global out-of-bounds in stack_slabs") > Signed-off-by: Andrey Konovalov <andreyknvl@google.com> Wait, I think there's a problem here. > diff --git a/lib/stackdepot.c b/lib/stackdepot.c > index 79e894cf8406..0eed9bbcf23e 100644 > --- a/lib/stackdepot.c > +++ b/lib/stackdepot.c > @@ -105,12 +105,13 @@ static bool init_stack_slab(void **prealloc) > if (depot_index + 1 < STACK_ALLOC_MAX_SLABS) { If we get to this branch, but the condition is false, this means that: - next_slab_inited == 0 - depot_index == STACK_ALLOC_MAX_SLABS+1 - stack_slabs[depot_index] != NULL. So stack_slabs[] is at full capacity, but upon leaving init_stack_slab() we'll always keep next_slab_inited==0. Now every time __stack_depot_save() is called for a known stack trace, it will preallocate 1<<STACK_ALLOC_ORDER pages (because next_slab_inited==0), then find the stack trace id in the hash, then pass the preallocated pages to init_stack_slab(), which will not change the value of next_slab_inited. Then the preallocated pages will be freed, and next time __stack_depot_save() is called they'll be allocated again.
On Tue, Jan 31, 2023 at 10:30 AM Alexander Potapenko <glider@google.com> wrote: > > Wait, I think there's a problem here. > > > diff --git a/lib/stackdepot.c b/lib/stackdepot.c > > index 79e894cf8406..0eed9bbcf23e 100644 > > --- a/lib/stackdepot.c > > +++ b/lib/stackdepot.c > > @@ -105,12 +105,13 @@ static bool init_stack_slab(void **prealloc) > > if (depot_index + 1 < STACK_ALLOC_MAX_SLABS) { > If we get to this branch, but the condition is false, this means that: > - next_slab_inited == 0 > - depot_index == STACK_ALLOC_MAX_SLABS+1 > - stack_slabs[depot_index] != NULL. > > So stack_slabs[] is at full capacity, but upon leaving > init_stack_slab() we'll always keep next_slab_inited==0. > > Now every time __stack_depot_save() is called for a known stack trace, > it will preallocate 1<<STACK_ALLOC_ORDER pages (because > next_slab_inited==0), then find the stack trace id in the hash, then > pass the preallocated pages to init_stack_slab(), which will not > change the value of next_slab_inited. > Then the preallocated pages will be freed, and next time > __stack_depot_save() is called they'll be allocated again. Ah, right, missed that. What do you think about renaming next_slab_inited to next_slab_required and inverting the used values (0/1 -> 1/0)? This would make this part of code less confusing.
On Tue, Jan 31, 2023 at 1:18 AM Andrew Morton <akpm@linux-foundation.org> wrote: > > On Mon, 30 Jan 2023 21:49:25 +0100 andrey.konovalov@linux.dev wrote: > > > In commit 305e519ce48e ("lib/stackdepot.c: fix global out-of-bounds in > > stack_slabs"), init_stack_slab was changed to only use preallocated > > memory for the next slab if the slab number limit is not reached. > > However, setting next_slab_inited was not moved together with updating > > stack_slabs. > > > > Set next_slab_inited only if the preallocated memory was used for the > > next slab. > > Please provide a full description of the user-visible runtime effects > of the bug (always always). > > I'll add the cc:stable (per your comments in the [0/N] cover letter), > but it's more reliable to add it to the changelog yourself. Right, will do this next time. > As to when I upstream this: don't know - that depends on the > user-visible-effects thing. Looks like there's no bug to fix after all as per comments by Alexander. Thanks!
On Tue, Jan 31, 2023 at 8:00 PM Andrey Konovalov <andreyknvl@gmail.com> wrote: > > On Tue, Jan 31, 2023 at 10:30 AM Alexander Potapenko <glider@google.com> wrote: > > > > Wait, I think there's a problem here. > > > > > diff --git a/lib/stackdepot.c b/lib/stackdepot.c > > > index 79e894cf8406..0eed9bbcf23e 100644 > > > --- a/lib/stackdepot.c > > > +++ b/lib/stackdepot.c > > > @@ -105,12 +105,13 @@ static bool init_stack_slab(void **prealloc) > > > if (depot_index + 1 < STACK_ALLOC_MAX_SLABS) { > > If we get to this branch, but the condition is false, this means that: > > - next_slab_inited == 0 > > - depot_index == STACK_ALLOC_MAX_SLABS+1 > > - stack_slabs[depot_index] != NULL. > > > > So stack_slabs[] is at full capacity, but upon leaving > > init_stack_slab() we'll always keep next_slab_inited==0. > > > > Now every time __stack_depot_save() is called for a known stack trace, > > it will preallocate 1<<STACK_ALLOC_ORDER pages (because > > next_slab_inited==0), then find the stack trace id in the hash, then > > pass the preallocated pages to init_stack_slab(), which will not > > change the value of next_slab_inited. > > Then the preallocated pages will be freed, and next time > > __stack_depot_save() is called they'll be allocated again. > > Ah, right, missed that. > > What do you think about renaming next_slab_inited to > next_slab_required and inverting the used values (0/1 -> 1/0)? This > would make this part of code less confusing. "Required" as in "requires a preallocated buffer, but does not have one yet"? Yes, that's probably better. (In any case we'll need to add a comment to that variable explaining the circumstances under which one or another value is possible).
diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 79e894cf8406..0eed9bbcf23e 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -105,12 +105,13 @@ static bool init_stack_slab(void **prealloc) 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); } - /* - * 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; }