Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp5846762rwb; Mon, 5 Dec 2022 05:04:00 -0800 (PST) X-Google-Smtp-Source: AA0mqf40T1M8lV8i44zFwCCkYEI7tdRXz7gh8Vy4LVzgQRzeiHjS3poDsM9Fy9SQvvVdlncOmhNq X-Received: by 2002:a17:90a:d157:b0:211:710e:7d03 with SMTP id t23-20020a17090ad15700b00211710e7d03mr81843229pjw.56.1670245440353; Mon, 05 Dec 2022 05:04:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670245440; cv=none; d=google.com; s=arc-20160816; b=W+FCAxQkOTEQ/lZq2RFbKYqNHVdvNCqh+sGMmWEKiIgoWXpGvLMzJEOCriI6BPfpnm olx9IPAVytUbSvKUjkMr0fI/03AesMus6EonVDHoATsR9ImPQAvgMQjseY60aDDOaf1E HHx/chOj13rM/z7qR5T8oKiuvmqK9nC2a8e872VVhmGPkUp9nY1yFVh0wyoMTFkpMGrl sKPuWUYQ9iBooqsHhR2Idh4l1rZ67VVXow0aTDfoKjdH2hV5FMS2Yj+4FiefYIviMPmU yythQJ/WhqJxA/6Gb9GPgjq4xK9PQkmo/v9mjRTZCa32lI0ucpDdLhLFymR5R8ZPoR1B KA7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=jNj52cvhsLb/czB5sBFOr4EU9MV53AtoUc+t/2UeDik=; b=KOqPm2d98SusWnRrmZz5kEWquhpF5Cvu9s7opEnyOtIByJIBPz3/QMloZWc2lrj3sy 2k7yn+dhvUGjE5FfSQnUmupf9P+6XNfewSwj39f0Mv992+vNXDAyP+G9MpHXo11RUiry Ddz8g7qVVtHDVTQse/zysXcmSW1f4c6Pj4u4692AZBXm3/CchgHFGZw4CmZvHLzQkkgK T0ocIgBsCQTtU1lEPPmIjpRonKqKEFnFsb0YmXExKVX1hS3ksM48xbTe/cSkKnQjE1d3 QT/1485b/E+/1FdO3IqOWmPlThTc3/1qnJMsU9EigY0Vz7bQ7ZcpwWC3g9R9HfXBOpgR 6jzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QsCo3793; 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 q7-20020a056a00084700b005739d652a89si9640328pfk.223.2022.12.05.05.03.46; Mon, 05 Dec 2022 05:04: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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QsCo3793; 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 S231649AbiLEMMZ (ORCPT + 82 others); Mon, 5 Dec 2022 07:12:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231639AbiLEMMU (ORCPT ); Mon, 5 Dec 2022 07:12:20 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC1B41A20E for ; Mon, 5 Dec 2022 04:12:19 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 9F019B80F88 for ; Mon, 5 Dec 2022 12:12:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ED0ACC433D7; Mon, 5 Dec 2022 12:12:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1670242337; bh=/ZRjwpE8MVqj9FdbA8dKxciOYI4zz5mw0HeE6Kg7bRk=; h=From:To:Cc:Subject:Date:From; b=QsCo3793g7Ra5twgUwIr6bTot9wdAbhKra1sKjxB8gkTtHeGO/8xp/FT2Lis6+Clc 7xuHvWZt6CPou5KxdSC/d19dEnOp9hXXFerGlHmQtVUOYl8stFSwtIkOWNSvvhilQf kKXVWcvqqTZUDev5m+5dAGALqL8IKcsBr4LUzUGU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , Jason Gunthorpe , Sakari Ailus , Jason Gunthorpe , Andy Shevchenko , Matthew Wilcox , "Rafael J. Wysocki" Subject: [PATCH v2 1/4] container_of: add container_of_const() that preserves const-ness of the pointer Date: Mon, 5 Dec 2022 13:12:03 +0100 Message-Id: <20221205121206.166576-1-gregkh@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1916; i=gregkh@linuxfoundation.org; h=from:subject; bh=/ZRjwpE8MVqj9FdbA8dKxciOYI4zz5mw0HeE6Kg7bRk=; b=owGbwMvMwCRo6H6F97bub03G02pJDMm9D/irvr074783v8dpCcuj9bfyIjdHSTyZYMB2RTfI5nJk R/mNjlgWBkEmBlkxRZYv23iO7q84pOhlaHsaZg4rE8gQBi5OAbjIEYaZjM4dtUz2HukXJuVod7f9Fa p6u8iLYcFMzklTRBisDvw5xDRD9v8NMfGkXysB X-Developer-Key: i=gregkh@linuxfoundation.org; a=openpgp; fpr=F4B60CC5BF78C2214A313DCB3147D40DDB2DFB29 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham 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 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 --- 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