Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp1130695rwb; Thu, 11 Aug 2022 16:51:22 -0700 (PDT) X-Google-Smtp-Source: AA6agR5kQGhQ46Z2csw/UaCxJDRMa221nfVPB5B8+MpWn4U/2MhHN9I9g0Lu9eS2JAZilIdmhIss X-Received: by 2002:a17:903:228f:b0:16f:1b48:234c with SMTP id b15-20020a170903228f00b0016f1b48234cmr1376240plh.5.1660261881798; Thu, 11 Aug 2022 16:51:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660261881; cv=none; d=google.com; s=arc-20160816; b=iUvEeAMJMnzXzvmKCJke0K4v84hPoUP6x6G+zNa4eN77/vMdOuaYP2dSLnY/peFHgI 9ASEN/7K9uvXbklPbw9PwVyi3SP/Tfan3z2zP9vYkNzFTM832h+9RN0mb9Np2m+9DNuP 0drX+a+WAljCGnf5xCFoECFCvdgXeqGefBHR3UzgHzcvKRNmt4PDvNhfLjTvXZeaqWyW tXnlE6kIBcJEGZFTtFYujN7ZSEGdKnZTpAe+g8VMS2NPkm8zwMxmU0zBLCgxy15COIvb h6NHgh2i45+GleZ+lsQ6dQmJVOURQLVPObbpK7WzHJ8ahemZ0BoKNj9J8tdxK7KxF3j0 gy4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=6R8kboFD3CAfTqSklA4Vn4DDki7u/5wDAFHZn76QBWA=; b=XTl9TprSyb64fo+dRBRRGNy7mH4rJx9nxf01IOguCJui1V1wek2y1ml6X84kYpVFGO 4yACkHkQ14QcEuxeZy7DA9jZFBXO7g65RP8Jq7wowhYUFXF9ze8yzRfXdbCa5EhAOwoQ sRgznxto82VUxZNzYujU0xKtdFtA3pDkVXzhLLKKHzB9qFJ5GtGSTZThRbB9YMFEGdZA H1jf0oLLpoD0N0OTP+Oh6wR8K0EIHhutAnhVP/0KirG/aQIQ7PWc2N4wRVkkOEoNRasc WQBBHfPaN5ZaGfSanKW4eEyqBhHUhse1XwMxR+pMIaFGvnJQp4P7r3PCYTsQPFy1Y9Bx WdHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b="ON9OXYj/"; 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 e36-20020a635464000000b0041603073c17si757728pgm.394.2022.08.11.16.51.07; Thu, 11 Aug 2022 16:51:21 -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=@linux-foundation.org header.s=korg header.b="ON9OXYj/"; 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 S236427AbiHKXA1 (ORCPT + 99 others); Thu, 11 Aug 2022 19:00:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236515AbiHKXAY (ORCPT ); Thu, 11 Aug 2022 19:00:24 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 420C4CC9 for ; Thu, 11 Aug 2022 16:00:22 -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 95FFF61559 for ; Thu, 11 Aug 2022 23:00:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C7A23C433D6; Thu, 11 Aug 2022 23:00:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1660258821; bh=zUD6ps9EUrGl2WNrqIea32JkQhG6EuoV7XvAvbJoKrk=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=ON9OXYj/TbX1s1HgqO9bUsiHHWt8rQuxBUipZMXgHT3yXGwV01PTLeOqckMR4BWJv U4pLIg31s6mEApPAxBNCj2cfBUAva+zjwmG7Cm36E72SBmy+glSExnwXNzW6MTbuGH PLiM8VbItAZVdgxp2luh2gM2QPlclpL+92lD0vS4= Date: Thu, 11 Aug 2022 16:00:20 -0700 From: Andrew Morton To: Kefeng Wang Cc: , , Abhishek Shah , Gabriel Ryan Subject: Re: [PATCH] mm: ksm: fix data-race in __ksm_enter / run_store Message-Id: <20220811160020.1e6823094217e8d6d3aaebdf@linux-foundation.org> In-Reply-To: <20220802151550.159076-1-wangkefeng.wang@huawei.com> References: <20220802151550.159076-1-wangkefeng.wang@huawei.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,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, 2 Aug 2022 23:15:50 +0800 Kefeng Wang wrote: > Abhishek reported a data-race issue, OK, but it would be better to perform an analysis of the alleged bug, describe the potential effects if the race is hit, etc. > --- a/mm/ksm.c > +++ b/mm/ksm.c > @@ -2507,6 +2507,7 @@ int __ksm_enter(struct mm_struct *mm) > { > struct mm_slot *mm_slot; > int needs_wakeup; > + bool ksm_run_unmerge; > > mm_slot = alloc_mm_slot(); > if (!mm_slot) > @@ -2515,6 +2516,10 @@ int __ksm_enter(struct mm_struct *mm) > /* Check ksm_run too? Would need tighter locking */ > needs_wakeup = list_empty(&ksm_mm_head.mm_list); > > + mutex_lock(&ksm_thread_mutex); > + ksm_run_unmerge = !!(ksm_run & KSM_RUN_UNMERGE); > + mutex_unlock(&ksm_thread_mutex); > > spin_lock(&ksm_mmlist_lock); > insert_to_mm_slots_hash(mm, mm_slot); > /* > @@ -2527,7 +2532,7 @@ int __ksm_enter(struct mm_struct *mm) > * scanning cursor, otherwise KSM pages in newly forked mms will be > * missed: then we might as well insert at the end of the list. > */ > - if (ksm_run & KSM_RUN_UNMERGE) > + if (ksm_run_unmerge) run_store() can alter ksm_run right here, so __ksm_enter() is still acting on the old setting? > list_add_tail(&mm_slot->mm_list, &ksm_mm_head.mm_list); > else > list_add_tail(&mm_slot->mm_list, &ksm_scan.mm_slot->mm_list);