From patchwork Thu May 3 17:46:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 10378791 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 436EA603B4 for ; Thu, 3 May 2018 17:47:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 253A0291FF for ; Thu, 3 May 2018 17:47:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 190C729203; Thu, 3 May 2018 17:47:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 29424291FF for ; Thu, 3 May 2018 17:47:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2B8636B0007; Thu, 3 May 2018 13:47:26 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 28E3C6B0009; Thu, 3 May 2018 13:47:26 -0400 (EDT) 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 17EDD6B000A; Thu, 3 May 2018 13:47:26 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk0-f198.google.com (mail-qk0-f198.google.com [209.85.220.198]) by kanga.kvack.org (Postfix) with ESMTP id E12476B0007 for ; Thu, 3 May 2018 13:47:25 -0400 (EDT) Received: by mail-qk0-f198.google.com with SMTP id d128so13744658qkf.18 for ; Thu, 03 May 2018 10:47:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:date:from:to:cc:subject :message-id:mime-version:content-disposition:user-agent; bh=O0zRygE7IMq1Zf6gAz4DxL4z9STb4+s1ZOoQ7R8B24I=; b=kBCtp4KBYFRaMcS0AjwrTQm7IGFcIWIid6UtdWAaiq7CrqXejO1iW8bPuPKMY2SnWP 09yvuRmDKhxAjhWIDyeBFI3qht6Sss0kxOx8Xgq2XX984aP7RmrrEjVRZobUXvcdq/px fV/T65hB6vqR3gHG/ydZn3thpPs5m/3wCDExgOwN4NkD9gw4ijQLXNrPzZTdLWAmku9C l7Q4+cs6U8oSnbQLfmwLfBDiemIySzvRy4Lo+mH9IyV8AMbSJw23nJTCuQ/24e9rG1kL gdEM9JO4kvvABJQu69H5lCRc/DtZpKm9VIIhJkmKDh8dIiVtPzRrs9mondqNQ2tsR5o6 jktA== X-Gm-Message-State: ALQs6tBcR8CrXDO8KLPmVCvQyVgh9z8fqKSCWgNrMlNRgbuwkR+efcgz I17RU/XCT2feHWpZD86To4yvpyRHOKuzhcsq3/LB99PJiyOLyL1NwX8ZBf2py7i9kjnIek/iTW5 BjpwzwqF6/ogRACnVPwXWn5diq/ZBzqJe3smklMwH8uNRbmWu/L9WfjyXd/HRYPmlrQ== X-Received: by 2002:ac8:836:: with SMTP id u51-v6mr20787266qth.407.1525369645587; Thu, 03 May 2018 10:47:25 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpbAtUpKrsOcuKHSYygq6A9Zc35HbTF97gnTD2H+Fq6tuO6uxlCZH/E7lgt/IJ7ZhFVG5uK X-Received: by 2002:ac8:836:: with SMTP id u51-v6mr20787218qth.407.1525369644594; Thu, 03 May 2018 10:47:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525369644; cv=none; d=google.com; s=arc-20160816; b=DO0qOp+Lc6eycG55sejVzDwhzUq2IJSXL03K147jAY2a2LOGuYeJoGwYtOIOqGvkZM ojcGmTyQUNskyUNWJ1K3cCvt9pM+PAzOTvcnrWi5BXbiMJxsnyhzBx2sVsLQbB5tnlKO noJBelZuAD/SKGg8bhNaecLU93OVd6OzUaB3u9rY1ZMd3eMoof/tEWBeskbX/oMUgqNk H1vOpyDYwFPrQ7MGIvBn0T4proUz47T+4O6+NGNCfJnGi1CsAdcCoRoduhPVHWYtjxrP 0NIs6VL2/ZPK3lt5Ncov2Ud8u3A5JI2+JRHtg86dfaVTbYlfGhJ1Qq978DCawMVXwAlR y9SQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:content-disposition:mime-version:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=O0zRygE7IMq1Zf6gAz4DxL4z9STb4+s1ZOoQ7R8B24I=; b=xn2ZEX11mrFLUsYU2yrFWtwdPLT+0iYFelsE/Q4G6R8kmPkupyD0VCiEsAYRWYLA2q YIm7s6JFpBjDrytbHivAquhx2L5nucdHa5kf8fTbzwn6desNAsecR766QJyTsPc9P2oz nCxCacs+IOFi8cjwcUwD0nN9Z8tvVjwlMvd1Pevnshwb4FOhRUv38PyqaLAm15CVDUaf GU2eq45FlVfLk3BClqLrwIY1Ej4rsJYN7iy6FKomp2sHmotDneTcr65+UwpqCOBifp6+ 89DZp1QXBni9Nrzje5F78++64U6qxd84O3SBAZLr6hIL2AuGIjZU0QIoIhRzZU7vKTHc i4Ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2017-10-26 header.b=IWhub1gq; spf=pass (google.com: domain of darrick.wong@oracle.com designates 141.146.126.79 as permitted sender) smtp.mailfrom=darrick.wong@oracle.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from aserp2130.oracle.com (aserp2130.oracle.com. [141.146.126.79]) by mx.google.com with ESMTPS id i13-v6si2347250qvk.231.2018.05.03.10.47.23 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 03 May 2018 10:47:24 -0700 (PDT) Received-SPF: pass (google.com: domain of darrick.wong@oracle.com designates 141.146.126.79 as permitted sender) client-ip=141.146.126.79; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2017-10-26 header.b=IWhub1gq; spf=pass (google.com: domain of darrick.wong@oracle.com designates 141.146.126.79 as permitted sender) smtp.mailfrom=darrick.wong@oracle.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from pps.filterd (aserp2130.oracle.com [127.0.0.1]) by aserp2130.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w43HjrXS149023; Thu, 3 May 2018 17:47:04 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : mime-version : content-type; s=corp-2017-10-26; bh=O0zRygE7IMq1Zf6gAz4DxL4z9STb4+s1ZOoQ7R8B24I=; b=IWhub1gqjfLqOkY6+QOZmlx2ir6l/EWVTWrGr9DzuAaHm/hR5CC52GwHnqSMUQiHnfVL 3F+Klytxazjtp3x0aW8UROpS+r98gPDJrvJP3Ab1pmRAdhU8EA7i8wk7TaZZ+03Qr/jW 837Nx6qej4SI0X+zElR3CkQHN8nRIIlJl1E2bACQMfMiCLNY8bd+8CX0wGZtQArOTGyo NhIPdjOZYWJYzsRLA95s4IuFJlXq0/M0GBUcPFHyYxFBJhuX/fOcydfQV2MCmU0OOGgy rMfdyj8AV3xcP7r9KN96xk11VT7ImRfmzdNOkeClOqTvdoe3mnyOVs/9IZe6PCrvHyvY WA== Received: from userv0021.oracle.com (userv0021.oracle.com [156.151.31.71]) by aserp2130.oracle.com with ESMTP id 2hmeg63298-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 03 May 2018 17:47:04 +0000 Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by userv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w43Hl2O4030833 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 3 May 2018 17:47:02 GMT Received: from abhmp0018.oracle.com (abhmp0018.oracle.com [141.146.116.24]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id w43Hl1iZ016439; Thu, 3 May 2018 17:47:01 GMT Received: from localhost (/67.169.218.210) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 03 May 2018 10:47:01 -0700 Date: Thu, 3 May 2018 10:46:59 -0700 From: "Darrick J. Wong" To: xfs , linux-fsdevel , linux-mm@kvack.org Cc: hch@infradead.org, cyberax@amazon.com, jack@suse.cz, osandov@osandov.com, Eryu Guan Subject: [PATCH v3 1/2] iomap: add a swapfile activation function Message-ID: <20180503174659.GD4127@magnolia> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8882 signatures=668698 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1711220000 definitions=main-1805030153 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: X-Virus-Scanned: ClamAV using ClamSMTP From: Darrick J. Wong Add a new iomap_swapfile_activate function so that filesystems can activate swap files without having to use the obsolete and slow bmap function. This enables XFS to support fallocate'd swap files and swap files on realtime devices. Signed-off-by: Darrick J. Wong --- v3: catch null iomap addr, fix too-short extent detection v2: document the swap file layout requirements, combine adjacent real/unwritten extents, align reported swap extents to physical page size boundaries, fix compiler errors when swap disabled --- fs/iomap.c | 162 +++++++++++++++++++++++++++++++++++++++++++++++++ fs/xfs/xfs_aops.c | 12 ++++ include/linux/iomap.h | 11 +++ 3 files changed, 185 insertions(+) diff --git a/fs/iomap.c b/fs/iomap.c index afd163586aa0..ac7115492366 100644 --- a/fs/iomap.c +++ b/fs/iomap.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "internal.h" @@ -1089,3 +1090,164 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, return ret; } EXPORT_SYMBOL_GPL(iomap_dio_rw); + +/* Swapfile activation */ + +#ifdef CONFIG_SWAP +struct iomap_swapfile_info { + struct iomap iomap; /* accumulated iomap */ + struct swap_info_struct *sis; + uint64_t lowest_ppage; /* lowest physical addr seen (pages) */ + uint64_t highest_ppage; /* highest physical addr seen (pages) */ + unsigned long nr_pages; /* number of pages collected */ + int nr_extents; /* extent count */ +}; + +/* + * Collect physical extents for this swap file. Physical extents reported to + * the swap code must be trimmed to align to a page boundary. The logical + * offset within the file is irrelevant since the swapfile code maps logical + * page numbers of the swap device to the physical page-aligned extents. + */ +static int iomap_swapfile_add_extent(struct iomap_swapfile_info *isi) +{ + struct iomap *iomap = &isi->iomap; + unsigned long nr_pages; + uint64_t first_ppage; + uint64_t first_ppage_reported; + uint64_t last_ppage; + int error; + + /* + * Round the start up and the end down so that the physical + * extent aligns to a page boundary. + */ + first_ppage = ALIGN(iomap->addr, PAGE_SIZE) >> PAGE_SHIFT; + last_ppage = (ALIGN_DOWN(iomap->addr + iomap->length, PAGE_SIZE) >> + PAGE_SHIFT) - 1; + + /* Skip too-short physical extents. */ + if (first_ppage > last_ppage) + return 0; + nr_pages = last_ppage - first_ppage + 1; + + /* + * Calculate how much swap space we're adding; the first page contains + * the swap header and doesn't count. The mm still wants that first + * page fed to add_swap_extent, however. + */ + first_ppage_reported = first_ppage; + if (iomap->offset == 0) + first_ppage_reported++; + if (isi->lowest_ppage > first_ppage_reported) + isi->lowest_ppage = first_ppage_reported; + if (isi->highest_ppage < last_ppage) + isi->highest_ppage = last_ppage; + + /* Add extent, set up for the next call. */ + error = add_swap_extent(isi->sis, isi->nr_pages, nr_pages, first_ppage); + if (error < 0) + return error; + isi->nr_extents += error; + isi->nr_pages += nr_pages; + return 0; +} + +/* + * Accumulate iomaps for this swap file. We have to accumulate iomaps because + * swap only cares about contiguous page-aligned physical extents and makes no + * distinction between written and unwritten extents. + */ +static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos, + loff_t count, void *data, struct iomap *iomap) +{ + struct iomap_swapfile_info *isi = data; + int error; + + /* Skip holes. */ + if (iomap->type == IOMAP_HOLE) + goto out; + + /* Only one bdev per swap file. */ + if (iomap->bdev != isi->sis->bdev) + goto err; + + /* Only real or unwritten extents. */ + if (iomap->type != IOMAP_MAPPED && iomap->type != IOMAP_UNWRITTEN) + goto err; + + /* No uncommitted metadata or shared blocks or inline data. */ + if (iomap->flags & (IOMAP_F_DIRTY | IOMAP_F_SHARED | + IOMAP_F_DATA_INLINE)) + goto err; + + /* No null physical addresses. */ + if (iomap->addr == IOMAP_NULL_ADDR) + goto err; + + if (isi->iomap.length == 0) { + /* No accumulated extent, so just store it. */ + memcpy(&isi->iomap, iomap, sizeof(isi->iomap)); + } else if (isi->iomap.addr + isi->iomap.length == iomap->addr) { + /* Append this to the accumulated extent. */ + isi->iomap.length += iomap->length; + } else { + /* Otherwise, add the retained iomap and store this one. */ + error = iomap_swapfile_add_extent(isi); + if (error) + return error; + memcpy(&isi->iomap, iomap, sizeof(isi->iomap)); + } +out: + return count; +err: + pr_err("swapon: file cannot be used for swap\n"); + return -EINVAL; +} + +/* + * Iterate a swap file's iomaps to construct physical extents that can be + * passed to the swapfile subsystem. + */ +int iomap_swapfile_activate(struct swap_info_struct *sis, + struct file *swap_file, sector_t *pagespan, + const struct iomap_ops *ops) +{ + struct iomap_swapfile_info isi = { + .sis = sis, + .lowest_ppage = (sector_t)-1ULL, + }; + struct address_space *mapping = swap_file->f_mapping; + struct inode *inode = mapping->host; + loff_t pos = 0; + loff_t len = ALIGN_DOWN(i_size_read(inode), PAGE_SIZE); + loff_t ret; + + ret = filemap_write_and_wait(inode->i_mapping); + if (ret) + return ret; + + while (len > 0) { + ret = iomap_apply(inode, pos, len, IOMAP_REPORT, + ops, &isi, iomap_swapfile_activate_actor); + if (ret <= 0) + return ret; + + pos += ret; + len -= ret; + } + + if (isi.iomap.length) { + ret = iomap_swapfile_add_extent(&isi); + if (ret) + return ret; + } + + *pagespan = 1 + isi.highest_ppage - isi.lowest_ppage; + sis->max = isi.nr_pages; + sis->pages = isi.nr_pages - 1; + sis->highest_bit = isi.nr_pages - 1; + return isi.nr_extents; +} +EXPORT_SYMBOL_GPL(iomap_swapfile_activate); +#endif /* CONFIG_SWAP */ diff --git a/fs/xfs/xfs_aops.c b/fs/xfs/xfs_aops.c index 0ab824f574ed..80de476cecf8 100644 --- a/fs/xfs/xfs_aops.c +++ b/fs/xfs/xfs_aops.c @@ -1475,6 +1475,16 @@ xfs_vm_set_page_dirty( return newly_dirty; } +static int +xfs_iomap_swapfile_activate( + struct swap_info_struct *sis, + struct file *swap_file, + sector_t *span) +{ + sis->bdev = xfs_find_bdev_for_inode(file_inode(swap_file)); + return iomap_swapfile_activate(sis, swap_file, span, &xfs_iomap_ops); +} + const struct address_space_operations xfs_address_space_operations = { .readpage = xfs_vm_readpage, .readpages = xfs_vm_readpages, @@ -1488,6 +1498,7 @@ const struct address_space_operations xfs_address_space_operations = { .migratepage = buffer_migrate_page, .is_partially_uptodate = block_is_partially_uptodate, .error_remove_page = generic_error_remove_page, + .swap_activate = xfs_iomap_swapfile_activate, }; const struct address_space_operations xfs_dax_aops = { @@ -1495,4 +1506,5 @@ const struct address_space_operations xfs_dax_aops = { .direct_IO = noop_direct_IO, .set_page_dirty = noop_set_page_dirty, .invalidatepage = noop_invalidatepage, + .swap_activate = xfs_iomap_swapfile_activate, }; diff --git a/include/linux/iomap.h b/include/linux/iomap.h index 19a07de28212..4bd87294219a 100644 --- a/include/linux/iomap.h +++ b/include/linux/iomap.h @@ -106,4 +106,15 @@ typedef int (iomap_dio_end_io_t)(struct kiocb *iocb, ssize_t ret, ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, const struct iomap_ops *ops, iomap_dio_end_io_t end_io); +#ifdef CONFIG_SWAP +struct file; +struct swap_info_struct; + +int iomap_swapfile_activate(struct swap_info_struct *sis, + struct file *swap_file, sector_t *pagespan, + const struct iomap_ops *ops); +#else +# define iomap_swapfile_activate(sis, swapfile, pagespan, ops) (-EIO) +#endif /* CONFIG_SWAP */ + #endif /* LINUX_IOMAP_H */