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 X-Spam-Level: X-Spam-Status: No, score=-15.2 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7BDCDC433FE for ; Thu, 3 Dec 2020 18:03:55 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C90EF20793 for ; Thu, 3 Dec 2020 18:03:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C90EF20793 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 375B46B0070; Thu, 3 Dec 2020 13:03:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3000B8D0003; Thu, 3 Dec 2020 13:03:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A1198D0002; Thu, 3 Dec 2020 13:03:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0176.hostedemail.com [216.40.44.176]) by kanga.kvack.org (Postfix) with ESMTP id F34A36B0070 for ; Thu, 3 Dec 2020 13:03:53 -0500 (EST) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id A66B0180AD80F for ; Thu, 3 Dec 2020 18:03:53 +0000 (UTC) X-FDA: 77552744346.04.cap19_1f0cc9c273bd Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin04.hostedemail.com (Postfix) with ESMTP id 897778009E8E for ; Thu, 3 Dec 2020 18:03:53 +0000 (UTC) X-HE-Tag: cap19_1f0cc9c273bd X-Filterd-Recvd-Size: 3257 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by imf46.hostedemail.com (Postfix) with ESMTP for ; Thu, 3 Dec 2020 18:03:52 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 8CE2DAC2E; Thu, 3 Dec 2020 18:03:51 +0000 (UTC) To: Zhaoyang Huang , Zhaoyang Huang , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <1606995362-16413-1-git-send-email-zhaoyang.huang@unisoc.com> From: Vlastimil Babka Subject: Re: [PATCH] mm: fix a race on nr_swap_pages Message-ID: <4c9b5a0c-9971-9960-b6a2-4e2966fb145b@suse.cz> Date: Thu, 3 Dec 2020 19:03:50 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 MIME-Version: 1.0 In-Reply-To: <1606995362-16413-1-git-send-email-zhaoyang.huang@unisoc.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: quoted-printable 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 12/3/20 12:36 PM, Zhaoyang Huang wrote: > The scenario on which "Free swap -4kB" happens in my system, which is c= aused by > get_swap_page_of_type or get_swap_pages racing with show_mem. Remove t= he race > here. >=20 > Signed-off-by: Zhaoyang Huang > --- > mm/swapfile.c | 7 +++---- > 1 file changed, 3 insertions(+), 4 deletions(-) >=20 > diff --git a/mm/swapfile.c b/mm/swapfile.c > index cf63b5f..13201b6 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -974,6 +974,8 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entr= ies[], int entry_size) > /* Only single cluster request supported */ > WARN_ON_ONCE(n_goal > 1 && size =3D=3D SWAPFILE_CLUSTER); > =20 > + spin_lock(&swap_avail_lock); > + > avail_pgs =3D atomic_long_read(&nr_swap_pages) / size; > if (avail_pgs <=3D 0) > goto noswap; This goto will leave with the spin lock locked, so that's a bug. > @@ -986,8 +988,6 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entr= ies[], int entry_size) > =20 > atomic_long_sub(n_goal * size, &nr_swap_pages); > =20 > - spin_lock(&swap_avail_lock); > - Is the problem that while we adjust n_goal with a min3(..., avail_pgs), s= omebody else can decrease nr_swap_pages in the meanwhile and then we underflow? I= f yes, the spin lock won't eliminate all such cases it seems, as e.g. get_swap_page_of_type isn't done under the same lock, AFAIK. > start_over: > node =3D numa_node_id(); > plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_li= sts[node]) { > @@ -1061,14 +1061,13 @@ swp_entry_t get_swap_page_of_type(int type) > =20 > spin_lock(&si->lock); > if (si->flags & SWP_WRITEOK) { > - atomic_long_dec(&nr_swap_pages); > /* This is called for allocating swap entry, not cache */ > offset =3D scan_swap_map(si, 1); > if (offset) { > + atomic_long_dec(&nr_swap_pages); > spin_unlock(&si->lock); > return swp_entry(type, offset); > } > - atomic_long_inc(&nr_swap_pages); This hunk looks safer, unless I miss something. Did you check if it's eno= ugh to prevent the negative values on your systems? > } > spin_unlock(&si->lock); > fail: >=20