Skip to content

Commit c63e56a

Browse files
committed
ovl: do not open/llseek lower file with upper sb_writers held
overlayfs file open (ovl_maybe_lookup_lowerdata) and overlay file llseek take the ovl_inode_lock, without holding upper sb_writers. In case of nested lower overlay that uses same upper fs as this overlay, lockdep will warn about (possibly false positive) circular lock dependency when doing open/llseek of lower ovl file during copy up with our upper sb_writers held, because the locking ordering seems reverse to the locking order in ovl_copy_up_start(): - lower ovl_inode_lock - upper sb_writers Let the copy up "transaction" keeps an elevated mnt write count on upper mnt, but leaves taking upper sb_writers to lower level helpers only when they actually need it. This allows to avoid holding upper sb_writers during lower file open/llseek and prevents the lockdep warning. Minimizing the scope of upper sb_writers during copy up is also needed for fixing another possible deadlocks by a following patch. Signed-off-by: Amir Goldstein <[email protected]>
1 parent 162d064 commit c63e56a

File tree

2 files changed

+61
-23
lines changed

2 files changed

+61
-23
lines changed

fs/overlayfs/copy_up.c

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,9 @@ static int ovl_copy_up_file(struct ovl_fs *ofs, struct dentry *dentry,
252252
return PTR_ERR(old_file);
253253

254254
/* Try to use clone_file_range to clone up within the same fs */
255+
ovl_start_write(dentry);
255256
cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0);
257+
ovl_end_write(dentry);
256258
if (cloned == len)
257259
goto out_fput;
258260
/* Couldn't clone, so now we try to copy the data */
@@ -287,8 +289,12 @@ static int ovl_copy_up_file(struct ovl_fs *ofs, struct dentry *dentry,
287289
* it may not recognize all kind of holes and sometimes
288290
* only skips partial of hole area. However, it will be
289291
* enough for most of the use cases.
292+
*
293+
* We do not hold upper sb_writers throughout the loop to avert
294+
* lockdep warning with llseek of lower file in nested overlay:
295+
* - upper sb_writers
296+
* -- lower ovl_inode_lock (ovl_llseek)
290297
*/
291-
292298
if (skip_hole && data_pos < old_pos) {
293299
data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA);
294300
if (data_pos > old_pos) {
@@ -303,9 +309,11 @@ static int ovl_copy_up_file(struct ovl_fs *ofs, struct dentry *dentry,
303309
}
304310
}
305311

312+
ovl_start_write(dentry);
306313
bytes = do_splice_direct(old_file, &old_pos,
307314
new_file, &new_pos,
308315
this_len, SPLICE_F_MOVE);
316+
ovl_end_write(dentry);
309317
if (bytes <= 0) {
310318
error = bytes;
311319
break;
@@ -555,14 +563,16 @@ static int ovl_link_up(struct ovl_copy_up_ctx *c)
555563
struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
556564
struct inode *udir = d_inode(upperdir);
557565

566+
ovl_start_write(c->dentry);
567+
558568
/* Mark parent "impure" because it may now contain non-pure upper */
559569
err = ovl_set_impure(c->parent, upperdir);
560570
if (err)
561-
return err;
571+
goto out;
562572

563573
err = ovl_set_nlink_lower(c->dentry);
564574
if (err)
565-
return err;
575+
goto out;
566576

567577
inode_lock_nested(udir, I_MUTEX_PARENT);
568578
upper = ovl_lookup_upper(ofs, c->dentry->d_name.name, upperdir,
@@ -581,10 +591,12 @@ static int ovl_link_up(struct ovl_copy_up_ctx *c)
581591
}
582592
inode_unlock(udir);
583593
if (err)
584-
return err;
594+
goto out;
585595

586596
err = ovl_set_nlink_upper(c->dentry);
587597

598+
out:
599+
ovl_end_write(c->dentry);
588600
return err;
589601
}
590602

@@ -719,30 +731,41 @@ static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
719731
.link = c->link
720732
};
721733

722-
/* workdir and destdir could be the same when copying up to indexdir */
723-
err = -EIO;
724-
if (lock_rename(c->workdir, c->destdir) != NULL)
725-
goto unlock;
726-
727734
err = ovl_prep_cu_creds(c->dentry, &cc);
728735
if (err)
729-
goto unlock;
736+
return err;
730737

738+
ovl_start_write(c->dentry);
739+
inode_lock(wdir);
731740
temp = ovl_create_temp(ofs, c->workdir, &cattr);
741+
inode_unlock(wdir);
742+
ovl_end_write(c->dentry);
732743
ovl_revert_cu_creds(&cc);
733744

734-
err = PTR_ERR(temp);
735745
if (IS_ERR(temp))
736-
goto unlock;
746+
return PTR_ERR(temp);
737747

738748
/*
739749
* Copy up data first and then xattrs. Writing data after
740750
* xattrs will remove security.capability xattr automatically.
741751
*/
742752
path.dentry = temp;
743753
err = ovl_copy_up_data(c, &path);
744-
if (err)
754+
/*
755+
* We cannot hold lock_rename() throughout this helper, because or
756+
* lock ordering with sb_writers, which shouldn't be held when calling
757+
* ovl_copy_up_data(), so lock workdir and destdir and make sure that
758+
* temp wasn't moved before copy up completion or cleanup.
759+
* If temp was moved, abort without the cleanup.
760+
*/
761+
ovl_start_write(c->dentry);
762+
if (lock_rename(c->workdir, c->destdir) != NULL ||
763+
temp->d_parent != c->workdir) {
764+
err = -EIO;
765+
goto unlock;
766+
} else if (err) {
745767
goto cleanup;
768+
}
746769

