From patchwork Thu Dec 3 11:36:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhaoyang Huang X-Patchwork-Id: 11948521 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.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT 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 2EAD0C64E8A for ; Thu, 3 Dec 2020 11:36:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5D489206A1 for ; Thu, 3 Dec 2020 11:36:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5D489206A1 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 60AC26B0068; Thu, 3 Dec 2020 06:36:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 594526B0070; Thu, 3 Dec 2020 06:36:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 433236B0071; Thu, 3 Dec 2020 06:36:10 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0155.hostedemail.com [216.40.44.155]) by kanga.kvack.org (Postfix) with ESMTP id 2A97E6B0068 for ; Thu, 3 Dec 2020 06:36:10 -0500 (EST) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id DFA193626 for ; Thu, 3 Dec 2020 11:36:09 +0000 (UTC) X-FDA: 77551767258.30.maid30_2e09862273bb Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin30.hostedemail.com (Postfix) with ESMTP id CEE11180B3C8E for ; Thu, 3 Dec 2020 11:36:09 +0000 (UTC) X-HE-Tag: maid30_2e09862273bb X-Filterd-Recvd-Size: 4243 Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) by imf25.hostedemail.com (Postfix) with ESMTP for ; Thu, 3 Dec 2020 11:36:09 +0000 (UTC) Received: by mail-pl1-f195.google.com with SMTP id 4so995940plk.5 for ; Thu, 03 Dec 2020 03:36:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=wqd6B3MGg9FU3pSe+TUt1wCgmmcknZUopdwCUSxaGrw=; b=guM1o5Y6XAw7IphxI5qXSTe0jGqIc10MncBmcXcjxH8b1fRlicsuQdbzOwICJHy+Bi aEcJM8QxU0srioGSh9WB8pliTssXUkmNcrvZ45yZaMwas12tKu/0d5hdf7gwH3Rh0KjQ hakt7IjgtSc9hs2vfAvbf88qSQFdUv9eLT23WOysdX+Or9kmNf08ikGr9//0EQcTkAaZ MR3yFv+JroEaox/qJnWcO1yVOIKFoZvLq6SqfFDjBpxTiLdQyqWBkOPAX/0v48rvinEn Fvq2RM+/PXwyZYrWys7rPAEw0nCrR593R9inQsNOrwttWkFbA+jnHPUz05HAzLsStO69 a9yQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=wqd6B3MGg9FU3pSe+TUt1wCgmmcknZUopdwCUSxaGrw=; b=ArSc9q89McTJUABbKwjcIpkOGZcmgPK7RA+/ZhePS0ZXCEplGrsn4T7wj5zb3ZFmZs u9UXWZZztFfdpDKwLKChTQuGSVnGeWq9QCvS4UIKLhqudb+K7C+scdPoIybtPEL2Covi fvJiMYYMJE3AtOBCQEjLNVJy14QZaFJlQtZmlGPDCiX9RJXlq2oTdmAUVDjv3qAzQ5YJ BVVkbmWerhjAQfTeuoAvbRkV/4mWlAgqujUZfJviJcAz5P4S1yLTxS3BABUCudAc3Q55 qpgP1ZdMAIYMyNobuA3dn7N3arp5dSEnAkbtyr8MXuwsAvt11Q1Xv2nDApoB+tcyo6Oq 3M3w== X-Gm-Message-State: AOAM532shrAMdBzicfJwgSAHqmEphpXiM8lqf6MjIxX3tkSeBoU55TZA kWbSekfffBktr9ajQLYVh4U= X-Google-Smtp-Source: ABdhPJx46Y2bHQRP5Q9sMsDoVEb7XeWcubw21qfiGoH8CWJMxbcrxY9HCu1UGQ0LqNWsBHsLSk2BZQ== X-Received: by 2002:a17:902:b28a:b029:da:725c:14db with SMTP id u10-20020a170902b28ab02900da725c14dbmr2683231plr.72.1606995368488; Thu, 03 Dec 2020 03:36:08 -0800 (PST) Received: from bj03382pcu.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id y25sm1554665pfn.44.2020.12.03.03.36.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 03 Dec 2020 03:36:07 -0800 (PST) From: Zhaoyang Huang X-Google-Original-From: Zhaoyang Huang To: Zhaoyang Huang , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH] mm: fix a race on nr_swap_pages Date: Thu, 3 Dec 2020 19:36:02 +0800 Message-Id: <1606995362-16413-1-git-send-email-zhaoyang.huang@unisoc.com> X-Mailer: git-send-email 1.7.9.5 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: The scenario on which "Free swap -4kB" happens in my system, which is caused by get_swap_page_of_type or get_swap_pages racing with show_mem. Remove the race here. Signed-off-by: Zhaoyang Huang --- mm/swapfile.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) 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_entries[], int entry_size) /* Only single cluster request supported */ WARN_ON_ONCE(n_goal > 1 && size == SWAPFILE_CLUSTER); + spin_lock(&swap_avail_lock); + avail_pgs = atomic_long_read(&nr_swap_pages) / size; if (avail_pgs <= 0) goto noswap; @@ -986,8 +988,6 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_size) atomic_long_sub(n_goal * size, &nr_swap_pages); - spin_lock(&swap_avail_lock); - start_over: node = numa_node_id(); plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) { @@ -1061,14 +1061,13 @@ swp_entry_t get_swap_page_of_type(int type) 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 = 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); } spin_unlock(&si->lock); fail: