staging: erofs: fix race when the managed cache is enabled
authorGao Xiang <gaoxiang25@huawei.com>
Thu, 22 Nov 2018 17:16:00 +0000 (01:16 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 19 Mar 2019 12:12:42 +0000 (13:12 +0100)
commiteab8018fd289304c742212cbdcecc2e08716ba83
tree7904bcea6f6263494d8c146ded930aa93d4544aa
parent96ce54b24c38913cc132659dabb6780f660fda3f
staging: erofs: fix race when the managed cache is enabled

commit 51232df5e4b268936beccde5248f312a316800be upstream.

When the managed cache is enabled, the last reference count
of a workgroup must be used for its workstation.

Otherwise, it could lead to incorrect (un)freezes in
the reclaim path, and it would be harmful.

A typical race as follows:

Thread 1 (In the reclaim path)  Thread 2
workgroup_freeze(grp, 1)                                refcnt = 1
...
workgroup_unfreeze(grp, 1)                              refcnt = 1
                                workgroup_get(grp)      refcnt = 2 (x)
workgroup_put(grp)                                      refcnt = 1 (x)
                                ...unexpected behaviors

* grp is detached but still used, which violates cache-managed
  freeze constraint.

Reviewed-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Gao Xiang <gaoxiang25@huawei.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/staging/erofs/internal.h
drivers/staging/erofs/utils.c