Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp696846ybh; Wed, 22 Jul 2020 10:48:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzuB7n+5z4cUMdw+OJQ3TlATxgjHWQvBXM1Ey7Pculj0ZmNAHDoBgDLXJaEGt1XYwB/OKZd X-Received: by 2002:a05:6402:1ca6:: with SMTP id cz6mr546499edb.171.1595440106009; Wed, 22 Jul 2020 10:48:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595440106; cv=none; d=google.com; s=arc-20160816; b=f5LvMqWRIv0bZeFwlsqmsNt0Wmt3M1iJv6mz232fnQP7C6M/BnVzfbEp8rIVE2GBch qVY9P7/UGO9FqNAJfhRNQtipl3sWK7PTaHVsOB7sJvJ97BRD8PztF1AFzGZjfMnyWEF0 jjh0QfCazb9+mlNir1HXbpLIYQQmUg37wmE0n4pfruyJegM0IBTUaII7Av/NLh5MtS91 2//kgYeiPeQsZbCkaT2IsQh82jS7Xd/S5OCe5zPe6CQmOFe24aUsYBGzVU91wdF1sCMc D5ZOGaqbF+CraNo+PbDmWmVVAPj567OVHQx1QikfSUZb5EStx6yWGJVWesNRos9BlSsT 93ZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition:mime-version :message-id:subject:cc:to:from:date; bh=wIK2o+UarAf7XWWj0TPiEcgHlsE+IXk4111M8CYmrKA=; b=fdGTYBzEQwY0LI991+u2BDw7lf8BahJ4hnevrm8/ba8wUNJ2J9nJbRFeLYd2wYHYa6 GR3ACiQHRakgAtJq82p8JwqnO7YLZSLHD9FT2+lpAzRZsG9NRz5JhDkrjK7cjtHbMrqq GW6zrV6gxp+TCzQjSppkbL2CSX/5UMVFLSOzOx8jV2eW2bQXWEu7sw/4Fh6SZbzvUUsH YD4qO09LALq6g3gtJ87MGfptbYJtkiNzluu9YpZmIQC/jyZ3t2WuZPGB2Ayc2Auwd5UE ykZH8VrGauDlmdcye7xAGHM6r5BFdJAPzymaYLakgEu9qqXm/DWUGnYSVkgJyC2fvXes 4E2g== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=canonical.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l2si436605eja.112.2020.07.22.10.48.02; Wed, 22 Jul 2020 10:48:25 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730292AbgGVRom (ORCPT + 99 others); Wed, 22 Jul 2020 13:44:42 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:46018 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729830AbgGVRol (ORCPT ); Wed, 22 Jul 2020 13:44:41 -0400 Received: from mail-wr1-f72.google.com ([209.85.221.72]) by youngberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1jyInH-0007OX-Ud for linux-kernel@vger.kernel.org; Wed, 22 Jul 2020 17:44:39 +0000 Received: by mail-wr1-f72.google.com with SMTP id v4so860061wrb.2 for ; Wed, 22 Jul 2020 10:44:39 -0700 (PDT) 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:message-id:mime-version :content-disposition; bh=wIK2o+UarAf7XWWj0TPiEcgHlsE+IXk4111M8CYmrKA=; b=g5DKY566cQVjxRJQSZSZVD9HSSFqJLqZue1P9vttrR4HMGNesUXpzC/BryFBBlBGzY q7ize93U3308WVz32/hzBGDVhX9SDd7jwwfED0z+42E3H6AUzcUKXwHWaJJ8BNG3HCOj 8e2ZBjQ0o448hlawJuZ+QmfmcN1791htcU1X138LaDT8idGgvR42TlMP0PuWNEE/Yt52 DlyLxzUSSjNKiGAKRen6GFRhfLUKH4Jsyaqkhp98cDtBSlzAuerj2spX5desL4GwBHQ2 tkZcfMAtkHd1Mwqn48KC34RTp6XC9yl0EMC89JxoW/uD08jldCmpBzq7CMHmBBOFytUy Fo6g== X-Gm-Message-State: AOAM533f8SQppTJazfscmv3FdGrHvB3HXryk1ukbVjXmIZzUGNH4bMH8 PS5KyR51w73MGxgHJtIDXFD6/4nbeFCmfcSBGqnVMpnFfJiuThQXqePCcQ6bocHA3XgdsnrEFm+ sIKbw8jai0UvD39f3Ia3gQJA5GpAfLjROxn6jErFGjg== X-Received: by 2002:adf:a11d:: with SMTP id o29mr566752wro.421.1595439879197; Wed, 22 Jul 2020 10:44:39 -0700 (PDT) X-Received: by 2002:adf:a11d:: with SMTP id o29mr566735wro.421.1595439878868; Wed, 22 Jul 2020 10:44:38 -0700 (PDT) Received: from localhost (host-87-11-131-192.retail.telecomitalia.it. [87.11.131.192]) by smtp.gmail.com with ESMTPSA id 12sm430632wmg.6.2020.07.22.10.44.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Jul 2020 10:44:38 -0700 (PDT) Date: Wed, 22 Jul 2020 19:44:36 +0200 From: Andrea Righi To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH] mm: swap: do not wait for lock_page() in unuse_pte_range() Message-ID: <20200722174436.GB841369@xps-13> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Waiting for lock_page() with mm->mmap_sem held in unuse_pte_range() can lead to stalls while running swapoff (i.e., not being able to ssh into the system, inability to execute simple commands like 'ps', etc.). Replace lock_page() with trylock_page() and release mm->mmap_sem if we fail to lock it, giving other tasks a chance to continue and prevent the stall. This issue can be easily reproduced running swapoff in systems with a large amount of RAM (>=100GB) and a lot of pages swapped out to disk. A specific use case is to run swapoff immediately after resuming from hibernation. Under these conditions and without this patch applied the system can be stalled even for 15min, with this patch applied the system is always responsive. Signed-off-by: Andrea Righi --- mm/swapfile.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 987276c557d1..794935ecf82a 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1977,7 +1977,11 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, return -ENOMEM; } - lock_page(page); + if (!trylock_page(page)) { + ret = -EAGAIN; + put_page(page); + goto out; + } wait_on_page_writeback(page); ret = unuse_pte(vma, pmd, addr, entry, page); if (ret < 0) { @@ -2100,11 +2104,17 @@ static int unuse_mm(struct mm_struct *mm, unsigned int type, struct vm_area_struct *vma; int ret = 0; +retry: mmap_read_lock(mm); for (vma = mm->mmap; vma; vma = vma->vm_next) { if (vma->anon_vma) { ret = unuse_vma(vma, type, frontswap, fs_pages_to_unuse); + if (ret == -EAGAIN) { + mmap_read_unlock(mm); + cond_resched(); + goto retry; + } if (ret) break; } -- 2.25.1