From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 50528C00140 for ; Tue, 2 Aug 2022 15:45:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 455398E0001; Tue, 2 Aug 2022 11:45:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4039C6B0072; Tue, 2 Aug 2022 11:45:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2F4728E0001; Tue, 2 Aug 2022 11:45:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 218836B0071 for ; Tue, 2 Aug 2022 11:45:05 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id BCF0AC1388 for ; Tue, 2 Aug 2022 15:45:04 +0000 (UTC) X-FDA: 79755076128.07.C645815 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf26.hostedemail.com (Postfix) with ESMTP id B206F140100 for ; Tue, 2 Aug 2022 15:45:02 +0000 (UTC) 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> ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1659455103; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=c8A0QKURN28UpiUL/e1DYdUv6Gv6n1LaHMwV8hNKfF4=; b=jlVMs8A2zwxvciKzF6+Dyn2Yw3wpTYgTy8UQFF8Ogrx63loI5+ZPkfIpXtt0ZfPnF7y+bt qBLGIBgYW+N9vFEsdXSVm4ovYhrx9eQbaVgkybETjq8Sb7txt/7gf/b44mE1UHnZ4lFCGA hroev5i7bNf0BD+FQI5Cb1xZdsvZ0yY= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=EsqYrBsK; spf=none (imf26.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1659455103; a=rsa-sha256; cv=none; b=4nI8DmlVKBCbqM4LxfVMMEV93ymw6pCvjXV4+I9oKh/VslDKKDNNOTt2CfgEU87skKveVR 2GEiHwnPzkrmP0N2buzS3ZiKqMvF6Jiig5pZaulK57uhR4SgbHjLm9Jqs2Ta2vUiTkQHIo Gl5VInnLkFKR0/TRV2Qt8a4QeTayLTU= Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=EsqYrBsK; spf=none (imf26.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: B206F140100 X-Stat-Signature: tofq7j4933ykygwfigcucjry3oyk73xe X-HE-Tag: 1659455102-68844 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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)