Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp5162926rwb; Tue, 8 Aug 2023 22:45:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFP1cfgSx10wjM5xvOq+4qeaFWQjndwTQ+7pzC+tT8ze/QQGRQOWW13zC6i2T12KSEm/esJ X-Received: by 2002:a17:906:20d0:b0:99b:e6c3:f6b0 with SMTP id c16-20020a17090620d000b0099be6c3f6b0mr1283227ejc.62.1691559940926; Tue, 08 Aug 2023 22:45:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691559940; cv=none; d=google.com; s=arc-20160816; b=tWkLOUoHFRrVqePXeFr1mJ400MpeAgj9QeNO9errD7OfaJyKbFJXHo7RVmfY0eBbpz TSf/gMRZCybONQSnRwJkzxD5NYf8TasxGMwts/Wzm7YiizBYTWBAU/ahpKPBTefpYg5R JrP1eMGU7BsaF4LhRLKNcmzjTREP7vugGwF5AE1wELlX0/1aMWqX4GaHtMODkRplQsa0 adEPSozxGVqGmBoJiir4kwHx0B5KbjYSm1u44uHQRzphdvUZ1IrpiAqezBnWR/foXHki TOAUD+qGwRODl3gxMpXugMIiWt7EsRgi8kUevyNJwZN/AchA0IY6lRfdJBlt8zBheJUV 9kxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:to:content-language:subject:cc:user-agent:mime-version :date:message-id:dkim-signature; bh=DLYnHaRQSJ/7TSJnJK+Cv3rJAfptKDWdQt4TOSmxDM4=; fh=aV4UCXSOvxJMtamiXu2uXEOA8ujXG2Njex5FNF1OAas=; b=Es5AdL4W2TmyABMcEn3ncKXAzW/pxwcD8IPj9gHLbRN6wYSrGsI1nwT7ERHuepH2Z3 84HUPHp9p97opFYYFZCNAIdkKSW632KTzF2KymS7llmDXtRIdKhQ/ldDbjqpSu7bR19R scnRTfPSd7El/lBdxuUPwl+u/GbFbzdn+OEJgkh6P1NFb+OSvCxzrpRoJwH0Klc+A2GV 9XG8TyEoNob4L8yBdIZIRQUTfLObJFHBX0UibfP2QDNcsF93CKJXX69b5jH4CBgeisbG xivq75aY100t4X6Sh5+P6+6ODQ8alxumbkt7waG/K+YNaw0zcUAgvd/jQ7lyUWPfTL3Z HYeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=nCzWb4J3; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d27-20020a170906345b00b0098759716e36si2959372ejb.217.2023.08.08.22.45.16; Tue, 08 Aug 2023 22:45:40 -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=@collabora.com header.s=mail header.b=nCzWb4J3; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230143AbjHIEbc (ORCPT + 99 others); Wed, 9 Aug 2023 00:31:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230101AbjHIEb3 (ORCPT ); Wed, 9 Aug 2023 00:31:29 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D94A1BDA; Tue, 8 Aug 2023 21:31:28 -0700 (PDT) Received: from [192.168.100.7] (unknown [59.103.218.230]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: usama.anjum) by madras.collabora.co.uk (Postfix) with ESMTPSA id A65296607193; Wed, 9 Aug 2023 05:31:19 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1691555486; bh=xGTOVBf346p5z7gNl1KYsQVdZ1a5kLLBqA30qKLUwLk=; h=Date:Cc:Subject:To:References:From:In-Reply-To:From; b=nCzWb4J3wzCCrxjFs9KzjZu7S+A0zBSNj0abZIZQiqxXmLJdf/8OYdTkD8AkSHq55 8YEL9f/3M1+EuVWhryjVnX/jKuTQlpudcZrrlS1+/oRx0HPQBNbKIDOva/d90sU/5Q LqCI5WWJxwlA+lhzD04b7IPIETpF6et+ddKWYlW5TdxZiiveoHdDevHJ5gCNgL7E6Y GBWr5AG3OcbOD7vJyYeJbJwqP9BgB6WjIvRJ8ZfYZ9hCpHjc7i9wvmePInqQvFOqgK afBzw2NwqrvsYSRR3BGya817C3Pd5oShlxGV6GGOEe6WJ9yZ3mUdThWd7eOvDTZS1y tGyyCHoAGs2Wg== Message-ID: Date: Wed, 9 Aug 2023 09:31:14 +0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.1 Cc: Muhammad Usama Anjum , Peter Xu , David Hildenbrand , Andrew Morton , =?UTF-8?B?TWljaGHFgiBNaXJvc8WC?= =?UTF-8?Q?aw?= , 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, =?UTF-8?B?TWljaGHFgiBNaXJvc8WCYXc=?= Subject: Re: [PATCH v27 2/6] fs/proc/task_mmu: Implement IOCTL to get and optionally clear info about PTEs Content-Language: en-US To: Andrei Vagin References: <20230808104309.357852-1-usama.anjum@collabora.com> <20230808104309.357852-3-usama.anjum@collabora.com> <624cfa26-5650-ee0d-8e0a-1d844175bcaf@collabora.com> From: Muhammad Usama Anjum In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED 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 8/9/23 12:55 AM, Andrei Vagin wrote: > On Tue, Aug 8, 2023 at 12:35 PM Muhammad Usama Anjum > wrote: >> >> On 8/9/23 12:21 AM, Andrei Vagin wrote: >>> On Tue, Aug 8, 2023 at 3:43 AM Muhammad Usama Anjum >>> wrote: >>> >>> .... >>> >>>> +static int pagemap_scan_output(unsigned long categories, >>>> + struct pagemap_scan_private *p, >>>> + unsigned long addr, unsigned long *end) >>>> +{ >>>> + unsigned long n_pages, total_pages; >>>> + int ret = 0; >>>> + >>>> + if (!p->vec_buf) >>>> + return 0; >>>> + >>>> + categories &= p->arg.return_mask; >>>> + >>>> + n_pages = (*end - addr) / PAGE_SIZE; >>>> + if (check_add_overflow(p->found_pages, n_pages, &total_pages) || //TODO >>> >>> Need to fix this TODO. >> Sorry, I forgot to remove the "//TODO". As far as I've understood, the last >> discussion ended in keeping the check_add_overflow(). [1] I'll just remove >> the TODO. >> >> https://lore.kernel.org/all/CABb0KFEfmRz+Z_-7GygTL12E5Y254dvoUfWe4uSv9-wOx+Cs8w@mail.gmail.com >> >> >>> >>>> + total_pages > p->arg.max_pages) { >>>> + size_t n_too_much = total_pages - p->arg.max_pages; >>>> + *end -= n_too_much * PAGE_SIZE; >>>> + n_pages -= n_too_much; >>>> + ret = -ENOSPC; >>>> + } >>>> + >>>> + if (!pagemap_scan_push_range(categories, p, addr, *end)) { >>>> + *end = addr; >>>> + n_pages = 0; >>>> + ret = -ENOSPC; >>>> + } >>>> + >>>> + p->found_pages += n_pages; >>>> + if (ret) >>>> + p->walk_end_addr = *end; >>>> + >>>> + return ret; >>>> +} >>>> + >>> >>> ... >>> >>>> +static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg) >>>> +{ >>>> + struct mmu_notifier_range range; >>>> + struct pagemap_scan_private p; >>>> + unsigned long walk_start; >>>> + size_t n_ranges_out = 0; >>>> + int ret; >>>> + >>>> + memset(&p, 0, sizeof(p)); >>>> + ret = pagemap_scan_get_args(&p.arg, uarg); >>>> + if (ret) >>>> + return ret; >>>> + >>>> + p.masks_of_interest = MASKS_OF_INTEREST(p.arg); >>>> + ret = pagemap_scan_init_bounce_buffer(&p); >>>> + if (ret) >>>> + return ret; >>>> + >>>> + /* Protection change for the range is going to happen. */ >>>> + if (p.arg.flags & PM_SCAN_WP_MATCHING) { >>>> + mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA, 0, >>>> + mm, p.arg.start, p.arg.end); >>>> + mmu_notifier_invalidate_range_start(&range); >>>> + } >>>> + >>>> + walk_start = p.arg.start; >>>> + for (; walk_start < p.arg.end; walk_start = p.arg.walk_end) { >>>> + int n_out; >>>> + >>>> + if (fatal_signal_pending(current)) { >>>> + ret = -EINTR; >>>> + break; >>>> + } >>>> + >>>> + ret = mmap_read_lock_killable(mm); >>>> + if (ret) >>>> + break; >>>> + ret = walk_page_range(mm, walk_start, p.arg.end, >>>> + &pagemap_scan_ops, &p); >>>> + mmap_read_unlock(mm); >>>> + >>>> + n_out = pagemap_scan_flush_buffer(&p); >>>> + if (n_out < 0) >>>> + ret = n_out; >>>> + else >>>> + n_ranges_out += n_out; >>>> + >>>> + if (ret != -ENOSPC || p.arg.vec_len - 1 == 0 || >>>> + p.found_pages == p.arg.max_pages) { >>>> + p.walk_end_addr = p.arg.end; >>> >>> You should not change p.walk_end_addr If ret is ENOSPC. Pls add a test >>> case to check this. >> Yeah, I'm not setting walk_end_addr if ret is ENOSPC. >> >> I'm setting walk_end_addr only when ret = 0. I'd added this as a result of >> a test case in my local test application. I can look at adding some tests >> in pagemap_ioctl.c kselftest as well. > > I am not sure that I understand what you mean here. ENOSPC can be returned > when the vec array is full and in this case, walk_end_addr should be > the address when it stops scanning. I'll copy a test case in kselftest to prove or dis-prove the correctness of walk_end address. > >> >>> >>>> + break; >>>> + } >>>> + } >>>> + >>>> + if (p.cur_buf.start != p.cur_buf.end) { >>>> + if (copy_to_user(p.vec_out, &p.cur_buf, sizeof(p.cur_buf))) >>>> + ret = -EFAULT; >>>> + else >>>> + ++n_ranges_out; >>>> + } >>>> + >>>> + /* ENOSPC signifies early stop (buffer full) from the walk. */ >>>> + if (!ret || ret == -ENOSPC) >>>> + ret = n_ranges_out; >>>> + >>>> + p.arg.walk_end = p.walk_end_addr ? p.walk_end_addr : walk_start; >>>> + if (pagemap_scan_writeback_args(&p.arg, uarg)) >>>> + ret = -EFAULT; >>>> + >>>> + if (p.arg.flags & PM_SCAN_WP_MATCHING) >>>> + mmu_notifier_invalidate_range_end(&range); >>>> + >>>> + kfree(p.vec_buf); >>>> + return ret; >>>> +} >>> >>> Thanks, >>> Andrei >> >> -- >> BR, >> Muhammad Usama Anjum -- BR, Muhammad Usama Anjum