Black Lives Matter. Support the Equal Justice Initiative.

Source file src/runtime/netpoll.go

Documentation: runtime

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build aix || darwin || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris || windows
     6  // +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris windows
     7  
     8  package runtime
     9  
    10  import (
    11  	"runtime/internal/atomic"
    12  	"unsafe"
    13  )
    14  
    15  // Integrated network poller (platform-independent part).
    16  // A particular implementation (epoll/kqueue/port/AIX/Windows)
    17  // must define the following functions:
    18  //
    19  // func netpollinit()
    20  //     Initialize the poller. Only called once.
    21  //
    22  // func netpollopen(fd uintptr, pd *pollDesc) int32
    23  //     Arm edge-triggered notifications for fd. The pd argument is to pass
    24  //     back to netpollready when fd is ready. Return an errno value.
    25  //
    26  // func netpollclose(fd uintptr) int32
    27  //     Disable notifications for fd. Return an errno value.
    28  //
    29  // func netpoll(delta int64) gList
    30  //     Poll the network. If delta < 0, block indefinitely. If delta == 0,
    31  //     poll without blocking. If delta > 0, block for up to delta nanoseconds.
    32  //     Return a list of goroutines built by calling netpollready.
    33  //
    34  // func netpollBreak()
    35  //     Wake up the network poller, assumed to be blocked in netpoll.
    36  //
    37  // func netpollIsPollDescriptor(fd uintptr) bool
    38  //     Reports whether fd is a file descriptor used by the poller.
    39  
    40  // Error codes returned by runtime_pollReset and runtime_pollWait.
    41  // These must match the values in internal/poll/fd_poll_runtime.go.
    42  const (
    43  	pollNoError        = 0 // no error
    44  	pollErrClosing     = 1 // descriptor is closed
    45  	pollErrTimeout     = 2 // I/O timeout
    46  	pollErrNotPollable = 3 // general error polling descriptor
    47  )
    48  
    49  // pollDesc contains 2 binary semaphores, rg and wg, to park reader and writer
    50  // goroutines respectively. The semaphore can be in the following states:
    51  // pdReady - io readiness notification is pending;
    52  //           a goroutine consumes the notification by changing the state to nil.
    53  // pdWait - a goroutine prepares to park on the semaphore, but not yet parked;
    54  //          the goroutine commits to park by changing the state to G pointer,
    55  //          or, alternatively, concurrent io notification changes the state to pdReady,
    56  //          or, alternatively, concurrent timeout/close changes the state to nil.
    57  // G pointer - the goroutine is blocked on the semaphore;
    58  //             io notification or timeout/close changes the state to pdReady or nil respectively
    59  //             and unparks the goroutine.
    60  // nil - none of the above.
    61  const (
    62  	pdReady uintptr = 1
    63  	pdWait  uintptr = 2
    64  )
    65  
    66  const pollBlockSize = 4 * 1024
    67  
    68  // Network poller descriptor.
    69  //
    70  // No heap pointers.
    71  //
    72  //go:notinheap
    73  type pollDesc struct {
    74  	link *pollDesc // in pollcache, protected by pollcache.lock
    75  
    76  	// The lock protects pollOpen, pollSetDeadline, pollUnblock and deadlineimpl operations.
    77  	// This fully covers seq, rt and wt variables. fd is constant throughout the PollDesc lifetime.
    78  	// pollReset, pollWait, pollWaitCanceled and runtime·netpollready (IO readiness notification)
    79  	// proceed w/o taking the lock. So closing, everr, rg, rd, wg and wd are manipulated
    80  	// in a lock-free way by all operations.
    81  	// NOTE(dvyukov): the following code uses uintptr to store *g (rg/wg),
    82  	// that will blow up when GC starts moving objects.
    83  	lock    mutex // protects the following fields
    84  	fd      uintptr
    85  	closing bool
    86  	everr   bool      // marks event scanning error happened
    87  	user    uint32    // user settable cookie
    88  	rseq    uintptr   // protects from stale read timers
    89  	rg      uintptr   // pdReady, pdWait, G waiting for read or nil
    90  	rt      timer     // read deadline timer (set if rt.f != nil)
    91  	rd      int64     // read deadline
    92  	wseq    uintptr   // protects from stale write timers
    93  	wg      uintptr   // pdReady, pdWait, G waiting for write or nil
    94  	wt      timer     // write deadline timer
    95  	wd      int64     // write deadline
    96  	self    *pollDesc // storage for indirect interface. See (*pollDesc).makeArg.
    97  }
    98  
    99  type pollCache struct {
   100  	lock  mutex
   101  	first *pollDesc
   102  	// PollDesc objects must be type-stable,
   103  	// because we can get ready notification from epoll/kqueue
   104  	// after the descriptor is closed/reused.
   105  	// Stale notifications are detected using seq variable,
   106  	// seq is incremented when deadlines are changed or descriptor is reused.
   107  }
   108  
   109  var (
   110  	netpollInitLock mutex
   111  	netpollInited   uint32
   112  
   113  	pollcache      pollCache
   114  	netpollWaiters uint32
   115  )
   116  
   117  //go:linkname poll_runtime_pollServerInit internal/poll.runtime_pollServerInit
   118  func poll_runtime_pollServerInit() {
   119  	netpollGenericInit()
   120  }
   121  
   122  func netpollGenericInit() {
   123  	if atomic.Load(&netpollInited) == 0 {
   124  		lockInit(&netpollInitLock, lockRankNetpollInit)
   125  		lock(&netpollInitLock)
   126  		if netpollInited == 0 {
   127  			netpollinit()
   128  			atomic.Store(&netpollInited, 1)
   129  		}
   130  		unlock(&netpollInitLock)
   131  	}
   132  }
   133  
   134  func netpollinited() bool {
   135  	return atomic.Load(&netpollInited) != 0
   136  }
   137  
   138  //go:linkname poll_runtime_isPollServerDescriptor internal/poll.runtime_isPollServerDescriptor
   139  
   140  // poll_runtime_isPollServerDescriptor reports whether fd is a
   141  // descriptor being used by netpoll.
   142  func poll_runtime_isPollServerDescriptor(fd uintptr) bool {
   143  	return netpollIsPollDescriptor(fd)
   144  }
   145  
   146  //go:linkname poll_runtime_pollOpen internal/poll.runtime_pollOpen
   147  func poll_runtime_pollOpen(fd uintptr) (*pollDesc, int) {
   148  	pd := pollcache.alloc()
   149  	lock(&pd.lock)
   150  	if pd.wg != 0 && pd.wg != pdReady {
   151  		throw("runtime: blocked write on free polldesc")
   152  	}
   153  	if pd.rg != 0 && pd.rg != pdReady {
   154  		throw("runtime: blocked read on free polldesc")
   155  	}
   156  	pd.fd = fd
   157  	pd.closing = false
   158  	pd.everr = false
   159  	pd.rseq++
   160  	pd.rg = 0
   161  	pd.rd = 0
   162  	pd.wseq++
   163  	pd.wg = 0
   164  	pd.wd = 0
   165  	pd.self = pd
   166  	unlock(&pd.lock)
   167  
   168  	errno := netpollopen(fd, pd)
   169  	if errno != 0 {
   170  		pollcache.free(pd)
   171  		return nil, int(errno)
   172  	}
   173  	return pd, 0
   174  }
   175  
   176  //go:linkname poll_runtime_pollClose internal/poll.runtime_pollClose
   177  func poll_runtime_pollClose(pd *pollDesc) {
   178  	if !pd.closing {
   179  		throw("runtime: close polldesc w/o unblock")
   180  	}
   181  	if pd.wg != 0 && pd.wg != pdReady {
   182  		throw("runtime: blocked write on closing polldesc")
   183  	}
   184  	if pd.rg != 0 && pd.rg != pdReady {
   185  		throw("runtime: blocked read on closing polldesc")
   186  	}
   187  	netpollclose(pd.fd)
   188  	pollcache.free(pd)
   189  }
   190  
   191  func (c *pollCache) free(pd *pollDesc) {
   192  	lock(&c.lock)
   193  	pd.link = c.first
   194  	c.first = pd
   195  	unlock(&c.lock)
   196  }
   197  
   198  // poll_runtime_pollReset, which is internal/poll.runtime_pollReset,
   199  // prepares a descriptor for polling in mode, which is 'r' or 'w'.
   200  // This returns an error code; the codes are defined above.
   201  //go:linkname poll_runtime_pollReset internal/poll.runtime_pollReset
   202  func poll_runtime_pollReset(pd *pollDesc, mode int) int {
   203  	errcode := netpollcheckerr(pd, int32(mode))
   204  	if errcode != pollNoError {
   205  		return errcode
   206  	}
   207  	if mode == 'r' {
   208  		pd.rg = 0
   209  	} else if mode == 'w' {
   210  		pd.wg = 0
   211  	}
   212  	return pollNoError
   213  }
   214  
   215  // poll_runtime_pollWait, which is internal/poll.runtime_pollWait,
   216  // waits for a descriptor to be ready for reading or writing,
   217  // according to mode, which is 'r' or 'w'.
   218  // This returns an error code; the codes are defined above.
   219  //go:linkname poll_runtime_pollWait internal/poll.runtime_pollWait
   220  func poll_runtime_pollWait(pd *pollDesc, mode int) int {
   221  	errcode := netpollcheckerr(pd, int32(mode))
   222  	if errcode != pollNoError {
   223  		return errcode
   224  	}
   225  	// As for now only Solaris, illumos, and AIX use level-triggered IO.
   226  	if GOOS == "solaris" || GOOS == "illumos" || GOOS == "aix" {
   227  		netpollarm(pd, mode)
   228  	}
   229  	for !netpollblock(pd, int32(mode), false) {
   230  		errcode = netpollcheckerr(pd, int32(mode))
   231  		if errcode != pollNoError {
   232  			return errcode
   233  		}
   234  		// Can happen if timeout has fired and unblocked us,
   235  		// but before we had a chance to run, timeout has been reset.
   236  		// Pretend it has not happened and retry.
   237  	}
   238  	return pollNoError
   239  }
   240  
   241  //go:linkname poll_runtime_pollWaitCanceled internal/poll.runtime_pollWaitCanceled
   242  func poll_runtime_pollWaitCanceled(pd *pollDesc, mode int) {
   243  	// This function is used only on windows after a failed attempt to cancel
   244  	// a pending async IO operation. Wait for ioready, ignore closing or timeouts.
   245  	for !netpollblock(pd, int32(mode), true) {
   246  	}
   247  }
   248  
   249  //go:linkname poll_runtime_pollSetDeadline internal/poll.runtime_pollSetDeadline
   250  func poll_runtime_pollSetDeadline(pd *pollDesc, d int64, mode int) {
   251  	lock(&pd.lock)
   252  	if pd.closing {
   253  		unlock(&pd.lock)
   254  		return
   255  	}
   256  	rd0, wd0 := pd.rd, pd.wd
   257  	combo0 := rd0 > 0 && rd0 == wd0
   258  	if d > 0 {
   259  		d += nanotime()
   260  		if d <= 0 {
   261  			// If the user has a deadline in the future, but the delay calculation
   262  			// overflows, then set the deadline to the maximum possible value.
   263  			d = 1<<63 - 1
   264  		}
   265  	}
   266  	if mode == 'r' || mode == 'r'+'w' {
   267  		pd.rd = d
   268  	}
   269  	if mode == 'w' || mode == 'r'+'w' {
   270  		pd.wd = d
   271  	}
   272  	combo := pd.rd > 0 && pd.rd == pd.wd
   273  	rtf := netpollReadDeadline
   274  	if combo {
   275  		rtf = netpollDeadline
   276  	}
   277  	if pd.rt.f == nil {
   278  		if pd.rd > 0 {
   279  			pd.rt.f = rtf
   280  			// Copy current seq into the timer arg.
   281  			// Timer func will check the seq against current descriptor seq,
   282  			// if they differ the descriptor was reused or timers were reset.
   283  			pd.rt.arg = pd.makeArg()
   284  			pd.rt.seq = pd.rseq
   285  			resettimer(&pd.rt, pd.rd)
   286  		}
   287  	} else if pd.rd != rd0 || combo != combo0 {
   288  		pd.rseq++ // invalidate current timers
   289  		if pd.rd > 0 {
   290  			modtimer(&pd.rt, pd.rd, 0, rtf, pd.makeArg(), pd.rseq)
   291  		} else {
   292  			deltimer(&pd.rt)
   293  			pd.rt.f = nil
   294  		}
   295  	}
   296  	if pd.wt.f == nil {
   297  		if pd.wd > 0 && !combo {
   298  			pd.wt.f = netpollWriteDeadline
   299  			pd.wt.arg = pd.makeArg()
   300  			pd.wt.seq = pd.wseq
   301  			resettimer(&pd.wt, pd.wd)
   302  		}
   303  	} else if pd.wd != wd0 || combo != combo0 {
   304  		pd.wseq++ // invalidate current timers
   305  		if pd.wd > 0 && !combo {
   306  			modtimer(&pd.wt, pd.wd, 0, netpollWriteDeadline, pd.makeArg(), pd.wseq)
   307  		} else {
   308  			deltimer(&pd.wt)
   309  			pd.wt.f = nil
   310  		}
   311  	}
   312  	// If we set the new deadline in the past, unblock currently pending IO if any.
   313  	var rg, wg *g
   314  	if pd.rd < 0 || pd.wd < 0 {
   315  		atomic.StorepNoWB(noescape(unsafe.Pointer(&wg)), nil) // full memory barrier between stores to rd/wd and load of rg/wg in netpollunblock
   316  		if pd.rd < 0 {
   317  			rg = netpollunblock(pd, 'r', false)
   318  		}
   319  		if pd.wd < 0 {
   320  			wg = netpollunblock(pd, 'w', false)
   321  		}
   322  	}
   323  	unlock(&pd.lock)
   324  	if rg != nil {
   325  		netpollgoready(rg, 3)
   326  	}
   327  	if wg != nil {
   328  		netpollgoready(wg, 3)
   329  	}
   330  }
   331  
   332  //go:linkname poll_runtime_pollUnblock internal/poll.runtime_pollUnblock
   333  func poll_runtime_pollUnblock(pd *pollDesc) {
   334  	lock(&pd.lock)
   335  	if pd.closing {
   336  		throw("runtime: unblock on closing polldesc")
   337  	}
   338  	pd.closing = true
   339  	pd.rseq++
   340  	pd.wseq++
   341  	var rg, wg *g
   342  	atomic.StorepNoWB(noescape(unsafe.Pointer(&rg)), nil) // full memory barrier between store to closing and read of rg/wg in netpollunblock
   343  	rg = netpollunblock(pd, 'r', false)
   344  	wg = netpollunblock(pd, 'w', false)
   345  	if pd.rt.f != nil {
   346  		deltimer(&pd.rt)
   347  		pd.rt.f = nil
   348  	}
   349  	if pd.wt.f != nil {
   350  		deltimer(&pd.wt)
   351  		pd.wt.f = nil
   352  	}
   353  	unlock(&pd.lock)
   354  	if rg != nil {
   355  		netpollgoready(rg, 3)
   356  	}
   357  	if wg != nil {
   358  		netpollgoready(wg, 3)
   359  	}
   360  }
   361  
   362  // netpollready is called by the platform-specific netpoll function.
   363  // It declares that the fd associated with pd is ready for I/O.
   364  // The toRun argument is used to build a list of goroutines to return
   365  // from netpoll. The mode argument is 'r', 'w', or 'r'+'w' to indicate
   366  // whether the fd is ready for reading or writing or both.
   367  //
   368  // This may run while the world is stopped, so write barriers are not allowed.
   369  //go:nowritebarrier
   370  func netpollready(toRun *gList, pd *pollDesc, mode int32) {
   371  	var rg, wg *g
   372  	if mode == 'r' || mode == 'r'+'w' {
   373  		rg = netpollunblock(pd, 'r', true)
   374  	}
   375  	if mode == 'w' || mode == 'r'+'w' {
   376  		wg = netpollunblock(pd, 'w', true)
   377  	}
   378  	if rg != nil {
   379  		toRun.push(rg)
   380  	}
   381  	if wg != nil {
   382  		toRun.push(wg)
   383  	}
   384  }
   385  
   386  func netpollcheckerr(pd *pollDesc, mode int32) int {
   387  	if pd.closing {
   388  		return pollErrClosing
   389  	}
   390  	if (mode == 'r' && pd.rd < 0) || (mode == 'w' && pd.wd < 0) {
   391  		return pollErrTimeout
   392  	}
   393  	// Report an event scanning error only on a read event.
   394  	// An error on a write event will be captured in a subsequent
   395  	// write call that is able to report a more specific error.
   396  	if mode == 'r' && pd.everr {
   397  		return pollErrNotPollable
   398  	}
   399  	return pollNoError
   400  }
   401  
   402  func netpollblockcommit(gp *g, gpp unsafe.Pointer) bool {
   403  	r := atomic.Casuintptr((*uintptr)(gpp), pdWait, uintptr(unsafe.Pointer(gp)))
   404  	if r {
   405  		// Bump the count of goroutines waiting for the poller.
   406  		// The scheduler uses this to decide whether to block
   407  		// waiting for the poller if there is nothing else to do.
   408  		atomic.Xadd(&netpollWaiters, 1)
   409  	}
   410  	return r
   411  }
   412  
   413  func netpollgoready(gp *g, traceskip int) {
   414  	atomic.Xadd(&netpollWaiters, -1)
   415  	goready(gp, traceskip+1)
   416  }
   417  
   418  // returns true if IO is ready, or false if timedout or closed
   419  // waitio - wait only for completed IO, ignore errors
   420  func netpollblock(pd *pollDesc, mode int32, waitio bool) bool {
   421  	gpp := &pd.rg
   422  	if mode == 'w' {
   423  		gpp = &pd.wg
   424  	}
   425  
   426  	// set the gpp semaphore to pdWait
   427  	for {
   428  		old := *gpp
   429  		if old == pdReady {
   430  			*gpp = 0
   431  			return true
   432  		}
   433  		if old != 0 {
   434  			throw("runtime: double wait")
   435  		}
   436  		if atomic.Casuintptr(gpp, 0, pdWait) {
   437  			break
   438  		}
   439  	}
   440  
   441  	// need to recheck error states after setting gpp to pdWait
   442  	// this is necessary because runtime_pollUnblock/runtime_pollSetDeadline/deadlineimpl
   443  	// do the opposite: store to closing/rd/wd, membarrier, load of rg/wg
   444  	if waitio || netpollcheckerr(pd, mode) == 0 {
   445  		gopark(netpollblockcommit, unsafe.Pointer(gpp), waitReasonIOWait, traceEvGoBlockNet, 5)
   446  	}
   447  	// be careful to not lose concurrent pdReady notification
   448  	old := atomic.Xchguintptr(gpp, 0)
   449  	if old > pdWait {
   450  		throw("runtime: corrupted polldesc")
   451  	}
   452  	return old == pdReady
   453  }
   454  
   455  func netpollunblock(pd *pollDesc, mode int32, ioready bool) *g {
   456  	gpp := &pd.rg
   457  	if mode == 'w' {
   458  		gpp = &pd.wg
   459  	}
   460  
   461  	for {
   462  		old := *gpp
   463  		if old == pdReady {
   464  			return nil
   465  		}
   466  		if old == 0 && !ioready {
   467  			// Only set pdReady for ioready. runtime_pollWait
   468  			// will check for timeout/cancel before waiting.
   469  			return nil
   470  		}
   471  		var new uintptr
   472  		if ioready {
   473  			new = pdReady
   474  		}
   475  		if atomic.Casuintptr(gpp, old, new) {
   476  			if old == pdWait {
   477  				old = 0
   478  			}
   479  			return (*g)(unsafe.Pointer(old))
   480  		}
   481  	}
   482  }
   483  
   484  func netpolldeadlineimpl(pd *pollDesc, seq uintptr, read, write bool) {
   485  	lock(&pd.lock)
   486  	// Seq arg is seq when the timer was set.
   487  	// If it's stale, ignore the timer event.
   488  	currentSeq := pd.rseq
   489  	if !read {
   490  		currentSeq = pd.wseq
   491  	}
   492  	if seq != currentSeq {
   493  		// The descriptor was reused or timers were reset.
   494  		unlock(&pd.lock)
   495  		return
   496  	}
   497  	var rg *g
   498  	if read {
   499  		if pd.rd <= 0 || pd.rt.f == nil {
   500  			throw("runtime: inconsistent read deadline")
   501  		}
   502  		pd.rd = -1
   503  		atomic.StorepNoWB(unsafe.Pointer(&pd.rt.f), nil) // full memory barrier between store to rd and load of rg in netpollunblock
   504  		rg = netpollunblock(pd, 'r', false)
   505  	}
   506  	var wg *g
   507  	if write {
   508  		if pd.wd <= 0 || pd.wt.f == nil && !read {
   509  			throw("runtime: inconsistent write deadline")
   510  		}
   511  		pd.wd = -1
   512  		atomic.StorepNoWB(unsafe.Pointer(&pd.wt.f), nil) // full memory barrier between store to wd and load of wg in netpollunblock
   513  		wg = netpollunblock(pd, 'w', false)
   514  	}
   515  	unlock(&pd.lock)
   516  	if rg != nil {
   517  		netpollgoready(rg, 0)
   518  	}
   519  	if wg != nil {
   520  		netpollgoready(wg, 0)
   521  	}
   522  }
   523  
   524  func netpollDeadline(arg interface{}, seq uintptr) {
   525  	netpolldeadlineimpl(arg.(*pollDesc), seq, true, true)
   526  }
   527  
   528  func netpollReadDeadline(arg interface{}, seq uintptr) {
   529  	netpolldeadlineimpl(arg.(*pollDesc), seq, true, false)
   530  }
   531  
   532  func netpollWriteDeadline(arg interface{}, seq uintptr) {
   533  	netpolldeadlineimpl(arg.(*pollDesc), seq, false, true)
   534  }
   535  
   536  func (c *pollCache) alloc() *pollDesc {
   537  	lock(&c.lock)
   538  	if c.first == nil {
   539  		const pdSize = unsafe.Sizeof(pollDesc{})
   540  		n := pollBlockSize / pdSize
   541  		if n == 0 {
   542  			n = 1
   543  		}
   544  		// Must be in non-GC memory because can be referenced
   545  		// only from epoll/kqueue internals.
   546  		mem := persistentalloc(n*pdSize, 0, &memstats.other_sys)
   547  		for i := uintptr(0); i < n; i++ {
   548  			pd := (*pollDesc)(add(mem, i*pdSize))
   549  			pd.link = c.first
   550  			c.first = pd
   551  		}
   552  	}
   553  	pd := c.first
   554  	c.first = pd.link
   555  	lockInit(&pd.lock, lockRankPollDesc)
   556  	unlock(&c.lock)
   557  	return pd
   558  }
   559  
   560  // makeArg converts pd to an interface{}.
   561  // makeArg does not do any allocation. Normally, such
   562  // a conversion requires an allocation because pointers to
   563  // go:notinheap types (which pollDesc is) must be stored
   564  // in interfaces indirectly. See issue 42076.
   565  func (pd *pollDesc) makeArg() (i interface{}) {
   566  	x := (*eface)(unsafe.Pointer(&i))
   567  	x._type = pdType
   568  	x.data = unsafe.Pointer(&pd.self)
   569  	return
   570  }
   571  
   572  var (
   573  	pdEface interface{} = (*pollDesc)(nil)
   574  	pdType  *_type      = efaceOf(&pdEface)._type
   575  )
   576  

View as plain text