From patchwork Mon Apr 8 19:56:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 10890061 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 120AD1669 for ; Mon, 8 Apr 2019 19:56:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F0FD628698 for ; Mon, 8 Apr 2019 19:56:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E4CEE286FE; Mon, 8 Apr 2019 19:56:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 61F5A28698 for ; Mon, 8 Apr 2019 19:56:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 640E26B0007; Mon, 8 Apr 2019 15:56:16 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 5EFEB6B000A; Mon, 8 Apr 2019 15:56:16 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4DFA16B000C; Mon, 8 Apr 2019 15:56:16 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.200]) by kanga.kvack.org (Postfix) with ESMTP id 17BD96B0007 for ; Mon, 8 Apr 2019 15:56:16 -0400 (EDT) Received: by mail-pf1-f200.google.com with SMTP id g1so11208075pfo.2 for ; Mon, 08 Apr 2019 12:56:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:date:from:to:cc:subject :in-reply-to:message-id:references:user-agent:mime-version; bh=E+yVjhVz7hUuMuUS71zV31X7NgehVdKX9ZgoshuaFks=; b=Q+L6LrrTSsKKYqeLUIjntesEKqe0sU1oOCWAbtLpDSQfrTa3/ihun0c6HvWeY/x86D mEoGfbk2p4R+CjoEcYwegFy9V1zC2OtE9NrEnd821MaEz+CgJ0m9ijZtptPLl7cPd43C KmFEUTmQWFFi7Cwoa+LhfnJIX4kX5hiP55/hYchqBpNaQaOgcNh7CJrrwdLTYrMbD16F VfSzYkLk9mELYqYf7FGmPmGn5jCKraAbeyzt1n6GCPInSxAJlWx0SlYFEPjh9dCR4q2A HmtncYN1kCCXCPCY5/GjMPvSsquJ13A+eV/H7fIuSyPf3sVN7HtgTjFYCjh8DI8h7dtA 25rg== X-Gm-Message-State: APjAAAXOc0PeKn0rotKpzirv7HzA8hf7NOZigYAapfGcwF1Z+ogX7aXA 3/LUKHIErAKSmZK/PZujK1Tsh/jRyl6+kYW6XdrAIi49rU01Ze5G2YdUkj1ZKqaaB23zE9qfSIr 7NVGMX6mbCv7qBgpYwm97AZrKXj11SSL+myK4+yKA2cJMUNDktryTjymVOm7ZCICxug== X-Received: by 2002:a17:902:3e3:: with SMTP id d90mr32200794pld.271.1554753375599; Mon, 08 Apr 2019 12:56:15 -0700 (PDT) X-Received: by 2002:a17:902:3e3:: with SMTP id d90mr32200728pld.271.1554753374904; Mon, 08 Apr 2019 12:56:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554753374; cv=none; d=google.com; s=arc-20160816; b=AuAy+zP/xSPtXrqjnqa0RTwd0kNYJTbh6/VZqBuuCqqvgWV3qwPhqxl4uVCtoJ+aLo l3pabqzwV8+Cj5xk7i97IuM8vnxmDCNUGS1/75rORFYyDhNx9iPodHkT0dswCbQx/v2p LdX/N7D5aesC3x0w3jI70Qv5OC0bp31jbG6q8p+QG4IoONXEvqyHub7owD3Uuf9bJDoy Ufv+JBL0iaAGm6wDGZaG0xVCKdoNhptkFvidu6vfEIKu8NIxYnOWzSxeN98NNWy8BveM A0hdfGPovfHsSX1ctaAgzn5d1qX7TfXmgTkCOFCdstGnuPJYEkC63KS2WpC/NlfevffW LerA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:user-agent:references:message-id:in-reply-to:subject :cc:to:from:date:dkim-signature; bh=E+yVjhVz7hUuMuUS71zV31X7NgehVdKX9ZgoshuaFks=; b=ts8XV/j16DU8wGnyQDI3EbB3g7fxvj7HX3kMHJMZGRls+DIrnuXNE6jG7ivFI9KZTw DbmmQiinx/zlEGTJh2+PuDVkvAYjMptMYkBvU6/bofjFaVKF+rSjoOEhoK0mFh0WbLeN ibDwlXFmkK56rhUR6ZQhnFUM92mUJExWeXtxkjtYRFZbb6uv1Lrog1ZWzeSoBHe+BMcD eJx074dVDeR5AvX/1OTukYI+1wUH00dwNbuNbvf9GNZnPId3vvaMYXNn3MvsCjCf5MpX cwldnQ8Qc8TfvWYCSktwxEvwu5Ns3gMOyk1o3QDRUmQ4S+HlOVxUk9E36CxD9msVU6nZ pjng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=AMkTDxs1; spf=pass (google.com: domain of hughd@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=hughd@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id t11sor30384219pfh.36.2019.04.08.12.56.14 for (Google Transport Security); Mon, 08 Apr 2019 12:56:14 -0700 (PDT) Received-SPF: pass (google.com: domain of hughd@google.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=AMkTDxs1; spf=pass (google.com: domain of hughd@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=hughd@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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=E+yVjhVz7hUuMuUS71zV31X7NgehVdKX9ZgoshuaFks=; b=AMkTDxs1LHsHlchUXT3nGVvExN68Rr2G6ioVk0vWn79VfbBGuSrXUKd0aMLgaapNL1 v2UgpFjkjxIgHdLsqbOBzBU50FtFFja71QzwlDk7EijQZPac6FpLeM3eb7tfVnxSegBt UZJMawK98mMNgFOw76z8wQ3XeaqLIIGjxMwYxN4YRuGV1chNg7vvVra6khsBGAPMng0V Jq8ABXsISFdSvxCqsySwOBNubuEm8b3l4OGlUgUt7PiXncdwGtTGxaYiYY01N3rhpQHk Bkl4UDP+Pzn3WTQVSemDaW9Wv3kiln6EvB8xyIVnVNjQJcpXPzpK21u7vZCAkMWruZHu /HGg== X-Google-Smtp-Source: APXvYqyr6Nw2aE69+Sv05HhyzHpd5PPgyTc/AzcorrzgRuqd+2YHFvCQexJLbjXUeRLfMimlQ5muiQ== X-Received: by 2002:a62:4e86:: with SMTP id c128mr31870152pfb.39.1554753373844; Mon, 08 Apr 2019 12:56:13 -0700 (PDT) Received: from [100.112.89.103] ([104.133.8.103]) by smtp.gmail.com with ESMTPSA id j16sm41054868pfi.58.2019.04.08.12.56.12 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 08 Apr 2019 12:56:13 -0700 (PDT) Date: Mon, 8 Apr 2019 12:56:12 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Andrew Morton cc: Konstantin Khlebnikov , "Alex Xu (Hello71)" , Vineeth Pillai , Kelley Nielsen , Rik van Riel , Huang Ying , Hugh Dickins , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 1/4] mm: swapoff: shmem_find_swap_entries() filter out other types In-Reply-To: Message-ID: References: User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP Swapfile "type" was passed all the way down to shmem_unuse_inode(), but then forgotten from shmem_find_swap_entries(): with the result that removing one swapfile would try to free up all the swap from shmem - no problem when only one swapfile anyway, but counter-productive when more, causing swapoff to be unnecessarily OOM-killed when it should succeed. Fixes: b56a2d8af914 ("mm: rid swapoff of quadratic complexity") Signed-off-by: Hugh Dickins --- mm/shmem.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) --- 5.1-rc4/mm/shmem.c 2019-03-17 16:18:15.701823872 -0700 +++ linux/mm/shmem.c 2019-04-07 19:12:23.603858531 -0700 @@ -1099,10 +1099,11 @@ extern struct swap_info_struct *swap_inf static int shmem_find_swap_entries(struct address_space *mapping, pgoff_t start, unsigned int nr_entries, struct page **entries, pgoff_t *indices, - bool frontswap) + unsigned int type, bool frontswap) { XA_STATE(xas, &mapping->i_pages, start); struct page *page; + swp_entry_t entry; unsigned int ret = 0; if (!nr_entries) @@ -1116,13 +1117,12 @@ static int shmem_find_swap_entries(struc if (!xa_is_value(page)) continue; - if (frontswap) { - swp_entry_t entry = radix_to_swp_entry(page); - - if (!frontswap_test(swap_info[swp_type(entry)], - swp_offset(entry))) - continue; - } + entry = radix_to_swp_entry(page); + if (swp_type(entry) != type) + continue; + if (frontswap && + !frontswap_test(swap_info[type], swp_offset(entry))) + continue; indices[ret] = xas.xa_index; entries[ret] = page; @@ -1194,7 +1194,7 @@ static int shmem_unuse_inode(struct inod pvec.nr = shmem_find_swap_entries(mapping, start, nr_entries, pvec.pages, indices, - frontswap); + type, frontswap); if (pvec.nr == 0) { ret = 0; break; From patchwork Mon Apr 8 19:58:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 10890063 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 693891708 for ; Mon, 8 Apr 2019 19:58:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5305328691 for ; Mon, 8 Apr 2019 19:58:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4601D286D1; Mon, 8 Apr 2019 19:58:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 04EE928691 for ; Mon, 8 Apr 2019 19:58:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21FF36B0008; Mon, 8 Apr 2019 15:58:19 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 1CF526B000A; Mon, 8 Apr 2019 15:58:19 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0BF3C6B000C; Mon, 8 Apr 2019 15:58:19 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) by kanga.kvack.org (Postfix) with ESMTP id C2BCB6B0008 for ; Mon, 8 Apr 2019 15:58:18 -0400 (EDT) Received: by mail-pl1-f200.google.com with SMTP id 4so10673694plb.5 for ; Mon, 08 Apr 2019 12:58:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:date:from:to:cc:subject :in-reply-to:message-id:references:user-agent:mime-version; bh=29W9ucIOqItYnzmL2gQIQ5ExGyQgwi2PVG1yglWaKBM=; b=hxMyuD554iAbMdVsBzMReGT/iggMt2S5wIEj+2zKZVKVQ8RUYyGGT/aJMSFq61JBch yecjBtw38fLQWhcSYYfh/l0FPanx8vBRHGHtyipo3Dl/svD1kFZDHbBRgMCCN0Qdt+v8 XmJWWlTR1A0wD38KZjGmyhnNq/lx8PekrgzhCpamk59TFGD7LBgGrRl5Q2kv88G03IvG WbBsnBgqa9uN7SbxAiihS1hqfswLQtrpvH6Z6hi89mn+xbd76TwWW0Aeaw/rVhBZgZq/ neyfjBR+hDDOv5b1NMxZelQDB9JRMqedUnNDxyX/BG0B+4ikmfhQtT/g7zBL7n6jPGju k1Pw== X-Gm-Message-State: APjAAAWelubiuHFCPiXjTzGwEvUK0dnlzkjn7lFaJblhAFr/Uzof4xtw 1EqluICE4G6IOLgA+u3Exqn9vPYUWhNqCiFoG3+NFAWawV/ugJ/yfzfol+XFAHXujM12y28Rp32 8jDHAGPuTAFUD3k9Nr++79mzNzbqJ6+DMQ05lY1VhKGtsx/AN8WjhDwuB6Uo+S/y77A== X-Received: by 2002:a62:3892:: with SMTP id f140mr31668405pfa.128.1554753498403; Mon, 08 Apr 2019 12:58:18 -0700 (PDT) X-Received: by 2002:a62:3892:: with SMTP id f140mr31668350pfa.128.1554753497664; Mon, 08 Apr 2019 12:58:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554753497; cv=none; d=google.com; s=arc-20160816; b=uO/B/JuXd3+HOBh9JtbbohlkYzaCE844YjBewHCFDkdCftOYcJBWnuxHQDuHFSJNBZ atx2FBtHkGTreeUJoqUWRCnX/31QFRyLOsx6LQDHfEApsYgKpQIOiXo2ZX6RkCa4XA0Z ApRy/mmMFUBbhUr64E9Rl/i2GlA0UXOx4mf10pqLwroBg5mWd+8R6o3CgP177fucTb90 5BXhWwTxEZTx/HpRHN7SIoTAc8ykeUbDFo18TWeA4GIbt9j7B0Ft7vXcW2KtL47TCcF3 xb0rnmjE6WDv2n+PEmiIJv2Qo742+Fpvl/mIeNqwh/+BA75XHbMVQcf9V5jxQVFJeMPO fGZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:user-agent:references:message-id:in-reply-to:subject :cc:to:from:date:dkim-signature; bh=29W9ucIOqItYnzmL2gQIQ5ExGyQgwi2PVG1yglWaKBM=; b=hyqIXgk9MNN6E96HY8zJzkSGAKuzy6kxnDIe4EvgCST9eOBs0ga6hA29WOeGn5cjTP sdoJejsUw4hBpLlw6CS9XpQFzlMeczyso339RoLOzaUSc/q4CwrqMFJTVr/mVNmws3z6 lqXGHt+SEmxHlz5ZfXKX4FfYX4Z2kwETRyJ40XVodLK9wGm3KVpypUHGsbWChcgt7l2g XtXCLDBCxG76mplt97WrJciydTgAajUr8+fHZiyIpEbWMWNJEanIZU80OVJe5O/X7GiC UCtfZytRsorFZMYx+wjnU7tiboDglro6na2eLfTG2ydjjH8B7FqeEa3Z3D4vf+fO+SlQ LFjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Fy4t1CyU; spf=pass (google.com: domain of hughd@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=hughd@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id t65sor31766194pfb.3.2019.04.08.12.58.17 for (Google Transport Security); Mon, 08 Apr 2019 12:58:17 -0700 (PDT) Received-SPF: pass (google.com: domain of hughd@google.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Fy4t1CyU; spf=pass (google.com: domain of hughd@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=hughd@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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=29W9ucIOqItYnzmL2gQIQ5ExGyQgwi2PVG1yglWaKBM=; b=Fy4t1CyUr8Vd/Tq1kRsSKxv77weNKQj5DhgPy9nD2bCEsiBn3FEGyA8sZ83Fpwo6bW tRb1hXjVG3V8vMhxBjeLI6C1RvZMzNOtU33E4GDSDs0Ps0T0NgRjLBkn9n5ALGsshrwb Oxspjm1t7dapzNkUm1UBxH7cqGC7RV4j4v9uZukrNkd09KxA+zKmao0FCp1YBCvYdbAN 76r1I75mjP8bgo4nUEuuqhIjwkoGsrIzEzuTrPl0JUP+dwFcE0tW06qlb0KlIOOev7tg 5f+JD1JqYlb1nfbuCaPOdDZFwIKIgmIe2jO8gc4TX04rNrLUFEsId6g6YZYGYoCqlm2Z r0zA== X-Google-Smtp-Source: APXvYqwgOOpcElBS1UxKQNb+mYhbX92mCmLobuI2RnT22NAeFYi++VncsNYP/Uv6ILgmoBb2UinsgQ== X-Received: by 2002:a62:209c:: with SMTP id m28mr31113893pfj.233.1554753496623; Mon, 08 Apr 2019 12:58:16 -0700 (PDT) Received: from [100.112.89.103] ([104.133.8.103]) by smtp.gmail.com with ESMTPSA id l5sm55488310pfi.97.2019.04.08.12.58.15 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 08 Apr 2019 12:58:15 -0700 (PDT) Date: Mon, 8 Apr 2019 12:58:14 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Andrew Morton cc: Konstantin Khlebnikov , "Alex Xu (Hello71)" , Vineeth Pillai , Kelley Nielsen , Rik van Riel , Huang Ying , Hugh Dickins , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 2/4] mm: swapoff: remove too limiting SWAP_UNUSE_MAX_TRIES In-Reply-To: Message-ID: References: User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP SWAP_UNUSE_MAX_TRIES 3 appeared to work well in earlier testing, but further testing has proved it to be a source of unnecessary swapoff EBUSY failures (which can then be followed by unmount EBUSY failures). When mmget_not_zero() or shmem's igrab() fails, there is an mm exiting or inode being evicted, freeing up swap independent of try_to_unuse(). Those typically completed much sooner than the old quadratic swapoff, but now it's more common that swapoff may need to wait for them. It's possible to move those cases from init_mm.mmlist and shmem_swaplist to separate "exiting" swaplists, and try_to_unuse() then wait for those lists to be emptied; but we've not bothered with that in the past, and don't want to risk missing some other forgotten case. So just revert to cycling around until the swap is gone, without any retries limit. Fixes: b56a2d8af914 ("mm: rid swapoff of quadratic complexity") Signed-off-by: Hugh Dickins --- mm/swapfile.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) --- 5.1-rc4/mm/swapfile.c 2019-03-17 16:18:15.713823942 -0700 +++ linux/mm/swapfile.c 2019-04-07 19:15:01.269054187 -0700 @@ -2023,7 +2023,6 @@ static unsigned int find_next_to_unuse(s * If the boolean frontswap is true, only unuse pages_to_unuse pages; * pages_to_unuse==0 means all pages; ignored if frontswap is false */ -#define SWAP_UNUSE_MAX_TRIES 3 int try_to_unuse(unsigned int type, bool frontswap, unsigned long pages_to_unuse) { @@ -2035,7 +2034,6 @@ int try_to_unuse(unsigned int type, bool struct page *page; swp_entry_t entry; unsigned int i; - int retries = 0; if (!si->inuse_pages) return 0; @@ -2117,14 +2115,16 @@ retry: * If yes, we would need to do retry the unuse logic again. * Under global memory pressure, swap entries can be reinserted back * into process space after the mmlist loop above passes over them. - * Its not worth continuosuly retrying to unuse the swap in this case. - * So we try SWAP_UNUSE_MAX_TRIES times. + * + * Limit the number of retries? No: when shmem_unuse()'s igrab() fails, + * a shmem inode using swap is being evicted; and when mmget_not_zero() + * above fails, that mm is likely to be freeing swap from exit_mmap(). + * Both proceed at their own independent pace: we could move them to + * separate lists, and wait for those lists to be emptied; but it's + * easier and more robust (though cpu-intensive) just to keep retrying. */ - if (++retries >= SWAP_UNUSE_MAX_TRIES) - retval = -EBUSY; - else if (si->inuse_pages) + if (si->inuse_pages) goto retry; - out: return (retval == FRONTSWAP_PAGES_UNUSED) ? 0 : retval; } From patchwork Mon Apr 8 19:59:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 10890065 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1D0A71669 for ; Mon, 8 Apr 2019 19:59:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0523F285D2 for ; Mon, 8 Apr 2019 19:59:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E98AA28592; Mon, 8 Apr 2019 19:59:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7092C28592 for ; Mon, 8 Apr 2019 19:59:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 68F356B0007; Mon, 8 Apr 2019 15:59:37 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 616736B0008; Mon, 8 Apr 2019 15:59:37 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4DE7C6B000A; Mon, 8 Apr 2019 15:59:37 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) by kanga.kvack.org (Postfix) with ESMTP id 0EDF36B0007 for ; Mon, 8 Apr 2019 15:59:37 -0400 (EDT) Received: by mail-pl1-f197.google.com with SMTP id b34so10650678pld.17 for ; Mon, 08 Apr 2019 12:59:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:date:from:to:cc:subject :in-reply-to:message-id:references:user-agent:mime-version; bh=RSStTgCMJwvCqECwFlnDBhxtF8I569p9e6PuxiQP1oM=; b=Ml/qo4sD5b0KIGRgn7UKSkgiEj2t2PUCmjTPx00zC8GblbVym/Pgl9TGfASeI/hTML 2YdmTiAf71Jst8uprc545REPyzGWLzetcYuEGE9TBmw9DPWUH9Y5OFN+n4OKGKxmj0L5 J0sdzllhxYnrFuLU0ECE8wY5ZnAOWQSusQo5etUrSqxSz7Cyxg2AzS2GcGMjzu9VTI8U GJIWfZKT73IIvDS2m499Er3kVMotkF/mX6FEcQaCmpJUoq91rvlTPFegi+YRmaOcqZaC arRn/9xIgC2tdVsvkVtdstyiqhx8Cd2IKbZabDqy2i6gAleyp91rYdOrX9DrYHIYuFZY X+5Q== X-Gm-Message-State: APjAAAV+6zd9agQ7O594/6w6ZKY+fXGhRJV4nsoLvrdTm9mG5hWISmIx oe9u4jKIGNLnInlwXZKIk3kXrgm1wGAUO8eNn4XRiyXi3ZQjIGcxv/TpSzVFe7JB41LGI7A+bKa ALeIlBPacMWbVyL6PpzyHBr3STQ8//3Va5wedzo+WYEgwzPKwqtnggQq3BXQyyh283Q== X-Received: by 2002:a65:5ac3:: with SMTP id d3mr31047792pgt.168.1554753576666; Mon, 08 Apr 2019 12:59:36 -0700 (PDT) X-Received: by 2002:a65:5ac3:: with SMTP id d3mr31047751pgt.168.1554753575947; Mon, 08 Apr 2019 12:59:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554753575; cv=none; d=google.com; s=arc-20160816; b=0zd7AI80mBVirqJmg4j6dfvmqAAB9/hxji/hvp5nF963UJuzRKprw6zx+tuGkiFBE5 FS72iwXWi+Jamo874UuMQOSRKn5pZpnr/t4ORwkb7uxfVkwBToEF+M4fA5Pi2ePDkiM6 Gv4iDN9EvyYkvfwrDsu77rFwHYxsTDPLMErouVzGqDEhmPz9aNOWH5GwzLleWjA1oTTB nf6893jictrH8aFOKTHRbiY9ypmeDO3iQsINCb7cnuzDPC8+qBQl2cJ1l12QsHm3iYtE nsD1c4yXUezs+7LMvit8AWVFDML1Hrkf7RJl6IFqvdDLWh1NqFuooZ+ni/UjdjXmTyId 1Vcg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:user-agent:references:message-id:in-reply-to:subject :cc:to:from:date:dkim-signature; bh=RSStTgCMJwvCqECwFlnDBhxtF8I569p9e6PuxiQP1oM=; b=ko0fPHd8zVftg/LTbnpaOXXaaZgf/mx5a4U2Jvz+l9CTOak0mLzcxSrP8MSc7Kdnva 4qX4sxjcyC1H+f99TFtb12yVrEyNw3dTU6RFQFMFaUtgld/0HjiTzZfYcVVKtG+oCK6I /9ihdw020vqa8ldZ1diqyQdRDQPtT33Q7yi+vxID5iTrJsVGzQZPrtPWlLysWqjwZ/F7 jsjSIo8tPeX4NTBj/lhgZz81dmGDpGLyGhtC7+QGcf4OOpcwdeTbWzpX+7J62OsRtdCV xugM6eJ8LC1BiJBcpOgnNteVWo27dc9Ra3KEnLn/y2TdlpKjHJ2PcQ5g2n5p8q2ey0sD G23Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="IaVs/v49"; spf=pass (google.com: domain of hughd@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=hughd@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id a73sor12823628pge.24.2019.04.08.12.59.35 for (Google Transport Security); Mon, 08 Apr 2019 12:59:35 -0700 (PDT) Received-SPF: pass (google.com: domain of hughd@google.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="IaVs/v49"; spf=pass (google.com: domain of hughd@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=hughd@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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=RSStTgCMJwvCqECwFlnDBhxtF8I569p9e6PuxiQP1oM=; b=IaVs/v49W9F4RB08+llfR3LzCZzsnZdb9KI01bTePyyBQrFAkbpQgsu5FDJDr4nZ/m YA6tTLHgZuvBGyWo/ywVHmrLflS/IVvDlnqE04G9JZQ6H5X7Xn+Co+j/3YHc5dB4U07s hbx3CzJxJZajypW7gQfNirQWke+RqbuAZFyNwtCXf8XQqi4eOU+ydwDNX5yNZ0pO+7N5 GmmBYlJvxnFG59cHiMFKUAf6KwMbkN173hB3GNxcqSoZtnqFuDaGFZTSblrnWbNsLT/W kMw1dwylomWuocQ2hW4ZhH1yohtb86UkZvXeLQtvEK96NHD+YFp2Wh06ZkCcU3MSMPt2 H3pQ== X-Google-Smtp-Source: APXvYqzfmpdTWEuDsxZC2Dtn2LoyYq29oaMJey0FNJtIvQ4jPQhwzXYeWniqDVNGM3maON9MarpjjA== X-Received: by 2002:a65:5383:: with SMTP id x3mr12785290pgq.60.1554753574808; Mon, 08 Apr 2019 12:59:34 -0700 (PDT) Received: from [100.112.89.103] ([104.133.8.103]) by smtp.gmail.com with ESMTPSA id j16sm41059643pfi.58.2019.04.08.12.59.33 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 08 Apr 2019 12:59:33 -0700 (PDT) Date: Mon, 8 Apr 2019 12:59:32 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Andrew Morton cc: Konstantin Khlebnikov , "Alex Xu (Hello71)" , Vineeth Pillai , Kelley Nielsen , Rik van Riel , Huang Ying , Hugh Dickins , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 3/4] mm: swapoff: take notice of completion sooner In-Reply-To: Message-ID: References: User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP The old try_to_unuse() implementation was driven by find_next_to_unuse(), which terminated as soon as all the swap had been freed. Add inuse_pages checks now (alongside signal_pending()) to stop scanning mms and swap_map once finished. The same ought to be done in shmem_unuse() too, but never was before, and needs a different interface: so leave it as is for now. Fixes: b56a2d8af914 ("mm: rid swapoff of quadratic complexity") Signed-off-by: Hugh Dickins --- mm/swapfile.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) --- 5.1-rc4/mm/swapfile.c 2019-04-07 19:15:01.269054187 -0700 +++ linux/mm/swapfile.c 2019-04-07 19:17:13.291957539 -0700 @@ -2051,11 +2051,9 @@ retry: spin_lock(&mmlist_lock); p = &init_mm.mmlist; - while ((p = p->next) != &init_mm.mmlist) { - if (signal_pending(current)) { - retval = -EINTR; - break; - } + while (si->inuse_pages && + !signal_pending(current) && + (p = p->next) != &init_mm.mmlist) { mm = list_entry(p, struct mm_struct, mmlist); if (!mmget_not_zero(mm)) @@ -2082,7 +2080,9 @@ retry: mmput(prev_mm); i = 0; - while ((i = find_next_to_unuse(si, i, frontswap)) != 0) { + while (si->inuse_pages && + !signal_pending(current) && + (i = find_next_to_unuse(si, i, frontswap)) != 0) { entry = swp_entry(type, i); page = find_get_page(swap_address_space(entry), i); @@ -2123,8 +2123,11 @@ retry: * separate lists, and wait for those lists to be emptied; but it's * easier and more robust (though cpu-intensive) just to keep retrying. */ - if (si->inuse_pages) - goto retry; + if (si->inuse_pages) { + if (!signal_pending(current)) + goto retry; + retval = -EINTR; + } out: return (retval == FRONTSWAP_PAGES_UNUSED) ? 0 : retval; } From patchwork Mon Apr 8 20:01:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 10890069 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0EEEF1669 for ; Mon, 8 Apr 2019 20:01:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ECC8928737 for ; Mon, 8 Apr 2019 20:01:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E0A0028749; Mon, 8 Apr 2019 20:01:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 27E7328737 for ; Mon, 8 Apr 2019 20:01:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4DEB36B0007; Mon, 8 Apr 2019 16:01:06 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4B44C6B0008; Mon, 8 Apr 2019 16:01:06 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 357236B000A; Mon, 8 Apr 2019 16:01:06 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) by kanga.kvack.org (Postfix) with ESMTP id EBF6D6B0007 for ; Mon, 8 Apr 2019 16:01:05 -0400 (EDT) Received: by mail-pg1-f199.google.com with SMTP id j184so10885845pgd.7 for ; Mon, 08 Apr 2019 13:01:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:date:from:to:cc:subject :in-reply-to:message-id:references:user-agent:mime-version; bh=xVh0abGIk/CaXpks5JMNE1h7dPbyclFz8hd/2iNFqZI=; b=KZreyiYSY+MQ/4uOXNuZUefTcTC61r70rm+Shc9706sT0JTyRmLOt1/OCY4Cwhq/gV 6RHgeRTLSncO7isPcX1cfPz5OfZ+n478sFh4Qah93WaNr6JYLguWQ4Cat8VodyTwcLZy SnZiVuk1yBNBMMcqyiEfNWKicaSw2oG+0+SoeGJXoMhiL0/HasQ2B14pRWibamfatp9G oZ2V6HkARChw+3xdbfIHSGjwPQKc56vdRIYJvsQsASfnMswM8qOQGiyI3+C1Ou98s6fB gRJPNeyP3XXRVmGH0cHYcEZBZ5tGt3ySL+k4gRSBgG50E+nLcG1pHJsyINL5eJfzlPdE 82cg== X-Gm-Message-State: APjAAAWM5cR+Ibud1KpsRWMndN7JGr/EjzFjTLylhPWO+LueC8RrPJV1 THcbvhahgg1rnqh/GvvTn0c49nwGWREUBt18gcFjKp2ICS/jZ80+uwBW183Q7H+qkssM47x25h0 RQN0OJjf43PmvBS0yUb4Q903NbJyWvGe9wICdSTzMkACS0P+nMalCgjLc9/ReUE175g== X-Received: by 2002:a65:5003:: with SMTP id f3mr30541517pgo.29.1554753665462; Mon, 08 Apr 2019 13:01:05 -0700 (PDT) X-Received: by 2002:a65:5003:: with SMTP id f3mr30541416pgo.29.1554753664414; Mon, 08 Apr 2019 13:01:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554753664; cv=none; d=google.com; s=arc-20160816; b=OsQmqku5hddqnK/bDcJr3o4Q2GbkPFVI3E8+6pDvH8S4QJoeVgZBaU7oShFwtp9VoX 6WEvBSPap7w1vgVz05Jw/0U/h8/1SoWwcmKp759EOfzCAsjUxqngcp4qf3a6gLJKMXT+ 0DJR6+xhSasnsWGjK4gLlFn+GfF8UBlVp1aJesnhpgh7dAdmwrBJfAjLMeNoAx/Hfvxe 7oI6EMMuJIDxiVKMBrHBaxL2tFkUafbxpW7Q7pNQ0wxlE6yK615vw5lBatYlrf38fNyU H0yhDwQm3XFXSHNzfbUpLpI6G/yqG49ExKhKFCTtJrZoJFc1l4QMCr3SUpmtCrRUEe8+ bKjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:user-agent:references:message-id:in-reply-to:subject :cc:to:from:date:dkim-signature; bh=xVh0abGIk/CaXpks5JMNE1h7dPbyclFz8hd/2iNFqZI=; b=TRc0Kx3Yl0CCNsS9Ahi9YWB1QX1DMo+42oLDze5XJ+LTd8lPfsuyp9LXubwYJVJOrh 9UTqkIPU5fGeozDVyP8zZdWTcF/QrdHfZO9aW6AHYmqMPRirZQXFtG8Aihpz3uQeTQZS +p0W31L4AllWC6+QQRJyaTjs88eNlVU71hfxN76edmX3smMXejp06T9Z/OUPlWSLiUNy pFQVcbfKbO0NbivyLMqG9qg1iEy35kGnXFqFpg9buwk/wd9tLLANYV40KOPeq/c7Gglt zbLZpeEZzJxXINBNShyv9rGc5Y0VpW2Z+5Awv8fHwIWFTjwESti+Ny41CILPEZsZRXi0 0jwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=DqSizuz1; spf=pass (google.com: domain of hughd@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=hughd@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id f24sor30912333pfn.22.2019.04.08.13.01.04 for (Google Transport Security); Mon, 08 Apr 2019 13:01:04 -0700 (PDT) Received-SPF: pass (google.com: domain of hughd@google.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=DqSizuz1; spf=pass (google.com: domain of hughd@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=hughd@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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=xVh0abGIk/CaXpks5JMNE1h7dPbyclFz8hd/2iNFqZI=; b=DqSizuz1cjGbUl2f26QC9KngTLatAZfM/se0JI0BjNqjgur9mq4Nx84E9ZkjbzdxN/ 4O94IL3JOzNAf7rfNuN53wPw9laVx2mpMMCE3PMHQse8USTHUaZeGSaLAlJDyQZ2fwMO 6FuFauc5u7Cp56G7GcAj4ADlAxEz6E3DFH9oAHp2A0AvCZW6Iso9VaudXWY4aLeH3Z9F Sy4jm6Lny8DzfBp236OLgXcWw7+e1mNYbkot5LktWIt5/bFsoDs46XVvXQbefHsdqB93 maFy6MEpS3tfBXsjM+H4L1nLVGJo0CXuPfxhWe+i1vy9Pk6R2Y8TqmW9y052dRAlS9pO VgWw== X-Google-Smtp-Source: APXvYqzu7cQVsvXaE3SZpOUeFoUHm3+r3PKIuTBGROvfIPGhokfKI5/K2klTZVQ/lfq32h9BYMURgw== X-Received: by 2002:a62:2046:: with SMTP id g67mr31481444pfg.121.1554753663028; Mon, 08 Apr 2019 13:01:03 -0700 (PDT) Received: from [100.112.89.103] ([104.133.8.103]) by smtp.gmail.com with ESMTPSA id d187sm35843164pgc.43.2019.04.08.13.01.01 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 08 Apr 2019 13:01:01 -0700 (PDT) Date: Mon, 8 Apr 2019 13:01:00 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Andrew Morton cc: Konstantin Khlebnikov , "Alex Xu (Hello71)" , Vineeth Pillai , Kelley Nielsen , Rik van Riel , Huang Ying , Hugh Dickins , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 4/4] mm: swapoff: shmem_unuse() stop eviction without igrab() In-Reply-To: Message-ID: References: User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP The igrab() in shmem_unuse() looks good, but we forgot that it gives no protection against concurrent unmounting: a point made by Konstantin Khlebnikov eight years ago, and then fixed in 2.6.39 by 778dd893ae78 ("tmpfs: fix race between umount and swapoff"). The current 5.1-rc swapoff is liable to hit "VFS: Busy inodes after unmount of tmpfs. Self-destruct in 5 seconds. Have a nice day..." followed by GPF. Once again, give up on using igrab(); but don't go back to making such heavy-handed use of shmem_swaplist_mutex as last time: that would spoil the new design, and I expect could deadlock inside shmem_swapin_page(). Instead, shmem_unuse() just raise a "stop_eviction" count in the shmem- specific inode, and shmem_evict_inode() wait for that to go down to 0. Call it "stop_eviction" rather than "swapoff_busy" because it can be put to use for others later (huge tmpfs patches expect to use it). That simplifies shmem_unuse(), protecting it from both unlink and unmount; and in practice lets it locate all the swap in its first try. But do not rely on that: there's still a theoretical case, when shmem_writepage() might have been preempted after its get_swap_page(), before making the swap entry visible to swapoff. Fixes: b56a2d8af914 ("mm: rid swapoff of quadratic complexity") Signed-off-by: Hugh Dickins Signed-off-by: Hugh Dickins --- include/linux/shmem_fs.h | 1 + mm/shmem.c | 39 ++++++++++++++++++--------------------- mm/swapfile.c | 11 +++++------ 3 files changed, 24 insertions(+), 27 deletions(-) --- 5.1-rc4/include/linux/shmem_fs.h 2019-03-17 16:18:15.181820820 -0700 +++ linux/include/linux/shmem_fs.h 2019-04-07 19:18:43.248639711 -0700 @@ -21,6 +21,7 @@ struct shmem_inode_info { struct list_head swaplist; /* chain of maybes on swap */ struct shared_policy policy; /* NUMA memory alloc policy */ struct simple_xattrs xattrs; /* list of xattrs */ + atomic_t stop_eviction; /* hold when working on inode */ struct inode vfs_inode; }; --- 5.1-rc4/mm/shmem.c 2019-04-07 19:12:23.603858531 -0700 +++ linux/mm/shmem.c 2019-04-07 19:18:43.248639711 -0700 @@ -1081,9 +1081,15 @@ static void shmem_evict_inode(struct ino } spin_unlock(&sbinfo->shrinklist_lock); } - if (!list_empty(&info->swaplist)) { + while (!list_empty(&info->swaplist)) { + /* Wait while shmem_unuse() is scanning this inode... */ + wait_var_event(&info->stop_eviction, + !atomic_read(&info->stop_eviction)); mutex_lock(&shmem_swaplist_mutex); list_del_init(&info->swaplist); + /* ...but beware of the race if we peeked too early */ + if (!atomic_read(&info->stop_eviction)) + list_del_init(&info->swaplist); mutex_unlock(&shmem_swaplist_mutex); } } @@ -1227,36 +1233,27 @@ int shmem_unuse(unsigned int type, bool unsigned long *fs_pages_to_unuse) { struct shmem_inode_info *info, *next; - struct inode *inode; - struct inode *prev_inode = NULL; int error = 0; if (list_empty(&shmem_swaplist)) return 0; mutex_lock(&shmem_swaplist_mutex); - - /* - * The extra refcount on the inode is necessary to safely dereference - * p->next after re-acquiring the lock. New shmem inodes with swap - * get added to the end of the list and we will scan them all. - */ list_for_each_entry_safe(info, next, &shmem_swaplist, swaplist) { if (!info->swapped) { list_del_init(&info->swaplist); continue; } - - inode = igrab(&info->vfs_inode); - if (!inode) - continue; - + /* + * Drop the swaplist mutex while searching the inode for swap; + * but before doing so, make sure shmem_evict_inode() will not + * remove placeholder inode from swaplist, nor let it be freed + * (igrab() would protect from unlink, but not from unmount). + */ + atomic_inc(&info->stop_eviction); mutex_unlock(&shmem_swaplist_mutex); - if (prev_inode) - iput(prev_inode); - prev_inode = inode; - error = shmem_unuse_inode(inode, type, frontswap, + error = shmem_unuse_inode(&info->vfs_inode, type, frontswap, fs_pages_to_unuse); cond_resched(); @@ -1264,14 +1261,13 @@ int shmem_unuse(unsigned int type, bool next = list_next_entry(info, swaplist); if (!info->swapped) list_del_init(&info->swaplist); + if (atomic_dec_and_test(&info->stop_eviction)) + wake_up_var(&info->stop_eviction); if (error) break; } mutex_unlock(&shmem_swaplist_mutex); - if (prev_inode) - iput(prev_inode); - return error; } @@ -2238,6 +2234,7 @@ static struct inode *shmem_get_inode(str info = SHMEM_I(inode); memset(info, 0, (char *)inode - (char *)info); spin_lock_init(&info->lock); + atomic_set(&info->stop_eviction, 0); info->seals = F_SEAL_SEAL; info->flags = flags & VM_NORESERVE; INIT_LIST_HEAD(&info->shrinklist); --- 5.1-rc4/mm/swapfile.c 2019-04-07 19:17:13.291957539 -0700 +++ linux/mm/swapfile.c 2019-04-07 19:18:43.248639711 -0700 @@ -2116,12 +2116,11 @@ retry: * Under global memory pressure, swap entries can be reinserted back * into process space after the mmlist loop above passes over them. * - * Limit the number of retries? No: when shmem_unuse()'s igrab() fails, - * a shmem inode using swap is being evicted; and when mmget_not_zero() - * above fails, that mm is likely to be freeing swap from exit_mmap(). - * Both proceed at their own independent pace: we could move them to - * separate lists, and wait for those lists to be emptied; but it's - * easier and more robust (though cpu-intensive) just to keep retrying. + * Limit the number of retries? No: when mmget_not_zero() above fails, + * that mm is likely to be freeing swap from exit_mmap(), which proceeds + * at its own independent pace; and even shmem_writepage() could have + * been preempted after get_swap_page(), temporarily hiding that swap. + * It's easy and robust (though cpu-intensive) just to keep retrying. */ if (si->inuse_pages) { if (!signal_pending(current))