Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp3042305ybg; Sat, 6 Jun 2020 08:56:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyPdMpZBR4h4zYlTTygLhj/8kz+PcqKkvRXExcYGxFfhcZlBsS0QwsmOcbDO5r0+5/jX3WU X-Received: by 2002:aa7:d74c:: with SMTP id a12mr6973238eds.369.1591459014108; Sat, 06 Jun 2020 08:56:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591459014; cv=none; d=google.com; s=arc-20160816; b=RLKHt+9ca2w3lVNR1fTW125phBkNMNTxJpXKu5MRPlKdI3cmqPc8Lny3gNpxte5q1l o0DFdqYcfJzo7Eo5fCSxh2wiMX8EsB6IqNcW5u/sNI+d4Pk1vL/nhmcp6mTEtNHZiV84 WgO3d3My/ZAT02lrGIrP5fhkWqKBHs0u0FYFc6/KBY8qCl0G9dP+OYp7QmNGw3IgZo5q L4qG++PTCL9JtNoCMDQZ6UPN80CiMPUbpJl8mPr6mhKjyGDStVdOUk5RJHGsz4su5l89 x9Q9pMHLo4vDZvLPo11b7YxkTdC1C2xNCcOrX/BMrhh2a8Ffn4V55LFNto3/ovklNA54 +11Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=9Knot+Lv+Z0XYiseAFBiA62c5+NhkBbKX/gLTI08qrs=; b=WPBglnc/8fVHwcMj2+itwhvOVGK/lVPhOy60sh9THQkOjZ4OIrmAXcmmesS8d9J6JC SmfUuU/GH+qtmDm63OUl5apYhlQ/8R8SGxs6InS/VggNlby6olCuOy1zz1aXpIsdvWhQ bYt2ontw8kDb0mWIPMJvNBqkhmjbjGJ5FIfYJ+DFntDkCOKhlebTD5qvo4JU1OyEmmZn LJlEaGDXcV7gd7xfPw2ku85QYFV8A9GH5nRQtvvrduz6IWZwVowijn3z79iF2LSvenFW 9vz/09CNvr98B8YAaWhiJjhtBXUbE2Og7wCCucuRWINOQaykayNgAr8xo+jTQzK8aqzf af+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=BIkLwIVP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 65si5826209edn.401.2020.06.06.08.56.31; Sat, 06 Jun 2020 08:56:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=BIkLwIVP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728660AbgFFPwa (ORCPT + 99 others); Sat, 6 Jun 2020 11:52:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727100AbgFFPw3 (ORCPT ); Sat, 6 Jun 2020 11:52:29 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E378C03E96A; Sat, 6 Jun 2020 08:52:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=9Knot+Lv+Z0XYiseAFBiA62c5+NhkBbKX/gLTI08qrs=; b=BIkLwIVPjTQ4Z6dLUSwN9BNF2d jF6fhDw7QKtkUY0qVUNSVq9AuL8hjm+dtqFVaVANc6f6TM0lnrILTlyCzv0Zn5AN1XdlGt9cdBwHi zD4X30KKbBKG0kzETQ6drLgJ3Xm5yfjabYFhl168BExjPSZP8wck4zfkTJ/uEj0mPlieD9rGrnMG6 oHS20QVWiOO4dubZEecydxU8HQa8CKlZjNY5CeOYhnFTWlNFS18HJHQ5PEw6bzRyrREoU/dHDX3lo 5nr4gMAxgOOu45lOty30n8+bLPrmT9YJXIxJAyFMEXFkjOBDaMyZrsiHZP9xz4cTJdqHnZJglVMc5 Y9vIXEPQ==; Received: from willy by bombadil.infradead.org with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1jhb7I-0001SV-JG; Sat, 06 Jun 2020 15:52:16 +0000 Date: Sat, 6 Jun 2020 08:52:16 -0700 From: Matthew Wilcox To: Scott Branden Cc: Luis Chamberlain , Wolfram Sang , Greg Kroah-Hartman , David Brown , Alexander Viro , Shuah Khan , bjorn.andersson@linaro.org, Shuah Khan , Arnd Bergmann , Mimi Zohar , "Rafael J . Wysocki" , linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-fsdevel@vger.kernel.org, BCM Kernel Feedback , Olof Johansson , Andrew Morton , Dan Carpenter , Colin Ian King , Kees Cook , Takashi Iwai , linux-kselftest@vger.kernel.org, Andy Gross , linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, Christoph Hellwig Subject: Re: [PATCH v7 1/8] fs: introduce kernel_pread_file* support Message-ID: <20200606155216.GP19604@bombadil.infradead.org> References: <20200606050458.17281-1-scott.branden@broadcom.com> <20200606050458.17281-2-scott.branden@broadcom.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200606050458.17281-2-scott.branden@broadcom.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 05, 2020 at 10:04:51PM -0700, Scott Branden wrote: > -int kernel_read_file(struct file *file, void **buf, loff_t *size, > - loff_t max_size, enum kernel_read_file_id id) > -{ > - loff_t i_size, pos; > +int kernel_pread_file(struct file *file, void **buf, loff_t *size, > + loff_t pos, loff_t max_size, > + enum kernel_pread_opt opt, > + enum kernel_read_file_id id) > +{ > + loff_t alloc_size; > + loff_t buf_pos; > + loff_t read_end; > + loff_t i_size; > ssize_t bytes = 0; > int ret; > Look, it's not your fault, but this is a great example of how we end up with atrocious interfaces. Someone comes along and implements a simple DWIM interface that solves their problem. Then somebody else adds a slight variant that solves their problem, and so on and so on, and we end up with this bonkers API where the arguments literally change meaning depending on other arguments. > @@ -950,21 +955,31 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size, > ret = -EINVAL; > goto out; > } > - if (i_size > SIZE_MAX || (max_size > 0 && i_size > max_size)) { > + > + /* Default read to end of file */ > + read_end = i_size; > + > + /* Allow reading partial portion of file */ > + if ((opt == KERNEL_PREAD_PART) && > + (i_size > (pos + max_size))) > + read_end = pos + max_size; > + > + alloc_size = read_end - pos; > + if (i_size > SIZE_MAX || (max_size > 0 && alloc_size > max_size)) { > ret = -EFBIG; > goto out; ... like that. I think what we actually want is: ssize_t vmap_file_range(struct file *, loff_t start, loff_t end, void **bufp); void vunmap_file_range(struct file *, void *buf); If end > i_size, limit the allocation to i_size. Returns the number of bytes allocated, or a negative errno. Writes the pointer allocated to *bufp. Internally, it should use the page cache to read in the pages (taking appropriate reference counts). Then it maps them using vmap() instead of copying them to a private vmalloc() array. kernel_read_file() can be converted to use this API. The users will need to be changed to call kernel_read_end(struct file *file, void *buf) instead of vfree() so it can call allow_write_access() for them. vmap_file_range() has a lot of potential uses. I'm surprised we don't have it already, to be honest.