Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp5519657ioo; Wed, 1 Jun 2022 07:13:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwWRmRXKSUIto/tm8L1c4T+3lth8jrjpaYiV/7wH9kCUsWhtnQgDRHbOmuOjAhyZS7M1mx4 X-Received: by 2002:a05:6a00:1d8e:b0:518:87e7:db00 with SMTP id z14-20020a056a001d8e00b0051887e7db00mr54212496pfw.84.1654092812135; Wed, 01 Jun 2022 07:13:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654092812; cv=none; d=google.com; s=arc-20160816; b=hH34U3oquvLNRoDtl+5QED8eZsv5/+rOGfJlgVftoWz8Q8SlCiyeYKAeHuV0VY7cZf TntQbraoVe3W75OKYPNX2ncvQwz+s5gZr9Bb1wqPZV+jIywSzSp5RKKITpLNYhvWPMQm aizup/A5roHTCn2oBp2VUeFxTlH8bOQ4jP6/zbQcFoufpoV70LfJFWHZzf2dgu0sDjbO fZaFXKbumm9cM6Rw7MwLkU3t8jNkxQvZ8HP2FhByR5buzuaf/3rg8p2ppjCVJccM15T0 vHrRIUxZ+jQvsItG0cPG4YYPYD53vlSHsGU0NLBGouMQESQ5KoA6QLcd+tW3s301aWHJ fGmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=/9a2BkRL4ZAjKXfqbP9m7K45lhUKTSecyMZfjSTX5wQ=; b=v8xtZV8tlKwWwiT4Vx4pKoPz4PTMuK3GrINxtF9JT5ElRQNhotU/hEVP1JBYLBBVgI qhI9Mz8AIGYhhURdwsrm5Mi6dNFBYqwB33GGE+mNaaTiM0obimmArklwK/3nVUZXVlqd eAYiM3wqHqJlvJrJXPAU2AW3cGmlvk+9IxjyQgrbqWuV6IqqBR8AExsOfMsrtyOiZnDC 64AdmLyj1GfjsT2PfAee9Fa2xeGgdA+kaOFmm/KlSvd1v2mys++Do8fxe/Gjh1MxvOZi WtHTMKttPfyK7PE4zV6pD8w4JYWxnlvEEOJlXICY1G3eM7DQO8xYXvm/IjvoOx6yUol2 PYmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=n8RzKOwP; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m18-20020a170902db1200b00165638d6e2esi2005641plx.445.2022.06.01.07.13.16; Wed, 01 Jun 2022 07:13:32 -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=@kernel.org header.s=k20201202 header.b=n8RzKOwP; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345452AbiEaPDQ (ORCPT + 99 others); Tue, 31 May 2022 11:03:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245030AbiEaPDO (ORCPT ); Tue, 31 May 2022 11:03:14 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD615986CE; Tue, 31 May 2022 08:03:13 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5D30A6023F; Tue, 31 May 2022 15:03:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B2E20C385A9; Tue, 31 May 2022 15:03:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1654009392; bh=m0jg7/GG/1GqctpKJeTLM1iJ6iRQvTeTBHHJu4GUHlQ=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=n8RzKOwPBEZvK6mSHWVlzIZlgvI6medx09sZwG9C1J0Dd9D0cwCgGYslMXDwq69Fq W82WU5oWShwWTPYwV75gwihPKeZy2GM9nTYYtpird0HFGJP43fcrmm2jlOzpI5NLme oaO8hbhD4Piyym0t0lGmmZnibACUaLtS80x1zt3Ji6PWjHmgBpdjSR9flsTRL/F4Uy sSHnDMDwPDVDabzPC3v7agPny65kOXEVR6EYe955cUTmZUtD52DwG95ZQ8MpGf6EE3 XMvTAW9AcBW//Nudwab9zLqKd7j3vkUz6nW/Fry7k6IrJ22bUuG2Ko0+6jkm2vHYlr GE+3c9oF9ba4A== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 461255C02A9; Tue, 31 May 2022 08:03:12 -0700 (PDT) Date: Tue, 31 May 2022 08:03:12 -0700 From: "Paul E. McKenney" To: Paul =?iso-8859-1?Q?Heidekr=FCger?= Cc: Alan Stern , Andrea Parri , Will Deacon , Peter Zijlstra , Boqun Feng , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , Akira Yokosawa , Daniel Lustig , Joel Fernandes , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, llvm@lists.linux.dev, Marco Elver , Charalampos Mainas , Pramod Bhatotia , Soham Shakraborty , Martin Fink Subject: Re: Broken Address Dependency in mm/ksm.c::cmp_and_merge_page() Message-ID: <20220531150312.GH1790663@paulmck-ThinkPad-P17-Gen-1> Reply-To: paulmck@kernel.org References: <20220426203254.GJ4285@paulmck-ThinkPad-P17-Gen-1> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 Tue, May 31, 2022 at 01:47:12PM +0200, Paul Heidekrüger wrote: > On Tue, Apr 26, 2022 at 01:32:54PM -0700, Paul E. McKenney wrote: > > On Fri, Apr 22, 2022 at 12:35:41PM +0200, Paul Heidekrüger wrote: > > > Hi all, > > > > > > My dependency checker is flagging yet another broken dependency. For > > > context, see [1]. > > > > > > Thankfully, it is fairly straight-forward to explain this time. > > > > > > > stable_node = page_stable_node(page); > > > > > > Line 2032 in mm/ksm.c::cmp_and_merge_page() sees the return value of a > > > call to "page_stable_node()", which can depend on a "READ_ONCE()", being > > > assigned to "stable_node". > > > > > > > if (stable_node) { > > > > if (stable_node->head != &migrate_nodes && > > > > get_kpfn_nid(READ_ONCE(stable_node->kpfn)) != > > > > NUMA(stable_node->nid)) { > > > > stable_node_dup_del(stable_node); ‣dup: stable_node > > > > stable_node->head = &migrate_nodes; > > > > list_add(&stable_node->list, stable_node->head); > > > > > > The dependency chain then runs into the two following if's, through an > > > assignment of "migrate_nodes" to "stable_node->head" (line 2038) and > > > finally reaches a call to "list_add()" (line 2039) where > > > "stable_node->head" gets passed as the second function argument. > > > > Huh. > > > > But migrate_nodes is nothing more or less than a list_head structure. > > So one would expect that some other mechanism is protecting its ->prev > > and ->next pointers. > > > > > > } > > > > } > > > > > > > > static inline void list_add(struct list_head *new, struct list_head *head) > > > > { > > > > __list_add(new, head, head->next); > > > > } > > > > > > > > static inline void __list_add(struct list_head *new, > > > > struct list_head *prev, > > > > struct list_head *next) > > > > { > > > > if (!__list_add_valid(new, prev, next)) > > > > return; > > > > > > > > next->prev = new; > > > > new->next = next; > > > > new->prev = prev; > > > > WRITE_ONCE(prev->next, new); > > > > } > > > > > > By being passed into "list_add()" via "stable_node->head", the > > > dependency chain eventually reaches a "WRITE_ONCE()" in "__list_add()" > > > whose destination address, "stable_node->head->next", is part of the > > > dependency chain and therefore carries an address dependency. > > > > > > However, as a result of the assignment in line 2038, Clang knows that > > > "stable_node->head" is "migrate_nodes" and replaces it, thereby breaking > > > the dependency chain. > > > > > > What do you think? > > > > Given that this is a non-atomic update, there had better be something > > protecting it. This something might be a lock, a decremented-to-zero > > reference count, a rule about only one kthread being permitted to update > > that list, and so on. In all such cases, the code would not be relying > > on the dependency, but rather on whatever was protecting that operation. > > > > Or am I missing something here? > > Nope, missing nothing, that was exactly it! > > In ksm_scan_thread(), which calls ksm_do_scan(), which calls > cmp_and_merge_page(), there is a mutex_lock() / mutex_unlock() pair, > surrounding the dependency. Whew!!! ;-) > Still keeping this as a trophy for our dependency checker though ;-) As well you should! Thanx, Paul > Many thanks, > Paul > > PS Sorry for the late reply - been distracted .. > > > > > Thanx, Paul > > > > > Many thanks, > > > Paul > > > > > > -- > > > [1]: https://lore.kernel.org/all/Yk7%2FT8BJITwz+Og1@Pauls-MacBook-Pro.local/ > > >