From patchwork Thu Jan 26 20:15:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 13117709 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3A907C54EAA for ; Thu, 26 Jan 2023 20:16:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC8468E0001; Thu, 26 Jan 2023 15:16:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A77886B0075; Thu, 26 Jan 2023 15:16:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 966CF8E0001; Thu, 26 Jan 2023 15:16:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 836806B0074 for ; Thu, 26 Jan 2023 15:16:00 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 5351B1C67F9 for ; Thu, 26 Jan 2023 20:16:00 +0000 (UTC) X-FDA: 80398056480.17.F0BBBB8 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf20.hostedemail.com (Postfix) with ESMTP id B96A31C0006 for ; Thu, 26 Jan 2023 20:15:58 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b="UsTx/4AL"; spf=none (imf20.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674764158; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=ewxYBF8rCgV9/rysFXLSjcXueedMcKRMGf5oGjYm2XM=; b=Gk/A32isShX1NEMTd+EAu5wSL9JaMyRpJBNET2ecVbyJ+dubC0xrjfNwi7w/oqysG6SEVg gG97ugNrKZ3YmPlwWULNOFfvdpoepxadFaR5hMv91LbZEeeB02YEDaaleJxkXCgf1qNTfJ V6iFxY9f5PBdS2G3UnTmHe1hvIKarts= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b="UsTx/4AL"; spf=none (imf20.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674764158; a=rsa-sha256; cv=none; b=YKCDLsisPUQoLBgXabqpxMblwzKGvYS54B5MByyQ+dvzX4gAMP7maO9IxfEbNU+b0OBz8S F/5zH6pKGBiXJh8e8y+CImvwFPAaxLOkZV9rUU1XrQ7DnS9PW55yHhf9cehBP8cAoM6g0z tsvbSC2FZDfOAzLqMOZCeeEWXluHz3s= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:In-Reply-To:References; bh=ewxYBF8rCgV9/rysFXLSjcXueedMcKRMGf5oGjYm2XM=; b=UsTx/4ALB9cFNh15XNo+OrOl5t q/Pq1uGc0bwb+6ugOPQ2mrvEyhqLwLSYVzsCNBRmJjqsJISUPk0LkG+zx3eFwL5b6rMY7kXfaswJZ u/I8zM5lv7rpNF/W2CV+cAYSA6C/TpDLDQEiIcOiv/FflEr2Ag6hYc48ya9ZTPDsNolPbj4BQrT3X EUDc7GYJ/ye0gGIPJvDhj7jS4f2Y+uI7lMNd7vYW4DtlEXc9lzgQDdoUY5REIkAldrhrbv77ke9F9 iQrOFsbpsKjowzR7q9GjUD93Za704felpCbz+FLm+ugzu9qDKFEXIqzI1VYOrCbJgNt3cyKufoC1T okqCB+hA==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pL8f5-0073Sj-3U; Thu, 26 Jan 2023 20:15:55 +0000 From: "Matthew Wilcox (Oracle)" To: Andrew Morton Cc: "Matthew Wilcox (Oracle)" , "Fabio M. De Francesco" , Ira Weiny , linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Subject: [PATCH] mm: Add memcpy_from_file_folio() Date: Thu, 26 Jan 2023 20:15:52 +0000 Message-Id: <20230126201552.1681588-1-willy@infradead.org> X-Mailer: git-send-email 2.37.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: B96A31C0006 X-Stat-Signature: c8q7nj98pjk6ifgncgft53fu9utht4ni X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1674764158-706596 X-HE-Meta: U2FsdGVkX183flbdVisYl7KNoJI1o0G9teIrmsJXi3qLp+QHYpRGQVrXcHNRzyPDBQmX92ym/2XHYqJzKkvRSkEbX+V/M6ab0jIz/WEtYNF4fx1I2AEviNSgWWRIBZ5g0CtcaKFLj1b8NXT7pKssPWuCCDRg0ScaSCzQuj2VGEfMn5wmpEuyQlseq0Mm9sHucj6VtFsU0LPVsxoUUn5O8Uix45chCpVcCEtLZm3IKXAyIH0qHsaQWGn3Azafvgx46A2B26kL1FHU0LlCbmY/F2GEoVUNETk21Y/W7lN39GBhZmM973qUZ4IKP3rffXAq0SWB2tfT5U4lK+8ZEEoA62t0jvxXJ/XIC67EKLsUBh1hWQBkgmuoVIxvxgDd9UKX5UmjDzq19/p1HTa0sI2ifDNu6VXU6XMCOwMhsSWYct0iwue2ZUxJDCN5tMAUm8x4/cfkqYlr+ix7/U1JTyL/160A4iW4sKcpnHVJZnq9m+lfzM6Gs4ksYCbQGHYKWbR1OovG4Uj/5oSKHeWVo7G74W1uLqyZjAnJABeLWqTN33aNhfhlorxRnPBATZw2TtqMTfb8xhYpwK8Kmyn3v7zGufyilJaqsmWe8rZT6eOy5KX9rpq/2jSlkm/L5Foa9UJDPwsuMTCwdrI2DjZnkYRzgmKkh8Dmjg5oFysC3MA2E6TylTAS/d0GQ0vJ8qykmY6oeVHtO0tQajqz/8M0TzGNMDnXmtz4A5OX+T8wRSjEdBSuDFYNPoelwzO0EUmiq7f+IUPBGLVGhx06BIv5idC8MXZ31WGj/oJ18lVKXGePFRTDdaSHXQNPnKRrKT0oDF3UFOz0eiKpulOlF1/WvSkGlawVQr32z1u9ggG5ohTVL1BWNzZ1JaUArK/0NxCBNhJWEM5OHHnqiJv3/59Q2tQ8hRV9vKt7nI6OoqsJWZZy8ySrtI5CAYmlzx+/4yehNxjwdDaHeilpGhC2EDkBUnp h2fLwEOB q9CkpojGU3tXEYm41MSE240yaKupHVwzi0ThUh3TMjVuGB9FzOtnyJQEzGdTyQ+t9LN00TBszCRzLkr+D/Qohg8/VWIYPDErsd7KCLfHZ1eMk7nxlN957iOqBCJKdYEMkBhBI8pF7fNpiicxn+dCBg3lGAyf2tGo63vjYjgmCVUchuaaGLwut+PyffYHpIZ6sEZvI17MXYhT6HO8CqALrv04TCB19F3tB72zKsQZzUKgX+KFmCiln5bKgXQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This is the equivalent of memcpy_from_page(). It differs in that it takes the position in a file instead of offset in a folio, it accepts the total number of bytes to be copied (instead of the number of bytes to be copied from this folio) and it returns how many bytes were copied from the folio, rather than making the caller calculate that and then checking if the caller got it right. Signed-off-by: Matthew Wilcox (Oracle) --- include/linux/highmem.h | 29 +++++++++++++++++++++++++++++ include/linux/page-flags.h | 1 + 2 files changed, 30 insertions(+) diff --git a/include/linux/highmem.h b/include/linux/highmem.h index e22509420ac6..b517707b028d 100644 --- a/include/linux/highmem.h +++ b/include/linux/highmem.h @@ -413,6 +413,35 @@ static inline void memzero_page(struct page *page, size_t offset, size_t len) kunmap_local(addr); } +/** + * memcpy_from_file_folio - Copy some bytes from a file folio. + * @to: The destination buffer. + * @folio: The folio to copy from. + * @pos: The position in the file. + * @len: The maximum number of bytes to copy. + * + * Copy up to @len bytes from this folio. This may be limited by PAGE_SIZE + * if the folio comes from HIGHMEM, and by the size of the folio. + * + * Return: The number of bytes copied from the folio. + */ +static inline size_t memcpy_from_file_folio(char *to, struct folio *folio, + loff_t pos, size_t len) +{ + size_t offset = offset_in_folio(folio, pos); + char *from = kmap_local_folio(folio, offset); + + if (folio_test_highmem(folio)) + len = min(len, PAGE_SIZE - offset); + else + len = min(len, folio_size(folio) - offset); + + memcpy(to, from, len); + kunmap_local(from); + + return len; +} + /** * folio_zero_segments() - Zero two byte ranges in a folio. * @folio: The folio to write to. diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index 09804ad91927..0425f22a9c82 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -531,6 +531,7 @@ PAGEFLAG(Readahead, readahead, PF_NO_COMPOUND) * available at this point. */ #define PageHighMem(__p) is_highmem_idx(page_zonenum(__p)) +#define folio_test_highmem(__f) is_highmem_idx(folio_zonenum(__f)) #else PAGEFLAG_FALSE(HighMem, highmem) #endif