Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1870141pxm; Thu, 24 Feb 2022 10:57:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJxe3S/NkfcR8tqy9vOFAtAB5q18HKU9uJb4aCW6vccGdpl/mLNZPbf0KImkGnVyM2Gm4j7x X-Received: by 2002:a50:9f8b:0:b0:413:2dbd:8793 with SMTP id c11-20020a509f8b000000b004132dbd8793mr3698380edf.39.1645729038201; Thu, 24 Feb 2022 10:57:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645729038; cv=none; d=google.com; s=arc-20160816; b=sv2IVkDgTRi7jSeNlV9GXXNkEBEbQEXEAYS5bngCwVgZ9aAV9qFyrhKP/NNxD9mvhI 2A+RIzpJhzeU44b56hJFIpY9jRZ0xeyeP0M8NuqzVoSYy2JcRGAORWH+toEbKrk4mhml R0gOZ7P3xs57y/G9rd+k6kAXCzR0B+BoZRPmOJJsxiusWAEMr2LJw4vVZaCdkk6ITNCK wcvhJYKLHvervQARh3FRUnue+E6nWOPiRFyKoWAEjQGaj8NDvbY3zr/TQwkOxyRxCqLw SPzmhwDV9VvH3B5ZC2zDrnHshS4+INEPH/5XxlLlP9xe7Ph1MxKsDDA/2sDeibUGp2aR +U8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=pOBmVUTfu7dJw7T0At09LeWpqEdb7Gx94oz8UfcdCXs=; b=vYV4z7v2y/cbqgTqRae9ST7J52s8IxNU+fpCsF+oTS47v7t5bZDwiriB7yvq6hRD+S /Oaf+4G2k3DK0MAcPYql+xhWWhmWySyeoT68uR4c62yBBLxZ3MUlw/l3h5sAhqOywlf2 Sa8TcZXTRpSSvI4uVFVyaDRyNDGhhYXOvHllCBaqBFrn2WYfoW4i5VcaDdhm1avZwE9p IupFwehLppPD0rPbbC1XKpvDUSV7eZU01NPk68mUukHtb9jZvE0RdcPB2JfJ18tcXGcP SzpIEv6O/TxRZLqm5qDy3AKa7RogoTKCZmxUppLJR0pGS/U5p9651i080JVl2FxvmXua O+Yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=google header.b=AILmembZ; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id el8-20020a170907284800b006ae709825c7si151301ejc.41.2022.02.24.10.56.53; Thu, 24 Feb 2022 10:57:18 -0800 (PST) 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=pass header.i=@linuxfoundation.org header.s=google header.b=AILmembZ; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229708AbiBXRp6 (ORCPT + 99 others); Thu, 24 Feb 2022 12:45:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231367AbiBXRp4 (ORCPT ); Thu, 24 Feb 2022 12:45:56 -0500 Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0134279470 for ; Thu, 24 Feb 2022 09:45:25 -0800 (PST) Received: by mail-lf1-x12e.google.com with SMTP id u20so5216783lff.2 for ; Thu, 24 Feb 2022 09:45:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=pOBmVUTfu7dJw7T0At09LeWpqEdb7Gx94oz8UfcdCXs=; b=AILmembZ985YYL0f4wZ5s11RyKgR5u6qiQrV7RZNkT9G5n/SUdL4Vhrq9B5bX8IAIQ Y6nGnLQuOzkqFVDrilbDkVYSx1GL7mMY7i0oqQFQ167eDMte/SkF8KYTQPuW8ZBUYD0I S+wsm+vFqKevBhVyTZaq6iuYgrlCFfQmfxWN4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=pOBmVUTfu7dJw7T0At09LeWpqEdb7Gx94oz8UfcdCXs=; b=WCIaTSuZA7klWeF8di3ltUqskNNNKSVyzZuwg4lz195sJSLxyLO8wcKjk1dRSVGIN/ hA4HbICgwbvejgZciKrMV7+QJ3cTcUdyFb6lM+g7TRZZuTuzLLbREti87TMSk6PRKY/O XSiHFAl+6L9lOcP0nBULu9vyIboX2WJdTc0CjGxVCqbNdNdu5GJ5E+EWcBfyhw6yAxzG 7s/krpw9gNal19cPMWvlQhDMIUoXYhC6ATYaZOc1eCwIzHTBPrxFKZcxijyLbVINCyVN SHLSbm0I/FZsshKxfhHb9suh0tDp33W0X4KG+FmHdQ4oZkOH1hMOWbPaADuoFr7O8fwy 1xuQ== X-Gm-Message-State: AOAM532JqeulMfIApPzobi6f3QfkzAXtueO3du+UWOH/11Ae8epXXMoc +SDOAG1gIRqshB4/DZk80lGZOacxgeVQAhmTQLA= X-Received: by 2002:ac2:5cb5:0:b0:442:13cb:481f with SMTP id e21-20020ac25cb5000000b0044213cb481fmr2445403lfq.174.1645724723835; Thu, 24 Feb 2022 09:45:23 -0800 (PST) Received: from mail-lj1-f170.google.com (mail-lj1-f170.google.com. [209.85.208.170]) by smtp.gmail.com with ESMTPSA id bu40sm160169lfb.273.2022.02.24.09.45.22 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 24 Feb 2022 09:45:22 -0800 (PST) Received: by mail-lj1-f170.google.com with SMTP id bn33so3987334ljb.6 for ; Thu, 24 Feb 2022 09:45:22 -0800 (PST) X-Received: by 2002:a2e:80c6:0:b0:246:3334:9778 with SMTP id r6-20020a2e80c6000000b0024633349778mr2640059ljg.443.1645724721879; Thu, 24 Feb 2022 09:45:21 -0800 (PST) MIME-Version: 1.0 References: <20220221122638.7971-1-franciman12@gmail.com> <87o82wvhtk.fsf@kernel.org> <20220224075346.GL3943@kadam> <87fso8vb3w.fsf_-_@kernel.org> In-Reply-To: <87fso8vb3w.fsf_-_@kernel.org> From: Linus Torvalds Date: Thu, 24 Feb 2022 09:45:05 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: Use of void pointer arithmetic? To: Kalle Valo Cc: Dan Carpenter , Francesco Magliocca , Jeff Johnson , ath10k@lists.infradead.org, rmanohar@qti.qualcomm.com, linux-wireless , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, 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 lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Feb 24, 2022 at 1:59 AM Kalle Valo wrote: > > > What does -Wpointer-arith buy us? > > A good question. I have always just thought we should avoid void pointer > arithmetic due to the C standard, but now that you mention it void > pointers can indeed simplify the code. So I'm not so sure anymore. > > Any opinions? Is there a kernel wide recommendation for this? We consciously use arithmetic on 'void *' in some places, although it's not exactly _hugely_ common. It's much more common to turn a pointer into an 'unsigned long' and then doing basic operations on that. The advantage of 'void *' is that it does avoid the need to cast the pointer back. But at the same time it will never replace the 'cast to an actual integer type', because the 'void *' arithmetic only does simple addition and subtraction, and many pointer operations need more (ie alignment needs to do the bitops). So I think it's mostly a personal preference. I *do* think that doing arithmetic on 'void *' is generally superior to doing it the old-fashioned C way on 'char *' - unless, of course, 'char *' is simply the native type in question. So if 'char *' casts (and casting back) is the alternative, then by all means use 'void *' as a kind of generic and type-independent "byte pointer". That is how it is meant to be used in the gcc extension. And no, nobody should ever use -Wpointer-arith on the kernel in general. Our use of it is not _hugely_ common, but it's does exist, and it's not really frowned upon. Linus