From patchwork Thu Nov 3 15:24:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 14948 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp604523wru; Thu, 3 Nov 2022 08:35:00 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4K/5ffGp6KJtiEFvMl3zK2d9a8vf3vkL2zZLZlHrgcVbZ7BavdncrcuWtpOFwO1Sc6732S X-Received: by 2002:a05:6402:42d0:b0:457:d16e:283d with SMTP id i16-20020a05640242d000b00457d16e283dmr30449741edc.395.1667489700624; Thu, 03 Nov 2022 08:35:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667489700; cv=none; d=google.com; s=arc-20160816; b=UfbmF6EUngHEIUrpayhGF7ICpCLQP/pVnpDSAeZ/37gOA3jPcO0JlyDCHCrqrvr6mP Z7c1kMhpCwQg4DVOLdz3kYeOgFy+FbSPuxvHnpQmb68woIH0LqiLkQk1LtA+f2Qhl8I+ LDw6ilbl8Wj/Q3+gnW1vomKl9ZvnzfxcQNGdh9ZWpRStiLbucgHeeHmk4elZDLDiYjBL bG3KSDha7UpQRlpk7aoovcB0WtIL8CLHs+W++KzsZ1d+v6K78R8nRrrfD2dKmmWE95a+ 0sF8DYAIF7kTeLHv7lzPAhYWNC71pgkFRbxeFEK8414Ziu79632yEyvHQHzpFSshOQif /8Uw== 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 :message-id:date:subject:cc:to:dkim-signature:from; bh=X3Pmb+Zb3m3++cDfQs/cvoxtDi0mbjsQq8E50s56KD4=; b=zEoieN4QkcHLFYs7YfK4eYjn3r6JyG9OcL1O4w1eA+WGh7zzWx4bTrQOnC/wuC4UaV IdiT2v8DfZNPjsl/sSHjDQWmwbmiIgbvgcakLu+GZZMe82+3O1ST8z6/lm9zkzO6RSpZ nN52kdRC9Sy1iR78cMgszfSlkt1A/NTHNPJ32snRC+Hk1X5EWPJlkdUPyMwWAb17pA/Q LMBBJSEG+6GcjlB0IVljPyDgtbNstI66ohGeHFh13WGHI8TIs7wff9WddbJrBLMHa5ie 3PkWIyHWfNiLtEEA+Hir3U0G/JcGA57GmxoautmUXLOvM/kSTaL7R8UtyCjgE1eFdHMG 8qQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@weissschuh.net header.s=mail header.b=UpfoN063; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j11-20020a05640211cb00b004517955b673si1941793edw.124.2022.11.03.08.34.35; Thu, 03 Nov 2022 08:35:00 -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=fail header.i=@weissschuh.net header.s=mail header.b=UpfoN063; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229877AbiKCPaL (ORCPT + 99 others); Thu, 3 Nov 2022 11:30:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231801AbiKCP3n (ORCPT ); Thu, 3 Nov 2022 11:29:43 -0400 Received: from todd.t-8ch.de (todd.t-8ch.de [IPv6:2a01:4f8:c010:41de::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97052167C4; Thu, 3 Nov 2022 08:29:14 -0700 (PDT) From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=weissschuh.net; s=mail; t=1667489352; bh=PZByBmsOolWMq3HddyC3qa9xFMg1ubeZTofhZPwh6dU=; h=From:To:Cc:Subject:Date:From; b=UpfoN0634Tdc0FuIrNGCbUamoyuSTtDZbAwWz8a4BCoRn7haOurZLRe2Tdyf9QjHn UY5Bb+FCaVDRXVuQIDXIzGNlwYVV+aCniBlt6JUuMaAPUnVCl8TPiGZaGF94ScseXJ +txdOwwgFAPoNDH/ngArRZvvG+MJm/b4ZPp+zTDo= To: Greg KH , "Rafael J. Wysocki" Cc: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Karel Zak , Masatake YAMATO , linux-api@vger.kernel.org Subject: [PATCH v3] kernel/ksysfs.c: export kernel cpu byteorder Date: Thu, 3 Nov 2022 16:24:07 +0100 Message-Id: <20221103152407.3348-1-linux@weissschuh.net> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1667489045; l=3398; s=20211113; h=from:subject; bh=PZByBmsOolWMq3HddyC3qa9xFMg1ubeZTofhZPwh6dU=; b=v15hMi/ivTX5CWvpzMcrySqQJkPXO7/fXMBiavSMAxGqtvwsL8eF+Mp4fP0ecL1SX2Iwzmu4fohL FraMLAZ9DpcuMeJIWm+xwp7YEopLU1TeDLC3VXEdM/sZGLXRwIdj X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=9LP6KM4vD/8CwHW7nouRBhWLyQLcK1MkP6aTZbzUlj4= X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748489680353929225?= X-GMAIL-MSGID: =?utf-8?q?1748489680353929225?= Certain files in procfs are formatted in byteorder-dependent formats. For example the IP addresses in /proc/net/udp. When using emulation like qemu-user, applications are not guaranteed to be using the same byteorder as the kernel. Therefore the kernel needs to provide a way for applications to discover the byteorder used in API-filesystems. Using systemcalls is not enough because these are intercepted and translated by the emulation. Also this makes it easier for non-compiled applications like shellscripts to discover the byteorder. Signed-off-by: Thomas Weißschuh --- Development of userspace part: https://github.com/util-linux/util-linux/pull/1872 Changelog: v1: https://lore.kernel.org/lkml/20221101005043.1791-1-linux@weissschuh.net/ v1->v2: * Move file to /sys/kernel/byteorder v2: https://lore.kernel.org/lkml/20221101130401.1841-1-linux@weissschuh.net/ v2->v3: * Fix commit title to mention sysfs * Use explicit cpu_byteorder name * Use sysfs_emit * Use myself as Contact * Reword commit message --- .../ABI/testing/sysfs-kernel-cpu_byteorder | 12 ++++++++++++ kernel/ksysfs.c | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-kernel-cpu_byteorder base-commit: 8e5423e991e8cd0988d0c4a3f4ac4ca1af7d148a diff --git a/Documentation/ABI/testing/sysfs-kernel-cpu_byteorder b/Documentation/ABI/testing/sysfs-kernel-cpu_byteorder new file mode 100644 index 000000000000..f0e6ac1b5356 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-kernel-cpu_byteorder @@ -0,0 +1,12 @@ +What: /sys/kernel/cpu_byteorder +Date: February 2023 +KernelVersion: 6.2 +Contact: Thomas Weißschuh +Description: + The endianness of the running kernel. + + Access: Read + + Valid values: + "little", "big" +Users: util-linux diff --git a/kernel/ksysfs.c b/kernel/ksysfs.c index 65dba9076f31..2df00b789b90 100644 --- a/kernel/ksysfs.c +++ b/kernel/ksysfs.c @@ -6,6 +6,7 @@ * Copyright (C) 2004 Kay Sievers */ +#include #include #include #include @@ -20,6 +21,14 @@ #include /* rcu_expedited and rcu_normal */ +#if defined(__LITTLE_ENDIAN) +#define CPU_BYTEORDER_STRING "little" +#elif defined(__BIG_ENDIAN) +#define CPU_BYTEORDER_STRING "big" +#else +#error Unknown byteorder +#endif + #define KERNEL_ATTR_RO(_name) \ static struct kobj_attribute _name##_attr = __ATTR_RO(_name) @@ -34,6 +43,14 @@ static ssize_t uevent_seqnum_show(struct kobject *kobj, } KERNEL_ATTR_RO(uevent_seqnum); +/* cpu byteorder */ +static ssize_t cpu_byteorder_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%s\n", CPU_BYTEORDER_STRING); +} +KERNEL_ATTR_RO(cpu_byteorder); + #ifdef CONFIG_UEVENT_HELPER /* uevent helper program, used during early boot */ static ssize_t uevent_helper_show(struct kobject *kobj, @@ -215,6 +232,7 @@ EXPORT_SYMBOL_GPL(kernel_kobj); static struct attribute * kernel_attrs[] = { &fscaps_attr.attr, &uevent_seqnum_attr.attr, + &cpu_byteorder_attr.attr, #ifdef CONFIG_UEVENT_HELPER &uevent_helper_attr.attr, #endif