Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2773938pxb; Mon, 19 Apr 2021 13:38:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJznW5unwo6gOt5lih6GXrzswSRFNVpZugpx5hW/OIJKW7uRLUeeK2P/lKAIQzLqz5DACckO X-Received: by 2002:a17:906:3c55:: with SMTP id i21mr9367304ejg.391.1618864729340; Mon, 19 Apr 2021 13:38:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618864729; cv=none; d=google.com; s=arc-20160816; b=Q0c7X5JT6SEu5mxJRZa3tE0EbuN/ZjZu2Nbk3f6UEGFfNXvqkkuVEwBd8O+IR4EO8J bHs67AV4dYPNMVXiJs8IH4I/cjRj2MZOxV8sA0gz49golaDeXUtI6A7piZuTsXsZhPgH iUrUY8kvuKnrxyQmSfMnxzIKZFWJbDwHNijDkcz9/zCgIOdbJbGg2GaK6WjVNoK/7axP Mly7wAB6LSmXxKgRKxFc/1Cc4G3zLJn85kJw3en5T1Pw10Nr/pkNDxQc16d57cG2Ta0i vhwuBkFfvn0MQ21+H5zaSeiSS/octuuYE9knDAGJUj542o+Eye53nVpjENieJ0gmXCxD ivdw== 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=TWJ6b+wJROTKL7GE+elLAj17ElKzrLVUoC3BxvWRtfw=; b=pWsrJzxOsJGfgt7mFMubg3qBVB2JQW0Srj7P8kvegfQbRtK7axGfkJGAh8ATJ1QblJ UxSWn5SrYrgB893d9fHOa8NBQzwfpp7fQtsD28x5XFCOH6xp+sHJpagYZVNSyzEipHL2 m4pQSeqRibTmq80SkgTgyTFwUldVRxAQUOd9VJb4ip3i0vXCXJwVg7+5uU/GxF8AmZYN +iOsDCdgonitR0POGYnbfUPw229IDJXxBm7GAnSTItYpDSX5RjrkT6KIisP5BlymCnd8 5a0BjP0BxADoNH6tFXNCdJ4uGxqTZzGvm8t9CwhUc+tnf6nHrkRZhlWCi0g/W4ZAvDVx +rpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b="lN1UlO/C"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id fs20si11656304ejc.630.2021.04.19.13.38.25; Mon, 19 Apr 2021 13:38:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b="lN1UlO/C"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239306AbhDSRGM (ORCPT + 99 others); Mon, 19 Apr 2021 13:06:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52952 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230127AbhDSRGL (ORCPT ); Mon, 19 Apr 2021 13:06:11 -0400 Received: from mail.skyhub.de (mail.skyhub.de [IPv6:2a01:4f8:190:11c2::b:1457]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6153C06174A for ; Mon, 19 Apr 2021 10:05:41 -0700 (PDT) Received: from zn.tnic (p200300ec2f078100273c47da03104508.dip0.t-ipconnect.de [IPv6:2003:ec:2f07:8100:273c:47da:310:4508]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 43CB91EC0347; Mon, 19 Apr 2021 19:05:40 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1618851940; h=from:from: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; bh=TWJ6b+wJROTKL7GE+elLAj17ElKzrLVUoC3BxvWRtfw=; b=lN1UlO/CFvknjp15asW0ZjeOuNvhRMptkMK/gweUTMxxNvforBchg1vlWjrYeAe+bXpwVd nxIkuqLwqc/72mjiNg3LtpAWPRPPPD9z+XPythoADeeA+N7pNLTu3Lj+UTDMC5fLRpWcBF QqTSQyHQwRdB4f9UswHxATVUCyPTCw0= Date: Mon, 19 Apr 2021 19:05:38 +0200 From: Borislav Petkov To: Naoya Horiguchi Cc: linux-mm@kvack.org, Tony Luck , Aili Yao , Andrew Morton , Oscar Salvador , David Hildenbrand , Andy Lutomirski , Naoya Horiguchi , linux-kernel@vger.kernel.org Subject: Re: [PATCH v1 1/3] mm/memory-failure: Use a mutex to avoid memory_failure() races Message-ID: <20210419170538.GG9093@zn.tnic> References: <20210412224320.1747638-1-nao.horiguchi@gmail.com> <20210412224320.1747638-2-nao.horiguchi@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20210412224320.1747638-2-nao.horiguchi@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 13, 2021 at 07:43:18AM +0900, Naoya Horiguchi wrote: > From: Tony Luck > > There can be races when multiple CPUs consume poison from the same > page. The first into memory_failure() atomically sets the HWPoison > page flag and begins hunting for tasks that map this page. Eventually > it invalidates those mappings and may send a SIGBUS to the affected > tasks. > > But while all that work is going on, other CPUs see a "success" > return code from memory_failure() and so they believe the error > has been handled and continue executing. > > Fix by wrapping most of the internal parts of memory_failure() in > a mutex. > > Signed-off-by: Tony Luck > Signed-off-by: Naoya Horiguchi > --- > mm/memory-failure.c | 19 +++++++++++++++++-- > 1 file changed, 17 insertions(+), 2 deletions(-) > > diff --git v5.12-rc5/mm/memory-failure.c v5.12-rc5_patched/mm/memory-failure.c > index 24210c9bd843..c1509f4b565e 100644 > --- v5.12-rc5/mm/memory-failure.c > +++ v5.12-rc5_patched/mm/memory-failure.c > @@ -1381,6 +1381,8 @@ static int memory_failure_dev_pagemap(unsigned long pfn, int flags, > return rc; > } > > +static DEFINE_MUTEX(mf_mutex); > + > /** > * memory_failure - Handle memory failure of a page. > * @pfn: Page Number of the corrupted page > @@ -1424,12 +1426,18 @@ int memory_failure(unsigned long pfn, int flags) > return -ENXIO; > } So the locking patterns are done in two different ways, which are confusing when following this code: > + mutex_lock(&mf_mutex); > + > try_again: > - if (PageHuge(p)) > - return memory_failure_hugetlb(pfn, flags); > + if (PageHuge(p)) { > + res = memory_failure_hugetlb(pfn, flags); > + goto out2; > + } You have the goto to a label where you do the unlocking (btw, pls do s/out2/out_unlock/g;)... > + > if (TestSetPageHWPoison(p)) { > pr_err("Memory failure: %#lx: already hardware poisoned\n", > pfn); > + mutex_unlock(&mf_mutex); > return 0; ... and you have the other case where you unlock before returning. Since you've added the label, I think *all* the unlocking should do "goto out_unlock" instead of doing either/or. Thx. -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette