From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ABFB51F192E for ; Fri, 16 Jan 2026 02:38:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768531101; cv=none; b=kXIpDzFqYM2q84eiEe8YxDPpdW4nEF0e53QYkZ1OTXEr236ypoAW6IJvpPCUtwGlZ4JhfGN7BtxhwlpRq3kibc89U6+HAMI5aS0jJrEs5sz9+s5Hmj+kxCpszpHGmCgfEA4tdmeqH54ETVXZRWlooNEmmVJyCELuYjE/19ur/tI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768531101; c=relaxed/simple; bh=p+22kgeBsgnSIyq5SAh8/KKOsILQTuQeuv+YPGKAS/w=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=VpeVergZQuVzVoUfXSv7HrQFPD4g56iSIAsKxVBjivxCNBtne5YFk4k5b4Ib9j1Xszza5uLyq1ueobs+HSIndIBvBaNPO3xceKNh5prABzS3otecomxJxEcxU7tvEqYCrWXZGTlIUwxebOkFU6PdZfJABi6x+y2diFTfImKJeJc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LA7feMTf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LA7feMTf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 05933C116D0; Fri, 16 Jan 2026 02:38:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1768531101; bh=p+22kgeBsgnSIyq5SAh8/KKOsILQTuQeuv+YPGKAS/w=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=LA7feMTf5OhQNkBxcSbWrXVn4VpbI1mpLuEplow35p+lglgr/i3cHbCbLpgGYsssi SDfvHpDdFCeja60UyQP2UOiC8rOUZ9ZJjbSQHXeoO3eD5bwfESBvkX93EmCvQ6WLyn hUzG05qMfTFPDS6sWHuoYgn5x9JhgDyAD1IibfdmicjeYIG024TdSYsxe6zrFUbJxp brXit+fGINNUJ6ODTY6WfN9b84Aqzj9jbsy+8aFuCP2dRlOMQiCvOybsR+V6Of2oF9 Fc5NBSXAVH5YBinvOmzvjxtzw/svnNQtUoR56lv4fOv1o6Y6j4xtXvNbmSIHiMX/fn p8Ch8zaaqTkNA== Date: Thu, 15 Jan 2026 18:38:19 -0800 From: Dennis Zhou To: Chris Mason Cc: Tejun Heo , Christoph Lameter , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Sebastian Andrzej Siewior Subject: Re: [PATCH] percpu: add basic double free check Message-ID: References: <20251220002737.84100-1-dennis@kernel.org> <20260114122209.1075584-1-clm@meta.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260114122209.1075584-1-clm@meta.com> Hi Chris, On Wed, Jan 14, 2026 at 04:22:00AM -0800, Chris Mason wrote: > On Fri, 19 Dec 2025 16:27:37 -0800 Dennis Zhou wrote: > > > This adds a basic double free check by validating the first bit of the > > allocation in alloc_map and bound_map are set. If the alloc_map bit is > > not set, then this means the area is currently unallocated. If the > > bound_map bit is not set, then we are not freeing from the beginning of > > the allocation. > > Hi Dennis, > > I ran mm-new through the AI review prompts, and this one was flagged: > Wow that's cool, thanks! > > diff --git a/mm/percpu.c b/mm/percpu.c > > --- a/mm/percpu.c > > +++ b/mm/percpu.c > > [ ... ] > > > @@ -1285,6 +1286,11 @@ static int pcpu_free_area(struct pcpu_chunk *chunk, int off) > > > > bit_off = off / PCPU_MIN_ALLOC_SIZE; > > > > + /* check double free */ > > + if (!test_bit(bit_off, chunk->alloc_map) || > > + !test_bit(bit_off, chunk->bound_map)) > > + return 0; > > Does this early return corrupt pcpu_stats.nr_cur_alloc and > chunk->nr_alloc? > > Looking at pcpu_free_area(), earlier in the function (before this new > check) there is a call to pcpu_stats_area_dealloc(chunk) which decrements > these counters: > > static int pcpu_free_area(struct pcpu_chunk *chunk, int off) > { > ... > lockdep_assert_held(&pcpu_lock); > pcpu_stats_area_dealloc(chunk); <-- decrements counters here > ... > bit_off = off / PCPU_MIN_ALLOC_SIZE; > > /* check double free */ > if (!test_bit(bit_off, chunk->alloc_map) || > !test_bit(bit_off, chunk->bound_map)) > return 0; <-- returns without actual free > ... > } > > When a double-free is detected: > > 1. pcpu_stats_area_dealloc() runs, decrementing pcpu_stats.nr_cur_alloc > and chunk->nr_alloc > 2. The check detects the double-free and returns 0 > 3. No actual memory is freed, but the counters are now wrong > > Should pcpu_stats_area_dealloc() be moved after the double-free check, > or should there be a compensating increment on the early return path? I moved pcpu_stats_area_dealloc() to the end of the function in v2. > > This could also affect percpu_stats_show() in mm/percpu-stats.c, which > uses chunk->nr_alloc via find_max_nr_alloc() to size a buffer: > > buffer = vmalloc_array(2 * max_nr_alloc + 1, sizeof(int)); > > If nr_alloc is underreported due to this bug, the buffer may be undersized > for the actual number of allocations tracked in alloc_map, which > chunk_map_stats() iterates based on the actual bitmap contents. >