Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp691732pxv; Thu, 24 Jun 2021 18:06:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxdlgJU7AkeydAOLbaCAw60RF/y6w9PoeboRtfwTYeN+gpR1DoR7IcSOmYTlIUhxsL8njk1 X-Received: by 2002:a17:906:4c8c:: with SMTP id q12mr8093281eju.254.1624583187165; Thu, 24 Jun 2021 18:06:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624583187; cv=none; d=google.com; s=arc-20160816; b=t4e7fyubQIyuC5MZljgTGe1MlmZvNFpQ23BjoSPKOTEhiy+73dQSSVCPJ1S0iwJ/1B YCBQEigHBzFbdlZqwAWWTJhRNRKm3lekFhrdQQ2wH6b55p9taVO1VOLptFqDMwpdU+Ql SND5PUvsa8ih0NkPZ/CMuZex2wbGxhEnd68Tp4ws8Vs8c4psPJ7xRMxOV0EHhvRSNUyl 2fvq3FeOQuKYXQFYyimh/F14MfwzEPHGamolyvN2BJJKtSKCIX/i5WoexFNsSuKMC+XS Nn6k3F3Hl01yR34+nDfix1XoFVTgQ1aCWsH5OhNM3EGUA/xvCh8tOHJ+wBCNfLTptIh8 Kcfg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=PTl2a45oK7ererAoqlIPpBJuapGCJZ6anTugsk9TTSQ=; b=gb/3mIFSTVLNfQAeVTNhPwTeCfzMpy6Ng556pLzNKvrFtVKz62P1B2uN6sKmp712nT QvRyp5G+wfbWUotqJ0MXuS1jLqEJB+5hjQhB9480GNxKqWTUwGvWyQKZLKooVV2NtfL1 JJxNJH37w4lwwZs+0Mh/S00ui8MYI+YAY64Oe3DwlQJVYwGthce8DrNcFuGqjW+L/wBO inkMb06VRjclhoS8NSvWiTAaknWpjlyQaxEWBXso5R+nOJPdC5NDOZLvlbhtRtuERorx LYobGbgIGHeqyFek7YTQZtvvGt6hyTmy7hiaYOWtA1RqfmGWbBpKlJtbySWG+j33i9t+ wWHA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w23si4131729ejf.188.2021.06.24.18.06.04; Thu, 24 Jun 2021 18:06:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233028AbhFYBE6 (ORCPT + 99 others); Thu, 24 Jun 2021 21:04:58 -0400 Received: from mail-ej1-f42.google.com ([209.85.218.42]:42820 "EHLO mail-ej1-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232996AbhFYBEw (ORCPT ); Thu, 24 Jun 2021 21:04:52 -0400 Received: by mail-ej1-f42.google.com with SMTP id bg14so12382911ejb.9; Thu, 24 Jun 2021 18:02:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PTl2a45oK7ererAoqlIPpBJuapGCJZ6anTugsk9TTSQ=; b=hm5wTy3q6g0A2cHvPxRyPMBk9cnkqHfwIev5BnRaTkWNMTY9sZHTmKirIrIEwJ0Gm8 atrr1l5gcmQGbT35CBN06ZtptMGFRlVren+VP0hn13Nob258EDbouRMdqqWMPTIC6uZi G0vlhubmn240wZnsJlSpId87tUgw9K5Ng1oDdvIefstxEuJRxZv6X9HN7tlcACKSGOnK 5T8zrULsGEvcbxyYXIRWCNX6+S7uKJJ69HqwdDkaGCZvaK1aRdazfkOWAFlym2Dl4sF/ +Ab7cFzCT/Nz9MqeGWxPtGkYmqWeudQK2Y35XUKiVMj4Lb6h5IlixoeAG0MbNibiJYqb t62g== X-Gm-Message-State: AOAM5314HeSdM5lhAX1g6etMYYIcri50hZzDbMD3Ca3HILNKD9/KjYPC m4vpvn2O49SgQux4JdmuMAkg/VZuVweb7Q== X-Received: by 2002:a17:906:c1d0:: with SMTP id bw16mr8146696ejb.146.1624582950900; Thu, 24 Jun 2021 18:02:30 -0700 (PDT) Received: from msft-t490s.home (host-95-251-17-240.retail.telecomitalia.it. [95.251.17.240]) by smtp.gmail.com with ESMTPSA id yc29sm1921909ejb.106.2021.06.24.18.02.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Jun 2021 18:02:30 -0700 (PDT) From: Matteo Croce To: linux-kernel@vger.kernel.org, Nick Kossifidis , Guo Ren , Christoph Hellwig , David Laight , Palmer Dabbelt , Emil Renner Berthing , Drew Fustini Cc: linux-arch@vger.kernel.org, Andrew Morton , Nick Desaulniers , linux-riscv@lists.infradead.org Subject: [PATCH 3/3] lib/string: optimized memset Date: Fri, 25 Jun 2021 03:02:00 +0200 Message-Id: <20210625010200.362755-4-mcroce@linux.microsoft.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210625010200.362755-1-mcroce@linux.microsoft.com> References: <20210625010200.362755-1-mcroce@linux.microsoft.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Matteo Croce The generic memset is defined as a byte at time write. This is always safe, but it's slower than a 4 byte or even 8 byte write. Write a generic memset which fills the data one byte at time until the destination is aligned, then fills using the largest size allowed, and finally fills the remaining data one byte at time. Signed-off-by: Matteo Croce --- lib/string.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/string.c b/lib/string.c index 69adec252597..598ece5434e9 100644 --- a/lib/string.c +++ b/lib/string.c @@ -811,10 +811,36 @@ EXPORT_SYMBOL(__sysfs_match_string); */ void *memset(void *s, int c, size_t count) { - char *xs = s; + union types dest = { .as_u8 = s }; + if (count >= MIN_THRESHOLD) { + unsigned long cu = (unsigned long)c; + + /* Compose an ulong with 'c' repeated 4/8 times */ + cu |= cu << 8; + cu |= cu << 16; +#if BITS_PER_LONG == 64 + cu |= cu << 32; +#endif + + if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) { + /* + * Fill the buffer one byte at time until + * the destination is word aligned. + */ + for (; count && dest.as_uptr & word_mask; count--) + *dest.as_u8++ = c; + } + + /* Copy using the largest size allowed */ + for (; count >= bytes_long; count -= bytes_long) + *dest.as_ulong++ = cu; + } + + /* copy the remainder */ while (count--) - *xs++ = c; + *dest.as_u8++ = c; + return s; } EXPORT_SYMBOL(memset); -- 2.31.1