Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp1170095lqm; Thu, 2 May 2024 07:14:27 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXPcgvHAcEgo8AWu+rGwsEdLv5ykzkmWQnkltg5VG0WJS4Yp74j5QyUIEO46SjuSK3iyLpbWW7iy/+zI7ZRYUMXu01R3iEeKtldYg3pkw== X-Google-Smtp-Source: AGHT+IGncObnMHVTCg+lx5ljBYfxGaLwnotMjctaV30SgpWWoKhsScDakaAUS9L7RNtaNCOy2j9u X-Received: by 2002:a05:620a:13f7:b0:790:9dee:5ee5 with SMTP id h23-20020a05620a13f700b007909dee5ee5mr5457064qkl.2.1714659266792; Thu, 02 May 2024 07:14:26 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714659266; cv=pass; d=google.com; s=arc-20160816; b=f5G2PAoHzGmdUr3l1gkhHvxIlvz31fsf0ENYINz4Vm19VhTWUT3sAvfcLbzeQ6d9ER AhD2zYkr6xokgxmfHA9iuPcwg5OqaWhQ+gd0l16wy1BXmHIpss0U0QymWLbVcIwxcnIU 6Hez9Y3Re6wJboDExXswY0Mv6gm5j5QpPXPGJD8WwSYcqIl3KjXqrdFGneWbPthDxhHa /97dtF8PbbsTV0+24/tgohAALObW7RtMcNiOpzDqUGQjd2iwCW32fvF7AkskeBWRp8ft Iwyjcr3uCB+hcGiQBRcQPBwSj8SDkk3PX6GqqZpuyMfab+KmViEu6YNvgEONO0mP5a2l t/sg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=ZK9MhMJmPLU0U1h9aoilxp/KfAO26agqEnlepbavk/M=; fh=REPDBRuVcFjIjrN5efL5Akr3zhq7XgiJsQoIkCw5OoE=; b=byt5/RPgxjWaCHlDXKxLu0/eta11OJGydluDzy+aVKHHcJMdjlrt+yf8TzZP3CMEPy /OangJQfTIfYnIOZuNhoRKeJ+1g1E846iHvoGWE5b8ydhkAx8/vm7xn1IOLVd6hS6fBH cakFagAAm/bw2KXHTqTyrHPQxdjCd8vDkkGZ9gYnbFzy6w51LTQ8lXZY6gocqT8DXd4z K1aZGiI/YdlXFtzmRBPlx3mxVVf5LIKYsrG336otkCRjz5YjHeFdZlVrHlxu2lNifg3q anT9ieCySz7t2v623rseqcAtdLei1MEb1zwndx97FxL0BORT4cjKEAzAGPQPnGD1Ex/z 3qlg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=MrNpNSSo; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-166546-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-166546-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id pc30-20020a05620a841e00b00790f6ef8617si988055qkn.210.2024.05.02.07.14.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 07:14:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-166546-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=MrNpNSSo; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-166546-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-166546-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 752A01C2167C for ; Thu, 2 May 2024 14:14:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 121EA13C81B; Thu, 2 May 2024 14:14:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MrNpNSSo" Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C3C0513C805; Thu, 2 May 2024 14:14:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714659247; cv=none; b=IHuZdlOgMOagcewdbDI/3GPX5mR3Ir+5MuDXZ8GfGxVJjwWf7HFr4iCq04qC45X75JuJjquLAycl0qyyaYtIuQBYEwpRPwpVfDBdWYvoHtUFYDjASxXBVn6y7fTRqVcJIbYiicIXj1rY0tHvkWjuTqOuOdZPoyJOMMD5yW1XMN0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714659247; c=relaxed/simple; bh=lbWfnrf/xgAKQua0DHb+A0y6pv4+Q2Pc2fzVcmh6W7k=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=VJXsBzg68kmmhTz9LCtHFauzRx0L5PnDhSAGEeuv/JfNRAODv+iyAIzTiFpg6MexXX6xzIyxf3z4BU6N941pLdI8OdEf+q1ijSfCc/kyHsIdRjmRSNMyBBJH6NCLa7meqPBSczot73+p35Ka67PItakqzxrseAXRw9ugYBNzetc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MrNpNSSo; arc=none smtp.client-ip=209.85.210.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-6ed627829e6so9331259b3a.1; Thu, 02 May 2024 07:14:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714659245; x=1715264045; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ZK9MhMJmPLU0U1h9aoilxp/KfAO26agqEnlepbavk/M=; b=MrNpNSSoe3uH2rB2n+4PEq6UusckOCS4TvKQ7yVH9qwpkDLgou8I+/q9wAud9biwMN PaIBoeZDMoFhICn1+cNhgZ8q80RtWvpHHsgdkMvXweOshbZK0FcrmR4lU0znOXID52uI fHING7FGsWkcPz5iP/WjnI5Z9dCLvjD3wA4oCHRElhiIqrHiYhEeawopYW8l5LflzRUm FtwNI+jzK3y47YonxXc9T/5G/S1cn8glx4vTWpVOt+C2XprHIMfJbTSgzlE1RvGgUSLH 9vZ2UE+0PPfi3zOwtlEXg1ko0GEhReYHlf+4kCp7rLRxiG/dVYy7x7AI+d87twB3mCv7 DStw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714659245; x=1715264045; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ZK9MhMJmPLU0U1h9aoilxp/KfAO26agqEnlepbavk/M=; b=XcYZ/LtplxZZfSiPWcpqCLrN8fCGxeEdoSK6jFEEYZvPT7WcoUqDhml+Bneg3lG9em FYgRMCnu3WfE+Qd6GwnqRNvop+1ccMUSoBBYmLGxKNq9Lo/VKaw1UNfMRxv96w/Sn06z RTovYsHX66QIbpZ3ZYGOM70Yyg92zbOgiWwBdDsMMmzHoFTKga3LRczOesWtrfYWVlG9 b4zysz56JAeIcpkjskz+duQEuAE+z3uZ7clfb2bnguf6yyeMjMzosg1Szh9uDPHrIFpC 0YgxAcQQmSJHJDbNjzIw9Jo31KzPvVH39+NffFxX4OUwdWpakh0rE9iPU8ks7qyblVyu Avfw== X-Forwarded-Encrypted: i=1; AJvYcCXsrMI1lr91PS7Zj7OrJxpPyMGQ0AQdXPnezoE1/fnloFodFLZRsuDXnfIb2Rtur6Cg2AVjEak89CbPsFHBxTwK5FZdKyRAtptQVpffGlkVlL8YvP0NRD0k3WHvysvC7u8C57t0Gl5A5k+NSD3x X-Gm-Message-State: AOJu0Yze34sPEnEheuHurOhIAaIitdQ4I4fsyadhXxNTe1XoFAJBhuoU 8wjFDzp3XOfxqsLTdzLHBO0Ox6dMx74Kyd+Uqq5bYzKZClQLm2Dj X-Received: by 2002:a05:6a20:4320:b0:1a9:5eb1:7f35 with SMTP id h32-20020a056a20432000b001a95eb17f35mr7072909pzk.46.1714659244972; Thu, 02 May 2024 07:14:04 -0700 (PDT) Received: from localhost.localdomain (125-229-101-177.hinet-ip.hinet.net. [125.229.101.177]) by smtp.gmail.com with ESMTPSA id h17-20020a056a00219100b006ed06c4074bsm1252591pfi.85.2024.05.02.07.14.03 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 02 May 2024 07:14:04 -0700 (PDT) From: "Hsin-Yu.Chen" To: keescook@chromium.org Cc: andy@kernel.org, akpm@linux-foundation.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, "Hsin-Yu.Chen" Subject: [PATCH 2/2] string: improve strlen performance Date: Thu, 2 May 2024 22:13:59 +0800 Message-Id: <20240502141359.89567-1-harry021633@gmail.com> X-Mailer: git-send-email 2.38.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Port `strlen` in gcc, which enhance performance over 10 times Please refer to these following articles 1. [Determine if a word has a byte less than n] (https://graphics.stanford.edu/~seander/bithacks.html#HasLessInWord) 2. [Determine if a word has a zero byte] (https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord) Signed-off-by: Hsin-Yu.Chen --- lib/string.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 4 deletions(-) diff --git a/lib/string.c b/lib/string.c index 6891d15ce991..31e8642422af 100644 --- a/lib/string.c +++ b/lib/string.c @@ -398,11 +398,80 @@ EXPORT_SYMBOL(strnchr); #ifndef __HAVE_ARCH_STRLEN size_t strlen(const char *s) { - const char *sc; + const char *char_ptr; + const unsigned long *longword_ptr; + unsigned long longword, himagic, lomagic; - for (sc = s; *sc != '\0'; ++sc) - /* nothing */; - return sc - s; + /* Handle the first few characters by reading one character at a time. + * Do this until CHAR_PTR is aligned on a longword boundary. + */ + for (char_ptr = s; ((unsigned long) char_ptr + & (sizeof(longword) - 1)) != 0; + ++char_ptr) + if (*char_ptr == '\0') + return char_ptr - s; + + /* All these elucidatory comments refer to 4-byte longwords, + * but the theory applies equally well to 8-byte longwords. + */ + longword_ptr = (unsigned long *) char_ptr; + + /* Bits 31, 24, 16, and 8 of this number are zero. + * Call these bits the "holes." + * Note that there is a hole just to the left of + * each byte, with an extra at the end: + * bits: 01111110 11111110 11111110 11111111 + * bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD + * The 1-bits make sure that carries propagate to the next 0-bit. + * The 0-bits provide holes for carries to fall into. + */ + himagic = 0x80808080L; + lomagic = 0x01010101L; + + if (sizeof(longword) > 4) { + /* 64-bit version of the magic. */ + /* Do the shift in two steps to avoid a warning if long has 32 bits. + */ + himagic = ((himagic << 16) << 16) | himagic; + lomagic = ((lomagic << 16) << 16) | lomagic; + } + + if (sizeof(longword) > 8) + abort(); + + /* Instead of the traditional loop which tests each character, + * we will test a longword at a time. The tricky part is testing + * if *any of the four* bytes in the longword in question are zero. + */ + for (;;) { + longword = *longword_ptr++; + if (((longword - lomagic) & ~longword & himagic) != 0) { + + /* Which of the bytes was the zero? + * If none of them were, it was a misfire; continue the search. + */ + const char *cp = (const char *) (longword_ptr - 1); + + if (cp[0] == 0) + return cp - s; + else if (cp[1] == 0) + return cp - s + 1; + else if (cp[2] == 0) + return cp - s + 2; + else if (cp[3] == 0) + return cp - s + 3; + if (sizeof(longword) > 4) { + if (cp[4] == 0) + return cp - s + 4; + else if (cp[5] == 0) + return cp - s + 5; + else if (cp[6] == 0) + return cp - s + 6; + else if (cp[7] == 0) + return cp - s + 7; + } + } + } } EXPORT_SYMBOL(strlen); #endif -- 2.38.1