Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60852C433EF for ; Tue, 3 May 2022 18:57:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238525AbiECTAj (ORCPT ); Tue, 3 May 2022 15:00:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238429AbiECTAf (ORCPT ); Tue, 3 May 2022 15:00:35 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2048.outbound.protection.outlook.com [40.107.93.48]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95FA525C5A for ; Tue, 3 May 2022 11:57:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cqrzVhlpT5Z8RCuUvOEA9s7O2ztUoNGEsZ79TGpC//NpNhhwIAm5YehvqrqQHDlqSs7mSjn/8ULoiPWOAmMUBeQenBfuikLa2fm3iYDqB0Nj4nf9FymVAL6X2p9BaeLQeElLcaFRun4A+hsKSCFn0ZxijGAAqgQWe5wAGJ/DRq6xxpb83WhLoeVDn8VUOTxTr0Di1QdxaOXOt8v/Y0uOhCpUrN6iGuc11OFT+Zufg4hJVUop8Ww7LHMLC+t4QVfmInCDiHcQKmzkEkkUBZWFy13w1wX56jy4rVbdqci3dsAx7H/kqMF+oqjJlsrPEwsPDSR4XL0DPLRDhwc9CjBIRQ== 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=u4smPnT9mkm92BFUAGeVn+lW/TgkAFUEcuxCXsObwH0=; b=ALFjWJ6nzC0tBHuKVrwrPejImElr6FgtrLb+jhke4IxPKHkN7a/YLl2befi15EPM9czKygufATtUGIphyIGPZTxh75PfULwKEIWLJNF7XrBQPs8fIt0jWkT3Wyprsu9EQ9ODAvWoWsoiDlY94SjGviIULicBS7tMttjDIE6eadP7g6rLEbOjETl9rX8SwpeXpL9Z6Wlq2EZ7viSOwkBhd2RoF70vFkz6GZoFbFJsKEUuYn6Z0vINMlKx0iKuMr1W+I//yzY9nfzvklE0l8btUJ7MflaTs/XiB1TXMtKCPYCQqUZBqXCMezYrzNubND2gRUuNXTzpQ8M0L6z+mhHcsQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=u4smPnT9mkm92BFUAGeVn+lW/TgkAFUEcuxCXsObwH0=; b=XlCY0a69NY9QeMKvX3Htjw+/SCycY9PKpZaCFuTuaAwUgMytIFABAUmdM8VXPov64bV6R9TDwpQtsS/TjkvVAwQbAV5gkMbIAF+UB1mpHUClHDl+9BFK9vBeEmHxAzbTyFADDtz1IH690ktY8xig+XtPKRrgIGhJIzosFt4B0XPqHhjP7y3YSWL/oNWTBQkChMD+nejQRh57O48HIjMDCibr93twYsM4Yb6+cDFQkHySIYYNEMFHIDVEZ/Cw/Z7uajdqNVmiLLQ6hFcX1oTR/21Ypk380ceHHZrOgKghI6HPZTSpC55VcArHKPeYNs3wjb+Of2a4RjuhTzdK9KCdvA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from BL1PR12MB5142.namprd12.prod.outlook.com (2603:10b6:208:312::22) by BL1PR12MB5158.namprd12.prod.outlook.com (2603:10b6:208:31c::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5206.13; Tue, 3 May 2022 18:57:00 +0000 Received: from BL1PR12MB5142.namprd12.prod.outlook.com ([fe80::cdc2:b13d:821b:d3d7]) by BL1PR12MB5142.namprd12.prod.outlook.com ([fe80::cdc2:b13d:821b:d3d7%3]) with mapi id 15.20.5206.024; Tue, 3 May 2022 18:56:59 +0000 Message-ID: Date: Tue, 3 May 2022 21:56:48 +0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.8.1 Subject: Re: [PATCH net-next] tls: Add opt-in zerocopy mode of sendfile() Content-Language: en-US To: Jakub Kicinski Cc: "David S. Miller" , Daniel Borkmann , Paolo Abeni , Boris Pismenny , Tariq Toukan , Saeed Mahameed , Gal Pressman , netdev@vger.kernel.org References: <20220427175048.225235-1-maximmi@nvidia.com> <20220428151142.3f0ccd83@kernel.org> <20220429121117.21bf7490@kernel.org> From: Maxim Mikityanskiy In-Reply-To: <20220429121117.21bf7490@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-ClientProxiedBy: LO4P265CA0089.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:2bc::6) To BL1PR12MB5142.namprd12.prod.outlook.com (2603:10b6:208:312::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 29fa541e-eda0-406b-1db5-08da2d36b3cf X-MS-TrafficTypeDiagnostic: BL1PR12MB5158:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: NRLmetgM3x35U+okwburBrn+77GakPnuLWPXUupxQnp4CDfC9I+802Y8383gwgfHpnpBt6sFVJGxVtUTjYBI3IgibY8qY/RijCSTlioEzhYwFS/Q47iLMMuV63lCEa1OzQawiQ9q5yre4rdutVHUdMh8GQ16hNGEl+4Ite2edL/qlNgtmKlryPmFQifebSzVCAgJ+dIze7zwtj6ViBjipEbhVP9oJ+XApLNZd888NLgeBjz8djbNjuwjoRpi2NVy4zewJsU8lqfEmwtcwlis8vTUANkuIw86NJMayzcEX6LNjmY7Ej9A5KCN6+3lR+lzZqWYSU8or3z6VNFWYrUfd+NjzAOhJXJ87HDQRY/u4p3eOOhA9zvldKKzfdajGylBo+yJVXcGXRQR+AaQILvkDPbEmdDy/Tznz48NprefvQLNyWX63avJRT2TF1JZkZrSNL5nS3GyGhJXkN5F57jkmBaROq7lqlKS+SKeDXGq25r61CuuX34jMg46JxBSVZJdae3uj7YcfvTF419DhTCBtJ5pKx9WjBP5BiRq4ofEnluXDPBfI5ih7VYFrMR+P5F3pqJbdqi1FShCWBgmkKMYp7Q9RnMqSZwC835zeQAUjh+MAjOXnP++E4Jrgg/PWGx7DALo8DUh7WyC7eWvmg+1erKDYd9p8tnvlPoqZjS6f5DQdzt+sJJyXFaRJ77sOjFh5dQ1zYsLyijvrmdUqr+mdsU/JYdAqc8uSe9Zn5Tx6XISUZwT7gckuOsHYgw9F8JA X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BL1PR12MB5142.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(4636009)(366004)(6486002)(2906002)(316002)(2616005)(6666004)(38100700002)(86362001)(31696002)(186003)(8936002)(83380400001)(508600001)(53546011)(6506007)(5660300002)(26005)(6512007)(66476007)(36756003)(8676002)(66556008)(4326008)(66946007)(6916009)(31686004)(54906003)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?N1pVcHZLWGZjOXhDUm8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utf-8?B?WFJYQWZjQ09VU0E4eDRJalRpL09ldTY0QnY4dXBYU0NxVHVOUDVIbERGUlRS?= =?utf-8?B?aWREYkFHOWxiL3VTUzE0akRrNzNLaEcrdkNRemdCdlpqSEJSRkVFY1hsSi9O?= =?utf-8?B?WXNXc3haZVpSVkNoTEFtUEQ4bVZtd0E1WHA5Qm8xaUg2dUwvakxvWktKRVk0?= =?utf-8?B?bnQ1RUN5ek1ZUC8xb0c4RGU1ekthVFNzNk9zTitCWUphZ0Jsc2NQbHRsTGlG?= =?utf-8?B?U1gwSjY0a3gycHllODBocldwaUVxRWY4bVl3WU5SSWNRMmZ3UzBsYTV2Wjgy?= =?utf-8?B?ME12ZlRSanpJMkNIWk9UbURLRHNjWTdBNGFlSmRaNGJtdStWamN3WE5EU3U2?= =?utf-8?B?cXN3cmRKRWtpelJoRWdYaUVsSVpoS3BncHhqRXRhUDk0YTRkN2pBUEJvQTcy?= =?utf-8?B?aEZ4Z2VQazJ3NDJ2YjNFNFV2d1R6TUZiUDE3aFhpS1VQUWE2WE9MREdmTmgr?= =?utf-8?B?L2ZyT2RCeDZSaiswMmVySXVQSFlHdGM3cno2UWo2bjVJa1dqNnZrQT09?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 29fa541e-eda0-406b-1db5-08da2d36b3cf X-MS-Exchange-CrossTenant-AuthSource: BL1PR12MB5142.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 May 2022 18:56:59.7806 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: t1cSkV/4ztoPdirIwh0oMB8vtbS2pDbeZwfaxkpW1UbaRYRhIBqVp4QwHUBn4Z6LI2ScxAq8LZye+4mvClDXsA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5158 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org On 2022-04-29 22:11, Jakub Kicinski wrote: > On Fri, 29 Apr 2022 17:21:59 +0300 Maxim Mikityanskiy wrote: >> On 2022-04-29 01:11, Jakub Kicinski wrote: >>>> diff --git a/net/tls/tls_device.c b/net/tls/tls_device.c >>>> index b12f81a2b44c..715401b20c8b 100644 >>>> --- a/net/tls/tls_device.c >>>> +++ b/net/tls/tls_device.c >>>> @@ -411,10 +411,16 @@ static int tls_device_copy_data(void *addr, size_t bytes, struct iov_iter *i) >>>> return 0; >>>> } >>>> >>>> +union tls_iter_offset { >>>> + struct iov_iter *msg_iter; >>>> + int offset; >>>> +}; >>> >>> Is this sort of atrocity used elsewhere in the kernel? >>> If you can't refactor the code you can pack args into >>> a structure >> >> What's the point of packing arguments into a struct in this particular >> case? Depending on zc_page, I need either msg_iter or offset, and I'm >> reusing the same CPU register to pass either of them. The performance >> isn't affected, and the amount of memory used is the same. A struct >> won't allow to achieve this, it would force me to drag 8 extra bytes, >> but we already use all 6 registers used to pass parameters on x86_64. > > I know why you're doing this, but you're not writing assembly: > > + rc = tls_push_data(sk, (union tls_iter_offset)&msg_iter, size, > > + return tls_push_data(sk, (union tls_iter_offset)&msg_iter, 0, flags, > > even if it's legal C (i.e. not UB) it looks awkward. It's not UB, cast between a union and its element type is well-defined in C. >>> but I've not seen people cast mutually exclusive >>> arguments to a union type. >> >> It's the purpose of a union, to hold one of mutually exclusive values, >> isn't it? > > The union itself is not the problem. > >>> Is this "inspired" by some higher >>> level language? >> >> It's unfortunately inspired by C and its freedom to allow >> microoptimizations/hacks. The hack here is that I use a pointer being >> NULL or not-NULL as an indicator what type the other argument has. >> >> The closest alternative from high-level languages I can think of is >> enums with attached data from rust or swift. However, rust isn't smart >> enough to perform the optimization I described, so no, it's not inspired >> by it :) >> >> Options that I see here: >> >> 1. Union. >> >> 2. Just pass both parameters and use one of them. Drawbacks: now we have >> 7 parameters, one will be passed through the stack, and it's datapath code. >> >> 3. Pass `struct iov_iter *` and cast it to `int offset` when zc_page >> isn't NULL. As we are compiling with -fno-strict-aliasing, and int >> shouldn't be bigger than a pointer on all supported architectures, it's >> going to work, and we still have 6 parameters. >> >> 4. Combine `int offset` and `int flags` into a single 64-bit parameter. >> >> Which one do you prefer, or do you have anything better in mind? > > If you declare the union on the stack in the callers, and pass by value > - is the compiler not going to be clever enough to still DDRT? Ah, OK, it should do the thing. I thought you wanted me to ditch the union altogether. >>>> static int tls_push_data(struct sock *sk, >>>> - struct iov_iter *msg_iter, >>>> + union tls_iter_offset iter_offset, >>>> size_t size, int flags, >>>> - unsigned char record_type) >>>> + unsigned char record_type, >>>> + struct page *zc_page) >>>> { >>>> struct tls_context *tls_ctx = tls_get_ctx(sk); >>>> struct tls_prot_info *prot = &tls_ctx->prot_info; >>>> @@ -480,15 +486,29 @@ static int tls_push_data(struct sock *sk, >>>> } >>>> >>>> record = ctx->open_record; >>>> - copy = min_t(size_t, size, (pfrag->size - pfrag->offset)); >>>> - copy = min_t(size_t, copy, (max_open_record_len - record->len)); >>>> - >>>> - if (copy) { >>>> - rc = tls_device_copy_data(page_address(pfrag->page) + >>>> - pfrag->offset, copy, msg_iter); >>>> - if (rc) >>>> - goto handle_error; >>>> - tls_append_frag(record, pfrag, copy); >>>> + >>>> + if (!zc_page) { >>>> + copy = min_t(size_t, size, pfrag->size - pfrag->offset); >>>> + copy = min_t(size_t, copy, max_open_record_len - record->len); >>> >>> Nope, refactor this please. 95% sure you don't need 4 indentation >>> levels here. Space left in record can be calculated before the if, >>> then you can do >>> >>> if (zc_page) { >>> .. >>> } else if (copy) { >>> .. >>> } >> >> It'll save one indentation level for the zc_page case, but not for the >> other: >> >> copy = min_t(size_t, size, max_open_record_len - record->len); >> if (zc_page && copy) { >> ... >> } else { >> // I still have to do this in non-zc case: >> copy = min_t(size_t, copy, pfrag->size - pfrag->offset); > > Can pfrag->size - pfrag->offset really be 0 provided > tls_do_allocation() did not return an error? No, it can't. Now it makes more sense to me. Thanks for the clarification. >> if (copy) { >> // Same indentation level as in my patch. >> ... >> } >> } >> >> Is it good enough? > >>> We should allow setting the option for non-HW. It's an opt-in, the app >>> has opted in, the fact that the kernel will not make use of the liberty >>> to apply the optimization is not important, no? >> >> Yes, I agree that if the application opted in, it should work properly >> regardless of whether the optimization actually did turn on. However, >> the indication could be useful, for example, for diagnostic purposes, to >> show the user whether zerocopy mode was enabled, if someone is trying to >> debug some performance issue. If you insist, though, I can make >> setsockopt succeed and getsockopt return 1. What do you think? > > I'd say "whether the optimization is applicable" rather than "whether > the optimization is turned on". User can check whether the connection > is using SW or HW TLS if they want to make sure it's taken advantage of. > > Speaking of which, should we report the state of this knob via socket > diag? That sounds like an option, I'll take a look. TLS doesn't expose anything via diag yet, does it? The only option to distinguish SW/HW TLS is ethtool, and there is no per-socket check, right? Cause a HW TLS socket can downgrade to SW after tls_device_down, and ethtool won't show it.