From patchwork Fri Dec 4 02:52:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhaoyang Huang X-Patchwork-Id: 11950469 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 1898FC4361A for ; Fri, 4 Dec 2020 02:52:31 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 78B7D224BE for ; Fri, 4 Dec 2020 02:52:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 78B7D224BE 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 7876E6B0036; Thu, 3 Dec 2020 21:52:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 737286B005C; Thu, 3 Dec 2020 21:52:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 64E0E8D0001; Thu, 3 Dec 2020 21:52:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0095.hostedemail.com [216.40.44.95]) by kanga.kvack.org (Postfix) with ESMTP id 4E0896B0036 for ; Thu, 3 Dec 2020 21:52:29 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 179843635 for ; Fri, 4 Dec 2020 02:52:29 +0000 (UTC) X-FDA: 77554076418.12.match82_54045a6273c1 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin12.hostedemail.com (Postfix) with ESMTP id E4A7618015E19 for ; Fri, 4 Dec 2020 02:52:28 +0000 (UTC) X-HE-Tag: match82_54045a6273c1 X-Filterd-Recvd-Size: 4672 Received: from mail-pj1-f65.google.com (mail-pj1-f65.google.com [209.85.216.65]) by imf01.hostedemail.com (Postfix) with ESMTP for ; Fri, 4 Dec 2020 02:52:28 +0000 (UTC) Received: by mail-pj1-f65.google.com with SMTP id iq13so2242290pjb.3 for ; Thu, 03 Dec 2020 18:52:28 -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=9yoTAkzlTrHVucyW+2c6nj5iiSRSPcc39rk34imqDjQ=; b=dpssYGWzzQYoAO6rwOZaXRmG3s21o5XP8gnSgw2V4/CygNQCn8nAzlNEnWDIaxygUd I16d0KpdxLtIXgdqTHdbfgeQoD1RxFLAf59+s8vH1Y2d2KVLjbNqFcGnmdCoCM0T/qR3 eNO7nOjx8AjjyWqCdPthIPKIRZPUj/gFN/2+dC7lw7wzWYBN7tAd5ksiC3wwliNfW2kZ 7evMS9DTmjq5bC4SaHTAOlo/VHNkjOcNQAg1dD/QoSl3qtqOLwOr58/vbL9XSBYHri2I b5Xd8Z2pRR9H2z50kyjMb7zWVLY7YsQPf4WSPXIzVTdks8+E4EgbDI93U7qWhjk3rJ/3 Oh7A== 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=9yoTAkzlTrHVucyW+2c6nj5iiSRSPcc39rk34imqDjQ=; b=e86ETZuXlikdxDHnIt2dJI4HbcnimDEY12Ewbr7xToq7Exb0QU4BO9Ih6fLKO1ahLa /kMpeJBjPLOKJIdTpm11vyKvowyaYb4WgYGJawY0ylHBEG2VLsYcBKWC+MrtksrX1I7X bUW+6D6rx6JVFeHZ6EaYW5g+2zMXSvm+hRVXB9EyGnhek0XFbb98LC8u2ykfAZa8kaK3 VCLiwh6nYYkEVQ1Y4YkzsIMYjU/J75FZHNAYET6JvbbhSBt66W57Cg5Uv6aP9csHZEka E6Np0I24BNT24N2VZmKC0L57Ao4Q3os8suMnERRnpnAGAerWl3b8U6pPhQzYAvjBOT3o yzgg== X-Gm-Message-State: AOAM530NbpGOvuvfc54NZDsf2wF80q+hMeCvjxkXJXGqQi4Dmu5R3Ny3 H9a+GZlmgzyl9mYaCwYkQ6k= X-Google-Smtp-Source: ABdhPJyDW/IMuaTHv4k8S/2xfuDKsJyQYf5NNZE+ECk2HqemCduDyBwiZY7gid6ffPfhTDfitsYb/A== X-Received: by 2002:a17:90a:2b0b:: with SMTP id x11mr2052721pjc.65.1607050347603; Thu, 03 Dec 2020 18:52:27 -0800 (PST) Received: from bj03382pcu.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id e14sm2156455pgv.64.2020.12.03.18.52.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 03 Dec 2020 18:52:27 -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 v2] mm: fix a race on nr_swap_pages Date: Fri, 4 Dec 2020 10:52:20 +0800 Message-Id: <1607050340-4535-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 several get_swap_pages racing with each other and show_swap_cache_info happens simutaniously. No need to add a lock on get_swap_page_of_type as we remove "Presub/PosAdd" here. ProcessA ProcessB ProcessC ngoals = 1 ngoals = 1 avail = nr_swap_pages(1) avail = nr_swap_pages(1) nr_swap_pages(1) -= ngoals nr_swap_pages(0) -= ngoals nr_swap_pages = -1 Signed-off-by: Zhaoyang Huang Acked-by: Vlastimil Babka --- change of v2: fix bug of unpaired of spin_lock --- --- mm/swapfile.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index cf63b5f..1212f17 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -974,9 +974,13 @@ 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) + if (avail_pgs <= 0) { + spin_unlock(&swap_avail_lock); goto noswap; + } if (n_goal > SWAP_BATCH) n_goal = SWAP_BATCH; @@ -986,8 +990,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 +1063,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: