Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3689230ybv; Mon, 10 Feb 2020 04:45:26 -0800 (PST) X-Google-Smtp-Source: APXvYqzbFU+b2kBIGR6Z6nSK4QCFbjitZT49y4FGg6R/omc3UxIjuLOZwDV4egL5qq9USmZLOmIa X-Received: by 2002:a54:4003:: with SMTP id x3mr682311oie.0.1581338726189; Mon, 10 Feb 2020 04:45:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581338726; cv=none; d=google.com; s=arc-20160816; b=IH+iGaNKAxi2OYRzWD7XeTVz1y/0Vh7ZpNdAnFyu7SpJqExn0trsoWaSckUsxD+DKq /n0YtUcwfKNfQ9tN0LiR0lnW4r7wcs3gLvmynZRmjdsIQHud5E5FXcBm2qiSIAR6pN/s QQrC5PAJG85bdQvaTBr/wiNpNftah52zkdLJgHTk7huyF7qzLa+WfEJ4R5wi53X1mtAa H2F/2rqgXsZRCrKRFRJH0cu/oWjRVXUSiYrq+p25xQHaJb7udZgjJ3C/XQ+/dc4kWOED WOSUyiKb448WCJtmMd4fYg05oRAIAX5CYTgsbfFEZBdqz1l+JOfHThVFneh0bUEkxW09 KBew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=cwdevDusWEixY5zGX4pI2CNdJ9ZK+oMf51d48xQCuDg=; b=jq85sIUeg7S311kzuVHrny1nU8Hti7Luu0hs62w/J8PdoRkSW5F2sTVjDUKyAENkhc CsaqucaEm/JJs6TOh6dwma+gvXC99I0447VDf5T7TvW8mUTweHry+Kx3ncxMVOFFll6/ CWe8BBdNivQAFmF73GrInhl6/UqgbpnW+5ob1Ayj3BNCy5jE9v/YNhRZaHVA5bv3/SAz +Jy9Xmgkczl29p7pYFCyL0cfMV174EB49Y96OSiTkomgQ+aB0GuYez43zBJBcU6aMeZ9 zcBaVkESmPR0rpST4bbXxI2izfbX7cQXkzkyiux1SsqAfssh8hsmZCl54N7FGujJtsP9 Vv9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=trpqj4iG; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 19si120560oiq.128.2020.02.10.04.45.14; Mon, 10 Feb 2020 04:45:26 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=trpqj4iG; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730750AbgBJMpA (ORCPT + 99 others); Mon, 10 Feb 2020 07:45:00 -0500 Received: from mail.kernel.org ([198.145.29.99]:41618 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729148AbgBJMks (ORCPT ); Mon, 10 Feb 2020 07:40:48 -0500 Received: from localhost (unknown [209.37.97.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id ABEE120842; Mon, 10 Feb 2020 12:40:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581338447; bh=gZjRPmNca0NHA9D6FXoRdzdcoMOP0o5FB1DbHMFVvHY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=trpqj4iGvxexztGIVGUiYjp5nGdXw29aTFBO4tKp4SiKP0VrlccN9fta9dj3nVn/O Z0xY1pI5+U5QJgu+sbpYPqEo2l6fCGtMv3zrDXE3mBNua3H5ELguHCVe7fnYS6Bw1q i5/Uutcbk65ptpsSgajNQEmsvsTUz+Eu/8bLPvXo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Trond Myklebust , Benjamin Coddington , Anna Schumaker Subject: [PATCH 5.5 190/367] NFS: Directory page cache pages need to be locked when read Date: Mon, 10 Feb 2020 04:31:43 -0800 Message-Id: <20200210122442.153060068@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200210122423.695146547@linuxfoundation.org> References: <20200210122423.695146547@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Trond Myklebust commit 114de38225d9b300f027e2aec9afbb6e0def154b upstream. When a NFS directory page cache page is removed from the page cache, its contents are freed through a call to nfs_readdir_clear_array(). To prevent the removal of the page cache entry until after we've finished reading it, we must take the page lock. Fixes: 11de3b11e08c ("NFS: Fix a memory leak in nfs_readdir") Cc: stable@vger.kernel.org # v2.6.37+ Signed-off-by: Trond Myklebust Reviewed-by: Benjamin Coddington Signed-off-by: Anna Schumaker Signed-off-by: Greg Kroah-Hartman --- fs/nfs/dir.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) --- a/fs/nfs/dir.c +++ b/fs/nfs/dir.c @@ -702,8 +702,6 @@ int nfs_readdir_filler(void *data, struc static void cache_page_release(nfs_readdir_descriptor_t *desc) { - if (!desc->page->mapping) - nfs_readdir_clear_array(desc->page); put_page(desc->page); desc->page = NULL; } @@ -717,19 +715,28 @@ struct page *get_cache_page(nfs_readdir_ /* * Returns 0 if desc->dir_cookie was found on page desc->page_index + * and locks the page to prevent removal from the page cache. */ static -int find_cache_page(nfs_readdir_descriptor_t *desc) +int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc) { int res; desc->page = get_cache_page(desc); if (IS_ERR(desc->page)) return PTR_ERR(desc->page); - - res = nfs_readdir_search_array(desc); + res = lock_page_killable(desc->page); if (res != 0) - cache_page_release(desc); + goto error; + res = -EAGAIN; + if (desc->page->mapping != NULL) { + res = nfs_readdir_search_array(desc); + if (res == 0) + return 0; + } + unlock_page(desc->page); +error: + cache_page_release(desc); return res; } @@ -744,7 +751,7 @@ int readdir_search_pagecache(nfs_readdir desc->last_cookie = 0; } do { - res = find_cache_page(desc); + res = find_and_lock_cache_page(desc); } while (res == -EAGAIN); return res; } @@ -783,7 +790,6 @@ int nfs_do_filldir(nfs_readdir_descripto desc->eof = true; kunmap(desc->page); - cache_page_release(desc); dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", (unsigned long long)*desc->dir_cookie, res); return res; @@ -829,13 +835,13 @@ int uncached_readdir(nfs_readdir_descrip status = nfs_do_filldir(desc); + out_release: + nfs_readdir_clear_array(desc->page); + cache_page_release(desc); out: dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status); return status; - out_release: - cache_page_release(desc); - goto out; } /* The file offset position represents the dirent entry number. A @@ -900,6 +906,8 @@ static int nfs_readdir(struct file *file break; res = nfs_do_filldir(desc); + unlock_page(desc->page); + cache_page_release(desc); if (res < 0) break; } while (!desc->eof);