Skip to contentSkip to conversation

PATCH: allow percentile size of tmpfs (2.5.66 / 2.4.20-pre2)

PATCH CaT 3 participants 6 messages Apr 1, 2003

Subsystems (2)
lore
Share & tools
lore

CaT

author
#0 lore raw

I believe the patch below will apply to both the above (I know it does to 2.5.66 and 2.4.20-pre2 mm/shmem.c does not look any different so it should be fine. :)

Anyways, what this patch does is allow you to specify the max amount of memory tmpfs can use as a percentage of available real ram. This (in my eyes) is useful so that you do not have to remember to change the setting if you want something other then 50% and some of your ram does (and you can't replacew it immediately).

Usage of this option is as follows:

tmpfs /dev/shm tmpfs rw,size=63%,noauto 0 0

This is taken from my working system and sets the tmpfs size to 63% of my real RAM (256MB). The end result is:

Filesystem 1k-blocks Used Available Use% Mounted on
/dev/shm/tmp 160868 6776 154092 5% /tmp

I've also tested remounting to silly values (and sane ones) and it all works fine with no oopses or freezes and the correct values appearing in df.

All up I feel safer using this then a hard value.

Please apply. :)

Signature · 5 lines
"Other countries of course, bear the same risk. But there's no doubt his
hatred is mainly directed at us. After all this is the guy who tried to
kill my dad."
        - George W. Bush Jr, Leader of the United States Regime
          September 26, 2002 (from a political fundraiser in Houston, Texas)
mm/shmem.c renamed from mm/shmem.c.old +6 -0
@@ -1630,6 +1630,12 @@
 		if (!strcmp(this_char,"size")) {
 			unsigned long long size;
 			size = memparse(value,&rest);
+			if (*rest == '%') {
+				struct sysinfo si;
+				si_meminfo(&si);
+				size = (si.totalram << PAGE_CACHE_SHIFT) / 100 * size;
+				rest++;
+			}
 			if (*rest)
 				goto bad_val;
 			*blocks = size >> PAGE_CACHE_SHIFT;
#1 lore raw
                        size = memparse(value,&rest);
+                       if (*rest == '%') {
+                               struct sysinfo si;
+                               si_meminfo(&si);
+                               size = (si.totalram << PAGE_CACHE_SHIFT) / 100 * size;

(si.totalram << PAGE_CACHE_SHIFT) * size / 100;
would have been better precision-wise.

Xav

#2 lore raw
Quoted 8 lines · Xavier Bestel
                        size = memparse(value,&rest);
mm/shmem.c renamed from mm/shmem.c.old +4 -0
+                       if (*rest == '%') {
+                               struct sysinfo si;
+                               si_meminfo(&si);
+                               size = (si.totalram << PAGE_CACHE_SHIFT) / 100 * size;

(si.totalram << PAGE_CACHE_SHIFT) * size / 100;
would have been better precision-wise.

Hardly, it'll overflow in even more cases
than CaT's (si.totalram << PAGE_CACHE_SHIFT).
I'll take a look at this later, not right now.

Hugh

author
#3 lore raw
Quoted 7 lines · Hugh Dickins
Quoted 6 lines · Xavier Bestel
                        size = memparse(value,&rest);
mm/shmem.c renamed from mm/shmem.c.old +4 -0
+                       if (*rest == '%') {
+                               struct sysinfo si;
+                               si_meminfo(&si);
+                               size = (si.totalram << PAGE_CACHE_SHIFT) / 100 * size;
Quoted 5 lines
Quoted 2 lines

(si.totalram << PAGE_CACHE_SHIFT) * size / 100;
would have been better precision-wise.

Hardly, it'll overflow in even more cases
than CaT's (si.totalram << PAGE_CACHE_SHIFT).

Yes. I had it initially as Xavier suggested but after thinking about it a bit I felt that making the value smaller and -then- bigger was safer.

Quoted 1 line

I'll take a look at this later, not right now.

It is still an unsigned long long int so (AFAIK) it wont overflow till it hits 18,446,744,073,709,551,615. Now... if you have that much ram... wow! :)

Basically it'll only overflow for astoundingly stupid values of size and when it comes down to it, I think you could still make it overflow by setting a REALLY large static value for size anyways (liek greater then the above). Same issue as far as I can see.

Signature · 5 lines
"Other countries of course, bear the same risk. But there's no doubt his
hatred is mainly directed at us. After all this is the guy who tried to
kill my dad."
        - George W. Bush Jr, Leader of the United States Regime
          September 26, 2002 (from a political fundraiser in Houston, Texas)
#4 lore raw
Quoted 4 lines · CaT
Quoted 3 lines

Hardly, it'll overflow in even more cases
than CaT's (si.totalram << PAGE_CACHE_SHIFT).

Quoted 8 lines

Yes. I had it initially as Xavier suggested but after thinking about it a bit I felt that making the value smaller and -then- bigger was safer.

Quoted 1 line

I'll take a look at this later, not right now.

It is still an unsigned long long int so (AFAIK) it wont overflow till it hits 18,446,744,073,709,551,615. Now... if you have that much ram... wow! :)

There's plenty of room in unsigned long long size, yes, but si.totalram is only an unsigned long, so the arithmetic as you have it starts out overflowing an unsigned long.

I don't know yet what it should say: RH2.96-110 is getting confused by the do_div(size, 100) I have there (to respect Xavier's point), and this is definitely _not_ worth adding a compiler dependency for.

Hugh

author
#5 lore raw
Quoted 3 lines · Hugh Dickins

There's plenty of room in unsigned long long size, yes, but si.totalram is only an unsigned long, so the arithmetic as you have it starts out overflowing an unsigned long.

Ahh yes.

Quoted 3 lines

I don't know yet what it should say: RH2.96-110 is getting confused by the do_div(size, 100) I have there (to respect Xavier's point), and this is definitely _not_ worth adding a compiler dependency for.

Would a cast be bad? ie:

mm/shmem.c renamed from mm/shmem.c.old +6 -0
@@ -1630,6 +1630,12 @@
 		if (!strcmp(this_char,"size")) {
 			unsigned long long size;
 			size = memparse(value,&rest);
+			if (*rest == '%') {
+				struct sysinfo si;
+				si_meminfo(&si);
+				size = ((unsigned long long)si.totalram << PAGE_CACHE_SHIFT) / 100 * size;
+				rest++;
+			}
 			if (*rest)
 				goto bad_val;
 			*blocks = size >> PAGE_CACHE_SHIFT;
Signature · 5 lines
"Other countries of course, bear the same risk. But there's no doubt his
hatred is mainly directed at us. After all this is the guy who tried to
kill my dad."
        - George W. Bush Jr, Leader of the United States Regime
          September 26, 2002 (from a political fundraiser in Houston, Texas)