From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 5D00F41C0C9 for ; Mon, 2 Mar 2026 15:53:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772466814; cv=none; b=eFDQWfXN0IjEdeGTXGJ3Iy7caDHJE7PrKb1331Vn/XEDw57Hk8utAgUvAmcYtXlrT9+fiy0eOZHOq/gFkbm7JozwpUGVKcZp0yFfH15/NClVtFUN2SbLIljIbV7DYnqrh7EjrfiEwII4l0dmGVgOvq+F156oFfoGw+I3m/MDx+Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772466814; c=relaxed/simple; bh=7Uie62BpTymcWUuvR63UQAUN0jqebG+DhKiQwh24Bg0=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=serg13126q2b//SfAXdcIA/rSiBbYXRzjbgMDgECzA+uMLPXBaRH3UcwofH208c+7ROn40p4Okp7x12kykB1ysi9+IE1aK0o2Hjmqhm1IucVsky3IG4oAYxLlOyZcrNWawTG/XF70wFvW8cjCAxxLDSjc35Yt+RZ8P5dCh0e660= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=f0P3IaV/; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="f0P3IaV/" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772466808; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=3fFl+ZQpqgOCDxOl7PBCDqalPNAwSCe0FsyOaa3j76M=; b=f0P3IaV/VYukrHzGv1uUgwSBYw6DQ2uDXs9gJ7ewfnPaNEvqagIji7ZWXZVEv8juyFiP0X M5OrK+Ho2aUd5UkcO/QE0d3MUahfux17fM4Pd1uugXux24KheNRb8ULUelyJd2vIhYrdsW 6iPeJCljjtR3PcfiTAW28aZ6rBnpD0c= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-402-81pMIkUsN5O9DDZjxwXCsw-1; Mon, 02 Mar 2026 10:53:25 -0500 X-MC-Unique: 81pMIkUsN5O9DDZjxwXCsw-1 X-Mimecast-MFC-AGG-ID: 81pMIkUsN5O9DDZjxwXCsw_1772466802 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3FB5C18002DE; Mon, 2 Mar 2026 15:53:22 +0000 (UTC) Received: from tpad.localdomain (unknown [10.96.133.6]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 052691956053; Mon, 2 Mar 2026 15:53:20 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id 8E103401D9E1C; Mon, 2 Mar 2026 12:53:00 -0300 (-03) Message-ID: <20260302155105.184592916@redhat.com> User-Agent: quilt/0.69 Date: Mon, 02 Mar 2026 12:49:46 -0300 From: Marcelo Tosatti To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Vlastimil Babka , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Leonardo Bras , Thomas Gleixner , Waiman Long , Boqun Feun , Frederic Weisbecker Subject: [PATCH v2 1/5] slab: distinguish lock and trylock for sheaf_flush_main() References: <20260302154945.143996316@redhat.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=UTF-8 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 From: Vlastimil Babka sheaf_flush_main() can be called from __pcs_replace_full_main() where the trylock can in theory fail, and pcs_flush_all() where it's not expected to and it would be actually a problem if it failed and left the main sheaf not flushed. To make this explicit, split the function into sheaf_flush_main() (using local_lock()) and sheaf_try_flush_main() (using local_trylock()) where both call __sheaf_flush_main_batch() to flush a single batch of objects. This will allow lockdep to verify our assumptions. Signed-off-by: Vlastimil Babka --- mm/slub.c | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 18c30872d196..12912b29f5bb 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2844,19 +2844,19 @@ static void __kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p); * object pointers are moved to a on-stack array under the lock. To bound the * stack usage, limit each batch to PCS_BATCH_MAX. * - * returns true if at least partially flushed + * Must be called with s->cpu_sheaves->lock locked, returns with the lock + * unlocked. + * + * Returns how many objects are remaining to be flushed */ -static bool sheaf_flush_main(struct kmem_cache *s) +static unsigned int __sheaf_flush_main_batch(struct kmem_cache *s) { struct slub_percpu_sheaves *pcs; unsigned int batch, remaining; void *objects[PCS_BATCH_MAX]; struct slab_sheaf *sheaf; - bool ret = false; -next_batch: - if (!local_trylock(&s->cpu_sheaves->lock)) - return ret; + lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock)); pcs = this_cpu_ptr(s->cpu_sheaves); sheaf = pcs->main; @@ -2874,10 +2874,37 @@ static bool sheaf_flush_main(struct kmem_cache *s) stat_add(s, SHEAF_FLUSH, batch); - ret = true; + return remaining; +} - if (remaining) - goto next_batch; +static void sheaf_flush_main(struct kmem_cache *s) +{ + unsigned int remaining; + + do { + local_lock(&s->cpu_sheaves->lock); + + remaining = __sheaf_flush_main_batch(s); + + } while (remaining); +} + +/* + * Returns true if the main sheaf was at least partially flushed. + */ +static bool sheaf_try_flush_main(struct kmem_cache *s) +{ + unsigned int remaining; + bool ret = false; + + do { + if (!local_trylock(&s->cpu_sheaves->lock)) + return ret; + + ret = true; + remaining = __sheaf_flush_main_batch(s); + + } while (remaining); return ret; } @@ -5685,7 +5712,7 @@ __pcs_replace_full_main(struct kmem_cache *s, struct slub_percpu_sheaves *pcs, if (put_fail) stat(s, BARN_PUT_FAIL); - if (!sheaf_flush_main(s)) + if (!sheaf_try_flush_main(s)) return NULL; if (!local_trylock(&s->cpu_sheaves->lock)) --- base-commit: 27125df9a5d3b4cfd03bce3a8ec405a368cc9aae change-id: 20260211-b4-sheaf-flush-2eb99a9c8bfb Best regards, -- Vlastimil Babka