From patchwork Thu Oct 31 10:39:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Monakhov X-Patchwork-Id: 11220879 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 07F0715AB for ; Thu, 31 Oct 2019 10:39:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D02EE20873 for ; Thu, 31 Oct 2019 10:39:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=yandex-team.ru header.i=@yandex-team.ru header.b="kDteKn5X" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727240AbfJaKj3 (ORCPT ); Thu, 31 Oct 2019 06:39:29 -0400 Received: from forwardcorp1j.mail.yandex.net ([5.45.199.163]:39044 "EHLO forwardcorp1j.mail.yandex.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726867AbfJaKj3 (ORCPT ); Thu, 31 Oct 2019 06:39:29 -0400 Received: from mxbackcorp1j.mail.yandex.net (mxbackcorp1j.mail.yandex.net [IPv6:2a02:6b8:0:1619::162]) by forwardcorp1j.mail.yandex.net (Yandex) with ESMTP id 767022E0DC6; Thu, 31 Oct 2019 13:39:26 +0300 (MSK) Received: from sas2-62907d92d1d8.qloud-c.yandex.net (sas2-62907d92d1d8.qloud-c.yandex.net [2a02:6b8:c08:b895:0:640:6290:7d92]) by mxbackcorp1j.mail.yandex.net (nwsmtp/Yandex) with ESMTP id 55UrcCtsWr-dPiqKoIV; Thu, 31 Oct 2019 13:39:26 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1572518366; bh=g3jMoOgCIV+oJdbBwMXSmMefvpQfsP/udePjE7IgkA0=; h=Message-Id:Date:Subject:To:From:Cc; b=kDteKn5X2Jv9cxfWOVFnCYgvL8M2nBvClu1WnYrbyCxh7exoJKQla+hEdUVbPGJ5M b8rJ57LHsL4v18r0+kw7G2gWo4setPYN36f9YGWFrE5QyDQYXMg/gSUbihFLkVLtWx FT4A9SZxBKBE0NaSQvbmi5C9WEgF0kYhuKsbIGjM= Authentication-Results: mxbackcorp1j.mail.yandex.net; dkim=pass header.i=@yandex-team.ru Received: from 95.108.174.193-red.dhcp.yndx.net (95.108.174.193-red.dhcp.yndx.net [95.108.174.193]) by sas2-62907d92d1d8.qloud-c.yandex.net (nwsmtp/Yandex) with ESMTPSA id ab6iIZdbUM-dPVKTtPu; Thu, 31 Oct 2019 13:39:25 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client certificate not present) From: Dmitry Monakhov To: linux-fsdevel@vger.kernel.org Cc: linux-ext4@vger.kernel.org, jack@suse.cz, Dmitry Monakhov , Konstantin Khlebnikov Subject: [PATCH 1/2] fs/quota: fix livelock in dquot_writeback_dquots Date: Thu, 31 Oct 2019 10:39:19 +0000 Message-Id: <20191031103920.3919-1-dmonakhov@openvz.org> X-Mailer: git-send-email 2.18.0 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org From: Dmitry Monakhov Write only quotas which are dirty at entry. XFSTEST: https://github.com/dmonakhov/xfstests/commit/b10ad23566a5bf75832a6f500e1236084083cddc Signed-off-by: Konstantin Khlebnikov Signed-off-by: Dmitry Monakhov --- fs/quota/dquot.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/fs/quota/dquot.c b/fs/quota/dquot.c index 26812a6..b492b9e 100644 --- a/fs/quota/dquot.c +++ b/fs/quota/dquot.c @@ -622,7 +622,7 @@ EXPORT_SYMBOL(dquot_scan_active); /* Write all dquot structures to quota files */ int dquot_writeback_dquots(struct super_block *sb, int type) { - struct list_head *dirty; + struct list_head dirty; struct dquot *dquot; struct quota_info *dqopt = sb_dqopt(sb); int cnt; @@ -636,9 +636,10 @@ int dquot_writeback_dquots(struct super_block *sb, int type) if (!sb_has_quota_active(sb, cnt)) continue; spin_lock(&dq_list_lock); - dirty = &dqopt->info[cnt].dqi_dirty_list; - while (!list_empty(dirty)) { - dquot = list_first_entry(dirty, struct dquot, + /* Move list away to avoid livelock. */ + list_replace_init(&dqopt->info[cnt].dqi_dirty_list, &dirty); + while (!list_empty(&dirty)) { + dquot = list_first_entry(&dirty, struct dquot, dq_dirty); WARN_ON(!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)); From patchwork Thu Oct 31 10:39:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Monakhov X-Patchwork-Id: 11220881 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9F8971668 for ; Thu, 31 Oct 2019 10:39:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7EA43208C0 for ; Thu, 31 Oct 2019 10:39:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=yandex-team.ru header.i=@yandex-team.ru header.b="FxquD+5R" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727260AbfJaKja (ORCPT ); Thu, 31 Oct 2019 06:39:30 -0400 Received: from forwardcorp1j.mail.yandex.net ([5.45.199.163]:39086 "EHLO forwardcorp1j.mail.yandex.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726913AbfJaKja (ORCPT ); Thu, 31 Oct 2019 06:39:30 -0400 Received: from mxbackcorp1o.mail.yandex.net (mxbackcorp1o.mail.yandex.net [IPv6:2a02:6b8:0:1a2d::301]) by forwardcorp1j.mail.yandex.net (Yandex) with ESMTP id 4313A2E0DD2; Thu, 31 Oct 2019 13:39:27 +0300 (MSK) Received: from sas1-7fab0cd91cd2.qloud-c.yandex.net (sas1-7fab0cd91cd2.qloud-c.yandex.net [2a02:6b8:c14:3a93:0:640:7fab:cd9]) by mxbackcorp1o.mail.yandex.net (nwsmtp/Yandex) with ESMTP id 3cec8TshNm-dQei0e3i; Thu, 31 Oct 2019 13:39:27 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1572518367; bh=L9UYXWAgwZMaVyLOOzOt+ACtOFd1s/GUXWoDpY54l7s=; h=In-Reply-To:Message-Id:References:Date:Subject:To:From:Cc; b=FxquD+5RhfjDSq5dXzcScL6eS8oKzAE+Mo/0o2dZ9OXaVEfQGdcX/1jB1JrGhq5hl /+lBPkWy3ZflQRUXM7J2rdjsEu7Yk4yGSO1IdKv3t967ErW6zbP+CQwANLqHNWyOSN bMUAqV7cFsqHLblJgGV+x/H3tdXwDp4OyduA4OEw= Authentication-Results: mxbackcorp1o.mail.yandex.net; dkim=pass header.i=@yandex-team.ru Received: from 95.108.174.193-red.dhcp.yndx.net (95.108.174.193-red.dhcp.yndx.net [95.108.174.193]) by sas1-7fab0cd91cd2.qloud-c.yandex.net (nwsmtp/Yandex) with ESMTPSA id N03nYWr42k-dQWKoRGl; Thu, 31 Oct 2019 13:39:26 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client certificate not present) From: Dmitry Monakhov To: linux-fsdevel@vger.kernel.org Cc: linux-ext4@vger.kernel.org, jack@suse.cz, Dmitry Monakhov Subject: [PATCH 2/2] fs/quota: Check that quota is not dirty before release Date: Thu, 31 Oct 2019 10:39:20 +0000 Message-Id: <20191031103920.3919-2-dmonakhov@openvz.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20191031103920.3919-1-dmonakhov@openvz.org> References: <20191031103920.3919-1-dmonakhov@openvz.org> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org From: Dmitry Monakhov There is a race window where quota was redirted once we drop dq_list_lock inside dqput(), but before we grab dquot->dq_lock inside dquot_release() TASK1 TASK2 (chowner) ->dqput() we_slept: spin_lock(&dq_list_lock) if (dquot_dirty(dquot)) { spin_unlock(&dq_list_lock); dquot->dq_sb->dq_op->write_dquot(dquot); goto we_slept if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) { spin_unlock(&dq_list_lock); dquot->dq_sb->dq_op->release_dquot(dquot); dqget() mark_dquot_dirty() dqput() goto we_slept; } So dquot dirty quota will be released by TASK1, but on next we_sleept loop we detect this and call ->write_dquot() for it. XFSTEST: https://github.com/dmonakhov/xfstests/commit/440a80d4cbb39e9234df4d7240aee1d551c36107 Signed-off-by: Dmitry Monakhov --- fs/ocfs2/quota_global.c | 2 +- fs/quota/dquot.c | 2 +- include/linux/quotaops.h | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/fs/ocfs2/quota_global.c b/fs/ocfs2/quota_global.c index 7a92219..eda8348 100644 --- a/fs/ocfs2/quota_global.c +++ b/fs/ocfs2/quota_global.c @@ -728,7 +728,7 @@ static int ocfs2_release_dquot(struct dquot *dquot) mutex_lock(&dquot->dq_lock); /* Check whether we are not racing with some other dqget() */ - if (atomic_read(&dquot->dq_count) > 1) + if (dquot_is_busy(dquot)) goto out; /* Running from downconvert thread? Postpone quota processing to wq */ if (current == osb->dc_task) { diff --git a/fs/quota/dquot.c b/fs/quota/dquot.c index b492b9e..72d24a5 100644 --- a/fs/quota/dquot.c +++ b/fs/quota/dquot.c @@ -497,7 +497,7 @@ int dquot_release(struct dquot *dquot) mutex_lock(&dquot->dq_lock); /* Check whether we are not racing with some other dqget() */ - if (atomic_read(&dquot->dq_count) > 1) + if (dquot_is_busy(dquot)) goto out_dqlock; if (dqopt->ops[dquot->dq_id.type]->release_dqblk) { ret = dqopt->ops[dquot->dq_id.type]->release_dqblk(dquot); diff --git a/include/linux/quotaops.h b/include/linux/quotaops.h index 185d948..91e0b76 100644 --- a/include/linux/quotaops.h +++ b/include/linux/quotaops.h @@ -54,6 +54,16 @@ static inline struct dquot *dqgrab(struct dquot *dquot) atomic_inc(&dquot->dq_count); return dquot; } + +static inline bool dquot_is_busy(struct dquot *dquot) +{ + if (test_bit(DQ_MOD_B, &dquot->dq_flags)) + return true; + if (atomic_read(&dquot->dq_count) > 1) + return true; + return false; +} + void dqput(struct dquot *dquot); int dquot_scan_active(struct super_block *sb, int (*fn)(struct dquot *dquot, unsigned long priv),