747770
err = ovl_copy_up_metadata(c, temp);
748771
if (err)
@@ -779,6 +802,7 @@ static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
779802
ovl_set_flag(OVL_WHITEOUTS, inode);
780803
unlock:
781804
unlock_rename(c->workdir, c->destdir);
805+
ovl_end_write(c->dentry);
782806

783807
return err;
784808

@@ -802,9 +826,10 @@ static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
802826
if (err)
803827
return err;
804828

829+
ovl_start_write(c->dentry);
805830
tmpfile = ovl_do_tmpfile(ofs, c->workdir, c->stat.mode);
831+
ovl_end_write(c->dentry);
806832
ovl_revert_cu_creds(&cc);
807-
808833
if (IS_ERR(tmpfile))
809834
return PTR_ERR(tmpfile);
810835

@@ -815,9 +840,11 @@ static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
815840
goto out_fput;
816841
}
817842

843+
ovl_start_write(c->dentry);
844+
818845
err = ovl_copy_up_metadata(c, temp);
819846
if (err)
820-
goto out_fput;
847+
goto out;
821848

822849
inode_lock_nested(udir, I_MUTEX_PARENT);
823850

@@ -831,7 +858,7 @@ static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
831858
inode_unlock(udir);
832859

833860
if (err)
834-
goto out_fput;
861+
goto out;
835862

836863
if (c->metacopy_digest)
837864
ovl_set_flag(OVL_HAS_DIGEST, d_inode(c->dentry));
@@ -843,6 +870,8 @@ static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
843870
ovl_set_upperdata(d_inode(c->dentry));
844871
ovl_inode_update(d_inode(c->dentry), dget(temp));
845872

873+
out:
874+
ovl_end_write(c->dentry);
846875
out_fput:
847876
fput(tmpfile);
848877
return err;
@@ -893,7 +922,9 @@ static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
893922
* Mark parent "impure" because it may now contain non-pure
894923
* upper
895924
*/
925+
ovl_start_write(c->dentry);
896926
err = ovl_set_impure(c->parent, c->destdir);
927+
ovl_end_write(c->dentry);
897928
if (err)
898929
return err;
899930
}
@@ -909,6 +940,7 @@ static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
909940
if (c->indexed)
910941
ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
911942

943+
ovl_start_write(c->dentry);
912944
if (to_index) {
913945
/* Initialize nlink for copy up of disconnected dentry */
914946
err = ovl_set_nlink_upper(c->dentry);
@@ -923,6 +955,7 @@ static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
923955
ovl_dentry_set_upper_alias(c->dentry);
924956
ovl_dentry_update_reval(c->dentry, ovl_dentry_upper(c->dentry));
925957
}
958+
ovl_end_write(c->dentry);
926959

927960
out:
928961
if (to_index)
@@ -1011,15 +1044,16 @@ static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
10111044
* Writing to upper file will clear security.capability xattr. We
10121045
* don't want that to happen for normal copy-up operation.
10131046
*/
1047+
ovl_start_write(c->dentry);
10141048
if (capability) {
10151049
err = ovl_do_setxattr(ofs, upperpath.dentry, XATTR_NAME_CAPS,
10161050
capability, cap_size, 0);
1017-
if (err)
1018-
goto out_free;
10191051
}
1020-
1021-
1022-
err = ovl_removexattr(ofs, upperpath.dentry, OVL_XATTR_METACOPY);
1052+
if (!err) {
1053+
err = ovl_removexattr(ofs, upperpath.dentry,
1054+
OVL_XATTR_METACOPY);
1055+
}
1056+
ovl_end_write(c->dentry);
10231057
if (err)
10241058
goto out_free;
10251059

fs/overlayfs/util.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,10 @@ bool ovl_already_copied_up(struct dentry *dentry, int flags)
670670
return false;
671671
}
672672

673+
/*
674+
* The copy up "transaction" keeps an elevated mnt write count on upper mnt,
675+
* but leaves taking freeze protection on upper sb to lower level helpers.
676+
*/
673677
int ovl_copy_up_start(struct dentry *dentry, int flags)
674678
{
675679
struct inode *inode = d_inode(dentry);
@@ -682,7 +686,7 @@ int ovl_copy_up_start(struct dentry *dentry, int flags)
682686
if (ovl_already_copied_up_locked(dentry, flags))
683687
err = 1; /* Already copied up */
684688
else
685-
err = ovl_want_write(dentry);
689+
err = ovl_get_write_access(dentry);
686690
if (err)
687691
goto out_unlock;
688692

@@ -695,7 +699,7 @@ int ovl_copy_up_start(struct dentry *dentry, int flags)
695699

696700
void ovl_copy_up_end(struct dentry *dentry)
697701
{
698-
ovl_drop_write(dentry);
702+
ovl_put_write_access(dentry);
699703
ovl_inode_unlock(d_inode(dentry));
700704
}
701705

0 commit comments

Comments
 (0)