From patchwork Tue Dec 10 16:24:50 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 11282943 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EA016109A for ; Tue, 10 Dec 2019 16:25:02 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AD7EC207FF for ; Tue, 10 Dec 2019 16:25:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=kernel-dk.20150623.gappssmtp.com header.i=@kernel-dk.20150623.gappssmtp.com header.b="hhZ7xYi6" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AD7EC207FF Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 609CC6B2D38; Tue, 10 Dec 2019 11:25:00 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 5B8946B2D39; Tue, 10 Dec 2019 11:25:00 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4F7916B2D3A; Tue, 10 Dec 2019 11:25:00 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0130.hostedemail.com [216.40.44.130]) by kanga.kvack.org (Postfix) with ESMTP id 2B8156B2D38 for ; Tue, 10 Dec 2019 11:25:00 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with SMTP id DEA393D12 for ; Tue, 10 Dec 2019 16:24:59 +0000 (UTC) X-FDA: 76249755918.12.plant30_e7c582802148 X-Spam-Summary: 40,2.5,0,ad1870cb10c5df9c,d41d8cd98f00b204,axboe@kernel.dk,::linux-fsdevel@vger.kernel.org:linux-block@vger.kernel.org:axboe@kernel.dk,RULES_HIT:41:355:379:541:800:960:968:988:989:1260:1311:1314:1345:1359:1431:1437:1515:1535:1544:1605:1711:1730:1747:1777:1792:2194:2198:2199:2200:2393:2559:2562:3138:3139:3140:3141:3142:3865:3866:3867:3870:3871:3872:3874:4118:4250:4321:4605:5007:6261:6653:7875:7903:8531:8957:10011:11026:11232:11473:11658:11914:12043:12291:12296:12297:12438:12517:12519:12555:12683:12895:12986:13894:14096:14110:14181:14394:14721:21080:21444:21451:21627:21740:21990:30054,0,RBL:209.85.166.194:@kernel.dk:.lbl8.mailshell.net-62.2.0.100 66.100.201.201,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:1:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: plant30_e7c582802148 X-Filterd-Recvd-Size: 7620 Received: from mail-il1-f194.google.com (mail-il1-f194.google.com [209.85.166.194]) by imf48.hostedemail.com (Postfix) with ESMTP for ; Tue, 10 Dec 2019 16:24:59 +0000 (UTC) Received: by mail-il1-f194.google.com with SMTP id b15so16632665ila.7 for ; Tue, 10 Dec 2019 08:24:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=g934jRFitGK2n249ARajPKYt/UpPdfJOZffggpxpM9A=; b=hhZ7xYi6iAcufTEkO6fc43ItaJGoyWZp9Y4vBkYqSNRtCXuWhdjFbxsbu9OktdC0IO nW3gunLzXkKUO8wgJ+0yeoSppeRI+4D7y2tLEMNIdky0dwtNX9fQ4rFUoawcOgFbAgdp 9E0vTmcrsVvH1daaGcCOFDz7jCcGskD3Tp/aKtGTezQEJ9j19GUYbGJplvp7jB8alSM9 leCUbUP7hxo8rytSDGedyPJAe3H4NsHZiigF8DhwcE2J2SfZO82HFed+Pj7kzwG+JQk8 l3AeLqWHAdDGy/QPsdnbrN019CohHLWmjj2JGShCbv+uEBHUxUgsIQ6MAbnS3j1dCpKu lIFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=g934jRFitGK2n249ARajPKYt/UpPdfJOZffggpxpM9A=; b=ACLaUiizwCk+B4lkZ++lCzteJ7FvWdywtSC7SM30SWwbpk70rbukRFQzDnpD/oCA4R LbqiX7q8YRkRF0HKo1ojcrAVN9t1cjsnaPnmliI/eBAonrT8AMfG/m3sdueu+eUdDTX7 tTywPtenDHoLJfObn3sEq48bHtmUdTbSbjhk6+PDYsEmV2Sf4v2D2OpZadAbbxq7pckp zw0EPTi0NPfbphelHqQb96s3are3zp+lEY5SlhwxD0Ej5Z269AZ8+GMMyLjneaRTbVu3 yRvykddVli72ARXPaFPItB3d5ulLEZn69ay2d1VI7pCWFXYRUVPQ6nKi2pNhYRhq5Pg7 Kn4g== X-Gm-Message-State: APjAAAWCqvYjgU5KxLs5HZ0HqMVR7L3+WsoNmHlqF8DctrNtyUOlbx8f 0EvwgHTkkkzGnQbfc4/yBBe77Zum6QP5iQ== X-Google-Smtp-Source: APXvYqwjwuIZVZbUrdsT5Ok1m2EfYH1iKu0ClsiGMys8G17rmQyXyJbWlm1NyAXEqetUUjxlwNaq8A== X-Received: by 2002:a92:84dd:: with SMTP id y90mr31666919ilk.99.1575995098180; Tue, 10 Dec 2019 08:24:58 -0800 (PST) Received: from x1.thefacebook.com ([65.144.74.34]) by smtp.gmail.com with ESMTPSA id y11sm791174iol.23.2019.12.10.08.24.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Dec 2019 08:24:57 -0800 (PST) From: Jens Axboe To: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org Cc: Jens Axboe Subject: [PATCH 1/5] fs: add read support for RWF_UNCACHED Date: Tue, 10 Dec 2019 09:24:50 -0700 Message-Id: <20191210162454.8608-2-axboe@kernel.dk> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191210162454.8608-1-axboe@kernel.dk> References: <20191210162454.8608-1-axboe@kernel.dk> MIME-Version: 1.0 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: If RWF_UNCACHED is set for io_uring (or preadv2(2)), we'll drop the cache for buffered reads if we are the ones instantiating it. If the data is already cached, we leave it cached. Signed-off-by: Jens Axboe --- include/linux/fs.h | 3 +++ include/uapi/linux/fs.h | 5 ++++- mm/filemap.c | 46 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/include/linux/fs.h b/include/linux/fs.h index 98e0349adb52..092ea2a4319b 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -314,6 +314,7 @@ enum rw_hint { #define IOCB_SYNC (1 << 5) #define IOCB_WRITE (1 << 6) #define IOCB_NOWAIT (1 << 7) +#define IOCB_UNCACHED (1 << 8) struct kiocb { struct file *ki_filp; @@ -3418,6 +3419,8 @@ static inline int kiocb_set_rw_flags(struct kiocb *ki, rwf_t flags) ki->ki_flags |= (IOCB_DSYNC | IOCB_SYNC); if (flags & RWF_APPEND) ki->ki_flags |= IOCB_APPEND; + if (flags & RWF_UNCACHED) + ki->ki_flags |= IOCB_UNCACHED; return 0; } diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h index 379a612f8f1d..357ebb0e0c5d 100644 --- a/include/uapi/linux/fs.h +++ b/include/uapi/linux/fs.h @@ -299,8 +299,11 @@ typedef int __bitwise __kernel_rwf_t; /* per-IO O_APPEND */ #define RWF_APPEND ((__force __kernel_rwf_t)0x00000010) +/* drop cache after reading or writing data */ +#define RWF_UNCACHED ((__force __kernel_rwf_t)0x00000040) + /* mask of flags supported by the kernel */ #define RWF_SUPPORTED (RWF_HIPRI | RWF_DSYNC | RWF_SYNC | RWF_NOWAIT |\ - RWF_APPEND) + RWF_APPEND | RWF_UNCACHED) #endif /* _UAPI_LINUX_FS_H */ diff --git a/mm/filemap.c b/mm/filemap.c index bf6aa30be58d..ed23a11b3e34 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -933,8 +933,8 @@ int add_to_page_cache_locked(struct page *page, struct address_space *mapping, } EXPORT_SYMBOL(add_to_page_cache_locked); -int add_to_page_cache_lru(struct page *page, struct address_space *mapping, - pgoff_t offset, gfp_t gfp_mask) +static int __add_to_page_cache(struct page *page, struct address_space *mapping, + pgoff_t offset, gfp_t gfp_mask, bool lru) { void *shadow = NULL; int ret; @@ -956,9 +956,17 @@ int add_to_page_cache_lru(struct page *page, struct address_space *mapping, WARN_ON_ONCE(PageActive(page)); if (!(gfp_mask & __GFP_WRITE) && shadow) workingset_refault(page, shadow); - lru_cache_add(page); + if (lru) + lru_cache_add(page); } return ret; + +} + +int add_to_page_cache_lru(struct page *page, struct address_space *mapping, + pgoff_t offset, gfp_t gfp_mask) +{ + return __add_to_page_cache(page, mapping, offset, gfp_mask, true); } EXPORT_SYMBOL_GPL(add_to_page_cache_lru); @@ -2032,6 +2040,7 @@ static ssize_t generic_file_buffered_read(struct kiocb *iocb, offset = *ppos & ~PAGE_MASK; for (;;) { + bool drop_page = false; struct page *page; pgoff_t end_index; loff_t isize; @@ -2048,6 +2057,9 @@ static ssize_t generic_file_buffered_read(struct kiocb *iocb, if (!page) { if (iocb->ki_flags & IOCB_NOWAIT) goto would_block; + /* UNCACHED implies no read-ahead */ + if (iocb->ki_flags & IOCB_UNCACHED) + goto no_cached_page; page_cache_sync_readahead(mapping, ra, filp, index, last_index - index); @@ -2147,6 +2159,26 @@ static ssize_t generic_file_buffered_read(struct kiocb *iocb, offset &= ~PAGE_MASK; prev_offset = offset; + /* + * If we're dropping this page due to drop-behind, then + * lock it first. Ignore errors here, we can just leave it + * in the page cache. Note that we didn't add this page to + * the LRU when we added it to the page cache. So if we + * fail removing it, or lock it, add to the LRU. + */ + if (drop_page) { + bool addlru = true; + + if (!lock_page_killable(page)) { + if (page->mapping == mapping) + addlru = !remove_mapping(mapping, page); + else + addlru = false; + unlock_page(page); + } + if (addlru) + lru_cache_add(page); + } put_page(page); written += ret; if (!iov_iter_count(iter)) @@ -2234,8 +2266,12 @@ static ssize_t generic_file_buffered_read(struct kiocb *iocb, error = -ENOMEM; goto out; } - error = add_to_page_cache_lru(page, mapping, index, - mapping_gfp_constraint(mapping, GFP_KERNEL)); + if (iocb->ki_flags & IOCB_UNCACHED) + drop_page = true; + + error = __add_to_page_cache(page, mapping, index, + mapping_gfp_constraint(mapping, GFP_KERNEL), + !drop_page); if (error) { put_page(page); if (error == -EEXIST) {