Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp2584527lqb; Tue, 28 May 2024 04:48:24 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV1//Wb+B3ccAlLQs1SlIqtPWgke/ZF5O8/DV40uJMLhhsNP2bbejOvD3ifiEMP1M/ZpK8LmW2TnSR9LeQRJMw+Q5yWq5NM2KcvQvBbRA== X-Google-Smtp-Source: AGHT+IH3/S0hnYxXbuKypb8qXJWmV1QiE6k6jYGdMG79sR6lfckkw7roudDBk445i9hXfaRWl8fl X-Received: by 2002:a05:6871:5b04:b0:23c:2d6e:96c0 with SMTP id 586e51a60fabf-24ca145ef65mr12807870fac.39.1716896904299; Tue, 28 May 2024 04:48:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716896904; cv=pass; d=google.com; s=arc-20160816; b=W+pS3MsmUWpysq+1XSntOlKf5AMS5oQDciDVm/tOzQPTVTQzT8MFyT0oqCw51HIGuf Sjh0toIY/du5fA5AZffQOrpGqDHFb4ye0dIOKevnW929i3Qa1ka3ZHczta1a+SAnMknz eoUXTIJDc1jshKkn/PcZ0ETGu7pZlgJF4+wKfiws1IY3eoHw8q7GZknsIxZSQEY6DnJ4 if6RDr1Fh25mVmVVC97BMFI8QyvMyOOU2y4NSdb6OAse9I1vut40P8N7XrsjJZ7lLeLD Ovvqb8j1Lbox7dk1E+gfm3qngNvV9NKU9r4jPK+zRWB8KxSevSK/qU8VyfE1enaErf2Y 8UlQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=5+fy7bxhmTSYRqmL4m3IRHCvKHkrSDKxVpOHKJ74CWc=; fh=PqQ+kWb6fG02bDA4ZqnJTy25jHOmjilcS0aRZD2BfPc=; b=tLUeE3czLKXx46fSWVmJDpKfgyOfh+9b0iSzsxx4CD/rLv1Y8ieZ1YSwSseZ35uKGt oI58Whqe9MVltJWyXZdkdP2C4j/6LJiHjQ3Gr0uWPCWluOmCYA9r47GQc10haWnZUmEs JRhnvw2DoYcIWcMzZLI7xW64cSvXtR09XjFqSAWsd5WV+0AtoHDvF9YXFvIrsw7Akqqg JspSEiSPFIOyedqRoGivLxvpE6/SJC+nlwfnlLroRMU3P8U9iKsnrr1LTaaTxR2XpMcR 8gHnHrKG9l0kUGtsN+BKrslpk7F2zDIY0ZbMHYa5ink5MPJtxw5AvUWIIKD1WEO7qjVA s+oA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=v4i+jrAp; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-192188-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-192188-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 41be03b00d2f7-68229d0cfadsi8259502a12.623.2024.05.28.04.48.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 May 2024 04:48:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-192188-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=v4i+jrAp; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-192188-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-192188-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id C2D0728A2D6 for ; Tue, 28 May 2024 11:37:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C070D16C69D; Tue, 28 May 2024 11:36:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="v4i+jrAp" Received: from mail-qv1-f46.google.com (mail-qv1-f46.google.com [209.85.219.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7F77816ABCC for ; Tue, 28 May 2024 11:36:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716896215; cv=none; b=c5c+/2QaD9DjF34LDC1fchCor2T3O0C0qpbbWFBwcsgJ8HhwEqtMm//PEDTcgTz3hqmFclRDlwILBfyNpRaYwM+90QB3/llHl+sDoRgG7YJCaqWUeqw+PtmxVRdlMS8TKwbmPX2QkQcTOY01MO8e9ALdlnbO3mGFesqqRyE6P0I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716896215; c=relaxed/simple; bh=M0MaGrmyVgPDZ5O+a4ObngblY3U7Ng60oW1EiVKZdNs=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=VW+Yld+OX/0SuzyHp7dq5G/BJjW64xByzUOL6voSo8GHAAGW8RjpNLdf3Yrw3MSwgOj310PBQxA0+8F1Hwl19YcFaoM4J9fAW2p5lDd5Lvo4MWKZn22uIOaNT1ziQYFa8+rIIQBEbnsOXu6JLhUxaXDQv2u7HDt1zRQlSgRL8Rs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=v4i+jrAp; arc=none smtp.client-ip=209.85.219.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-qv1-f46.google.com with SMTP id 6a1803df08f44-6ab9d01cb11so4202196d6.1 for ; Tue, 28 May 2024 04:36:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1716896213; x=1717501013; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=5+fy7bxhmTSYRqmL4m3IRHCvKHkrSDKxVpOHKJ74CWc=; b=v4i+jrApphkfH7mcoJKI5zVjB4itgKSGZ2HFlkTD7h43d56AyRZq3dltB/wJ0IAh+E cpIidJBaGW6SkbWx3lRACVkNXrHKlk77GQdLzBHDdu9uHEazyYA4qMWxCVv6v05/Bypg PO+zf+ggzh4C8GGEK2UDbHu461Yf7CpQVztYPCXHpbInUFGkkc7NLVSW1iLv0COcXHFH SaKhx3EcEIaxcjJmtZbsBbBFqa9vK8+JymgrK9DZa/yWz0OaiYL0JGofLtqgAREm3TpV EFCCwIqDTNEmJSeisn3DjZlGbfC4DUhX7doQKqB93Up12YmM3bpPaIUzKUsY8hsMzgUz uNTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716896213; x=1717501013; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5+fy7bxhmTSYRqmL4m3IRHCvKHkrSDKxVpOHKJ74CWc=; b=og7DwptlkVjGtxv0bD55GGfDvvO013789xzYkTyvbApASoVzq57w9iJ0F4MnYyPL61 aqeuz2zSabujNcxpj9sanmvzvR6wt/HLbaVInDAfm5Gq9Sbs5kaRiJmeNepPwjPlwURk +I19l1H9JtsZmi8UfWcPA95s8VvFvmvt3E4W1LPwY64nZEykN+6LpqMVyb5cvgjPWB+H g2Nz4N72kD2VD1BnQG5rANYzS7vnWEm2XEg9u344dvDn706sasdkthCwMK0MtKy0XELZ j+nZUXRgole4actXWugbqZ0kJ+r2KRnhOtYaWBk+wt43f7cELbgO0jyP8a+2S+j0UIpX S54Q== X-Forwarded-Encrypted: i=1; AJvYcCWAaS7ydFCo4zGPLOmpVpNEZ8yxNxaV3lk26x/kbVRyh+WdBSszpNOOQA5+8QX0tEs0S5p7kcsifB1JHz0TyXGQ6hC5uKzPR0GNWX+O X-Gm-Message-State: AOJu0Ywpx/MkmLjKXQuZPOOnSFmBrxBnG4FDbkCYDL7nfTEUM7GMFKpO WD7LuVDfEfZbPzaL/ZVa6OSkxG2+T0lujeMnc7Oa7hwZZusvHFQiCaga5QBYy3AE3ycYb/q0NfW ualwFVX5A+xt/z803/pwkyY8GBliLgOer8stf X-Received: by 2002:a05:6214:4602:b0:6ab:92b7:5903 with SMTP id 6a1803df08f44-6abbbcb0b35mr137433736d6.21.1716896213239; Tue, 28 May 2024 04:36:53 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <986294ee-8bb1-4bf4-9f23-2bc25dbad561@efficios.com> <944d79b5-177d-43ea-a130-25bd62fc787f@efficios.com> <7236a148-c513-4053-9778-0bce6657e358@efficios.com> In-Reply-To: From: Alexander Potapenko Date: Tue, 28 May 2024 13:36:11 +0200 Message-ID: Subject: Re: Use of zero-length arrays in bcachefs structures inner fields To: Kent Overstreet , Mathieu Desnoyers Cc: Brian Foster , Kees Cook , linux-kernel , linux-bcachefs@vger.kernel.org, Marco Elver , Dmitry Vyukov , kasan-dev@googlegroups.com, Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , llvm@lists.linux.dev Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, May 24, 2024 at 7:30=E2=80=AFPM Kent Overstreet wrote: > > On Fri, May 24, 2024 at 12:04:11PM -0400, Mathieu Desnoyers wrote: > > On 2024-05-24 11:35, Mathieu Desnoyers wrote: > > > [ Adding clang/llvm and KMSAN maintainers/reviewers in CC. ] > > > > > > On 2024-05-24 11:28, Kent Overstreet wrote: > > > > On Thu, May 23, 2024 at 01:53:42PM -0400, Mathieu Desnoyers wrote: > > > > > Hi Kent, > > > > > > > > > > Looking around in the bcachefs code for possible causes of this K= MSAN > > > > > bug report: > > > > > > > > > > https://lore.kernel.org/lkml/000000000000fd5e7006191f78dc@google.= com/ > > > > > > > > > > I notice the following pattern in the bcachefs structures: zero-l= ength > > > > > arrays members are inserted in structures (not always at the end)= , > > > > > seemingly to achieve a result similar to what could be done with = a > > > > > union: > > > > > > > > > > fs/bcachefs/bcachefs_format.h: > > > > > > > > > > struct bkey_packed { > > > > > __u64 _data[0]; > > > > > > > > > > /* Size of combined key and value, in u64s */ > > > > > __u8 u64s; > > > > > [...] > > > > > }; > > > > > > > > > > likewise: > > > > > > > > > > struct bkey_i { > > > > > __u64 _data[0]; > > > > > > > > > > struct bkey k; > > > > > struct bch_val v; > > > > > }; I took a glance at the LLVM IR for fs/bcachefs/bset.c, and it defines struct bkey_packed and bkey_i as: %struct.bkey_packed =3D type { [0 x i64], i8, i8, i8, [0 x i8], [37 x i= 8] } %struct.bkey_i =3D type { [0 x i64], %struct.bkey, %struct.bch_val } , which more or less looks as expected, so I don't think it could be causing problems with KMSAN right now. Moreover, there are cases in e.g. include/linux/skbuff.h where zero-length arrays are used for the same purpose, and KMSAN handles them just fine. Yet I want to point out that even GCC discourages the use of zero-length arrays in the middle of a struct: https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html, so Clang is not unique here. Regarding the original KMSAN bug, as noted in https://lore.kernel.org/all/0000000000009f9447061833d477@google.com/T/, we might be missing the event of copying data from the disk to bcachefs structs. I'd appreciate help from someone knowledgeable about how disk I/O is implemented in the kernel.