Skip to content

Commit aa39cc6

Browse files
kyeongyrichardweinberger
authored andcommitted
jffs2: GC deadlock reading a page that is used in jffs2_write_begin()
GC task can deadlock in read_cache_page() because it may attempt to release a page that is actually allocated by another task in jffs2_write_begin(). The reason is that in jffs2_write_begin() there is a small window a cache page is allocated for use but not set Uptodate yet. This ends up with a deadlock between two tasks: 1) A task (e.g. file copy) - jffs2_write_begin() locks a cache page - jffs2_write_end() tries to lock "alloc_sem" from jffs2_reserve_space() <-- STUCK 2) GC task (jffs2_gcd_mtd3) - jffs2_garbage_collect_pass() locks "alloc_sem" - try to lock the same cache page in read_cache_page() <-- STUCK So to avoid this deadlock, hold "alloc_sem" in jffs2_write_begin() while reading data in a cache page. Signed-off-by: Kyeong Yoo <kyeong.yoo@alliedtelesis.co.nz> Signed-off-by: Richard Weinberger <richard@nod.at>
1 parent 50cb437 commit aa39cc6

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

fs/jffs2/file.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,15 @@ static int jffs2_write_begin(struct file *filp, struct address_space *mapping,
136136
struct page *pg;
137137
struct inode *inode = mapping->host;
138138
struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
139+
struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
139140
pgoff_t index = pos >> PAGE_SHIFT;
140141
uint32_t pageofs = index << PAGE_SHIFT;
141142
int ret = 0;
142143

143-
pg = grab_cache_page_write_begin(mapping, index, flags);
144-
if (!pg)
145-
return -ENOMEM;
146-
*pagep = pg;
147-
148144
jffs2_dbg(1, "%s()\n", __func__);
149145

150146
if (pageofs > inode->i_size) {
151147
/* Make new hole frag from old EOF to new page */
152-
struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
153148
struct jffs2_raw_inode ri;
154149
struct jffs2_full_dnode *fn;
155150
uint32_t alloc_len;
@@ -160,7 +155,7 @@ static int jffs2_write_begin(struct file *filp, struct address_space *mapping,
160155
ret = jffs2_reserve_space(c, sizeof(ri), &alloc_len,
161156
ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
162157
if (ret)
163-
goto out_page;
158+
goto out_err;
164159

165160
mutex_lock(&f->sem);
166161
memset(&ri, 0, sizeof(ri));
@@ -190,7 +185,7 @@ static int jffs2_write_begin(struct file *filp, struct address_space *mapping,
190185
ret = PTR_ERR(fn);
191186
jffs2_complete_reservation(c);
192187
mutex_unlock(&f->sem);
193-
goto out_page;
188+
goto out_err;
194189
}
195190
ret = jffs2_add_full_dnode_to_inode(c, f, fn);
196191
if (f->metadata) {
@@ -205,13 +200,26 @@ static int jffs2_write_begin(struct file *filp, struct address_space *mapping,
205200
jffs2_free_full_dnode(fn);
206201
jffs2_complete_reservation(c);
207202
mutex_unlock(&f->sem);
208-
goto out_page;
203+
goto out_err;
209204
}
210205
jffs2_complete_reservation(c);
211206
inode->i_size = pageofs;
212207
mutex_unlock(&f->sem);
213208
}
214209

210+
/*
211+
* While getting a page and reading data in, lock c->alloc_sem until
212+
* the page is Uptodate. Otherwise GC task may attempt to read the same
213+
* page in read_cache_page(), which causes a deadlock.
214+
*/
215+
mutex_lock(&c->alloc_sem);
216+
pg = grab_cache_page_write_begin(mapping, index, flags);
217+
if (!pg) {
218+
ret = -ENOMEM;
219+
goto release_sem;
220+
}
221+
*pagep = pg;
222+
215223
/*
216224
* Read in the page if it wasn't already present. Cannot optimize away
217225
* the whole page write case until jffs2_write_end can handle the
@@ -221,15 +229,17 @@ static int jffs2_write_begin(struct file *filp, struct address_space *mapping,
221229
mutex_lock(&f->sem);
222230
ret = jffs2_do_readpage_nolock(inode, pg);
223231
mutex_unlock(&f->sem);
224-
if (ret)
225-
goto out_page;
232+
if (ret) {
233+
unlock_page(pg);
234+
put_page(pg);
235+
goto release_sem;
236+
}
226237
}
227238
jffs2_dbg(1, "end write_begin(). pg->flags %lx\n", pg->flags);
228-
return ret;
229239

230-
out_page:
231-
unlock_page(pg);
232-
put_page(pg);
240+
release_sem:
241+
mutex_unlock(&c->alloc_sem);
242+
out_err:
233243
return ret;
234244
}
235245

0 commit comments

Comments
 (0)