From nobody Tue Oct 7 21:39:56 2025 Received: from mx9.didiglobal.com (mx9.didiglobal.com [111.202.70.124]) by smtp.subspace.kernel.org (Postfix) with SMTP id E2E79249F9; Mon, 7 Jul 2025 07:00:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=111.202.70.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751871648; cv=none; b=CyYzjRoEqurUdwsR13feXvSNZ77mXqfphp2DQa4FZI7/blInMU5l0LpgaPWLkYsc5emJJh2G6choj7S9AwZuDwM22kpYFBJIN0NV+AKGqW71oybPWR6KjJsY0xV624nGE65Q8AqBM9ce+mXnVj3PKS+y8pe1yiJX9VDp+wEOzJI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751871648; c=relaxed/simple; bh=2xP5WfN4XUwSSHvH10cIGlaPoQTzYl8jSpkNxauZyN8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:Content-Type: MIME-Version; b=Zhech13lasNLU7YNrhqpAeqOi85+BRrDkIvpYWUxzihmVaIYVKG5PfxFpe++XCcSik/QHpferAtqPkADf/6ovBjf7ZPsUlwEnuQtnkaErp1Ny0+OmJWMu7mREc0bqTE5fH9FCzaFieq/MXDBacQX0VkSiz4wiqBY+LCetT04Pew= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=didiglobal.com; spf=pass smtp.mailfrom=didiglobal.com; dkim=pass (1024-bit key) header.d=didiglobal.com header.i=@didiglobal.com header.b=h/NjmpQJ; arc=none smtp.client-ip=111.202.70.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=didiglobal.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=didiglobal.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=didiglobal.com header.i=@didiglobal.com header.b="h/NjmpQJ" Received: from mail.didiglobal.com (unknown [10.79.65.20]) by mx9.didiglobal.com (MailData Gateway V2.8.8) with ESMTPS id 4781C196C52384; Mon, 7 Jul 2025 14:56:27 +0800 (CST) Received: from BJ01-ACTMBX-09.didichuxing.com (10.79.64.19) by BJ02-ACTMBX-02.didichuxing.com (10.79.65.20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1748.10; Mon, 7 Jul 2025 15:00:30 +0800 Received: from BJ03-ACTMBX-07.didichuxing.com (10.79.71.34) by BJ01-ACTMBX-09.didichuxing.com (10.79.64.19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1748.10; Mon, 7 Jul 2025 15:00:30 +0800 Received: from BJ03-ACTMBX-07.didichuxing.com ([fe80::2e1a:dd47:6d25:287e]) by BJ03-ACTMBX-07.didichuxing.com ([fe80::2e1a:dd47:6d25:287e%7]) with mapi id 15.02.1748.010; Mon, 7 Jul 2025 15:00:30 +0800 X-MD-Sfrom: chentaotao@didiglobal.com X-MD-SrcIP: 10.79.65.20 From: =?gb2312?B?s8LMzszOIFRhb3RhbyBDaGVu?= To: "tytso@mit.edu" , "hch@infradead.org" , "adilger.kernel@dilger.ca" , "willy@infradead.org" , "brauner@kernel.org" , "jani.nikula@linux.intel.com" , "rodrigo.vivi@intel.com" , "tursulin@ursulin.net" , "airlied@gmail.com" CC: "linux-fsdevel@vger.kernel.org" , "linux-ext4@vger.kernel.org" , "linux-block@vger.kernel.org" , "intel-gfx@lists.freedesktop.org" , "dri-devel@lists.freedesktop.org" , "linux-mm@kvack.org" , "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "chentao325@qq.com" , "frank.li@vivo.com" , =?gb2312?B?s8LMzszOIFRhb3RhbyBDaGVu?= Subject: [PATCH v4 2/5] drm/i915: Refactor shmem_pwrite() to use kiocb and write_iter Thread-Topic: [PATCH v4 2/5] drm/i915: Refactor shmem_pwrite() to use kiocb and write_iter Thread-Index: AQHb7wzTZeitC7sAYUmZCP91w6B2Nw== Date: Mon, 7 Jul 2025 07:00:30 +0000 Message-ID: <20250707070023.206725-3-chentaotao@didiglobal.com> In-Reply-To: <20250707070023.206725-1-chentaotao@didiglobal.com> Accept-Language: zh-CN, en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=didiglobal.com; s=2025; t=1751871390; bh=2xP5WfN4XUwSSHvH10cIGlaPoQTzYl8jSpkNxauZyN8=; h=From:To:CC:Subject:Date:Message-ID:Content-Type; b=h/NjmpQJE58utzSH/XV/sPoYuYIBUTwpoJmFTsbAzXOR3BnGkvjRGAUpPwM8luNH1 OlZHMOs/BkqwsmX7Y0yQL1Xu/tb7DUsyWSRCEj50LbzOEhxKKNbdZ1lO5GlAhQM5qt FXrRgYZXOfDF/DsgXvKXHN0tkKbke+QPK4uBbv3g= Content-Type: text/plain; charset="utf-8" From: Taotao Chen Refactors shmem_pwrite() to replace the ->write_begin/end logic with a write_iter-based implementation using kiocb and iov_iter. While kernel_write() was considered, it caused about 50% performance regression. vfs_write() is not exported for kernel use. Therefore, file->f_op->write_iter() is called directly with a synchronously initialized kiocb to preserve performance and remove write_begin usage. Performance results use gem_pwrite on Intel CPU i7-10700 (average of 10 runs): - ./gem_pwrite --run-subtest bench -s 16384 Before: 0.205s, After: 0.214s - ./gem_pwrite --run-subtest bench -s 524288 Before: 6.1021s, After: 4.8047s Part of a series refactoring address_space_operations write_begin and write_end callbacks to use struct kiocb for passing write context and flags. Signed-off-by: Taotao Chen --- drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 81 ++++++----------------- 1 file changed, 21 insertions(+), 60 deletions(-) diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c b/drivers/gpu/drm/i9= 15/gem/i915_gem_shmem.c index 1e8f66ac48ca..43b42be7ca2a 100644 --- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c +++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c @@ -400,12 +400,12 @@ static int shmem_pwrite(struct drm_i915_gem_object *obj, const struct drm_i915_gem_pwrite *arg) { - struct address_space *mapping =3D obj->base.filp->f_mapping; - const struct address_space_operations *aops =3D mapping->a_ops; char __user *user_data =3D u64_to_user_ptr(arg->data_ptr); - u64 remain; - loff_t pos; - unsigned int pg; + struct file *file =3D obj->base.filp; + struct kiocb kiocb; + struct iov_iter iter; + ssize_t written; + u64 size =3D arg->size; =20 /* Caller already validated user args */ GEM_BUG_ON(!access_ok(user_data, arg->size)); @@ -428,63 +428,24 @@ shmem_pwrite(struct drm_i915_gem_object *obj, if (obj->mm.madv !=3D I915_MADV_WILLNEED) return -EFAULT; =20 - /* - * Before the pages are instantiated the object is treated as being - * in the CPU domain. The pages will be clflushed as required before - * use, and we can freely write into the pages directly. If userspace - * races pwrite with any other operation; corruption will ensue - - * that is userspace's prerogative! - */ + if (size > MAX_RW_COUNT) + return -EFBIG; =20 - remain =3D arg->size; - pos =3D arg->offset; - pg =3D offset_in_page(pos); + if (!file->f_op->write_iter) + return -EINVAL; =20 - do { - unsigned int len, unwritten; - struct folio *folio; - void *data, *vaddr; - int err; - char __maybe_unused c; - - len =3D PAGE_SIZE - pg; - if (len > remain) - len =3D remain; - - /* Prefault the user page to reduce potential recursion */ - err =3D __get_user(c, user_data); - if (err) - return err; - - err =3D __get_user(c, user_data + len - 1); - if (err) - return err; - - err =3D aops->write_begin(obj->base.filp, mapping, pos, len, - &folio, &data); - if (err < 0) - return err; - - vaddr =3D kmap_local_folio(folio, offset_in_folio(folio, pos)); - pagefault_disable(); - unwritten =3D __copy_from_user_inatomic(vaddr, user_data, len); - pagefault_enable(); - kunmap_local(vaddr); - - err =3D aops->write_end(obj->base.filp, mapping, pos, len, - len - unwritten, folio, data); - if (err < 0) - return err; - - /* We don't handle -EFAULT, leave it to the caller to check */ - if (unwritten) - return -ENODEV; - - remain -=3D len; - user_data +=3D len; - pos +=3D len; - pg =3D 0; - } while (remain); + init_sync_kiocb(&kiocb, file); + kiocb.ki_pos =3D arg->offset; + iov_iter_ubuf(&iter, ITER_SOURCE, (void __user *)user_data, size); + + written =3D file->f_op->write_iter(&kiocb, &iter); + BUG_ON(written =3D=3D -EIOCBQUEUED); + + if (written !=3D size) + return -EIO; + + if (written < 0) + return written; =20 return 0; } --=20 2.34.1