From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by sourceware.org (Postfix) with ESMTPS id 3C9A23858423 for ; Sat, 15 Apr 2023 16:17:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3C9A23858423 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-x12d.google.com with SMTP id 2adb3069b0e04-4ec8133c698so2167239e87.0 for ; Sat, 15 Apr 2023 09:17:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681575451; x=1684167451; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sDZIXFYfeOute3ymEClV98cknuxjoD6/Lir08/+mRcs=; b=OqjUdtzeOVUxJG+z+pF+1Ctjz3c7PrWX3vjtNK69uakL3iInRVBZ9dPeVFS83fF6am RCbsZ1TmolEyKyxCEj9U9IaF3iVZhtmxxJxZcLK4LJtX+s7r+yYzktTz2w6fe32Hz6dx Wv14J7RYF9+v65doKfMLIt74nZQNLQIN5aJoac3Vo7mSgz9rrJENoep6d2GJytn2+RR+ kYa3M1NJAzwpnvaS/eCKLvyFl5ywsIg9LwqMxgsosqQpddW5SEsNFx89mihldtuj3S7f kzC0mUlUuCgoEvvZCehvs6c+rGPPA+weL1qmipSZvPVClu81EexhJlZudkl0ZYXKaIuS fsKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681575451; x=1684167451; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sDZIXFYfeOute3ymEClV98cknuxjoD6/Lir08/+mRcs=; b=BCs+8qwa6Ib9pi1fD+CE5F8ofIvuVez0pgHlsZB3XfntxYseFEtLgsP3l1HgvdYAvW skcLox/x5RV9Zka6zJg5JYuraTmLQdjb26w9T5Cwvck3AWr9Et1gYQDlT260I9eQe8q9 ius5IGpllVXlBmGAF+rlY7mA/9My7GWQYCBINFCcatwyJcb/3NlMEtzPYWy2x4oINCuR OMUx7xlBjmUS/mkqAZVlXjsbtjGcXYbhmuIfLNlY43gOEVUPgw4q0Dxx1iZW9R+Wld6g z9anfMqIWWrejgDDOy/2tFaAhWSdmle+aT6yMvU3WLKqraK+r2uqRIlibAy+4x+/8rNl Kmiw== X-Gm-Message-State: AAQBX9emAEnO0F94+D0VF/OGbaNSryTGgsdQYbANocXyuhW3x7wu0Zjg TPj7ADRh29uf3QxuZ56fcSOU+B+5k5cX/Q== X-Google-Smtp-Source: AKy350YGRmIS1/FlrTgF9jBTS35e/IlnRu6oTkULgUUcbdMRPabcSKeoA7mb2L3I9TlwSyaoYjgIDw== X-Received: by 2002:a05:6512:1156:b0:4db:971:82cd with SMTP id m22-20020a056512115600b004db097182cdmr538383lfg.17.1681575451244; Sat, 15 Apr 2023 09:17:31 -0700 (PDT) Received: from surface-pro-6.. ([2a00:1370:818c:4a57:1942:fa6f:6d42:319f]) by smtp.gmail.com with ESMTPSA id k15-20020a2e240f000000b002a79de1dcf4sm1400188ljk.121.2023.04.15.09.17.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Apr 2023 09:17:30 -0700 (PDT) From: Sergey Bugaev To: libc-alpha@sourceware.org, bug-hurd@gnu.org Cc: Samuel Thibault , Sergey Bugaev Subject: [PATCH 1/1] hurd: Avoid extra ctty RPCs in init_dtable () Date: Sat, 15 Apr 2023 19:17:18 +0300 Message-Id: <20230415161718.80668-2-bugaevc@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230415161718.80668-1-bugaevc@gmail.com> References: <20230415161718.80668-1-bugaevc@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: It is common to have (some of) stdin, stdout and stderr point to the very same port. We were making the ctty RPCs that _hurd_port2fd () does for each one of them separately: 1. term_getctty () 2. mach_port_deallocate () 3. term_open_ctty () Instead, let's detect this case and duplicate the ctty port we already have. This means we do 1 RPC instead of 3 (and create a single protid on the server side) if the file is our ctty, and no RPCs instead of 1 if it's not. A clear win! Signed-off-by: Sergey Bugaev --- hurd/dtable.c | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/hurd/dtable.c b/hurd/dtable.c index 5dd04131..41f4d7af 100644 --- a/hurd/dtable.c +++ b/hurd/dtable.c @@ -60,18 +60,50 @@ init_dtable (void) _hurd_dtable[i] = NULL; else { + int copy; /* Allocate a new file descriptor structure. */ struct hurd_fd *new = malloc (sizeof (struct hurd_fd)); if (new == NULL) __libc_fatal ("hurd: Can't allocate initial file descriptors\n"); - /* Initialize the port cells. */ - _hurd_port_init (&new->port, MACH_PORT_NULL); - _hurd_port_init (&new->ctty, MACH_PORT_NULL); - - /* Install the port in the descriptor. - This sets up all the ctty magic. */ - _hurd_port2fd (new, _hurd_init_dtable[i], 0); + /* See if this file descriptor is the same as a previous one we have + already installed. In this case, we can just copy over the same + ctty port without making any more RPCs. We only check the the + immediately preceding fd and fd 0 -- this should be enough to + handle the common cases while not requiring quadratic + complexity. */ + if (i > 0 && _hurd_init_dtable[i] == _hurd_init_dtable[i - 1]) + copy = i - 1; + else if (i > 0 && _hurd_init_dtable[i] == _hurd_init_dtable[0]) + copy = 0; + else + copy = -1; + + if (copy < 0) + { + /* Initialize the port cells. */ + _hurd_port_init (&new->port, MACH_PORT_NULL); + _hurd_port_init (&new->ctty, MACH_PORT_NULL); + + /* Install the port in the descriptor. + This sets up all the ctty magic. */ + _hurd_port2fd (new, _hurd_init_dtable[i], 0); + } + else + { + /* Copy over ctty from the already set up file descriptor that + contains the same port. We can access the contents of the + cell without any locking since no one could have seen it + yet. */ + mach_port_t ctty = _hurd_dtable[copy]->ctty.port; + + if (MACH_PORT_VALID (ctty)) + __mach_port_mod_refs (__mach_task_self (), ctty, + MACH_PORT_RIGHT_SEND, +1); + + _hurd_port_init (&new->port, _hurd_init_dtable[i]); + _hurd_port_init (&new->ctty, ctty); + } _hurd_dtable[i] = new; } -- 2.39.2