From patchwork Sat Sep 9 17:32:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Trond Myklebust X-Patchwork-Id: 9945391 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D5985602C9 for ; Sat, 9 Sep 2017 17:32:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BACE727F97 for ; Sat, 9 Sep 2017 17:32:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ACCB7286D5; Sat, 9 Sep 2017 17:32:32 +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=-6.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0B0B227F97 for ; Sat, 9 Sep 2017 17:32:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751261AbdIIRcX (ORCPT ); Sat, 9 Sep 2017 13:32:23 -0400 Received: from mail-io0-f194.google.com ([209.85.223.194]:37853 "EHLO mail-io0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750939AbdIIRcW (ORCPT ); Sat, 9 Sep 2017 13:32:22 -0400 Received: by mail-io0-f194.google.com with SMTP id 93so608187iol.4 for ; Sat, 09 Sep 2017 10:32:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=YJiOH3yuMTTtJNCx3dItgFyusaCg+yNs+GWHFNWO9bs=; b=FCDRFDajqu+k7vPYlhMrnLQNEMjf1vjzMnXP/wL8vIatYUofWHvu9dryNAwMfxd9Qe Is/O4jmTDqai+NobiJqjTg4J0miKCQdLJpz3ESBidE1K2Myncg7EaZgwovndqoMqgErD h2/gZE7aGOm3Fm/aH/OSv5C5ohgAKwK7D0oYYjcHx4hIQVfB+O/qPO0KQ/O8Pp/rndr7 hfjQOvK6n3B2Lr2gCxJdILKZYZEAifn+hJJvo4FTBAk+KFx8nLf73vecXOoNF97hJmOh KjHYNVGJnXw0ZJiitpshhlsjYbEqVeSZOxVLzy9BSaswlV+WtNIaX221cuA6sru4tNZG 44KA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:subject:date:message-id :mime-version:content-transfer-encoding; bh=YJiOH3yuMTTtJNCx3dItgFyusaCg+yNs+GWHFNWO9bs=; b=W/oMJwHWokyqKqt79dRkekT8jdX8zsvxqZNxjjpjjHis0QPKEA8pksbATsLFestCSc ro2okY5ExY2VnbzCkpHcHGRXLbdsnnpIq8TgZSo4KdcbpoxXoaG7q/VmUraNhYZ5opoJ /xc4aNXViTwMSEJE0XYxwxlbUYocBq6Xw75+LlTSMXJLKPdRxpob9ieZp7l7rjEhumQ1 lbVHYHSfBew5Bf0uUR/4LZFTtyODXF99d2s1EHKHiSni334/wjRHpmIxR2i04BzUG4JZ Bg/aic+gxcL4pOW9Bd8wkEjsgzH44QGyOfWtLNfmYFGE7Vpz1D5XhIzlnlw2V2JuUJJS FX2A== X-Gm-Message-State: AHPjjUi3IULBVkn56WU+VS1XQhImZkcu0B51qcbu2SnfyPtIjCRmxt31 AcrT6TIRk7Lae4JWnVg= X-Google-Smtp-Source: AOwi7QCQdf/4OVAPEsZkhK/0/pJLRxkhwd/ycsoZjta7qDg3MiOUGVU9jtq6rQsdxJHeIrG4fY/3/A== X-Received: by 10.107.47.17 with SMTP id j17mr8592722ioo.16.1504978341377; Sat, 09 Sep 2017 10:32:21 -0700 (PDT) Received: from localhost.localdomain (c-68-49-162-121.hsd1.mi.comcast.net. [68.49.162.121]) by smtp.gmail.com with ESMTPSA id y3sm2381369ioy.15.2017.09.09.10.32.19 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 09 Sep 2017 10:32:20 -0700 (PDT) From: Trond Myklebust To: linux-nfs@vger.kernel.org Subject: [PATCH 1/2] NFS: nfs_lock_and_join_requests and nfs_scan_commit_list can deadlock Date: Sat, 9 Sep 2017 13:32:16 -0400 Message-Id: <20170909173217.118251-1-trond.myklebust@primarydata.com> X-Mailer: git-send-email 2.13.5 MIME-Version: 1.0 Sender: linux-nfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Since the commit list is not ordered, it is possible for nfs_scan_commit_list to hold a request that nfs_lock_and_join_requests() is waiting for, while at the same time trying to grab a request that nfs_lock_and_join_requests already holds. Signed-off-by: Trond Myklebust --- fs/nfs/pnfs_nfs.c | 16 +++++++++++----- fs/nfs/write.c | 15 +++++++++++---- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/fs/nfs/pnfs_nfs.c b/fs/nfs/pnfs_nfs.c index 303ff171cb5d..d03d836b6ee0 100644 --- a/fs/nfs/pnfs_nfs.c +++ b/fs/nfs/pnfs_nfs.c @@ -91,22 +91,28 @@ static int pnfs_generic_transfer_commit_list(struct list_head *src, struct list_head *dst, struct nfs_commit_info *cinfo, int max) { - struct nfs_page *req; + struct nfs_page *req, *tmp; int ret = 0; - while(!list_empty(src)) { - req = list_first_entry(src, struct nfs_page, wb_list); - +restart: + list_for_each_entry_safe(req, tmp, src, wb_list) { kref_get(&req->wb_kref); if (!nfs_lock_request(req)) { int status; + + /* Prevent deadlock with nfs_lock_and_join_requests */ + if (!list_empty(dst)) { + nfs_release_request(req); + continue; + } + /* Ensure we make progress to prevent livelock */ mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); status = nfs_wait_on_request(req); nfs_release_request(req); mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); if (status < 0) break; - continue; + goto restart; } nfs_request_remove_commit_list(req, cinfo); clear_bit(PG_COMMIT_TO_DS, &req->wb_flags); diff --git a/fs/nfs/write.c b/fs/nfs/write.c index ae26775b5448..c3f627b08ec6 100644 --- a/fs/nfs/write.c +++ b/fs/nfs/write.c @@ -1028,21 +1028,28 @@ int nfs_scan_commit_list(struct list_head *src, struct list_head *dst, struct nfs_commit_info *cinfo, int max) { - struct nfs_page *req; + struct nfs_page *req, *tmp; int ret = 0; - while(!list_empty(src)) { - req = list_first_entry(src, struct nfs_page, wb_list); +restart: + list_for_each_entry_safe(req, tmp, src, wb_list) { kref_get(&req->wb_kref); if (!nfs_lock_request(req)) { int status; + + /* Prevent deadlock with nfs_lock_and_join_requests */ + if (!list_empty(dst)) { + nfs_release_request(req); + continue; + } + /* Ensure we make progress to prevent livelock */ mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); status = nfs_wait_on_request(req); nfs_release_request(req); mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); if (status < 0) break; - continue; + goto restart; } nfs_request_remove_commit_list(req, cinfo); nfs_list_add_request(req, dst);