Message ID | ZVfJ6Fxidvw_gz7r@rric.localdomain |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9910:0:b0:403:3b70:6f57 with SMTP id i16csp788786vqn; Fri, 17 Nov 2023 12:16:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IEXlYjrZYyGmE0uqOPRMK5aah7lwN3fLTPAvLoi0MvAmCgQy3JoZwE07XZA53NbhTmnA/D7 X-Received: by 2002:a17:90b:1646:b0:281:d55:e3c7 with SMTP id il6-20020a17090b164600b002810d55e3c7mr677142pjb.26.1700252210142; Fri, 17 Nov 2023 12:16:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700252210; cv=pass; d=google.com; s=arc-20160816; b=EDSxA7rLpsAqu7P1WF3dLZCqsdOSH/7RDF2w00ZZJWDOZbuD03BaYtG9wNzyI5zeLq /8w0Ffyo+osUlhaHa1OSOwQ7L1zuzc3aSpawynwuEXW0Le89e0o1wceztXW07ygAtnoi 2YdJUCSQ6vi7WSlc/45UeVeMQ2rpq63mgPlWrWqL4m9VIkMkNWVh7/7lKocug7P57suK +2CtmitcdPKGpsgGtzqSqj4Y3Xws3XQLsXr38YHWf3XWGX47zgICmnQQpKuOKiquDqAm MvfOybbIMwX2Xp4Z1BlGPssVvVBO4L9ORnKvYCN24n/3DZUKIWIZBLIo1vMBzAW6xa50 eo0Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:in-reply-to:content-disposition :references:message-id:subject:cc:to:from:date:dkim-signature; bh=/mhMu3XVa9U34TEQSnw8CdH2MCgx1O5RDKfbMFTwQSU=; fh=6g8HGADHvP3YboDOQicY2YFGfnAFzZJgpCKdcyuSMIY=; b=GRrh+cW70VMcJce4YhiUCyOhl/xPxTA4PAdGmQooJX7O6iuPa/+5Smjrub/6zH+wQS pT5CqCOEz9L0Ti+efi9egOO81ezETtxbNVm3KgEcmanh/FoFr100orFBfZhOJRO1sPSb zsJmXVTj4asb4j6rsxCDb84QukK059foC4wc/9ckz9PZTpAj0++ejekfTt3Z1ysnCk9g PbTvM9DevxVHgcOBRyLlKD0deotHcAXcK1nz83H419FeKOF3JOAFyMYw2TinKndZ/hN6 iYtqc0XrwCI8+q/ufLaFBYsfxsZ3yc45VBL3J+7K3ZxkbGLuzX0jyAUrD3WJk9PX1SRw myqw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=frFgT9tP; arc=pass (i=1 spf=pass spfdomain=amd.com dkim=pass dkdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id mq4-20020a17090b380400b00276e95d7657si5371422pjb.33.2023.11.17.12.16.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Nov 2023 12:16:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=frFgT9tP; arc=pass (i=1 spf=pass spfdomain=amd.com dkim=pass dkdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id A4E79809716D; Fri, 17 Nov 2023 12:16:40 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235675AbjKQUQY (ORCPT <rfc822;jaysivo@gmail.com> + 29 others); Fri, 17 Nov 2023 15:16:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231921AbjKQUQG (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 17 Nov 2023 15:16:06 -0500 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2076.outbound.protection.outlook.com [40.107.92.76]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26CCF19B2; Fri, 17 Nov 2023 12:15:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DCbgZKzi3Mb9Ca5HWws7uqi6LWnBrAHq5mIA2cuRtRV9aU8I9YfkArJJcVZxNZi1Pc4Oo9tMK5/GZ+8C1kZ3cdfsYsvq3GAGF2MVRd5YvtM/+KmMATkZVJveTs+Zkez0tVM/MB5QTniU89kiSG/NtSyCaRxiEWUDrYnMrzeG1W80g69wI3lpuTRdvOuzawZ6Bxj0vXlh9ChirUZretTi3y8hG3W38iRN9gWO3ThkSwtQAUZRoxw1H8kbnvCH1I9D6EH28rs76uHbUF/bQk7NGhzdgC46tZdAdmJT+BY0UZEY5SaegqwkRdl7Cthc5HU5BeWGn7Y+VvJsNQlzzWn+4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=/mhMu3XVa9U34TEQSnw8CdH2MCgx1O5RDKfbMFTwQSU=; b=kvgmF40c/i0ORw57cRKHxFGaJRIil8KKNhivMuQQ7M60S1P/WeyFz8aZFDzn+jCpBuAsXZZY/lb13OzcNJgTWtzKX6CJNF3siJ9DlPuVXrLiCjxeeX4vs4iBtipNGzcUo8IueYYlFq3NUpiexaYt/n70MzRn+10UXgJYrjlyZELxtd8kQgyMGw0rKiNsKfuvEQIhBB0T8K325M5kuZjGzx23itr9IhKfCH4/GQTkMAgVFVZbFgznrTH6w8gIAYE4JRtmjpLFWPA5bluaisFsP8AUZtznc/UBVLYgj/JgeodjPpkb8el6jSnGf0fWAYHD801NhZ/UiGXFqNoaDZaSrw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=/mhMu3XVa9U34TEQSnw8CdH2MCgx1O5RDKfbMFTwQSU=; b=frFgT9tPWkXehoAZw3lqht/sma+JutpBLYUy6bP6sW0qLH/ZO20p8VA8Rlst7PGwRIrI2NF14geh0h0NrgU8M2WrcfkFAuhrWexObpXBjU3/bWRW7e53636EBhUE+pQ+5nMLe/EXjAre3yyJXtRXto0cH5ON8VZ6T3IS1dNhrAo= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=amd.com; Received: from DM6PR12MB3657.namprd12.prod.outlook.com (2603:10b6:5:149::18) by CH2PR12MB4312.namprd12.prod.outlook.com (2603:10b6:610:af::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7002.23; Fri, 17 Nov 2023 20:15:42 +0000 Received: from DM6PR12MB3657.namprd12.prod.outlook.com ([fe80::5786:22a:27df:9a70]) by DM6PR12MB3657.namprd12.prod.outlook.com ([fe80::5786:22a:27df:9a70%7]) with mapi id 15.20.7002.022; Fri, 17 Nov 2023 20:15:42 +0000 Date: Fri, 17 Nov 2023 21:15:36 +0100 From: Robert Richter <rrichter@amd.com> To: Dan Williams <dan.j.williams@intel.com> Cc: Ira Weiny <ira.weiny@intel.com>, Davidlohr Bueso <dave@stgolabs.net>, Jonathan Cameron <jonathan.cameron@huawei.com>, Dave Jiang <dave.jiang@intel.com>, Alison Schofield <alison.schofield@intel.com>, Vishal Verma <vishal.l.verma@intel.com>, linux-cxl@vger.kernel.org, linux-kernel@vger.kernel.org, Fan Ni <nifan.cxl@gmail.com>, Lukas Wunner <lukas@wunner.de> Subject: [PATCH] cxl/pci: Get rid of pointer arithmetic reading CDAT table Message-ID: <ZVfJ6Fxidvw_gz7r@rric.localdomain> References: <20231116-fix-cdat-devm-free-v1-1-b148b40707d7@intel.com> <ZVfIaNhiSc-yQZo5@rric.localdomain> Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <ZVfIaNhiSc-yQZo5@rric.localdomain> X-ClientProxiedBy: FR2P281CA0044.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:92::18) To DM6PR12MB3657.namprd12.prod.outlook.com (2603:10b6:5:149::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3657:EE_|CH2PR12MB4312:EE_ X-MS-Office365-Filtering-Correlation-Id: 929f79f5-2e92-478c-1dac-08dbe7a9f975 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: SpgSZGPJjiWF6U86nmbiKcWtyfp/qe4vV/Xl2oLW6RVHZR5YPqLEsrmjcInIwlch1BUtOsZi1PlgBB/Ec//k24r9eRC9gtqc4DYYWojH2PSgR3z0+VhCUwhCWulWsAo3T8thRFk/DfjsjFBfYk49JEGCtNmamVMwJCwfZsK3TcRp3oNr4qvjdMbL2jgOzF2ulYQAFuIK6e24HOPN2ixk0Jh5aufezgS42YGFU5JWUvVOcmmb/T6R4SpT20sa5I2DNMPUYbvht98GEy8/kfcGA1IjJeWfjXocjQYyWGXidUquX3g5nFkGlILyNs/QlmJJVVKFWiXSW+DTqpQH3wgVfEeHK1ZtpWP7Q04vQoLDvWedFTOJjWsSZa7o1jAxVw75Q9wz2RfKkIuaQXZoG1LLbYLq4uKgsc6B/ua+8A/VdMYhoL2rpqtL9jbgiNj08x2MiXx/k2WR1HldOoF3oCrJTdqa61oEK4uW1Mlkhr30i4jq8QOIs86G8xtNk4s7So7AEcalq280Eg6K0VNSk/3u8No7XtiMEnr6p5lJlQngMIVg3sFbxZCNi8DCqOKeb5WlvjOE6HO7SvNkAO7exN0eVowpbEB0rTm2kcjLUVPpGjk= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3657.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(346002)(136003)(396003)(39860400002)(366004)(376002)(230922051799003)(64100799003)(451199024)(186009)(1800799009)(54906003)(66946007)(66556008)(66476007)(6666004)(316002)(6916009)(83380400001)(478600001)(6486002)(7416002)(5660300002)(6506007)(53546011)(38100700002)(2906002)(41300700001)(26005)(9686003)(4326008)(6512007)(8936002)(8676002)(26583001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: gEGK2qZZKSIiKLkob5Z4ttEaPvJcrPE+E38VZULwh0ioT0cDS0KHJ5YV80sk66H8bZmIg8AaHn2hIdRT2oewvww56fNnyGx9UZNZ6a4aBiV6BeFfJ2QGi8cr3c8QEaaXcbU2VIX7GGb6yKswX/9R2tdBWng6vWlMAZUlTSD8B1KlxWnhHuvAeRO7nFbChT1t0NcdXAc1cuD/VQhNPT582lFqd7TFlIRTqiHLEjuJrOjiSjbmxGU2OAecsohXVedrf9WsCBZ1EDrmjH8lScO5LwGxZFzBnX0x6MX3Ka9UsOEMUVkFhqbfA42a5aRLqLNibFpujuzlCqYnM+5utgh7cTrr70di5YgOGzx8sf3GT3PoMSZ38QcTuB36z1s3sWAOPBU0XmmdgBrlJ4R9sZ29UEQXAbzFKIEZLU/WKGubilhovD87QAXwB5EGBx6PwksTY93E8QTGfPZP+jmEpog/sp8rhu9xd8bWHN9Ki+XSTR4ALGi0xkFDp/9851+sKObNcTOVKzl2CeJsButSVxn7z0RkdMatL0QTFjIz2ANjA9Q+pbYg7P00I9KKPR6sInB9BAqyWhJOokDNYdr6bynIE9+lmgisV8CxvPFNBnxk+sWeXulf/OEJO2itm14XADVVzR/0oouXwD6IVKFENIFiqytUp7FFtrf8r9XBDIyuaOwfZHzGkcbjXpKv9pUjW/1WlgcD6GucRe6lK1bEG8382tZumP3A9Aj8/cxtCrGXCylhCxUVNvdkWMqX4fR24vIRHk770xe0fPvGq9Op13eGMh4A+k63dBliDXah/Gq3i3r1uLXdqKXfLWr1933VW9ngpZDrb9YFTtcmmcwzfrTxejjiM+5xH6nPRjEvKxcGgHaHJzheWw4bNt9kr7NdOwQVW6KVNpIDUNGc5JSHQrB1RCmofLCIaJ/i6cRzcUVJhDJpK7QQh5A3cPFCt+IPmsjRQ9qTq4e+1MAws9TWqiVhcdnZi8s7wkTj4z9O2Tq5U8uhpJ2fFM4FATqYq6rMpz4v/9VN+htnW6TwUHUWkUCCcm0bx2WeTN6rRoTBokA41n3gWAAOUUU14a3fsqAch7PyTTDh7kfoKjHlOcWtVtDuW9ESClRZlMv9Sr8rNNKbNnze2qIWdSyqvdpWZqW9teNv0vfDq3VU9cxBlllNEo7qAmkw/WSSjvzSRBqmVYKExhFHaJdWH22L/gR+YWpI00afTvNeGk4s9BVtBGbVAgIPQjOfv2cnWdyoiEjvhBesuZmuyiuufXJ+W0d6PemJmPpZdwEj8M7296NiPrDI8fTTyNXSZaUqdf9lOf1D2iSR837iH7txLGNLShr5bBVDeDI1rEMqMbjhUOVsVfaA1a+ONc9Rb2iAt2gZd/nYnlbgxnS4nEX6Xgi+Wub3jMWSyX6+aLPWK595cYrV97xtnsbhA82z2LtEve+RQXSyyh590QdNtdt87NswTmVhAJxwwtRD0s0/KoOJsOhPS8Rx/oCiioDtkfqItrzwkvOa64u3xdg9bMUmLktS3oQdLaxBeuLbmL0rsuBttRj6+3w4+m/Cr6Ifx1UW31LC2u4Z08rrb10XBoo1TwSrlOEwxzmTHY9o X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 929f79f5-2e92-478c-1dac-08dbe7a9f975 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3657.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Nov 2023 20:15:42.5822 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Cb0S2ibHdmehFoYEiqQNTe9nZMzGW3tLpt9dqvvw7LUKFC58y5n5du8FA82I7N6jvbMqpLT+VtrQBR4eVVCIBw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4312 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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 lipwig.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Fri, 17 Nov 2023 12:16:40 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782843661234988136 X-GMAIL-MSGID: 1782843661234988136 |
Series |
cxl/pci: Get rid of pointer arithmetic reading CDAT table
|
|
Commit Message
Robert Richter
Nov. 17, 2023, 8:15 p.m. UTC
On 17.11.23 21:09:18, Robert Richter wrote: > I will send an on-top patch for 6.8 that reworks that code area to > remove the pointer arithmetic. Here it is: From 13787f72c20b8c54754ae86015d982307eae0397 Mon Sep 17 00:00:00 2001 From: Robert Richter <rrichter@amd.com> Subject: [PATCH] cxl/pci: Get rid of pointer arithmetic reading CDAT table Reading the CDAT table using DOE requires a Table Access Response Header in addition to the CDAT entry. In current implementation this has caused offsets with sizeof(__le32) to the actual buffers. This led to hardly readable code and even bugs (see fix of devm_kfree() in read_cdat_data()). Rework code to avoid calculations with sizeof(__le32). Introduce struct cdat_doe for this which contains the Table Access Response Header and a variable payload size for various data structures afterwards to access the CDAT table and its CDAT Data Structures without recalculating buffer offsets. Cc: Lukas Wunner <lukas@wunner.de> Cc: Dave Jiang <dave.jiang@intel.com> Cc: Fan Ni <nifan.cxl@gmail.com> Signed-off-by: Robert Richter <rrichter@amd.com> --- drivers/cxl/core/pci.c | 80 ++++++++++++++++++++---------------------- drivers/cxl/cxlpci.h | 19 ++++++++++ 2 files changed, 57 insertions(+), 42 deletions(-)
Comments
On 11/17/23 13:15, Robert Richter wrote: > On 17.11.23 21:09:18, Robert Richter wrote: >> I will send an on-top patch for 6.8 that reworks that code area to >> remove the pointer arithmetic. > > Here it is: > > From 13787f72c20b8c54754ae86015d982307eae0397 Mon Sep 17 00:00:00 2001 > From: Robert Richter <rrichter@amd.com> > Subject: [PATCH] cxl/pci: Get rid of pointer arithmetic reading CDAT table > > Reading the CDAT table using DOE requires a Table Access Response > Header in addition to the CDAT entry. In current implementation this > has caused offsets with sizeof(__le32) to the actual buffers. This led > to hardly readable code and even bugs (see fix of devm_kfree() in > read_cdat_data()). > > Rework code to avoid calculations with sizeof(__le32). Introduce > struct cdat_doe for this which contains the Table Access Response > Header and a variable payload size for various data structures > afterwards to access the CDAT table and its CDAT Data Structures > without recalculating buffer offsets. > > Cc: Lukas Wunner <lukas@wunner.de> > Cc: Dave Jiang <dave.jiang@intel.com> > Cc: Fan Ni <nifan.cxl@gmail.com> > Signed-off-by: Robert Richter <rrichter@amd.com> Reviewed-by: Dave Jiang <dave.jiang@intel.com> > --- > drivers/cxl/core/pci.c | 80 ++++++++++++++++++++---------------------- > drivers/cxl/cxlpci.h | 19 ++++++++++ > 2 files changed, 57 insertions(+), 42 deletions(-) > > diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c > index 5aaa0b36c42a..f900740c6dea 100644 > --- a/drivers/cxl/core/pci.c > +++ b/drivers/cxl/core/pci.c > @@ -517,14 +517,14 @@ EXPORT_SYMBOL_NS_GPL(cxl_hdm_decode_init, CXL); > FIELD_PREP(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE, (entry_handle))) > > static int cxl_cdat_get_length(struct device *dev, > - struct pci_doe_mb *cdat_doe, > + struct pci_doe_mb *doe_mb, > size_t *length) > { > __le32 request = CDAT_DOE_REQ(0); > __le32 response[2]; > int rc; > > - rc = pci_doe(cdat_doe, PCI_DVSEC_VENDOR_ID_CXL, > + rc = pci_doe(doe_mb, PCI_DVSEC_VENDOR_ID_CXL, > CXL_DOE_PROTOCOL_TABLE_ACCESS, > &request, sizeof(request), > &response, sizeof(response)); > @@ -542,56 +542,54 @@ static int cxl_cdat_get_length(struct device *dev, > } > > static int cxl_cdat_read_table(struct device *dev, > - struct pci_doe_mb *cdat_doe, > - void *cdat_table, size_t *cdat_length) > + struct pci_doe_mb *doe_mb, > + struct cdat_doe *doe, size_t *length) > { > - size_t length = *cdat_length + sizeof(__le32); > - __le32 *data = cdat_table; > + size_t received, remaining = *length; > int entry_handle = 0; > __le32 saved_dw = 0; > > do { > __le32 request = CDAT_DOE_REQ(entry_handle); > - struct cdat_entry_header *entry; > - size_t entry_dw; > int rc; > > - rc = pci_doe(cdat_doe, PCI_DVSEC_VENDOR_ID_CXL, > + rc = pci_doe(doe_mb, PCI_DVSEC_VENDOR_ID_CXL, > CXL_DOE_PROTOCOL_TABLE_ACCESS, > &request, sizeof(request), > - data, length); > + doe, sizeof(*doe) + remaining); > if (rc < 0) { > dev_err(dev, "DOE failed: %d", rc); > return rc; > } > > - /* 1 DW Table Access Response Header + CDAT entry */ > - entry = (struct cdat_entry_header *)(data + 1); > + if (rc < sizeof(*doe)) > + return -EIO; > + > + received = rc - sizeof(*doe); > + > if ((entry_handle == 0 && > - rc != sizeof(__le32) + sizeof(struct cdat_header)) || > + received != sizeof(doe->header[0])) || > (entry_handle > 0 && > - (rc < sizeof(__le32) + sizeof(*entry) || > - rc != sizeof(__le32) + le16_to_cpu(entry->length)))) > + (received < sizeof(doe->entry[0]) || > + received != le16_to_cpu(doe->entry->length)))) > return -EIO; > > /* Get the CXL table access header entry handle */ > entry_handle = FIELD_GET(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE, > - le32_to_cpu(data[0])); > - entry_dw = rc / sizeof(__le32); > - /* Skip Header */ > - entry_dw -= 1; > + le32_to_cpu(doe->doe_header)); > + > /* > * Table Access Response Header overwrote the last DW of > * previous entry, so restore that DW > */ > - *data = saved_dw; > - length -= entry_dw * sizeof(__le32); > - data += entry_dw; > - saved_dw = *data; > + doe->doe_header = saved_dw; > + remaining -= received; > + doe = (void *)doe + received; > + saved_dw = doe->doe_header; > } while (entry_handle != CXL_DOE_TABLE_ACCESS_LAST_ENTRY); > > /* Length in CDAT header may exceed concatenation of CDAT entries */ > - *cdat_length -= length - sizeof(__le32); > + *length -= remaining; > > return 0; > } > @@ -616,11 +614,11 @@ void read_cdat_data(struct cxl_port *port) > { > struct device *uport = port->uport_dev; > struct device *dev = &port->dev; > - struct pci_doe_mb *cdat_doe; > + struct pci_doe_mb *doe_mb; > struct pci_dev *pdev = NULL; > struct cxl_memdev *cxlmd; > - size_t cdat_length; > - void *cdat_table, *cdat_buf; > + struct cdat_doe *doe; > + size_t length; > int rc; > > if (is_cxl_memdev(uport)) { > @@ -637,40 +635,38 @@ void read_cdat_data(struct cxl_port *port) > if (!pdev) > return; > > - cdat_doe = pci_find_doe_mailbox(pdev, PCI_DVSEC_VENDOR_ID_CXL, > - CXL_DOE_PROTOCOL_TABLE_ACCESS); > - if (!cdat_doe) { > + doe_mb = pci_find_doe_mailbox(pdev, PCI_DVSEC_VENDOR_ID_CXL, > + CXL_DOE_PROTOCOL_TABLE_ACCESS); > + if (!doe_mb) { > dev_dbg(dev, "No CDAT mailbox\n"); > return; > } > > port->cdat_available = true; > > - if (cxl_cdat_get_length(dev, cdat_doe, &cdat_length)) { > + if (cxl_cdat_get_length(dev, doe_mb, &length)) { > dev_dbg(dev, "No CDAT length\n"); > return; > } > > - cdat_buf = devm_kzalloc(dev, cdat_length + sizeof(__le32), > - GFP_KERNEL); > - if (!cdat_buf) > - return; > + doe = devm_kzalloc(dev, sizeof(*doe) + length, GFP_KERNEL); > + if (!doe) > + goto err; > > - rc = cxl_cdat_read_table(dev, cdat_doe, cdat_buf, &cdat_length); > + rc = cxl_cdat_read_table(dev, doe_mb, doe, &length); > if (rc) > goto err; > > - cdat_table = cdat_buf + sizeof(__le32); > - if (cdat_checksum(cdat_table, cdat_length)) > + if (cdat_checksum(doe->table, length)) > goto err; > > - port->cdat.table = cdat_table; > - port->cdat.length = cdat_length; > - return; > + port->cdat.table = doe->table; > + port->cdat.length = length; > > + return; > err: > /* Don't leave table data allocated on error */ > - devm_kfree(dev, cdat_buf); > + devm_kfree(dev, doe); > dev_err(dev, "Failed to read/validate CDAT.\n"); > } > EXPORT_SYMBOL_NS_GPL(read_cdat_data, CXL); > diff --git a/drivers/cxl/cxlpci.h b/drivers/cxl/cxlpci.h > index 0fa4799ea316..d12ed9d8dec1 100644 > --- a/drivers/cxl/cxlpci.h > +++ b/drivers/cxl/cxlpci.h > @@ -85,6 +85,25 @@ struct cdat_entry_header { > __le16 length; > } __packed; > > +/* > + * Response contains the CDAT only response header of the DOE. The > + * response payload is a CDAT structure (either CDAT header or entry), > + * it may also mark the beginning of the CDAT table. > + * > + * Spec refs: > + * > + * CXL 3.1 Table 8-14: Read Entry Response > + * CDAT Specification 1.03: 2 CDAT Data Structures > + */ > +struct cdat_doe { > + __le32 doe_header; > + union { > + u8 table[0]; > + struct cdat_header header[0]; > + struct cdat_entry_header entry[0]; > + }; > +} __packed; > + > int devm_cxl_port_enumerate_dports(struct cxl_port *port); > struct cxl_dev_state; > int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
Robert Richter wrote: > On 17.11.23 21:09:18, Robert Richter wrote: > > I will send an on-top patch for 6.8 that reworks that code area to > > remove the pointer arithmetic. > > Here it is: > > From 13787f72c20b8c54754ae86015d982307eae0397 Mon Sep 17 00:00:00 2001 > From: Robert Richter <rrichter@amd.com> > Subject: [PATCH] cxl/pci: Get rid of pointer arithmetic reading CDAT table > > Reading the CDAT table using DOE requires a Table Access Response > Header in addition to the CDAT entry. In current implementation this > has caused offsets with sizeof(__le32) to the actual buffers. This led > to hardly readable code and even bugs (see fix of devm_kfree() in > read_cdat_data()). > > Rework code to avoid calculations with sizeof(__le32). Introduce > struct cdat_doe for this which contains the Table Access Response > Header and a variable payload size for various data structures > afterwards to access the CDAT table and its CDAT Data Structures > without recalculating buffer offsets. Thanks for this. > > Cc: Lukas Wunner <lukas@wunner.de> > Cc: Dave Jiang <dave.jiang@intel.com> > Cc: Fan Ni <nifan.cxl@gmail.com> > Signed-off-by: Robert Richter <rrichter@amd.com> > --- > drivers/cxl/core/pci.c | 80 ++++++++++++++++++++---------------------- > drivers/cxl/cxlpci.h | 19 ++++++++++ > 2 files changed, 57 insertions(+), 42 deletions(-) > > diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c > index 5aaa0b36c42a..f900740c6dea 100644 > --- a/drivers/cxl/core/pci.c > +++ b/drivers/cxl/core/pci.c > @@ -517,14 +517,14 @@ EXPORT_SYMBOL_NS_GPL(cxl_hdm_decode_init, CXL); > FIELD_PREP(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE, (entry_handle))) > > static int cxl_cdat_get_length(struct device *dev, > - struct pci_doe_mb *cdat_doe, > + struct pci_doe_mb *doe_mb, NIT: Why change the variable name here? > size_t *length) > { > __le32 request = CDAT_DOE_REQ(0); > __le32 response[2]; > int rc; > > - rc = pci_doe(cdat_doe, PCI_DVSEC_VENDOR_ID_CXL, > + rc = pci_doe(doe_mb, PCI_DVSEC_VENDOR_ID_CXL, > CXL_DOE_PROTOCOL_TABLE_ACCESS, > &request, sizeof(request), > &response, sizeof(response)); > @@ -542,56 +542,54 @@ static int cxl_cdat_get_length(struct device *dev, > } > > static int cxl_cdat_read_table(struct device *dev, > - struct pci_doe_mb *cdat_doe, > - void *cdat_table, size_t *cdat_length) > + struct pci_doe_mb *doe_mb, > + struct cdat_doe *doe, size_t *length) > { > - size_t length = *cdat_length + sizeof(__le32); > - __le32 *data = cdat_table; > + size_t received, remaining = *length; > int entry_handle = 0; > __le32 saved_dw = 0; > > do { > __le32 request = CDAT_DOE_REQ(entry_handle); > - struct cdat_entry_header *entry; > - size_t entry_dw; > int rc; > > - rc = pci_doe(cdat_doe, PCI_DVSEC_VENDOR_ID_CXL, > + rc = pci_doe(doe_mb, PCI_DVSEC_VENDOR_ID_CXL, > CXL_DOE_PROTOCOL_TABLE_ACCESS, > &request, sizeof(request), > - data, length); > + doe, sizeof(*doe) + remaining); > if (rc < 0) { > dev_err(dev, "DOE failed: %d", rc); > return rc; > } > > - /* 1 DW Table Access Response Header + CDAT entry */ > - entry = (struct cdat_entry_header *)(data + 1); > + if (rc < sizeof(*doe)) > + return -EIO; > + > + received = rc - sizeof(*doe); This is not ideal. See comments on struct cdat_doe below. > + > if ((entry_handle == 0 && > - rc != sizeof(__le32) + sizeof(struct cdat_header)) || > + received != sizeof(doe->header[0])) || > (entry_handle > 0 && > - (rc < sizeof(__le32) + sizeof(*entry) || > - rc != sizeof(__le32) + le16_to_cpu(entry->length)))) > + (received < sizeof(doe->entry[0]) || > + received != le16_to_cpu(doe->entry->length)))) > return -EIO; > > /* Get the CXL table access header entry handle */ > entry_handle = FIELD_GET(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE, > - le32_to_cpu(data[0])); > - entry_dw = rc / sizeof(__le32); > - /* Skip Header */ > - entry_dw -= 1; > + le32_to_cpu(doe->doe_header)); > + > /* > * Table Access Response Header overwrote the last DW of > * previous entry, so restore that DW > */ > - *data = saved_dw; > - length -= entry_dw * sizeof(__le32); > - data += entry_dw; > - saved_dw = *data; > + doe->doe_header = saved_dw; > + remaining -= received; > + doe = (void *)doe + received; > + saved_dw = doe->doe_header; > } while (entry_handle != CXL_DOE_TABLE_ACCESS_LAST_ENTRY); > > /* Length in CDAT header may exceed concatenation of CDAT entries */ > - *cdat_length -= length - sizeof(__le32); > + *length -= remaining; > > return 0; > } > @@ -616,11 +614,11 @@ void read_cdat_data(struct cxl_port *port) > { > struct device *uport = port->uport_dev; > struct device *dev = &port->dev; > - struct pci_doe_mb *cdat_doe; > + struct pci_doe_mb *doe_mb; > struct pci_dev *pdev = NULL; > struct cxl_memdev *cxlmd; > - size_t cdat_length; > - void *cdat_table, *cdat_buf; > + struct cdat_doe *doe; Since we are trying to make this cleaner I would prefer a struct and variable name of cdat_doe_rsp. So... struct cdat_doe_rsp *cdat_doe_rsp; > + size_t length; > int rc; > > if (is_cxl_memdev(uport)) { > @@ -637,40 +635,38 @@ void read_cdat_data(struct cxl_port *port) > if (!pdev) > return; > > - cdat_doe = pci_find_doe_mailbox(pdev, PCI_DVSEC_VENDOR_ID_CXL, > - CXL_DOE_PROTOCOL_TABLE_ACCESS); > - if (!cdat_doe) { > + doe_mb = pci_find_doe_mailbox(pdev, PCI_DVSEC_VENDOR_ID_CXL, > + CXL_DOE_PROTOCOL_TABLE_ACCESS); > + if (!doe_mb) { > dev_dbg(dev, "No CDAT mailbox\n"); > return; > } > > port->cdat_available = true; > > - if (cxl_cdat_get_length(dev, cdat_doe, &cdat_length)) { > + if (cxl_cdat_get_length(dev, doe_mb, &length)) { > dev_dbg(dev, "No CDAT length\n"); > return; > } > > - cdat_buf = devm_kzalloc(dev, cdat_length + sizeof(__le32), > - GFP_KERNEL); > - if (!cdat_buf) > - return; > + doe = devm_kzalloc(dev, sizeof(*doe) + length, GFP_KERNEL); > + if (!doe) > + goto err; > > - rc = cxl_cdat_read_table(dev, cdat_doe, cdat_buf, &cdat_length); > + rc = cxl_cdat_read_table(dev, doe_mb, doe, &length); > if (rc) > goto err; > > - cdat_table = cdat_buf + sizeof(__le32); > - if (cdat_checksum(cdat_table, cdat_length)) > + if (cdat_checksum(doe->table, length)) > goto err; > > - port->cdat.table = cdat_table; > - port->cdat.length = cdat_length; > - return; > + port->cdat.table = doe->table; As an aside: the type of port->cdat may need to change at some point too. > + port->cdat.length = length; > > + return; > err: > /* Don't leave table data allocated on error */ > - devm_kfree(dev, cdat_buf); > + devm_kfree(dev, doe); > dev_err(dev, "Failed to read/validate CDAT.\n"); > } > EXPORT_SYMBOL_NS_GPL(read_cdat_data, CXL); > diff --git a/drivers/cxl/cxlpci.h b/drivers/cxl/cxlpci.h > index 0fa4799ea316..d12ed9d8dec1 100644 > --- a/drivers/cxl/cxlpci.h > +++ b/drivers/cxl/cxlpci.h > @@ -85,6 +85,25 @@ struct cdat_entry_header { > __le16 length; > } __packed; > > +/* > + * Response contains the CDAT only response header of the DOE. The > + * response payload is a CDAT structure (either CDAT header or entry), > + * it may also mark the beginning of the CDAT table. > + * > + * Spec refs: > + * > + * CXL 3.1 Table 8-14: Read Entry Response > + * CDAT Specification 1.03: 2 CDAT Data Structures > + */ > +struct cdat_doe { > + __le32 doe_header; > + union { > + u8 table[0]; At a minimum we need to use flexible arrays here. See: .../Documentation/process/deprecated.rst 'Zero-length and one-element arrays' > + struct cdat_header header[0]; > + struct cdat_entry_header entry[0]; So this would need to be: DECLARE_FLEX_ARRAY(u8, table); DECLARE_FLEX_ARRAY(struct cdat_header, header); DECLARE_FLEX_ARRAY(struct cdat_entry_header, entry); Also I think it would be best to use sizeof_field(). That said I got crossed up when this structure was used to represent not only the individual DOE responses but also the cdat table as a whole. I think it would be best to call this cdat_doe_rsp and somehow make a distinction in read_cdat_data() when assigning to the entire port cdat table. Ira > + }; > +} __packed; > + > int devm_cxl_port_enumerate_dports(struct cxl_port *port); > struct cxl_dev_state; > int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm, > -- > 2.39.2 > >
Ira Weiny wrote: > Robert Richter wrote: [..] > > - cdat_table = cdat_buf + sizeof(__le32); > > - if (cdat_checksum(cdat_table, cdat_length)) > > + if (cdat_checksum(doe->table, length)) > > goto err; > > > > - port->cdat.table = cdat_table; > > - port->cdat.length = cdat_length; > > - return; > > + port->cdat.table = doe->table; > > As an aside: the type of port->cdat may need to change at some point too. I did not understand this comment relative to what you expect to see in the next posting, but maybe Robert will when he respins this patch.
Dan Williams wrote: > Ira Weiny wrote: > > Robert Richter wrote: > [..] > > > - cdat_table = cdat_buf + sizeof(__le32); > > > - if (cdat_checksum(cdat_table, cdat_length)) > > > + if (cdat_checksum(doe->table, length)) > > > goto err; > > > > > > - port->cdat.table = cdat_table; > > > - port->cdat.length = cdat_length; > > > - return; > > > + port->cdat.table = doe->table; > > > > As an aside: the type of port->cdat may need to change at some point too. > > I did not understand this comment relative to what you expect to see in > the next posting, but maybe Robert will when he respins this patch. Robert introduced some new types which better defined what the read algorithm was doing. Right now the table is just a void *. It would be nice to type it more strongly as it starts to be used in the kernel. Ira
Robert Richter wrote: > On 17.11.23 21:09:18, Robert Richter wrote: > > I will send an on-top patch for 6.8 that reworks that code area to > > remove the pointer arithmetic. > > Here it is: > > From 13787f72c20b8c54754ae86015d982307eae0397 Mon Sep 17 00:00:00 2001 > From: Robert Richter <rrichter@amd.com> > Subject: [PATCH] cxl/pci: Get rid of pointer arithmetic reading CDAT table > > Reading the CDAT table using DOE requires a Table Access Response > Header in addition to the CDAT entry. In current implementation this > has caused offsets with sizeof(__le32) to the actual buffers. This led > to hardly readable code and even bugs (see fix of devm_kfree() in > read_cdat_data()). > > Rework code to avoid calculations with sizeof(__le32). Introduce > struct cdat_doe for this which contains the Table Access Response > Header and a variable payload size for various data structures > afterwards to access the CDAT table and its CDAT Data Structures > without recalculating buffer offsets. I like reworking the code to introduce an explicit type for the response buffer, but as Ira points out, lets call it a "response" not a "cdat_doe". The feedback on the flex array is accurate, but I see no reason to have 3 flex arrays vs: struct cdat_response { __le32 doe_header; union { struct cdat_header header; struct cdat_entry_header entry; u8 table[]; }; } __packed; As far as I can see nothing outside of drivers/cxl/core/pci.c needs 'struct cdat_response', so it can stay local to this C file. While you are working on that I will do another lead-in cleanup to kill the goto in cxl_cdat_read_table() and let you come back and kill off the open-coded "+ sizeof(__le32)" that I will leave behind.
This threat slipped away end of last year... On 14.12.23 20:34:09, Dan Williams wrote: > Robert Richter wrote: > > On 17.11.23 21:09:18, Robert Richter wrote: > > > I will send an on-top patch for 6.8 that reworks that code area to > > > remove the pointer arithmetic. > > > > Here it is: > > > > From 13787f72c20b8c54754ae86015d982307eae0397 Mon Sep 17 00:00:00 2001 > > From: Robert Richter <rrichter@amd.com> > > Subject: [PATCH] cxl/pci: Get rid of pointer arithmetic reading CDAT table > > > > Reading the CDAT table using DOE requires a Table Access Response > > Header in addition to the CDAT entry. In current implementation this > > has caused offsets with sizeof(__le32) to the actual buffers. This led > > to hardly readable code and even bugs (see fix of devm_kfree() in > > read_cdat_data()). > > > > Rework code to avoid calculations with sizeof(__le32). Introduce > > struct cdat_doe for this which contains the Table Access Response > > Header and a variable payload size for various data structures > > afterwards to access the CDAT table and its CDAT Data Structures > > without recalculating buffer offsets. > > I like reworking the code to introduce an explicit type for the response > buffer, but as Ira points out, lets call it a "response" not a > "cdat_doe". Looks good. > > The feedback on the flex array is accurate, but I see no reason to have > 3 flex arrays vs: > > struct cdat_response { > __le32 doe_header; > union { > struct cdat_header header; > struct cdat_entry_header entry; > u8 table[]; > }; > } __packed; The flex arrays are due to sizeof(*doe) which is just the size of the base payload without any variable data then. Another nice effect of this is pointer creation of @header and @entry: doe->header vs. &doe->header etc. ... which aligns with doe->table too. This all leads to well readable code. > > As far as I can see nothing outside of drivers/cxl/core/pci.c needs > 'struct cdat_response', so it can stay local to this C file. > > While you are working on that I will do another lead-in cleanup to kill > the goto in cxl_cdat_read_table() and let you come back and kill off the > open-coded "+ sizeof(__le32)" that I will leave behind. I briefly looked into your patch, but will send for reference a v2 with a rebase onto cxl/next and small updates only. I could prepare a v3 that bases on your patch afterwards. Thanks, -Robert
On 14.12.23 20:34:09, Dan Williams wrote: > struct cdat_response { > __le32 doe_header; > union { > struct cdat_header header; > struct cdat_entry_header entry; > u8 table[]; > }; > } __packed; > > As far as I can see nothing outside of drivers/cxl/core/pci.c needs > 'struct cdat_response', so it can stay local to this C file. I moved that close to cdat_header and cdat_entry_header which is also defined in cxlpci.h but only used in core/pci.c. I would like to keep them together but we could move that completely into pci.c in a separate patch? Thanks, -Robert
On 28.11.23 12:06:27, Ira Weiny wrote: > Robert Richter wrote: > > On 17.11.23 21:09:18, Robert Richter wrote: > > > I will send an on-top patch for 6.8 that reworks that code area to > > > remove the pointer arithmetic. > > > > Here it is: > > > > From 13787f72c20b8c54754ae86015d982307eae0397 Mon Sep 17 00:00:00 2001 > > From: Robert Richter <rrichter@amd.com> > > Subject: [PATCH] cxl/pci: Get rid of pointer arithmetic reading CDAT table > > > > Reading the CDAT table using DOE requires a Table Access Response > > Header in addition to the CDAT entry. In current implementation this > > has caused offsets with sizeof(__le32) to the actual buffers. This led > > to hardly readable code and even bugs (see fix of devm_kfree() in > > read_cdat_data()). > > > > Rework code to avoid calculations with sizeof(__le32). Introduce > > struct cdat_doe for this which contains the Table Access Response > > Header and a variable payload size for various data structures > > afterwards to access the CDAT table and its CDAT Data Structures > > without recalculating buffer offsets. > > Thanks for this. Thanks for your comments. > > > > > Cc: Lukas Wunner <lukas@wunner.de> > > Cc: Dave Jiang <dave.jiang@intel.com> > > Cc: Fan Ni <nifan.cxl@gmail.com> > > Signed-off-by: Robert Richter <rrichter@amd.com> > > --- > > drivers/cxl/core/pci.c | 80 ++++++++++++++++++++---------------------- > > drivers/cxl/cxlpci.h | 19 ++++++++++ > > 2 files changed, 57 insertions(+), 42 deletions(-) > > > > diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c > > index 5aaa0b36c42a..f900740c6dea 100644 > > --- a/drivers/cxl/core/pci.c > > +++ b/drivers/cxl/core/pci.c > > @@ -517,14 +517,14 @@ EXPORT_SYMBOL_NS_GPL(cxl_hdm_decode_init, CXL); > > FIELD_PREP(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE, (entry_handle))) > > > > static int cxl_cdat_get_length(struct device *dev, > > - struct pci_doe_mb *cdat_doe, > > + struct pci_doe_mb *doe_mb, > > NIT: Why change the variable name here? There was a conflict with cdat_doe, so I renamed this. Now, that cdat_doe is not introduced any longer there is not really a need. On the other side 'doe_mb' is much more describing the actual meaning, so I decided to change the name anyway in the next respin but do this with a separate patch. This makes esp. this patch much more readable. And of course. Jon would complain about it. :-) > > > size_t *length) > > { > > __le32 request = CDAT_DOE_REQ(0); > > __le32 response[2]; > > int rc; > > > > - rc = pci_doe(cdat_doe, PCI_DVSEC_VENDOR_ID_CXL, > > + rc = pci_doe(doe_mb, PCI_DVSEC_VENDOR_ID_CXL, > > CXL_DOE_PROTOCOL_TABLE_ACCESS, > > &request, sizeof(request), > > &response, sizeof(response)); > > @@ -542,56 +542,54 @@ static int cxl_cdat_get_length(struct device *dev, > > } > > > > static int cxl_cdat_read_table(struct device *dev, > > - struct pci_doe_mb *cdat_doe, > > - void *cdat_table, size_t *cdat_length) > > + struct pci_doe_mb *doe_mb, > > + struct cdat_doe *doe, size_t *length) > > { > > - size_t length = *cdat_length + sizeof(__le32); > > - __le32 *data = cdat_table; > > + size_t received, remaining = *length; > > int entry_handle = 0; > > __le32 saved_dw = 0; > > > > do { > > __le32 request = CDAT_DOE_REQ(entry_handle); > > - struct cdat_entry_header *entry; > > - size_t entry_dw; > > int rc; > > > > - rc = pci_doe(cdat_doe, PCI_DVSEC_VENDOR_ID_CXL, > > + rc = pci_doe(doe_mb, PCI_DVSEC_VENDOR_ID_CXL, > > CXL_DOE_PROTOCOL_TABLE_ACCESS, > > &request, sizeof(request), > > - data, length); > > + doe, sizeof(*doe) + remaining); > > if (rc < 0) { > > dev_err(dev, "DOE failed: %d", rc); > > return rc; > > } > > > > - /* 1 DW Table Access Response Header + CDAT entry */ > > - entry = (struct cdat_entry_header *)(data + 1); > > + if (rc < sizeof(*doe)) > > + return -EIO; > > + > > + received = rc - sizeof(*doe); > > This is not ideal. See comments on struct cdat_doe below. Thanks for your kernel doc pointer, I will rework the flex array handling. Here, there is not other way to get the header size of the struct (without the flexible arrays). Looking esp. at the implementation of struct_size() using sizeof() for actual variables is allowed and safe to use. > > > + > > if ((entry_handle == 0 && > > - rc != sizeof(__le32) + sizeof(struct cdat_header)) || > > + received != sizeof(doe->header[0])) || > > (entry_handle > 0 && > > - (rc < sizeof(__le32) + sizeof(*entry) || > > - rc != sizeof(__le32) + le16_to_cpu(entry->length)))) > > + (received < sizeof(doe->entry[0]) || > > + received != le16_to_cpu(doe->entry->length)))) > > return -EIO; > > > > /* Get the CXL table access header entry handle */ > > entry_handle = FIELD_GET(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE, > > - le32_to_cpu(data[0])); > > - entry_dw = rc / sizeof(__le32); > > - /* Skip Header */ > > - entry_dw -= 1; > > + le32_to_cpu(doe->doe_header)); > > + > > /* > > * Table Access Response Header overwrote the last DW of > > * previous entry, so restore that DW > > */ > > - *data = saved_dw; > > - length -= entry_dw * sizeof(__le32); > > - data += entry_dw; > > - saved_dw = *data; > > + doe->doe_header = saved_dw; > > + remaining -= received; > > + doe = (void *)doe + received; > > + saved_dw = doe->doe_header; > > } while (entry_handle != CXL_DOE_TABLE_ACCESS_LAST_ENTRY); > > > > /* Length in CDAT header may exceed concatenation of CDAT entries */ > > - *cdat_length -= length - sizeof(__le32); > > + *length -= remaining; > > > > return 0; > > } > > @@ -616,11 +614,11 @@ void read_cdat_data(struct cxl_port *port) > > { > > struct device *uport = port->uport_dev; > > struct device *dev = &port->dev; > > - struct pci_doe_mb *cdat_doe; > > + struct pci_doe_mb *doe_mb; > > struct pci_dev *pdev = NULL; > > struct cxl_memdev *cxlmd; > > - size_t cdat_length; > > - void *cdat_table, *cdat_buf; > > + struct cdat_doe *doe; > > Since we are trying to make this cleaner I would prefer a struct and > variable name of cdat_doe_rsp. > > So... > struct cdat_doe_rsp *cdat_doe_rsp; Looks reasonable, though I will use the shorter 'rsp' for the variable. > > > + size_t length; > > int rc; > > > > if (is_cxl_memdev(uport)) { > > @@ -637,40 +635,38 @@ void read_cdat_data(struct cxl_port *port) > > if (!pdev) > > return; > > > > - cdat_doe = pci_find_doe_mailbox(pdev, PCI_DVSEC_VENDOR_ID_CXL, > > - CXL_DOE_PROTOCOL_TABLE_ACCESS); > > - if (!cdat_doe) { > > + doe_mb = pci_find_doe_mailbox(pdev, PCI_DVSEC_VENDOR_ID_CXL, > > + CXL_DOE_PROTOCOL_TABLE_ACCESS); > > + if (!doe_mb) { > > dev_dbg(dev, "No CDAT mailbox\n"); > > return; > > } > > > > port->cdat_available = true; > > > > - if (cxl_cdat_get_length(dev, cdat_doe, &cdat_length)) { > > + if (cxl_cdat_get_length(dev, doe_mb, &length)) { > > dev_dbg(dev, "No CDAT length\n"); > > return; > > } > > > > - cdat_buf = devm_kzalloc(dev, cdat_length + sizeof(__le32), > > - GFP_KERNEL); > > - if (!cdat_buf) > > - return; > > + doe = devm_kzalloc(dev, sizeof(*doe) + length, GFP_KERNEL); > > + if (!doe) > > + goto err; > > > > - rc = cxl_cdat_read_table(dev, cdat_doe, cdat_buf, &cdat_length); > > + rc = cxl_cdat_read_table(dev, doe_mb, doe, &length); > > if (rc) > > goto err; > > > > - cdat_table = cdat_buf + sizeof(__le32); > > - if (cdat_checksum(cdat_table, cdat_length)) > > + if (cdat_checksum(doe->table, length)) > > goto err; > > > > - port->cdat.table = cdat_table; > > - port->cdat.length = cdat_length; > > - return; > > + port->cdat.table = doe->table; > > As an aside: the type of port->cdat may need to change at some point too. > > > + port->cdat.length = length; > > > > + return; > > err: > > /* Don't leave table data allocated on error */ > > - devm_kfree(dev, cdat_buf); > > + devm_kfree(dev, doe); > > dev_err(dev, "Failed to read/validate CDAT.\n"); > > } > > EXPORT_SYMBOL_NS_GPL(read_cdat_data, CXL); > > diff --git a/drivers/cxl/cxlpci.h b/drivers/cxl/cxlpci.h > > index 0fa4799ea316..d12ed9d8dec1 100644 > > --- a/drivers/cxl/cxlpci.h > > +++ b/drivers/cxl/cxlpci.h > > @@ -85,6 +85,25 @@ struct cdat_entry_header { > > __le16 length; > > } __packed; > > > > +/* > > + * Response contains the CDAT only response header of the DOE. The > > + * response payload is a CDAT structure (either CDAT header or entry), > > + * it may also mark the beginning of the CDAT table. > > + * > > + * Spec refs: > > + * > > + * CXL 3.1 Table 8-14: Read Entry Response > > + * CDAT Specification 1.03: 2 CDAT Data Structures > > + */ > > +struct cdat_doe { > > + __le32 doe_header; > > + union { > > + u8 table[0]; > > At a minimum we need to use flexible arrays here. See: > > .../Documentation/process/deprecated.rst > 'Zero-length and one-element arrays' > > > + struct cdat_header header[0]; > > + struct cdat_entry_header entry[0]; > > So this would need to be: > > DECLARE_FLEX_ARRAY(u8, table); > DECLARE_FLEX_ARRAY(struct cdat_header, header); > DECLARE_FLEX_ARRAY(struct cdat_entry_header, entry); Changed that. > > Also I think it would be best to use sizeof_field(). > > That said I got crossed up when this structure was used to represent not > only the individual DOE responses but also the cdat table as a whole. > > I think it would be best to call this cdat_doe_rsp and somehow make a > distinction in read_cdat_data() when assigning to the entire port cdat > table. I will name the table buffer's variable 'buf' and add a comment. Thanks, -Robert > > Ira > > > + }; > > +} __packed; > > + > > int devm_cxl_port_enumerate_dports(struct cxl_port *port); > > struct cxl_dev_state; > > int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm, > > -- > > 2.39.2 > > > > > >
diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c index 5aaa0b36c42a..f900740c6dea 100644 --- a/drivers/cxl/core/pci.c +++ b/drivers/cxl/core/pci.c @@ -517,14 +517,14 @@ EXPORT_SYMBOL_NS_GPL(cxl_hdm_decode_init, CXL); FIELD_PREP(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE, (entry_handle))) static int cxl_cdat_get_length(struct device *dev, - struct pci_doe_mb *cdat_doe, + struct pci_doe_mb *doe_mb, size_t *length) { __le32 request = CDAT_DOE_REQ(0); __le32 response[2]; int rc; - rc = pci_doe(cdat_doe, PCI_DVSEC_VENDOR_ID_CXL, + rc = pci_doe(doe_mb, PCI_DVSEC_VENDOR_ID_CXL, CXL_DOE_PROTOCOL_TABLE_ACCESS, &request, sizeof(request), &response, sizeof(response)); @@ -542,56 +542,54 @@ static int cxl_cdat_get_length(struct device *dev, } static int cxl_cdat_read_table(struct device *dev, - struct pci_doe_mb *cdat_doe, - void *cdat_table, size_t *cdat_length) + struct pci_doe_mb *doe_mb, + struct cdat_doe *doe, size_t *length) { - size_t length = *cdat_length + sizeof(__le32); - __le32 *data = cdat_table; + size_t received, remaining = *length; int entry_handle = 0; __le32 saved_dw = 0; do { __le32 request = CDAT_DOE_REQ(entry_handle); - struct cdat_entry_header *entry; - size_t entry_dw; int rc; - rc = pci_doe(cdat_doe, PCI_DVSEC_VENDOR_ID_CXL, + rc = pci_doe(doe_mb, PCI_DVSEC_VENDOR_ID_CXL, CXL_DOE_PROTOCOL_TABLE_ACCESS, &request, sizeof(request), - data, length); + doe, sizeof(*doe) + remaining); if (rc < 0) { dev_err(dev, "DOE failed: %d", rc); return rc; } - /* 1 DW Table Access Response Header + CDAT entry */ - entry = (struct cdat_entry_header *)(data + 1); + if (rc < sizeof(*doe)) + return -EIO; + + received = rc - sizeof(*doe); + if ((entry_handle == 0 && - rc != sizeof(__le32) + sizeof(struct cdat_header)) || + received != sizeof(doe->header[0])) || (entry_handle > 0 && - (rc < sizeof(__le32) + sizeof(*entry) || - rc != sizeof(__le32) + le16_to_cpu(entry->length)))) + (received < sizeof(doe->entry[0]) || + received != le16_to_cpu(doe->entry->length)))) return -EIO; /* Get the CXL table access header entry handle */ entry_handle = FIELD_GET(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE, - le32_to_cpu(data[0])); - entry_dw = rc / sizeof(__le32); - /* Skip Header */ - entry_dw -= 1; + le32_to_cpu(doe->doe_header)); + /* * Table Access Response Header overwrote the last DW of * previous entry, so restore that DW */ - *data = saved_dw; - length -= entry_dw * sizeof(__le32); - data += entry_dw; - saved_dw = *data; + doe->doe_header = saved_dw; + remaining -= received; + doe = (void *)doe + received; + saved_dw = doe->doe_header; } while (entry_handle != CXL_DOE_TABLE_ACCESS_LAST_ENTRY); /* Length in CDAT header may exceed concatenation of CDAT entries */ - *cdat_length -= length - sizeof(__le32); + *length -= remaining; return 0; } @@ -616,11 +614,11 @@ void read_cdat_data(struct cxl_port *port) { struct device *uport = port->uport_dev; struct device *dev = &port->dev; - struct pci_doe_mb *cdat_doe; + struct pci_doe_mb *doe_mb; struct pci_dev *pdev = NULL; struct cxl_memdev *cxlmd; - size_t cdat_length; - void *cdat_table, *cdat_buf; + struct cdat_doe *doe; + size_t length; int rc; if (is_cxl_memdev(uport)) { @@ -637,40 +635,38 @@ void read_cdat_data(struct cxl_port *port) if (!pdev) return; - cdat_doe = pci_find_doe_mailbox(pdev, PCI_DVSEC_VENDOR_ID_CXL, - CXL_DOE_PROTOCOL_TABLE_ACCESS); - if (!cdat_doe) { + doe_mb = pci_find_doe_mailbox(pdev, PCI_DVSEC_VENDOR_ID_CXL, + CXL_DOE_PROTOCOL_TABLE_ACCESS); + if (!doe_mb) { dev_dbg(dev, "No CDAT mailbox\n"); return; } port->cdat_available = true; - if (cxl_cdat_get_length(dev, cdat_doe, &cdat_length)) { + if (cxl_cdat_get_length(dev, doe_mb, &length)) { dev_dbg(dev, "No CDAT length\n"); return; } - cdat_buf = devm_kzalloc(dev, cdat_length + sizeof(__le32), - GFP_KERNEL); - if (!cdat_buf) - return; + doe = devm_kzalloc(dev, sizeof(*doe) + length, GFP_KERNEL); + if (!doe) + goto err; - rc = cxl_cdat_read_table(dev, cdat_doe, cdat_buf, &cdat_length); + rc = cxl_cdat_read_table(dev, doe_mb, doe, &length); if (rc) goto err; - cdat_table = cdat_buf + sizeof(__le32); - if (cdat_checksum(cdat_table, cdat_length)) + if (cdat_checksum(doe->table, length)) goto err; - port->cdat.table = cdat_table; - port->cdat.length = cdat_length; - return; + port->cdat.table = doe->table; + port->cdat.length = length; + return; err: /* Don't leave table data allocated on error */ - devm_kfree(dev, cdat_buf); + devm_kfree(dev, doe); dev_err(dev, "Failed to read/validate CDAT.\n"); } EXPORT_SYMBOL_NS_GPL(read_cdat_data, CXL); diff --git a/drivers/cxl/cxlpci.h b/drivers/cxl/cxlpci.h index 0fa4799ea316..d12ed9d8dec1 100644 --- a/drivers/cxl/cxlpci.h +++ b/drivers/cxl/cxlpci.h @@ -85,6 +85,25 @@ struct cdat_entry_header { __le16 length; } __packed; +/* + * Response contains the CDAT only response header of the DOE. The + * response payload is a CDAT structure (either CDAT header or entry), + * it may also mark the beginning of the CDAT table. + * + * Spec refs: + * + * CXL 3.1 Table 8-14: Read Entry Response + * CDAT Specification 1.03: 2 CDAT Data Structures + */ +struct cdat_doe { + __le32 doe_header; + union { + u8 table[0]; + struct cdat_header header[0]; + struct cdat_entry_header entry[0]; + }; +} __packed; + int devm_cxl_port_enumerate_dports(struct cxl_port *port); struct cxl_dev_state; int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,