Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp705517pxf; Thu, 8 Apr 2021 11:03:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwRGRnp44Gx4vpmW7cVWccGyfUB9tgP6Z3NNBkTwQ2Q5VMBRGHgP0xP5ZP5se2Esf9WT/S2 X-Received: by 2002:a62:2cc8:0:b029:23d:bd0d:2ac3 with SMTP id s191-20020a622cc80000b029023dbd0d2ac3mr8752848pfs.41.1617904981609; Thu, 08 Apr 2021 11:03:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617904981; cv=none; d=google.com; s=arc-20160816; b=pQc8byL3TJlG8s0zoFZLLl2RXMJ2LUxah866QwgbQBg+TtCp9jr+fGm8v4IlVEYLMN DCUUvcOwon1masI2WizsERuAoQVhT77xORJ5vbGiw9yu4WwJ/y0uiOP+acWQ8cU0aoXP mZmTzRKvTW2XELXSgxzg4i8AnRexfWxqJwsY5wVXWuP9ktRksnLBShIz79AIQ3dw0Nw1 dkKDE5hH4TWKfClFW63zPd4K2YZgOjMzqAwHSWwJ5fcf1guQ5ByqS4q4ASj48tbdKt/5 gNmHtOQo1qb8K7X5mDF68XWPTVOT+bM775QZmJc3CXde2TkF8ClVJIV/4kzNyl6ofbLK 6tPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:mime-version:message-id:date :dkim-signature; bh=E42XR5KeRAoN2g3+y3om4yOJDcpHthzb/pVdZ1Nq0nk=; b=NXV1MViKUm5TRudacB5O6pkghSvtAi9lX3v8aXy1vL6Sa4MageVLHvBUMVZzjsHtQw KKfD7uNp7Go21V9EWWaMBGs1XTBgLTGVwtQangqsLkKmoMuuCG420EkydBUK9V+SA5HZ roHKOwRrakejJkbXS/tnyxvYq81kI9A7VwMnZJoh9jIdhI6l1VFihAHTSteS55fg1k7h kQLaYogCoToQrWly8MSK8658/EsMy+QcsZaJx/wO/YbRwT3C4TaaBoeRow9M7GqdU0fD N2r48VzFm/0SlhNn19XlALnruw78iLznge/S1gk4fIYOGeZJRnU7RwHx3DqboAtcPNLk 8Wpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="ZP0k/DiF"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t22si27534584pgv.74.2021.04.08.11.02.49; Thu, 08 Apr 2021 11:03:01 -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=@google.com header.s=20161025 header.b="ZP0k/DiF"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232383AbhDHSBX (ORCPT + 99 others); Thu, 8 Apr 2021 14:01:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232123AbhDHSBV (ORCPT ); Thu, 8 Apr 2021 14:01:21 -0400 Received: from mail-ed1-x549.google.com (mail-ed1-x549.google.com [IPv6:2a00:1450:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1FC8CC061760 for ; Thu, 8 Apr 2021 11:01:10 -0700 (PDT) Received: by mail-ed1-x549.google.com with SMTP id o24so1394654edt.15 for ; Thu, 08 Apr 2021 11:01:10 -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=E42XR5KeRAoN2g3+y3om4yOJDcpHthzb/pVdZ1Nq0nk=; b=ZP0k/DiFREpyUihVD3ELUkH578x3zA/bRYz+PWu1pA2BVRGnF1JE7GExXaazTvofGY Y5NTt1zBOssn5pDsq/KxfyrTm7uYXt+mcEyZwm3ggJ/YqjgjP3eSHBVOkwITrqDatnf7 Iul2VKN1XZm+j9KOLua2FGd3+WtWlf2Tle9wxirmrSnqxAsT4+NRvBCgvUdC3lO6MHC8 eeTHW5Ds3xy2dxGM5NhUzXpWPbe2c6iymwVq7LJ6coNVjxdOaI9LvfsZrv5gzZ1fSB04 pa8lKAxXCxJm8YzfoW+yybynFtVejzLeTtq9mT61bBp4tNnPqG8G62gNUbh3ZJKytuwV 1ReA== 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=E42XR5KeRAoN2g3+y3om4yOJDcpHthzb/pVdZ1Nq0nk=; b=sVSN7+avOpgqusBNFxB8ug2iS0a7/tZHl9QCXMVGp1l5tQ0DmyOV/tZ7aCdXsLFTNe 0krp0pMIDw17bXnzhIaPKUFWzxOfUlvnnH1ZejN3BhXI9TXFmHVt8oJsHMVLrKVpZjkj zMyrLiHCpxJTmdGDLbJdX43gW4bcMbBZjBgAKah+ZvNAxNnHm7l2IknXVLuBOk49CM2g V40kGWht27ZfXoGKkL4R/22GCZENFiujmjIQ12ge7H3HecvhGg4xz5HsFodaHIjnv4ov UvvEqF8Dr4U86M+lqiXaspYJcfqkahnswBZh9nvXBLI/5jfSO5r3qK5V1HeTAmYEAvGg vJhA== X-Gm-Message-State: AOAM531037KCzdjHobqt5GG6sepIW4rh6KpFYU27e3BhZSIC5Z65Lxvd RpnmTtYujh4i4W5NNle5gMjy7HqrN0j8 X-Received: from r2d2-qp.c.googlers.com ([fda3:e722:ac3:10:28:9cb1:c0a8:1652]) (user=qperret job=sendgmr) by 2002:a17:906:814a:: with SMTP id z10mr12131886ejw.476.1617904868688; Thu, 08 Apr 2021 11:01:08 -0700 (PDT) Date: Thu, 8 Apr 2021 18:01:05 +0000 Message-Id: <20210408180105.2496212-1-qperret@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.31.0.208.g409f899ff0-goog Subject: [PATCH] export: Make CRCs robust to symbol trimming From: Quentin Perret To: gregkh@linuxfoundation.org, masahiroy@kernel.org Cc: linux-kernel@vger.kernel.org, maennich@google.com, gprocida@google.com, kernel-team@android.com Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The CRC calculation done by genksyms is triggered when the parser hits EXPORT_SYMBOL*() macros. At this point, genksyms recursively expands the types, and uses that as the input for the CRC calculation. In the case of forward-declared structs, the type expands to 'UNKNOWN'. Next, the result of the expansion of each type is cached, and is re-used when/if the same type is seen again for another exported symbol in the file. Unfortunately, this can cause CRC 'stability' issues when a struct definition becomes visible in the middle of a C file. For example, let's assume code with the following pattern: struct foo; int bar(struct foo *arg) { /* Do work ... */ } EXPORT_SYMBOL_GPL(bar); /* This contains struct foo's definition */ #include "foo.h" int baz(struct foo *arg) { /* Do more work ... */ } EXPORT_SYMBOL_GPL(baz); Here, baz's CRC will be computed using the expansion of struct foo that was cached after bar's CRC calculation ('UNKOWN' here). But if EXPORT_SYMBOL_GPL(bar) is removed from the file (because of e.g. symbol trimming using CONFIG_TRIM_UNUSED_KSYMS), struct foo will be expanded late, during baz's CRC calculation, which now has visibility over the full struct definition, hence resulting in a different CRC for baz. This can cause annoying issues for distro kernel (such as the Android Generic Kernel Image) which use CONFIG_UNUSED_KSYMS_WHITELIST. Indeed, as per the above, adding a symbol to the whitelist can change the CRC of symbols that are already kept exported. As such, modules built against a kernel with a trimmed ABI may not load against the same kernel built with an extended whitelist, even though they are still strictly binary compatible. While rebuilding the modules would obviously solve the issue, I believe this classifies as an odd genksyms corner case, and it gets in the way of kernel updates in the GKI context. To work around the issue, make sure to keep issuing the __GENKSYMS_EXPORT_SYMBOL macros for all trimmed symbols, hence making the genksyms parsing insensitive to symbol trimming. Signed-off-by: Quentin Perret --- include/linux/export.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/linux/export.h b/include/linux/export.h index 6271a5d9c988..27d848712b90 100644 --- a/include/linux/export.h +++ b/include/linux/export.h @@ -140,7 +140,12 @@ struct kernel_symbol { #define ___cond_export_sym(sym, sec, ns, enabled) \ __cond_export_sym_##enabled(sym, sec, ns) #define __cond_export_sym_1(sym, sec, ns) ___EXPORT_SYMBOL(sym, sec, ns) + +#ifdef __GENKSYMS__ +#define __cond_export_sym_0(sym, sec, ns) __GENKSYMS_EXPORT_SYMBOL(sym) +#else #define __cond_export_sym_0(sym, sec, ns) /* nothing */ +#endif #else -- 2.31.0.208.g409f899ff0-goog