Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp2620150imn; Tue, 2 Aug 2022 09:36:20 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v2XLGHtiAIq9RXIu6oDfLvigs9fwbY0W2NA1iBg5yT7PhpbenpQHMSrWr7sSXyMZJY9Xo4 X-Received: by 2002:a17:907:168f:b0:72b:5571:2228 with SMTP id hc15-20020a170907168f00b0072b55712228mr16780850ejc.397.1659458180611; Tue, 02 Aug 2022 09:36:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659458180; cv=none; d=google.com; s=arc-20160816; b=PHcLBzomfoMpKKEilxf0IExtQQbvGLTafW0MgfxiLW+BbqT2XnVkMECG/Chjju2Pon 4UP/6etTeQo0v5g41LM3UHORCnxywUEDMBjAmF6qWpldNVrctDA7bq5z4HJmGoUlurg/ +5JuUY5PCzkd6f4xPfibWTyob40IoV6/OUSbaxk9d7F+VrHK/TCQk1/MMiQ4PUBRmQps mtFOPeyuS//vyrAEybzJjL0kNTPpDWlHeAzt8To9vtox3ag0GwT07V1G691Arsgxx/DN ijXhjPa9DlUvp+gzUDZPBwwSHhHIxHErTfVRJ5Y2VPch22USckzlkHBGxQek31kXb873 sGDw== 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-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=c8A0QKURN28UpiUL/e1DYdUv6Gv6n1LaHMwV8hNKfF4=; b=ZucVCaXBPYlBQPrOiJLH5cQ0E6zVLoXiqkiTWVUvMxT82S8LuGIE6tHwv77HUzPXKn Fub7acpu/dCAhGSl6RlcOUtA2Cgc7eDx/1JT7i+AI9UPmqCCUu43MN455bQHCEW+yjRo FBHj3YMTlImalx3X73O1TQkIBNB37+40mF4MegJ5MBrr7QV6uSDOvqKo+bYiaVjqNFA7 XUyJAGVDANg6ozp50Fu/gSNR3Bcwgmfz84Vamdsu3qxOt6REA6J3KgM7EMWxCLtRZ3V3 +/BlZxrgya1uh1TYrPnY+m4rfePqYJsqgE3DC74sGhpGD+Hb5jSURe94rgVx0n2Yhn+3 5TDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=EsqYrBsK; 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 s25-20020a1709066c9900b007065d999015si10758389ejr.232.2022.08.02.09.35.56; Tue, 02 Aug 2022 09:36:20 -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=@infradead.org header.s=casper.20170209 header.b=EsqYrBsK; 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 S232254AbiHBPpL (ORCPT + 99 others); Tue, 2 Aug 2022 11:45:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229637AbiHBPpK (ORCPT ); Tue, 2 Aug 2022 11:45:10 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 947DB281 for ; Tue, 2 Aug 2022 08:45:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=c8A0QKURN28UpiUL/e1DYdUv6Gv6n1LaHMwV8hNKfF4=; b=EsqYrBsKLIEEbToIGUCVejRLiH OaKvP0ebyxP/nXFgZbiAEqgAIlMEVdNONVVLJFd8QOGXDO5hXOwCfE2+omviU+iSB1NkBuZPSFd77 jAnnl53gpjdN+lI2ltxwnBxal6pUsueFAeJcgoAdhet+2Tke8JlcVlY1ljLuIUH0ELkml4CrCIT9k qOFWxUNm9HC3vJUrSATGZS0Q2Td5kEjacVr0tSoCjsRJ2T7uESktytAy7LK6v0mzPk3tIhG/KWgOQ n5V+jVqr6hgZMr1kUNfy70JubUyVtBHDiqxSdMzYK7cBUy8b+9SBrGx54GuQ1Qp/dGnZ1odwYhovy hwSBF/Nw==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1oIu4c-008TE9-51; Tue, 02 Aug 2022 15:44:46 +0000 Date: Tue, 2 Aug 2022 16:44:46 +0100 From: Matthew Wilcox To: Kefeng Wang Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Abhishek Shah , Gabriel Ryan Subject: Re: [PATCH] mm: ksm: fix data-race in __ksm_enter / run_store Message-ID: References: <20220802151550.159076-1-wangkefeng.wang@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220802151550.159076-1-wangkefeng.wang@huawei.com> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE 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, Aug 02, 2022 at 11:15:50PM +0800, Kefeng Wang wrote: > The ksm_run is alread protected by ksm_thread_mutex in run_store, we > could add this lock in __ksm_enter() to avoid the above issue. I don't think this is a great fix. Why not protect the store with ksm_mmlist_lock? ie: mutex_lock(&ksm_thread_mutex); wait_while_offlining(); if (ksm_run != flags) { + spin_lock(&ksm_mmlist_lock); ksm_run = flags; + spin_unlock(&ksm_mmlist_lock); if (flags & KSM_RUN_UNMERGE) { set_current_oom_origin(); err = unmerge_and_remove_all_rmap_items(); clear_current_oom_origin(); if (err) { + spin_lock(&ksm_mmlist_lock); ksm_run = KSM_RUN_STOP; + spin_unlock(&ksm_mmlist_lock); ... (I also don't think this is a real bug, because the call to unmerge_and_remove_all_rmap_items() will "cure" the misplacement of items in the list, but there's value in shutting up the tools, I suppose)