From patchwork Thu Nov 7 16:34:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Wajdeczko X-Patchwork-Id: 13866725 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ADD7221219F; Thu, 7 Nov 2024 16:35:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730997309; cv=none; b=hwcXr/Q5otnX/jnIM4yFZVlgpgdHbavHO0nrnB1Sm2nHnmif7qkHv1CuLB5g+XnbDEKf7OLWkk7yGt+/RY6KSDxp11JSahBrpbybLdtbLqkzyV1WNEnzKzVe+F/ZL5s8o7uFspPzOXIj8uuj1uUH1Bien0qgEntGFem4sjXmlfk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730997309; c=relaxed/simple; bh=dmPkADiBavZ9q8H4vqaymnrDdnCSO5K6p28HUKcBoQg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mM8O8nAGHlCE635twCXy+F/q6rdspZi24jaMIkC3OpCY9GMQslwq0/+XCMR5iDGc4Sjc+YWvsY/v9ofjwl5dR+nmWQkCu9vsS2fviv0fdUbhJ2LgElaOc1mTaGqTtsDsf3Tt3xKOdRatWVdwGuAEyUDF1GBc+buXERaUMCClsBg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Sh+yWnAN; arc=none smtp.client-ip=192.198.163.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Sh+yWnAN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730997307; x=1762533307; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=dmPkADiBavZ9q8H4vqaymnrDdnCSO5K6p28HUKcBoQg=; b=Sh+yWnANUNSezt06L9VumPaRYbk016MexMjL5hWeFFYXcyfTcfIvjIUC MaZVSqoK7HchpO1eu6Da078DhLy3sNS/Hm4F0pSreCNcnO0pdTjVvOerj Z/40lJ61gAImWb/b4/KQhT9NIXGf0dNqNo59p1EkrVyipXNXIjItFDw/8 zFuueRhJgQR93gTYzmWzsEK8Ac+ZBpvduZR5x5Ms9/mEqvI3voqGiqAKO iTv9+xy5Odye1lG21dFm2/OMWgfUn9kE7mTGkVK+5jeUAMXSBB2iSvcph SeOwXZksvQ2xbsyCC4Lafs6SIpCo9CJS5ugarXo63oYHSfW19FeXMyTUn Q==; X-CSE-ConnectionGUID: rFJUb9b9SpCb0oxCi8MEhQ== X-CSE-MsgGUID: yQGtWUbuT66C5hjUQZNuWw== X-IronPort-AV: E=McAfee;i="6700,10204,11249"; a="18480131" X-IronPort-AV: E=Sophos;i="6.12,135,1728975600"; d="scan'208";a="18480131" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2024 08:35:07 -0800 X-CSE-ConnectionGUID: O6fhHQF1RVGaJWWucZdTJg== X-CSE-MsgGUID: 5TIc9iALTBuLr09DPBb7KQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,135,1728975600"; d="scan'208";a="89797004" Received: from mwajdecz-mobl.ger.corp.intel.com ([10.246.2.138]) by fmviesa004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2024 08:35:05 -0800 From: Michal Wajdeczko To: intel-xe@lists.freedesktop.org Cc: Michal Wajdeczko , Alexander Viro , Andrew Morton , Matthew Wilcox , Rodrigo Vivi , Matthew Brost , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH 1/4] iov_iter: Provide copy_iomem_to|from_iter() Date: Thu, 7 Nov 2024 17:34:45 +0100 Message-Id: <20241107163448.2123-2-michal.wajdeczko@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20241107163448.2123-1-michal.wajdeczko@intel.com> References: <20241107163448.2123-1-michal.wajdeczko@intel.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Define simple copy helpers that work on I/O memory. This will allow reuse of existing framework functions in new use cases. Signed-off-by: Michal Wajdeczko --- Cc: Alexander Viro Cc: Andrew Morton Cc: Matthew Wilcox Cc: Rodrigo Vivi Cc: Matthew Brost Cc: linux-kernel@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org --- include/linux/uio.h | 4 ++++ lib/iov_iter.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/include/linux/uio.h b/include/linux/uio.h index 853f9de5aa05..6d2a24293bd1 100644 --- a/include/linux/uio.h +++ b/include/linux/uio.h @@ -178,6 +178,10 @@ size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, struct iov_iter *i); size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, struct iov_iter *i); +size_t copy_iomem_to_iter(const void __iomem *from, size_t offset, + size_t bytes, struct iov_iter *i); +size_t copy_iomem_from_iter(void __iomem *to, size_t offset, + size_t bytes, struct iov_iter *i); size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i); size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i); diff --git a/lib/iov_iter.c b/lib/iov_iter.c index 908e75a28d90..e8c1f1c68716 100644 --- a/lib/iov_iter.c +++ b/lib/iov_iter.c @@ -435,6 +435,48 @@ size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, } EXPORT_SYMBOL(copy_page_from_iter); +size_t copy_iomem_to_iter(const void __iomem *from, size_t offset, + size_t bytes, struct iov_iter *i) +{ + unsigned char buf[SMP_CACHE_BYTES]; + size_t progress = 0, copied, len; + + from += offset; + while (bytes) { + len = min(bytes, sizeof(buf)); + memcpy_fromio(buf, from + progress, len); + copied = _copy_to_iter(buf, len, i); + if (!copied) + break; + bytes -= copied; + progress += copied; + } + + return progress; +} +EXPORT_SYMBOL(copy_iomem_to_iter); + +size_t copy_iomem_from_iter(void __iomem *to, size_t offset, + size_t bytes, struct iov_iter *i) +{ + unsigned char buf[SMP_CACHE_BYTES]; + size_t progress = 0, copied, len; + + to += offset; + while (bytes) { + len = min(bytes, sizeof(buf)); + copied = _copy_from_iter(buf, len, i); + if (!copied) + break; + memcpy_toio(to + progress, buf, copied); + bytes -= copied; + progress += copied; + } + + return progress; +} +EXPORT_SYMBOL(copy_iomem_from_iter); + static __always_inline size_t zero_to_user_iter(void __user *iter_to, size_t progress, size_t len, void *priv, void *priv2) From patchwork Thu Nov 7 16:34:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Wajdeczko X-Patchwork-Id: 13866726 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C5F10212D22; Thu, 7 Nov 2024 16:35:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730997312; cv=none; b=FQ9Xx+rPVGRggPibceqJ9OsRSpIqO1AP2MeO8P60ObNDG/67oSVUH0ZPLcn9bwdxuCpgNWovEx3RfA+JZWOT/oOWhkYgNeJX1E3/djGxUtwojAijuKi/lIbBc9+KS+y5GLLYeeqm00cs1gJr1nJ8xvRJJS9jMbh7gXViK8CNXiQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730997312; c=relaxed/simple; bh=4DveGDMZgs7lCoOyvHyj8q8j4M1x/bD6dp6XNYVbKTM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QYHvHJD3WCcACCLNVGhDfWdC8ZSZ42k+OVIDDggoG0Dy8Ne2gtYDDKIBbdsAuwFGkpExr10OnzmNrGk1UMrmyYHLBLDT0KwW9ifx0Db+74Ufij2R/3plDSLeJfEeykv1b6ggvYakDaXii/6EIjpI+G76fHW2N0EdHHAc3fW8nCg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=KNgqU/Nu; arc=none smtp.client-ip=192.198.163.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="KNgqU/Nu" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730997310; x=1762533310; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4DveGDMZgs7lCoOyvHyj8q8j4M1x/bD6dp6XNYVbKTM=; b=KNgqU/NuMq8Kg73Sig6K+Bd9yCJPcFMyM0EPmBseU6dPJvhobdwC2uLb 0glzEoe6p3t2/M5zTODg4ftHgExuDRwyOTVGc/Yk4F/FzjT0lxwcoqFIh qAmow77DUvrHKWz3cSDfvTBIQtW2ijOhosbqDU+hNASaNTuEYx/nqqM5k hHOH3xWsyBMH9HwZ/xOG6I8lhgmIREP8sHvZQ9itk9Ux5eQ0XK7BB8A4x am+3rlTIb26hFGYG8jq2xe2YaHfzL5jqsqjfzUFqbF8CmZfi+6pwCc5AW PUKf3ZNiu95HJX8nOBIR51Gntm6m3twAOBMpgXwaaTCKwheylawHdjdSk A==; X-CSE-ConnectionGUID: jsBBQyIxSL+KEqnJDZ26Xg== X-CSE-MsgGUID: DYjrkIXkTDSJnYFCzX7d+w== X-IronPort-AV: E=McAfee;i="6700,10204,11249"; a="18480139" X-IronPort-AV: E=Sophos;i="6.12,135,1728975600"; d="scan'208";a="18480139" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2024 08:35:10 -0800 X-CSE-ConnectionGUID: doG/WlBBQIChIKsPv8x1Nw== X-CSE-MsgGUID: h0TCrU8AQDOPX/IPx066NQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,135,1728975600"; d="scan'208";a="89797021" Received: from mwajdecz-mobl.ger.corp.intel.com ([10.246.2.138]) by fmviesa004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2024 08:35:07 -0800 From: Michal Wajdeczko To: intel-xe@lists.freedesktop.org Cc: Michal Wajdeczko , Alexander Viro , Christian Brauner , Jan Kara , Rodrigo Vivi , Matthew Brost , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/4] libfs: Provide simple_read_from|write_to_iomem() Date: Thu, 7 Nov 2024 17:34:46 +0100 Message-Id: <20241107163448.2123-3-michal.wajdeczko@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20241107163448.2123-1-michal.wajdeczko@intel.com> References: <20241107163448.2123-1-michal.wajdeczko@intel.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 New functions are similar to simple_read_from|write_to_buffer() but work on the I/O memory instead. Will allow wider code reuse. Signed-off-by: Michal Wajdeczko --- Cc: Alexander Viro Cc: Christian Brauner Cc: Jan Kara Cc: Rodrigo Vivi Cc: Matthew Brost Cc: linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- fs/libfs.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 5 +++ 2 files changed, 83 insertions(+) diff --git a/fs/libfs.c b/fs/libfs.c index 46966fd8bcf9..0e4f0d50d4f3 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -1095,6 +1095,84 @@ void simple_release_fs(struct vfsmount **mount, int *count) } EXPORT_SYMBOL(simple_release_fs); +/** + * simple_read_from_iomem() - copy data from the I/O memory to user space + * @to: the user space buffer to read to + * @count: the maximum number of bytes to read + * @ppos: the current position in the buffer + * @from: the I/O memory to read from + * @available: the size of the iomem memory + * + * The simple_read_from_iomem() function reads up to @count bytes from the + * I/O memory @from at offset @ppos into the user space address starting at @to. + * + * Return: On success, the number of bytes read is returned and the offset + * @ppos is advanced by this number, or negative value is returned on error. + */ +ssize_t simple_read_from_iomem(void __user *to, size_t count, loff_t *ppos, + const void __iomem *from, size_t available) +{ + struct iov_iter iter; + loff_t pos = *ppos; + size_t copied; + + if (pos < 0) + return -EINVAL; + if (pos >= available || !count) + return 0; + if (count > available - pos) + count = available - pos; + if (import_ubuf(ITER_DEST, to, count, &iter)) + return -EFAULT; + + copied = copy_iomem_to_iter(from, pos, count, &iter); + if (!copied) + return -EFAULT; + + *ppos = pos + copied; + return copied; +} +EXPORT_SYMBOL(simple_read_from_iomem); + +/** + * simple_write_to_iomem() - copy data from user space to the I/O memory + * @to: the I/O memory to write to + * @available: the size of the I/O memory + * @ppos: the current position in the buffer + * @from: the user space buffer to read from + * @count: the maximum number of bytes to read + * + * The simple_write_to_iomem() function reads up to @count bytes from the user + * space address starting at @from into the I/O memory @to at offset @ppos. + * + * Return: On success, the number of bytes written is returned and the offset + * @ppos is advanced by this number, or negative value is returned on error. + */ +ssize_t simple_write_to_iomem(void __iomem *to, size_t available, loff_t *ppos, + const void __user *from, size_t count) +{ + struct iov_iter iter; + loff_t pos = *ppos; + size_t copied; + + if (pos < 0) + return -EINVAL; + if (pos >= available || !count) + return 0; + if (count > available - pos) + count = available - pos; + if (import_ubuf(ITER_SOURCE, (void __user *)from, count, &iter)) + return -EFAULT; + + copied = copy_iomem_from_iter(to, pos, count, &iter); + if (!copied) + return -EFAULT; + + *ppos = pos + copied; + return copied; +} +EXPORT_SYMBOL(simple_write_to_iomem); + /** * simple_read_from_buffer - copy data from the buffer to user space * @to: the user space buffer to read to diff --git a/include/linux/fs.h b/include/linux/fs.h index 3559446279c1..2cc73c5961b0 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3426,6 +3426,11 @@ extern ssize_t simple_read_from_buffer(void __user *to, size_t count, extern ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos, const void __user *from, size_t count); +ssize_t simple_read_from_iomem(void __user *to, size_t count, loff_t *ppos, + const void __iomem *from, size_t available); +ssize_t simple_write_to_iomem(void __iomem *to, size_t available, loff_t *ppos, + const void __user *from, size_t count); + struct offset_ctx { struct maple_tree mt; unsigned long next_offset;