Skip to content

Commit 080ad91

Browse files
committed
Fix dangling smgr_owner pointer when a fake relcache entry is freed.
A fake relcache entry can "own" a SmgrRelation object, like a regular relcache entry. But when it was free'd, the owner field in SmgrRelation was not cleared, so it was left pointing to free'd memory. Amazingly this apparently hasn't caused crashes in practice, or we would've heard about it earlier. Andres found this with Valgrind. Report and fix by Andres Freund, with minor modifications by me. Backpatch to all supported versions.
1 parent c7a186e commit 080ad91

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

src/backend/access/transam/xlogutils.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,9 @@ CreateFakeRelcacheEntry(RelFileNode rnode)
421421
void
422422
FreeFakeRelcacheEntry(Relation fakerel)
423423
{
424+
/* make sure the fakerel is not referenced by the SmgrRelation anymore */
425+
if (fakerel->rd_smgr != NULL)
426+
smgrclearowner(&fakerel->rd_smgr, fakerel->rd_smgr);
424427
pfree(fakerel);
425428
}
426429

src/backend/storage/smgr/smgr.c

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ static SMgrRelation first_unowned_reln = NULL;
8686

8787
/* local function prototypes */
8888
static void smgrshutdown(int code, Datum arg);
89+
static void add_to_unowned_list(SMgrRelation reln);
8990
static void remove_from_unowned_list(SMgrRelation reln);
9091

9192

@@ -176,9 +177,8 @@ smgropen(RelFileNode rnode, BackendId backend)
176177
for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
177178
reln->md_fd[forknum] = NULL;
178179

179-
/* place it at head of unowned list (to make smgrsetowner cheap) */
180-
reln->next_unowned_reln = first_unowned_reln;
181-
first_unowned_reln = reln;
180+
/* it has no owner yet */
181+
add_to_unowned_list(reln);
182182
}
183183

184184
return reln;
@@ -193,7 +193,7 @@ smgropen(RelFileNode rnode, BackendId backend)
193193
void
194194
smgrsetowner(SMgrRelation *owner, SMgrRelation reln)
195195
{
196-
/* We don't currently support "disowning" an SMgrRelation here */
196+
/* We don't support "disowning" an SMgrRelation here, use smgrclearowner */
197197
Assert(owner != NULL);
198198

199199
/*
@@ -215,6 +215,40 @@ smgrsetowner(SMgrRelation *owner, SMgrRelation reln)
215215
*owner = reln;
216216
}
217217

218+
/*
219+
* smgrclearowner() -- Remove long-lived reference to an SMgrRelation object
220+
* if one exists
221+
*/
222+
void
223+
smgrclearowner(SMgrRelation *owner, SMgrRelation reln)
224+
{
225+
/* Do nothing if the SMgrRelation object is not owned by the owner */
226+
if (reln->smgr_owner != owner)
227+
return;
228+
229+
/* unset the owner's reference */
230+
*owner = NULL;
231+
232+
/* unset our reference to the owner */
233+
reln->smgr_owner = NULL;
234+
235+
add_to_unowned_list(reln);
236+
}
237+
238+
/*
239+
* add_to_unowned_list -- link an SMgrRelation onto the unowned list
240+
*
241+
* Check remove_from_unowned_list()'s comments for performance
242+
* considerations.
243+
*/
244+
static void
245+
add_to_unowned_list(SMgrRelation reln)
246+
{
247+
/* place it at head of the list (to make smgrsetowner cheap) */
248+
reln->next_unowned_reln = first_unowned_reln;
249+
first_unowned_reln = reln;
250+
}
251+
218252
/*
219253
* remove_from_unowned_list -- unlink an SMgrRelation from the unowned list
220254
*

src/include/storage/smgr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ extern void smgrinit(void);
8282
extern SMgrRelation smgropen(RelFileNode rnode, BackendId backend);
8383
extern bool smgrexists(SMgrRelation reln, ForkNumber forknum);
8484
extern void smgrsetowner(SMgrRelation *owner, SMgrRelation reln);
85+
extern void smgrclearowner(SMgrRelation *owner, SMgrRelation reln);
8586
extern void smgrclose(SMgrRelation reln);
8687
extern void smgrcloseall(void);
8788
extern void smgrclosenode(RelFileNodeBackend rnode);

0 commit comments

Comments
 (0)