Refcount socket Dirents correctly.

This should fix the socket Dirent memory leak.

fs.NewFile takes a new reference. It should hold the *only* reference.
DecRef that socket Dirent.

Before the globalDirentMap was introduced, a mis-refcounted Dirent
would be garbage collected when all references to it were gone. For
socket Dirents, this meant that they would be garbage collected when
the associated fs.Files disappeared.

After the globalDirentMap, Dirents *must* be reference-counted
correctly to be garbage collected, as Dirents remove themselves
from the global map when their refcount goes to -1 (see Dirent.destroy).
That removes the last pointer to that Dirent.

PiperOrigin-RevId: 196878973
Change-Id: Ic7afcd1de97c7101ccb13be5fc31de0fb50963f0
This commit is contained in:
Christopher Koch 2018-05-16 13:28:29 -07:00 committed by Shentubot
parent f295e26b8a
commit d154c6a25f
4 changed files with 4 additions and 0 deletions

View File

@ -116,6 +116,7 @@ type SocketOperations struct {
// New creates a new endpoint socket.
func New(t *kernel.Task, family int, skType unix.SockType, queue *waiter.Queue, endpoint tcpip.Endpoint) *fs.File {
dirent := socket.NewDirent(t, epsocketDevice)
defer dirent.DecRef()
return fs.NewFile(t, dirent, fs.FileFlags{Read: true, Write: true}, &SocketOperations{
Queue: queue,
family: family,

View File

@ -65,6 +65,7 @@ func newSocketFile(ctx context.Context, fd int, nonblock bool) (*fs.File, *syser
return nil, syserr.FromError(err)
}
dirent := socket.NewDirent(ctx, socketDevice)
defer dirent.DecRef()
return fs.NewFile(ctx, dirent, fs.FileFlags{NonBlocking: nonblock, Read: true, Write: true}, s), nil
}

View File

@ -89,6 +89,7 @@ func (*socketProvider) Socket(t *kernel.Task, stype unix.SockType, protocol int)
}
d := socket.NewDirent(t, netlinkSocketDevice)
defer d.DecRef()
return fs.NewFile(t, d, fs.FileFlags{Read: true, Write: true}, s), nil
}

View File

@ -56,6 +56,7 @@ type SocketOperations struct {
// New creates a new unix socket.
func New(ctx context.Context, endpoint unix.Endpoint) *fs.File {
dirent := socket.NewDirent(ctx, unixSocketDevice)
defer dirent.DecRef()
return NewWithDirent(ctx, dirent, endpoint, fs.FileFlags{Read: true, Write: true})
}