Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp2271306pxm; Fri, 4 Mar 2022 12:48:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJz0vSuSBIXxT0S35WL/orVBNZoSoTj4/pt46vJbwh8NlQnwdfaZHbU6Is50knnuyh6JJ4Nj X-Received: by 2002:a05:6a00:a8b:b0:4cd:6030:4df3 with SMTP id b11-20020a056a000a8b00b004cd60304df3mr533833pfl.40.1646426901098; Fri, 04 Mar 2022 12:48:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646426901; cv=none; d=google.com; s=arc-20160816; b=HcKDHgsYhcTN5TndjsTFSZIzg29b0TmlaT05+BSkKlvinU7ySQ2Esmr0Xhblczfzox Hq57JJmMHpGDazo1q8FiE4PikoI+WYD/Y6tcgX4jb6qNlAqcpMaoNOv7gz9MDgkBY3ce STL1SBuqTDQULqOp7/MqFIIMOP/PNM5P25V3vjxMvZAGyexi6nbPicTp9AmTHIs4gDUw r66IiWbuz4JLGB+3euyeW7YWXwcMmx/Y1arNo497xPFJIvhFO1g3ahJvtlefgMOO6MjO Y4HM0FdxLRKzvZiBUKXM68fHCWo1d46gsvRnmkBzLMdFstBBkflXADWr9ZxrMiGTIUNP GidA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature:dkim-signature; bh=tt49sqFxCrGkXmPXZ9oMGkRFOU2rUQvc4pyyYVC4QWg=; b=nqPFubtzt/Z5pxjdSkCcGE77AHd2ac7eLXwEKLcMY1IT0D9z+TlNROdR8yv6LD7t6b qiIYOZqRPEex67tiviustd6OXgDquIGbDiNk83RjrmwxzkYGgfiuB5QHMz8aQzXVIIKf kbTZwIIr8UMsSLbghQduLOoJCtvCFYG/hJBjbJE2T/Kg21ugrmG4b+fAUJNTx/AAcmVY PCEbttlFJiFNJ2xJNyw/YV9y1nYx/ROkkWa2P6BdG5U2w9MFDA0N3KgDVtCbdz8L5YzF yMjGuws1T0Jf/e94xnjmTHFZfHJJqvbu3V17q1sYBXWDWCnicV4eo+ZpAx2OoreMUw8c Baeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=ibPrHdVX; dkim=neutral (no key) header.i=@suse.cz; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id d2-20020a636802000000b00374662120b1si5286609pgc.212.2022.03.04.12.48.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Mar 2022 12:48:21 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=ibPrHdVX; dkim=neutral (no key) header.i=@suse.cz; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D37CB2FA6A0; Fri, 4 Mar 2022 11:49:20 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231251AbiCDTCL (ORCPT + 99 others); Fri, 4 Mar 2022 14:02:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231213AbiCDTCK (ORCPT ); Fri, 4 Mar 2022 14:02:10 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 909A31DBA9B for ; Fri, 4 Mar 2022 11:01:22 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 192081F38A; Fri, 4 Mar 2022 19:01:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1646420481; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tt49sqFxCrGkXmPXZ9oMGkRFOU2rUQvc4pyyYVC4QWg=; b=ibPrHdVXCSddMXrXx3xD0EDxeu/v84LxFsPVauKbTbydXSnx9NsRzaKbNtsYKsAL1beGT3 yvPHPfB3dNtBsaTtP1RysudxCHU5g8GjoHVEt3oPVOlTW+CJ+Cotz67ci0mRAOKfeVGNKU /GqB1a/q2vc2Q/9RsTTd/aOhRSoi4Cg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1646420481; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tt49sqFxCrGkXmPXZ9oMGkRFOU2rUQvc4pyyYVC4QWg=; b=b1OsdYPquuc85r7XwG2CZX73jPE8d1SoPTyy3GDepmZbv6Gj07uzhpx9W3Wj5rzZCiYgYt ZOPhsSfXfJxyf5Bg== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id E6B8E13B5B; Fri, 4 Mar 2022 19:01:20 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id WXWpNwBiImLkDgAAMHmgww (envelope-from ); Fri, 04 Mar 2022 19:01:20 +0000 Message-ID: <6763e97b-88bf-59b0-c80e-26c3846531fc@suse.cz> Date: Fri, 4 Mar 2022 20:01:20 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.6.1 Subject: Re: [PATCH v2 5/5] mm/slub: refactor deactivate_slab() Content-Language: en-US To: Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org Cc: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Marco Elver , Matthew WilCox , Roman Gushchin , linux-kernel@vger.kernel.org References: <20220304063427.372145-1-42.hyeyoo@gmail.com> <20220304063427.372145-6-42.hyeyoo@gmail.com> From: Vlastimil Babka In-Reply-To: <20220304063427.372145-6-42.hyeyoo@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,RDNS_NONE,SPF_HELO_NONE, T_SCC_BODY_TEXT_LINE autolearn=no 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: X-Mailing-List: linux-kernel@vger.kernel.org On 3/4/22 07:34, Hyeonggon Yoo wrote: > Simplify deactivate_slab() by unlocking n->list_lock and retrying > cmpxchg_double() when cmpxchg_double() fails, and perform > add_{partial,full} only when it succeed. > > Releasing and taking n->list_lock again here is not harmful as SLUB > avoids deactivating slabs as much as possible. > > [ vbabka@suse.cz: perform add_{partial,full} when cmpxchg_double() > succeed. ] > > Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> Looks good, just noticed a tiny issue. > --- > mm/slub.c | 81 ++++++++++++++++++++++--------------------------------- > 1 file changed, 32 insertions(+), 49 deletions(-) > > diff --git a/mm/slub.c b/mm/slub.c > index f9ae983a3dc6..c1a693ec5874 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -2344,8 +2344,8 @@ static void deactivate_slab(struct kmem_cache *s, struct slab *slab, > { > enum slab_modes { M_NONE, M_PARTIAL, M_FULL, M_FREE }; > struct kmem_cache_node *n = get_node(s, slab_nid(slab)); > - int lock = 0, free_delta = 0; > - enum slab_modes l = M_NONE, m = M_NONE; > + int free_delta = 0; > + enum slab_modes mode = M_NONE; > void *nextfree, *freelist_iter, *freelist_tail; > int tail = DEACTIVATE_TO_HEAD; > unsigned long flags = 0; > @@ -2387,14 +2387,10 @@ static void deactivate_slab(struct kmem_cache *s, struct slab *slab, > * Ensure that the slab is unfrozen while the list presence > * reflects the actual number of objects during unfreeze. > * > - * We setup the list membership and then perform a cmpxchg > - * with the count. If there is a mismatch then the slab > - * is not unfrozen but the slab is on the wrong list. > - * > - * Then we restart the process which may have to remove > - * the slab from the list that we just put it on again > - * because the number of objects in the slab may have > - * changed. > + * We first perform cmpxchg holding lock and insert to list > + * when it succeed. If there is mismatch then slub is not > + * unfrozen and number of objects in the slab may have changed. > + * Then release lock and retry cmpxchg again. > */ > redo: > > @@ -2414,57 +2410,44 @@ static void deactivate_slab(struct kmem_cache *s, struct slab *slab, > new.frozen = 0; > > if (!new.inuse && n->nr_partial >= s->min_partial) > - m = M_FREE; > + mode = M_FREE; > else if (new.freelist) { > - m = M_PARTIAL; > - if (!lock) { > - lock = 1; > - /* > - * Taking the spinlock removes the possibility that > - * acquire_slab() will see a slab that is frozen > - */ > - spin_lock_irqsave(&n->list_lock, flags); > - } > - } else { > - m = M_FULL; > - if (kmem_cache_debug_flags(s, SLAB_STORE_USER) && !lock) { This used to set m = M_FULL; always. > - lock = 1; > - /* > - * This also ensures that the scanning of full > - * slabs from diagnostic functions will not see > - * any frozen slabs. > - */ > - spin_lock_irqsave(&n->list_lock, flags); > - } > + mode = M_PARTIAL; > + /* > + * Taking the spinlock removes the possibility that > + * acquire_slab() will see a slab that is frozen > + */ > + spin_lock_irqsave(&n->list_lock, flags); > + } else if (kmem_cache_debug_flags(s, SLAB_STORE_USER)) { > + mode = M_FULL; Now you only set it for SLAB_STORE_USER caches. > + /* > + * This also ensures that the scanning of full > + * slabs from diagnostic functions will not see > + * any frozen slabs. > + */ > + spin_lock_irqsave(&n->list_lock, flags); > } > > - if (l != m) { > - if (l == M_PARTIAL) > - remove_partial(n, slab); > - else if (l == M_FULL) > - remove_full(s, n, slab); > - > - if (m == M_PARTIAL) > - add_partial(n, slab, tail); > - else if (m == M_FULL) > - add_full(s, n, slab); > - } > > - l = m; > if (!cmpxchg_double_slab(s, slab, > old.freelist, old.counters, > new.freelist, new.counters, > - "unfreezing slab")) > + "unfreezing slab")) { > + if (mode == M_PARTIAL || mode == M_FULL) > + spin_unlock_irqrestore(&n->list_lock, flags); > goto redo; > + } > > - if (lock) > - spin_unlock_irqrestore(&n->list_lock, flags); > > - if (m == M_PARTIAL) > + if (mode == M_PARTIAL) { > + add_partial(n, slab, tail); > + spin_unlock_irqrestore(&n->list_lock, flags); > stat(s, tail); > - else if (m == M_FULL) > + } else if (mode == M_FULL) { > + add_full(s, n, slab); > + spin_unlock_irqrestore(&n->list_lock, flags); > stat(s, DEACTIVATE_FULL); As a result, full slabs without SLAB_STORE_USER will not count DEACTIVATE_FULL anymore. I guess the easiest way to solve it is to e.g. add a M_FULL_NOLIST mode that only does the DEACTIVATE_FULL counting. > - else if (m == M_FREE) { > + } else if (mode == M_FREE) { > stat(s, DEACTIVATE_EMPTY); > discard_slab(s, slab); > stat(s, FREE_SLAB);