diff mbox series

[1/2] tomoyo: Convert get_user_pages*() to pin_user_pages*()

Message ID 1604737451-19082-1-git-send-email-jrdr.linux@gmail.com (mailing list archive)
State New, archived
Headers show
Series [1/2] tomoyo: Convert get_user_pages*() to pin_user_pages*() | expand

Commit Message

Souptick Joarder Nov. 7, 2020, 8:24 a.m. UTC
In 2019, we introduced pin_user_pages*() and now we are converting
get_user_pages*() to the new API as appropriate. [1] & [2] could
be referred for more information. This is case 5 as per document [1].

[1] Documentation/core-api/pin_user_pages.rst

[2] "Explicit pinning of user-space pages":
        https://lwn.net/Articles/807108/

Signed-off-by: Souptick Joarder <jrdr.linux@gmail.com>
Cc: John Hubbard <jhubbard@nvidia.com>
---
 security/tomoyo/domain.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

John Hubbard Nov. 7, 2020, 9:04 a.m. UTC | #1
On 11/7/20 12:24 AM, Souptick Joarder wrote:
> In 2019, we introduced pin_user_pages*() and now we are converting
> get_user_pages*() to the new API as appropriate. [1] & [2] could
> be referred for more information. This is case 5 as per document [1].

It turns out that Case 5 can be implemented via a better pattern, as long
as we're just dealing with a page at a time, briefly:

lock_page()
write to page's data
unlock_page()

...which neatly synchronizes with writeback and other fs activities.

I was going to track down the Case 5's and do that [1].

+CC Jan and Matthew, to keep us on the straight and narrow, just in case
I'm misunderstanding something.

[1] https://lore.kernel.org/r/e78fb7af-627b-ce80-275e-51f97f1f3168@nvidia.com

thanks,
John Hubbard Nov. 7, 2020, 7:17 p.m. UTC | #2
On 11/7/20 1:04 AM, John Hubbard wrote:
> On 11/7/20 12:24 AM, Souptick Joarder wrote:
>> In 2019, we introduced pin_user_pages*() and now we are converting
>> get_user_pages*() to the new API as appropriate. [1] & [2] could
>> be referred for more information. This is case 5 as per document [1].
> 
> It turns out that Case 5 can be implemented via a better pattern, as long
> as we're just dealing with a page at a time, briefly:
> 
> lock_page()
> write to page's data
> unlock_page()
> 
> ...which neatly synchronizes with writeback and other fs activities.

Ahem, I left out a key step: set_page_dirty()!

lock_page()
write to page's data
set_page_dirty()
unlock_page()


thanks,
Tetsuo Handa Nov. 8, 2020, 1:13 a.m. UTC | #3
On 2020/11/08 4:17, John Hubbard wrote:
> On 11/7/20 1:04 AM, John Hubbard wrote:
>> On 11/7/20 12:24 AM, Souptick Joarder wrote:
>>> In 2019, we introduced pin_user_pages*() and now we are converting
>>> get_user_pages*() to the new API as appropriate. [1] & [2] could
>>> be referred for more information. This is case 5 as per document [1].
>>
>> It turns out that Case 5 can be implemented via a better pattern, as long
>> as we're just dealing with a page at a time, briefly:
>>
>> lock_page()
>> write to page's data
>> unlock_page()
>>
>> ...which neatly synchronizes with writeback and other fs activities.
> 
> Ahem, I left out a key step: set_page_dirty()!
> 
> lock_page()
> write to page's data
> set_page_dirty()
> unlock_page()
> 

Excuse me, but Documentation/core-api/pin_user_pages.rst says 
"CASE 5: Pinning in order to _write_ to the data within the page"
while tomoyo_dump_page() is for "_read_ the data within the page".
Do we want to convert to pin_user_pages_remote() or lock_page() ?
John Hubbard Nov. 8, 2020, 2:17 a.m. UTC | #4
On 11/7/20 5:13 PM, Tetsuo Handa wrote:
> On 2020/11/08 4:17, John Hubbard wrote:
>> On 11/7/20 1:04 AM, John Hubbard wrote:
>>> On 11/7/20 12:24 AM, Souptick Joarder wrote:
>>>> In 2019, we introduced pin_user_pages*() and now we are converting
>>>> get_user_pages*() to the new API as appropriate. [1] & [2] could
>>>> be referred for more information. This is case 5 as per document [1].
>>>
>>> It turns out that Case 5 can be implemented via a better pattern, as long
>>> as we're just dealing with a page at a time, briefly:
>>>
>>> lock_page()
>>> write to page's data
>>> unlock_page()
>>>
>>> ...which neatly synchronizes with writeback and other fs activities.
>>
>> Ahem, I left out a key step: set_page_dirty()!
>>
>> lock_page()
>> write to page's data
>> set_page_dirty()
>> unlock_page()
>>
> 
> Excuse me, but Documentation/core-api/pin_user_pages.rst says
> "CASE 5: Pinning in order to _write_ to the data within the page"
> while tomoyo_dump_page() is for "_read_ the data within the page".
> Do we want to convert to pin_user_pages_remote() or lock_page() ?
> 

