Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp4879509rwb; Tue, 8 Aug 2023 15:49:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFv2iKEfQqXKjSYl2w8CT9dtgidcLwa8qMc87CAIND9pGyxCYvn14N2ap/zGIsy1YprjK1U X-Received: by 2002:a17:907:1b18:b0:99b:dd23:4f01 with SMTP id mp24-20020a1709071b1800b0099bdd234f01mr11918889ejc.33.1691534968335; Tue, 08 Aug 2023 15:49:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691534968; cv=none; d=google.com; s=arc-20160816; b=ZntJGLZ9PgceO0QP/lRuGS/CB3kkpyxzgvfb/aBddj0yoV3V0VVLDWv/lF7rXukajj xxP41LZmQur3T7t47tk0bvvXypEGX5mXmk5hzTt4x5X7aSKxHMnI0hsfwuMIJH5fDtbS 10B6C1zi76SVxOfvO+SpSvm9lHz1sirZ/M8ZPoGemnLLwgkHIRKW51VnIxKSpHDFXCWo GoVWstJiJmm7KFnTKisBMXPcUBRvJWxzneavRDyLHEzswUG+oxMuVpCvtS4AG/6oPjRH saVDGxlJ6POeSu7XZFvaMBxLZdpIx8zjG8hf8qH9eZa4muICgWxXcVlsxikxuCh1AzOR Mrfw== 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 :content-language:references:to:subject:cc:user-agent:mime-version :date:message-id:dkim-signature; bh=nm+a+LZzF7kCg6EX8dgYglMBCe7wsEDIz7hH/+aQDkA=; fh=aV4UCXSOvxJMtamiXu2uXEOA8ujXG2Njex5FNF1OAas=; b=01haoEwdIlNEZtGMw5A/xzFu/WW2cKHVtKaCPP/n2x1kz7LNI6BOUpu/7tKybvMcso xTpdVyS4b1Q+sY/H1KDC5491LFLIrb07sb4D1w/kpxq25umZhSWpVhpAoQPXaGMdHyg/ WsF2E5ADpIFH2RwdDd8HrjU+pgphLZ35wIhGCrF8flSF2F/Hz2JfRxuO5o+xG3C1tjjA VjG9WrVf7ns1FNwotXtucdMPfX1ClNbGzoB2Jza4k+8ui4HDXGTAr4vIXs2ZdYZdlkve tvM0Wc2mXc4V1eiOdn4aUX3Qt2buEdbHydCwO9BsRUcRQHnvLHoI5xDN69MUsX5UzlPM he/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=GVGRPRGg; 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 t8-20020a170906064800b0099845f657a8si7832268ejb.616.2023.08.08.15.49.03; Tue, 08 Aug 2023 15:49:28 -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=GVGRPRGg; 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 S234524AbjHHUZH (ORCPT + 99 others); Tue, 8 Aug 2023 16:25:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234298AbjHHUYx (ORCPT ); Tue, 8 Aug 2023 16:24:53 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e5ab]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8844A273; Tue, 8 Aug 2023 12:35:13 -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 34A2F66071EF; Tue, 8 Aug 2023 20:35:06 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1691523311; bh=1+Z3o6x/w64sAUDuV9BLbfjMD1tjNE9ITcvlYGSrFCA=; h=Date:Cc:Subject:To:References:From:In-Reply-To:From; b=GVGRPRGg5bp00TFStvcTgcHZrdGu8CbEKIX5GK/131gbeAjlAmTXBtZcbhXbokZea t0Xv5kj5mdk7PGNoj3x0Kxg3g8rYoeQd0mHjbIrMA/rgo7jn9LxTJDOCzZrH8tjtbH 1CIrJrH9949atBbLsdw+B3zQx6SvGIsy3uF0oxuJvZ6mZXUbpO9qTuXMeqL6gUzzEI t9AiryugpvDYuOGgz2irZV5t3lJPd9gLxleRhsbhrT0hdBiFQBoZ/ipQksYAn/8LQB QCZtrHXClSEdpc2wOVjNtgiUXupXIbxcKiMPNLalFZeJc043XSF9VemqVmryFnzlve UyVJfg11YPk3w== Message-ID: <624cfa26-5650-ee0d-8e0a-1d844175bcaf@collabora.com> Date: Wed, 9 Aug 2023 00:35:01 +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 To: Andrei Vagin References: <20230808104309.357852-1-usama.anjum@collabora.com> <20230808104309.357852-3-usama.anjum@collabora.com> Content-Language: en-US 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, RCVD_IN_DNSWL_BLOCKED,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: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. > >> + 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