Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp2730010rdb; Mon, 4 Dec 2023 06:10:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IG02RJvmb2moJqKaKAAPqd2n9CzbQ49Oc6cfTsM+znpxpRMRKbq61+YksckvuQ1bjcpOhPj X-Received: by 2002:a17:90b:4ac3:b0:286:83f8:48f3 with SMTP id mh3-20020a17090b4ac300b0028683f848f3mr2378648pjb.28.1701699005775; Mon, 04 Dec 2023 06:10:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701699005; cv=none; d=google.com; s=arc-20160816; b=Lx24n2tkS/9SbfXKCr4eZZ+hXWcwA2AYA7Mee7WtfoEKnjvZnLQ5sKI3LHQlMAPUhO T/gT9xzjbgz1aCaIo160+XMuBuatnVjdnkhmq2mBWBsFzHe+xQsf7LzEh3jqzJtSHQGj lYssl2Mu7nwYCCv2imxe+WYwX5+nfnHG7lE1WoVdRBY9sARNRw3w05Z/t/xRJq3pIlkB u6Rfuqgr2WiR+mFj4VtdAmNY5Bu8LdFENlhTBO3KJ77l3Dw2CJWzOOQR8fDtI6a+kD0V lOznAGfV4X5JqZJfJFh4TVXhjxLAlIerhAjFdBoEhCo3ZecS5fRASCoeQJ4zFViM26ob FB2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=C7YWLKYL+TZy0FDvwaUZBqPs1GQRI/NVi7Z/lE5VeVA=; fh=OCU++GF2nYZJyjuektsDfQYtNsLqOaxlmTR02Q1g1Ig=; b=HgT81rtj9FPX58/gUa4MTcWQi2tDjtoYr+832YvZqcJeVUus8QuI6E3zg/+QHp34A+ WCo6KHa7qVPoyymmYpa6uN+NHXtkYqvln40N5fQcqZgL8S2Pxf7bOMC2iBrriE8+UGGA 9/YAu4d43oaHhsoQ9J6mUfVxEUxGEwwC06A4ABqrtBn4KJWiuohivHPhDxokoqbBxxaQ RaxP9lyXAs9eGtNdNizetb2GoyWnmdbYeDpH+3pWxyOT47KkdkyRMwTQOv11qqfHlRKx JB8dpYF46KBjPsmbWLNcITByDWl3NHi1daiy0Tl6riltgeSDYsKhnkux/Osr2fPy24DZ oa7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=iPjoApDh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id z15-20020a17090a66cf00b00286bb86d9cesi1295667pjl.38.2023.12.04.06.10.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 06:10:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=iPjoApDh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id C443680774B1; Mon, 4 Dec 2023 06:10:02 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344957AbjLDOJa (ORCPT + 99 others); Mon, 4 Dec 2023 09:09:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234497AbjLDOJ3 (ORCPT ); Mon, 4 Dec 2023 09:09:29 -0500 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 701EEB9; Mon, 4 Dec 2023 06:09:35 -0800 (PST) Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-54c5d041c23so3924959a12.2; Mon, 04 Dec 2023 06:09:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701698974; x=1702303774; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=C7YWLKYL+TZy0FDvwaUZBqPs1GQRI/NVi7Z/lE5VeVA=; b=iPjoApDheAbwNKXWg+iBUUId++BWtFfaG3BS3rI2km/wTHvNrt3Q1adq5XnTgNH3cb uGVC18b0YXQr9fWajlr/oxTyoS8UbnqP56Kam2fOZKTM4oOURNm1/3OiDP4gYijYFxuF AYpwWW5+nyeFHZ1z4kDGM2NjP7j+tmuIY9d3xHZXcSFpXtDBJi+uFIJZK8tGOo3VpiMs oBCQCPgwbzgDxBL3Fybl3kcuen7B1fcApQ5wuy9yCB2skGuHuoQXBnbW/ruZ29hJRluA Nty6yLevq7aHEFQ5ZP5XqkinBvB3RmcOPpWzYhsuQ/vK690BgUFGkb2r5BFyfdGuOS6w 8t9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701698974; x=1702303774; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=C7YWLKYL+TZy0FDvwaUZBqPs1GQRI/NVi7Z/lE5VeVA=; b=atDx/gPN4tQ2JN2vaErjf8ILnKEcgNWXamHzMYUQoEOBjSjryaFOZJW4IsNBKOFbhB +mttE73lH38qHpeHjVM9PD0ldaVqDlHLNkwxzRG0H0DooFSBSuqfjJVU/C088ePNdHhD hpPv2zZjgrxEEcCqZjEVLYvAFdZLzbNu/FKJcSGXUUBVXUySA51m3sQjmkLXq9Wrq2FK bdQWCogUwprI+guRr5UF0vYXnNBFPU3r0g7RXIyvGdnBypc3Pf6ZnjaLzEz9iWat14v7 d3O3wL5jRTQgt9Luwl4HKtv8j+UyRwnm8QfbhLY+nlMFklT3g31bB7UYJtbtwuJT/Xmo d0tw== X-Gm-Message-State: AOJu0YyqeS9wjBR7T7f5HrA1NmlSjciMQ1c4UGXOXzUBwvTu3y5MmXB8 ICJN4V9n9zJTTGqmDsElbeg= X-Received: by 2002:a17:906:73c2:b0:a19:d40a:d1e8 with SMTP id n2-20020a17090673c200b00a19d40ad1e8mr1648024ejl.180.1701698973692; Mon, 04 Dec 2023 06:09:33 -0800 (PST) Received: from mail ([87.66.46.106]) by smtp.gmail.com with ESMTPSA id gq18-20020a170906e25200b009ad89697c86sm5416098ejb.144.2023.12.04.06.09.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 06:09:33 -0800 (PST) Date: Mon, 4 Dec 2023 15:09:32 +0100 From: Luc Van Oostenryck To: Lukas Wunner Cc: Dan Carpenter , oe-kbuild-all@lists.linux.dev, linux-kernel@vger.kernel.org, Bjorn Helgaas , kernel test robot , Mika Westerberg , linux-sparse@vger.kernel.org Subject: Re: drivers/pci/pcie/../pci.h:325:17: sparse: sparse: cast from restricted pci_channel_state_t Message-ID: References: <20231203165932.GA6480@wunner.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231203165932.GA6480@wunner.de> X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Mon, 04 Dec 2023 06:10:02 -0800 (PST) On Sun, Dec 03, 2023 at 05:59:32PM +0100, Lukas Wunner wrote: > Hi Dan, > > > > Please advise whether these sparse warnings are false positives which > > > can be ignored and if they aren't, how to resolve them. If you happen > > > to know the rationale for the cast, I'd be grateful if you could shed > > > some light on it. Thanks a lot! > > > > The question is more why is pci_channel_state_t declared as __bit_wise. > > __bit_wise data can only be used through accessor functions, like user > > pointers have to go through copy_from_user() and endian data has to go > > through le32_to_cpu() etc. > > __bitwise is not only to ensure endian correctness. It can be used to > define data types which are integer-based, but treated as a distinct > data type by sparse. A pci_channel_state_t value cannot be assigned > to an integer variable of a different type, for example. Correct. It was done on purpose to make a sort of 'strong' enum type, and thus warn if pci_channel_state_t values are mixed with some other types. > A few arches define arch_xchg() and arch_cmpxchg() as pure macros. > The sparse warning for pci_channel_state_t does not appear on those > arches. (These are: arc csky riscv x86) > > All other arches use a mix of macros and static inlines to define > arch_xchg() and arch_cmpxchg(). The static inlines use unsigned long > as argument and return types and the macros cast the argument type to > unsigned long. > > Why are the casts necessary? Because there are callers of xchg() and > cmpxchg() which pass pointers instead of integers as arguments. Hmmm, I'm missing the precise context but it make me wonder what's happening on 64-bit archs where enums are usually only 32-bit ... > Examples include llist_del_all(), __wake_q_add(), mark_oom_victim(), > fsnotify_attach_connector_to_object(). (Note that NULL is defined as > "(void *)0".) > > When using xchg() or cmpxchg() with __bitwise arguments (as is done > by commit 74ff8864cc84 in pci_dev_set_io_state()), the arches which > define arch_xchg() and arch_cmpxchg() with static inlines see sparse > warnings because the __bitwise arguments are cast to unsigned long. > Those arches are: alpha arm arm64 hexagon loongarch m68k mips openrisc > parisc powerpc s390 sh sparc xtensa OK, if the underlying macros do as: switch (size) { case 1: ... then things are ok there (but only if the size is given by a sizeof() of the correct type (not casted to long or something). > Indeed adding __force to the cast, as you suggest, should avoid the > issue. sparse cannot parse the inline assembler, so it does not > understand that arch_xchg() and arch_cmpxchg() internally perform a > comparison and/or assignment. By adding __force, we therefore do not > lose any validation coverage. Yes, indeed, using __force inside specific accessors or any low-level macros, like here these arch_xchg(), is OK. It's done in plenty of similar cases. > A better approach might be to teach sparse that arch_xchg() and > arch_cmpxchg() internally perform a comparison and/or assignment. > Then sparse could validate the argument and return value types. > > builtin.c in the sparse source code already contains functions > to handle __atomic_exchange() and __atomic_compare_exchange(), > so I think xchg() and cmpxchg() should be handled there as well. I don't agree. Sparse shouldn't know about the semantics of functions in the kernel. Sparse offers some tools (annotations like the __bitwise, noderef or address_space attributes for __user, __iomem, ... and type checking stricter than standard/GCC C). By using these annotations, the kernel can define a stricter semantic, that better suits its needs, Sparse should know nothing about this, it's not its job. Best regards, -- Luc