Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 55566c9

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 ad7b48e commit 55566c9

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
@@ -445,6 +445,9 @@ CreateFakeRelcacheEntry(RelFileNode rnode)
445445
void
446446
FreeFakeRelcacheEntry(Relation fakerel)
447447
{
448+
/* make sure the fakerel is not referenced by the SmgrRelation anymore */
449+
if (fakerel->rd_smgr != NULL)
450+
smgrclearowner(&fakerel->rd_smgr, fakerel->rd_smgr);
448451
pfree(fakerel);
449452
}
450453

src/backend/storage/smgr/smgr.c

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

8585
/* local function prototypes */
8686
static void smgrshutdown(int code, Datum arg);
87+
static void add_to_unowned_list(SMgrRelation reln);
8788
static void remove_from_unowned_list(SMgrRelation reln);
8889

8990

@@ -174,9 +175,8 @@ smgropen(RelFileNode rnode, BackendId backend)
174175
for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
175176
reln->md_fd[forknum] = NULL;
176177

177-
/* place it at head of unowned list (to make smgrsetowner cheap) */
178-
reln->next_unowned_reln = first_unowned_reln;
179-
first_unowned_reln = reln;
178+
/* it has no owner yet */
179+
add_to_unowned_list(reln);
180180
}
181181

182182
return reln;
@@ -191,7 +191,7 @@ smgropen(RelFileNode rnode, BackendId backend)
191191
void
192192
smgrsetowner(SMgrRelation *owner, SMgrRelation reln)
193193
{
194-
/* We don't currently support "disowning" an SMgrRelation here */
194+
/* We don't support "disowning" an SMgrRelation here, use smgrclearowner */
195195
Assert(owner != NULL);
196196

197197
/*
@@ -213,6 +213,40 @@ smgrsetowner(SMgrRelation *owner, SMgrRelation reln)
213213
*owner = reln;
214214
}
215215

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

src/include/storage/smgr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ extern void smgrinit(void);
8080
extern SMgrRelation smgropen(RelFileNode rnode, BackendId backend);
8181
extern bool smgrexists(SMgrRelation reln, ForkNumber forknum);
8282
extern void smgrsetowner(SMgrRelation *owner, SMgrRelation reln);
83+
extern void smgrclearowner(SMgrRelation *owner, SMgrRelation reln);
8384
extern void smgrclose(SMgrRelation reln);
8485
extern void smgrcloseall(void);
8586
extern void smgrclosenode(RelFileNodeBackend rnode);

0 commit comments

Comments
 (0)