From patchwork Fri Dec 3 15:38:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 12655397 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 87F4FC433EF for ; Fri, 3 Dec 2021 15:39:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4EDE36B0073; Fri, 3 Dec 2021 10:38:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 49C0A6B0075; Fri, 3 Dec 2021 10:38:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2111F6B007B; Fri, 3 Dec 2021 10:38:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0236.hostedemail.com [216.40.44.236]) by kanga.kvack.org (Postfix) with ESMTP id 040496B0073 for ; Fri, 3 Dec 2021 10:38:46 -0500 (EST) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id BBB9682499B9 for ; Fri, 3 Dec 2021 15:38:35 +0000 (UTC) X-FDA: 78876890190.07.19FEBC6 Received: from mail-il1-f182.google.com (mail-il1-f182.google.com [209.85.166.182]) by imf14.hostedemail.com (Postfix) with ESMTP id 646ED6001986 for ; Fri, 3 Dec 2021 15:38:35 +0000 (UTC) Received: by mail-il1-f182.google.com with SMTP id m5so3046811ilh.11 for ; Fri, 03 Dec 2021 07:38:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4DFUFvaV0G/Q7jMqV2RbUUz9oX67OaVlTxzTJrNM/Ao=; b=US11acHjTsIZlHpwqc0CpkAopjX/F5w0mftn0NuN4EDfAAQooNYA9xLEhw5iAM323n 7qmNVtAAwmRGZ629l9uQibQFdzyhBn3Hmi4rNPJBse095i1lL4Rf6hsHskPDU85pVEpw s9M04LXnIC9YJIWREWtBmilPyEa2S7hkKulF8yl0/CAHowduzlhMSR2a+7HNzgcGyUPP IqWkBlhrWC3CAjm5maf5prTyjj3ij37qmA1sFel7FeQGbzQgTydxDL64jtXSgAlBW77+ WmJfmlFtjbokqGU2i+vlQd7oMIVjwb3qfMRNeqWBsNPcRZaH3XOp7hDAQhnOiswsYDXJ 7Fjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4DFUFvaV0G/Q7jMqV2RbUUz9oX67OaVlTxzTJrNM/Ao=; b=KnQHTEiOMZ5kMO+x76YfwjWGKGhPwdN/u3GKJfgOC8CIEio4Gn0d/zpmukzmz6GK/C LVFXdoa+pld+VszzP8UVfvfaCiSV3/2Vk+DvReHslvBi0Ysainwz8o3AxV9kTsx7lMl+ 46Ljhr8lD+hg7UVZ1R1LBK9COff47pLt2naeVuXkR9rrAaj2emMnMQLR9bfhbZPDwvqn H7G4Dh5KqgM/itv9awC7LaUtiprAgVrMc/eeNeE7hbUSCLoU3iUuUlG+Jb8KKNsT6kOG /NBES0QtPHl6dsDhMWnEVIKPv/IJconNODr/sYc6W9VsICbIRIcJslFRzapWZ61f3Q8D JXlw== X-Gm-Message-State: AOAM533BBgrx7FoD/+2a3V1Xoan9S1IqJvBHfyu5GgkjTHMOWSBYQ06P jAzOFAtZ/v4kjqcobhU52C6csw== X-Google-Smtp-Source: ABdhPJxF5B/TooZb00Dmyre+qqFISBooHUDLdPHCH1GXfo3mnLG1tprnmFTQZC7G9sMIsZ9oBk4wCw== X-Received: by 2002:a92:d24e:: with SMTP id v14mr18550782ilg.55.1638545914395; Fri, 03 Dec 2021 07:38:34 -0800 (PST) Received: from p1.localdomain ([207.135.234.126]) by smtp.gmail.com with ESMTPSA id c15sm1753042ilq.50.2021.12.03.07.38.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Dec 2021 07:38:34 -0800 (PST) From: Jens Axboe To: linux-block@vger.kernel.org, linux-mm@kvack.org Cc: Jens Axboe Subject: [PATCH 1/2] mm: move filemap_range_needs_writeback() into header Date: Fri, 3 Dec 2021 08:38:28 -0700 Message-Id: <20211203153829.298893-2-axboe@kernel.dk> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20211203153829.298893-1-axboe@kernel.dk> References: <20211203153829.298893-1-axboe@kernel.dk> MIME-Version: 1.0 X-Stat-Signature: cmemdzs9fsmte6oujbbhm39wdbbh3nh5 Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=kernel-dk.20210112.gappssmtp.com header.s=20210112 header.b=US11acHj; dmarc=none; spf=pass (imf14.hostedemail.com: domain of axboe@kernel.dk designates 209.85.166.182 as permitted sender) smtp.mailfrom=axboe@kernel.dk X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 646ED6001986 X-HE-Tag: 1638545915-735817 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: No functional changes in this patch, just in preparation for efficiently calling this light function from the block O_DIRECT handling. Signed-off-by: Jens Axboe Reviewed-by: Matthew Wilcox (Oracle) --- include/linux/fs.h | 35 +++++++++++++++++++++++++++++++++++ mm/filemap.c | 38 +++----------------------------------- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/include/linux/fs.h b/include/linux/fs.h index bbf812ce89a8..0cc4f5fd4cfe 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2845,6 +2845,41 @@ static inline int filemap_fdatawait(struct address_space *mapping) return filemap_fdatawait_range(mapping, 0, LLONG_MAX); } +/* Returns true if writeback might be needed or already in progress. */ +static inline bool mapping_needs_writeback(struct address_space *mapping) +{ + return mapping->nrpages; +} + +bool filemap_range_has_writeback(struct address_space *mapping, + loff_t start_byte, loff_t end_byte); + +/** + * filemap_range_needs_writeback - check if range potentially needs writeback + * @mapping: address space within which to check + * @start_byte: offset in bytes where the range starts + * @end_byte: offset in bytes where the range ends (inclusive) + * + * Find at least one page in the range supplied, usually used to check if + * direct writing in this range will trigger a writeback. Used by O_DIRECT + * read/write with IOCB_NOWAIT, to see if the caller needs to do + * filemap_write_and_wait_range() before proceeding. + * + * Return: %true if the caller should do filemap_write_and_wait_range() before + * doing O_DIRECT to a page in this range, %false otherwise. + */ +static inline bool filemap_range_needs_writeback(struct address_space *mapping, + loff_t start_byte, + loff_t end_byte) +{ + if (!mapping_needs_writeback(mapping)) + return false; + if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && + !mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK)) + return false; + return filemap_range_has_writeback(mapping, start_byte, end_byte); +} + extern bool filemap_range_has_page(struct address_space *, loff_t lstart, loff_t lend); extern bool filemap_range_needs_writeback(struct address_space *, diff --git a/mm/filemap.c b/mm/filemap.c index daa0e23a6ee6..65238440fa0a 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -640,14 +640,8 @@ int filemap_fdatawait_keep_errors(struct address_space *mapping) } EXPORT_SYMBOL(filemap_fdatawait_keep_errors); -/* Returns true if writeback might be needed or already in progress. */ -static bool mapping_needs_writeback(struct address_space *mapping) -{ - return mapping->nrpages; -} - -static bool filemap_range_has_writeback(struct address_space *mapping, - loff_t start_byte, loff_t end_byte) +bool filemap_range_has_writeback(struct address_space *mapping, + loff_t start_byte, loff_t end_byte) { XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT); pgoff_t max = end_byte >> PAGE_SHIFT; @@ -667,34 +661,8 @@ static bool filemap_range_has_writeback(struct address_space *mapping, } rcu_read_unlock(); return page != NULL; - -} - -/** - * filemap_range_needs_writeback - check if range potentially needs writeback - * @mapping: address space within which to check - * @start_byte: offset in bytes where the range starts - * @end_byte: offset in bytes where the range ends (inclusive) - * - * Find at least one page in the range supplied, usually used to check if - * direct writing in this range will trigger a writeback. Used by O_DIRECT - * read/write with IOCB_NOWAIT, to see if the caller needs to do - * filemap_write_and_wait_range() before proceeding. - * - * Return: %true if the caller should do filemap_write_and_wait_range() before - * doing O_DIRECT to a page in this range, %false otherwise. - */ -bool filemap_range_needs_writeback(struct address_space *mapping, - loff_t start_byte, loff_t end_byte) -{ - if (!mapping_needs_writeback(mapping)) - return false; - if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && - !mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK)) - return false; - return filemap_range_has_writeback(mapping, start_byte, end_byte); } -EXPORT_SYMBOL_GPL(filemap_range_needs_writeback); +EXPORT_SYMBOL_GPL(filemap_range_has_writeback); /** * filemap_write_and_wait_range - write out & wait on a file range From patchwork Fri Dec 3 15:38:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 12655399 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 6A751C433F5 for ; Fri, 3 Dec 2021 15:39:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EFE266B0075; Fri, 3 Dec 2021 10:38:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EAD8B6B0078; Fri, 3 Dec 2021 10:38:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D75296B007B; Fri, 3 Dec 2021 10:38:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0084.hostedemail.com [216.40.44.84]) by kanga.kvack.org (Postfix) with ESMTP id C8F836B0075 for ; Fri, 3 Dec 2021 10:38:46 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 8B8091813CB88 for ; Fri, 3 Dec 2021 15:38:36 +0000 (UTC) X-FDA: 78876890232.16.CC0BD99 Received: from mail-il1-f170.google.com (mail-il1-f170.google.com [209.85.166.170]) by imf01.hostedemail.com (Postfix) with ESMTP id 1F7F040002 for ; Fri, 3 Dec 2021 15:38:35 +0000 (UTC) Received: by mail-il1-f170.google.com with SMTP id h16so3087337ila.4 for ; Fri, 03 Dec 2021 07:38:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=n0F2twjIx4NmxUZNO6rvzL0OidoPYFlJudXfQWPXRJI=; b=HOYS28dB+hf7PjSdhk0KEDvwuC5KVLlJPN5YqZZZeFpwh8+wgfG6WE3Y7xlAXUNK00 riELvOdzKOTEmTmSBb+trw1x9oUpxw1UZqyOjnz0zLRLXYcMCcS3dmjaOmUffQVxQyWs 4VeJjqwe+mOtQ3u133rotKSCR5U3vawPNBedcF8wtFZrah9Ajeb42I9KLxdKj2NWFEz0 QvdC9dbengdqfr5dpD50D/WlahrRAOc/wEUI6yZgsHVrufF5mOX6Zcih6rKfmf3jVo/D nD3idtZJws1ItQ71mu/xBTJWti1NjCa85o7CVYgFxv2x/IhSO9uVmXXEEPD6/2MAedbG bScw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=n0F2twjIx4NmxUZNO6rvzL0OidoPYFlJudXfQWPXRJI=; b=Z4v79uh5qZdCBHMW1k8NJJ0fOwEhquW+kes+44AkRAahaQ4w+jKF72HBrvUJ42knDp untSy/pKjYvwahNnGa6pUoUOUtiwsMoHXhG6zjkBjaYo1bYGSYSAdJ/8g2ipmywGSvHd z0Txw36H0cG04uxSs/qqpr9+4+dV2jth3bJY6Rq2ounAiL+dZvAgrokH4raKreje6P7P 94AwwQpxmfkYiT7uDGa5EFPLGNSf3LgHA8mVC6YCP/eecwgE23x7QmP1hgU1vjdZJIi6 OBuR5mEeKIHdx7MN5y+DcGAS9wCMupzWhA58mlS5faN+Nc+dAHosDxvnKwcT6JkSchHl GCeg== X-Gm-Message-State: AOAM5313kd8wVcIgQv1ZPp2oPRJFoEZvzGsoPsI6zQWAag9J0jXSLS1A nbBw72B3v4qofif8GsXOaA4g9w== X-Google-Smtp-Source: ABdhPJypBb2scW85GQGRdq2BTbwmtkv74gMtEvSbk1YgLbiD/4HFcK6ryenFEVuW9Pac/1fyDooBww== X-Received: by 2002:a92:4a04:: with SMTP id m4mr18093924ilf.103.1638545915002; Fri, 03 Dec 2021 07:38:35 -0800 (PST) Received: from p1.localdomain ([207.135.234.126]) by smtp.gmail.com with ESMTPSA id c15sm1753042ilq.50.2021.12.03.07.38.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Dec 2021 07:38:34 -0800 (PST) From: Jens Axboe To: linux-block@vger.kernel.org, linux-mm@kvack.org Cc: Jens Axboe Subject: [PATCH 2/2] block: move direct_IO into our own read_iter handler Date: Fri, 3 Dec 2021 08:38:29 -0700 Message-Id: <20211203153829.298893-3-axboe@kernel.dk> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20211203153829.298893-1-axboe@kernel.dk> References: <20211203153829.298893-1-axboe@kernel.dk> MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 1F7F040002 X-Stat-Signature: sews4tmhr6ff5gfabj7xuhdy78arjop4 Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel-dk.20210112.gappssmtp.com header.s=20210112 header.b=HOYS28dB; spf=pass (imf01.hostedemail.com: domain of axboe@kernel.dk designates 209.85.166.170 as permitted sender) smtp.mailfrom=axboe@kernel.dk; dmarc=none X-HE-Tag: 1638545915-139271 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: Don't call into generic_file_read_iter() if we know it's O_DIRECT, just set it up ourselves and call our own handler. This avoids an indirect call for O_DIRECT. Fall back to filemap_read() if we fail. Signed-off-by: Jens Axboe --- block/fops.c | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/block/fops.c b/block/fops.c index 93bb5bf66f69..10015e1a5b01 100644 --- a/block/fops.c +++ b/block/fops.c @@ -566,21 +566,48 @@ static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to) { struct block_device *bdev = iocb->ki_filp->private_data; loff_t size = bdev_nr_bytes(bdev); + size_t count = iov_iter_count(to); loff_t pos = iocb->ki_pos; size_t shorted = 0; - ssize_t ret; + ssize_t ret = 0; - if (unlikely(pos + iov_iter_count(to) > size)) { + if (unlikely(pos + count > size)) { if (pos >= size) return 0; size -= pos; - if (iov_iter_count(to) > size) { - shorted = iov_iter_count(to) - size; + if (count > size) { + shorted = count - size; iov_iter_truncate(to, size); } } - ret = generic_file_read_iter(iocb, to); + if (iocb->ki_flags & IOCB_DIRECT) { + struct address_space *mapping = iocb->ki_filp->f_mapping; + + if (iocb->ki_flags & IOCB_NOWAIT) { + if (filemap_range_needs_writeback(mapping, iocb->ki_pos, + iocb->ki_pos + count - 1)) + return -EAGAIN; + } else { + ret = filemap_write_and_wait_range(mapping, + iocb->ki_pos, + iocb->ki_pos + count - 1); + if (ret < 0) + return ret; + } + + file_accessed(iocb->ki_filp); + + ret = blkdev_direct_IO(iocb, to); + if (ret >= 0) { + iocb->ki_pos += ret; + count -= ret; + } + if (ret < 0 || !count) + return ret; + } + + ret = filemap_read(iocb, to, ret); if (unlikely(shorted)) iov_iter_reexpand(to, iov_iter_count(to) + shorted);