Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp26318344rwd; Mon, 3 Jul 2023 08:08:55 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6H8tjNO5TyiXMYezuC865Lr3cWY36f0cHXIroEZgjeZlNDXjIsvGIkCIeGQTWVieEoAR3q X-Received: by 2002:a17:90b:291:b0:262:ec04:4ff7 with SMTP id az17-20020a17090b029100b00262ec044ff7mr21288444pjb.16.1688396935213; Mon, 03 Jul 2023 08:08:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688396935; cv=none; d=google.com; s=arc-20160816; b=yOMBHPrgcjEeMfkRCk4uAGPI1eXWTp2J7+2/m7I9URMcK4LX/CLirwcxpt7A8EI/B0 qKY32lOyQbnDuP9lDYFscP3n5+z2s+R1cUCCmOQKH7JXUnJjRFNBWo7FEi7xHFrlHRdg 6vGACi149Uz+pB8OmMltT+rxa5DNYoPVGM9lYSkztCRWjd+JbKAesqWnU2Uh+y66qTGf 8lEzBndQwJO0t0tagpWeiSlcDfQLS7wq9YpP4BCqTJ59awF6eKl4inalLN4NpioVGj0v 8/POPwfIi/7Q3RMbO431QDIxLB1CiTZo/PcpYTFuf3BRPqhrfmSno5CTsJTQd7WeCRTf zzBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=x1uVO/PuFL/rvkGyp3hYqkKcZEPmzxpFJfkhQ3PxmoQ=; fh=yJrd2rNLaubHWVPiPZvN8SQeaivbPfQnuyOpc7saJ2E=; b=aIwuZONdGz8ItTkzuD2fvf+pc3WGPbYDhMNFivwjL7h9ytSMzoHA/ASSJ/GgWrSgrh lrOFHQXpDw/kYe/fl2hhTxuD20Znxsm3wIMH2XkRdrR71+7a7m1b5yxrlwNyb25LUlge 4NB4+p1/6EUmtfzQKjmns9XckKgl7U4aelr41LX9cTxIbaX+2KsT3oWVBFPoqr2MYVVo HonQV5BWpsxUXkNDiCnlU+gi/ulXruRb8n5RnoSVCewjY7YjyvVtSJ0R2S4apjPmtOfg VxlfgdYBO6LqjVab9jRKhjEBomanBti30O/m9XVSUMMUfx0A8Hg/Ccsf7mFZkmPX3d9Z X91Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=iwo2OIPI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u62-20020a638541000000b0054ff0049d08si18224863pgd.404.2023.07.03.08.08.34; Mon, 03 Jul 2023 08:08:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=iwo2OIPI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231628AbjGCPHV (ORCPT + 99 others); Mon, 3 Jul 2023 11:07:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47184 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230131AbjGCPHT (ORCPT ); Mon, 3 Jul 2023 11:07:19 -0400 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A309A114; Mon, 3 Jul 2023 08:07:18 -0700 (PDT) Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-668711086f4so2857358b3a.1; Mon, 03 Jul 2023 08:07:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1688396838; x=1690988838; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=x1uVO/PuFL/rvkGyp3hYqkKcZEPmzxpFJfkhQ3PxmoQ=; b=iwo2OIPINlyiMM4BJWLbMPpT5T2hKPkSeYRjYSgeyo38d6pU263zWBU9GVfl3eaTaN RLh4ExDBmcd5oZRVV7TMcXVeBJzKuebXT4gymYOPdTNZNfhMDA63jeeVhMhAv9qKLAaK XOhKc9QLv92YA95k57e2UkTdlAMGhpuQLB6ucSgZRro/sYZBSIueJCEa88zEMvRw+rz4 yxsgrJOMjUcXhAxqwUgtU/S49gxptMrcT4k5RjtTPXgQezVNDU4nZdcOos8uKVGGeH2n 8+z7hqJJ7MPO0XT1Yuq4O4fcGnHFp0giYaoEiHzvHbirP5D9SvaQF43Nzm9PsDapqdKn dUzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688396838; x=1690988838; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=x1uVO/PuFL/rvkGyp3hYqkKcZEPmzxpFJfkhQ3PxmoQ=; b=MBR6zdvWNms21UdZqX9xRJwiDUR8yPLamRU2uf+pCYpeHoAoEcZ350r+U6R+L17ucR uxPGIqrvv7avhUQipX0MtLyG3KXcr4WvXxNN6wK90tBDY99EXcrZCvXGZej+IqKPCdZf id8ouNs8xZc66q2+x8W6ApMDRjstg21GtUts0tt+Elo5aQJxBP41OUWKNavDwv6Mhwlm eA1tymoCT88wmG3ouKfoEeo/D4SVn8esQzaCv79mp7X8XyHmITl7HIUpjmAKwZcc8IZU GSbFPCK5bhPa71pUJju4+uQ3XIOz/6bXenC4wjdBr+viCLvW+qK7EMctZ7jtQaFNReDx Vjgw== X-Gm-Message-State: ABy/qLY3/XkMubI3r3VczMwH0E9SLGQjyWv2a4A8UcTYAFPSi86zP5zd GxT+o1g+ZNvnFUV7B6IF03c= X-Received: by 2002:a05:6a00:b90:b0:666:c1ab:d6e5 with SMTP id g16-20020a056a000b9000b00666c1abd6e5mr15935078pfj.16.1688396837726; Mon, 03 Jul 2023 08:07:17 -0700 (PDT) Received: from gmail.com ([2601:600:8500:5f14:d627:c51e:516e:a105]) by smtp.gmail.com with ESMTPSA id x2-20020aa79182000000b00682a16f0b00sm273814pfa.210.2023.07.03.08.07.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jul 2023 08:07:16 -0700 (PDT) Date: Mon, 3 Jul 2023 08:07:14 -0700 From: Andrei Vagin To: Muhammad Usama Anjum Cc: Peter Xu , David Hildenbrand , Andrew Morton , =?utf-8?B?TWljaGHFgiBNaXJvc8WCYXc=?= , Danylo Mocherniuk , Paul Gofman , Cyrill Gorcunov , Mike Rapoport , Nadav Amit , Alexander Viro , Shuah Khan , Christian Brauner , Yang Shi , Vlastimil Babka , "Liam R . Howlett" , Yun Zhou , Suren Baghdasaryan , Alex Sierra , Matthew Wilcox , Pasha Tatashin , Axel Rasmussen , "Gustavo A . R . Silva" , Dan Williams , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Greg KH , kernel@collabora.com Subject: Re: [PATCH v22 2/5] fs/proc/task_mmu: Implement IOCTL to get and optionally clear info about PTEs Message-ID: References: <20230628095426.1886064-1-usama.anjum@collabora.com> <20230628095426.1886064-3-usama.anjum@collabora.com> <49015b82-2f7f-fa34-d56a-2f427f38f28f@collabora.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <49015b82-2f7f-fa34-d56a-2f427f38f28f@collabora.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,FSL_HELO_FAKE, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jul 03, 2023 at 11:47:37AM +0500, Muhammad Usama Anjum wrote: > On 6/30/23 8:01 PM, Andrei Vagin wrote: > > On Wed, Jun 28, 2023 at 02:54:23PM +0500, Muhammad Usama Anjum wrote: > >> This IOCTL, PAGEMAP_SCAN on pagemap file can be used to get and/or clear > >> the info about page table entries. The following operations are supported > >> in this ioctl: > >> - Get the information if the pages have been written-to (PAGE_IS_WRITTEN), > >> file mapped (PAGE_IS_FILE), present (PAGE_IS_PRESENT), swapped > >> (PAGE_IS_SWAPPED) or page has pfn zero (PAGE_IS_PFNZERO). > >> - Find pages which have been written-to and/or write protect the pages > >> (atomic PM_SCAN_OP_GET + PM_SCAN_OP_WP) > >> > >> This IOCTL can be extended to get information about more PTE bits. The > >> entire address range passed by user [start, end) is scanned until either > >> the user provided buffer is full or max_pages have been found. > >> > >> Signed-off-by: Muhammad Usama Anjum > > > > > > > >> + > >> +static long do_pagemap_scan(struct mm_struct *mm, unsigned long __arg) > >> +{ > >> + struct pm_scan_arg __user *uarg = (struct pm_scan_arg __user *)__arg; > >> + unsigned long long start, end, walk_start, walk_end; > >> + unsigned long empty_slots, vec_index = 0; > >> + struct mmu_notifier_range range; > >> + struct page_region __user *vec; > >> + struct pagemap_scan_private p; > >> + struct pm_scan_arg arg; > >> + int ret = 0; > >> + > >> + if (copy_from_user(&arg, uarg, sizeof(arg))) > >> + return -EFAULT; > >> + > >> + start = untagged_addr((unsigned long)arg.start); > >> + end = untagged_addr((unsigned long)arg.end); > >> + vec = (struct page_region __user *)untagged_addr((unsigned long)arg.vec); > >> + > >> + ret = pagemap_scan_args_valid(&arg, start, end, vec); > >> + if (ret) > >> + return ret; > >> + > >> + p.max_pages = (arg.max_pages) ? arg.max_pages : ULONG_MAX; > >> + p.found_pages = 0; > >> + p.required_mask = arg.required_mask; > >> + p.anyof_mask = arg.anyof_mask; > >> + p.excluded_mask = arg.excluded_mask; > >> + p.return_mask = arg.return_mask; > >> + p.flags = arg.flags; > >> + p.flags |= ((p.required_mask | p.anyof_mask | p.excluded_mask) & > >> + PAGE_IS_WRITTEN) ? PM_SCAN_REQUIRE_UFFD : 0; > >> + p.cur_buf.start = p.cur_buf.len = p.cur_buf.flags = 0; > >> + p.vec_buf = NULL; > >> + p.vec_buf_len = PAGEMAP_WALK_SIZE >> PAGE_SHIFT; > >> + > >> + /* > >> + * Allocate smaller buffer to get output from inside the page walk > >> + * functions and walk page range in PAGEMAP_WALK_SIZE size chunks. As > >> + * we want to return output to user in compact form where no two > >> + * consecutive regions should be continuous and have the same flags. > >> + * So store the latest element in p.cur_buf between different walks and > >> + * store the p.cur_buf at the end of the walk to the user buffer. > >> + */ > >> + if (IS_PM_SCAN_GET(p.flags)) { > >> + p.vec_buf = kmalloc_array(p.vec_buf_len, sizeof(*p.vec_buf), > >> + GFP_KERNEL); > >> + if (!p.vec_buf) > >> + return -ENOMEM; > >> + } > >> + > >> + if (IS_PM_SCAN_WP(p.flags)) { > >> + mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA, 0, > >> + mm, start, end); > >> + mmu_notifier_invalidate_range_start(&range); > >> + } > >> + > >> + walk_start = walk_end = start; > >> + while (walk_end < end && !ret) { > >> + if (IS_PM_SCAN_GET(p.flags)) { > >> + p.vec_buf_index = 0; > >> + > >> + /* > >> + * All data is copied to cur_buf first. When more data > >> + * is found, we push cur_buf to vec_buf and copy new > >> + * data to cur_buf. Subtract 1 from length as the > >> + * index of cur_buf isn't counted in length. > >> + */ > >> + empty_slots = arg.vec_len - vec_index; > >> + p.vec_buf_len = min(p.vec_buf_len, empty_slots - 1); > >> + } > >> + > >> + ret = mmap_read_lock_killable(mm); > >> + if (ret) > >> + goto return_status; > >> + > >> + walk_end = min((walk_start + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK, end); > >> + > >> + ret = walk_page_range(mm, walk_start, walk_end, > >> + &pagemap_scan_ops, &p); > >> + mmap_read_unlock(mm); > >> + > >> + if (ret && ret != PM_SCAN_FOUND_MAX_PAGES && > >> + ret != PM_SCAN_END_WALK) > >> + goto return_status; > >> + > >> + walk_start = walk_end; > >> + if (IS_PM_SCAN_GET(p.flags) && p.vec_buf_index) { > >> + if (copy_to_user(&vec[vec_index], p.vec_buf, > >> + p.vec_buf_index * sizeof(*p.vec_buf))) { > >> + /* > >> + * Return error even though the OP succeeded > >> + */ > >> + ret = -EFAULT; > >> + goto return_status; > >> + } > >> + vec_index += p.vec_buf_index; > >> + } > >> + } > >> + > >> + if (p.cur_buf.len) { > >> + if (copy_to_user(&vec[vec_index], &p.cur_buf, sizeof(p.cur_buf))) { > >> + ret = -EFAULT; > >> + goto return_status; > >> + } > >> + vec_index++; > >> + } > >> + > >> + ret = vec_index; > >> + > >> +return_status: > >> + arg.start = (unsigned long)walk_end; > > > > This doesn't look right. pagemap_scan_pmd_entry can stop early. For > > example, it can happen when it hits the max_pages limit. Do I miss > > something? > The walk_page_range() calls pagemap_scan_pmd_entry(). So whatever status is > returned from pagemap_scan_pmd_entry(), walk_page_range() returns to this > function where we are handling the status code. After while loop starts, > there is only 1 return path. Hence there isn't any path missing where we'll > miss setting arg.start. I mean walk_end isn't actually the end address. The end adress should be the next page after the last revised page. Here we don't know whether all pages in [walk_start, walk_end) has been revised. > > > > >> + if (copy_to_user(&uarg->start, &arg.start, sizeof(arg.start))) > >> + ret = -EFAULT; > >> + > >> + if (IS_PM_SCAN_WP(p.flags)) > >> + mmu_notifier_invalidate_range_end(&range); > >> + > >> + kfree(p.vec_buf); > >> + return ret; > >> +} > >> + > > -- > BR, > Muhammad Usama Anjum