Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp6005546rwb; Mon, 5 Dec 2022 06:55:00 -0800 (PST) X-Google-Smtp-Source: AA0mqf5MS7ywhhBBvA9/ssMksHAmXePQJx18nj7YT1sttUvrFjT6VTgsr4DMQ/LBnp0vXBlL1u2Z X-Received: by 2002:aa7:c042:0:b0:462:2f5a:8618 with SMTP id k2-20020aa7c042000000b004622f5a8618mr74727539edo.42.1670252100411; Mon, 05 Dec 2022 06:55:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670252100; cv=none; d=google.com; s=arc-20160816; b=rPqi755L/zal+ZQmentlc2r2UJ1vZbrQRYwc/J+ZrI2Ul9o37SdpeLpdSWDe0oSjS0 6rB2Rc2V6k5TrXV4SFnZ18bLkBdpE8EX28Cv7RAqvGcquuMa5iUzl47CsghC2w4lCA4L EJDs1sptcXoP1jUjgLOUaQ5NiyK2iz+xS1NsmYax2BRDLmFs/afxrmv/xnV5H+Il54hL kEoLQHIH3USKsIUSzZwEC+bkHY/7HCM4b/tWe2Gjjl029Z6kVH/rZV9bhHbVJLZvsOy8 Up258kF4mfA1gjSQmW7wGhnSfg6O4esB40KpMT2rQI6FkmqqW25RyiJPv32wUAYQOqFS em4Q== 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; bh=Mc+cLLtHJGOP66cgSux+fMADmZs9geR0ByZCxx6UhW8=; b=C9DLZu9pux5afFulRntmlYutW1r9gAIANJrXCvNe2aiiu+m7Nv39Gebwci6+SkjkCo o5C32zU1DfSb/dAcphvCz7hd8eRk7k48g30j1MOMpyNF+VjuGDvo1SAnbTCYZsteO7o+ js33dU841Hjq4KPJ5HEOpUAetBwsoUFHNbGw3R3NuXqm8595y0jjPkkhAeLWU+a80lWK +jVmxMa0NBagV6M/+vD0cKrnjxkl4k6Q2ePhoXCwVlAhBhi6EsIdv4HpGTZzJlM8NW7d ZS4HODNHvkecvDwbEHvvsPwJYbalO4ueL6jPXXtHyGzY6rr0XouLyme5m0GJuLdISbBj u68A== ARC-Authentication-Results: i=1; mx.google.com; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dm5-20020a170907948500b0078d1dc1b64csi1292126ejc.170.2022.12.05.06.54.39; Mon, 05 Dec 2022 06:55:00 -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; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230246AbiLEOAH (ORCPT + 81 others); Mon, 5 Dec 2022 09:00:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229982AbiLEOAG (ORCPT ); Mon, 5 Dec 2022 09:00:06 -0500 Received: from mail-qk1-f180.google.com (mail-qk1-f180.google.com [209.85.222.180]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA318EE13 for ; Mon, 5 Dec 2022 06:00:04 -0800 (PST) Received: by mail-qk1-f180.google.com with SMTP id s10so3418964qkg.8 for ; Mon, 05 Dec 2022 06:00:04 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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=Mc+cLLtHJGOP66cgSux+fMADmZs9geR0ByZCxx6UhW8=; b=dcA+CKALs3v3yWgLoLv6Im5Q0FCiGYalGkOrfujc38CCg1+dizT+6ag9Ldg5FOAHaV rvOd7Z/0Pu5esu372X9Zs7Sldia20SoxnLsgGbIzUBvtwALVsas9IyLP9Y2qe1FIS1f/ Z0A9Cy3559ZsWiHJA89AYfX34xfSjwgN9Q9Y9zRlnPePNjJqNkBQ8+Gf9LuyivK9L5/8 FdWuMTLAeiBMsWpPvw21rUSNpgUWRt5EqU89N7wVhmZ6jZKSWmwNrk8YFLkthv0oufkS z4SkNsI/dOhZ3ZqQvGlEI/tqka/ROZV+1dL3/CtdkMlrGDaFIM8+eP6Fw+Kj5Ev6Om3k oAew== X-Gm-Message-State: ANoB5pnyzP2jBPoueTnU2ufLEyNHn4TklsomCeLv/RCeaunxGK4R10SL 6DJOpkeBg7SY/fT7NHANAsttR074Bc7pF5RaJsA= X-Received: by 2002:a05:620a:4611:b0:6fa:af7e:927c with SMTP id br17-20020a05620a461100b006faaf7e927cmr71735755qkb.443.1670248803895; Mon, 05 Dec 2022 06:00:03 -0800 (PST) MIME-Version: 1.0 References: <20221205121206.166576-1-gregkh@linuxfoundation.org> In-Reply-To: <20221205121206.166576-1-gregkh@linuxfoundation.org> From: "Rafael J. Wysocki" Date: Mon, 5 Dec 2022 14:59:49 +0100 Message-ID: Subject: Re: [PATCH v2 1/4] container_of: add container_of_const() that preserves const-ness of the pointer To: Greg Kroah-Hartman Cc: linux-kernel@vger.kernel.org, Jason Gunthorpe , Sakari Ailus , Jason Gunthorpe , Andy Shevchenko , Matthew Wilcox , "Rafael J. Wysocki" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no 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 Mon, Dec 5, 2022 at 1:12 PM Greg Kroah-Hartman wrote: > > container_of does not preserve the const-ness of a pointer that is > passed into it, which can cause C code that passes in a const pointer to > get a pointer back that is not const and then scribble all over the data > in it. To prevent this, container_of_const() will preserve the const > status of the pointer passed into it using the newly available _Generic() > method. > > Suggested-by: Jason Gunthorpe > Suggested-by: Sakari Ailus > Reviewed-by: Jason Gunthorpe > Reviewed-by: Andy Shevchenko > Cc: Matthew Wilcox > Cc: "Rafael J. Wysocki" > Signed-off-by: Greg Kroah-Hartman Acked-by: Rafael J. Wysocki > --- > v2: - removed one parameter, now matches container_of(), thanks to > suggestion from Sakari > - changed Jason's tag to suggested-by and reviewed-by > - added Andy's tag > > include/linux/container_of.h | 13 +++++++++++++ > 1 file changed, 13 insertions(+) > > diff --git a/include/linux/container_of.h b/include/linux/container_of.h > index 2008e9f4058c..1d898f9158b4 100644 > --- a/include/linux/container_of.h > +++ b/include/linux/container_of.h > @@ -22,4 +22,17 @@ > "pointer type mismatch in container_of()"); \ > ((type *)(__mptr - offsetof(type, member))); }) > > +/** > + * container_of_const - cast a member of a structure out to the containing > + * structure and preserve the const-ness of the pointer > + * @ptr: the pointer to the member > + * @type: the type of the container struct this is embedded in. > + * @member: the name of the member within the struct. > + */ > +#define container_of_const(ptr, type, member) \ > + _Generic(ptr, \ > + const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\ > + default: ((type *)container_of(ptr, type, member)) \ > + ) > + > #endif /* _LINUX_CONTAINER_OF_H */ > -- > 2.38.1 >