Bug 195186 - JSScript should keep the cache file locked for the duration of its existence and should truncate the cache when it is out of date
Summary: JSScript should keep the cache file locked for the duration of its existence ...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Saam Barati
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2019-02-28 14:39 PST by Saam Barati
Modified: 2019-03-06 19:07 PST (History)
14 users (show)

See Also:


Attachments
patch (11.15 KB, patch)
2019-03-04 16:09 PST, Saam Barati
keith_miller: review+
Details | Formatted Diff | Diff
patch for landing (11.05 KB, patch)
2019-03-06 17:04 PST, Saam Barati
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Saam Barati 2019-02-28 14:39:43 PST
...
Comment 1 Tadeu Zagallo 2019-02-28 23:57:45 PST
I don't think we need to keep the file locked after we mmap'd it. Since we are using MAP_PRIVATE, we won't see the updates until the next time we mmap it.
Comment 2 Saam Barati 2019-03-01 00:39:50 PST
I don’t think that’s how mmap works. I think it’s probably unspecified, but if I had to guess, if we mmap it in, then someone else changes content on disk, and then we page fault in that new content on a read, we’ll see the updated content.

But there’s a more interesting reason we want to keep it locked: we want to update the API to iteratively update the cache
Comment 3 Saam Barati 2019-03-01 00:42:20 PST
One man page agrees with me:

“MAP_SHARED and MAP_PRIVATE describe the disposition of write references to the memory object. If MAP_SHARED is specified, write references change the underlying object. If MAP_PRIVATE is specified, modifications to the mapped data by the calling process will be visible only to the calling process and will not change the underlying object. It is unspecified whether modifications to the underlying object done after the MAP_PRIVATE mapping is established are visible through the MAP_PRIVATE mapping. Either MAP_SHARED or MAP_PRIVATE can be specified, but not both. The mapping type is retained across fork().”

https://pubs.opengroup.org/onlinepubs/7908799/xsh/mmap.html
Comment 4 Tadeu Zagallo 2019-03-01 00:47:03 PST
Yes, you're right, I came across the same thing and was about to comment too.

Either way, I don't think we should keep the file locked for updates, since that would require an exclusive lock on the file, so only one process would be able to use the cache at a time. I think we should keep the shared lock during the script execution and only acquire the exclusive lock when we want to write to the cache.
Comment 5 Saam Barati 2019-03-04 14:02:40 PST
(In reply to Tadeu Zagallo from comment #4)
> Yes, you're right, I came across the same thing and was about to comment too.
> 
> Either way, I don't think we should keep the file locked for updates, since
> that would require an exclusive lock on the file, so only one process would
> be able to use the cache at a time. I think we should keep the shared lock
> during the script execution and only acquire the exclusive lock when we want
> to write to the cache.

I think it makes more sense to make the cache file locked to a single VM at a time. It just simplifies thinking about two VMs concurrently iteratively updating it. That use will not be something we need to support. We could relax this in the future, but I think we should start with the more constrained semantics and only open it up once we're sure we can support such use cases.
Comment 6 Saam Barati 2019-03-04 16:09:39 PST
Created attachment 363564 [details]
patch
Comment 7 Tadeu Zagallo 2019-03-05 09:39:02 PST
Comment on attachment 363564 [details]
patch

View in context: https://bugs.webkit.org/attachment.cgi?id=363564&action=review

Nice, I agree this makes sense for now.

> Source/JavaScriptCore/API/JSScript.mm:190
> +        int fd = open([m_cachePath path].UTF8String, O_CREAT | O_RDWR, 0666);

Not this diff, but you can combine the locking with open by doing 

    open([m_cachePath path].UTF8String, O_CREAT | O_RDWR | O_EXLOCK | O_NONBLOCK, 0666)

I did it somewhere else, but forgot it here.
Comment 8 Saam Barati 2019-03-05 11:22:10 PST
Comment on attachment 363564 [details]
patch

View in context: https://bugs.webkit.org/attachment.cgi?id=363564&action=review

>> Source/JavaScriptCore/API/JSScript.mm:190
>> +        int fd = open([m_cachePath path].UTF8String, O_CREAT | O_RDWR, 0666);
> 
> Not this diff, but you can combine the locking with open by doing 
> 
>     open([m_cachePath path].UTF8String, O_CREAT | O_RDWR | O_EXLOCK | O_NONBLOCK, 0666)
> 
> I did it somewhere else, but forgot it here.

Nice. I'll fix.
Comment 9 Keith Miller 2019-03-06 15:15:48 PST
Comment on attachment 363564 [details]
patch

View in context: https://bugs.webkit.org/attachment.cgi?id=363564&action=review

r=me.

> Source/JavaScriptCore/API/JSScript.mm:54
> -    UniquedStringImpl* m_moduleKey;
> +    int m_cacheFileDescriptor;

Can you use C++ initializers here? e.g. { -1 }
Comment 10 Saam Barati 2019-03-06 16:59:20 PST
Comment on attachment 363564 [details]
patch

View in context: https://bugs.webkit.org/attachment.cgi?id=363564&action=review

>> Source/JavaScriptCore/API/JSScript.mm:54
>> +    int m_cacheFileDescriptor;
> 
> Can you use C++ initializers here? e.g. { -1 }

Doesn't seem like we can. This is a compile error.
Comment 11 Saam Barati 2019-03-06 17:04:29 PST
Created attachment 363821 [details]
patch for landing
Comment 12 WebKit Commit Bot 2019-03-06 18:03:45 PST
Comment on attachment 363821 [details]
patch for landing

Clearing flags on attachment: 363821

Committed r242585: <https://trac.webkit.org/changeset/242585>
Comment 13 WebKit Commit Bot 2019-03-06 18:03:47 PST
All reviewed patches have been landed.  Closing bug.
Comment 14 Radar WebKit Bug Importer 2019-03-06 19:07:34 PST
<rdar://problem/48662679>