From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj2-f1.google.com (mail-pj2-f1.google.com [74.125.227.129]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9F91B1A6809 for ; Mon, 29 Jun 2026 01:54:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.227.129 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782698057; cv=none; b=LLnm4zjQd/U3cR4D4BfkGld4V/PQZC7rNJ5zZ+Fwn17dRzvbKqAJwqsLzOlFyIfbzjOe+yQ3eDNHZREl7b98T922PPfQJRlx98XAr9JOeUKq4Oo5KRBWAHdWfp+hk1Etm4ev0cLyroNs7kwLciQllJ9TFDEzty6ROju8a2PDiKo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782698057; c=relaxed/simple; bh=HSeHyu7n4wGWlWse2EQKzTJhnB1BKzvXNRtXslnfDAM=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=lItYJecQz/blXi+FIEPJGyE1Wkqe+lm4GmEwKrbZ6+3N6YHUSMdDXoctJvMsLouZ4+kGjyrEt/2HfjP1sAXAnyjFrfGrbQHmcaVnhfo/xNyRqjzx8Zcdl4Q1RXnGL3ERL1NuS0G/sBRL7ZGCy8pwGvwXAdkMMuqx0zTSXO4qE4w= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=UNYqLfgW; arc=none smtp.client-ip=74.125.227.129 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UNYqLfgW" Received: by mail-pj2-f1.google.com with SMTP id 98e67ed59e1d1-37fc08a224cso74913a91.0 for ; Sun, 28 Jun 2026 18:54:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782698056; x=1783302856; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=lRdWVg7KJgFb12WQoGhPg3Je4rPMNfAc0SvIof4ZQ70=; b=UNYqLfgWqoWeEvKJv52iiZU3wK3zEbcQ2SStonJ3LUd77ZELePWdFLxaBHyclfG0R8 pd2igBQDg5M3yasEB5xC0gDQMIY76JqTMvSmw2jKDu5PUqhGIJeaA7poEyLdBp5DXNOy w4XzgOXkM8AndRu/iCpnwn1etGs56XHvrjJNv32pWSTlprn/89/rZU9SH5qaDT3K+mz7 qesSohQQkFSzIHDRbD370Cl3CJxUep21dW4yPz8c6i2jHgOn8ooNEcclURIPulydNHpI F+MeDW79yTzFZBrAhhCa2Rd3rEOaaM+mAco/hgU9s6NCPtzYfCV1/yxgL8+mv+bWue3S kIIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782698056; x=1783302856; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=lRdWVg7KJgFb12WQoGhPg3Je4rPMNfAc0SvIof4ZQ70=; b=i+fBiiGBHcxTGVHClOg7+L3BYoNvGVgEdmQLnKNWk7ekHyXhADYC2eYwkiIWTZ/n9W YayFC96xxzQaJic+xr2cD8s5a+W83iUOWv+QnFdO5kGUP37bFPwB5dISOyqN+T/cTS1n 26RcEnS2qw1upTDJ8ZyuzKlucI5aCAZoVY9tRHKIgHKPydKJgTYPjpMwHSx2jk7Biq6Y de+oBxuan7v8efQZhEnBTAnAGX3PH6h3gODe4aB8Vptpo9FWcV6zXVUr3rL0HnaZfieI s/PtJf3ZtcbrWQbaygv0/HQGFjQtIezeeslkXofT/g8FOMjuD6yFq8tdj3YxR4uFJdqQ pztg== X-Forwarded-Encrypted: i=1; AHgh+RoGjPZvCWK6TN+XnGjElAM8YP+NTfcD23LEaKPwLH6G+gRa3TyvWVbub5GrS/9waBy9Ajgzsis9iuxsdCA=@vger.kernel.org X-Gm-Message-State: AOJu0YyPxW7Gaba9vNfZITUKeRR9RPWqqEvyDQ/Nr5fdqGB24x+w78tg xFqLc9OFCc+TYclkL/U4gLY+gH4as90+R1z34B1EIO4Pnoql2Ov/Fg2J X-Gm-Gg: AfdE7ckGOQJuBd6wDuMAEU6OzX3YEh4H8iycRu8GwYotP9pIE3EH6HqwESLBQnvs8bf wSgj6MUkldf3yeuj4szfdROc5OdeWMo/qfOZzVp/xK2s03krmTpFeRw6VskVq8nsatdes8mqjkg eTnE9iJvC7irG+/gHdlX+3FhN2pqugGHUbyP3Llpra79hAUXCqvUw3LfHdktpUCNSf0h1lDVxZi 3K2yg41MZa3jKT7Q+o5VDdX22SrjR5W+ha8FY+XfaoO7SE2bURV7mtWjZOBqCbbqEdK51UuCog6 6VAxudCl4fm0+AvexO4OinGKf8u+W94trgKqCyrQUvYsxG+LjTS52lwPSWNAH3bV7lVdACd2pVZ LIeUXT+7s9rCQ5oOBQgZaVv67t2bD8pmAq6Hf2JKXDf9Qu21FBDdzqBC8qyM40Ny7Q7aqo54m6b KHvhwOOeuBu/x+J0Z3TV828hRNuU3vTrBHDn27Qm3QXJQJhAI= X-Received: by 2002:a17:90b:2886:b0:380:925:aa38 with SMTP id 98e67ed59e1d1-3800925bb16mr964016a91.1.1782698055967; Sun, 28 Jun 2026 18:54:15 -0700 (PDT) Received: from [192.168.10.237] ([154.83.91.239]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c9ae00b6545sm107557a12.12.2026.06.28.18.54.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 28 Jun 2026 18:54:15 -0700 (PDT) Message-ID: Date: Mon, 29 Jun 2026 09:54:11 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] btrfs: compression: allocate buckets with workspace To: Rosen Penev , linux-btrfs@vger.kernel.org Cc: Chris Mason , David Sterba , open list References: <20260629002505.1552238-1-rosenp@gmail.com> Content-Language: en-US From: Sun YangKai In-Reply-To: <20260629002505.1552238-1-rosenp@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2026/6/29 08:25, Rosen Penev wrote: > Convert 3 allocations into one. Simplifies code slightly. > > Signed-off-by: Rosen Penev > --- > fs/btrfs/compression.c | 18 +++++------------- > 1 file changed, 5 insertions(+), 13 deletions(-) > > diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c > index ffb6b52863a7..da6749ff5924 100644 > --- a/fs/btrfs/compression.c > +++ b/fs/btrfs/compression.c > @@ -650,11 +650,11 @@ struct heuristic_ws { > /* Partial copy of input data */ > u8 *sample; > u32 sample_size; > - /* Buckets store counters for each byte value */ > - struct bucket_item *bucket; > /* Sorting buffer */ > struct bucket_item *bucket_b; > struct list_head list; > + /* Buckets store counters for each byte value */ > + struct bucket_item bucket[]; > }; > > static void free_heuristic_ws(struct list_head *ws) > @@ -664,8 +664,6 @@ static void free_heuristic_ws(struct list_head *ws) > workspace = list_entry(ws, struct heuristic_ws, list); > > kvfree(workspace->sample); > - kfree(workspace->bucket); > - kfree(workspace->bucket_b); > kfree(workspace); > } > > @@ -673,22 +671,16 @@ static struct list_head *alloc_heuristic_ws(struct btrfs_fs_info *fs_info) > { > struct heuristic_ws *ws; > > - ws = kzalloc_obj(*ws); > + ws = kzalloc(struct_size(ws, bucket, BUCKET_SIZE * 2), GFP_KERNEL); It seems that size is fixed and known at compile time, and since we want to inline the buckets in the struct, I think we can have something like this to save one pointer: struct heuristic_ws { ... struct bucket_item bucket[BUCKET_SIZE]; struct bucket_item bucket_b[BUCKET_SIZE]; } However, each bucket array takes exactly 1024B memory. Currently we have a 48B allocation for the struct it self, 2 * 1024B allocation for bucket array, and 8192B allocation for sample. With these 2 arrays inlined, the struct will need 32 + 2048B allocation if my calculation is correct, which will goes into kmalloc-4k. Seems not good. Thanks, Sun YangKai > if (!ws) > return ERR_PTR(-ENOMEM); > > + ws->bucket_b = ws->bucket + BUCKET_SIZE; > + > ws->sample = kvmalloc(MAX_SAMPLE_SIZE, GFP_KERNEL); > if (!ws->sample) > goto fail; > > - ws->bucket = kzalloc_objs(*ws->bucket, BUCKET_SIZE); > - if (!ws->bucket) > - goto fail; > - > - ws->bucket_b = kzalloc_objs(*ws->bucket_b, BUCKET_SIZE); > - if (!ws->bucket_b) > - goto fail; > - > INIT_LIST_HEAD(&ws->list); > return &ws->list; > fail: