From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A8C68C48BE8 for ; Fri, 18 Jun 2021 21:32:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8745E61209 for ; Fri, 18 Jun 2021 21:32:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234007AbhFRVek (ORCPT ); Fri, 18 Jun 2021 17:34:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56536 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232997AbhFRVei (ORCPT ); Fri, 18 Jun 2021 17:34:38 -0400 Received: from zeniv-ca.linux.org.uk (zeniv-ca.linux.org.uk [IPv6:2607:5300:60:148a::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 101F1C061574; Fri, 18 Jun 2021 14:32:28 -0700 (PDT) Received: from viro by zeniv-ca.linux.org.uk with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1luM6B-009kzH-QZ; Fri, 18 Jun 2021 21:32:24 +0000 Date: Fri, 18 Jun 2021 21:32:23 +0000 From: Al Viro To: Linus Torvalds Cc: Omar Sandoval , linux-fsdevel , linux-btrfs , Linux API , Kernel Team Subject: Re: [PATCH RESEND x3 v9 1/9] iov_iter: add copy_struct_from_iter() Message-ID: References: <6caae597eb20da5ea23e53e8e64ce0c4f4d9c6d2.1623972519.git.osandov@fb.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: Al Viro Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org On Fri, Jun 18, 2021 at 02:10:36PM -0700, Linus Torvalds wrote: > > Just put the size of the encoded part first and be done with that. > > Magical effect of the iovec sizes is a bloody bad idea. > > That makes everything uglier and more complicated, honestly. Then > you'd have to do it in _two_ operations ("get the size, then get the > rest"), *AND* you'd have to worry about all the corner-cases (ie > people putting the structure in pieces across multiple iov entries. Huh? All corner cases are already taken care of by copy_from_iter{,_full}(). What I'm proposing is to have the size as a field in 'encoded' and do this if (!copy_from_iter_full(&encoded, sizeof(encoded), &i)) return -EFAULT; if (encoded.size > sizeof(encoded)) { // newer than what we expect if (!iov_iter_check_zeroes(&i, encoded.size - sizeof(encoded)) return -EINVAL; } else if (encoded.size < sizeof(encoded)) { // older than what we expect iov_iter_revert(&i, sizeof(encoded) - encoded.size); memset((void *)&encoded + encoded.size, 0, sizoef(encoded) - encoded.size); } I don't think it would be more complex, but that's a matter of taste; I *really* doubt it would be any slower or have higher odds of bugs, regardless of the corner cases. And it certainly would be much smaller on the lib/iov_iter.c side - implementation of iov_iter_check_zeroes() would be simply this: bool iov_iter_check_zeroes(struct iov_iter *i, size_t size) { bool failed = false; iterate_and_advance(i, bytes, base, len, off, failed = (check_zeroed_user(base, len) != 1), failed = (memchr_inv(base, 0, len) != NULL)) if (unlikely(failed)) iov_iter_revert(i, bytes); return !failed; } And that's it, no need to do anything special for xarray, etc. This + EXPORT_SYMBOL + extern in uio.h + snippet above in the user... I could buy an argument that for userland the need to add encoded.size = sizeof(encoded); or equivalent when initializing that thing would make life too complex, but on the kernel side I'd say that Omar's variant is considerably more complex than the above...