Sorry, I missed the direction here, was too focused on the Case 5
aspect. Yes. Case 5 (which, again, I think we're about to re-document)
is only about *writing* to data within the page.

So in this case, where it is just reading from the page, I think it's
already from a gup vs pup point of view.

btw, it's not clear to me whether the current code is susceptible to any
sort of problem involving something writing to the page while it
is being dumped (I am curious). But changing from gup to pup wouldn't
fix that, if it were a problem. It a separate question from this patch.

(Souptick, if you're interested, the Case 5 documentation change and
callsite retrofit is all yours if you want it. Otherwise it's on
my list.)

thanks,
Tetsuo Handa Nov. 8, 2020, 4:12 a.m. UTC | #5
On 2020/11/08 11:17, John Hubbard wrote:
>> Excuse me, but Documentation/core-api/pin_user_pages.rst says
>> "CASE 5: Pinning in order to _write_ to the data within the page"
>> while tomoyo_dump_page() is for "_read_ the data within the page".
>> Do we want to convert to pin_user_pages_remote() or lock_page() ?
>>
> 
> Sorry, I missed the direction here, was too focused on the Case 5
> aspect. Yes. Case 5 (which, again, I think we're about to re-document)
> is only about *writing* to data within the page.
> 
> So in this case, where it is just reading from the page, I think it's
> already from a gup vs pup point of view.
> 
> btw, it's not clear to me whether the current code is susceptible to any
> sort of problem involving something writing to the page while it
> is being dumped (I am curious). But changing from gup to pup wouldn't
> fix that, if it were a problem. It a separate question from this patch.

The "struct page" tomoyo_dump_page() accesses is argv/envp arguments passed
to execve() syscall. Therefore, these pages are not visible from threads
except current thread, and thus there is no possibility that these pages
are modified by other threads while current thread is reading.
John Hubbard Nov. 8, 2020, 5 a.m. UTC | #6
On 11/7/20 8:12 PM, Tetsuo Handa wrote:
> On 2020/11/08 11:17, John Hubbard wrote:
>>> Excuse me, but Documentation/core-api/pin_user_pages.rst says
>>> "CASE 5: Pinning in order to _write_ to the data within the page"
>>> while tomoyo_dump_page() is for "_read_ the data within the page".
>>> Do we want to convert to pin_user_pages_remote() or lock_page() ?
>>>
>>
>> Sorry, I missed the direction here, was too focused on the Case 5
>> aspect. Yes. Case 5 (which, again, I think we're about to re-document)
>> is only about *writing* to data within the page.
>>
>> So in this case, where it is just reading from the page, I think it's
>> already from a gup vs pup point of view.
>>
>> btw, it's not clear to me whether the current code is susceptible to any
>> sort of problem involving something writing to the page while it
>> is being dumped (I am curious). But changing from gup to pup wouldn't
>> fix that, if it were a problem. It a separate question from this patch.
> 
> The "struct page" tomoyo_dump_page() accesses is argv/envp arguments passed
> to execve() syscall. Therefore, these pages are not visible from threads
> except current thread, and thus there is no possibility that these pages
> are modified by other threads while current thread is reading.
> 

Perfect. So since I accidentally left out the word "correct" above (I meant
to write, "it's already correct"), let me be extra clear: Souptick, we
should just drop this patch.

thanks,
Souptick Joarder Nov. 9, 2020, 3:36 a.m. UTC | #7
On Sun, Nov 8, 2020 at 7:47 AM John Hubbard <jhubbard@nvidia.com> wrote:
>
> On 11/7/20 5:13 PM, Tetsuo Handa wrote:
> > On 2020/11/08 4:17, John Hubbard wrote:
> >> On 11/7/20 1:04 AM, John Hubbard wrote:
> >>> On 11/7/20 12:24 AM, Souptick Joarder wrote:
> >>>> In 2019, we introduced pin_user_pages*() and now we are converting
> >>>> get_user_pages*() to the new API as appropriate. [1] & [2] could
> >>>> be referred for more information. This is case 5 as per document [1].
> >>>
> >>> It turns out that Case 5 can be implemented via a better pattern, as long
> >>> as we're just dealing with a page at a time, briefly:
> >>>
> >>> lock_page()
> >>> write to page's data
> >>> unlock_page()
> >>>
> >>> ...which neatly synchronizes with writeback and other fs activities.
> >>
> >> Ahem, I left out a key step: set_page_dirty()!
> >>
> >> lock_page()
> >> write to page's data
> >> set_page_dirty()
> >> unlock_page()
> >>
> >
> > Excuse me, but Documentation/core-api/pin_user_pages.rst says
> > "CASE 5: Pinning in order to _write_ to the data within the page"
> > while tomoyo_dump_page() is for "_read_ the data within the page".
> > Do we want to convert to pin_user_pages_remote() or lock_page() ?
> >
>
> Sorry, I missed the direction here, was too focused on the Case 5
> aspect. Yes. Case 5 (which, again, I think we're about to re-document)
> is only about *writing* to data within the page.
>
> So in this case, where it is just reading from the page, I think it's
> already from a gup vs pup point of view.
>
> btw, it's not clear to me whether the current code is susceptible to any
> sort of problem involving something writing to the page while it
> is being dumped (I am curious). But changing from gup to pup wouldn't
> fix that, if it were a problem. It a separate question from this patch.
>
> (Souptick, if you're interested, the Case 5 documentation change and
> callsite retrofit is all yours if you want it. Otherwise it's on
> my list.)

Sure John, I will take it.
Souptick Joarder Nov. 9, 2020, 3:38 a.m. UTC | #8
On Sun, Nov 8, 2020 at 10:30 AM John Hubbard <jhubbard@nvidia.com> wrote:
>
> On 11/7/20 8:12 PM, Tetsuo Handa wrote:
> > On 2020/11/08 11:17, John Hubbard wrote:
> >>> Excuse me, but Documentation/core-api/pin_user_pages.rst says
> >>> "CASE 5: Pinning in order to _write_ to the data within the page"
> >>> while tomoyo_dump_page() is for "_read_ the data within the page".
> >>> Do we want to convert to pin_user_pages_remote() or lock_page() ?
> >>>
> >>
> >> Sorry, I missed the direction here, was too focused on the Case 5
> >> aspect. Yes. Case 5 (which, again, I think we're about to re-document)
> >> is only about *writing* to data within the page.
> >>
> >> So in this case, where it is just reading from the page, I think it's
> >> already from a gup vs pup point of view.
> >>
> >> btw, it's not clear to me whether the current code is susceptible to any
> >> sort of problem involving something writing to the page while it
> >> is being dumped (I am curious). But changing from gup to pup wouldn't
> >> fix that, if it were a problem. It a separate question from this patch.
> >
> > The "struct page" tomoyo_dump_page() accesses is argv/envp arguments passed
> > to execve() syscall. Therefore, these pages are not visible from threads
> > except current thread, and thus there is no possibility that these pages
> > are modified by other threads while current thread is reading.
> >
>
> Perfect. So since I accidentally left out the word "correct" above (I meant
> to write, "it's already correct"), let me be extra clear: Souptick, we
> should just drop this patch.
>

Agreed. I will drop this patch.
diff mbox series

Patch

diff --git a/security/tomoyo/domain.c b/security/tomoyo/domain.c
index dc4ecc0..bd748be 100644
--- a/security/tomoyo/domain.c
+++ b/security/tomoyo/domain.c
@@ -914,7 +914,7 @@  bool tomoyo_dump_page(struct linux_binprm *bprm, unsigned long pos,
 	 * (represented by bprm).  'current' is the process doing
 	 * the execve().
 	 */
-	if (get_user_pages_remote(bprm->mm, pos, 1,
+	if (pin_user_pages_remote(bprm->mm, pos, 1,
 				FOLL_FORCE, &page, NULL, NULL) <= 0)
 		return false;
 #else
@@ -936,7 +936,7 @@  bool tomoyo_dump_page(struct linux_binprm *bprm, unsigned long pos,
 	}
 	/* Same with put_arg_page(page) in fs/exec.c */
 #ifdef CONFIG_MMU
-	put_page(page);
+	unpin_user_page(page);
 #endif
 	return true;
 }