Print this page
    
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/uts/common/io/devpoll.c
          +++ new/usr/src/uts/common/io/devpoll.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22   22   * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
  24   24   */
  25   25  
  26   26  /*
  27   27   * Copyright (c) 2012 by Delphix. All rights reserved.
  28   28   * Copyright 2016 Joyent, Inc.
  29   29   */
  30   30  
  31   31  #include <sys/types.h>
  32   32  #include <sys/devops.h>
  33   33  #include <sys/conf.h>
  34   34  #include <sys/modctl.h>
  35   35  #include <sys/sunddi.h>
  36   36  #include <sys/stat.h>
  37   37  #include <sys/poll_impl.h>
  38   38  #include <sys/errno.h>
  39   39  #include <sys/kmem.h>
  40   40  #include <sys/mkdev.h>
  41   41  #include <sys/debug.h>
  42   42  #include <sys/file.h>
  43   43  #include <sys/sysmacros.h>
  44   44  #include <sys/systm.h>
  45   45  #include <sys/bitmap.h>
  46   46  #include <sys/devpoll.h>
  47   47  #include <sys/rctl.h>
  48   48  #include <sys/resource.h>
  49   49  #include <sys/schedctl.h>
  50   50  #include <sys/epoll.h>
  51   51  
  52   52  #define RESERVED        1
  53   53  
  54   54  /* local data struct */
  55   55  static  dp_entry_t      **devpolltbl;   /* dev poll entries */
  56   56  static  size_t          dptblsize;
  57   57  
  58   58  static  kmutex_t        devpoll_lock;   /* lock protecting dev tbl */
  59   59  int                     devpoll_init;   /* is /dev/poll initialized already */
  60   60  
  61   61  /* device local functions */
  62   62  
  63   63  static int dpopen(dev_t *devp, int flag, int otyp, cred_t *credp);
  64   64  static int dpwrite(dev_t dev, struct uio *uiop, cred_t *credp);
  65   65  static int dpioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
  66   66      int *rvalp);
  67   67  static int dppoll(dev_t dev, short events, int anyyet, short *reventsp,
  68   68      struct pollhead **phpp);
  69   69  static int dpclose(dev_t dev, int flag, int otyp, cred_t *credp);
  70   70  static dev_info_t *dpdevi;
  71   71  
  72   72  
  73   73  static struct cb_ops    dp_cb_ops = {
  74   74          dpopen,                 /* open */
  75   75          dpclose,                /* close */
  76   76          nodev,                  /* strategy */
  77   77          nodev,                  /* print */
  78   78          nodev,                  /* dump */
  79   79          nodev,                  /* read */
  80   80          dpwrite,                /* write */
  81   81          dpioctl,                /* ioctl */
  82   82          nodev,                  /* devmap */
  83   83          nodev,                  /* mmap */
  84   84          nodev,                  /* segmap */
  85   85          dppoll,                 /* poll */
  86   86          ddi_prop_op,            /* prop_op */
  87   87          (struct streamtab *)0,  /* streamtab */
  88   88          D_MP,                   /* flags */
  89   89          CB_REV,                 /* cb_ops revision */
  90   90          nodev,                  /* aread */
  91   91          nodev                   /* awrite */
  92   92  };
  93   93  
  94   94  static int dpattach(dev_info_t *, ddi_attach_cmd_t);
  95   95  static int dpdetach(dev_info_t *, ddi_detach_cmd_t);
  96   96  static int dpinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
  97   97  
  98   98  static struct dev_ops dp_ops = {
  99   99          DEVO_REV,               /* devo_rev */
 100  100          0,                      /* refcnt */
 101  101          dpinfo,                 /* info */
 102  102          nulldev,                /* identify */
 103  103          nulldev,                /* probe */
 104  104          dpattach,               /* attach */
 105  105          dpdetach,               /* detach */
 106  106          nodev,                  /* reset */
 107  107          &dp_cb_ops,             /* driver operations */
 108  108          (struct bus_ops *)NULL, /* bus operations */
 109  109          nulldev,                /* power */
 110  110          ddi_quiesce_not_needed,         /* quiesce */
 111  111  };
 112  112  
 113  113  
 114  114  static struct modldrv modldrv = {
 115  115          &mod_driverops,         /* type of module - a driver */
 116  116          "/dev/poll driver",
 117  117          &dp_ops,
 118  118  };
 119  119  
 120  120  static struct modlinkage modlinkage = {
 121  121          MODREV_1,
 122  122          (void *)&modldrv,
 123  123          NULL
 124  124  };
 125  125  
 126  126  static void pcachelink_assoc(pollcache_t *, pollcache_t *);
 127  127  static void pcachelink_mark_stale(pollcache_t *);
 128  128  static void pcachelink_purge_stale(pollcache_t *);
 129  129  static void pcachelink_purge_all(pollcache_t *);
 130  130  
 131  131  
 132  132  /*
 133  133   * Locking Design
 134  134   *
 135  135   * The /dev/poll driver shares most of its code with poll sys call whose
 136  136   * code is in common/syscall/poll.c. In poll(2) design, the pollcache
 137  137   * structure is per lwp. An implicit assumption is made there that some
 138  138   * portion of pollcache will never be touched by other lwps. E.g., in
 139  139   * poll(2) design, no lwp will ever need to grow bitmap of other lwp.
 140  140   * This assumption is not true for /dev/poll; hence the need for extra
 141  141   * locking.
 142  142   *
 143  143   * To allow more parallelism, each /dev/poll file descriptor (indexed by
 144  144   * minor number) has its own lock. Since read (dpioctl) is a much more
 145  145   * frequent operation than write, we want to allow multiple reads on same
 146  146   * /dev/poll fd. However, we prevent writes from being starved by giving
 147  147   * priority to write operation. Theoretically writes can starve reads as
 148  148   * well. But in practical sense this is not important because (1) writes
 149  149   * happens less often than reads, and (2) write operation defines the
 150  150   * content of poll fd a cache set. If writes happens so often that they
 151  151   * can starve reads, that means the cached set is very unstable. It may
 152  152   * not make sense to read an unstable cache set anyway. Therefore, the
 153  153   * writers starving readers case is not handled in this design.
 154  154   */
 155  155  
 156  156  int
 157  157  _init()
 158  158  {
 159  159          int     error;
 160  160  
 161  161          dptblsize = DEVPOLLSIZE;
 162  162          devpolltbl = kmem_zalloc(sizeof (caddr_t) * dptblsize, KM_SLEEP);
 163  163          mutex_init(&devpoll_lock, NULL, MUTEX_DEFAULT, NULL);
 164  164          devpoll_init = 1;
 165  165          if ((error = mod_install(&modlinkage)) != 0) {
 166  166                  kmem_free(devpolltbl, sizeof (caddr_t) * dptblsize);
 167  167                  devpoll_init = 0;
 168  168          }
 169  169          return (error);
 170  170  }
 171  171  
 172  172  int
 173  173  _fini()
 174  174  {
 175  175          int error;
 176  176  
 177  177          if ((error = mod_remove(&modlinkage)) != 0) {
 178  178                  return (error);
 179  179          }
 180  180          mutex_destroy(&devpoll_lock);
 181  181          kmem_free(devpolltbl, sizeof (caddr_t) * dptblsize);
 182  182          return (0);
 183  183  }
 184  184  
 185  185  int
 186  186  _info(struct modinfo *modinfop)
 187  187  {
 188  188          return (mod_info(&modlinkage, modinfop));
 189  189  }
 190  190  
 191  191  /*ARGSUSED*/
 192  192  static int
 193  193  dpattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
 194  194  {
 195  195          if (ddi_create_minor_node(devi, "poll", S_IFCHR, 0, DDI_PSEUDO, NULL)
 196  196              == DDI_FAILURE) {
 197  197                  ddi_remove_minor_node(devi, NULL);
 198  198                  return (DDI_FAILURE);
 199  199          }
 200  200          dpdevi = devi;
 201  201          return (DDI_SUCCESS);
 202  202  }
 203  203  
 204  204  static int
 205  205  dpdetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
 206  206  {
 207  207          if (cmd != DDI_DETACH)
 208  208                  return (DDI_FAILURE);
 209  209  
 210  210          ddi_remove_minor_node(devi, NULL);
 211  211          return (DDI_SUCCESS);
 212  212  }
 213  213  
 214  214  /* ARGSUSED */
 215  215  static int
 216  216  dpinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
 217  217  {
 218  218          int error;
 219  219  
 220  220          switch (infocmd) {
 221  221          case DDI_INFO_DEVT2DEVINFO:
 222  222                  *result = (void *)dpdevi;
 223  223                  error = DDI_SUCCESS;
 224  224                  break;
 225  225          case DDI_INFO_DEVT2INSTANCE:
 226  226                  *result = (void *)0;
 227  227                  error = DDI_SUCCESS;
 228  228                  break;
 229  229          default:
 230  230                  error = DDI_FAILURE;
 231  231          }
 232  232          return (error);
 233  233  }
 234  234  
 235  235  /*
 236  236   * dp_pcache_poll has similar logic to pcache_poll() in poll.c. The major
 237  237   * differences are: (1) /dev/poll requires scanning the bitmap starting at
 238  238   * where it was stopped last time, instead of always starting from 0,
 239  239   * (2) since user may not have cleaned up the cached fds when they are
 240  240   * closed, some polldats in cache may refer to closed or reused fds. We
 241  241   * need to check for those cases.
 242  242   *
 243  243   * NOTE: Upon closing an fd, automatic poll cache cleanup is done for
 244  244   *       poll(2) caches but NOT for /dev/poll caches. So expect some
 245  245   *       stale entries!
 246  246   */
 247  247  static int
 248  248  dp_pcache_poll(dp_entry_t *dpep, void *dpbuf,
 249  249      pollcache_t *pcp, nfds_t nfds, int *fdcntp)
 250  250  {
 251  251          int             start, ostart, end;
 252  252          int             fdcnt, fd;
 253  253          boolean_t       done;
 254  254          file_t          *fp;
 255  255          short           revent;
 256  256          boolean_t       no_wrap;
 257  257          pollhead_t      *php;
 258  258          polldat_t       *pdp;
 259  259          pollfd_t        *pfdp;
 260  260          epoll_event_t   *epoll;
 261  261          int             error = 0;
 262  262          short           mask = POLLRDHUP | POLLWRBAND;
 263  263          boolean_t       is_epoll = (dpep->dpe_flag & DP_ISEPOLLCOMPAT) != 0;
 264  264  
 265  265          ASSERT(MUTEX_HELD(&pcp->pc_lock));
 266  266          if (pcp->pc_bitmap == NULL) {
 267  267                  /*
 268  268                   * No Need to search because no poll fd
 269  269                   * has been cached.
 270  270                   */
 271  271                  return (error);
 272  272          }
 273  273  
 274  274          if (is_epoll) {
 275  275                  pfdp = NULL;
 276  276                  epoll = (epoll_event_t *)dpbuf;
 277  277          } else {
 278  278                  pfdp = (pollfd_t *)dpbuf;
 279  279                  epoll = NULL;
 280  280          }
 281  281  retry:
 282  282          start = ostart = pcp->pc_mapstart;
 283  283          end = pcp->pc_mapend;
 284  284          php = NULL;
 285  285  
 286  286          if (start == 0) {
 287  287                  /*
 288  288                   * started from every begining, no need to wrap around.
 289  289                   */
 290  290                  no_wrap = B_TRUE;
 291  291          } else {
 292  292                  no_wrap = B_FALSE;
 293  293          }
 294  294          done = B_FALSE;
 295  295          fdcnt = 0;
 296  296          while ((fdcnt < nfds) && !done) {
 297  297                  php = NULL;
 298  298                  revent = 0;
 299  299                  /*
 300  300                   * Examine the bit map in a circular fashion
 301  301                   * to avoid starvation. Always resume from
 302  302                   * last stop. Scan till end of the map. Then
 303  303                   * wrap around.
 304  304                   */
 305  305                  fd = bt_getlowbit(pcp->pc_bitmap, start, end);
 306  306                  ASSERT(fd <= end);
 307  307                  if (fd >= 0) {
 308  308                          if (fd == end) {
 309  309                                  if (no_wrap) {
 310  310                                          done = B_TRUE;
 311  311                                  } else {
 312  312                                          start = 0;
 313  313                                          end = ostart - 1;
 314  314                                          no_wrap = B_TRUE;
 315  315                                  }
 316  316                          } else {
 317  317                                  start = fd + 1;
 318  318                          }
 319  319                          pdp = pcache_lookup_fd(pcp, fd);
 320  320  repoll:
 321  321                          ASSERT(pdp != NULL);
 322  322                          ASSERT(pdp->pd_fd == fd);
 323  323                          if (pdp->pd_fp == NULL) {
 324  324                                  /*
 325  325                                   * The fd is POLLREMOVed. This fd is
 326  326                                   * logically no longer cached. So move
 327  327                                   * on to the next one.
 328  328                                   */
 329  329                                  continue;
 330  330                          }
 331  331                          if ((fp = getf(fd)) == NULL) {
 332  332                                  /*
 333  333                                   * The fd has been closed, but user has not
 334  334                                   * done a POLLREMOVE on this fd yet. Instead
 335  335                                   * of cleaning it here implicitly, we return
 336  336                                   * POLLNVAL. This is consistent with poll(2)
 337  337                                   * polling a closed fd. Hope this will remind
 338  338                                   * user to do a POLLREMOVE.
 339  339                                   */
 340  340                                  if (!is_epoll && pfdp != NULL) {
 341  341                                          pfdp[fdcnt].fd = fd;
 342  342                                          pfdp[fdcnt].revents = POLLNVAL;
 343  343                                          fdcnt++;
 344  344                                          continue;
 345  345                                  }
 346  346  
 347  347                                  /*
 348  348                                   * In the epoll compatibility case, we actually
 349  349                                   * perform the implicit removal to remain
 350  350                                   * closer to the epoll semantics.
 351  351                                   */
 352  352                                  if (is_epoll) {
 353  353                                          pdp->pd_fp = NULL;
 354  354                                          pdp->pd_events = 0;
 355  355  
 356  356                                          if (pdp->pd_php != NULL) {
 357  357                                                  pollhead_delete(pdp->pd_php,
 358  358                                                      pdp);
 359  359                                                  pdp->pd_php = NULL;
 360  360                                          }
 361  361  
 362  362                                          BT_CLEAR(pcp->pc_bitmap, fd);
 363  363                                          continue;
 364  364                                  }
 365  365                          }
 366  366  
 367  367                          if (fp != pdp->pd_fp) {
 368  368                                  /*
 369  369                                   * user is polling on a cached fd which was
 370  370                                   * closed and then reused. Unfortunately
 371  371                                   * there is no good way to inform user.
 372  372                                   * If the file struct is also reused, we
 373  373                                   * may not be able to detect the fd reuse
 374  374                                   * at all.  As long as this does not
 375  375                                   * cause system failure and/or memory leak,
 376  376                                   * we will play along. Man page states if
 377  377                                   * user does not clean up closed fds, polling
 378  378                                   * results will be indeterministic.
 379  379                                   *
 380  380                                   * XXX - perhaps log the detection of fd
 381  381                                   *       reuse?
 382  382                                   */
 383  383                                  pdp->pd_fp = fp;
 384  384                          }
 385  385                          /*
 386  386                           * XXX - pollrelock() logic needs to know which
 387  387                           * which pollcache lock to grab. It'd be a
 388  388                           * cleaner solution if we could pass pcp as
 389  389                           * an arguement in VOP_POLL interface instead
 390  390                           * of implicitly passing it using thread_t
 391  391                           * struct. On the other hand, changing VOP_POLL
 392  392                           * interface will require all driver/file system
 393  393                           * poll routine to change. May want to revisit
 394  394                           * the tradeoff later.
 395  395                           */
 396  396                          curthread->t_pollcache = pcp;
 397  397                          error = VOP_POLL(fp->f_vnode, pdp->pd_events, 0,
 398  398                              &revent, &php, NULL);
 399  399                          curthread->t_pollcache = NULL;
 400  400                          releasef(fd);
 401  401                          if (error != 0) {
 402  402                                  break;
 403  403                          }
 404  404  
 405  405                          /*
 406  406                           * layered devices (e.g. console driver)
 407  407                           * may change the vnode and thus the pollhead
 408  408                           * pointer out from underneath us.
 409  409                           */
 410  410                          if (php != NULL && pdp->pd_php != NULL &&
 411  411                              php != pdp->pd_php) {
 412  412                                  pollhead_delete(pdp->pd_php, pdp);
 413  413                                  pdp->pd_php = php;
 414  414                                  pollhead_insert(php, pdp);
 415  415                                  /*
 416  416                                   * The bit should still be set.
 417  417                                   */
 418  418                                  ASSERT(BT_TEST(pcp->pc_bitmap, fd));
 419  419                                  goto retry;
 420  420                          }
 421  421  
 422  422                          if (revent != 0) {
 423  423                                  if (pfdp != NULL) {
 424  424                                          pfdp[fdcnt].fd = fd;
 425  425                                          pfdp[fdcnt].events = pdp->pd_events;
 426  426                                          pfdp[fdcnt].revents = revent;
 427  427                                  } else if (epoll != NULL) {
 428  428                                          epoll_event_t *ep = &epoll[fdcnt];
 429  429  
 430  430                                          ASSERT(epoll != NULL);
 431  431                                          ep->data.u64 = pdp->pd_epolldata;
 432  432  
 433  433                                          /*
 434  434                                           * If any of the event bits are set for
 435  435                                           * which poll and epoll representations
 436  436                                           * differ, swizzle in the native epoll
 437  437                                           * values.
 438  438                                           */
 439  439                                          if (revent & mask) {
 440  440                                                  ep->events = (revent & ~mask) |
 441  441                                                      ((revent & POLLRDHUP) ?
 442  442                                                      EPOLLRDHUP : 0) |
 443  443                                                      ((revent & POLLWRBAND) ?
 444  444                                                      EPOLLWRBAND : 0);
 445  445                                          } else {
 446  446                                                  ep->events = revent;
 447  447                                          }
 448  448  
 449  449                                          /*
 450  450                                           * We define POLLWRNORM to be POLLOUT,
 451  451                                           * but epoll has separate definitions
 452  452                                           * for them; if POLLOUT is set and the
 453  453                                           * user has asked for EPOLLWRNORM, set
 454  454                                           * that as well.
 455  455                                           */
 456  456                                          if ((revent & POLLOUT) &&
 457  457                                              (pdp->pd_events & EPOLLWRNORM)) {
 458  458                                                  ep->events |= EPOLLWRNORM;
 459  459                                          }
 460  460                                  } else {
 461  461                                          pollstate_t *ps =
 462  462                                              curthread->t_pollstate;
 463  463                                          /*
 464  464                                           * The devpoll handle itself is being
 465  465                                           * polled.  Notify the caller of any
 466  466                                           * readable event(s), leaving as much
 467  467                                           * state as possible untouched.
 468  468                                           */
 469  469                                          VERIFY(fdcnt == 0);
 470  470                                          VERIFY(ps != NULL);
 471  471  
 472  472                                          /*
 473  473                                           * If a call to pollunlock() fails
 474  474                                           * during VOP_POLL, skip over the fd
 475  475                                           * and continue polling.
 476  476                                           *
 477  477                                           * Otherwise, report that there is an
 478  478                                           * event pending.
 479  479                                           */
 480  480                                          if ((ps->ps_flags & POLLSTATE_ULFAIL)
 481  481                                              != 0) {
 482  482                                                  ps->ps_flags &=
 483  483                                                      ~POLLSTATE_ULFAIL;
 484  484                                                  continue;
 485  485                                          } else {
 486  486                                                  fdcnt++;
 487  487                                                  break;
 488  488                                          }
 489  489                                  }
 490  490  
 491  491                                  /*
 492  492                                   * If POLLET is set, clear the bit in the
 493  493                                   * bitmap -- which effectively latches the
 494  494                                   * edge on a pollwakeup() from the driver.
 495  495                                   */
 496  496                                  if (pdp->pd_events & POLLET)
 497  497                                          BT_CLEAR(pcp->pc_bitmap, fd);
 498  498  
 499  499                                  /*
 500  500                                   * If POLLONESHOT is set, perform the implicit
 501  501                                   * POLLREMOVE.
 502  502                                   */
 503  503                                  if (pdp->pd_events & POLLONESHOT) {
 504  504                                          pdp->pd_fp = NULL;
 505  505                                          pdp->pd_events = 0;
 506  506  
 507  507                                          if (pdp->pd_php != NULL) {
 508  508                                                  pollhead_delete(pdp->pd_php,
 509  509                                                      pdp);
 510  510                                                  pdp->pd_php = NULL;
 511  511                                          }
 512  512  
 513  513                                          BT_CLEAR(pcp->pc_bitmap, fd);
 514  514                                  }
 515  515  
 516  516                                  fdcnt++;
 517  517                          } else if (php != NULL) {
 518  518                                  /*
 519  519                                   * We clear a bit or cache a poll fd if
 520  520                                   * the driver returns a poll head ptr,
 521  521                                   * which is expected in the case of 0
 522  522                                   * revents. Some buggy driver may return
 523  523                                   * NULL php pointer with 0 revents. In
 524  524                                   * this case, we just treat the driver as
 525  525                                   * "noncachable" and not clearing the bit
 526  526                                   * in bitmap.
 527  527                                   */
 528  528                                  if ((pdp->pd_php != NULL) &&
 529  529                                      ((pcp->pc_flag & PC_POLLWAKE) == 0)) {
 530  530                                          BT_CLEAR(pcp->pc_bitmap, fd);
 531  531                                  }
 532  532                                  if (pdp->pd_php == NULL) {
 533  533                                          pollhead_insert(php, pdp);
 534  534                                          pdp->pd_php = php;
 535  535                                          /*
 536  536                                           * An event of interest may have
 537  537                                           * arrived between the VOP_POLL() and
 538  538                                           * the pollhead_insert(); check again.
 539  539                                           */
 540  540                                          goto repoll;
 541  541                                  }
 542  542                          }
 543  543                  } else {
 544  544                          /*
 545  545                           * No bit set in the range. Check for wrap around.
 546  546                           */
 547  547                          if (!no_wrap) {
 548  548                                  start = 0;
 549  549                                  end = ostart - 1;
 550  550                                  no_wrap = B_TRUE;
 551  551                          } else {
 552  552                                  done = B_TRUE;
 553  553                          }
 554  554                  }
 555  555          }
 556  556  
 557  557          if (!done) {
 558  558                  pcp->pc_mapstart = start;
 559  559          }
 560  560          ASSERT(*fdcntp == 0);
 561  561          *fdcntp = fdcnt;
 562  562          return (error);
 563  563  }
 564  564  
 565  565  /*ARGSUSED*/
 566  566  static int
 567  567  dpopen(dev_t *devp, int flag, int otyp, cred_t *credp)
 568  568  {
 569  569          minor_t         minordev;
 570  570          dp_entry_t      *dpep;
 571  571          pollcache_t     *pcp;
 572  572  
 573  573          ASSERT(devpoll_init);
 574  574          ASSERT(dptblsize <= MAXMIN);
 575  575          mutex_enter(&devpoll_lock);
 576  576          for (minordev = 0; minordev < dptblsize; minordev++) {
 577  577                  if (devpolltbl[minordev] == NULL) {
 578  578                          devpolltbl[minordev] = (dp_entry_t *)RESERVED;
 579  579                          break;
 580  580                  }
 581  581          }
 582  582          if (minordev == dptblsize) {
 583  583                  dp_entry_t      **newtbl;
 584  584                  size_t          oldsize;
 585  585  
 586  586                  /*
 587  587                   * Used up every entry in the existing devpoll table.
 588  588                   * Grow the table by DEVPOLLSIZE.
 589  589                   */
 590  590                  if ((oldsize = dptblsize) >= MAXMIN) {
 591  591                          mutex_exit(&devpoll_lock);
 592  592                          return (ENXIO);
 593  593                  }
 594  594                  dptblsize += DEVPOLLSIZE;
 595  595                  if (dptblsize > MAXMIN) {
 596  596                          dptblsize = MAXMIN;
 597  597                  }
 598  598                  newtbl = kmem_zalloc(sizeof (caddr_t) * dptblsize, KM_SLEEP);
 599  599                  bcopy(devpolltbl, newtbl, sizeof (caddr_t) * oldsize);
 600  600                  kmem_free(devpolltbl, sizeof (caddr_t) * oldsize);
 601  601                  devpolltbl = newtbl;
 602  602                  devpolltbl[minordev] = (dp_entry_t *)RESERVED;
 603  603          }
 604  604          mutex_exit(&devpoll_lock);
 605  605  
 606  606          dpep = kmem_zalloc(sizeof (dp_entry_t), KM_SLEEP);
 607  607          /*
 608  608           * allocate a pollcache skeleton here. Delay allocating bitmap
 609  609           * structures until dpwrite() time, since we don't know the
 610  610           * optimal size yet.  We also delay setting the pid until either
 611  611           * dpwrite() or attempt to poll on the instance, allowing parents
 612  612           * to create instances of /dev/poll for their children.  (In the
 613  613           * epoll compatibility case, this check isn't performed to maintain
 614  614           * semantic compatibility.)
 615  615           */
 616  616          pcp = pcache_alloc();
 617  617          dpep->dpe_pcache = pcp;
 618  618          pcp->pc_pid = -1;
 619  619          *devp = makedevice(getmajor(*devp), minordev);  /* clone the driver */
 620  620          mutex_enter(&devpoll_lock);
 621  621          ASSERT(minordev < dptblsize);
 622  622          ASSERT(devpolltbl[minordev] == (dp_entry_t *)RESERVED);
 623  623          devpolltbl[minordev] = dpep;
 624  624          mutex_exit(&devpoll_lock);
 625  625          return (0);
 626  626  }
 627  627  
 628  628  /*
 629  629   * Write to dev/poll add/remove fd's to/from a cached poll fd set,
 630  630   * or change poll events for a watched fd.
 631  631   */
 632  632  /*ARGSUSED*/
 633  633  static int
 634  634  dpwrite(dev_t dev, struct uio *uiop, cred_t *credp)
 635  635  {
 636  636          minor_t         minor;
 637  637          dp_entry_t      *dpep;
 638  638          pollcache_t     *pcp;
 639  639          pollfd_t        *pollfdp, *pfdp;
 640  640          dvpoll_epollfd_t *epfdp;
 641  641          uintptr_t       limit;
 642  642          int             error, size;
 643  643          ssize_t         uiosize;
 644  644          size_t          copysize;
 645  645          nfds_t          pollfdnum;
 646  646          struct pollhead *php = NULL;
 647  647          polldat_t       *pdp;
 648  648          int             fd;
 649  649          file_t          *fp;
 650  650          boolean_t       is_epoll, fds_added = B_FALSE;
 651  651  
 652  652          minor = getminor(dev);
 653  653  
 654  654          mutex_enter(&devpoll_lock);
 655  655          ASSERT(minor < dptblsize);
 656  656          dpep = devpolltbl[minor];
 657  657          ASSERT(dpep != NULL);
 658  658          mutex_exit(&devpoll_lock);
 659  659  
 660  660          mutex_enter(&dpep->dpe_lock);
 661  661          pcp = dpep->dpe_pcache;
 662  662          is_epoll = (dpep->dpe_flag & DP_ISEPOLLCOMPAT) != 0;
 663  663          size = (is_epoll) ? sizeof (dvpoll_epollfd_t) : sizeof (pollfd_t);
 664  664          mutex_exit(&dpep->dpe_lock);
 665  665  
 666  666          if (!is_epoll && curproc->p_pid != pcp->pc_pid) {
 667  667                  if (pcp->pc_pid != -1) {
  
    | 
      ↓ open down ↓ | 
    667 lines elided | 
    
      ↑ open up ↑ | 
  
 668  668                          return (EACCES);
 669  669                  }
 670  670  
 671  671                  pcp->pc_pid = curproc->p_pid;
 672  672          }
 673  673  
 674  674          uiosize = uiop->uio_resid;
 675  675          pollfdnum = uiosize / size;
 676  676  
 677  677          /*
 678      -         * For epoll-enabled handles, restrict the allowed write size to 2.
 679      -         * This corresponds to an epoll_ctl(3C) performing an EPOLL_CTL_MOD
 680      -         * operation which is expanded into two operations (DEL and ADD).
 681      -         *
 682      -         * All other operations performed through epoll_ctl(3C) will consist of
 683      -         * a single entry.
 684      -         */
 685      -        if (is_epoll && pollfdnum > 2) {
 686      -                return (EINVAL);
 687      -        }
 688      -
 689      -        /*
 690  678           * We want to make sure that pollfdnum isn't large enough to DoS us,
 691  679           * but we also don't want to grab p_lock unnecessarily -- so we
 692  680           * perform the full check against our resource limits if and only if
 693  681           * pollfdnum is larger than the known-to-be-sane value of UINT8_MAX.
 694  682           */
 695  683          if (pollfdnum > UINT8_MAX) {
 696  684                  mutex_enter(&curproc->p_lock);
 697  685                  if (pollfdnum >
 698  686                      (uint_t)rctl_enforced_value(rctlproc_legacy[RLIMIT_NOFILE],
 699  687                      curproc->p_rctls, curproc)) {
 700  688                          (void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
 701  689                              curproc->p_rctls, curproc, RCA_SAFE);
 702  690                          mutex_exit(&curproc->p_lock);
 703  691                          return (EINVAL);
 704  692                  }
 705  693                  mutex_exit(&curproc->p_lock);
 706  694          }
 707  695  
 708  696          /*
 709  697           * Copy in the pollfd array.  Walk through the array and add
 710  698           * each polled fd to the cached set.
 711  699           */
 712  700          pollfdp = kmem_alloc(uiosize, KM_SLEEP);
 713  701          limit = (uintptr_t)pollfdp + (pollfdnum * size);
 714  702  
 715  703          /*
 716  704           * Although /dev/poll uses the write(2) interface to cache fds, it's
 717  705           * not supposed to function as a seekable device. To prevent offset
 718  706           * from growing and eventually exceed the maximum, reset the offset
 719  707           * here for every call.
 720  708           */
 721  709          uiop->uio_loffset = 0;
 722  710  
 723  711          /*
 724  712           * Use uiocopy instead of uiomove when populating pollfdp, keeping
 725  713           * uio_resid untouched for now.  Write syscalls will translate EINTR
 726  714           * into a success if they detect "successfully transfered" data via an
 727  715           * updated uio_resid.  Falsely suppressing such errors is disastrous.
 728  716           */
 729  717          if ((error = uiocopy((caddr_t)pollfdp, uiosize, UIO_WRITE, uiop,
 730  718              ©size)) != 0) {
 731  719                  kmem_free(pollfdp, uiosize);
 732  720                  return (error);
 733  721          }
 734  722  
 735  723          /*
 736  724           * We are about to enter the core portion of dpwrite(). Make sure this
 737  725           * write has exclusive access in this portion of the code, i.e., no
  
    | 
      ↓ open down ↓ | 
    38 lines elided | 
    
      ↑ open up ↑ | 
  
 738  726           * other writers in this code.
 739  727           *
 740  728           * Waiting for all readers to drop their references to the dpe is
 741  729           * unecessary since the pollcache itself is protected by pc_lock.
 742  730           */
 743  731          mutex_enter(&dpep->dpe_lock);
 744  732          dpep->dpe_writerwait++;
 745  733          while ((dpep->dpe_flag & DP_WRITER_PRESENT) != 0) {
 746  734                  ASSERT(dpep->dpe_refcnt != 0);
 747  735  
 748      -                /*
 749      -                 * The epoll API does not allow EINTR as a result when making
 750      -                 * modifications to the set of polled fds.  Given that write
 751      -                 * activity is relatively quick and the size of accepted writes
 752      -                 * is limited above to two entries, a signal-ignorant wait is
 753      -                 * used here to avoid the EINTR.
 754      -                 */
 755      -                if (is_epoll) {
 756      -                        cv_wait(&dpep->dpe_cv, &dpep->dpe_lock);
 757      -                        continue;
 758      -                }
 759      -
 760      -                /*
 761      -                 * Non-epoll writers to /dev/poll handles can tolerate EINTR.
 762      -                 */
 763  736                  if (!cv_wait_sig_swap(&dpep->dpe_cv, &dpep->dpe_lock)) {
 764  737                          dpep->dpe_writerwait--;
 765  738                          mutex_exit(&dpep->dpe_lock);
 766  739                          kmem_free(pollfdp, uiosize);
 767  740                          return (EINTR);
 768  741                  }
 769  742          }
 770  743          dpep->dpe_writerwait--;
 771  744          dpep->dpe_flag |= DP_WRITER_PRESENT;
 772  745          dpep->dpe_refcnt++;
 773  746  
 774  747          if (!is_epoll && (dpep->dpe_flag & DP_ISEPOLLCOMPAT) != 0) {
 775  748                  /*
 776  749                   * The epoll compat mode was enabled while we were waiting to
 777  750                   * establish write access. It is not safe to continue since
 778  751                   * state was prepared for non-epoll operation.
 779  752                   */
 780  753                  error = EBUSY;
 781  754                  goto bypass;
 782  755          }
 783  756          mutex_exit(&dpep->dpe_lock);
 784  757  
 785  758          /*
 786  759           * Since the dpwrite() may recursively walk an added /dev/poll handle,
 787  760           * pollstate_enter() deadlock and loop detection must be used.
 788  761           */
 789  762          (void) pollstate_create();
 790  763          VERIFY(pollstate_enter(pcp) == PSE_SUCCESS);
 791  764  
 792  765          if (pcp->pc_bitmap == NULL) {
 793  766                  pcache_create(pcp, pollfdnum);
 794  767          }
 795  768          for (pfdp = pollfdp; (uintptr_t)pfdp < limit;
 796  769              pfdp = (pollfd_t *)((uintptr_t)pfdp + size)) {
 797  770                  fd = pfdp->fd;
 798  771                  if ((uint_t)fd >= P_FINFO(curproc)->fi_nfiles) {
 799  772                          /*
 800  773                           * epoll semantics demand that we return EBADF if our
 801  774                           * specified fd is invalid.
 802  775                           */
 803  776                          if (is_epoll) {
 804  777                                  error = EBADF;
 805  778                                  break;
 806  779                          }
 807  780  
 808  781                          continue;
 809  782                  }
 810  783  
 811  784                  pdp = pcache_lookup_fd(pcp, fd);
 812  785                  if (pfdp->events != POLLREMOVE) {
 813  786  
 814  787                          fp = NULL;
 815  788  
 816  789                          if (pdp == NULL) {
 817  790                                  /*
 818  791                                   * If we're in epoll compatibility mode, check
 819  792                                   * that the fd is valid before allocating
 820  793                                   * anything for it; epoll semantics demand that
 821  794                                   * we return EBADF if our specified fd is
 822  795                                   * invalid.
 823  796                                   */
 824  797                                  if (is_epoll) {
 825  798                                          if ((fp = getf(fd)) == NULL) {
 826  799                                                  error = EBADF;
 827  800                                                  break;
 828  801                                          }
 829  802                                  }
 830  803  
 831  804                                  pdp = pcache_alloc_fd(0);
 832  805                                  pdp->pd_fd = fd;
 833  806                                  pdp->pd_pcache = pcp;
 834  807                                  pcache_insert_fd(pcp, pdp, pollfdnum);
 835  808                          } else {
 836  809                                  /*
 837  810                                   * epoll semantics demand that we error out if
 838  811                                   * a file descriptor is added twice, which we
 839  812                                   * check (imperfectly) by checking if we both
 840  813                                   * have the file descriptor cached and the
 841  814                                   * file pointer that correponds to the file
 842  815                                   * descriptor matches our cached value.  If
 843  816                                   * there is a pointer mismatch, the file
 844  817                                   * descriptor was closed without being removed.
 845  818                                   * The converse is clearly not true, however,
 846  819                                   * so to narrow the window by which a spurious
 847  820                                   * EEXIST may be returned, we also check if
 848  821                                   * this fp has been added to an epoll control
 849  822                                   * descriptor in the past; if it hasn't, we
 850  823                                   * know that this is due to fp reuse -- it's
 851  824                                   * not a true EEXIST case.  (By performing this
 852  825                                   * additional check, we limit the window of
 853  826                                   * spurious EEXIST to situations where a single
 854  827                                   * file descriptor is being used across two or
 855  828                                   * more epoll control descriptors -- and even
 856  829                                   * then, the file descriptor must be closed and
 857  830                                   * reused in a relatively tight time span.)
 858  831                                   */
 859  832                                  if (is_epoll) {
 860  833                                          if (pdp->pd_fp != NULL &&
 861  834                                              (fp = getf(fd)) != NULL &&
 862  835                                              fp == pdp->pd_fp &&
 863  836                                              (fp->f_flag2 & FEPOLLED)) {
 864  837                                                  error = EEXIST;
 865  838                                                  releasef(fd);
 866  839                                                  break;
 867  840                                          }
 868  841  
 869  842                                          /*
 870  843                                           * We have decided that the cached
 871  844                                           * information was stale: it either
 872  845                                           * didn't match, or the fp had never
 873  846                                           * actually been epoll()'d on before.
 874  847                                           * We need to now clear our pd_events
 875  848                                           * to assure that we don't mistakenly
 876  849                                           * operate on cached event disposition.
 877  850                                           */
 878  851                                          pdp->pd_events = 0;
 879  852                                  }
 880  853                          }
 881  854  
 882  855                          if (is_epoll) {
 883  856                                  epfdp = (dvpoll_epollfd_t *)pfdp;
 884  857                                  pdp->pd_epolldata = epfdp->dpep_data;
 885  858                          }
 886  859  
 887  860                          ASSERT(pdp->pd_fd == fd);
 888  861                          ASSERT(pdp->pd_pcache == pcp);
 889  862                          if (fd >= pcp->pc_mapsize) {
 890  863                                  mutex_exit(&pcp->pc_lock);
 891  864                                  pcache_grow_map(pcp, fd);
 892  865                                  mutex_enter(&pcp->pc_lock);
 893  866                          }
 894  867                          if (fd > pcp->pc_mapend) {
 895  868                                  pcp->pc_mapend = fd;
 896  869                          }
 897  870                          if (fp == NULL && (fp = getf(fd)) == NULL) {
 898  871                                  /*
 899  872                                   * The fd is not valid. Since we can't pass
 900  873                                   * this error back in the write() call, set
 901  874                                   * the bit in bitmap to force DP_POLL ioctl
 902  875                                   * to examine it.
 903  876                                   */
 904  877                                  BT_SET(pcp->pc_bitmap, fd);
 905  878                                  pdp->pd_events |= pfdp->events;
 906  879                                  continue;
 907  880                          }
 908  881  
 909  882                          /*
 910  883                           * To (greatly) reduce EEXIST false positives, we
 911  884                           * denote that this fp has been epoll()'d.  We do this
 912  885                           * regardless of epoll compatibility mode, as the flag
 913  886                           * is harmless if not in epoll compatibility mode.
 914  887                           */
 915  888                          fp->f_flag2 |= FEPOLLED;
 916  889  
 917  890                          /*
 918  891                           * Don't do VOP_POLL for an already cached fd with
 919  892                           * same poll events.
 920  893                           */
 921  894                          if ((pdp->pd_events == pfdp->events) &&
 922  895                              (pdp->pd_fp == fp)) {
 923  896                                  /*
 924  897                                   * the events are already cached
 925  898                                   */
 926  899                                  releasef(fd);
 927  900                                  continue;
 928  901                          }
 929  902  
 930  903                          /*
 931  904                           * do VOP_POLL and cache this poll fd.
 932  905                           */
 933  906                          /*
 934  907                           * XXX - pollrelock() logic needs to know which
 935  908                           * which pollcache lock to grab. It'd be a
 936  909                           * cleaner solution if we could pass pcp as
 937  910                           * an arguement in VOP_POLL interface instead
 938  911                           * of implicitly passing it using thread_t
 939  912                           * struct. On the other hand, changing VOP_POLL
 940  913                           * interface will require all driver/file system
 941  914                           * poll routine to change. May want to revisit
 942  915                           * the tradeoff later.
 943  916                           */
 944  917                          curthread->t_pollcache = pcp;
 945  918                          error = VOP_POLL(fp->f_vnode, pfdp->events, 0,
 946  919                              &pfdp->revents, &php, NULL);
 947  920                          curthread->t_pollcache = NULL;
 948  921                          /*
 949  922                           * We always set the bit when this fd is cached;
 950  923                           * this forces the first DP_POLL to poll this fd.
 951  924                           * Real performance gain comes from subsequent
 952  925                           * DP_POLL.  We also attempt a pollhead_insert();
 953  926                           * if it's not possible, we'll do it in dpioctl().
 954  927                           */
 955  928                          BT_SET(pcp->pc_bitmap, fd);
 956  929                          if (error != 0) {
 957  930                                  releasef(fd);
 958  931                                  break;
 959  932                          }
 960  933                          pdp->pd_fp = fp;
 961  934                          pdp->pd_events |= pfdp->events;
 962  935                          if (php != NULL) {
 963  936                                  if (pdp->pd_php == NULL) {
 964  937                                          pollhead_insert(php, pdp);
 965  938                                          pdp->pd_php = php;
 966  939                                  } else {
 967  940                                          if (pdp->pd_php != php) {
 968  941                                                  pollhead_delete(pdp->pd_php,
 969  942                                                      pdp);
 970  943                                                  pollhead_insert(php, pdp);
 971  944                                                  pdp->pd_php = php;
 972  945                                          }
 973  946                                  }
 974  947                          }
 975  948                          fds_added = B_TRUE;
 976  949                          releasef(fd);
 977  950                  } else {
 978  951                          if (pdp == NULL || pdp->pd_fp == NULL) {
 979  952                                  if (is_epoll) {
 980  953                                          /*
 981  954                                           * As with the add case (above), epoll
 982  955                                           * semantics demand that we error out
 983  956                                           * in this case.
 984  957                                           */
 985  958                                          error = ENOENT;
 986  959                                          break;
 987  960                                  }
 988  961  
 989  962                                  continue;
 990  963                          }
 991  964                          ASSERT(pdp->pd_fd == fd);
 992  965                          pdp->pd_fp = NULL;
 993  966                          pdp->pd_events = 0;
 994  967                          ASSERT(pdp->pd_thread == NULL);
 995  968                          if (pdp->pd_php != NULL) {
 996  969                                  pollhead_delete(pdp->pd_php, pdp);
 997  970                                  pdp->pd_php = NULL;
 998  971                          }
 999  972                          BT_CLEAR(pcp->pc_bitmap, fd);
1000  973                  }
1001  974          }
1002  975          /*
1003  976           * Wake any pollcache waiters so they can check the new descriptors.
1004  977           *
1005  978           * Any fds added to an recursive-capable pollcache could themselves be
1006  979           * /dev/poll handles. To ensure that proper event propagation occurs,
1007  980           * parent pollcaches are woken too, so that they can create any needed
1008  981           * pollcache links.
1009  982           */
1010  983          if (fds_added) {
1011  984                  cv_broadcast(&pcp->pc_cv);
1012  985                  pcache_wake_parents(pcp);
1013  986          }
1014  987          pollstate_exit(pcp);
1015  988          mutex_enter(&dpep->dpe_lock);
1016  989  bypass:
1017  990          dpep->dpe_flag &= ~DP_WRITER_PRESENT;
1018  991          dpep->dpe_refcnt--;
1019  992          cv_broadcast(&dpep->dpe_cv);
1020  993          mutex_exit(&dpep->dpe_lock);
1021  994          kmem_free(pollfdp, uiosize);
1022  995          if (error == 0) {
1023  996                  /*
1024  997                   * The state of uio_resid is updated only after the pollcache
1025  998                   * is successfully modified.
1026  999                   */
1027 1000                  uioskip(uiop, copysize);
1028 1001          }
1029 1002          return (error);
1030 1003  }
1031 1004  
1032 1005  #define DP_SIGMASK_RESTORE(ksetp) {                                     \
1033 1006          if (ksetp != NULL) {                                            \
1034 1007                  mutex_enter(&p->p_lock);                                \
1035 1008                  if (lwp->lwp_cursig == 0) {                             \
1036 1009                          t->t_hold = lwp->lwp_sigoldmask;                \
1037 1010                          t->t_flag &= ~T_TOMASK;                         \
1038 1011                  }                                                       \
1039 1012                  mutex_exit(&p->p_lock);                                 \
1040 1013          }                                                               \
1041 1014  }
1042 1015  
1043 1016  /*ARGSUSED*/
1044 1017  static int
1045 1018  dpioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
1046 1019  {
1047 1020          minor_t         minor;
1048 1021          dp_entry_t      *dpep;
1049 1022          pollcache_t     *pcp;
1050 1023          hrtime_t        now;
1051 1024          int             error = 0;
1052 1025          boolean_t       is_epoll;
1053 1026          STRUCT_DECL(dvpoll, dvpoll);
1054 1027  
1055 1028          if (cmd == DP_POLL || cmd == DP_PPOLL) {
1056 1029                  /* do this now, before we sleep on DP_WRITER_PRESENT */
1057 1030                  now = gethrtime();
1058 1031          }
1059 1032  
1060 1033          minor = getminor(dev);
1061 1034          mutex_enter(&devpoll_lock);
1062 1035          ASSERT(minor < dptblsize);
1063 1036          dpep = devpolltbl[minor];
1064 1037          mutex_exit(&devpoll_lock);
1065 1038          ASSERT(dpep != NULL);
1066 1039          pcp = dpep->dpe_pcache;
1067 1040  
1068 1041          mutex_enter(&dpep->dpe_lock);
1069 1042          is_epoll = (dpep->dpe_flag & DP_ISEPOLLCOMPAT) != 0;
1070 1043  
1071 1044          if (cmd == DP_EPOLLCOMPAT) {
1072 1045                  if (dpep->dpe_refcnt != 0) {
1073 1046                          /*
1074 1047                           * We can't turn on epoll compatibility while there
1075 1048                           * are outstanding operations.
1076 1049                           */
1077 1050                          mutex_exit(&dpep->dpe_lock);
1078 1051                          return (EBUSY);
1079 1052                  }
1080 1053  
1081 1054                  /*
1082 1055                   * epoll compatibility is a one-way street: there's no way
1083 1056                   * to turn it off for a particular open.
1084 1057                   */
1085 1058                  dpep->dpe_flag |= DP_ISEPOLLCOMPAT;
1086 1059                  mutex_exit(&dpep->dpe_lock);
1087 1060  
1088 1061                  return (0);
1089 1062          }
1090 1063  
1091 1064          if (!is_epoll && curproc->p_pid != pcp->pc_pid) {
1092 1065                  if (pcp->pc_pid != -1) {
1093 1066                          mutex_exit(&dpep->dpe_lock);
1094 1067                          return (EACCES);
1095 1068                  }
1096 1069  
1097 1070                  pcp->pc_pid = curproc->p_pid;
1098 1071          }
1099 1072  
1100 1073          /* Wait until all writers have cleared the handle before continuing */
1101 1074          while ((dpep->dpe_flag & DP_WRITER_PRESENT) != 0 ||
1102 1075              (dpep->dpe_writerwait != 0)) {
1103 1076                  if (!cv_wait_sig_swap(&dpep->dpe_cv, &dpep->dpe_lock)) {
1104 1077                          mutex_exit(&dpep->dpe_lock);
1105 1078                          return (EINTR);
1106 1079                  }
1107 1080          }
1108 1081          dpep->dpe_refcnt++;
1109 1082          mutex_exit(&dpep->dpe_lock);
1110 1083  
1111 1084          switch (cmd) {
1112 1085          case    DP_POLL:
1113 1086          case    DP_PPOLL:
1114 1087          {
1115 1088                  pollstate_t     *ps;
1116 1089                  nfds_t          nfds;
1117 1090                  int             fdcnt = 0;
1118 1091                  size_t          size, fdsize, dpsize;
1119 1092                  hrtime_t        deadline = 0;
1120 1093                  k_sigset_t      *ksetp = NULL;
1121 1094                  k_sigset_t      kset;
1122 1095                  sigset_t        set;
1123 1096                  kthread_t       *t = curthread;
1124 1097                  klwp_t          *lwp = ttolwp(t);
1125 1098                  struct proc     *p = ttoproc(curthread);
1126 1099  
1127 1100                  STRUCT_INIT(dvpoll, mode);
1128 1101  
1129 1102                  /*
1130 1103                   * The dp_setp member is only required/consumed for DP_PPOLL,
1131 1104                   * which otherwise uses the same structure as DP_POLL.
1132 1105                   */
1133 1106                  if (cmd == DP_POLL) {
1134 1107                          dpsize = (uintptr_t)STRUCT_FADDR(dvpoll, dp_setp) -
1135 1108                              (uintptr_t)STRUCT_FADDR(dvpoll, dp_fds);
1136 1109                  } else {
1137 1110                          ASSERT(cmd == DP_PPOLL);
1138 1111                          dpsize = STRUCT_SIZE(dvpoll);
1139 1112                  }
1140 1113  
1141 1114                  if ((mode & FKIOCTL) != 0) {
1142 1115                          /* Kernel-internal ioctl call */
1143 1116                          bcopy((caddr_t)arg, STRUCT_BUF(dvpoll), dpsize);
1144 1117                          error = 0;
1145 1118                  } else {
1146 1119                          error = copyin((caddr_t)arg, STRUCT_BUF(dvpoll),
1147 1120                              dpsize);
1148 1121                  }
1149 1122  
1150 1123                  if (error) {
1151 1124                          DP_REFRELE(dpep);
1152 1125                          return (EFAULT);
1153 1126                  }
1154 1127  
1155 1128                  deadline = STRUCT_FGET(dvpoll, dp_timeout);
1156 1129                  if (deadline > 0) {
1157 1130                          /*
1158 1131                           * Convert the deadline from relative milliseconds
1159 1132                           * to absolute nanoseconds.  They must wait for at
1160 1133                           * least a tick.
1161 1134                           */
1162 1135                          deadline = MSEC2NSEC(deadline);
1163 1136                          deadline = MAX(deadline, nsec_per_tick);
1164 1137                          deadline += now;
1165 1138                  }
1166 1139  
1167 1140                  if (cmd == DP_PPOLL) {
1168 1141                          void *setp = STRUCT_FGETP(dvpoll, dp_setp);
1169 1142  
1170 1143                          if (setp != NULL) {
1171 1144                                  if ((mode & FKIOCTL) != 0) {
1172 1145                                          /* Use the signal set directly */
1173 1146                                          ksetp = (k_sigset_t *)setp;
1174 1147                                  } else {
1175 1148                                          if (copyin(setp, &set, sizeof (set))) {
1176 1149                                                  DP_REFRELE(dpep);
1177 1150                                                  return (EFAULT);
1178 1151                                          }
1179 1152                                          sigutok(&set, &kset);
1180 1153                                          ksetp = &kset;
1181 1154                                  }
1182 1155  
1183 1156                                  mutex_enter(&p->p_lock);
1184 1157                                  schedctl_finish_sigblock(t);
1185 1158                                  lwp->lwp_sigoldmask = t->t_hold;
1186 1159                                  t->t_hold = *ksetp;
1187 1160                                  t->t_flag |= T_TOMASK;
1188 1161  
1189 1162                                  /*
1190 1163                                   * Like ppoll() with a non-NULL sigset, we'll
1191 1164                                   * call cv_reltimedwait_sig() just to check for
1192 1165                                   * signals.  This call will return immediately
1193 1166                                   * with either 0 (signalled) or -1 (no signal).
1194 1167                                   * There are some conditions whereby we can
1195 1168                                   * get 0 from cv_reltimedwait_sig() without
1196 1169                                   * a true signal (e.g., a directed stop), so
1197 1170                                   * we restore our signal mask in the unlikely
1198 1171                                   * event that lwp_cursig is 0.
1199 1172                                   */
1200 1173                                  if (!cv_reltimedwait_sig(&t->t_delay_cv,
1201 1174                                      &p->p_lock, 0, TR_CLOCK_TICK)) {
1202 1175                                          if (lwp->lwp_cursig == 0) {
1203 1176                                                  t->t_hold = lwp->lwp_sigoldmask;
1204 1177                                                  t->t_flag &= ~T_TOMASK;
1205 1178                                          }
1206 1179  
1207 1180                                          mutex_exit(&p->p_lock);
1208 1181  
1209 1182                                          DP_REFRELE(dpep);
1210 1183                                          return (EINTR);
1211 1184                                  }
1212 1185  
1213 1186                                  mutex_exit(&p->p_lock);
1214 1187                          }
1215 1188                  }
1216 1189  
1217 1190                  if ((nfds = STRUCT_FGET(dvpoll, dp_nfds)) == 0) {
1218 1191                          /*
1219 1192                           * We are just using DP_POLL to sleep, so
1220 1193                           * we don't any of the devpoll apparatus.
1221 1194                           * Do not check for signals if we have a zero timeout.
1222 1195                           */
1223 1196                          DP_REFRELE(dpep);
1224 1197                          if (deadline == 0) {
1225 1198                                  DP_SIGMASK_RESTORE(ksetp);
1226 1199                                  return (0);
1227 1200                          }
1228 1201  
1229 1202                          mutex_enter(&curthread->t_delay_lock);
1230 1203                          while ((error =
1231 1204                              cv_timedwait_sig_hrtime(&curthread->t_delay_cv,
1232 1205                              &curthread->t_delay_lock, deadline)) > 0)
1233 1206                                  continue;
1234 1207                          mutex_exit(&curthread->t_delay_lock);
1235 1208  
1236 1209                          DP_SIGMASK_RESTORE(ksetp);
1237 1210  
1238 1211                          return (error == 0 ? EINTR : 0);
1239 1212                  }
1240 1213  
1241 1214                  if (is_epoll) {
1242 1215                          size = nfds * (fdsize = sizeof (epoll_event_t));
1243 1216                  } else {
1244 1217                          size = nfds * (fdsize = sizeof (pollfd_t));
1245 1218                  }
1246 1219  
1247 1220                  /*
1248 1221                   * XXX It would be nice not to have to alloc each time, but it
1249 1222                   * requires another per thread structure hook. This can be
1250 1223                   * implemented later if data suggests that it's necessary.
1251 1224                   */
1252 1225                  ps = pollstate_create();
1253 1226  
1254 1227                  if (ps->ps_dpbufsize < size) {
1255 1228                          /*
1256 1229                           * If nfds is larger than twice the current maximum
1257 1230                           * open file count, we'll silently clamp it.  This
1258 1231                           * only limits our exposure to allocating an
1259 1232                           * inordinate amount of kernel memory; it doesn't
1260 1233                           * otherwise affect the semantics.  (We have this
1261 1234                           * check at twice the maximum instead of merely the
1262 1235                           * maximum because some applications pass an nfds that
1263 1236                           * is only slightly larger than their limit.)
1264 1237                           */
1265 1238                          mutex_enter(&p->p_lock);
1266 1239                          if ((nfds >> 1) > p->p_fno_ctl) {
1267 1240                                  nfds = p->p_fno_ctl;
1268 1241                                  size = nfds * fdsize;
1269 1242                          }
1270 1243                          mutex_exit(&p->p_lock);
1271 1244  
1272 1245                          if (ps->ps_dpbufsize < size) {
1273 1246                                  kmem_free(ps->ps_dpbuf, ps->ps_dpbufsize);
1274 1247                                  ps->ps_dpbuf = kmem_zalloc(size, KM_SLEEP);
1275 1248                                  ps->ps_dpbufsize = size;
1276 1249                          }
1277 1250                  }
1278 1251  
1279 1252                  VERIFY(pollstate_enter(pcp) == PSE_SUCCESS);
1280 1253                  for (;;) {
1281 1254                          pcp->pc_flag &= ~PC_POLLWAKE;
1282 1255  
1283 1256                          /*
1284 1257                           * Mark all child pcachelinks as stale.
1285 1258                           * Those which are still part of the tree will be
1286 1259                           * marked as valid during the poll.
1287 1260                           */
1288 1261                          pcachelink_mark_stale(pcp);
1289 1262  
1290 1263                          error = dp_pcache_poll(dpep, ps->ps_dpbuf,
1291 1264                              pcp, nfds, &fdcnt);
1292 1265                          if (fdcnt > 0 || error != 0)
1293 1266                                  break;
1294 1267  
1295 1268                          /* Purge still-stale child pcachelinks */
1296 1269                          pcachelink_purge_stale(pcp);
1297 1270  
1298 1271                          /*
1299 1272                           * A pollwake has happened since we polled cache.
1300 1273                           */
1301 1274                          if (pcp->pc_flag & PC_POLLWAKE)
1302 1275                                  continue;
1303 1276  
1304 1277                          /*
1305 1278                           * Sleep until we are notified, signaled, or timed out.
1306 1279                           */
1307 1280                          if (deadline == 0) {
1308 1281                                  /* immediate timeout; do not check signals */
1309 1282                                  break;
1310 1283                          }
1311 1284  
1312 1285                          error = cv_timedwait_sig_hrtime(&pcp->pc_cv,
1313 1286                              &pcp->pc_lock, deadline);
1314 1287  
1315 1288                          /*
1316 1289                           * If we were awakened by a signal or timeout then
1317 1290                           * break the loop, else poll again.
1318 1291                           */
1319 1292                          if (error <= 0) {
1320 1293                                  error = (error == 0) ? EINTR : 0;
1321 1294                                  break;
1322 1295                          } else {
1323 1296                                  error = 0;
1324 1297                          }
1325 1298                  }
1326 1299                  pollstate_exit(pcp);
1327 1300  
1328 1301                  DP_SIGMASK_RESTORE(ksetp);
1329 1302  
1330 1303                  if (error == 0 && fdcnt > 0) {
1331 1304                          /*
1332 1305                           * It should be noted that FKIOCTL does not influence
1333 1306                           * the copyout (vs bcopy) of dp_fds at this time.
1334 1307                           */
1335 1308                          if (copyout(ps->ps_dpbuf,
1336 1309                              STRUCT_FGETP(dvpoll, dp_fds), fdcnt * fdsize)) {
1337 1310                                  DP_REFRELE(dpep);
1338 1311                                  return (EFAULT);
1339 1312                          }
1340 1313                          *rvalp = fdcnt;
1341 1314                  }
1342 1315                  break;
1343 1316          }
1344 1317  
1345 1318          case    DP_ISPOLLED:
1346 1319          {
1347 1320                  pollfd_t        pollfd;
1348 1321                  polldat_t       *pdp;
1349 1322  
1350 1323                  STRUCT_INIT(dvpoll, mode);
1351 1324                  error = copyin((caddr_t)arg, &pollfd, sizeof (pollfd_t));
1352 1325                  if (error) {
1353 1326                          DP_REFRELE(dpep);
1354 1327                          return (EFAULT);
1355 1328                  }
1356 1329                  mutex_enter(&pcp->pc_lock);
1357 1330                  if (pcp->pc_hash == NULL) {
1358 1331                          /*
1359 1332                           * No Need to search because no poll fd
1360 1333                           * has been cached.
1361 1334                           */
1362 1335                          mutex_exit(&pcp->pc_lock);
1363 1336                          DP_REFRELE(dpep);
1364 1337                          return (0);
1365 1338                  }
1366 1339                  if (pollfd.fd < 0) {
1367 1340                          mutex_exit(&pcp->pc_lock);
1368 1341                          break;
1369 1342                  }
1370 1343                  pdp = pcache_lookup_fd(pcp, pollfd.fd);
1371 1344                  if ((pdp != NULL) && (pdp->pd_fd == pollfd.fd) &&
1372 1345                      (pdp->pd_fp != NULL)) {
1373 1346                          pollfd.revents = pdp->pd_events;
1374 1347                          if (copyout(&pollfd, (caddr_t)arg, sizeof (pollfd_t))) {
1375 1348                                  mutex_exit(&pcp->pc_lock);
1376 1349                                  DP_REFRELE(dpep);
1377 1350                                  return (EFAULT);
1378 1351                          }
1379 1352                          *rvalp = 1;
1380 1353                  }
1381 1354                  mutex_exit(&pcp->pc_lock);
1382 1355                  break;
1383 1356          }
1384 1357  
1385 1358          default:
1386 1359                  DP_REFRELE(dpep);
1387 1360                  return (EINVAL);
1388 1361          }
1389 1362          DP_REFRELE(dpep);
1390 1363          return (error);
1391 1364  }
1392 1365  
1393 1366  /*
1394 1367   * Overview of Recursive Polling
1395 1368   *
1396 1369   * It is possible for /dev/poll to poll for events on file descriptors which
1397 1370   * themselves are /dev/poll handles.  Pending events in the child handle are
1398 1371   * represented as readable data via the POLLIN flag.  To limit surface area,
1399 1372   * this recursion is presently allowed on only /dev/poll handles which have
1400 1373   * been placed in epoll mode via the DP_EPOLLCOMPAT ioctl.  Recursion depth is
1401 1374   * limited to 5 in order to be consistent with Linux epoll.
1402 1375   *
1403 1376   * Extending dppoll() for VOP_POLL:
1404 1377   *
1405 1378   * The recursive /dev/poll implementation begins by extending dppoll() to
1406 1379   * report when resources contained in the pollcache have relevant event state.
1407 1380   * At the highest level, it means calling dp_pcache_poll() so it indicates if
1408 1381   * fd events are present without consuming them or altering the pollcache
1409 1382   * bitmap.  This ensures that a subsequent DP_POLL operation on the bitmap will
1410 1383   * yield the initiating event.  Additionally, the VOP_POLL should return in
1411 1384   * such a way that dp_pcache_poll() does not clear the parent bitmap entry
1412 1385   * which corresponds to the child /dev/poll fd.  This means that child
1413 1386   * pollcaches will be checked during every poll which facilitates wake-up
1414 1387   * behavior detailed below.
1415 1388   *
1416 1389   * Pollcache Links and Wake Events:
1417 1390   *
1418 1391   * Recursive /dev/poll avoids complicated pollcache locking constraints during
1419 1392   * pollwakeup events by eschewing the traditional pollhead mechanism in favor
1420 1393   * of a different approach.  For each pollcache at the root of a recursive
1421 1394   * /dev/poll "tree", pcachelink_t structures are established to all child
1422 1395   * /dev/poll pollcaches.  During pollnotify() in a child pollcache, the
1423 1396   * linked list of pcachelink_t entries is walked, where those marked as valid
1424 1397   * incur a cv_broadcast to their parent pollcache.  Most notably, these
1425 1398   * pcachelink_t cv wakeups are performed without acquiring pc_lock on the
1426 1399   * parent pollcache (which would require careful deadlock avoidance).  This
1427 1400   * still allows the woken poll on the parent to discover the pertinent events
1428 1401   * due to the fact that bitmap entires for the child pollcache are always
1429 1402   * maintained by the dppoll() logic above.
1430 1403   *
1431 1404   * Depth Limiting and Loop Prevention:
1432 1405   *
1433 1406   * As each pollcache is encountered (either via DP_POLL or dppoll()), depth and
1434 1407   * loop constraints are enforced via pollstate_enter().  The pollcache_t
1435 1408   * pointer is compared against any existing entries in ps_pc_stack and is added
1436 1409   * to the end if no match (and therefore loop) is found.  Once poll operations
1437 1410   * for a given pollcache_t are complete, pollstate_exit() clears the pointer
1438 1411   * from the list.  The pollstate_enter() and pollstate_exit() functions are
1439 1412   * responsible for acquiring and releasing pc_lock, respectively.
1440 1413   *
1441 1414   * Deadlock Safety:
1442 1415   *
1443 1416   * Descending through a tree of recursive /dev/poll handles involves the tricky
1444 1417   * business of sequentially entering multiple pollcache locks.  This tree
1445 1418   * topology cannot define a lock acquisition order in such a way that it is
1446 1419   * immune to deadlocks between threads.  The pollstate_enter() and
1447 1420   * pollstate_exit() functions provide an interface for recursive /dev/poll
1448 1421   * operations to safely lock pollcaches while failing gracefully in the face of
1449 1422   * deadlocking topologies. (See pollstate_contend() for more detail about how
1450 1423   * deadlocks are detected and resolved.)
1451 1424   */
1452 1425  
1453 1426  /*ARGSUSED*/
1454 1427  static int
1455 1428  dppoll(dev_t dev, short events, int anyyet, short *reventsp,
1456 1429      struct pollhead **phpp)
1457 1430  {
1458 1431          minor_t         minor;
1459 1432          dp_entry_t      *dpep;
1460 1433          pollcache_t     *pcp;
1461 1434          int             res, rc = 0;
1462 1435  
1463 1436          minor = getminor(dev);
1464 1437          mutex_enter(&devpoll_lock);
1465 1438          ASSERT(minor < dptblsize);
1466 1439          dpep = devpolltbl[minor];
1467 1440          ASSERT(dpep != NULL);
1468 1441          mutex_exit(&devpoll_lock);
1469 1442  
1470 1443          mutex_enter(&dpep->dpe_lock);
1471 1444          if ((dpep->dpe_flag & DP_ISEPOLLCOMPAT) == 0) {
1472 1445                  /* Poll recursion is not yet supported for non-epoll handles */
1473 1446                  *reventsp = POLLERR;
1474 1447                  mutex_exit(&dpep->dpe_lock);
1475 1448                  return (0);
1476 1449          } else {
1477 1450                  dpep->dpe_refcnt++;
1478 1451                  pcp = dpep->dpe_pcache;
1479 1452                  mutex_exit(&dpep->dpe_lock);
1480 1453          }
1481 1454  
1482 1455          res = pollstate_enter(pcp);
1483 1456          if (res == PSE_SUCCESS) {
1484 1457                  nfds_t          nfds = 1;
1485 1458                  int             fdcnt = 0;
1486 1459                  pollstate_t     *ps = curthread->t_pollstate;
1487 1460  
1488 1461                  rc = dp_pcache_poll(dpep, NULL, pcp, nfds, &fdcnt);
1489 1462                  if (rc == 0) {
1490 1463                          *reventsp = (fdcnt > 0) ? POLLIN : 0;
1491 1464                  }
1492 1465                  pcachelink_assoc(pcp, ps->ps_pc_stack[0]);
1493 1466                  pollstate_exit(pcp);
1494 1467          } else {
1495 1468                  switch (res) {
1496 1469                  case PSE_FAIL_DEPTH:
1497 1470                          rc = EINVAL;
1498 1471                          break;
1499 1472                  case PSE_FAIL_LOOP:
1500 1473                  case PSE_FAIL_DEADLOCK:
1501 1474                          rc = ELOOP;
1502 1475                          break;
1503 1476                  default:
1504 1477                          /*
1505 1478                           * If anything else has gone awry, such as being polled
1506 1479                           * from an unexpected context, fall back to the
1507 1480                           * recursion-intolerant response.
1508 1481                           */
1509 1482                          *reventsp = POLLERR;
1510 1483                          rc = 0;
1511 1484                          break;
1512 1485                  }
1513 1486          }
1514 1487  
1515 1488          DP_REFRELE(dpep);
1516 1489          return (rc);
1517 1490  }
1518 1491  
1519 1492  /*
1520 1493   * devpoll close should do enough clean up before the pollcache is deleted,
1521 1494   * i.e., it should ensure no one still references the pollcache later.
1522 1495   * There is no "permission" check in here. Any process having the last
1523 1496   * reference of this /dev/poll fd can close.
1524 1497   */
1525 1498  /*ARGSUSED*/
1526 1499  static int
1527 1500  dpclose(dev_t dev, int flag, int otyp, cred_t *credp)
1528 1501  {
1529 1502          minor_t         minor;
1530 1503          dp_entry_t      *dpep;
1531 1504          pollcache_t     *pcp;
1532 1505          int             i;
1533 1506          polldat_t       **hashtbl;
1534 1507          polldat_t       *pdp;
1535 1508  
1536 1509          minor = getminor(dev);
1537 1510  
1538 1511          mutex_enter(&devpoll_lock);
1539 1512          dpep = devpolltbl[minor];
1540 1513          ASSERT(dpep != NULL);
1541 1514          devpolltbl[minor] = NULL;
1542 1515          mutex_exit(&devpoll_lock);
1543 1516          pcp = dpep->dpe_pcache;
1544 1517          ASSERT(pcp != NULL);
1545 1518          /*
1546 1519           * At this point, no other lwp can access this pollcache via the
1547 1520           * /dev/poll fd. This pollcache is going away, so do the clean
1548 1521           * up without the pc_lock.
1549 1522           */
1550 1523          hashtbl = pcp->pc_hash;
1551 1524          for (i = 0; i < pcp->pc_hashsize; i++) {
1552 1525                  for (pdp = hashtbl[i]; pdp; pdp = pdp->pd_hashnext) {
1553 1526                          if (pdp->pd_php != NULL) {
1554 1527                                  pollhead_delete(pdp->pd_php, pdp);
1555 1528                                  pdp->pd_php = NULL;
1556 1529                                  pdp->pd_fp = NULL;
1557 1530                          }
1558 1531                  }
1559 1532          }
1560 1533          /*
1561 1534           * pollwakeup() may still interact with this pollcache. Wait until
1562 1535           * it is done.
1563 1536           */
1564 1537          mutex_enter(&pcp->pc_no_exit);
1565 1538          ASSERT(pcp->pc_busy >= 0);
1566 1539          while (pcp->pc_busy > 0)
1567 1540                  cv_wait(&pcp->pc_busy_cv, &pcp->pc_no_exit);
1568 1541          mutex_exit(&pcp->pc_no_exit);
1569 1542  
1570 1543          /* Clean up any pollcache links created via recursive /dev/poll */
1571 1544          if (pcp->pc_parents != NULL || pcp->pc_children != NULL) {
1572 1545                  /*
1573 1546                   * Because of the locking rules for pcachelink manipulation,
1574 1547                   * acquring pc_lock is required for this step.
1575 1548                   */
1576 1549                  mutex_enter(&pcp->pc_lock);
1577 1550                  pcachelink_purge_all(pcp);
1578 1551                  mutex_exit(&pcp->pc_lock);
1579 1552          }
1580 1553  
1581 1554          pcache_destroy(pcp);
1582 1555          ASSERT(dpep->dpe_refcnt == 0);
1583 1556          kmem_free(dpep, sizeof (dp_entry_t));
1584 1557          return (0);
1585 1558  }
1586 1559  
1587 1560  static void
1588 1561  pcachelink_locked_rele(pcachelink_t *pl)
1589 1562  {
1590 1563          ASSERT(MUTEX_HELD(&pl->pcl_lock));
1591 1564          VERIFY(pl->pcl_refcnt >= 1);
1592 1565  
1593 1566          pl->pcl_refcnt--;
1594 1567          if (pl->pcl_refcnt == 0) {
1595 1568                  VERIFY(pl->pcl_state == PCL_INVALID);
1596 1569                  ASSERT(pl->pcl_parent_pc == NULL);
1597 1570                  ASSERT(pl->pcl_child_pc == NULL);
1598 1571                  ASSERT(pl->pcl_parent_next == NULL);
1599 1572                  ASSERT(pl->pcl_child_next == NULL);
1600 1573  
1601 1574                  pl->pcl_state = PCL_FREE;
1602 1575                  mutex_destroy(&pl->pcl_lock);
1603 1576                  kmem_free(pl, sizeof (pcachelink_t));
1604 1577          } else {
1605 1578                  mutex_exit(&pl->pcl_lock);
1606 1579          }
1607 1580  }
1608 1581  
1609 1582  /*
1610 1583   * Associate parent and child pollcaches via a pcachelink_t.  If an existing
1611 1584   * link (stale or valid) between the two is found, it will be reused.  If a
1612 1585   * suitable link is not found for reuse, a new one will be allocated.
1613 1586   */
1614 1587  static void
1615 1588  pcachelink_assoc(pollcache_t *child, pollcache_t *parent)
1616 1589  {
1617 1590          pcachelink_t    *pl, **plpn;
1618 1591  
1619 1592          ASSERT(MUTEX_HELD(&child->pc_lock));
1620 1593          ASSERT(MUTEX_HELD(&parent->pc_lock));
1621 1594  
1622 1595          /* Search for an existing link we can reuse. */
1623 1596          plpn = &child->pc_parents;
1624 1597          for (pl = child->pc_parents; pl != NULL; pl = *plpn) {
1625 1598                  mutex_enter(&pl->pcl_lock);
1626 1599                  if (pl->pcl_state == PCL_INVALID) {
1627 1600                          /* Clean any invalid links while walking the list */
1628 1601                          *plpn = pl->pcl_parent_next;
1629 1602                          pl->pcl_child_pc = NULL;
1630 1603                          pl->pcl_parent_next = NULL;
1631 1604                          pcachelink_locked_rele(pl);
1632 1605                  } else if (pl->pcl_parent_pc == parent) {
1633 1606                          /* Successfully found parent link */
1634 1607                          ASSERT(pl->pcl_state == PCL_VALID ||
1635 1608                              pl->pcl_state == PCL_STALE);
1636 1609                          pl->pcl_state = PCL_VALID;
1637 1610                          mutex_exit(&pl->pcl_lock);
1638 1611                          return;
1639 1612                  } else {
1640 1613                          plpn = &pl->pcl_parent_next;
1641 1614                          mutex_exit(&pl->pcl_lock);
1642 1615                  }
1643 1616          }
1644 1617  
1645 1618          /* No existing link to the parent was found.  Create a fresh one. */
1646 1619          pl = kmem_zalloc(sizeof (pcachelink_t), KM_SLEEP);
1647 1620          mutex_init(&pl->pcl_lock,  NULL, MUTEX_DEFAULT, NULL);
1648 1621  
1649 1622          pl->pcl_parent_pc = parent;
1650 1623          pl->pcl_child_next = parent->pc_children;
1651 1624          parent->pc_children = pl;
1652 1625          pl->pcl_refcnt++;
1653 1626  
1654 1627          pl->pcl_child_pc = child;
1655 1628          pl->pcl_parent_next = child->pc_parents;
1656 1629          child->pc_parents = pl;
1657 1630          pl->pcl_refcnt++;
1658 1631  
1659 1632          pl->pcl_state = PCL_VALID;
1660 1633  }
1661 1634  
1662 1635  /*
1663 1636   * Mark all child links in a pollcache as stale.  Any invalid child links found
1664 1637   * during iteration are purged.
1665 1638   */
1666 1639  static void
1667 1640  pcachelink_mark_stale(pollcache_t *pcp)
1668 1641  {
1669 1642          pcachelink_t    *pl, **plpn;
1670 1643  
1671 1644          ASSERT(MUTEX_HELD(&pcp->pc_lock));
1672 1645  
1673 1646          plpn = &pcp->pc_children;
1674 1647          for (pl = pcp->pc_children; pl != NULL; pl = *plpn) {
1675 1648                  mutex_enter(&pl->pcl_lock);
1676 1649                  if (pl->pcl_state == PCL_INVALID) {
1677 1650                          /*
1678 1651                           * Remove any invalid links while we are going to the
1679 1652                           * trouble of walking the list.
1680 1653                           */
1681 1654                          *plpn = pl->pcl_child_next;
1682 1655                          pl->pcl_parent_pc = NULL;
1683 1656                          pl->pcl_child_next = NULL;
1684 1657                          pcachelink_locked_rele(pl);
1685 1658                  } else {
1686 1659                          pl->pcl_state = PCL_STALE;
1687 1660                          plpn = &pl->pcl_child_next;
1688 1661                          mutex_exit(&pl->pcl_lock);
1689 1662                  }
1690 1663          }
1691 1664  }
1692 1665  
1693 1666  /*
1694 1667   * Purge all stale (or invalid) child links from a pollcache.
1695 1668   */
1696 1669  static void
1697 1670  pcachelink_purge_stale(pollcache_t *pcp)
1698 1671  {
1699 1672          pcachelink_t    *pl, **plpn;
1700 1673  
1701 1674          ASSERT(MUTEX_HELD(&pcp->pc_lock));
1702 1675  
1703 1676          plpn = &pcp->pc_children;
1704 1677          for (pl = pcp->pc_children; pl != NULL; pl = *plpn) {
1705 1678                  mutex_enter(&pl->pcl_lock);
1706 1679                  switch (pl->pcl_state) {
1707 1680                  case PCL_STALE:
1708 1681                          pl->pcl_state = PCL_INVALID;
1709 1682                          /* FALLTHROUGH */
1710 1683                  case PCL_INVALID:
1711 1684                          *plpn = pl->pcl_child_next;
1712 1685                          pl->pcl_parent_pc = NULL;
1713 1686                          pl->pcl_child_next = NULL;
1714 1687                          pcachelink_locked_rele(pl);
1715 1688                          break;
1716 1689                  default:
1717 1690                          plpn = &pl->pcl_child_next;
1718 1691                          mutex_exit(&pl->pcl_lock);
1719 1692                  }
1720 1693          }
1721 1694  }
1722 1695  
1723 1696  /*
1724 1697   * Purge all child and parent links from a pollcache, regardless of status.
1725 1698   */
1726 1699  static void
1727 1700  pcachelink_purge_all(pollcache_t *pcp)
1728 1701  {
1729 1702          pcachelink_t    *pl, **plpn;
1730 1703  
1731 1704          ASSERT(MUTEX_HELD(&pcp->pc_lock));
1732 1705  
1733 1706          plpn = &pcp->pc_parents;
1734 1707          for (pl = pcp->pc_parents; pl != NULL; pl = *plpn) {
1735 1708                  mutex_enter(&pl->pcl_lock);
1736 1709                  pl->pcl_state = PCL_INVALID;
1737 1710                  *plpn = pl->pcl_parent_next;
1738 1711                  pl->pcl_child_pc = NULL;
1739 1712                  pl->pcl_parent_next = NULL;
1740 1713                  pcachelink_locked_rele(pl);
1741 1714          }
1742 1715  
1743 1716          plpn = &pcp->pc_children;
1744 1717          for (pl = pcp->pc_children; pl != NULL; pl = *plpn) {
1745 1718                  mutex_enter(&pl->pcl_lock);
1746 1719                  pl->pcl_state = PCL_INVALID;
1747 1720                  *plpn = pl->pcl_child_next;
1748 1721                  pl->pcl_parent_pc = NULL;
1749 1722                  pl->pcl_child_next = NULL;
1750 1723                  pcachelink_locked_rele(pl);
1751 1724          }
1752 1725  
1753 1726          ASSERT(pcp->pc_parents == NULL);
1754 1727          ASSERT(pcp->pc_children == NULL);
1755 1728  }
  
    | 
      ↓ open down ↓ | 
    983 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX