From patchwork Thu Apr 22 19:18:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 12219049 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8F27FC433B4 for ; Thu, 22 Apr 2021 19:21:04 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D5994613F5 for ; Thu, 22 Apr 2021 19:21:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D5994613F5 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:Cc:To:From:Subject:Mime-Version:Message-Id:Date: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=T4y0T/OIWym2K7nV/2UayREbULv5CYCrrJ3mLZ1+7x4=; b=p8eHfehQCNUTCC7LZ4b+2nN/RY vhcuiSToCtYu6LrLl6a0b19w8QfQ5bHBMKtJV3sgqM23YCwXiT35HMkpBRdIrbR3+E2nFODP39LlT 1QPQuY1ZwHIYUILxNlsGSoiLyJuRA7QVnQj3J5iAfJVOQzCOxfUBY9GQZCPQi7xQBFnPaU8OZOUzp iRS2omVcJVytLoc1kDTXKT1+DmWBiFO0V/9hmib1oFTCSEYqQ/eqbQZS7fZt72TOmbADm3vu3rqOf C5XNK5LAuHPecQZepx8sNTG3N9R1bClPUbaaJq9i09vR2Hd6lmZ8Py/hk6R1DQVS4Q5embf26XPm4 dvUYgtbw==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lZeqW-00HJXz-VO; Thu, 22 Apr 2021 19:18:41 +0000 Received: from bombadil.infradead.org ([2607:7c80:54:e::133]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lZeqT-00HJXc-4a for linux-arm-kernel@desiato.infradead.org; Thu, 22 Apr 2021 19:18:38 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Type:Cc:To:From:Subject: Mime-Version:Message-Id:Date:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To:References; bh=MG1E5LmNjEnO9dOxTFKDeqsCA70+WVWf6tsEnOy3w2I=; b=YG+Tj9GA9+VxL7HiF/L4zC+YQf /5Z/JAa6L5qLzDJyHXG7tPjQJg7eLqSgF/B5nB/CtnMtv3UiK7gyphAHGixJo7n86uktWdx072kjY VlHHJxsKUBN0Ju7AX8WRKsoHNgVf0AE+D79UEvxFe+btfx50pvV62Qn9QLURJfHMRTw06LHQ/Yf6n CQtIbjjv2ff/V3pQ1qs58xZ0dvBcalaLaZTir9FD3IMKJPhu2sNWM0zUqHlsTCAa8/NtAMn9w9iHr xTtreCi+Fzi5brBrp9zVJw19bSF/5tOMGsxXDt2evnCNq/C+Xg6gSk5y+VPZcurr08WpDkuYX30D/ vQZwi3og==; Received: from mail-ed1-x549.google.com ([2a00:1450:4864:20::549]) by bombadil.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lZeqQ-00DwVD-7L for linux-arm-kernel@lists.infradead.org; Thu, 22 Apr 2021 19:18:35 +0000 Received: by mail-ed1-x549.google.com with SMTP id h13-20020a05640250cdb02903790a9c55acso17260515edb.4 for ; Thu, 22 Apr 2021 12:18:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=MG1E5LmNjEnO9dOxTFKDeqsCA70+WVWf6tsEnOy3w2I=; b=CTGWG2bwSDUTBpl+CTY4eDft9pkPUnqmV8/Ue14lePjuizSL+3Ykhz3ZJlkal+QnAA KmhvZ7sfSQNXPG6tqUXMs8QJkAPiJPn11KDWvRl9HUMBdP4ANxpCfZnSlkj6xsnD472v BbOp5SuiR2GC/IKu3JTpZy43oHxjGNpiGELZKh6mzx4KoVlWunsSAKsHPLskOooIXxGo zQHM3ZcE/+mxx2T/YFkCjogZKItkeGAmn3t3aCcQNEOTTE7IvVWQt0WrhXHJK4bQwt8T FO7B31ZxDPW/4ymStBxmI5Bo3x+rjI4B8VsX1j43ZDMKKKj6I27utYMk8vrpQVvIULKk aQXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=MG1E5LmNjEnO9dOxTFKDeqsCA70+WVWf6tsEnOy3w2I=; b=kroSFxQGGdj5rlCZmoGzeWJGZBFvfDno47cTafoa4o5jrftLRRuX5ScOlnpZqE0mTU bcCwluw4Fu4fQzG2RVAeVlt6Gy+tVYY8DbhzesLCS5R+DjIIIaqOoRCzH8AqESzUAwyh Whv43n/tHG0rcD4T4JEzIQQOCipz5H08KsntyEB6mMG6mbAow7pGkRcdxOzAQ+RakoLt orD78h0w6RKmrCPMLuku82zwl4rgxdqX9E9qH/Q4lCawCbww9IjE3m6UEpXDbxCiDhca oFC99700xnaU1qIGDn7aXZJCRHH2vAQZxjL8EALSLy0jnXeknnRO8zwjJYthb+s5MF94 ZxvA== X-Gm-Message-State: AOAM531at54jRypIpssDwwlc378tB1oRB7hWUqx684jcRfpFiEmjPXJr 8qnd/GXiqvJtM68QKIVf/zCFQzyekQ== X-Google-Smtp-Source: ABdhPJw0dS7tM+mkrOgUOXVjg379+Sp3vzFIHd9nGv05RWaB/g7pGx/VbIxJ1BfFq+uBYqyy4wtpUM3+0g== X-Received: from elver.muc.corp.google.com ([2a00:79e0:15:13:145c:dc52:6539:7ac5]) (user=elver job=sendgmr) by 2002:a05:6402:1004:: with SMTP id c4mr17333edu.364.1619119110010; Thu, 22 Apr 2021 12:18:30 -0700 (PDT) Date: Thu, 22 Apr 2021 21:18:22 +0200 Message-Id: <20210422191823.79012-1-elver@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.31.1.498.g6c1eba8ee3d-goog Subject: [PATCH tip v2 1/2] signal, perf: Fix siginfo_t by avoiding u64 on 32-bit architectures From: Marco Elver To: elver@google.com, peterz@infradead.org, mingo@redhat.com, tglx@linutronix.de Cc: m.szyprowski@samsung.com, jonathanh@nvidia.com, dvyukov@google.com, glider@google.com, arnd@arndb.de, christian@brauner.io, axboe@kernel.dk, pcc@google.com, oleg@redhat.com, David.Laight@aculab.com, kasan-dev@googlegroups.com, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210422_121834_311893_DA39F19B X-CRM114-Status: GOOD ( 21.35 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The alignment of a structure is that of its largest member. On architectures like 32-bit Arm (but not e.g. 32-bit x86) 64-bit integers will require 64-bit alignment and not its natural word size. This means that there is no portable way to add 64-bit integers to siginfo_t on 32-bit architectures without breaking the ABI, because siginfo_t does not yet (and therefore likely never will) contain 64-bit fields on 32-bit architectures. Adding a 64-bit integer could change the alignment of the union after the 3 initial int si_signo, si_errno, si_code, thus introducing 4 bytes of padding shifting the entire union, which would break the ABI. One alternative would be to use the __packed attribute, however, it is non-standard C. Given siginfo_t has definitions outside the Linux kernel in various standard libraries that can be compiled with any number of different compilers (not just those we rely on), using non-standard attributes on siginfo_t should be avoided to ensure portability. In the case of the si_perf field, word size is sufficient since there is no exact requirement on size, given the data it contains is user-defined via perf_event_attr::sig_data. On 32-bit architectures, any excess bits of perf_event_attr::sig_data will therefore be truncated when copying into si_perf. Since si_perf is intended to disambiguate events (e.g. encoding relevant information if there are more events of the same type), 32 bits should provide enough entropy to do so on 32-bit architectures. For 64-bit architectures, no change is intended. Fixes: fb6cc127e0b6 ("signal: Introduce TRAP_PERF si_code and si_perf to siginfo") Reported-by: Marek Szyprowski Tested-by: Marek Szyprowski Reported-by: Jon Hunter Tested-by: Jon Hunter Signed-off-by: Marco Elver --- v2: * Update commit message wording to be clearer and mentioned __packed, as pointed out by David Laight. I'm sure some time in the future somebody will wonder and perhaps run into the same issue, so let's try to give as much background as we can... v1: https://lkml.kernel.org/r/20210422064437.3577327-1-elver@google.com Note: I added static_assert()s to verify the siginfo_t layout to arch/arm and arch/arm64, which caught the problem. I'll send them separately to arm&arm64 maintainers respectively. --- include/linux/compat.h | 2 +- include/uapi/asm-generic/siginfo.h | 2 +- tools/testing/selftests/perf_events/sigtrap_threads.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/linux/compat.h b/include/linux/compat.h index c8821d966812..f0d2dd35d408 100644 --- a/include/linux/compat.h +++ b/include/linux/compat.h @@ -237,7 +237,7 @@ typedef struct compat_siginfo { u32 _pkey; } _addr_pkey; /* used when si_code=TRAP_PERF */ - compat_u64 _perf; + compat_ulong_t _perf; }; } _sigfault; diff --git a/include/uapi/asm-generic/siginfo.h b/include/uapi/asm-generic/siginfo.h index d0bb9125c853..03d6f6d2c1fe 100644 --- a/include/uapi/asm-generic/siginfo.h +++ b/include/uapi/asm-generic/siginfo.h @@ -92,7 +92,7 @@ union __sifields { __u32 _pkey; } _addr_pkey; /* used when si_code=TRAP_PERF */ - __u64 _perf; + unsigned long _perf; }; } _sigfault; diff --git a/tools/testing/selftests/perf_events/sigtrap_threads.c b/tools/testing/selftests/perf_events/sigtrap_threads.c index 9c0fd442da60..78ddf5e11625 100644 --- a/tools/testing/selftests/perf_events/sigtrap_threads.c +++ b/tools/testing/selftests/perf_events/sigtrap_threads.c @@ -44,7 +44,7 @@ static struct { } ctx; /* Unique value to check si_perf is correctly set from perf_event_attr::sig_data. */ -#define TEST_SIG_DATA(addr) (~(uint64_t)(addr)) +#define TEST_SIG_DATA(addr) (~(unsigned long)(addr)) static struct perf_event_attr make_event_attr(bool enabled, volatile void *addr) {