2019-10-31 10:40:07

by Dmitry Monakhov

[permalink] [raw]
Subject: [PATCH 1/2] fs/quota: fix livelock in dquot_writeback_dquots

From: Dmitry Monakhov <[email protected]>

Write only quotas which are dirty at entry.


XFSTEST: https://github.com/dmonakhov/xfstests/commit/b10ad23566a5bf75832a6f500e1236084083cddc

Signed-off-by: Konstantin Khlebnikov <[email protected]>
Signed-off-by: Dmitry Monakhov <[email protected]>
---
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));
--
2.7.4


2019-10-31 10:40:27

by Dmitry Monakhov

[permalink] [raw]
Subject: [PATCH 2/2] fs/quota: Check that quota is not dirty before release

From: Dmitry Monakhov <[email protected]>

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 <[email protected]>
---
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),
--
2.7.4

2019-10-31 18:02:00

by Jan Kara

[permalink] [raw]
Subject: Re: [PATCH 2/2] fs/quota: Check that quota is not dirty before release

On Thu 31-10-19 10:39:20, Dmitry Monakhov wrote:
> From: Dmitry Monakhov <[email protected]>
>
> 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

Yeah, good catch. Both patches look good to me. I've added them to my tree.

Honza

>
> Signed-off-by: Dmitry Monakhov <[email protected]>
> ---
> 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),
> --
> 2.7.4
>
--
Jan Kara <[email protected]>
SUSE Labs, CR

2019-10-31 18:12:50

by Jan Kara

[permalink] [raw]
Subject: Re: [PATCH 2/2] fs/quota: Check that quota is not dirty before release

On Thu 31-10-19 19:00:33, Jan Kara wrote:
> On Thu 31-10-19 10:39:20, Dmitry Monakhov wrote:
> > From: Dmitry Monakhov <[email protected]>
> >
> > 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
>
> Yeah, good catch. Both patches look good to me. I've added them to my tree.

And forgot to add: Thanks for both patches and also the regression tests! I
really appreciate it!

Honza


> > Signed-off-by: Dmitry Monakhov <[email protected]>
> > ---
> > 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),
> > --
> > 2.7.4
> >
> --
> Jan Kara <[email protected]>
> SUSE Labs, CR
--
Jan Kara <[email protected]>
SUSE Labs, CR