Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1351413rwb; Tue, 27 Sep 2022 11:46:48 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4qIari8FIHVz9fUxBj/JJMmcfoF1EOHi3w7Y6vJZIIbBG67xu1J07T8RQRFPT2ygdwc6T3 X-Received: by 2002:a17:907:728e:b0:782:8e91:64c8 with SMTP id dt14-20020a170907728e00b007828e9164c8mr21121813ejc.36.1664304407836; Tue, 27 Sep 2022 11:46:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664304407; cv=none; d=google.com; s=arc-20160816; b=CuI7GvLXRj7q9afToYBSJGrSloPF9bwxENROFJ+SgYyKUo48/dAYvRqmTvFHQBA/hi X6I5jllxbuR5TkLiB1lg3fU67KHkL8NxCRzCbI9RW2a9uKWDbi4xZtcCTU4oAGQXlIaS AxamH0VrA5XMeNhBJepOIha8f93aQEXgr6megE4wtIuIWfj/jLDOUimhNVo2tfsLhh6F 0eetz5FPgMO+sGblmd8RKwwMZdlgbioc5UQ0hUQVCvO0WYiZRslaJb5W2ngmD65oSyuN NMtRuIm7ZUhdnE/DWxZ60SrNhk1dvXbmsXNoB4ZermHzyuF97QHfGD65zKVT1vp+wCB+ mmIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=e7R+mDLju2JroAcNFMiEg35mWzGVebbcux9nnUdL0+s=; b=XAOvgVTZqKimxiWfhgGNuYRXsNiia2NC6i7rFkNyD280Aaekj1klJAgYUfctwScJVO d9WXYn4oJFmCeBr+osKW3KSaLIlXUoEtaykgm4PAdFEYiS3nu2rGCeMTqhMlOV8Ad8G4 czUVAhst0HlA0m9TAyBA1vR8ijxPi7BlH/XpKTKCnS+tpDFPUO6vdQOCTmGHHQ0Zq/ft oQ9V1/bFZdbyTanpe9fTuRTLM/+yRpHhQcpdn7autbKZfOVsuvrQuOHL25UoDCDtDHr6 dWlT/WAfH5DhTpP1d2yOYvS3UiYo8dm3eC6fgee/zNJcSqyCyZqnuq9tFOIydGh6o60W lPaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="XP3ei/YY"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q12-20020a50aa8c000000b0045485f48f48si2222610edc.549.2022.09.27.11.46.22; Tue, 27 Sep 2022 11:46:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="XP3ei/YY"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230265AbiI0SlI (ORCPT + 99 others); Tue, 27 Sep 2022 14:41:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229542AbiI0SlG (ORCPT ); Tue, 27 Sep 2022 14:41:06 -0400 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CFAD3F185A for ; Tue, 27 Sep 2022 11:41:04 -0700 (PDT) Received: by mail-pl1-x62c.google.com with SMTP id iw17so9907551plb.0 for ; Tue, 27 Sep 2022 11:41:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=e7R+mDLju2JroAcNFMiEg35mWzGVebbcux9nnUdL0+s=; b=XP3ei/YY35BS6LARIxHchz6xoHvIGGmZNaPaqubd/9AtflMvF6FPymZ23lgOvUUlX9 fq2g0GoJgzf55NsbkZ1ITvHY3yZW0lBZgRmpmhyDYqu0+Ww8kXYgc0kcf8ND0KfCFdu8 q6xmnfVySqteH9LyV2p4GPqh3RYh2csuyU7HY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=e7R+mDLju2JroAcNFMiEg35mWzGVebbcux9nnUdL0+s=; b=HmVhXDr+Xy3uFAi08I422FI17caaJzAtqFzPHmK8oE4XtfHzA7fOQ4HukXGpoqv9v9 lPkbEXIcqsyoqTzd9XwOrTa/BkLnjHes1St5pjY4W+rPV1X3+SmQdvTOC09ZeCsovmTV HV93YShwXnk0QbOqAGVKFabBF10//qPQSq0/7TPkNu0vfYwe118KLV7sF5k7ra8BzrL4 Xs5X5rey1vpXseTfHaayWPW0R9OK7yutQuNTS0Ljosf2jIEQfzgIG0dryANPbfitgDgG CMymzVAaD7m3NC8j9DJSYhbY24Py2cHzMJKHHujU4uLNZlsvnDp7ajHVAmMd1XI4DMl6 IpbQ== X-Gm-Message-State: ACrzQf2DFBsbDarf8UQbbjSV9T8JDSX+zx24wCK/QpaBmkjvI1LH488B SigQlacMU+TIoz8U38TppRkQ3A== X-Received: by 2002:a17:90b:380d:b0:202:56b2:4ef8 with SMTP id mq13-20020a17090b380d00b0020256b24ef8mr6134704pjb.2.1664304064224; Tue, 27 Sep 2022 11:41:04 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id h15-20020a170902f70f00b00173411a4385sm1912789plo.43.2022.09.27.11.41.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Sep 2022 11:41:03 -0700 (PDT) Date: Tue, 27 Sep 2022 11:41:02 -0700 From: Kees Cook To: Nick Desaulniers Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Peter Zijlstra , linux-kernel@vger.kernel.org, Linus Torvalds , llvm@lists.linux.dev, Andy Lutomirski Subject: Re: [PATCH v2] x86, mem: move memmove to out of line assembler Message-ID: <202209271126.605B4FF@keescook> References: <20220927172839.3708280-1-ndesaulniers@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220927172839.3708280-1-ndesaulniers@google.com> X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Sep 27, 2022 at 10:28:39AM -0700, Nick Desaulniers wrote: > When building ARCH=i386 with CONFIG_LTO_CLANG_FULL=y, it's possible > (depending on additional configs which I have not been able to isolate) > to observe a failure during register allocation: > > error: inline assembly requires more registers than available > > when memmove is inlined into tcp_v4_fill_cb() or tcp_v6_fill_cb(). > > memmove is quite large and probably shouldn't be inlined due to size > alone. A noinline function attribute would be the simplest fix, but > there's a few things that stand out with the current definition: > > In addition to having complex constraints that can't always be resolved, > the clobber list seems to be missing %bx and %dx, and possibly %cl. By > using numbered operands rather than symbolic operands, the constraints > are quite obnoxious to refactor. > > Having a large function be 99% inline asm is a code smell that this > function should simply be written in stand-alone out-of-line assembler. > That gives the opportunity for other cleanups like fixing the > inconsistent use of tabs vs spaces and instruction suffixes, and the > label 3 appearing twice. Symbolic operands and local labels would > provide this code with a fresh coat of paint. > > Moving this to out of line assembler guarantees that the compiler cannot > inline calls to memmove. > > This has been done previously for 64b: > commit 9599ec0471de ("x86-64, mem: Convert memmove() to assembly file > and fix return value bug") > > Signed-off-by: Nick Desaulniers Unfortunately, it seems something has gone wrong with this implementation. Before the patch: $ ./tools/testing/kunit/kunit.py run --arch=i386 memcpy ... [11:26:24] [PASSED] memmove_test ... After the patch: $ ./tools/testing/kunit/kunit.py run --arch=i386 memcpy ... [11:25:59] # memmove_test: ok: memmove() static initializers [11:25:59] # memmove_test: ok: memmove() direct assignment [11:25:59] # memmove_test: ok: memmove() complete overwrite [11:25:59] # memmove_test: ok: memmove() middle overwrite [11:25:59] # memmove_test: EXPECTATION FAILED at lib/memcpy_kunit.c:176 [11:25:59] Expected dest.data[i] == five.data[i], but [11:25:59] dest.data[i] == 136 [11:25:59] five.data[i] == 0 [11:25:59] line 176: dest.data[10] (0x88) != five.data[10] (0x00) [11:25:59] # memmove_test: ok: memmove() argument side-effects [11:25:59] # memmove_test: ok: memmove() overlapping wr\xf0te [11:25:59] not ok 3 - memmove_test [11:25:59] [FAILED] memmove_test ... data[10] starts set as 0x99, and in theory gets 0x0 written to it, but the self-test sees 0x88 there. (?!) It seems the macro side-effect test caught something else entirely? -Kees -- Kees Cook