Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp8549344rwi; Tue, 25 Oct 2022 08:02:27 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6ajZKvP4EtmTJBy9HKBjjRRVfJzt4PLeJyD8wDp0JyP7m81orHeRH1mJag2eyM8j/lZLVD X-Received: by 2002:a17:906:8a6f:b0:780:96b4:d19e with SMTP id hy15-20020a1709068a6f00b0078096b4d19emr32554773ejc.624.1666710146735; Tue, 25 Oct 2022 08:02:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666710146; cv=none; d=google.com; s=arc-20160816; b=BnUgG1gKJlZDGLdVJJSVONCLZ5j0IO1hmDxWZrSOvEjMVDm57Sa72wOmAZUx1Vi7TR I0Wgjb/Rl3tqmfvT2VksiWOW3Wwp+xreR3R/1OvckOUrItFaIqCllaLpiH6HRpl2vsZh qTSdjc4wyVV6r6dUALxIq0TwPnsefYDuokRxuxH5qIxdSlB8HECaNty32Fg3MVE+426n zqFylnX/Qh9+9bGDg7kySGwPe37imep1SzFmJJQB/XkNNKWFIf4jWdUYzAfMV2hN3U9E FouCN/vJ+nF+wt7fnnAeYD+mCwH81vaQeXI1mwk7yT3E5ZUUB0HUhLZr1lC7JTO5BZPl aPVg== 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=RoEGXx+CsUxH6vRbiyjgkwQmdQ57d5+pOzoy1MCTSxE=; b=G0HdK5Y8SQlXcDweN5Q0cGrMJNp1mQseBKLUzmEF/bNpIX3uvRI5pJWb8nwfU6jMhw 6iRGUtJTF+/eT5z8tOTzd3zTlpYh8mJb7RzEomZxB04h5OhFkmW4QFXp43teH6hMd06D B78TCK1EdE0rLqz3so29ciYB10MD0nQanhhOpluWUkg16lPotd8XXg3VZ/zOx46okqRe w1G6tcDndvLNxlTIf4vxahBf1357d56dk2rq1Jxu5PenlE8um0xT2fLL1EZf3DKxk9Nm nKMS3p28XiX8q7blYQePTWgNaKkvRWGpzWzpBxETDBANnBt6qTnCX9sqVQiu9jCTH6/O sdVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=25cKaaFc; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=qPwtU3v9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ne7-20020a1709077b8700b007a4feae7adfsi2820984ejc.565.2022.10.25.08.01.59; Tue, 25 Oct 2022 08:02:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=25cKaaFc; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=qPwtU3v9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232941AbiJYOKk (ORCPT + 99 others); Tue, 25 Oct 2022 10:10:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231484AbiJYOKM (ORCPT ); Tue, 25 Oct 2022 10:10:12 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 21FB8101ED; Tue, 25 Oct 2022 07:08:35 -0700 (PDT) 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 BC2C81FA48; Tue, 25 Oct 2022 14:08:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1666706913; 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=RoEGXx+CsUxH6vRbiyjgkwQmdQ57d5+pOzoy1MCTSxE=; b=25cKaaFcs2vuVtqnTFrQ7vRegg0CsjbfHGVKF7yckheWqDXaeYjv2Es6UZYsrBPerpQiKD GUvSLeW8RsxX6/mPYdj0DLOuE8svtna781AgN4Mbqu74zh6FZasUbNSV8lXD5dxgm7MCQc uC+anx//u6JzOi8rWST4ek7JYRNe91g= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1666706913; 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=RoEGXx+CsUxH6vRbiyjgkwQmdQ57d5+pOzoy1MCTSxE=; b=qPwtU3v9Yms0R9q4E8cn/pdrbrRzqbn74UwUm4PMxRYs4uF/+pKqoFt1ley+ocXsYlFsD1 4xus56OnANhfxJCw== 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 94981134CA; Tue, 25 Oct 2022 14:08:33 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 9O2VI+HtV2PPUgAAMHmgww (envelope-from ); Tue, 25 Oct 2022 14:08:33 +0000 Message-ID: Date: Tue, 25 Oct 2022 16:08:33 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.3.3 Subject: Re: amusing SLUB compaction bug when CC_OPTIMIZE_FOR_SIZE Content-Language: en-US To: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: Matthew Wilcox , Hugh Dickins , David Laight , Joel Fernandes , Andrew Morton , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rcu@vger.kernel.org, "Paul E . McKenney" References: <35502bdd-1a78-dea1-6ac3-6ff1bcc073fa@suse.cz> <7dddca4c-bc36-2cf0-de1c-a770bef9e1b7@suse.cz> From: Vlastimil Babka In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_SOFTFAIL,URIBL_BLOCKED autolearn=ham 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 10/25/22 15:47, Hyeonggon Yoo wrote: > On Mon, Oct 24, 2022 at 04:35:04PM +0200, Vlastimil Babka wrote: > > [,,,] > >> diff --git a/mm/slab.c b/mm/slab.c >> index 59c8e28f7b6a..219beb48588e 100644 >> --- a/mm/slab.c >> +++ b/mm/slab.c >> @@ -1370,6 +1370,8 @@ static struct slab *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, >> >> account_slab(slab, cachep->gfporder, cachep, flags); >> __folio_set_slab(folio); >> + /* Make the flag visible before any changes to folio->mapping */ >> + smp_wmb(); >> /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */ >> if (sk_memalloc_socks() && page_is_pfmemalloc(folio_page(folio, 0))) >> slab_set_pfmemalloc(slab); >> @@ -1387,9 +1389,11 @@ static void kmem_freepages(struct kmem_cache *cachep, struct slab *slab) >> >> BUG_ON(!folio_test_slab(folio)); >> __slab_clear_pfmemalloc(slab); >> - __folio_clear_slab(folio); >> page_mapcount_reset(folio_page(folio, 0)); >> folio->mapping = NULL; >> + /* Make the mapping reset visible before clearing the flag */ >> + smp_wmb(); >> + __folio_clear_slab(folio); >> >> if (current->reclaim_state) >> current->reclaim_state->reclaimed_slab += 1 << order; >> diff --git a/mm/slub.c b/mm/slub.c >> index 157527d7101b..6dc17cb915c5 100644 >> --- a/mm/slub.c >> +++ b/mm/slub.c >> @@ -1800,6 +1800,8 @@ static inline struct slab *alloc_slab_page(gfp_t flags, int node, >> >> slab = folio_slab(folio); >> __folio_set_slab(folio); >> + /* Make the flag visible before any changes to folio->mapping */ >> + smp_wmb(); >> if (page_is_pfmemalloc(folio_page(folio, 0))) >> slab_set_pfmemalloc(slab); >> >> @@ -2008,8 +2010,10 @@ static void __free_slab(struct kmem_cache *s, struct slab *slab) >> } >> >> __slab_clear_pfmemalloc(slab); >> - __folio_clear_slab(folio); >> folio->mapping = NULL; >> + /* Make the mapping reset visible before clearing the flag */ >> + smp_wmb(); >> + __folio_clear_slab(folio); >> if (current->reclaim_state) >> current->reclaim_state->reclaimed_slab += pages; >> unaccount_slab(slab, order, s); >> -- >> 2.38.0 > > Do we need to try this with memory barriers before frozen refcount lands in? There was IIRC an unresolved issue with frozen refcount tripping the page isolation code so I didn't want to be depending on that. > It's quite complicated and IIUC there is a still theoretical race: > > At isolate_movable_page: At slab alloc: At slab free: > folio = alloc_pages(flags, order) > > folio_try_get() > folio_test_slab() == false > __folio_set_slab(folio) > smp_wmb() > > call_rcu(&slab->rcu_head, rcu_free_slab); > > > smp_rmb() > __folio_test_movable() == true > > folio->mapping = NULL; > smp_wmb() > __folio_clear_slab(folio); > smp_rmb() > folio_test_slab() == false > > folio_trylock() There's also between above and below: if (!PageMovable(page) || PageIsolated(page)) goto out_no_isolated; mops = page_movable_ops(page); If we put another smp_rmb() before the PageMovable test, could that have helped? It would assure we observe the folio->mapping = NULL; from the "slab free" side? But yeah, it's getting ridiculous. Maybe there's a simpler way to check two bits in two different bytes atomically. Or maybe it's just an impossible task, I feel I just dunno computers at this point. > mops->isolate_page() (*crash*) > > > Please let me know if I'm missing something ;-) > Thanks! >