From nobody Sat Aug 27 21:06:43 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4MFTlm03Wgz4bCMd; Sat, 27 Aug 2022 21:06:44 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4MFTll6gRqz45mF; Sat, 27 Aug 2022 21:06:43 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1661634403; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=KKIp10hKaxUIDftUQR01SdFRTXlFGUw5lJtmBHN60ag=; b=jki6dLYMCrXFre/NSD5AuPhMsJ8647vC9+fqnt/DKm65xMlmFXZdTexreDyDo4TNhH41HB W7FYuayVFf/iFRRmByUwNHcy2f+WzXvpDKko1OHYVDe738kdtXEmv3nbhNWqYLyLtQyVXq 8B8h+eIsd7n09KBH5fy/lS32D5aDTDTWsCO6EGb43EZa5wmpucobVeg4R36p9PDW+sp2tv IjJg2/h7ZdPg9y7wlYSIhdS59B+3gQA0CcbxvdCNIL7ZoVdzgciRElAtHvdzRfaveKZmeL aA5FRp5gUe1AOpxSSdg4x3CrEI7X+TEVS6DPrAulEPD1qvwSWTTcLari3uE1og== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4MFTll5DXCzsQF; Sat, 27 Aug 2022 21:06:43 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 27RL6hXO038519; Sat, 27 Aug 2022 21:06:43 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 27RL6hlC038518; Sat, 27 Aug 2022 21:06:43 GMT (envelope-from git) Date: Sat, 27 Aug 2022 21:06:43 GMT Message-Id: <202208272106.27RL6hlC038518@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Doug Moore Subject: git: 5fbc2eb92ce5 - stable/13 - iommu_gas: avoid pointless augmentation List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: dougm X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 5fbc2eb92ce5401d5e8cbee49f9421186cff31c3 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1661634403; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=KKIp10hKaxUIDftUQR01SdFRTXlFGUw5lJtmBHN60ag=; b=SpUQwBMH58YeBvEo3AH1Ni6hqhfSybUydEw7Oqmmqk54I4inL3dHt7vmje8W7JOP7lSE1/ y8KJ4/du0LMqR6fOKoSDkxkXwCp9hY6rVzML3bZf0wPSn0Zqg3kTHus7kCkAmXCJazbAsK 2InD3XDaTTsDvcAfOFfrb7sNqOxSQIpm8+Hl53M3aF1SU9td/02oB8siDI1Ae2G7AzI8iu 6CNLSlA2pqJ9nV5AfCxNuDh/at/xQ4kAgQIGxhvJUSFR9sTLf6palz/BjkJHCBtEndOFa4 bkboKEs5d5oRbBaCCiRPur+XDWYJ0RotZep8QtwL3plqa4C06pVx8hT3U3OyHw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1661634403; a=rsa-sha256; cv=none; b=xamIJ+xYKM4z8mgTEzwCwygIEajF18C23F25LRmqU5fgMBJTks1+J8ZxlYNOTz8NICvMvh SxEOydTAO02qHXXCHTxqDOfuQSons2XL8uOR+mk/aaW2zJ/DhG0/lZ+0Xucj+g4Q3JpvFd 6NLXKVCfiS1MIUauuQVqm2uwzaNsxy5Na0aQjUSWLKlT3W2nQ1NFQ/LgsXSOx2iK+DVNSQ 2LWRXV1bOeLWiJNYH2NML4O6ipAgzjaY5NLUSUW7K/X9ByzigXYuQVknO0LX7sDqm8lQ60 p3yTIgRY5zOpqeNAou1E+rm+VSnNIX8ExwUNzMUn1sxLqLRPyIMXtQIJM+pmow== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by dougm: URL: https://cgit.FreeBSD.org/src/commit/?id=5fbc2eb92ce5401d5e8cbee49f9421186cff31c3 commit 5fbc2eb92ce5401d5e8cbee49f9421186cff31c3 Author: Doug Moore AuthorDate: 2022-08-06 18:26:18 +0000 Commit: Doug Moore CommitDate: 2022-08-27 21:05:50 +0000 iommu_gas: avoid pointless augmentation iommu_gas_augment_entry updates a map entry element. Invoked as RB_AUGMENT in RB tree code, it is applied from the point where the tree is modified, all the way up to the root, and is also applied when rotation moves a node down in the tree. There are several opportunities to invoke it less. The automatic augmentation with every rotation is a mistake. Delaying these augmentations until RB_INSERT_COLOR or RB_REMOVE_COLOR are finishing allows the augmentation code to be duplicated less, to work when there is less register pressure, and to be skipped when conditions allow it: In the double-rotate case of RB_INSERT_COLOR, augmentation after the first rotation is not necessary when the element being moved down the tree becomes a leaf. It was in the tree, and was a leaf, before the RB_INSERT operation began, and so recomputing augmentation for it would do nothing. In the final (possibly only) rotation of RB_REMOVE_COLOR, both the elements - the one moving up and the one moving down - end up in the path from the deletion point to the tree root, so there's no need to augment either of them immediately. In RB_REMOVE, when the right child of the removed node replaces it in tree, it began with a null left child. Replacement creates a non-NULL left child, and then rotation may put a NULL node back in that place. If that happens, start the augmenting-up-to-root with the parent of that node, since augmentation would do nothing. Adjust to avoid these needless augmentations. Reviewed by: alc MFC after: 3 weeks Differential Revision: https://reviews.freebsd.org/D35502 (cherry picked from commit 7f2ec173e4613a57732ca162572d25b0a546689f) --- sys/sys/tree.h | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/sys/sys/tree.h b/sys/sys/tree.h index 2baadf768340..50c4ec980b1e 100644 --- a/sys/sys/tree.h +++ b/sys/sys/tree.h @@ -372,7 +372,7 @@ struct { \ #endif #define RB_UPDATE_AUGMENT(elm, field) do { \ - __typeof(elm) rb_update_tmp = (elm); \ + __typeof(elm) rb_update_tmp = (elm); \ do { \ RB_AUGMENT(rb_update_tmp); \ } while ((rb_update_tmp = RB_PARENT(rb_update_tmp, field)) != NULL); \ @@ -395,7 +395,6 @@ struct { \ RB_SWAP_CHILD(head, elm, tmp, field); \ RB_LEFT(tmp, field) = (elm); \ RB_SET_PARENT(elm, tmp, field); \ - RB_AUGMENT(elm); \ } while (/*CONSTCOND*/ 0) #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \ @@ -406,7 +405,6 @@ struct { \ RB_SWAP_CHILD(head, elm, tmp, field); \ RB_RIGHT(tmp, field) = (elm); \ RB_SET_PARENT(elm, tmp, field); \ - RB_AUGMENT(elm); \ } while (/*CONSTCOND*/ 0) /* Generates prototypes and inline functions */ @@ -494,7 +492,9 @@ name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ elm = parent; \ continue; \ } \ - if (!RB_RED_RIGHT(elm, field)) { \ + if (RB_RED_RIGHT(elm, field)) \ + child = elm; \ + else { \ /* coverity[uninit_use] */ \ RB_ROTATE_LEFT(head, elm, child, field);\ if (RB_RED_RIGHT(child, field)) \ @@ -503,9 +503,11 @@ name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ RB_FLIP_ALL(elm, field); \ else \ RB_FLIP_LEFT(elm, field); \ - elm = child; \ + if ((RB_BITS(child, field) & \ + RB_RED_MASK) == 0) \ + elm = child; \ } \ - RB_ROTATE_RIGHT(head, parent, elm, field); \ + RB_ROTATE_RIGHT(head, parent, child, field); \ } else { \ if (RB_RED_RIGHT(parent, field)) { \ RB_FLIP_RIGHT(parent, field); \ @@ -517,7 +519,9 @@ name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ elm = parent; \ continue; \ } \ - if (!RB_RED_LEFT(elm, field)) { \ + if (RB_RED_LEFT(elm, field)) \ + child = elm; \ + else { \ /* coverity[uninit_use] */ \ RB_ROTATE_RIGHT(head, elm, child, field);\ if (RB_RED_LEFT(child, field)) \ @@ -526,11 +530,16 @@ name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ RB_FLIP_ALL(elm, field); \ else \ RB_FLIP_RIGHT(elm, field); \ - elm = child; \ + if ((RB_BITS(child, field) & \ + RB_RED_MASK) == 0) \ + elm = child; \ } \ - RB_ROTATE_LEFT(head, parent, elm, field); \ + RB_ROTATE_LEFT(head, parent, child, field); \ } \ - RB_BITS(elm, field) &= ~RB_RED_MASK; \ + RB_BITS(child, field) &= ~RB_RED_MASK; \ + if (elm != child) \ + RB_AUGMENT(elm); \ + RB_AUGMENT(parent); \ break; \ } \ } @@ -589,21 +598,22 @@ name##_RB_REMOVE_COLOR(struct name *head, \ else \ RB_FLIP_RIGHT(sib, field); \ RB_BITS(elm, field) |= RB_RED_MASK; \ - sib = elm; \ break; \ case RB_RED_L: \ if (RB_STRICT_HST && elm != NULL) { \ RB_FLIP_RIGHT(parent, field); \ RB_FLIP_ALL(sib, field); \ + elm = sib; \ break; \ } \ RB_FLIP_LEFT(parent, field); \ /* FALLTHROUGH */ \ default: \ RB_FLIP_RIGHT(sib, field); \ + elm = sib; \ break; \ } \ - RB_ROTATE_LEFT(head, parent, sib, field); \ + RB_ROTATE_LEFT(head, parent, elm, field); \ } else { \ if (!RB_RED_RIGHT(parent, field)) { \ RB_FLIP_RIGHT(parent, field); \ @@ -632,22 +642,25 @@ name##_RB_REMOVE_COLOR(struct name *head, \ else \ RB_FLIP_LEFT(sib, field); \ RB_BITS(elm, field) |= RB_RED_MASK; \ - sib = elm; \ break; \ case RB_RED_R: \ if (RB_STRICT_HST && elm != NULL) { \ RB_FLIP_LEFT(parent, field); \ RB_FLIP_ALL(sib, field); \ + elm = sib; \ break; \ } \ RB_FLIP_RIGHT(parent, field); \ /* FALLTHROUGH */ \ default: \ RB_FLIP_LEFT(sib, field); \ + elm = sib; \ break; \ } \ - RB_ROTATE_RIGHT(head, parent, sib, field); \ + RB_ROTATE_RIGHT(head, parent, elm, field); \ } \ + if (sib != elm) \ + RB_AUGMENT(sib); \ break; \ } while ((parent = RB_PARENT(elm, field)) != NULL); \ } @@ -687,6 +700,8 @@ name##_RB_REMOVE(struct name *head, struct type *elm) \ RB_SET_PARENT(child, parent, field); \ if (parent != NULL) { \ name##_RB_REMOVE_COLOR(head, parent, child); \ + if (parent == elm && RB_LEFT(parent, field) == NULL) \ + parent = RB_PARENT(parent, field); \ RB_UPDATE_AUGMENT(parent, field); \ } \ return (old); \