Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp11170pxv; Wed, 21 Jul 2021 14:04:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy8A9ltiVAx0zBi6LWA8Qam76klZgV3VTrCh2aibevwkco/iXeU4+KCJwXhyfZwrCX7WlLO X-Received: by 2002:a92:c10d:: with SMTP id p13mr24717579ile.83.1626901454846; Wed, 21 Jul 2021 14:04:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626901454; cv=none; d=google.com; s=arc-20160816; b=E4YyB7pl4LEyS/NAbt7nIS7EBv68Ztn/JBeBP+1bKIO7c9ZQ3iGvAjgXyRGKqtVD0z G4i0rILy5hJ+kXZbVC3U/LYEoSQoo59YEs14TnxUbktGOTFprQgepZX1cUXSrJCEMd2a xX7U8gZnAJ182ENJbozkDt+nXByrWeLz+z13Jz8vZD5cmGMGhHN/IuqjixQSGSha61mk OjswhjUec/JcoxopZtJp6R83kxK01zUzrhveWCyxe89EwJ2C/8WH4Fdmzq/+qTKUJz4G ktn9D9qpswrnS01JbWNiiZlVCvuOwlCfyjjWSQuVIUxv74hnXHlfDpJz/6ZEBwXRI9KL 5B6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=VPa37eNvC3uDqntcxwWWKIYs96FW2IfN+zBOwLvLWyk=; b=biOBEwKq2l+UKWucb9FNW4tRCXOUO+uWvD7zIUK8JsGyd+Zp60qnljmSKGam9sJ5/x bvAwNPzKKjDYpM+7mTl5ZJV939bqpQZ7nW53Ztgoa/2Iz9yKB8k3xJpphvzQ6mGHHQPj FeboZY5eJ20QbEqOCDY68J4iW/uiy1EBh2iLpMDFrNiztrh0DCZdq81ZrxkQq1SCxE6M 0E9hj/ErC+sgvM/fQf/gGAkWiVJ+2ITuj75ceBNrWgsrCBmMqO0CXOfnHH4oqe4I7Va3 ZiYHO8WTlEwkE1v2sUSqn8ILj0c5tSI5hvIaYTbNZCZSz0Tp41ep7aZWQLx8AUk47W2l Br9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=c+qRkAs1; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o18si31495057jah.71.2021.07.21.14.04.03; Wed, 21 Jul 2021 14:04:14 -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; dkim=pass header.i=@linux-foundation.org header.s=google header.b=c+qRkAs1; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236470AbhGURUv (ORCPT + 99 others); Wed, 21 Jul 2021 13:20:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231256AbhGURUo (ORCPT ); Wed, 21 Jul 2021 13:20:44 -0400 Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A644BC061757 for ; Wed, 21 Jul 2021 11:01:19 -0700 (PDT) Received: by mail-lf1-x12a.google.com with SMTP id 8so4429048lfp.9 for ; Wed, 21 Jul 2021 11:01:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=VPa37eNvC3uDqntcxwWWKIYs96FW2IfN+zBOwLvLWyk=; b=c+qRkAs1DwxE9hRy95PZsZTyPTKGRmiVDdYPr/bRhqbhIDs7uWTM2Wsf1S7FMeFGe1 FNgM0RY06bExtz9ZsFKG5uZQ1/OxFtWR3RXfJPOwfj+9JRRXgAw7yBN4moMgoQd9D6PA XBUgrH2V+hAGFnQQ1Pvz1igLS1r2cK9mRXng4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=VPa37eNvC3uDqntcxwWWKIYs96FW2IfN+zBOwLvLWyk=; b=WlAvXktGeph+rwrtByba0In4y5/9hDor4kaPdZeYmP/L9kvnEO8OlH+YilmwirE+uU iMB64lgfLA+6HFbejiQDclwIgi6gZnNT60GgPa/Jvjr1Pv9gKtoAbfStcRNSToBMBxTE dWKdUZnD3rrnsMuMaKZij0Rn7MKcGwX86/RaQhxTxf97JThn63dcGpG/zjweht0M5B0n Z8t93vq0pmHd56m5mozk16GfI2A9RCZN3i/DIcJJF6ZTFm4rL43XsAoDM3O5cYORWVHK fl6Yvcz/S1oNilRxXuisqhsMjQZyQ8IezzEmYCp/y2tfoLwULDZxLCtaTu8R6sZHNkds meRQ== X-Gm-Message-State: AOAM53094057GmRTz+DnR/8fdPpsAEb/jCpVKOufIPK3S0nh/SjBJvSX nXGEckln03PH39udrogTOJbqUk7ErAxpZPzh X-Received: by 2002:ac2:414e:: with SMTP id c14mr26575175lfi.632.1626890476806; Wed, 21 Jul 2021 11:01:16 -0700 (PDT) Received: from mail-lf1-f49.google.com (mail-lf1-f49.google.com. [209.85.167.49]) by smtp.gmail.com with ESMTPSA id d8sm1797015lfq.138.2021.07.21.11.01.16 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 21 Jul 2021 11:01:16 -0700 (PDT) Received: by mail-lf1-f49.google.com with SMTP id g8so4422583lfh.8 for ; Wed, 21 Jul 2021 11:01:16 -0700 (PDT) X-Received: by 2002:ac2:42d6:: with SMTP id n22mr26027265lfl.41.1626890475459; Wed, 21 Jul 2021 11:01:15 -0700 (PDT) MIME-Version: 1.0 References: <20210721135926.602840-1-nborisov@suse.com> In-Reply-To: <20210721135926.602840-1-nborisov@suse.com> From: Linus Torvalds Date: Wed, 21 Jul 2021 11:00:59 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] lib/string: Bring optimized memcmp from glibc To: Nikolay Borisov Cc: Linux Kernel Mailing List , Nick Desaulniers , linux-fsdevel , Dave Chinner Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jul 21, 2021 at 6:59 AM Nikolay Borisov wrote: > > This is glibc's memcmp version. The upside is that for architectures > which don't have an optimized version the kernel can provide some > solace in the form of a generic, word-sized optimized memcmp. I tested > this with a heavy IOCTL_FIDEDUPERANGE(2) workload and here are the > results I got: Hmm. I suspect the usual kernel use of memcmp() is _very_ skewed to very small memcmp calls, and I don't think I've ever seen that (horribly bad) byte-wise default memcmp in most profiles. I suspect that FIDEDUPERANGE thing is most likely a very special case. So I don't think you're wrong to look at this, but I think you've gone from our old "spend no effort at all" to "look at one special case". And I think the glibc implementation is horrible and doesn't know about machines where unaligned loads are cheap - which is all reasonable ones. That MERGE() macro is disgusting, and memcmp_not_common_alignment() should not exist on any sane architecture. It's literally doing extra work to make for slower accesses, when the hardware does it better natively. So honestly, I'd much rather see a much saner and simpler implementation that works well on the architectures that matter, and that don't want that "align things by hand". Aligning one of the sources by hand is fine and makes sense - so that _if_ the two strings end up being mutually aligned, all subsequent accesses are aligned. But then trying to do shift-and-masking for the possibly remaining unaligned source is crazy and garbage. Don't do it. And you never saw that, because your special FIDEDUPERANGE testcase will never have anything but mutually aligned cases. Which just shows that going from "don't care at all' to "care about one special case" is not the way to go. So I'd much rather see a simple default function that works well for the sane architectures, than go with the default code from glibc - and bad for the common modern architectures. Then architectures could choose that one with some select GENERIC_MEMCMP the same way we have select GENERIC_STRNCPY_FROM_USER for the (sane, for normal architectures) common optimized case for a special string instruction that matters a lot for the kernel. Linus