Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1667814pxj; Wed, 19 May 2021 11:02:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxBg3l+80dZ9xIEgCJXjscwDfQ862d1YpkzpYZVCWaKnQ22ibSN5iiW5ckhCKn3emR5bxHw X-Received: by 2002:a6b:4105:: with SMTP id n5mr728605ioa.148.1621447350817; Wed, 19 May 2021 11:02:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621447350; cv=none; d=google.com; s=arc-20160816; b=ROeHE7IiK3GgY6m/t1lI6NBBtSWb5ljuDK2PVyrk0+grD2kHx8k/YeuPsunKK0/BCK WI7tO7BOMTjVNJKCsq6ywNJt1hfPV+whpggKnfnGnRmxTIifdcDVH/rrL2wyJsPdprCr Xad3I18bs7naHcxU37fIkkZMvFZm5D1GwJM+tf+h6Q2UxnKxXwCwp75gejwybx0JLlL3 hlW7VbQLIYrDsXeZd9WeRcjrtto8Zd+oUzTzw3vMTHSf8zNNbXehVd1d4AzFGxl+0tr9 FYgQUTwFtvnHUkCz0EKtkjrQPzTl6wpxdexgSr7GfMaTKqeh0kB+KEJzoT6jJm0YHHUw ag+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:content-disposition:mime-version :message-id:subject:cc:to:from:date:dkim-signature; bh=+ggadvPF/wBhvvLjqD+tk3mIYZSXppcfW2r188p7A2Y=; b=jpRrZi4nm1oe7+ZSC0N8FddSTGsVBzr7SXUWKB1iXAybzNif4thZpGDiO0SUT2zRPn L2nw73naOfCktaziQ8a3mFcy0TmCmsOQavFAA0V2yQFlvSMS3Nsw7sYUM2QcMD1EkMmQ WGEf7Y8OPKw1dmcHAQD/R3ZXkHzLnXHnvlgWiw4v6xIk+Trgq5iEUbaQfi6Mn7ow+UA3 EggQjVW2N3AskxVI8dH0OAaGGyBHRj/SwJYAPs6HNhK+5PVMXupFJzolkVDxsHr1tCHh x/EpiyyKmtcIgoZekd2gdB8xWYPkccQWJ5RBciEyBEkRPHs9HpMOeM/Di/1H9Kv7C1L4 XcTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chrisdown.name header.s=google header.b=NJ+k8d7I; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chrisdown.name Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h31si28408789jav.116.2021.05.19.11.02.16; Wed, 19 May 2021 11:02:30 -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=@chrisdown.name header.s=google header.b=NJ+k8d7I; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chrisdown.name Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233080AbhERMBj (ORCPT + 99 others); Tue, 18 May 2021 08:01:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233219AbhERMBh (ORCPT ); Tue, 18 May 2021 08:01:37 -0400 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A12A5C061573 for ; Tue, 18 May 2021 05:00:19 -0700 (PDT) Received: by mail-wm1-x336.google.com with SMTP id b19-20020a05600c06d3b029014258a636e8so1327583wmn.2 for ; Tue, 18 May 2021 05:00:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=+ggadvPF/wBhvvLjqD+tk3mIYZSXppcfW2r188p7A2Y=; b=NJ+k8d7I5+RS9VOax/g12nzi/snsdbNv63dy8OB1RCUNltpv710MeXrKfvtOdTO4C3 Sxe5/14Hd0XZxG2484xtE9sBYlFBsA2jDKSZBJIj35aSf8hrdHX2Vhw3mplaRmITUfqp s5AwPMBdsDFUhYfKaQX3fpEnwxGxtyE8Jv3bg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=+ggadvPF/wBhvvLjqD+tk3mIYZSXppcfW2r188p7A2Y=; b=foNbFSvNLtmBrjQRroP2Pquf80xyfuppW6puJD1ExMDZ/fB4IGgZDOAHSpRUT56XMx w1DLR02JSEvwa17liExltqmk/1AcPFJEAq9BO6doU00b5mIABbqwXTCh9k46pNZd0WiT wPRgVDlI+WX0UqKogH9PqaJPrD6CLIqMXcsyP1HB+UnIW5Nbxd1WSndE/HyeNtoIUDzU GY6cpO8tOsiHbQB1X+ku+v2in00p94mOIDm4Y4P/swaIlec5eDVMuaoW62xXm31tT+QW fuDz3HlI6m707PcVA/JPYCE2EKBvy6VOmrG03KzdP5dAoIxVH9c9uZjW+CH9Y4uamcfB Hx2Q== X-Gm-Message-State: AOAM533qBLyJZ41J8nWASXro6+BWfTf7rQJCPAspWX+Z83wsqsylqgHU C1uWWYx1H9RpqiYcrR0VvGaktxAYmSfBankfKsA= X-Received: by 2002:a7b:c759:: with SMTP id w25mr4547426wmk.54.1621339217763; Tue, 18 May 2021 05:00:17 -0700 (PDT) Received: from localhost ([2620:10d:c093:400::5:5c4a]) by smtp.gmail.com with ESMTPSA id q3sm18815469wrr.43.2021.05.18.05.00.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 May 2021 05:00:17 -0700 (PDT) Date: Tue, 18 May 2021 13:00:16 +0100 From: Chris Down To: linux-kernel@vger.kernel.org Cc: Petr Mladek , Jessica Yu , Sergey Senozhatsky , John Ogness , Steven Rostedt , Greg Kroah-Hartman , Johannes Weiner , Kees Cook , Andy Shevchenko , Rasmus Villemoes , kernel-team@fb.com Subject: [PATCH v6 0/4] printk: Userspace format indexing support Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/2.0.7 (481f3800) (2021-05-04) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We have a number of systems industry-wide that have a subset of their functionality that works as follows: 1. Receive a message from local kmsg, serial console, or netconsole; 2. Apply a set of rules to classify the message; 3. Do something based on this classification (like scheduling a remediation for the machine), rinse, and repeat. As a couple of examples of places we have this implemented just inside Facebook, although this isn't a Facebook-specific problem, we have this inside our netconsole processing (for alarm classification), and as part of our machine health checking. We use these messages to determine fairly important metrics around production health, and it's important that we get them right. While for some kinds of issues we have counters, tracepoints, or metrics with a stable interface which can reliably indicate the issue, in order to react to production issues quickly we need to work with the interface which most kernel developers naturally use when developing: printk. Most production issues come from unexpected phenomena, and as such usually the code in question doesn't have easily usable tracepoints or other counters available for the specific problem being mitigated. We have a number of lines of monitoring defence against problems in production (host metrics, process metrics, service metrics, etc), and where it's not feasible to reliably monitor at another level, this kind of pragmatic netconsole monitoring is essential. As one would expect, monitoring using printk is rather brittle for a number of reasons -- most notably that the message might disappear entirely in a new version of the kernel, or that the message may change in some way that the regex or other classification methods start to silently fail. One factor that makes this even harder is that, under normal operation, many of these messages are never expected to be hit. For example, there may be a rare hardware bug which one wants to detect if it was to ever happen again, but its recurrence is not likely or anticipated. This precludes using something like checking whether the printk in question was printed somewhere fleetwide recently to determine whether the message in question is still present or not, since we don't anticipate that it should be printed anywhere, but still need to monitor for its future presence in the long-term. This class of issue has happened on a number of occasions, causing unhealthy machines with hardware issues to remain in production for longer than ideal. As a recent example, some monitoring around blk_update_request fell out of date and caused semi-broken machines to remain in production for longer than would be desirable. Searching through the codebase to find the message is also extremely fragile, because many of the messages are further constructed beyond their callsite (eg. btrfs_printk and other module-specific wrappers, each with their own functionality). Even if they aren't, guessing the format and formulation of the underlying message based on the aesthetics of the message emitted is not a recipe for success at scale, and our previous issues with fleetwide machine health checking demonstrate as much. This provides a solution to the issue of silently changed or deleted printks: we record pointers to all printk format strings known at compile time into a new .printk_index section, both in vmlinux and modules. At runtime, this can then be iterated by looking at /printk/index/, which emits the following format, both readable by humans and able to be parsed by machines: $ head -1 vmlinux; shuf -n 5 vmlinux # filename:line function "format" <5> block/blk-settings.c:661 disk_stack_limits "%s: Warning: Device %s is misaligned\n" <4> kernel/trace/trace.c:8296 trace_create_file "Could not create tracefs '%s' entry\n" <6> arch/x86/kernel/hpet.c:144 _hpet_print_config "hpet: %s(%d):\n" <6> init/do_mounts.c:605 prepare_namespace "Waiting for root device %s...\n" <6> drivers/acpi/osl.c:1410 acpi_no_auto_serialize_setup "ACPI: auto-serialization disabled\n" This mitigates the majority of cases where we have a highly-specific printk which we want to match on, as we can now enumerate and check whether the format changed or the printk callsite disappeared entirely in userspace. This allows us to catch changes to printks we monitor earlier and decide what to do about it before it becomes problematic. There is no additional runtime cost for printk callers or printk itself, and the assembly generated is exactly the same. --- v2: - Use seq_printf instead of step by step accumulation - Scope fptr closer to its use - Prevent seq_file from needing to alloc a new buffer - Always provide KERN_SOH + level, even if caller omitted it - Provide one file per module - Update changelog to show ease of parsing - Provide printk -> _printk for ARCH=um (thanks, LKP robot) - Move to debugfs (thanks, Steven) --- v3: - Reduce locking during open by moving size lifetime to the inode - Don't explicitly check for debugfs errors (thanks, gregkh) --- v4: - Fix a missed `extern int printk` fixup in ia64 (thanks, lkp) --- v5: Thanks Petr for the extensive feedback. - Move all module handling from module notifier to module.[ch] directly - Change to readable output format - Handled by new string_escape(ESCAPE_PRINTF) - Add file/line/function information - Mass rename everything to printk_index/CONFIG_PRINTK_INDEX/pi_* - As a result, this version does away with the mutex/hashtable - Use seq_file iteration API instead of simple_open - Remove debugfs file first to avoid ENOENT - Tear down backing datastructure if debugfs file creation fails - Move code under ifdef guard to kernel/printk/index.c - Add pi_sec (formerly printk_fmt_sec) documentation - Handle coexisting LOG_CONT + level - Add header to debugfs output - ...and probably some other stuff I forgot. :-) --- v6: - Rebase on next-20210518 - Remove pi_sec, do on demand lookups instead - Lookup debugfs file on demand - pi_sec_{store,remove} now only changes debugfs, so renamed - Don't check for debugfs failure (again) - Extract lookup logic into pi_get_entry - Make the stored struct `const __used` - Use _p_func directly, which allows including void-returning functions - Move more stuff outside PRINTK_INDEX #ifdef using __printk_index_emit - Scope __{start,stop}_printk_index more tightly - Move parse_prefix to printk_parse_prefix in internal.h - Put PRINTK_INDEX definition below TRACEDATA - Use a module notifier again - pi_object -> pi_entry, with clearer vars - Reword Kconfig - Split out printk_info_flags/string_helpers stuff into patches - Use seq_escape_str directly now that ESCAPE_APPEND exists - Add dev_printk indexing support Chris Down (4): string_helpers: Escape double quotes in escape_special printk: Straighten out log_flags into printk_info_flags printk: Userspace format indexing support printk: index: Add indexing support to dev_printk MAINTAINERS | 5 + arch/arm/kernel/entry-v7m.S | 2 +- arch/arm/lib/backtrace-clang.S | 2 +- arch/arm/lib/backtrace.S | 2 +- arch/arm/mach-rpc/io-acorn.S | 2 +- arch/arm/vfp/vfphw.S | 6 +- arch/ia64/include/uapi/asm/cmpxchg.h | 4 +- arch/openrisc/kernel/entry.S | 6 +- arch/powerpc/kernel/head_fsl_booke.S | 2 +- arch/um/include/shared/user.h | 3 +- arch/x86/kernel/head_32.S | 2 +- drivers/base/core.c | 6 +- include/asm-generic/vmlinux.lds.h | 13 ++ include/linux/dev_printk.h | 63 ++++++--- include/linux/module.h | 5 + include/linux/printk.h | 95 ++++++++++++- init/Kconfig | 14 ++ kernel/module.c | 5 + kernel/printk/Makefile | 1 + kernel/printk/index.c | 198 +++++++++++++++++++++++++++ kernel/printk/internal.h | 4 + kernel/printk/printk.c | 60 ++++---- kernel/printk/printk_ringbuffer.h | 6 + lib/string_helpers.c | 4 + lib/test-string_helpers.c | 14 +- 25 files changed, 448 insertions(+), 76 deletions(-) create mode 100644 kernel/printk/index.c -- 2.31.1