From mboxrd@z Thu Jan 1 00:00:00 1970 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 097B93AE198 for ; Sun, 24 May 2026 16:58:18 +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=1779641900; cv=none; b=oFmo2AcnOwwV6ZYFIBRM6SBqX+U8hwI4Pny/zLx3GWxaCWVmDMeMUFprunNWCzlmKc6NosD3oMsyoEM8J+pidww8hLN4/zE+VTE6ImUNtFaZuCwK/ir7GWfegI9dWffeYh+ldkkI2JcofR1Oestnq04Z2kyPL8h8zqwfZDz8QV4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779641900; c=relaxed/simple; bh=woxAs0TjB/f6Esra3HmYDkKs7vg26AUVV3klXb4VxuM=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; b=SZiAGASDCDm2wBMRl5lJSwu2NE3Gd+hk5zjWt1JOa9//O9Y6zI7UXMv15HIT7ebftwvB7f99Eo+5RAd55hErwXVzzanzhsEPXOFv1nIxofNrD95zoT/qleqmYziMl/LdJ/4csT7ZwtYF1Sqrthojvm00/Mty017A1U25P/0tWTI= 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=QYIaenur; 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="QYIaenur" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779641898; 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: in-reply-to:in-reply-to; bh=XYbRs3tCnjBDML/yVXAZgXnRWFC9EoMypl12Py3Braw=; b=QYIaenurbHO4Hr8ns65VqjcxAl7pqeBmCfr9mbk/xq3VrPTLPmk1+QPnxISaxLZBVyi4lY h2k94QX03DMK1QgGPkQxZ4NROhb8zbrcPpCj5RO85jXQp5gezqkVY3GN/rOXDGHLVzUUzD A68xNr7T1/Ml6BcuyffYbPDta3WSduE= Received: from mx-prod-mc-06.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-438-7I3IUvEbM8mkaBpGZEJI4A-1; Sun, 24 May 2026 12:58:15 -0400 X-MC-Unique: 7I3IUvEbM8mkaBpGZEJI4A-1 X-Mimecast-MFC-AGG-ID: 7I3IUvEbM8mkaBpGZEJI4A_1779641893 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id EF71F180044D; Sun, 24 May 2026 16:58:12 +0000 (UTC) Received: from fedora (unknown [10.44.48.14]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id CF0FE1685; Sun, 24 May 2026 16:58:07 +0000 (UTC) Received: by fedora (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Sun, 24 May 2026 18:58:12 +0200 (CEST) Date: Sun, 24 May 2026 18:58:06 +0200 From: Oleg Nesterov To: Alistair Popple , Andrew Morton , Byungchul Park , David Hildenbrand , Gregory Price , Joshua Hahn , Matthew Brost , Rakie Kim , Ying Huang , Zi Yan Cc: Jann Horn , Kees Cook , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/4] mm/mempolicy: kernel_migrate_pages: use find_get_task_by_vpid() Message-ID: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 kernel_migrate_pages() calls ptrace_may_access() under rcu_read_lock() for no reason. This is a leftover from before the commit 313674661925 ("Unify migrate_pages and move_pages access checks"), where rcu_read_lock() was needed to protect __task_cred(task). So we can drop the RCU lock right after get_task_struct(). Better yet, if pid != 0, we can use find_get_task_by_vpid() which does get_task_struct() itself, and avoid get/put_task_struct() otherwise. Signed-off-by: Oleg Nesterov --- mm/mempolicy.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 4326dff16aa6..2ec14001e4dc 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -1883,15 +1883,11 @@ static int kernel_migrate_pages(pid_t pid, unsigned long maxnode, if (err) goto out; - /* Find the mm_struct */ - rcu_read_lock(); - task = pid ? find_task_by_vpid(pid) : current; + task = pid ? find_get_task_by_vpid(pid) : current; if (!task) { - rcu_read_unlock(); err = -ESRCH; goto out; } - get_task_struct(task); err = -EINVAL; @@ -1900,11 +1896,9 @@ static int kernel_migrate_pages(pid_t pid, unsigned long maxnode, * Use the regular "ptrace_may_access()" checks. */ if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) { - rcu_read_unlock(); err = -EPERM; goto out_put; } - rcu_read_unlock(); task_nodes = cpuset_mems_allowed(task); /* Is the user allowed to access the target nodes? */ @@ -1932,7 +1926,8 @@ static int kernel_migrate_pages(pid_t pid, unsigned long maxnode, mmput(mm); out_put: - put_task_struct(task); + if (pid) + put_task_struct(task); out: NODEMASK_SCRATCH_FREE(scratch); return err; -- 2.52.0