Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp4782181ybc; Fri, 15 Nov 2019 09:59:58 -0800 (PST) X-Google-Smtp-Source: APXvYqyFZVzWKK5Gc5w1w6JsX/AwJNkJPN0chs5fQEVG7pH5OjPsPpSDS7Z6yugIyMZhLGvXJ3xM X-Received: by 2002:a17:906:3ed2:: with SMTP id d18mr2624329ejj.84.1573840798136; Fri, 15 Nov 2019 09:59:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573840798; cv=none; d=google.com; s=arc-20160816; b=fXMlAduAVYD86een2hJQ657tA3+W4OGFdt2XVYsW5sANQrdRfmyy/7abpV1p7vdCNd 8daP70rMSiVrFjohe92HF3CkJe/8ySY7AQ8504iNjK+68uzrFZNNp05DLOsaJQFVpZTH DggOFfqOtSV96Im8buKlAzh6GTAkjRuZDdXvIHSywr8NBpsQrtq5xCa+QOsNHoMowm65 +qBkDV6gJiCDN8e62eWotGb2k8l5gdT7OP74hEh6MvK2i81KAbNN6pvm/M6bo6O8Hwbj tP3rIHZeqPHXlK+T0TaalbZGH75UEquw9duTZhiTWa6SRRl7hrRC/Kk21T0b2s5VJo+m REVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date:dkim-signature; bh=f1Oh1RF8Ut5xfZg2ksqYtAu0PSRZLhofY5NfDnVJV1E=; b=YGgdwMzia5PHPNPWtjX8INatghJQjFfEHMpU30rJQ1YubkzGmmv8ofmJvoTQtUixlh QuAyCcZ36fKOjq3xC07d5w3fHEDcsvlb2AoqMluKPlkXWt0KgH8geThyWri9V/9gs3XL lCFlWtuE4psMmC1AdiWX5ykPJS7n7ChZyLFZi5IAnvGr/oxe/hgZ+Zh2mrGh13R5nvDy zz4hE6s/BLhnTs32a9GuLxQGfuISLLp+Jm0gIjMKn7UusIWHQGSpby7wwoQ62q6mtf0Y p0/xzq3WiM5ONO/kH3GEniNc2MwI5C6vsRPq/zSQja1iITJ3oQY+Ms7cb76FihRdq+l7 0Jfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=le2OgXjL; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id la4si6189087ejb.297.2019.11.15.09.59.33; Fri, 15 Nov 2019 09:59:58 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=le2OgXjL; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726655AbfKOR4A (ORCPT + 99 others); Fri, 15 Nov 2019 12:56:00 -0500 Received: from mail-pl1-f193.google.com ([209.85.214.193]:40404 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726131AbfKOR4A (ORCPT ); Fri, 15 Nov 2019 12:56:00 -0500 Received: by mail-pl1-f193.google.com with SMTP id e3so5164465plt.7 for ; Fri, 15 Nov 2019 09:56:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=f1Oh1RF8Ut5xfZg2ksqYtAu0PSRZLhofY5NfDnVJV1E=; b=le2OgXjLuxKOzGanxn690BYFbpSWFpbYsCAtepodK3Nppn99TktillXTJ3pTaoaS0b p5G0nIrWIC3UyFh6Q+lQmLk8L3n/FO+3mCYs9DPd+e7q3JeUINwprkOOXD7QDvRrDKYp DQWke5NnA3d/K+VJXND/cneNY+NW1Diq6zUfTceC0PhqmWZ9RhJqXPk5fFRm2a+vhnkY mftZjzzCvwuJoFAAwdNuD6Ppn/JuL5pF34UUe16ySj4tVAhtH0VhH4Cc6QlGVIFuGSbT /NcxCkkvR1Gxz7JHOgTe4FVkUPZkmkUi+BaUun16tPmTWsaele0h0izQ4ujuiKd2rX/V il6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=f1Oh1RF8Ut5xfZg2ksqYtAu0PSRZLhofY5NfDnVJV1E=; b=WYvcDYYv0Fk2sOUJ/+sA8gUpcJpbZcuuKWA5MJymgLdP0lIqBAb/fttkiAZg+uDMdJ LWCfoXqRDBv1msZAtSLdBYlAoCLb/ktEmdB+Vu4qAx4jmwedMnpVAvk6tlRY44oviPmR fgdl0CIy9WpTIeZlCvIiTGL+kpecFMQ8BUBPNFxGr+eAcXW8B7O/OGOQfR0hzLHwR1O1 sdvoHnVwr7xBfSOA8ot+RUNGO1vpzSjNdrX+HlUsNb/zbz/KicOLQn9gCo8n+IBHbECZ bERMzpOvtq3fhje/2/6nU2eG9xPHXNmBuouxXGMcjN73iChbO8DxIuVurvT0wOExA/ts li2Q== X-Gm-Message-State: APjAAAXuo6wMcGyKmhtqeoy0wqb6AMtowzLTKhY6jfdEIEaNjcdYg4O4 rIP8i7UAnnMc9QdrSSJYvXV7EA== X-Received: by 2002:a17:902:b416:: with SMTP id x22mr16514918plr.12.1573840558821; Fri, 15 Nov 2019 09:55:58 -0800 (PST) Received: from [100.112.92.218] ([104.133.9.106]) by smtp.gmail.com with ESMTPSA id y6sm9357412pfm.12.2019.11.15.09.55.57 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 15 Nov 2019 09:55:58 -0800 (PST) Date: Fri, 15 Nov 2019 09:55:46 -0800 (PST) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Andrey Ryabinin cc: Hugh Dickins , Andrea Arcangeli , "linux-mm@kvack.org" , LKML Subject: Re: KSM WARN_ON_ONCE(page_mapped(page)) in remove_stable_node() In-Reply-To: Message-ID: References: User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 13 Nov 2019, Andrey Ryabinin wrote: > When remove_stable_node() races with __mmput() and squeezes in between ksm_exit() and exit_mmap(), > the WARN_ON_ONCE(page_mapped(page)) in remove_stable_node() could be triggered. Thanks for doing the work on that. > > Should we just remove the warning? It seems to be safe to do, all callers are able to handle -EBUSY, > or there is a better way to fix this? Please just remove the warning and update the comment. At first I thought it would still be worth leaving the warning in, to explain rare EBUSY; but after looking (as you did) at exactly how remove_stable_node() gets to be called, the places that care about its failure already expect KSM to be quiesced, and tell the admin EBUSY if not. This case is not different, just slightly delayed. So there's no need to try to be any cleverer about it. Though now that you have found the case to be a real one, I do think it would be an improvement for remove_stable_node_chain() not to give up at the first -EBUSY, but remove as much as it can before finishing. But perhaps there's a subtlety that prevents that - I'm not familiar with the stable node chains. Hugh > > > > It's easily reproducible with the following script: > (ksm_test.c attached) > > #!/bin/bash > > gcc -lnuma -O2 ksm_test.c -o ksm_test > echo 1 > /sys/kernel/mm/ksm/run > ./ksm_test & > sleep 1 > echo 2 > /sys/kernel/mm/ksm/run > > and the patch bellow which provokes that race. > > --- > include/linux/ksm.h | 4 +++- > include/linux/mm_types.h | 1 + > kernel/fork.c | 4 ++++ > 3 files changed, 8 insertions(+), 1 deletion(-) > > diff --git a/include/linux/ksm.h b/include/linux/ksm.h > index e48b1e453ff5..18384ea472f8 100644 > --- a/include/linux/ksm.h > +++ b/include/linux/ksm.h > @@ -33,8 +33,10 @@ static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) > > static inline void ksm_exit(struct mm_struct *mm) > { > - if (test_bit(MMF_VM_MERGEABLE, &mm->flags)) > + if (test_bit(MMF_VM_MERGEABLE, &mm->flags)) { > __ksm_exit(mm); > + mm->ksm_wait = 1; > + } > } > > /* > diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h > index 270aa8fd2800..3df8290528c2 100644 > --- a/include/linux/mm_types.h > +++ b/include/linux/mm_types.h > @@ -463,6 +463,7 @@ struct mm_struct { > > /* Architecture-specific MM context */ > mm_context_t context; > + unsigned long ksm_wait; > > unsigned long flags; /* Must use atomic bitops to access */ > > diff --git a/kernel/fork.c b/kernel/fork.c > index 5fb7e1fa0b05..be6ef4e046f0 100644 > --- a/kernel/fork.c > +++ b/kernel/fork.c > @@ -1074,6 +1074,10 @@ static inline void __mmput(struct mm_struct *mm) > uprobe_clear_state(mm); > exit_aio(mm); > ksm_exit(mm); > + > + if (mm->ksm_wait) > + schedule_timeout_uninterruptible(10*HZ); > + > khugepaged_exit(mm); /* must run before exit_mmap */ > exit_mmap(mm); > mm_put_huge_zero_page(mm); > -- > 2.23.0 >