Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp2115664rdb; Sun, 3 Dec 2023 03:20:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IGZpdVeWTt+roDcr8c4lRDVhGBC4y+w6M/iRTmw6+gHuYIbg8cTVogLSWiSOyEOa1JkEAln X-Received: by 2002:a05:6871:588:b0:1fa:fa54:4d0a with SMTP id u8-20020a056871058800b001fafa544d0amr3528889oan.19.1701602409695; Sun, 03 Dec 2023 03:20:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701602409; cv=none; d=google.com; s=arc-20160816; b=igWy7Zo8fwehxC8B9VVpV70UdvsTeVaigy/zkaB6eAV3rrq0nRbVLtNoPrtniBMTtu Kj0h6lO2Jq409c4OjM9wT3KnU7+ocSqCP2edkmdA5MQ1tQM3FM1O4yHb1ysSquFX32H5 evq9NbXZKF6ZtqsqWN5IPWaUBNAX78pHNd7uYViSMI1HjPYR2wJacc9KlSor1PhH2gmN k6fLzpeeoHwAOLQYCphw9Yvh8w61PhzD5qh3Pl4KagJv0rs6jqbirK1mLafaIZ/RDEDG DtKt1Q3YkamEUKjvmojiOdWyZIrcisAcsu8koOLMefH55Bwk6lSQ7x+JPBY4CxWwB0d5 xIOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=Tw+/5qq37rQrr1uVu+tKsvD2lTjhdzLYaYrSz1LEfBY=; fh=b1R8eG2ceG5b6rU+z1hHdVbOtL4eS6GhviijkQNwgfg=; b=XvQ3uKSgk6ixhwXANUuZs3RakpRCXvZ+5RnRMADVFnK3PxSErU4mDlCm7seD/jvyLB VhhOAH1dns9SkBFrzu4DxFV4NrbjJWVZ0mR72FSzZ2XRdZ4BnoJ/UbdIWF6DLUtxTQBS dyCoiGZif84dJSOn1VZduAh3bx3Yh/t+2TgbReluWy3gUoSi2oYWGBBh2Ka9AAKWAWag U2O0lEQmaStd52XcGrIwmsfGp/KPJtKJk5sfih/H3bOAIik382iGmR8tk2kL79K/FUiZ yZqNBY/Xsd4fXTmaSI0+fkHw7yVXJcbT7qNPI+RMV2DkcUCfS/rg596QZhxtF+yF3iLL JCDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=EqRTHxh1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id x21-20020a63f715000000b005b92b8e70f1si6054365pgh.27.2023.12.03.03.20.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Dec 2023 03:20:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=EqRTHxh1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id DBEB58026E04; Sun, 3 Dec 2023 03:20:06 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233374AbjLCLTq (ORCPT + 99 others); Sun, 3 Dec 2023 06:19:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229995AbjLCLTo (ORCPT ); Sun, 3 Dec 2023 06:19:44 -0500 Received: from mail-qk1-x733.google.com (mail-qk1-x733.google.com [IPv6:2607:f8b0:4864:20::733]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6CEE10E for ; Sun, 3 Dec 2023 03:19:50 -0800 (PST) Received: by mail-qk1-x733.google.com with SMTP id af79cd13be357-77dd54d9da2so250490285a.0 for ; Sun, 03 Dec 2023 03:19:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701602390; x=1702207190; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Tw+/5qq37rQrr1uVu+tKsvD2lTjhdzLYaYrSz1LEfBY=; b=EqRTHxh1MumMP/SI4s2sxupUZTYM8WoE96qVu7sSyfllV5rjV775fdW5B8/fP4YNSR GWBtaVmLcdK0gB962cgsTFkZHmCF2spp2NGhB5iWZILShnwJIed4SqlddH8cDR/ovgP7 khbhtRtigi7hJfpcdm3LoetcLhusFqos4Nzspa9ZP5l7/SsWPfiCYY3PypC0z3z6P4Lo UfpxAlAC3kXzm40eHiygoY1HSxq6kFYkJsmrLUAy4n1DUZt6vOUg5DZ/PSFbvhHRU5Ry z2ARZ73vFclNFlZieo4LjvwvDNRnc/9WfK17HJIXNKKrWYvb6Tz1uiGAQSM38ImWm9Dz 3oIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701602390; x=1702207190; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Tw+/5qq37rQrr1uVu+tKsvD2lTjhdzLYaYrSz1LEfBY=; b=o8sSqH6VrCXr9n64MSmkP7SRuix/7UgyeHgmTRnwWHXMLpn1mYMkXdVXmWmHRRaVpg NNTb3tN32WrjZWD+ugcA0r4Vc3uP7Q40q8rqgLwdsCKSBHheRAdRCm/dYBnq6plPZTsy 1fyJOtNOLUkBh01EpxJMzLFSw4bYbxtV1VYylurZjcK7WvoJd1FtH2AYRolcTkDLbXXT hxvrGxPoQaUJPOxNmIkarFn31y8DN3NQMm0a2pd/CNWUJ66YN8LFp9UMgqxuPqh5/OKr XC4QQHCXgLfak8NKRU3oVbWztgHBqhnesUx3hoCtexgsWhWA2uEnLl8dHMmh1NrloXlp MfNQ== X-Gm-Message-State: AOJu0YwlChktSYDATTTNmyLZ1pIAnoNR6mEV6dzzLxr0zZxVVe+PiXFc wj9ngKJ3js+fB8Hamf2+F5nN0gpeAIJxkW87PsM= X-Received: by 2002:a05:620a:179e:b0:77e:fba3:81bd with SMTP id ay30-20020a05620a179e00b0077efba381bdmr3122145qkb.83.1701602389881; Sun, 03 Dec 2023 03:19:49 -0800 (PST) MIME-Version: 1.0 References: <20231102032330.1036151-1-chengming.zhou@linux.dev> <20231102032330.1036151-8-chengming.zhou@linux.dev> In-Reply-To: From: Hyeonggon Yoo <42.hyeyoo@gmail.com> Date: Sun, 3 Dec 2023 20:19:38 +0900 Message-ID: Subject: Re: [PATCH v5 7/9] slub: Optimize deactivate_slab() To: Chengming Zhou Cc: vbabka@suse.cz, cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, akpm@linux-foundation.org, roman.gushchin@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Chengming Zhou Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=0.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,HK_RANDOM_FROM,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,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 pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Sun, 03 Dec 2023 03:20:07 -0800 (PST) On Sun, Dec 3, 2023 at 7:26=E2=80=AFPM Chengming Zhou wrote: > > On 2023/12/3 17:23, Hyeonggon Yoo wrote: > > On Thu, Nov 2, 2023 at 12:25=E2=80=AFPM wrot= e: > >> > >> From: Chengming Zhou > >> > >> Since the introduce of unfrozen slabs on cpu partial list, we don't > >> need to synchronize the slab frozen state under the node list_lock. > >> > >> The caller of deactivate_slab() and the caller of __slab_free() won't > >> manipulate the slab list concurrently. > >> > >> So we can get node list_lock in the last stage if we really need to > >> manipulate the slab list in this path. > >> > >> Signed-off-by: Chengming Zhou > >> Reviewed-by: Vlastimil Babka > >> Tested-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> > >> --- > >> mm/slub.c | 79 ++++++++++++++++++------------------------------------= - > >> 1 file changed, 26 insertions(+), 53 deletions(-) > >> > >> diff --git a/mm/slub.c b/mm/slub.c > >> index bcb5b2c4e213..d137468fe4b9 100644 > >> --- a/mm/slub.c > >> +++ b/mm/slub.c > >> @@ -2468,10 +2468,8 @@ static void init_kmem_cache_cpus(struct kmem_ca= che *s) > >> static void deactivate_slab(struct kmem_cache *s, struct slab *slab, > >> void *freelist) > >> { > >> - enum slab_modes { M_NONE, M_PARTIAL, M_FREE, M_FULL_NOLIST }; > >> struct kmem_cache_node *n =3D get_node(s, slab_nid(slab)); > >> int free_delta =3D 0; > >> - enum slab_modes mode =3D M_NONE; > >> void *nextfree, *freelist_iter, *freelist_tail; > >> int tail =3D DEACTIVATE_TO_HEAD; > >> unsigned long flags =3D 0; > >> @@ -2509,65 +2507,40 @@ static void deactivate_slab(struct kmem_cache = *s, struct slab *slab, > >> /* > >> * Stage two: Unfreeze the slab while splicing the per-cpu > >> * freelist to the head of slab's freelist. > >> - * > >> - * Ensure that the slab is unfrozen while the list presence > >> - * reflects the actual number of objects during unfreeze. > >> - * > >> - * We first perform cmpxchg holding lock and insert to list > >> - * when it succeed. If there is mismatch then the slab is not > >> - * unfrozen and number of objects in the slab may have changed= . > >> - * Then release lock and retry cmpxchg again. > >> */ > >> -redo: > >> - > >> - old.freelist =3D READ_ONCE(slab->freelist); > >> - old.counters =3D READ_ONCE(slab->counters); > >> - VM_BUG_ON(!old.frozen); > >> - > >> - /* Determine target state of the slab */ > >> - new.counters =3D old.counters; > >> - if (freelist_tail) { > >> - new.inuse -=3D free_delta; > >> - set_freepointer(s, freelist_tail, old.freelist); > >> - new.freelist =3D freelist; > >> - } else > >> - new.freelist =3D old.freelist; > >> - > >> - new.frozen =3D 0; > >> + do { > >> + old.freelist =3D READ_ONCE(slab->freelist); > >> + old.counters =3D READ_ONCE(slab->counters); > >> + VM_BUG_ON(!old.frozen); > >> + > >> + /* Determine target state of the slab */ > >> + new.counters =3D old.counters; > >> + new.frozen =3D 0; > >> + if (freelist_tail) { > >> + new.inuse -=3D free_delta; > >> + set_freepointer(s, freelist_tail, old.freelist= ); > >> + new.freelist =3D freelist; > >> + } else { > >> + new.freelist =3D old.freelist; > >> + } > >> + } while (!slab_update_freelist(s, slab, > >> + old.freelist, old.counters, > >> + new.freelist, new.counters, > >> + "unfreezing slab")); > >> > >> + /* > >> + * Stage three: Manipulate the slab list based on the updated = state. > >> + */ > > > > deactivate_slab() might unconsciously put empty slabs into partial list= , like: > > > > deactivate_slab() __slab_free() > > cmpxchg(), slab's not empty > > cmpxchg(), slab's empty > > and unfrozen > > Hi, > > Sorry, but I don't get it here how __slab_free() can see the slab empty, > since the slab is not empty from deactivate_slab() path, and it can't be > used by any CPU at that time? The scenario is CPU B previously allocated an object from slab X, but put it into node partial list and then CPU A have taken slab X into cpu sla= b. While slab X is CPU A's cpu slab, when CPU B frees an object from slab X, it puts the object into slab X's freelist using cmpxchg. Let's say in CPU A the deactivation path performs cmpxchg and X.inuse was 1= , and then CPU B frees (__slab_free()) to slab X's freelist using cmpxchg, _before_ slab X's put into partial list by CPU A. Then CPU A thinks it's not empty so put it into partial list, but by CPU B the slab has become empty. Maybe I am confused, in that case please tell me I'm wrong :) Thanks! -- Hyeonggon