From nobody Fri Dec 19 00:18:06 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 8B0B41F12FA for ; Thu, 10 Apr 2025 03:57:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744257458; cv=none; b=E3L+xhOVn/20eXhIlHvgVCN7CsqTyCxYE1eT1DuRAOjo7dLdx7ATqezj7b+ALjXCKdPP3Jn8qQ/riFLwp7RzsXoRX5+C4mLJjaR8F+DWgOWSgU3vwVUlingf2cJrpe1FW2kts++8hJXCPg8rTVsF3zwKFkjkPn/1FL4MMuaVa34= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744257458; c=relaxed/simple; bh=xhN4itx0LE9yqkE1o0cdQbCMJsNrbt92KK3WKMDRZsM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-type; b=C3DXX26tpRsxVMDxoaB7DeZzkOGELlMBcsYzVY8gN+JQaqKqtlUNr0fnCwWnYSYoRqjH1mBk7lJomWo+SfqgN2aqDyQDFYJGpW/zHZ+nQ1AbHagcY7m0wwwQSqA2KpdNOqRhJ6F6BTRl40Stm8JoOrLjjtiYKABsjtngIwV8tGk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=fZkxp2W8; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="fZkxp2W8" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744257455; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lMkbdXy2zeXmVG+XN8Oyl1Hd6P5dnLAMb7gAr8RumIQ=; b=fZkxp2W8hVXLfZwyy0Sd35J/NQO7YN1qC/qCdciqHLSg4c3IoyGAGdL3uKQcEds6srRT5j PNoH/79XV0/Ejm9LwciNldFB5+BSjGABHKqzjjB63wOKRf2s/oPX+aaEZl74Acq99Tr0wq UxBzOjVHT1GbxjHd58Wd+a36eJvlfDE= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-38-9bda-oNtP0ed2q9iAoQTIA-1; Wed, 09 Apr 2025 23:57:32 -0400 X-MC-Unique: 9bda-oNtP0ed2q9iAoQTIA-1 X-Mimecast-MFC-AGG-ID: 9bda-oNtP0ed2q9iAoQTIA_1744257451 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id AF77519560B0; Thu, 10 Apr 2025 03:57:30 +0000 (UTC) Received: from MiWiFi-R3L-srv.redhat.com (unknown [10.72.112.38]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A03EF19560AD; Thu, 10 Apr 2025 03:57:26 +0000 (UTC) From: Baoquan He To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, david@redhat.com, osalvador@suse.de, yanjun.zhu@linux.dev, linux-kernel@vger.kernel.org, Baoquan He Subject: [PATCH v4 1/4] mm/gup: fix wrongly calculated returned value in fault_in_safe_writeable() Date: Thu, 10 Apr 2025 11:57:14 +0800 Message-ID: <20250410035717.473207-2-bhe@redhat.com> In-Reply-To: <20250410035717.473207-1-bhe@redhat.com> References: <20250410035717.473207-1-bhe@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" Not like fault_in_readable() or fault_in_writeable(), in fault_in_safe_writeable() local variable 'start' is increased page by page to loop till the whole address range is handled. However, it mistakenly calcalates the size of handled range with 'uaddr - start'. Fix it here. Signed-off-by: Baoquan He Fixes: fe673d3f5bf1 ("mm: gup: make fault_in_safe_writeable() use fixup_use= r_fault()") Acked-by: David Hildenbrand Reviewed-by: Oscar Salvador --- mm/gup.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 92351e2fa876..84461d384ae2 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2207,8 +2207,8 @@ size_t fault_in_safe_writeable(const char __user *uad= dr, size_t size) } while (start !=3D end); mmap_read_unlock(mm); =20 - if (size > (unsigned long)uaddr - start) - return size - ((unsigned long)uaddr - start); + if (size > start - (unsigned long)uaddr) + return size - (start - (unsigned long)uaddr); return 0; } EXPORT_SYMBOL(fault_in_safe_writeable); --=20 2.41.0 From nobody Fri Dec 19 00:18:06 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 CE8771F1932 for ; Thu, 10 Apr 2025 03:57:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744257461; cv=none; b=lqlw4cOGZotG2bFEAEcCMiuiCvSoBZTDew7mAZdpN9nwSrkOESBqajWcMoE+v/oTX7btC6cu9NyhFrAu6+x3D6BlbiKEC0VjUQJB3KxdFouME+S1sK7vk6yPu11SDApdPz8iv2LTv2dBB/L7zsQvPA+VbLvzuMdD1VzTmD8jWAs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744257461; c=relaxed/simple; bh=UM25YJxXAf3pkmBE/Q7zuAnTLPSileGOKfLykBX9hLc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-type; b=fGq8I6x4O8lquC9VG1ogddb5ZXbcLnJN13WLyrAywEN0NJUwkKaTXIe4hU15gEkWxkYlAlSJ6icGRSikbTmWDVHOpj6UqrEYa5PaLcWSmhsl0QZ8N/4AHfzfzilq1Q5eFjr5yqYH35YH261rFiYS/UAEcGZ80Ar6R3JRs6fc71A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=MoQEmZkY; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="MoQEmZkY" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744257458; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3pd14nYhYMgdjkQtwmpN+tA62d+FdLUKaQa7vusgjMQ=; b=MoQEmZkYxZA/cRffTKtCFgCN8yNtmYzH5Z8f2nfFoYJAnmmm3e+kBYNCKzlajpxRZgJRQv FhSpu8EtD+7QPI6Oys8ZJQX8rc9WmTp/uY50ALLb3fLVcUfEa4nLEb8B9UPm5FllHZ8F5e Ee1dLslfbc9D38xfx6SjNDEAfsnus0A= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-616-BPQvouKcNg2_QR0P7W8Big-1; Wed, 09 Apr 2025 23:57:36 -0400 X-MC-Unique: BPQvouKcNg2_QR0P7W8Big-1 X-Mimecast-MFC-AGG-ID: BPQvouKcNg2_QR0P7W8Big_1744257455 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8E72B1956050; Thu, 10 Apr 2025 03:57:35 +0000 (UTC) Received: from MiWiFi-R3L-srv.redhat.com (unknown [10.72.112.38]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A99A419560AD; Thu, 10 Apr 2025 03:57:31 +0000 (UTC) From: Baoquan He To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, david@redhat.com, osalvador@suse.de, yanjun.zhu@linux.dev, linux-kernel@vger.kernel.org, Baoquan He Subject: [PATCH v4 2/4] mm/gup: remove unneeded checking in follow_page_pte() Date: Thu, 10 Apr 2025 11:57:15 +0800 Message-ID: <20250410035717.473207-3-bhe@redhat.com> In-Reply-To: <20250410035717.473207-1-bhe@redhat.com> References: <20250410035717.473207-1-bhe@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" In __get_user_pages(), it will traverse page table and take a reference to the page the given user address corresponds to if GUP_GET or GUP_PIN is set. However, it's not supported both GUP_GET and GUP_PIN are set. Even though this check need be done, it should be done earlier, but not doing it till entering into follow_page_pte() and failed. Furthermore, this checking has been done in is_valid_gup_args() and all external users of __get_user_pages() will call is_valid_gup_args() to catch the illegal setting. We don't need to worry about internal users of __get_user_pages() because the gup_flags are set by MM code correctly. Here remove the checking in follow_page_pte(), and add VM_WARN_ON_ONCE() to catch the possible exceptional setting just in case. And also change the VM_BUG_ON to VM_WARN_ON_ONCE() for checking (!!pages !=3D !!(gup_flags & (FOLL_GET | FOLL_PIN))) because the checking has been done in is_valid_gup_args() for external users of __get_user_pages(). Signed-off-by: Baoquan He Reviewed-by: Oscar Salvador Acked-by: David Hildenbrand --- mm/gup.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 84461d384ae2..eb668da933e1 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -844,11 +844,6 @@ static struct page *follow_page_pte(struct vm_area_str= uct *vma, pte_t *ptep, pte; int ret; =20 - /* FOLL_GET and FOLL_PIN are mutually exclusive. */ - if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) =3D=3D - (FOLL_PIN | FOLL_GET))) - return ERR_PTR(-EINVAL); - ptep =3D pte_offset_map_lock(mm, pmd, address, &ptl); if (!ptep) return no_page_table(vma, flags, address); @@ -1432,7 +1427,11 @@ static long __get_user_pages(struct mm_struct *mm, =20 start =3D untagged_addr_remote(mm, start); =20 - VM_BUG_ON(!!pages !=3D !!(gup_flags & (FOLL_GET | FOLL_PIN))); + VM_WARN_ON_ONCE(!!pages !=3D !!(gup_flags & (FOLL_GET | FOLL_PIN))); + + /* FOLL_GET and FOLL_PIN are mutually exclusive. */ + VM_WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) =3D=3D + (FOLL_PIN | FOLL_GET)); =20 do { struct page *page; --=20 2.41.0 From nobody Fri Dec 19 00:18:06 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 0977B201031 for ; Thu, 10 Apr 2025 03:57:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744257470; cv=none; b=o4bmO8LGFX15Wc9OLZLMnndCvYxN/S9lmTYFMtSXHgM7LGqcIGmMM1tOUD0dvbfohYSTVf9dOY5N0Ogj7F/RiaInwSuHQMHEEz01WRtT0bxca9rehlPvsa/UIaBPvfjhsYNPWKW9FFwJskP9HgM8VgDOiT+RgOCbcxDDz/Vi5k4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744257470; c=relaxed/simple; bh=XbLhsbjg1pnvXCfaaBa7PJtCuV1G98Gervk3p8w/teg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-type; b=LNg9BH40ulBrnxh8iVEvqCcMv8aLDSQQ0gGrBZ5aqytmdM+Igp1TKe1WPNC8mhJed5FVRRbLPxJLKoUB09AxzQBbVWBkbnDFDJQaXKXpBLruNDmBoei9DcLxgIOA2cP7XZ+F6yAMy8t5KVu0eVzGvvQn7U95Xc/9O9b7TL4JpUI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Cipe7Qtl; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Cipe7Qtl" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744257465; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=f1N+YcIuKzs7QdLEHyEwZZ+GVIPuduBxUV08naMugGA=; b=Cipe7QtllYSJ2oHJFP6dX84AW1RpJ3q6/QxMo68ddXzh1ottmeDLOZm7fYQhuW8A9csr1G YFmoEV/4OWb08uza/EBrOxex1pVyxpSPiexcntBihx4gMk3KGSUIJHZhM4DVGmop8FykMQ V+QoBp/tOhIqEwxQ3GFcUvqWZcq1RgQ= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-122-lqz1GyQENkqkExtdQptSDQ-1; Wed, 09 Apr 2025 23:57:41 -0400 X-MC-Unique: lqz1GyQENkqkExtdQptSDQ-1 X-Mimecast-MFC-AGG-ID: lqz1GyQENkqkExtdQptSDQ_1744257460 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5455218004A9; Thu, 10 Apr 2025 03:57:40 +0000 (UTC) Received: from MiWiFi-R3L-srv.redhat.com (unknown [10.72.112.38]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7808219560AD; Thu, 10 Apr 2025 03:57:35 +0000 (UTC) From: Baoquan He To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, david@redhat.com, osalvador@suse.de, yanjun.zhu@linux.dev, linux-kernel@vger.kernel.org, Baoquan He Subject: [PATCH v4 3/4] mm/gup: remove gup_fast_pgd_leaf() and clean up the relevant codes Date: Thu, 10 Apr 2025 11:57:16 +0800 Message-ID: <20250410035717.473207-4-bhe@redhat.com> In-Reply-To: <20250410035717.473207-1-bhe@redhat.com> References: <20250410035717.473207-1-bhe@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" In the current kernel, only pud huge page is supported in some architectures. P4d and pgd huge pages haven't been supported yet. And in mm/gup.c, there's no pgd huge page handling in the follow_page_mask() code path. Hence it doesn't make sense to only have gup_fast_pgd_leaf() in gup_fast code path. Here remove gup_fast_pgd_leaf() and clean up the relevant codes. Signed-off-by: Baoquan He Reviewed-by: Oscar Salvador Acked-by: David Hildenbrand --- mm/gup.c | 49 +++---------------------------------------------- 1 file changed, 3 insertions(+), 46 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index eb668da933e1..77a5bc622567 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -3172,46 +3172,6 @@ static int gup_fast_pud_leaf(pud_t orig, pud_t *pudp= , unsigned long addr, return 1; } =20 -static int gup_fast_pgd_leaf(pgd_t orig, pgd_t *pgdp, unsigned long addr, - unsigned long end, unsigned int flags, struct page **pages, - int *nr) -{ - int refs; - struct page *page; - struct folio *folio; - - if (!pgd_access_permitted(orig, flags & FOLL_WRITE)) - return 0; - - BUILD_BUG_ON(pgd_devmap(orig)); - - page =3D pgd_page(orig); - refs =3D record_subpages(page, PGDIR_SIZE, addr, end, pages + *nr); - - folio =3D try_grab_folio_fast(page, refs, flags); - if (!folio) - return 0; - - if (unlikely(pgd_val(orig) !=3D pgd_val(*pgdp))) { - gup_put_folio(folio, refs, flags); - return 0; - } - - if (!pgd_write(orig) && gup_must_unshare(NULL, flags, &folio->page)) { - gup_put_folio(folio, refs, flags); - return 0; - } - - if (!gup_fast_folio_allowed(folio, flags)) { - gup_put_folio(folio, refs, flags); - return 0; - } - - *nr +=3D refs; - folio_set_referenced(folio); - return 1; -} - static int gup_fast_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr, unsigned long end, unsigned int flags, struct page **pages, int *nr) @@ -3306,12 +3266,9 @@ static void gup_fast_pgd_range(unsigned long addr, u= nsigned long end, next =3D pgd_addr_end(addr, end); if (pgd_none(pgd)) return; - if (unlikely(pgd_leaf(pgd))) { - if (!gup_fast_pgd_leaf(pgd, pgdp, addr, next, flags, - pages, nr)) - return; - } else if (!gup_fast_p4d_range(pgdp, pgd, addr, next, flags, - pages, nr)) + BUILD_BUG_ON(pgd_leaf(pgd)); + if (!gup_fast_p4d_range(pgdp, pgd, addr, next, flags, + pages, nr)) return; } while (pgdp++, addr =3D next, addr !=3D end); } --=20 2.41.0 From nobody Fri Dec 19 00:18:06 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 3908F201031 for ; Thu, 10 Apr 2025 03:57:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744257476; cv=none; b=O3mmYcYJNPs+B5KsldjGQ8mJrK06LLQzc8GSq77KXIOFdfuM7WWcVGj4E9VDoAxSZM05q46hzjJvLJpcZl3InJ4jbP5H2nyWcZG7NHoDNNLDCcgMPe/DLCbdJ2c3s9IpxhSHTDCEFQa1UO/YjK2+CtDV6ov2zQ+r5WiqDtlJvyI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744257476; c=relaxed/simple; bh=FDODwI3joPHBhYYHwx/rigJpxA2CfVzWCknO0NozRwg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-type; b=fENWZvkJA6Ramujxj6VPdsJuoI5lS6kqntr1J/weYQg+pDwCKvVZvRmYa2pZ6i/Lti3d5ppfsy3whWgprzhzZbfopxDg6M7c3MML9qnhwuKobOWHUHMK0jJVCA72zIBUjGZUBkpvjrKtMZTmt1s5XMBFXi93lokKgQQ/vfyiKUY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=eOLfOVyD; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="eOLfOVyD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744257473; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6pFd+u5mCoqhmCfr93K2zek+trGS6VlClORLBIf/qW0=; b=eOLfOVyDIueEQtp6I535dOobrS3KOZvQxnBmNakeRnPh/5nyL0gzBxUfAkycajvAOnnQlH THq0NgQsZ403ggDgpEj510JCN42kU4ZL4sL/gfT6BeYUaWuTpZ00hf2rQNmZL8XnXOdYhQ Rro1uXgQkDHrfaqvGvSCogqHQEoc50E= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-346-6oLAsj_QORK9FKjEnBq8rw-1; Wed, 09 Apr 2025 23:57:47 -0400 X-MC-Unique: 6oLAsj_QORK9FKjEnBq8rw-1 X-Mimecast-MFC-AGG-ID: 6oLAsj_QORK9FKjEnBq8rw_1744257465 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 65330180AF4E; Thu, 10 Apr 2025 03:57:45 +0000 (UTC) Received: from MiWiFi-R3L-srv.redhat.com (unknown [10.72.112.38]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5620119560AD; Thu, 10 Apr 2025 03:57:40 +0000 (UTC) From: Baoquan He To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, david@redhat.com, osalvador@suse.de, yanjun.zhu@linux.dev, linux-kernel@vger.kernel.org, Baoquan He Subject: [PATCH v4 4/4] mm/gup: clean up codes in fault_in_xxx() functions Date: Thu, 10 Apr 2025 11:57:17 +0800 Message-ID: <20250410035717.473207-5-bhe@redhat.com> In-Reply-To: <20250410035717.473207-1-bhe@redhat.com> References: <20250410035717.473207-1-bhe@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" The code style in fault_in_readable() and fault_in_writable() is a little inconsistent with fault_in_safe_writeable(). In fault_in_readable() and fault_in_writable(), it uses 'uaddr' passed in as loop cursor. While in fault_in_safe_writeable(), local variable 'start' is used as loop cursor. This may mislead people when reading code or making change in these codes. Here define explicit loop cursor and use for loop to simplify codes in these three functions. These cleanup can make them be consistent in code style and improve readability. Signed-off-by: Baoquan He --- mm/gup.c | 65 +++++++++++++++++++++++--------------------------------- 1 file changed, 26 insertions(+), 39 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 77a5bc622567..a76bd7e90a71 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2113,28 +2113,24 @@ static long __get_user_pages_locked(struct mm_struc= t *mm, unsigned long start, */ size_t fault_in_writeable(char __user *uaddr, size_t size) { - char __user *start =3D uaddr, *end; + const unsigned long start =3D (unsigned long)uaddr; + const unsigned long end =3D start + size; + unsigned long cur =3D start; =20 if (unlikely(size =3D=3D 0)) return 0; + if (!user_write_access_begin(uaddr, size)) return size; - if (!PAGE_ALIGNED(uaddr)) { - unsafe_put_user(0, uaddr, out); - uaddr =3D (char __user *)PAGE_ALIGN((unsigned long)uaddr); - } - end =3D (char __user *)PAGE_ALIGN((unsigned long)start + size); - if (unlikely(end < start)) - end =3D NULL; - while (uaddr !=3D end) { - unsafe_put_user(0, uaddr, out); - uaddr +=3D PAGE_SIZE; - } + + /* Stop once we overflow to 0. */ + for (; cur && cur < end; cur =3D PAGE_ALIGN_DOWN(cur + PAGE_SIZE)) + unsafe_put_user(0, (char __user *)cur, out); =20 out: user_write_access_end(); - if (size > uaddr - start) - return size - (uaddr - start); + if (size > cur - start) + return size - (cur - start); return 0; } EXPORT_SYMBOL(fault_in_writeable); @@ -2188,26 +2184,24 @@ EXPORT_SYMBOL(fault_in_subpage_writeable); */ size_t fault_in_safe_writeable(const char __user *uaddr, size_t size) { - unsigned long start =3D (unsigned long)uaddr, end; + const unsigned long start =3D (unsigned long)uaddr; + const unsigned long end =3D start + size; + unsigned long cur =3D start; struct mm_struct *mm =3D current->mm; bool unlocked =3D false; =20 if (unlikely(size =3D=3D 0)) return 0; - end =3D PAGE_ALIGN(start + size); - if (end < start) - end =3D 0; =20 mmap_read_lock(mm); - do { - if (fixup_user_fault(mm, start, FAULT_FLAG_WRITE, &unlocked)) + /* Stop once we overflow to 0. */ + for (; cur && cur < end; cur =3D PAGE_ALIGN_DOWN(cur + PAGE_SIZE)) + if (fixup_user_fault(mm, cur, FAULT_FLAG_WRITE, &unlocked)) break; - start =3D (start + PAGE_SIZE) & PAGE_MASK; - } while (start !=3D end); mmap_read_unlock(mm); =20 - if (size > start - (unsigned long)uaddr) - return size - (start - (unsigned long)uaddr); + if (size > cur - start) + return size - (cur - start); return 0; } EXPORT_SYMBOL(fault_in_safe_writeable); @@ -2222,30 +2216,23 @@ EXPORT_SYMBOL(fault_in_safe_writeable); */ size_t fault_in_readable(const char __user *uaddr, size_t size) { - const char __user *start =3D uaddr, *end; + const unsigned long start =3D (unsigned long)uaddr; + const unsigned long end =3D start + size; + unsigned long cur =3D start; volatile char c; =20 if (unlikely(size =3D=3D 0)) return 0; if (!user_read_access_begin(uaddr, size)) return size; - if (!PAGE_ALIGNED(uaddr)) { - unsafe_get_user(c, uaddr, out); - uaddr =3D (const char __user *)PAGE_ALIGN((unsigned long)uaddr); - } - end =3D (const char __user *)PAGE_ALIGN((unsigned long)start + size); - if (unlikely(end < start)) - end =3D NULL; - while (uaddr !=3D end) { - unsafe_get_user(c, uaddr, out); - uaddr +=3D PAGE_SIZE; - } - + /* Stop once we overflow to 0. */ + for (; cur && cur < end; cur =3D PAGE_ALIGN_DOWN(cur + PAGE_SIZE)) + unsafe_get_user(c, (const char __user *)cur, out); out: user_read_access_end(); (void)c; - if (size > uaddr - start) - return size - (uaddr - start); + if (size > cur - start) + return size - (cur - start); return 0; } EXPORT_SYMBOL(fault_in_readable); --=20 2.41.0